// Validation

function validate() {
	
	var txtForenames = document.getElementById("txtForenames");
	var txtSurname = document.getElementById("txtSurname");
	var txtHometelno = document.getElementById("txtHometelno");
	var txtMobiletelno = document.getElementById("txtMobiletelno");
	var txtEmail = document.getElementById("txtEmail");
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");

	
	if (chkPrivacyPolicy.checked==true && vrule_name(txtForenames.value) && vrule_name(txtSurname.value) && (vrule_telephone(txtHometelno.value) || vrule_telephone(txtMobiletelno.value)) && vrule_email(txtEmail.value))  {
		// tidy up the field content before submitting
		document.getElementById("txtHometelno").value = vtidy_telephone( txtHometelno.value );
		document.getElementById("txtMobiletelno").value = vtidy_telephone( txtMobiletelno.value );
		if(txtEmail.value == "Email"){
			document.getElementById("txtEmail").value  = ""
		}
		if(txtMobiletelno.value == "Mobile"){
			document.getElementById("txtMobiletelno").value  = ""
		}
		return true;	
	} else {
		var msg = "The following fields must be completed correctly before we can process your call back request\n\n";
		msg += (!vrule_name(txtForenames.value))?" - First name\n":"";
		msg += (!vrule_name(txtSurname.value))?" - Surname\n":"";
		msg += (!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value))?" - Telephone or Mobile number\n":"";
		msg += (!vrule_email(txtEmail.value))?" - A valid email address\n":"";
		msg += (!chkPrivacyPolicy.checked==true)?" - Agree to our Privacy Policy\n":"";
		msg += "\nPlease supply these missing details and then click on the 'Apply' button";
		alert(msg);	
		return false;
	}
}

function vrule_telephone( value ) {
	
	value = vtidy_telephone( value );
	
	telNoPattern = /^\d{11}$/;
	return telNoPattern.test(value);
}

function vtidy_telephone( value ) {
	// strip out any spaces and other junk
	return value.replace( /[^0-9]/g, "");
}

function vrule_name( value ) {
	
	if (value.length == 0 || value == "First name" || value == "Surname") return false; // name is required
	
	telNoPattern = /^[a-zA-Z\-\' ]*$/;
	
	return telNoPattern.test(value);
}

function vrule_text( value ) {
	
	if (value.length == 0){return false; }
	else
	{return true}
}

function vrule_DOB( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
        return value
    }   
}

function vrule_DOB_day( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
       if(value > 0 && value <=31)
       {return true}
       else
       {return false}
       
    }   

}

function vrule_DOB_month( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
       if(value > 0 && value <= 12)
       {return true}
       else
       {return false}
       
    }   

}

function vrule_income( value ) {

    if (isNaN(value) == true){
        return false
    }
    else
    {
        if (value >= 800)
        {
            return true
        }
        else
        {
            return false
        }
    }   

}


function vrule_email( value ) {
	
	if (value.length == 0 || value=="Email") return true; // email address is optional

	telNoPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	return telNoPattern.test(value);
}



// Pop up window

function openpopup(popurl){
var winpops=window.open(popurl,"","width=600,height=465,scrollbars,resizable")
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}



// Tooltip show hide
function showdiv(name){
if (document.getElementById(name)){
var obj = document.getElementById(name)
}

if (obj.style.display=="none"){
obj.style.display="";
document.getElementById("faq-" + name).style.backgroundImage = "url(images/minus.png)"

}else{
obj.style.display="none";
document.getElementById("faq-" + name).style.backgroundImage = "url(images/plus.png)"
}
}



// Debt test form ValidateDT

function validateDT() {

	var txtForenames = document.getElementById("txtForenames"); resetError(txtForenames)
	var txtSurname = document.getElementById("txtSurname"); resetError(txtSurname);
	var txtHometelno = document.getElementById("txtHometelno"); resetError(txtHometelno);
	var txtEmail = document.getElementById("txtEmail"); resetError(txtEmail);
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy"); resetError(chkPrivacyPolicy);
	var txtDebtAmount = document.getElementById("txtDebtAmount");resetError(txtDebtAmount);
	var txtResidential = document.getElementById("txtResidential");resetError(txtResidential);
	var txtEmployment = document.getElementById("txtEmployment");resetError(txtEmployment);
	var msg = "";

	
	if (txtDebtAmount.value != "0" && txtResidential.value != "0" && txtEmployment.value != "0" && chkPrivacyPolicy.checked == true && vrule_name(txtForenames.value) && vrule_name(txtSurname.value) && vrule_telephone(txtHometelno.value) && vrule_email(txtEmail.value)) {
		// tidy up the field content before submitting
		document.getElementById("txtHometelno").value = vtidy_telephone( txtHometelno.value );
		return true;	
	} else {
		if (txtDebtAmount.value == "0"){highlightErrorCallBack(txtDebtAmount);msg = msg + " - Your approximate total debt\n"} 
		if (txtResidential.value == "0"){highlightErrorCallBack(txtResidential);msg = msg + " - Your location\n"} 
		if (txtEmployment.value == "0"){highlightErrorCallBack(txtEmployment);msg = msg + " - Your employment status\n"} 
		if (!vrule_name(txtForenames.value)){highlightErrorCallBack(txtForenames);msg = msg + " - First name\n"} 
		if (!vrule_name(txtSurname.value)){highlightErrorCallBack(txtSurname);msg = msg + " - Surname\n"} 
		if(!vrule_telephone(txtHometelno.value)){highlightErrorCallBack(txtHometelno); msg = msg + " - A valid home or mobile number\n"} ;
		if(!vrule_email(txtEmail.value)){highlightErrorCallBack(txtEmail); msg = msg + " - A valid email address\n"} 
		if(!chkPrivacyPolicy.checked==true){highlightErrorCB(chkPrivacyPolicy); msg = msg + " - Agree to our privacy policy\n"} 
		alert (" - Please check the following have been filled correctly:\n\n" + msg )
		return false;
	}
}

function vrule_telephone( value ) {
	
	value = vtidy_telephone( value );
	
	telNoPattern = /^\d{11}$/;
	return telNoPattern.test(value);
}

function vtidy_telephone( value ) {
	// strip out any spaces and other junk
	return value.replace( /[^0-9]/g, "");
}

function vrule_name( value ) {
	
	if (value.length == 0 || value == "First name" || value == "Surname") return false; // name is required
	
	telNoPattern = /^[a-zA-Z\-\' ]*$/;
	
	return telNoPattern.test(value);
}

function vrule_text( value ) {
	
	if (value.length == 0){return false; }
	else
	{return true}
}


function vrule_email( value ) {
	
	if (value.length == 0) return true; // email address is optional
	
	telNoPattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
	return telNoPattern.test(value);
}


function displayError( msg ){
	document.getElementById('warning').innerHTML = msg
}

function highlightError( id ){
	id.style.borderColor='#FF0000';

}

function highlightErrorCallBack( id ){
	id.style.borderColor='#FF0000';
	id.style.backgroundImage = "url(../images/app-asterisk.png)";

}

function highlightErrorCB( id ){
	id.style.borderColor='#FF0000';
	id.style.backgroundColor='#FF0000';
}

function resetError( id ){
	id.style.borderColor='#A5A9AB';
	id.style.backgroundImage  = 'none'
}
function resetErrorMain( id ){
	id.style.borderColor='#A5A9AB';
	id.style.backgroundImage.src  = 'none'
}

