//this is used to check that the loans console is correctly validated
function validateLoansConsole(console)
{	
	//regular expression for loan amounts
	var re = /^\d/;
	var loanamount;	
	
	//if the amount to borrow is checked
	if(console.calcMethod[0].checked == true) 
	{
		//set up the loan amount
		loanamount = console.amount.value;
	
		//if the value is less or equal to 0
		if(loanamount <= 0)
		{
			alert("Please enter an amount you wish to borrow");		
			return false;	
		}				
	}
	else if(console.calcMethod[1].checked == true)
	{
		//set up the loan amount
		loanamount = console.monthlyamount.value;
		
		//if the value is less or equal to 0
		if(loanamount <= 0)
		{
			alert("Please enter an amount you can afford to repay");
			return false;
		}		
	}	
	
	//check that we have personal loan selected and expert match
	if((console.LoanType[0].checked == true) && (console.useExpertMatch.checked == true))
	{	
		//get the length of the postcode
		var postcodeLength = console.p_postcode_outward.value.length + console.p_postcode_inward.value.length;
		
		//check postcode length not zero
		if(postcodeLength == 0)
		{
			alert("You have selected to use ExpertMatch, please enter a valid postcode");
			return false;	
		}	
		else
		{
			//get postcode variable from form
			var strPostcode = console.p_postcode_outward.value + console.p_postcode_inward.value;
			//regular expression	
			var re = /^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/i;
				
			//test the reg expression
			if (re.test(strPostcode)) 
			{
				return true;
			} 
			else 
			{
				alert("Please give a valid full UK postcode");
				console.p_postcode_outward.focus();
				return false;
			}		
		}
	}	
}

//this function validates that a correct UK postcode has been entered in the cards console
function checkPostcode(console) {

//postcode length variable
var postcodeLength = console.p_postcode_outward.value.length + console.p_postcode_inward.value.length;

//check if the expert match is selected and if the postcode is not filled
if((console.useExpertMatch.checked == true) && (postcodeLength == 0))
{
		alert("You have selected to use ExpertMatch, please enter a valid postcode");
		return false;
}
//if expert match not selected then return true
else if(console.useExpertMatch.checked==false)
{
	return true;
}
//else check the postcode
else
{
	//get postcode variable from form
	var strPostcode = console.p_postcode_outward.value + console.p_postcode_inward.value;
	//regular expression	
	var re = /^((([A-PR-UWYZ])([0-9][0-9A-HJKS-UW]?))|(([A-PR-UWYZ][A-HK-Y])([0-9][0-9ABEHMNPRV-Y]?))\s{0,2}(([0-9])([ABD-HJLNP-UW-Z])([ABD-HJLNP-UW-Z])))|(((GI)(R))\s{0,2}((0)(A)(A)))$/i;
		
	//test the reg expression
	if (re.test(strPostcode)) 
	{
		return true;
	} 
	else 
	{
		alert("Please give a valid full UK postcode");
		console.p_postcode_outward.focus();
		return false;
	}
}
}

