$(document).ready(function() {
		
	$("#submitbutton").click(function(e){
			e.preventDefault();
			$("#loading").fadeIn("slow"); //show loading just when link is clicked
			var paramString = buildFormVarString(document.forms[0]);
			$.ajax({
				method: "post",url: "/contact.asp",
				data: paramString,
				timeout: (2*30000),
				beforeSend: function(){
						
					},
				complete: function(){
						$("#loading").fadeOut("slow"); //stop showing loading when the process is complete
					},
				success: function(html){ //so, if data is retrieved, store it in html
						$("#results").html(html); //show the returned result inside #results
						if(html == "<p>Please complete all fields.</p>"){
							// something
						} else {
							$("#contactform").hide("blind"); // hide form
						}
					}, // end of anonymous function to be called on ajax success
				error: function(xhr, status, error) {
						alert("An error has occurred whilst submitting your query. Please try again later or contact tom@invisual.co.uk");
					}
				}); //end $.ajax(
		});

}); //end $(document).ready

function buildFormVarString(theForm){
	var theString = "";
	
	for(i=0; i<theForm.elements.length -1; i++){
		var alertText = "";
		alertText += "Element Type: " + theForm.elements[i].type + "\n";
		
		if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
			alertText += "Element Value: " + theForm.elements[i].value + "\n";
			theString += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
		}
		else if(theForm.elements[i].type == "checkbox"){
			alertText += "Element Checked? " + theForm.elements[i].checked + "\n";
			theString += theForm.elements[i].name + "=" + theForm.elements[i].checked + "&";
		}
		else if(theForm.elements[i].type == "select-one"){
			alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n";
			theString += theForm.elements[i].name + "=" + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "&" ;
		}
		// alert(alertText);
	}
	// alert (theString);
	return theString;
}
