// 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=410,height=400,scrollbars,resizable")
}


//Routing to fade in layers
var fadeTime = ""
var mozfade = 0
var IEfade = 0
var previous = ""
function flipper(ID,targetID) {
	
   	clearInterval(fadeTime)
   
	document.getElementById(targetID).style.backgroundImage = "url(images/" + targetID + ".jpg)";
	
   
   if (previous!=targetID){
	document.getElementById("image-welcome").style.zIndex = 1;
	document.getElementById("image-mortgage").style.zIndex = 1;
	document.getElementById("image-loan").style.zIndex = 1;
	document.getElementById("image-debt").style.zIndex = 1;
	document.getElementById("image-repossession").style.zIndex = 1;
   }
	
	document.getElementById("one").className = "";
	document.getElementById("two").className = "";
	document.getElementById("three").className = "";
	document.getElementById("four").className = "";
	document.getElementById("five").className = "";
	
	if (previous==""){
		previous = "image-welcome"
		document.getElementById(targetID).style.zIndex = 9;
	}
	
	mozfade = 0
	IEfade = 0
	if (previous==targetID){
		document.getElementById(targetID).style.zIndex = 9;

	}
	
	else
	
	{
		fadeTime = setInterval("FadeIn('" + targetID + "')",10)
		document.getElementById(targetID).style.opacity = 0;
		document.getElementById(targetID).style.filter="alpha(opacity=0)";
		document.getElementById(previous).style.zIndex = 2;
		document.getElementById(targetID).style.zIndex = 9;
		
	}
	document.getElementById(ID).className = "active"
	previous = targetID
}


//Fading function
function FadeIn(targetID) {

		document.getElementById(targetID).style.opacity = mozfade 
		document.getElementById(targetID).style.filter = "alpha(opacity=" + IEfade + ")" 
		mozfade = mozfade + 0.1
		IEfade = IEfade + 10
		
		if (mozfade > 1 || IEfade > 100){
			clearInterval(fadeTime)
		}

	}


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 (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 );
		return true;	
	} else {
		if (!vrule_name(txtForenames.value)){highlightErrorCallBack(txtForenames);msg = msg + "<li>Forename</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>"} 
		if(!chkPrivacyPolicy.checked==true){highlightErrorCB(chkPrivacyPolicy); msg = msg + "<li>Agree to our privacy policy</li>"} 
		displayError ("<p>Please check the following fields have been filled in correctly:</p><ul>" + msg + "</ul><br/>")
		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';
}

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'
}
