$(document).ready(function(){

	var theProduct = getUrlVars()["theProduct"];
	//var first = getUrlVars()["id"];
	//var theProduct = $.url().fparam('theProduct');

function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}


    function ConvertText(){

    var textvalue = document.formtest.textest.value;

    if (textvalue.indexOf(" ")){
        var LinkText = textvalue.split(" ");
    }

    if (document.layers) { 
        document.layers.converted.document.write(textvalue); 
        document.layers.converted.document.close(); 
    }else if (document.all) { 
        converted.innerHTML = LinkText[0] + "%20" + LinkText[1]; 
    } 
}



	
	if (theProduct != "") { 
				$('#product').val(theProduct);
	}
	
	//function for contact form dropdown
	function contact() {
		if ($("#getQuoteBox").is(":hidden")){
			$("#getQuoteBox").slideDown("slow");
			//$("#backgroundPopup").css({"opacity": "0.7"});
			//$("#backgroundPopup").fadeIn("slow"); 
		}
		else{
			$("#getQuoteBox").slideUp("slow");
			//$("#backgroundPopup").fadeOut("slow");  
		}
	}
	 
	//run contact form when any contact link is clicked
	$(".contact").click(function(){contact()});


   //submission scripts
  $('.theQuoteForm').submit( function(){
		//statements to validate the form	
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var email = document.getElementById('email');
		if (!filter.test(email.value)) {
			$('.email-missing').show();
		} else {$('.email-missing').hide();}
		if (document.quoteForm.firstName.value == "") {
			$('.firstname-missing').show();
		} else {$('.firstname-missing').hide();}	
		if (document.quoteForm.lastName.value == "") {
			$('.lastname-missing').show();
		} else {$('.lastname-missing').hide();}
		if (document.quoteForm.company.value == "") {
			$('.company-missing').show();
		} else {$('.company-missing').hide();}
		if (document.quoteForm.phone.value == "") {
			$('.phone-missing').show();
		} else {$('.phone-missing').hide();}
		if (document.quoteForm.message.value == "") {
			$('.message-missing').show();
		} else {$('.message-missing').hide();}		
		if ((document.quoteForm.firstName.value == "") || (document.quoteForm.lastName.value == "") || (document.quoteForm.company.value == "") || (!filter.test(email.value)) || (document.quoteForm.phone.value == "") || (document.quoteForm.message.value == "")){
			return false;
		} 
		
		if ((document.quoteForm.firstName.value != "") && (document.quoteForm.lastName.value != "") && (document.quoteForm.company.value != "") && (filter.test(email.value)) && (document.quoteForm.phone.value != "") && (document.quoteForm.message.value != "")) {
			//hide the form
			$('.quoteForm').hide();
		
			//show the loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//send the ajax request
			$.post('/theme/comlight/contactform/mail.php',{firstName:$('#firstName').val(),
							  lastName:$('#lastName').val(),
							  email:$('#email').val(),
							  phone:$('#phone').val(),
							  company:$('#company').val(),
							  product:$('#product').val(),
							  thepage:$('#thepage').val(),
							  message:$('#message').val()},
		
			//return the data
			function(data){
			  //hide the graphic
			  $('.bar').css({display:'none'});
			  $('.loader').append(data);
			});
			
			//waits 2000, then closes the form and fades out
			setTimeout('$("#quoteForm").slideUp("slow")', 500);
			
			//stay on the page
			return false;
		} 
  });  
});
