
function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) {
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
				returnvalue=unescape(document.cookie.substring(offset, end))
			}
	}
	return returnvalue;
}

function updateLinks() {
	var meadprop = get_cookie("meadprop");
	if (meadprop != "") {
		var Properties = meadprop.split(":");

		for (Property in Properties) {
			if (Properties[Property] != "" && document.getElementById(Properties[Property])) {
				document.getElementById(Properties[Property]).innerHTML = "Remove from Report";
			}
		}
	}
}

function addremoveProperty(Property) {
	var meadprop = get_cookie("meadprop");
	if (meadprop != "") {
		var contains = meadprop.match(Property);
	}

	//Remove for report
	if (contains) {
		document.cookie = "meadprop=" + meadprop.replace(Property+":", "");
		document.getElementById(Property).innerHTML = "Add to Report";
	}
	//Add to report
	else {
		document.cookie = "meadprop=" + meadprop + Property + ":";
		document.getElementById(Property).innerHTML = "Remove from Report";
	}
}

function showForm() {
	var meadprop = get_cookie("meadprop");

	//No Property Selected
	if (meadprop == "") {
		document.getElementById("error1").innerHTML = '<span class="error">Please select some properties first!</span>';
	}

	//Write Form
	else {
		var form;
		form='	<p>Please complete the details below to request your free report.</p>';
		form+='	<p>Feel free to add some more properties before submitting your details.</p>';
		form+='	<form action="#" method="get" id="entryform">';
		form+='		<fieldset class="so">';
		form+='			<h5>Email my FREE Personal Report to</h5>';
		form+='			<label for="FirstName">Contact Name *:</label>';
		form+='			<input id="FirstName" name="FirstName" type="text" value="" class="input" /> <span id="errorFirstName" class="error"></span>';
		form+='			<label for="Email">Email Address *</label>';
		form+='			<input id="Email" name="Email" type="text" value="" class="input" /> <span id="errorEmail" class="error"></span>';
		form+='			<label for="CompanyName">Company Name *</label>';
		form+='			<input id="CompanyName" name="CompanyName" type="text" value="" class="input" /> <span id="errorCompanyName" class="error"></span>';
		form+='			<label for="Size">No. of Employees/Size</label>';
		form+='			<input id="Size" name="Size" type="text" value="" class="input" />';
		form+='			<label for="Telephone">Telephone Number *</label>';
		form+='			<input id="Telephone" name="Telephone" type="text" value="" class="input" /> <span id="errorTelephone" class="error"></span>';
		form+='			<label for="ExtraInfo">Any Extra Info?</label>';
		form+='			<textarea cols="15" rows="4" id="ExtraInfo" name="ExtraInfo" class="input"></textarea>';
		form+='			<div class="form_entry">';
		form+='			<input id="ContactSubmit" type="image" src="../images/global/btn_submit.gif" class="submit" value="Submit" onclick="submitForm(); return false;">';
		form+='			</div>';
		form+='		</fieldset>';
		form+='	</form>';

		document.getElementById("so-infobox").innerHTML = form;

	}
}

var xmlhttp

function submitData(url){
	xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest){
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=state_Change
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}
}

function state_Change() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
		// if "OK"
		if (xmlhttp.status==200 && xmlhttp.responseText == "true") {
			document.getElementById("so-infobox").innerHTML = '<p><strong>Thank you!</strong></p><p>You will hear from us shortly.</p>';
		}
		else {
			document.getElementById("so-infobox").innerHTML = '<p id="error2" class="error">There was a problem during the submission of your details. Please call us free on <strong>0800 9888340</strong> to discuss your enquiry.</p>';
		}
	}
}

function submitForm () {
	
	$error = false;
	
	//FirstName Check
	if (document.getElementById("FirstName").value == "") {
		document.getElementById("errorFirstName").innerHTML = "Required";
		$error = true;
	}
	else {
		document.getElementById("errorFirstName").innerHTML = "";	
	}
	
	//Email Check
	if (!((document.getElementById("Email").value.indexOf(".") > 2) && (document.getElementById("Email").value.indexOf("@") > 0))) {
		document.getElementById("errorEmail").innerHTML = "Required";
		$error = true;
	}
	else {
		document.getElementById("errorEmail").innerHTML = "";	
	}
	
	//CompanyName Check
	if (document.getElementById("CompanyName").value == "") {
		document.getElementById("errorCompanyName").innerHTML = "Required";
		$error = true;
	}
	else {
		document.getElementById("errorCompanyName").innerHTML = "";	
	}
	
	//Telephone Check
	if (document.getElementById("Telephone").value == "") {
		document.getElementById("errorTelephone").innerHTML = "Required";
		$error = true;
	}
	else {
		document.getElementById("errorTelephone").innerHTML = "";	
	}

	if ($error == false) {	
		document.getElementById("ContactSubmit").value = "Please Wait...";		
		var Query= "submit.php?";
		var SelectedOffices = get_cookie("meadprop");
		SelectedOffices = SelectedOffices.substring(0,SelectedOffices.length-1)
		Query += "Email=" + escape(document.getElementById("Email").value);
		Query += "&CompanyName=" + escape(document.getElementById("CompanyName").value);
		Query += "&FirstName=" + escape(document.getElementById("FirstName").value);
		Query += "&Size=" + escape(document.getElementById("Size").value);
		Query += "&Telephone=" + escape(document.getElementById("Telephone").value);
		Query += "&SelectedOffices=" + SelectedOffices;
		Query += "&ExtraInfo=" + escape(document.getElementById("ExtraInfo").value);
		submitData(Query);
	}
}
