function fourYears(id)
{
	var style = document.getElementById(id).style;
	if (style.display == "none")
	{
		style.display = "block";
	}
	else
	{
		style.display = "none";
	}
}


function didYouCheckIt()
{
	 if (!document.getElementById('chk').checked)
	 {
	 	alert('You must accept terms and condition before submitting your application');
	 }
	 else
	 {
	 	form.submit();
	 }
	 return false;
}

function isEmpty(str)
{
	// Check whether string is empty.
	for (var intLoop = 0; intLoop < str.length; intLoop++)
	{
	   if (str.charAt(intLoop)!= " ")
	   {
		  return false;
	   }
	}
	return true;
}

function checkRequired(f)
{
	if (!document.getElementById('chk').checked)
	{
	  alert('You must accept terms and condition before submitting your application');
	  return false;
	}
}

function checkRequired2(f)
{
	var strError = "";
	for (var intLoop = 0; intLoop<f.elements.length; intLoop++)
	{
		if(f.elements[intLoop].getAttribute("required")!=null)
		{
			if(isEmpty(f.elements[intLoop].value))
			{
				strError += "  " + f.elements[intLoop].name + "\n";
				if (strError!='')
				{
					alert("Required data is missing:\n" + strError);
					return false;
				}
			}
		}
	}
}

function checkPost()
{
	var post = document.getElementById('postcode');
	var val = post.value;
	
	if(val.length < 2)
	{
		post.value = '';
		alert("Please enter the postcode in the right format");
		return false;
	}
	
	if(val.indexOf(' ')==-1)
	{
		post.value = '';
		alert("Please ensure you have a space in the middle of your postcode");
		return false;
	}
}

function checkEmail()
{
	var email = document.getElementById('email');
	var email2 = email.value;
	var pos = email2.indexOf('@');
	if(pos==-1)
	{
		email.value = '';
		alert("Please enter a valid email address");
		return false;
	}
	else
	{
		var sub = email2.substr(pos);
		if(sub.indexOf('.')==-1)
		{
			email.value = '';
			alert("Please enter a valid email address");
			return false;
		}
	}
}

function checkMobile()
{
	var mobile = document.getElementById('mobile');
	var val = mobile.value;
	if(isNaN(val))
	{
		mobile.value = '';
		alert('please use the international format e.g. 447956555555');
		return false;
	}
	else
	{
		if(val != '')
		{
			if(val.charAt(0)=='0')
			{
				mobile.value = '';
				alert('please use the international format e.g. 447956555555');
				return false;
			}
			
			if(val.length!=12)
			{
				mobile.value = '';
				alert('please enter a valid mobile number');
				return false;
			}
		}
	}
}

function checkBankAcc()
{
	var bankacc = document.getElementById('accno');
	var val = bankacc.value;
	

	if(val.length!=8)
	{
		bankacc.value = '';
		alert('please enter a valid bank account number');
		return false;
	}
}

function checkBankSort()
{
	var sortc = document.getElementById('sortcode');
	var val = sortc.value;
	
	if(val.length!=6)
	{
		sortc.value = '';
		alert('please enter a valid sort code');
		return false;
	}
}

function checkFlat()
{
	var building = document.getElementById('building_name');
	var flatno = document.getElementById('flat');
	if(building.value != '' && flatno == '')
	{
		alert('please enter a flat number for the building');
	}
}

function checkFlat2()
{
	var building2 = document.getElementById('building_name2');
	var flatno2 = document.getElementById('flat2');
	if(building2.value != '' && flatno2 == '')
	{
		alert('please enter a flat number for the previous building');
	}
}

function checkDate(form)
{
	var d = form.Date_of_Birth.value;
	var m = form.Month_of_birth.value;
	var y = form.Year_of_Birth.value;
	
	if (m==4 || m==6 || m==9 || m==11)
	{
		if (d==31)
		{
			alert('please enter a valid date');
			return false;
		}
	}
		
	if (m==2)
	{
		if (d>29) 
		{
			alert('please enter a valid date');
			return false;
		}
		
		if (d==29 && ((y/4)!=parseInt(y/4)))
		{
			alert('please enter a valid date');
			return false;
		}
	}
}


function numerical(num)
{
	var val = num.value;
	if(isNaN(val))
	{
		num.value = '';
		alert('please enter a numerical value');
		return false;
	}
	
	for (var i = 0; i < val.length; i++)     
	{        
		var ch = val.charAt(i)        
		if (ch == '.')        
		{   
			num.value = '';
			alert('please enter a numerical value');
			return false;
		}   
	}
}

function numerical2(num)
{
	var val = num.value;
	//alert(val);
	if(isNaN(val))
	{
		num.value = '';
		alert('please enter a numerical value');
		return false;
	}
	
	var ch = val.indexOf('.');
	if(ch!=-1)
	{
		var str = val.substr(ch);
		if(str.length>3)
		{
			num.value='';
			alert('please enter upto only 2 decimal places');
		}
	}
}