//this validates that the priority factor is a number within the manager form
function validateManagerForm(form)
{
	var re = /^\d/;
	
	if(re.test(form.pf.value))
	{
		return true;			
	}
	else
	{
		alert("The priority factor must be a number");
		return false;
	}	
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// cross browser function to toggle DHTML help box
function toggleHelp(id) {
    if (document.getElementById && document.getElementById(id) != null) // IE5+ NS6+
         document.getElementById(id).style.display = (document.getElementById(id).style.display != 'block') ? 'block' : 'none';
    else if (document.layers && document.layers[id] != null) // NS4
		// replace this ALERT with text string from external js file?
        alert("Help not available in Netscape 4");
    else if (document.all) // IE4+
	    eval ("document.all." +id+ ".style.display = (document.all." +id+ ".style.display!='block') ? 'block' : 'none';")
}

// cross browser tool tip thingy (Limited in non-IE4+)
function toolTip(object,div) {
	if (document.getElementById(div).innerText) // IE4+
         object.title=document.all[div].innerText
		 else object.title="Explain this"
}

// toggle object display between block and none
function toggleDisplay(div) {
if (document.getElementById) {
	eval("document.getElementById('" +div+ "').style.display = (document.getElementById('" +div+ "').style.display!='none') ? 'none' : 'block'"); //IE5+ NS6+
	return;
	}
if (document.all) {
	eval("document.all['" +div+ "'].style.display = (document.all['" +div+ "'].style.display!='none') ? 'none' : 'block'"); //IE4
	return;
	}
}

// set object display to block or none
function setDisplay(div, displayState) {
if (document.getElementById) {
	eval("document.getElementById('" +div+ "').style.display = '" +displayState+ "'"); //IE5+ NS6+
	return;
	}
if (document.all) {
	eval("document.all['" +div+ "'].style.display = '" +displayState+ "'"); //IE4
	return;
	}
}

//this function adds the url to your favourites
function AddToFav(url,title)
{		
	window.external.AddFavorite(url,title);	
}

// toggle disable radio buts and checkboxes between true and false
function toggleDisable(object) {
if (object.length) {for(i=0; i<object.length; i++) object[i].disabled = (object[i].disabled!=false) ? false : true;} else {object.disabled = (object.disabled!=false) ? false : true;}
}

// function to draw a 'helpbox' containing the strings defined in the helpfile array (helpfiles.txt)
function drawHelpbox(id) {
// split the helpfile into two bits - keywords and explanatory paragraph
var keywords = help[id].split(":")[0];
var para = help[id].split(":")[1];
var helpBox = ("<p>" + para + "</p>");

document.write(helpBox);
}

//this validates that the field value passed contains only digits
function validateNumberField(field)
{
		//if the field is not empty then test it
		if(field.value != "")
		{
			var re = /^\d/;	
			if(re.test(field.value))
			{
				return true;			
			}
			else
			{		
				return false;
			}	
		}
		else
		{
			return true;
		}
}

//this is the function that validates the loans callback or freedom finance
function isReady(form)
{
	//alert(form.LoanAmount.value);
	
	//test for loan amount
	if(form.LoanAmount.value.length == 0)
	{
		alert("Please enter loan amount.");
		form.LoanAmount.focus();
		return false;
	}	
	else if(form.FirstName.value.length == 0)
	{
		alert("Please enter your first name.");
		form.FirstName.focus();
		return false;		
	}
	else if(form.LastName.value.length == 0)
	{
		alert("Please enter your surname.");
		form.LastName.focus();
		return false;		
	}	
	else if(form.HouseNameOrNumber.value.length == 0)
	{
		alert("Please enter your house name or number.");
		form.HouseNameOrNumber.focus();
		return false;		
	}
	else if(form.Postcode.value.length == 0)
	{
		alert("Please enter your postcode.");
		form.Postcode.focus();
		return false;		
	}
	else if((form.PhoneDay.value.length == 0) && (form.PhoneEve.value.length == 0) && (form.PhoneMobile.value.length == 0))
	{
		alert("Please enter at least one phone number.");
		form.PhoneDay.focus();
		return false;		
	}
	else if(form.ck1.checked == false)
	{
		alert("Please ensure that the data protection checkbox is ticked.");		
		return false;
	}
	
}

function checkZero(console){

if(console.CalcTotalOwed.value == 0) 
{ 
	alert("Your total outstanding debt is zero!"); 
	return false;
}
else if (console.CalcTotalRepayments.value == 0)
{ 
	alert("Your total monthly repayment is zero!");
	return false;
}
else
{
	//validate entered amounts
	var inputTotal;
	var inputRepay;
	var loop;

	//loop through amounts
	for(loop = 1 ; loop <= 6 ; loop ++)
	{		
		inputTotal = document.getElementById('tot_' + loop);
		inputRepay = document.getElementById('rep_' + loop);
		
		//check colours of tabs
		if(inputTotal.value > 0)
		{
			if(inputRepay.value == "")
			{
				alert("You must provide a monthly repayment for each outstanding debt!");
				inputRepay.focus();
				return false;
			}
		}
		
		if(inputRepay.value > 0)
		{
			if(inputTotal.value == "")
			{
				alert("You must provide a monthly repayment for each outstanding debt!");
				inputTotal.focus();
				return false;
			}		
		}		
	}	
}

}