function validateform(formname) {
	var sFocus = "";
	var sAlert = "";

	//Basic contact details, easy validation...
	//=========================================
	if(trim(formname.firstname.value) == "") {
		sAlert += "Please complete first name.<br/>";
		if(sFocus == "") sFocus = "firstname";
	}
	if(trim(formname.lastname.value) == "") {
		sAlert += "Please complete surname.<br/>";
		if(sFocus == "") sFocus = "lastname";
	}
	if(trim(formname.email.value) == "") {
		sAlert += "Please complete email address.<br/>";
		if(sFocus == "") sFocus = "email";
	} else if(!isValidEmail(trim(formname.email.value))) {
		sAlert += "Please complete a valid email address.<br/>";
		if(sFocus == "") sFocus = "email";
	}
	if(trim(formname.telephone.value) == "") {
		sAlert += "Please complete telephone number.<br/>";
		if(sFocus == "") sFocus = "telephone";
	}

	//Validate question answers
	//=========================
	//q1: How many Bank Accounts...
	if(trim(formname.accountnos.value) != "") {
		if(!isNumeric(trim(formname.accountnos.value))) {
			sAlert+="Please enter a numeric value in the number of accounts field.<br/>";
			if(sFocus == "") sFocus = "accountnos";
		}
	}

	//q3: If you have taken out a loan or Credit Card...
	if(document.getElementById("reclaimyes").checked == true) {
		if (document.getElementById("no_policies") != null) {
			if (trim(formname.no_policies.value) != "") {
				if(!isNumeric(trim(formname.no_policies.value))) {
					sAlert+="Please enter a numeric value in the number of policies field.<br/>";
					if(sFocus == "") sFocus = "no_policies";
				}
			} else {
	            		sAlert+="Please enter a value in the number of policies field.<br/>";
				if(sFocus == "") sFocus = "no_policies";				
			}
		}
	}

	//q7: Would you like to receive a FREE prepaid mastercard...
	/*if(formname.mastercard[0].checked) {
		if(formname.DOBday.value == "") {
			sAlert+="Please complete your day of birth.<br/>";
			if(sFocus == "") sFocus = "DOBday";
		}
		if (formname.DOBmonth.value == "") {							
			sAlert+="Please complete your month of birth.<br/>";
			if(sFocus == "") sFocus = "DOBmonth";
		}
		if (formname.DOByear.value == "") {							
			sAlert+="Please complete your year of birth.<br/>";
			if(sFocus == "") sFocus = "DOByear";
		}
		var accPassword = trim(formname.accpassword.value);
		if(accPassword == "") {
			sAlert+="Please enter your account password.<br/>";
			if(sFocus == "") sFocus = "accpassword";
		} else if(accPassword.length < 5 || accPassword.length > 10) {
			sAlert+="Account Password must be in between 5 and 10 characters.<br/>";
			if(sFocus == "") sFocus = "accpassword";
		}
		var secCode = trim(formname.securitycode.value);
		if(secCode == "") {							
			sAlert+="Please enter your security code.<br/>";
			if(sFocus == "") sFocus = "securitycode";
		} else if(secCode.length != 4) {							
			sAlert+="Security code must be four digits.<br/>";
			if(sFocus == "") sFocus = "securitycode";
		}
	}*/

	if (sAlert != "") {
		document.getElementById("validation-summary").innerHTML = sAlert;
		if(formname.elements[sFocus]) formname.elements[sFocus].focus();
		else if(formname.elements[sFocus][0]) formname.elements[sFocus][0].focus();
		return false;
	} else {
		//E-mail redirection based on question answers here:
		var defaultEmailAddress = "ic@debtadvisoryline.co.uk";
		formname.emailto1.value = (document.getElementById("chargedyes").checked) ? "bankaccounts@debtadvisoryline.co.uk" : defaultEmailAddress;
		formname.emailto2.value = (document.getElementById("reclaimyes").checked) ? "brwebppi@debtadvisoryline.co.uk" : defaultEmailAddress;
		formname.emailto3.value = (document.getElementById("mortgageyes").checked) ? "bripi@debtadvisoryline.co.uk" : defaultEmailAddress;
		formname.emailto4.value = (document.getElementById("debtyes").checked) ? "weblead@debtadvisoryline.co.uk" : defaultEmailAddress;
		formname.emailto5.value = (document.getElementById("billsyes").checked) ? "brli@debtadvisoryline.co.uk" : defaultEmailAddress;
		formname.emailto6.value = (document.getElementById("mastercardyes").checked) ? "brmal@debtadvisoryline.co.uk" : defaultEmailAddress;
		
		//DC Storm Tracking Code
		var dcEmail = formname.email.value;
        saleTrack.addSaleItem (1, 0, 'Can You Claim', dcEmail);
        saleTrack.logSale(1);
		
		formname.action = "/do-process-questions.aspx";
		return true;
	}
}

function showoptions(formname) {
	if(formname.mastercard[0].checked) {
		document.getElementById("cardQuestions").style.display = "block";
	} else {
		document.getElementById("cardQuestions").style.display = "none"
	}
}

function showpolicies() {
    if (document.getElementById("reclaimyes").checked == true){
        document.getElementById("no_policies_span").style.display = "block";
    } else {
        document.getElementById("no_policies_span").style.display = "none";
    }
}
//=====================================


//PRIVATE FUNCS (IF YOU WILL)
//=====================================
function isValidEmail(email) {
	//Regex e-mail check; might be some instances where this won't match, but highly unlikely
	return email.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/);
}

function isNumeric(value) {
	//REALLY basic 'isNaN' type functionality
	var result = true;
	var numericChars = "0123456789";
	var Char;
	for(i=0;i< value.length && result == true;i++) {
		thisChar = value.charAt(i);
		if (numericChars.indexOf(thisChar) == -1) {
			result = false;
		}
	}
	return result;
}

function trim(string_to_trim) {
	//Trims whitespace from start and end of value
	var l = 0; 
	var r = string_to_trim.length -1;
	while(l < string_to_trim.length && string_to_trim[l] == ' ') l++;
	while(r > l && string_to_trim[r] == ' ') r-=1;
	return string_to_trim.substring(l, r+1);
}
//=====================================