function show(section)
{
	var ex = document.getElementById(section);
	var ex2 = document.getElementById(section+'a');
	if(ex.className == 'expand')
	{
		ex.className = 'collapse';
		ex2.innerHTML = '+';
	}
	else
	{
		ex.className = 'expand';
		ex2.innerHTML = '-';
	}
	return true;
}


function checkdate(form)
{
	var d = form.Date_Day.value;

	var m = form.Date_Month.value;
	var y = form.Date_Year.value;
	
	if (m==4 || m==6 || m==9 || m==11)
	{
		if (d==31)
		{
			alert('please enter a valid date');
			return false;
		}
		
		if (m==2)
		{
			var b = parseInt(y/4);
			if(isNaN(b)) 
			{
				alert('please enter a valid date');
				return false;
			}
			
			if (d>29) 
			{
				alert('please enter a valid date');
				return false;
			}
			
			if (d==29 && ((y/4)!=parseInt(y/4)))
			{
				alert('please enter a valid date');
				return false;
			}
		}
	}
}

function Change(select) 
{
	if(select.selectedIndex == select.options.length - 1) 
	{
		select.style.display = 'none';
		var edit = document.getElementById(select.id + '_text');
		edit.style.display = '';
		edit.focus();
	}
}


/*function pcaFastAddressBegin(postcode, language, style, account_code, license_code, machine_id, options)
{
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&postcode=" + escape(postcode);
	//strUrl += "&building=" + escape(building);
	strUrl += "&language=" + escape(language);
	strUrl += "&style=" + escape(style);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&options=" + escape(options);
	strUrl += "&callback=pcaFastAddressEnd";

	//Make the request
	if (scriptTag) 
	 {
	    headTag.removeChild(scriptTag);
	 }
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl;
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScript";
	headTag.appendChild(scriptTag);
}

function pcaFastAddressEnd()
{
	//Test for an error
	if (pcaIsError)
	{
	   //Show the error message
	   alert(pcaErrorMessage);
	}
	else
	{
	   //Check if there were any items found
	   if (pcaRecordCount==0)
	   {
	   	alert("Sorry, no matching items found");
	   }
	   else
	   {
		var place = pca_line1[0];
		//var houseno = document.getElementById('building').value + ' ';
		//place = place.replace(houseno, '');
		document.getElementById("road").value=place;
		document.getElementById("town").value=pca_post_town[0];
		document.getElementById("postcode").value=pca_postcode[0];
	   }
	}
}

function pcaFastAddressBegin2(postcode, language, style, account_code, license_code, machine_id, options)
{
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&postcode=" + escape(postcode);
	//strUrl += "&building=" + escape(building);
	strUrl += "&language=" + escape(language);
	strUrl += "&style=" + escape(style);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&options=" + escape(options);
	strUrl += "&callback=pcaFastAddressEnd2";

	//Make the request
	if (scriptTag) 
	 {
	    headTag.removeChild(scriptTag);
	 }
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl;
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScript2";
	headTag.appendChild(scriptTag);
}

function pcaFastAddressEnd2()
{
	//Test for an error
	if (pcaIsError)
	{
	   //Show the error message
	   alert(pcaErrorMessage);
	}
	else
	{
	   //Check if there were any items found
	   if (pcaRecordCount==0)
	   {
	   	alert("Sorry, no matching items found");
	   }
	   else
	   {
		var place = pca_line1[0];
		//var houseno = document.getElementById('building2').value + ' ';
		//place = place.replace(houseno, '');
		document.getElementById("prevroad").value=place;
		document.getElementById("prevtown").value=pca_post_town[0];
		document.getElementById("postcode2").value=pca_postcode[0];
	   }
	}
}*/

//ADDED NEW
var account_code = 'EUROM11118';
var licence_code = 'HA31-XE44-YE19-FF43';
		
function pcaByPostcodeBegin()
{
	var postcode = document.getElementById("postcode").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	
	document.getElementById("divCustomLoading").style.display = '';
	
	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=lookup";
	strUrl += "&type=by_postcode";
	strUrl += "&postcode=" + escape(postcode);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(licence_code);
	strUrl += "&callback=pcaByPostcodeEnd";
	
	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById("selectaddress").style.display = '';
	document.getElementById("btnFetch").style.display = '';
}
 
function pcaByPostcodeEnd()
{
	document.getElementById("divCustomLoading").style.display = 'none';
	
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount == 0)
		{
		    alert("Sorry, no matching items found");
		}
		else
		{
			for (i=document.getElementById("selectaddress").options.length-1; i>=0; i--)
			{
				document.getElementById("selectaddress").options[i] = null;
			}
			for (i=0; i<pca_id.length; i++)
			{
				document.getElementById("selectaddress").options[document.getElementById("selectaddress").length] = new Option(pca_description[i], pca_id[i]);
			}
		}
	}
}
		
