function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;

	if(radioObj.tagName == "SELECT") {
		return "select";
	}

	if(!radioLength) {
		return "textfield";
	}
	
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function checkform() {
	showerror = false;

	//check fields
	for(i=0;i<formfieldsnum;i++) {	//is empty
		if(document.forms[formnum][formfields[i].name].value=="") {
			if(document.getElementById(formfields[i].name+"error")) {
				document.getElementById(formfields[i].name+"error").innerHTML = formfields[i].errors[0].empty;
			}
			showerror = true;
		} else {	//is radio empty
			if(getCheckedValue(document.forms[formnum][formfields[i].name]) == "") {
				if(document.getElementById(formfields[i].name+"error")) {
					document.getElementById(formfields[i].name+"error").innerHTML = formfields[i].errors[0].empty;
				}
				showerror = true;
			} else if(formfields[i].regexp) {//doesn't match regexp
				eval("var regexp = "+formfields[i].regexp);
				if(regexp.test(document.forms[formnum][formfields[i].name].value)) {
					if(document.getElementById(formfields[i].name+"error")) {
						document.getElementById(formfields[i].name+"error").innerHTML='';
					}
				} else {
					document.getElementById(formfields[i].name+"error").innerHTML = formfields[i].errors[0].regexp;
					showerror = true;
				}
			} else {
				if(document.getElementById(formfields[i].name+"error")) {
					document.getElementById(formfields[i].name+"error").innerHTML='';
				}
			}
		}
	}


	//check dependencies
	for(i=0;i<dependenciesnum;i++) {
		functionname = dependencies[i].functionname;
		evalresult = eval(functionname);
		if(evalresult == false)	{
			if(dependencies[i].errors[0].alert)	{
				alert(dependencies[i].errors[0].alert);
			}
			showerror = true;
			break;
		}
	}
	
	
	if(showerror == true) {
		document.getElementById('errormsg').style.display='block';
		return false;
	} else {
		return true;
	}
}


function checkMortgageValue(amountrequired, maxprice)	{
	var sAr = document.getElementById(amountrequired).value;
	var sMp = document.getElementById(maxprice).value;

	var patt = new RegExp("[0-9]+,?[0-9]+");
	var iAr = 0;
	var iMp = 0;
	if(sAr.length > 0) {
		var aResult = patt.exec(sAr);
		iAr = parseFloat(aResult[0].replace(/[^0-9]/g,""));
	}
	if(sMp.length > 0) {
		var aResult = patt.exec(sMp);
		iMp = parseFloat(aResult[0].replace(/[^0-9]/g,""));
	}
	if(iMp > 0) {
		var percentage_value = (iAr / iMp);
		//console.log(percentage_value);
		if( percentage_value > 0.90) {
			return false
		}
	}
	return true;
}


function createRequestObject() {
	var req;
	var browser = navigator.appName;

	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = false;
		}
		// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	return req;
}

function sndReq(postcode, specificAdviser) {
    http.open('get', '/check.php?postcode='+postcode+'&leadtypeid=2&adviser='+specificAdviser);

	http.onreadystatechange = function() {
		handleResponse(specificAdviser);
	}

	http.send(null);
}

function handleResponse(specificAdviser) {
	if(http.readyState == 4) {
		var response = http.responseText;
      
		if (specificAdviser) {
			switch(response) {
				case 'specific adviser ok':
				document.getElementById('adviserlist').innerHTML = "<p>Great! It looks as though the adviser which you have chosen is able to accept your enquiry.  Click the button, we'll pass your details to them and they will contact you very shortly by phone or by email!</p>";
				break;
				case 'specific adviser no details':
				document.getElementById('adviserlist').innerHTML = "<p>Enter your details above and click the button below. The FSA regulated adviser which you have specified will contact you as soon as possible by phone or by email.</p>";
				break;
				case 'no adviser':
				document.getElementById('adviserlist').innerHTML = "<p>Unfortunately we will be unable to put you in touch with the adviser which you have specified. However, we will pass your details to an FSA regulated adviser who will contact you as soon as possible.  We will let you know who will contact you by email. Your adviser will contact you by phone or email.</p>";
				break;
				default:
				document.getElementById('adviserlist').innerHTML = "<p>Unfortunately we will be unable to put you in touch with the adviser which you have specified. However, we will pass your details to one of the following recommended FSA regulated advisers who will contact you.  If this is ok, click the button below to submit your details.</p><blockquote>"+response+"</blockquote>";
				break;
			}
		} else {
			switch(response) {
				case 'no adviser':
				document.getElementById('adviserlist').innerHTML = "<p>Check your details above and click the button below. An FSA regulated adviser will contact you as soon as possible.We will let you know who will contact you by email.  Your adviser will contact you by phone or email.</p>";
		  		break;
				default:
				document.getElementById('adviserlist').innerHTML = "<p>Check your details above and click the button below. One of the following FSA regulated advisers will contact you by phone or by email:</p><blockquote>"+response+"</blockquote>";
				break;
			}
		}
	}
}

var http = createRequestObject();