// JavaScript Document

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



	function iepy(wht)
		{
			if (document.getElementById(wht).value == "") 
			{ 
				document.getElementById(wht).focus();						
				return true; 
			}	
			else 
			{ 

				return false; 
			}
		}
		
	function isnumber(wht)
		{
			if (isInteger(document.getElementById(wht).value) && document.getElementById(wht).value != "")
				{
					return false;	
				}
			else
				{
					document.getElementById(wht).focus();							
					return true;	
				}
		}
		
	function isphonenumber(wht)
		{
			if (document.getElementById(wht).value == "")
				{
				document.getElementById(wht).focus();							
					return true;	
				}
			else
				{
						if (checkInternationalPhone(document.getElementById(wht).value)==false) 
						{ 
				document.getElementById(wht).focus();					
							return true; 
						} 
					else 
						{
							return false;					
						}
				}
		}
		
	function isphonenumberthere(wht)
		{
			if (document.getElementById(wht).value == "")
				{
					return false;	
				}
			else
				{
						if (checkInternationalPhone(document.getElementById(wht).value)==false) 
						{ 
				document.getElementById(wht).focus();													
							return true; 
						} 
					else 
						{
							return false;
						}
				}
		}
		function checkerror(wht, idtofind)
			{			
			 if (document.getElementById(wht).value == "")			
			 	{
					document.getElementById(idtofind).style.display="inline";											
					return true;
				}
			else
				{
				document.getElementById(idtofind).style.display="none";											
					return false;				
			}
			}
		
		function ischoosen(wht)
			{
			 if (document.getElementById(wht).selectedIndex == "0")	
			 	{
					return true;
				}
			else
				{
					document.getElementById(wht).focus();
					return false;
				}
			}
			

	function checkform()
		{

		if (isnumber("txtborrow"))	{	alert("Please enter the amount you wish to borrow"); return false;	}
			else if (isnumber("txtpropvalue"))	{	alert("Please enter an esitmated value amount"); return false;	}
			else if (ischoosen("drpmortgage")) { alert("Please select a mortgage type"); return false; }
			else if (isnumber("txtlength"))	{	alert("Please enter a term length"); return false;	}			
			else if (ischoosen("drptitle")) { alert("Please select your Title"); return false; }						
			else if (iepy("txtfirstname"))	{	alert("Please enter your First Name"); return false;	}	
			else if (iepy("txtsurname"))	{	alert("Please enter your Surname");	 return false;}
			else if (iepy("txtaddress"))	{	alert("Please enter your Address");	return false;	}				
			else if (iepy("txtpostcode"))	{	alert("Please enter your Postcode");	return false;	}				
			else if (isphonenumber("txthphone"))	{	alert("Please enter a valid phone number"); return false;	}
			else if (isphonenumberthere("txtmphone"))	{	alert("Please enter a valid mobile phone number"); return false;	}
			else if (iepy("txtemail"))	{	alert("Please enter your email address");	return false;	}				
			else {	return true;}							
		}
		
		function checkphone(wht, idwht)
			{
				if (isphonenumber(wht))
					{
	document.getElementById(idwht).style.display="inline";
					}
				else
					{
					document.getElementById(idwht).style.display="none";
					}
			}					
		function checkselected(wht, idwht)
			{
				if (document.getElementById(wht).selectedIndex == "0")
					{
						document.getElementById(idwht).style.display="inline";
					}
				else
					{
					document.getElementById(idwht).style.display="none";
					}
			}
		
		function checknumber(wht, idwht)
			{ 
				if (isInteger(document.getElementById(wht).value) && document.getElementById(wht).value != "") 
					{ 
						document.getElementById(idwht).style.display="none";	
					}
				else 
					{ 
						document.getElementById(idwht).style.display="inline"; 
					}	
			}
			
		function checkempty(wht, idwht)
			{ 
				if (document.getElementById(wht).value == "") 
					{ 
						document.getElementById(idwht).style.display="inline"; 
					}	
				else 
					{ 
						document.getElementById(idwht).style.display="none"; 
					}	
				}
		
		function validateform()
			{
					document.getElementById('txtborrow').onchange = function() {  checknumber('txtborrow','s1') }
					document.getElementById('txtpropvalue').onchange = function() {  checknumber('txtpropvalue','s2') }
					document.getElementById('txtlength').onchange = function() {  checknumber('txtlength','s4') }
					document.getElementById('txtfirstname').onchange = function() {  checkempty('txtfirstname','s6') }
					document.getElementById('txtsurname').onchange = function() {  checkempty('txtsurname','s7') }					
					document.getElementById('txtaddress').onchange = function() {  checkempty('txtaddress','s8') }										
					document.getElementById('txtpostcode').onchange = function() {  checkempty('txtpostcode','s9') }									
					document.getElementById('txthphone').onchange = function() {  checkphone('txthphone','s10') }
					document.getElementById('txtmphone').onchange = function() 
							{  
								if (document.getElementById("txtmphone").value == "")
									{
										document.getElementById("s11").style.display = "none";
									} else {	checkphone('txtmphone','s11') }	}																																												
					document.getElementById('txtemail1').onchange = function() {  checkempty('txtemail1','s12') }																				
					document.getElementById('drpmortgage').onchange = function() {  checkselected('drpmortgage','s3') }																									
					document.getElementById('drptitle').onchange = function() {  checkselected('drptitle','s5') }																														

						
			}