function pcaFetchBegin()
{
	var address_id = document.getElementById("selectaddress").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	
	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&id=" + escape(address_id);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(licence_code);
	strUrl += "&style=raw";
	strUrl += "&callback=pcaFetchEnd";

	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById("selectaddress").style.display = 'none';
	document.getElementById("btnFetch").style.display =
 'none';
}

function pcaFetchEnd()
{
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount == 0)
		{
			alert("Sorry, no matching items found");
		}
		else
		{
			if (pca_building_flat[0]!='')
			{
				document.getElementById("flat").value = '' + pca_building_flat[0];
			}
			else
			{
				if(pca_sub_building_name[0]!='')
				{
					document.getElementById("flat").value = '' + pca_sub_building_name[0];
				}
				else
				{
					document.getElementById("flat").removeAttribute('disabled');
				}
			}
			
			if(pca_building_number[0]!='')
			{
				document.getElementById("building").value = '' + pca_building_number[0];
			}
			else
			{
				document.getElementById("building").removeAttribute('disabled');
			}
			
			if(pca_building_name[0]!='')
			{
				document.getElementById("building_name").value = '' + pca_building_name[0];
				document.getElementById("building").removeAttribute('required');
			}
			else
			{
				document.getElementById("building_name").removeAttribute('disabled');
			}
			
			//document.getElementById("road").value = '' + pca_dependent_thoroughfare_name[0] + ' ' + pca_dependent_thoroughfare_descriptor[0];
			if(pca_thoroughfare_name[0] == '' && pca_thoroughfare_descriptor[0] == '')
			{
				document.getElementById("road").removeAttribute('disabled');
			}
			else
			{
				document.getElementById("road").value = '' + pca_thoroughfare_name[0] + ' ' + pca_thoroughfare_descriptor[0];
			}
			
			//document.getElementById("custom_dd_locality").value = '' + pca_double_dependent_locality[0];
			//document.getElementById("custom_d_locality").value = '' + pca_dependent_locality[0];
			if(pca_dependent_locality[0]!='')
			{
				document.getElementById("district").value = '' + pca_dependent_locality[0];
			}
			else
			{
				document.getElementById("district").removeAttribute('required');
				document.getElementById("district").removeAttribute('disabled');
			}
			
			if(pca_post_town[0]!='')
			{
				document.getElementById("town").value = '' + pca_post_town[0];
			}
			else
			{
				document.getElementById("town").removeAttribute('disabled');
			}
			
			if(pca_county[0]!='')
			{
				document.getElementById("county").value = '' + pca_county[0];
			}
			else
			{
				document.getElementById("county").removeAttribute('required');
				document.getElementById("county").removeAttribute('disabled');
			}
			
			if(pca_postcode[0]!='')
			{
				document.getElementById("postcode").value = '' + pca_postcode[0];
				//document.getElementById("postcode").disabled = 'true';
			}
			
			//document.getElementById("custom_postcode2").value = '' + pca_postcode[0];
		}
	}
}

function pcaByPostcodeBegin2()
{
	var postcode2 = document.getElementById("postcode2").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	
	document.getElementById("divCustomLoading2").style.display = '';
	
	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=lookup";
	strUrl += "&type=by_postcode";
	strUrl += "&postcode=" + escape(postcode2);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(licence_code);
	strUrl += "&callback=pcaByPostcodeEnd2";
	
	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById("selectaddress2").style.display = '';
	document.getElementById("btnFetch2").style.display = '';
}
 
function pcaByPostcodeEnd2()
{
	document.getElementById("divCustomLoading2").style.display = 'none';
	
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount == 0)
		{
		    alert("Sorry, no matching items found");
		}
		else
		{
			for (i=document.getElementById("selectaddress2").options.length-1; i>=0; i--)
			{
				document.getElementById("selectaddress2").options[i] = null;
			}
			
			for (i=0; i<pca_id.length; i++)
			{
				document.getElementById("selectaddress2").options[document.getElementById("selectaddress2").length] = new Option(pca_description[i], pca_id[i]);
			}
		}
	}
}
		
function pcaFetchBegin2()
{
	var address_id2 = document.getElementById("selectaddress2").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";
	
	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&id=" + escape(address_id2);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(licence_code);
	strUrl += "&style=raw";
	strUrl += "&callback=pcaFetchEnd2";

	//Make the request
	if (scriptTag)
	{
		headTag.removeChild(scriptTag);
	}
	
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
	
	document.getElementById("selectaddress2").style.display = 'none';
	document.getElementById("btnFetch2").style.display = 'none';
}

