var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

$(document).ready(function(){

	if ($.cookie('brochure_form')=='OK') {
		$('#brochureform').hide();
		$("#brochuresection").show();
	}
	else {
		$('#brochureform').show();
		$("#brochuresection").hide();
	}
	
	$("#brochuresubmit").click(function(){		// Submit Button ID			   				   
		$('#brochureform').find('label').removeClass('red');
		$("#brochureerror").slideUp();
		
		var hasError = false;
		
		formdata = $("#form1").serialize();
		
		var nameVal 	= $("#b_Name").val();
		var surnameVal 	= $("#b_Surname").val();
		var emailVal 	= $("#b_Email").val();
		
		if(nameVal == '') {
			$("label[for='b_Name']").addClass('red');
			hasError = true;
		}
		if(emailVal == '') {
			$("label[for='b_Email']").addClass('red');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("label[for='b_Email']").addClass('red');
			hasError = true;
		}
		
		
		if(hasError == true) {
			$("#brochureerror").html('Please correct the highlighted fields and try again.').slideDown();
		}
		else {
			$(this).hide();
			var formaction = $("#form1").attr('action');	// Form ID
			var regex = new RegExp("[^0-9]*");
			formaction = formaction.replace(regex,"");
			formaction = 'scripts/submitform.asp?formid=' + formaction;
			
			$("#brochuresubmit").after('<img src="images/loading.gif" alt="" width="16" height="16" id="loading" style="margin:2px 0px 0px 3px;"/> Processing...');

			$.post(formaction,
					formdata,
   					function(data){
						$('#brochureform').hide(1300);
						$("#brochuresection").show(1000);
						$.cookie('brochure_form', 'OK', {expires:10});
					}
			);
			
			// Track Analytics
			pageTracker._trackPageview('/thankyou.html?new-car-brochure');
			
		}
		
		return false;
	});						   
});


//Enquire form

$(document).ready(function(){

	$("#enquirysection").hide();
	
	$("#enquirysubmit").click(function(){		// Submit Button ID			   				   
		$('#enquiryform').find('label').removeClass('red');
		$("#enquiryerror").slideUp();
		
		var hasError = false;
		
		var nameVal 	= $("#e_Name").val();
		var emailVal 	= $("#e_Email").val();
		var phoneVal 	= $("#e_Phone").val();
		
		if(nameVal == '') {
			$("label[for='e_Name']").addClass('red');
			hasError = true;
		} 
		if(phoneVal == '') {
			$("label[for='e_Phone']").addClass('red');
			hasError = true;
		} 
		if(emailVal == '') {
			$("label[for='e_Email']").addClass('red');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("label[for='e_Email']").addClass('red');
			hasError = true;
		}
		
		
		if(hasError == true) {
			$("#enquiryerror").html('Please correct the highlighted fields and try again.').slideDown();
		}
		else {
			$(this).hide();
			
			var formaction = $("#form2").attr('action');	// Form ID
			var regex = new RegExp("[^0-9]*");
			formaction = formaction.replace(regex,"");
			formaction = 'scripts/submitform.asp?formid=' + formaction;	
				
			$("#enquirysubmit").after('<img src="images/loading.gif" alt="" width="16" height="16" id="loading" style="margin:2px 0px 0px 3px;"/> Processing...');		
			
			$.post(formaction,
					$("#form2").serialize(),				// Form ID again
   					function(data){
						$('#enquiryform').hide(1300);
						$("#enquirysection").show(1000);
					}
			);
			
			// Track Analytics
			pageTracker._trackPageview('/thankyou.html?new-car-enquiry');
			
		}
		
		return false;
	});						   
});


//Value My Car form

$(document).ready(function(){

	$("#valuesection").hide();
	
	$("#valuesubmit").click(function(){		// Submit Button ID			   				   
		$('#valueform').find('label').removeClass('red');
		$("#valueerror").slideUp();
		
		var hasError = false;
		
		var nameVal 	= $("#v_Name").val();
		var emailVal 	= $("#v_Email").val();
		var phoneVal 	= $("#v_Phone").val();
		
		if(nameVal == '') {
			$("label[for='v_Name']").addClass('red');
			hasError = true;
		} 
		if(phoneVal == '') {
			$("label[for='v_Phone']").addClass('red');
			hasError = true;
		} 
		if(emailVal == '') {
			$("label[for='v_Email']").addClass('red');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("label[for='v_Email']").addClass('red');
			hasError = true;
		}
		
		if(hasError == true) {
			$("#valueerror").html('Please correct the highlighted fields and try again.').slideDown();
		}
		else {
			$(this).hide();
			
			var formaction = $("#form3").attr('action');	// Form ID
			var regex = new RegExp("[^0-9]*");
			formaction = formaction.replace(regex,"");
			formaction = 'scripts/submitform.asp?formid=' + formaction;
				
			$("#valuesubmit").after('<img src="images/loading.gif" alt="" width="16" height="16" id="loading" style="margin:2px 0px 0px 3px;"/> Processing...');		
			
			$.post(formaction,
					$("#form3").serialize(),				// Form ID again
   					function(data){
						$('#valueform').hide(1300);
						$("#valuesection").show(1000);
					}
			);
			
			// Track Analytics
			pageTracker._trackPageview('/thankyou.html?new-car-valuemycar');
			
		}
		
		return false;
	});						   
});