function checkOption(id){
	if(document.getElementById(id).checked==true){
		document.getElementById(id).checked=false;
	}else{
		document.getElementById(id).checked=true;
	}
}

function FRVal(){
	if(document.forms.selReg.reg.value == ""){
		document.getElementById("FRError").innerHTML="Error: Please enter your registration!";
	}else{
    	var regvalue = document.forms.selReg.reg.value;
		regvalue = regvalue.replace(" ","");
		document.forms.selReg.reg.value = regvalue.toUpperCase();
		document.forms.selReg.submit();
	}
}

function GQVal(){

	var errortext = "";
	document.forms.serviceQuote.yreg.value = document.forms.selReg.reg.value;

	if(document.forms.serviceQuote.yreg.value == ""){
		errortext += "<li>Vehicle Registration was blank</li>";
	}
	if(document.forms.serviceQuote.ymake.value==""){
		errortext += "<li>Vehicle Make was blank</li>";
	}
    if(document.forms.serviceQuote.ymodel.value==""){
		errortext += "<li>Vehicle Model was blank</li>";
	}
    if(document.forms.serviceQuote.yderiv.value==""){
		errortext += "<li>Vehicle Derivative was blank</li>";
	}
    if(document.forms.serviceQuote.ycc.value==""){
		errortext += "<li>Vehicle Engine CC was blank</li>";
	}
    if(document.forms.serviceQuote.yyear.value==""){
		errortext += "<li>Vehicle Year was blank</li>";
	}

	if(errortext == ""){
		document.forms.serviceQuote.submit();
	}else{
		var mainerrortext = "<span style='color:#000000; font-weight:bold;'>Please amend the following errors in order to find your available VXR Services and Upgrades...</span>";
		mainerrortext += "<ul>"+errortext+"</ul>";
		document.getElementById("GQError").innerHTML = mainerrortext;
		document.getElementById("GQError").style.display = "";
	}
}

function optionVal(){
	var opSelected = false;
	if(document.forms.selOptions.optionval.length == undefined){
        if(document.forms.selOptions.optionval.checked == true){
			opSelected = true;
		}
	}else{
		for(var i=0;i<document.forms.selOptions.optionval.length;i++){
			if(document.forms.selOptions.optionval[i].checked == true){
				opSelected = true;
			}
		}
	}

	if(opSelected == true){
        document.forms.selOptions.optionids.value=",";
		if(document.forms.selOptions.optionval.length == undefined){
			document.forms.selOptions.optionids.value+=document.forms.selOptions.optionval.value+",";
		}else{
			for(var i=0;i<document.forms.selOptions.optionval.length;i++){
				if(document.forms.selOptions.optionval[i].checked==true){
					document.forms.selOptions.optionids.value+=document.forms.selOptions.optionval[i].value+",";
				}
			}
		}
		document.forms.selOptions.submit();
	}else{
        var mainerrortext = "<span style='color:#000000; font-weight:bold;'>Please amend the following errors in order to obtain your VXR Upgrade Quote...</span>";
		mainerrortext += "<li>You must select at least one upgrade option</li>";
		document.getElementById("OpError").innerHTML = mainerrortext;
		document.getElementById("OpError").style.display = "";
	}
}

function formatPostcode(pvalue){
	pvalue = pvalue.replace(" ","");
	pvalue = pvalue.toLowerCase();
	return pvalue;
}

function getDir(){
	var almess = "";
	if(document.forms.upgradeSubmit.pcode.value=="" && document.forms.upgradeSubmit.prefbranch.value==""){
		almess = "To get directions please enter your postcode and select your preferred branch";
	}else if(document.forms.upgradeSubmit.pcode.value==""){
		almess = "To get directions please enter your postcode";
	}else if(document.forms.upgradeSubmit.prefbranch.value==""){
		almess = "To get directions please select your preferred branch";
	}

	if(almess == ""){
		var startpcode = document.forms.upgradeSubmit.pcode.value;
		var endpcode = document.forms.upgradeSubmit.branchpcode.value;
		window.open("http://maps.google.co.uk/maps?f=d&hl=en&geocode=&saddr="+formatPostcode(startpcode)+"&daddr="+formatPostcode(endpcode));
	}else{
		window.alert(almess);
	}
}