function pcaFetchEnd2()
{
	//Test for an error
	if (pcaIsError)
	{
		//Show the error message
		alert("Sorry but you've used today's free credits. Please call us if you'd like more.");
	}
	else
	{
		//Check if there were any items found
		if (pcaRecordCount == 0)
		{
			alert("Sorry, no matching items found");
		}
		else
		{
			if (pca_building_flat[0]!='')
			{
				document.getElementById("flat2").value = '' + pca_building_flat[0];
			}
			else
			{
				if(pca_sub_building_name[0]!='')
				{
					document.getElementById("flat2").value = '' + pca_sub_building_name[0];
				}
				else
				{
					document.getElementById("flat2").removeAttribute('disabled');
				}
			}
			
			if(pca_building_number[0]!='')
			{
				document.getElementById("building2").value = '' + pca_building_number[0];
			}
			else
			{
				document.getElementById("building2").removeAttribute('disabled');
			}
			
			if(pca_building_name[0]!='')
			{
				document.getElementById("building_name2").value = '' + pca_building_name[0];
				document.getElementById("building2").removeAttribute('required');
			}
			else
			{
				document.getElementById("building_name2").removeAttribute('disabled');
			}
			
			//document.getElementById("prevroad").value = '' + pca_dependent_thoroughfare_name[0] + ' ' + pca_dependent_thoroughfare_descriptor[0];
			
			if(pca_thoroughfare_name[0]=='' && pca_thoroughfare_descriptor[0]=='')
			{
				document.getElementById("prevroad").removeAttribute('disabled');
			}
			else
			{
				document.getElementById("prevroad").value = '' + pca_thoroughfare_name[0] + ' ' + pca_thoroughfare_descriptor[0];
			}
			
			//document.getElementById("custom_dd_locality").value = '' + pca_double_dependent_locality[0];
			//document.getElementById("custom_d_locality").value = '' + pca_dependent_locality[0];
			
			if(pca_dependent_locality[0]!='')
			{
				document.getElementById("prevdistrict").value = '' + pca_dependent_locality[0];
			}
			else
			{
				document.getElementById("prevdistrict").removeAttribute('required');
				document.getElementById("prevdistrict").removeAttribute('disabled');
			}
			
			if(pca_post_town[0]!='')
			{
				document.getElementById("prevtown").value = '' + pca_post_town[0];
			}
			else
			{
				document.getElementById("prevtown").removeAttribute('disabled');
			}
			
			if(pca_county[0]!='')
			{
				document.getElementById("prevcounty").value = '' + pca_county[0];
			}
			else
			{
				document.getElementById("prevcounty").removeAttribute('required');
				document.getElementById("prevcounty").removeAttribute('disabled');
			}
			
			if(pca_postcode[0]!='')
			{
				document.getElementById("postcode2").value = '' + pca_postcode[0];
				//document.getElementById("postcode2").disabled = 'true';
			}
			
			//document.getElementById("custom_postcode2").value = '' + pca_postcode[0];
		}
	}
}

function checkall(form)
{
	if(checkRequired2(form) == false)
	{
		return false;
	}
	

	if(checkPost() == false)
	{
		return false;
	}
	

	if(checkEmail() == false)
	{
		return false;
	}
	

	if(checkMobile() == false)
	{
		return false;
	}
	

	if(checkDate(form) == false)
	{
		return false;
	}
	
	

	if(checkBankAcc() == false)
	{
		return false;
	}
	
	
	if(checkBankSort() == false)
	{
		return false;
	}
	
	if(checkFlat() == false)
	{
		return false;
	}
	
	if(checkFlat2() == false)
	{
		return false;
	}
	
	UnDisableAll();
	
	return true;
}

function checkall2(form)
{
	if(checkRequired2(form) == false)
	{
		return false;
	}
	
	if(checkEmail() == false)
	{
		return false;
	}


	if(checkMobile() == false)
	{
		return false;
	}


	if(checkDate(form) == false)
	{
		return false;
	}
	
	return true;
}

function checkall3(form)
{
	if(checkRequired2(form) == false)
	{
		return false;
	}
	
    var number = document.getElementById('number');
	if(numerical(number) == false)
	{
		return false;
	}
	
	var number2 = document.getElementById('Amount');
	if(numerical2(number2) == false)
	{
		return false;
	}
	
	return true;
}

function UnDisableAll()
{
	var ids = new Array("flat", "building", "building_name", "road", "district", "town", "county", "flat2", "building2", "building_name2", "prevroad", "prevdistrict", "prevtown", "prevcounty");
	for(i=0; i<ids.length; i++)
	{
		if(document.getElementById(ids[i]).hasAttribute('disabled'))
		{
			document.getElementById(ids[i]).removeAttribute('disabled');
		}
	}
}