$(function(){

var email = $("input#email").val();
	if(isValidEmailAddress(email)) {
		$("input#email").after("hooray!");
	} else {
		$("input#email").addClass("yourerror").focus();
		$("input#email").after("<label class='error'>Email is not valid!</label>");
		return false;
	}
});

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}

function validate_SF(){
	if (!$("input[name='submitted[cps1_payment_terms]']:checked").val()){
		alert("Please answer the question No 1");
		scroll(0,0);
		return false;
	}

	if (!$("input[name='submitted[cps1_discount]']:checked").val()){
		alert("Please answer the question No 2");
		scroll(0,0);
		return false;
	}

	if (!$("input[name='submitted[cps1_payment_term2]']:checked").val()){
		alert("Please answer the question No 3");
		scroll(0,0);
		return false;
	}

	if (!$("input[name='submitted[cps1_days]']:checked").val()){
		alert("Please answer the question No 4");
		return false;
	}

	if (!$("input[name='submitted[cps1_effort]']:checked").val()){
		alert("Please answer the question No 5");
		return false;
	}

	if (!$("input[name='submitted[cps1_overdue]']:checked").val()){
		alert("Please answer the question No 6");
		return false;
	}

	if (!$("input[name='submitted[cps1_insurance]']:checked").val()){
		alert("Please answer the question No 7");
		return false;
	}

	if (!$("input[name='submitted[cps1_crunch]']:checked").val()){
		alert("Please answer the question No 8");
		return false;
	}

	if (!$("input[name='submitted[cps1_loosing]']:checked").val()){
		alert("Please answer the question No 9");
		return false;
	}

	if (!$("input[name='submitted[cps1_service]']:checked").val()){
		alert("Please answer the question No 10");
		return false;
	}

	if (!$("input[name='submitted[cps1_controlpay]']:checked").val()){
		alert("Please answer the question No 11");
		return false;
	}

	if (!$("input[name='submitted[cps1_fee]']:checked").val()){
		alert("Please answer the question No 12");
		return false;
	}

	if (!$("input[name='submitted[cps1_fee2]']:checked").val()){
		alert("Please answer the question No 13");
		return false;
	}

	if (!$("input[name='submitted[cps1_contact]']:checked").val()){
		alert("Please answer the question No 14");
		return false;
	}

	if (contact_on){
		if (!$("#edit-submitted-contact-title").val()){
			$("#edit-submitted-contact-title").select();
			alert("Please select Salutation");
			return false;
		}
	
		if (!$("input[name='submitted[contact][first_name]']").val()){
			$("input[name='submitted[contact][first_name]']").select();
			alert("Please fill First name field");
			return false;
		}
	
		if (!$("input[name='submitted[contact][last_name]']").val()){
			$("input[name='submitted[contact][last_name]']").select();
			alert("Please fill Last name field");
			return false;
		}
	
		if (!$("input[name='submitted[contact][company]']").val()){
			$("input[name='submitted[contact][company]']").select();
			alert("Please fill Company field");
			return false;
		}
	
		if (!$("input[name='submitted[contact][position]']").val()){
			$("input[name='submitted[contact][position]']").select();
			alert("Please fill Position field");
			return false;
		}
	
		if (!isValidEmailAddress($("input[name='submitted[contact][e_mail]']").val())){
			$("input[name='submitted[contact][e_mail]']").select();
			alert("Please provide valid e-mail address");
			return false;
		}
	
	}

	if (!$("input[name='submitted[disclaimer][Responses to the ControlPay survey are considered confidential and therefore individual responses will not be released, shared, or published. In participating in this survey, the participant recognizes that the information provided in this survey will be used in a Freight Receivable Finance aggregate report and therefore grants ControlPay unrestricted use of this information.]']:checked").val()){
		alert("Please confirm the Disclaimer");
		return false;
	}

	return true
}

//bkt: Show contact form visualization
var contact_on = false;
function detect_contact(){
	if (!$("input[name='submitted[cps1_contact]']:checked").val()) return;
		
		if ($("input[name='submitted[cps1_contact]']:checked").val() == "do not contact me for this service") {
			if (contact_on == true) {
				$("#webform-component-contact").slideUp("fast");
				contact_on = false;
				$("#edit-submitted-contact-1").val(false);
			}
			else {
				return
			}
		}
		else {
			if (contact_on != true) {
				$("#webform-component-contact").slideDown("fast");
				contact_on = true;
				$("#edit-submitted-contact-1").val(true);
			}
			else {
				return
			}
		}
};

//bkt: Process question 3 function
function validate_q3(){
	var q3_days = "";
	if ($("input[name='submitted[cps1_payment_term2]']:checked").val()) {
		if ($("input[name='submitted[cps1_payment_term2]']:checked").val() == "1-2 days") q3_days = "1-2";
		if ($("input[name='submitted[cps1_payment_term2]']:checked").val() == "2-7 days") q3_days = "2-7";
		if ($("input[name='submitted[cps1_payment_term2]']:checked").val() == "8-14 days") q3_days = "8-14";
		if ($("input[name='submitted[cps1_payment_term2]']:checked").val() == "14-30 days") q3_days = "14-30";
		if ($("input[name='submitted[cps1_payment_term2]']:checked").val() == "more than 30 days") q3_days = "30";
	}
	
	if (q3_days) q3_days = " within " + q3_days + " days"
	
	$("#webform-component-cps1_controlpay > .form-item > label").html('11. Would you be interested for ControlPay to take over the customer payment towards you' + q3_days + '?: <span class="form-required" title="This field is required.">*</span>');
	$("#webform-component-cps1_fee > .form-item > label").html('12. Which fee are you prepared to pay, in order to get 80% of your invoice worth for example &euro;1.000,- paid' + q3_days + '?: <span class="form-required" title="This field is required.">*</span>');
	$("#webform-component-cps1_fee2 > .form-item > label").html('13. Which fee are you prepared to pay, in order to get 100% of your invoice worth for example &euro;1.000,- paid' + q3_days + '?: <span class="form-required" title="This field is required.">*</span>');

};

$("#webform-client-form-41").submit(function() {
	return validate_SF();
})

$("#webform-component-cps1_payment_term2").click(function() {
	validate_q3()
});

$("#webform-component-cps1_contact").click(function() {
	detect_contact()
});

validate_q3()
detect_contact()