// Show hide
function showdiv(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
if (obj.style.display=="none"){
obj.style.display="";
}else{
obj.style.display="none";
}
}

function hidediv(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
obj.style.display="none";
}


// Pop up window
function openpopup(popurl){
var winpops=window.open(popurl,"","width=520,height=500,scrollbars,resizable")
}

//Font Resizer
var xmlhttp;
var DivStyle
function fontresize(divID) {
   
    DivStyle = divID 
	xmlhttp = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			xmlhttp = new XMLHttpRequest();
        } catch(e) {
			xmlhttp = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		xmlhttp = false;
        	}
		}
    }
    
	if(xmlhttp) {
		var url = '/containercookie.aspx?ClassName=' + divID
		xmlhttp.onreadystatechange = changeClass
		xmlhttp.open("GET", url , true)
		xmlhttp.send(null)
		
	}
}
 
function changeClass() {
    if (xmlhttp.readyState==4){
       if (xmlhttp.status==200) {
        document.getElementById("main-container").className = DivStyle
       }
    }
}

function validate() {

	var txtForenames = document.getElementById("txtForenames"); resetError(txtForenames)
	var txtSurname = document.getElementById("txtSurname"); resetError(txtSurname);
	var txtHometelno = document.getElementById("txtHometelno"); resetError(txtHometelno);
	var txtMobiletelno = document.getElementById("txtMobiletelno"); resetError(txtMobiletelno);
	var txtEmail = document.getElementById("txtEmail"); resetError(txtEmail);
	//var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy"); resetError(chkPrivacyPolicy);
	var msg = "";

	
	if (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 );
		return true;	
	} else {
		if (!vrule_name(txtForenames.value)){highlightErrorCallBack(txtForenames);msg = msg + "<li>First name</li>"} 
		if (!vrule_name(txtSurname.value)){highlightErrorCallBack(txtSurname);msg = msg + "<li>Surname</li>"} 
		if(!vrule_telephone(txtHometelno.value) && !vrule_telephone(txtMobiletelno.value)){highlightErrorCallBack(txtHometelno); msg = msg + "<li>A valid home or mobile number</li>"} ;
		if(!vrule_email(txtEmail.value)){highlightErrorCallBack(txtEmail); msg = msg + "<li>A valid email address</li>"}  
		displayError ("<p>Please check the following fields have been filled correctly:</p><ul>" + msg + "</ul>")
		return false;
	}
}

function vrule_debttest( value ) {
	
	if (value == "0")
	{ return false;}
	else
	{return value;}
}

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);
}

// Tooltip show hide
function showdiv(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
if (obj.style.display=="none"){
obj.style.display="";
}else{
obj.style.display="none";
}
}

function hidediv(name){
var obj = (document.getElementById)? document.getElementById(name) : eval("document.all[name]");
obj.style.display="none";
}

// Show details and errors

function showDetails() {
	changeDisplay( "details", "block" );
}

function hideDetails() {
	changeDisplay( "details", "none" );
}

function changeDisplay( id, type ) {
	var sel = document.getElementById( id );
	sel.style.display = type;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Pop up window

function openpopup(popurl){
var winpops=window.open(popurl,"","width=520,height=520,scrollbars,resizable")
}

function displayError( msg ){
	document.getElementById('warning').innerHTML = msg
	document.getElementById('warning').style.display= 'block'}
		
function displayErrorDebtTest( msg ){
	document.getElementById('warning-home').innerHTML = msg
	document.getElementById('warning-home').style.display= 'block'}

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'
}

function debttest() {
	
	var txtDebtAmount = document.getElementById("txtDebtAmount");resetError(txtDebtAmount)
	var txtLocation = document.getElementById("txtLocation");resetError(txtLocation)
	var txtEmployment = document.getElementById("txtEmployment");resetError(txtEmployment)
	var txtForenames = document.getElementById("txtForenames");resetError(txtForenames)
	var txtSurname = document.getElementById("txtSurname");resetError(txtSurname)
	var txtHometelno = document.getElementById("txtHometelno");resetError(txtHometelno)
	var chkPrivacyPolicy = document.getElementById("chkPrivacyPolicy");resetError(chkPrivacyPolicy)
	var msg = "";
	
	if(chkPrivacyPolicy.checked!= true || txtDebtAmount.value == "0" || txtlocation.value== "0" || txtEmployment.value == "0" || !vrule_name(txtForenames.value) || !vrule_name(txtSurname.value) || !vrule_telephone(txtHometelno.value)){		
		msg += (!vrule_debttest(txtDebtAmount.value))?"<li>Your approximate amount of debt</li>":"";
		msg += (!vrule_debttest(txtLocation.value))?"<li>Your location</li>":"";
		msg += (!vrule_debttest(txtEmployment.value))?"<li>Employment status</li>":"";
		msg += (!vrule_name(txtForenames.value))?"<li>First name</li>":"";
		msg += (!vrule_name(txtSurname.value))?"<li>Surname</li>":"";
		msg += (!vrule_telephone(txtHometelno.value))?"<li>A valid home or mobile number</li>":"";
		msg += (!chkPrivacyPolicy.checked==true)?"<li>Please tick the box to accept our privacy policy</li>":"";
		displayErrorDebtTest ("<p>Please check the following fields have been filled correctly:</p><ul>" + msg + "</ul>")
		return false;
	}
	else
	{
	return true
	}
	}
