(function() {
	var XMLHTTP_PROGIDS = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP',
			'Msxml2.XMLHTTP.4.0' ];

	var getXhr = function() {
		var http, last_e;
		try {
			http = new XMLHttpRequest();
		} catch (e) {
		}
		if (!http) {
			for ( var i = 0; i < 3; ++i) {
				var progid = XMLHTTP_PROGIDS[i];
				try {
					http = new ActiveXObject(progid);
				} catch (e) {
					last_e = e;
				}

				if (http) {
					break;
				}
			}
		}

		if (!http) {
			throw new Error("XMLHTTP not available: " + last_e);
		} else {
			return http;
		}
	};

	var setContent = function(content) {
		var contentDiv = document.getElementById("content");
		if (contentDiv.hasChildNodes()) {
			contentDiv.removeChild(contentDiv.firstChild);
		}
		contentDiv.appendChild(content);
	};

	var loadPage = function(page, div) {
		var http = getXhr();
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				div.innerHTML = http.responseText;
				setContent(div);
			}
		};
		http.open("GET", page + ".html");
		http.send();
	};

	var servicesDiv;
	services = function() {
		if (!servicesDiv) {
			servicesDiv = document.createElement("div");
			loadPage("services", servicesDiv);
		} else {
			setContent(servicesDiv);
		}
	};

	var aboutUsDiv;
	aboutUs = function() {
		if (!aboutUsDiv) {
			aboutUsDiv = document.createElement("div");
			loadPage("aboutUs", aboutUsDiv);
		} else {
			setContent(aboutUsDiv);
		}
	};

	var contactUsDiv;
	contactUs = function() {
		if (!contactUsDiv) {
			contactUsDiv = document.createElement("div");
			loadPage("contactUs", contactUsDiv);
		} else {
			setContent(contactUsDiv);
		}
	};

	var mapDiv;
	map = function() {
		if (!mapDiv) {
			mapDiv = document.createElement("div");
			setContent(mapDiv);

			mapDiv.style.width="100%";
			mapDiv.style.height="100%";

			initializeGoogleMaps = function() {
				var myLatlng = new google.maps.LatLng(50.84495, 0.47017);
				var myOptions = {
					zoom : 14,
					center : myLatlng,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				}
				var map = new google.maps.Map(mapDiv, myOptions);

				var marker = new google.maps.Marker(
						{
							position : myLatlng,
							title : "Our office is here. Burnside Mews is a small road that is easy to miss; coming from the A529, it is just before a sign shop and lies opposite a Post Office."
						});

				marker.setMap(map);
			}

			var script = document.createElement("script");
			script.type = "text/javascript";
			script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initializeGoogleMaps";
			document.body.appendChild(script);
		} else {
			setContent(mapDiv);
		}
	};
	
	var emailRegex = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;

	sendOnlineQuestion = function() {
		var name = document.getElementById("name");
		var nameText = document.getElementById("nameText");

		var phone = document.getElementById("phone");
		var email = document.getElementById("email");
		var emailText = document.getElementById("emailText");
		var phoneOrEmailText = document.getElementById("phoneOrEmailText");

		var question = document.getElementById("question");
		var questionText = document.getElementById("questionText");

		var isValid = true;

		if (!name.value) {
			nameText.className = "invalidInputText";
			isValid = false;
		} else {
			nameText.className = "validInputText";
		}

		if (!phone.value && !email.value) {
			phoneOrEmailText.className = "invalidInputText";
			isValid = false;
		} else {
			phoneOrEmailText.className = "validInputText";
		}
		
		if (email.value && email.value.search(emailRegex) == -1) {
			emailText.className = "invalidInputText";
			isValid = false;
		} else {
			emailText.className = "validInputText";
		}

		if (!question.value) {
			questionText.className = "invalidInputText";
			isValid = false;
		} else {
			questionText.className = "validInputText";
		}

		if (isValid) {
			var http = getXhr();
			http.onreadystatechange = function() {
				if (http.readyState == 4) {
					var http2 = getXhr();
					http2.onreadystatechange = function() {
						if (http2.readyState == 4) {
							document.getElementById("contactUs-online").innerHTML = http2.responseText;
						}
					};
					http2.open("GET", "onlineQuestionSubmitted.html");
					http2.send();
				}
			};
			var url = "onlineQuestion.php?name=" + escape(name.value)
					+ "&email=" + escape(email.value) + "&phone="
					+ escape(phone.value) + "&question="
					+ escape(question.value);
			http.open("GET", url);
			http.send();
		}
	};
})();