function changeBPcode(){
	var selVal;
	if(document.forms.upgradeSubmit.prefbranch.value == "Pentagon VXR Centre - Burton"){
		selVal = "DE130BJ";
	}else if(document.forms.upgradeSubmit.prefbranch.value == "Pentagon VXR Centre - Rochdale"){
		selVal = "OL161TR";
	}else{
		selVal = "";
	}
	document.forms.upgradeSubmit.branchpcode.value = selVal;
}

function SBVal(){

	var errortext = "";

       function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
	   		return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    	return false
		}

	 	if (str.indexOf(at,(lat+1))!=-1){
	    	return false
	 	}

	 	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    	return false
	 	}

	 	if (str.indexOf(dot,(lat+2))==-1){
	    	return false
	 	}

	 	if (str.indexOf(" ")!=-1){
	    	return false
	 	}

		 	return true
	}

	if(document.forms.upgradeSubmit.title.value == ""){
		errortext += "<li>You did not select your Title</li>";
	}
	if(document.forms.upgradeSubmit.fnme.value==""){
		errortext += "<li>You did not enter your First Name</li>";
	}
       if(document.forms.upgradeSubmit.lnme.value==""){
		errortext += "<li>You did not enter your Last Name</li>";
	}
       if(document.forms.upgradeSubmit.pcode.value==""){
		errortext += "<li>You did not enter your Postcode</li>";
	}
       if(document.forms.upgradeSubmit.aline1.value==""){
		errortext += "<li>You did not enter your Address 1</li>";
	}
       if(document.forms.upgradeSubmit.telephone.value==""){
		errortext += "<li>You did not enter your Telephone Number</li>";
	}
       if(document.forms.upgradeSubmit.email.value==""){
		errortext += "<li>You did not enter your Email Address</li>";
	}else{
		if(echeck(document.forms.upgradeSubmit.email.value)==false){
			errortext += "<li>Your Email Address was not valid</li>";
		}
	}
	if(document.forms.upgradeSubmit.prefbranch.value==""){
		errortext += "<li>You did not select your Preferred Branch</li>";
	}

	if(errortext == ""){
		document.forms.upgradeSubmit.submit();
	}else{
		var mainerrortext = "<span style='color:#000000; font-weight:bold;'>Please amend the following errors in order to submit your Service Booking...</span>";
		mainerrortext += "<ul>"+errortext+"</ul>";
		document.getElementById("SBError").innerHTML = mainerrortext;
		document.getElementById("SBError").style.display = "";
	}
}

function displayhover(area,action){
	if(action == 1){
		document.getElementById(area).style.display="";
	}else{
		document.getElementById(area).style.display="none";
	}
}

function copyFormData(passform){
	var thisform = document.forms[passform];
	var bookingform = document.forms.upgradeSubmit;

	thisform.title.value = bookingform.title.value;
	thisform.fnme.value = bookingform.fnme.value;
	thisform.lnme.value = bookingform.lnme.value;
	thisform.telephone.value = bookingform.telephone.value;
	thisform.mobile.value = bookingform.mobile.value;
	thisform.email.value = bookingform.email.value;
	thisform.prefbranch.value = bookingform.prefbranch.value;
	thisform.prefday.value = bookingform.prefday.value;
	thisform.prefmonth.value = bookingform.prefmonth.value;
	thisform.prefyear.value = bookingform.prefyear.value;
	thisform.preftime.value = bookingform.preftime.value;
	thisform.comments.value = bookingform.comments.value;
}

function discountSubmit(){

	document.forms.discountCodeForm.pcode.value = document.forms.upgradeSubmit.pcode.value;
	document.forms.discountCodeForm.aline1.value = document.forms.upgradeSubmit.aline1.value;
	document.forms.discountCodeForm.aline2.value = document.forms.upgradeSubmit.aline2.value;
	document.forms.discountCodeForm.towncity.value = document.forms.upgradeSubmit.towncity.value;
	document.forms.discountCodeForm.county.value = document.forms.upgradeSubmit.county.value;

	copyFormData("discountCodeForm");
	document.forms.discountCodeForm.submit();
}

function findPostcode(action){
	if(action == 0){
		if(document.forms.upgradeSubmit.pcode.value == ""){
			window.alert("Please enter a Postcode to lookup");
			return false;
		}else{
            document.forms.findPostcodeForm.postcode_find.value = document.forms.upgradeSubmit.pcode.value;
		}
	}else if(action == 1){
           document.forms.findPostcodeForm.address_find.value = document.forms.upgradeSubmit.address_find.value;
	}

	copyFormData("findPostcodeForm");
	document.forms.findPostcodeForm.submit();
}