// JavaScript Document
	$(document).ready(function(){
	
	var badDomains = ["Hotmail","Yahoo","Gmail","Googlemail","Aol","Lycos","msn","wanadoo","blueyonder","virgin","freeserve","go2","o2","freemail","wp","tlen","btinternet","ntlworld","webmail","btconnect","rediffmail","mail2hungary","mail2world","caramail","bermuda","pookmail","inbox","yandex","narod","viszme","xglxpg"];
	
	function decode(obj) {
	        return decodeURIComponent(obj.replace(/\+/g,  " "));
        }

        //Pre-populate email address
	if(getParameterByName("e") !="")
	{
		$("#frmEmail").val( getParameterByName("e") );
		$("#q2028_q6").val( getParameterByName("e") );
		$("#q2646_q6").val( getParameterByName("e") );
	}
	
	//Pre-populate email address
	if(getParameterByName("p") !="")
	{
		$("#frmPhone").val( decode( getParameterByName("p") ) );
	}

	//Pre-populate name
	if(getParameterByName("n") !="")
	{
		$("#frmName").val( decode( getParameterByName("n") ) );
	}

	//Newsletter Submit Form
	$("#submit-standard").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmNameVal = $("#frmName").val();
		if(frmNameVal == '') {
			errorMsg+=' - You forgot to enter your name.\n';
			hasError = true;
		}

		var frmOrganisationVal = $("#frmOrganisation").val();
		if(frmOrganisationVal == '') {
			errorMsg+=' - You forgot to enter the name of your organisation.\n';
			hasError = true;
		}

		var frmEmailVal = $("#frmEmail").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		var frmPhoneVal = $("#frmPhone").val();
		if(frmPhoneVal == '') {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		var frmBusinessTypeVal = $("#frmBusinessType").val();
		if(frmBusinessTypeVal == '- please select -') {
			errorMsg+=' - You forgot to us how many emails you send each month.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});
	
	//Free Trial Submit Form (checks email)
	$("#submit-validate-email").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmNameVal = $("#frmName").val();
		if(frmNameVal == '') {
			errorMsg+=' - You forgot to enter your name.\n';
			hasError = true;
		}

		var frmOrganisationVal = $("#frmOrganisation").val();
		if(frmOrganisationVal == '') {
			errorMsg+=' - You forgot to enter the name of your organisation.\n';
			hasError = true;
		}

		var frmEmailVal = $("#frmEmail").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		for(var x = 0;x < badDomains.length;x++)
		{
			if(frmEmailVal.indexOf('@' + badDomains[x]) > 0)
			{
				errorMsg+=' - Sorry, we can\'t accept submissions that use an email address supplied by an ISP.\n';
				hasError = true;					
			}
		}

                var frmPhoneVal = $("#frmPhone").val();
		if(frmPhoneVal == '') {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		var frmBusinessTypeVal = $("#frmBusinessType").val();
		if(frmBusinessTypeVal == '- please select -') {
			errorMsg+=' - You forgot to us how many emails you send each month.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});
	
	//Free Trial AdWords Form - validate domain
	$("#form_email_2028_submit").click(function(){
                var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmNameVal = $("#q2028_q1").val();
		if(frmNameVal == '') {
			errorMsg+=' - You forgot to enter your name.\n';
			hasError = true;
		}

		var frmOrganisationVal = $("#q2028_q1").val();
		if(frmOrganisationVal == '') {
			errorMsg+=' - You forgot to enter the name of your organisation.\n';
			hasError = true;
		}

		var frmEmailVal = $("#q2028_q6").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		for(var x = 0;x < badDomains.length;x++)
		{
			if(frmEmailVal.indexOf('@' + badDomains[x]) > 0)
			{
				errorMsg+=' - Sorry, we can\'t accept submissions that use an email address supplied by an ISP.\n';
				hasError = true;					
			}
		}
		
		var frmPhoneVal = $("#q2028_q4").val();
		if(frmPhoneVal == '') {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		var frmBusinessTypeVal = $("#q2028_q5").val();
		if(frmBusinessTypeVal == '- please select -') {
			errorMsg+=' - You forgot to us how many emails you send each month.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});


	//Request Pricelist Form
	$("#form_email_2646_submit").click(function(){
                var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmNameVal = $("#q2646_q1").val();
		if(frmNameVal == '') {
			errorMsg+=' - You forgot to enter your name.\n';
			hasError = true;
		}

		var frmOrganisationVal = $("#q2646_q1").val();
		if(frmOrganisationVal == '') {
			errorMsg+=' - You forgot to enter the name of your organisation.\n';
			hasError = true;
		}

		var frmEmailVal = $("#q2646_q6").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
				
		var frmPhoneVal = $("#q2646_q4").val();
		if(frmPhoneVal == '') {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		var frmBusinessTypeVal = $("#q2646_q5").val();
		if(frmBusinessTypeVal == '- please select -') {
			errorMsg+=' - You forgot to us how many emails you send each month.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});


        /* CTA Newsletter */
	$("#form_email_2685_submit").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmEmailVal = $("#q2685_q6").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});

/* CTA Call */
	$("#form_email_2727_submit").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmPhoneVal = $("#q2727_q6").val();
		if(frmPhoneVal == '' || frmPhoneVal == 'your phone number...' ) {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});

/* CTA Trial*/
	$("#form_email_2700_submit").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmEmailVal = $("#q2700_q6").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		for(var x = 0;x < badDomains.length;x++)
		{
			if(frmEmailVal.indexOf('@' + badDomains[x]) > 0)
			{
				errorMsg+=' - Sorry, we can\'t accept submissions that use an email address supplied by an ISP.\n';
				hasError = true;					
			}
		}

                //Submit the form
		if(hasError == true) {
			alert(errorMsg);			
			return false;
		} else {
			return true;
		}
	});


$("#submit-call-me").click(function(){
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var errorMsg = "Your form contained the following errors:\n"

		var frmNameVal = $("#frmName").val();
		if(frmNameVal == '') {
			errorMsg+=' - You forgot to enter your name.\n';
			hasError = true;
		}

		var frmOrganisationVal = $("#frmOrganisation").val();
		if(frmOrganisationVal == '') {
			errorMsg+=' - You forgot to enter the name of your organisation.\n';
			hasError = true;
		}

		var frmEmailVal = $("#frmEmail").val();
		if(frmEmailVal == '') {
			errorMsg+=' - You forgot to enter your email address.\n';
			hasError = true;
		} else if(!emailReg.test(frmEmailVal)) {	
			errorMsg+=' - You didn\'t enter a valid email address.\n';
			hasError = true;
		}
		
		var frmPhoneVal = $("#frmPhone").val();
		if(frmPhoneVal == '') {
			errorMsg+=' - You forgot to enter your phone number.\n';
			hasError = true;
		}

		var frmBusinessTypeVal = $("#frmBusinessType").val();
		if(frmBusinessTypeVal == '- please select -') {
			errorMsg+=' - You forgot to us how many emails you send each month.\n';
			hasError = true;
		}
		
		//Submit the form
		if(hasError == true) {
			alert(errorMsg);			
		} else {
			$("#display-form").hide();
			$("#display-message").show();
			$("#formcontent").load("http://forms.pure360.com/_act/web_capture_call_me.php", {errorPage:'redirect: http://www.pure360.com?call-me-error', sequenceNum: '1', captureSourceDesc: 'call me / quote me form', name: frmNameVal, organisation: frmOrganisationVal, email: frmEmailVal, phone: frmPhoneVal, businessType: frmBusinessTypeVal}, function(){
    		alert($("#formcontent").html().length);
			if($("#formcontent").html().length == 0)
			{
				//Something went wrong, redirect
				window.location.replace("http://www.pure360.com/call-me-error");
			} else {
				//All good, redirect
				window.location.replace("http://www.pure360.com/call-me-thanks");
			}
			
	    	});		
		}
	});
  });
	
function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
