function getParameterByName( name ){  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  var regexS = "[\\?&]"+name+"=([^&#]*)";  var regex = new RegExp( regexS );  var results = regex.exec( window.location.href );  if( results == null )    return "";  else    return results[1];}

var isValidEmailAddress = function(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
};

$(function() {
	
	$("#menu a").each(function(){
	
		var dropdownid = $(this).attr("id").replace(/menu\-item/, "drop-down");
		if (dropdownid) {
			var dropdown = $("#" + dropdownid);
			if (dropdown.size()>0) {
				
				var hideDropdown = function() {
					dropdown.hide();
				};
				
				var dropdownTimer = null;
				
				$(this).mouseenter(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					var pos = $(this).offset();
					var x = pos.left - 20;
					var y = pos.top + 44;
					dropdown.css({
						"left": x + "px",
						"top": y + "px"
					});
					dropdown.show();
				});
				
				$(this).mouseleave(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					dropdownTimer = setTimeout(hideDropdown, 250);
				});
				
				dropdown.mouseenter(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
				});

				dropdown.mouseleave(function() {
					if (dropdownTimer) { clearTimeout(dropdownTimer); }
					dropdownTimer = setTimeout(hideDropdown, 250);
				});
				
			}
		}		
	
	});
	
	var header = $("div.column-left div.header");
	var headerClass = header.attr("class").replace(/header\s/, "");
	header.find("a").each(function() {
		if (headerClass==$(this).attr("id")) {
			$(this).addClass("selected");
			return false;
		}
	});
	
	$("tbody tr").each(function(index, el) {
		$(el).attr("class", index%2==0? "even" : "odd");
	});
	
	var errorEmptyFields = "<div class='message error'>Please fill in all the highlighted field.</div>";
	var errorEmailFields = "<div class='message error'>Please provide a valid email address.</div>";
	var errorEForm = "<div class='message error'>There was an error sending your details. Please try again later.</div>";
	var successEForm = "<div class='message success'>Thank you. Your details have been sent!</div>";
	var sendingEForm = "<div class='message sending'>Please wait while we send your details.</div>";
	
	var sendEForm = function() {

		$("label").removeClass("error");
		$("div.message").remove();

		var fieldNames = new Array(
			"Family_name",
			"Given_name",
			"Maiden_name",
			"Postal_address",
			"Home_Phone",
			"Mobile_phone",
			"Email_address",
			"Company_name",
			"Your_position_in_the_company",
			"Company_phone",
			"Company_Fax",
			"Company_Email",
			"Website",
			"Services",
			"Year_left",
			"Recent_news"
		);
		
		// check empty fields
		
		var emptyFields = new Array("Family_name", "Email_address");
		var empty = false;
		
		$.each(emptyFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var txt = field.attr("value");
			if (!txt) {
				empty = true;
				field.parents("tr").find("label").attr("class", "error");
			}
			
		});
		
		if (empty) {
			$(errorEmptyFields).insertBefore("#KT_Custom1");
			return false;
		}
		
		// check emails
		
		var invalidEmailFields = new Array("Email_address");
		var invalidEmails = false;
		
		$.each(invalidEmailFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var ok = isValidEmailAddress(field.attr("value"));
			if (!ok) {
				invalidEmails = true;
				field.parents("tr").find("label").attr("class", "error");
			}
		
		});
		
		if (invalidEmails) {
			$(errorEmailFields).insertBefore("#KT_Custom1");
			return false;
		}
		
		// send
		
		$(sendingEForm).insertBefore("#KT_Custom1");
		$("#KT_Custom1").hide();
		
		var dataValues = "c=SendEForm&time=" + new Date();
		$.each(fieldNames, function(index, fieldName) {
			var field = $("[name=" + fieldName + "]");
			var value = field.attr("value");
			dataValues += "&" + fieldName + "=" + value;
		});		
		
		$.ajax({
			type: "GET",
			url: "Requests/Request.aspx",
			data: dataValues,
			success: function(data, textStatus){
				$("div.message").remove();
				if (data.match(/ok/)) {
					$(successEForm).insertBefore("#KT_Custom1");
				} else {
					$(errorEForm).insertBefore("#KT_Custom1");
					$("#KT_Custom1").show();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("div.message").remove();
				$(errorEForm).insertBefore("#KT_Custom1");
				$("#KT_Custom1").show();
			}
		});		
		
		return false;

	};
	
	$("#KT_Custom1").click(sendEForm);
	
	// enrolment
	
	var sendEnrolementForm = function() {


		$("label").removeClass("error");
		$("div.message").remove();

		var fieldNames = new Array(
			"Title",
			"Christian_Name",
			"Family_Name",
			"Address",
			"Suburb",
			"State",
			"postcode",
			"Australian_resident",
			"Country_of_Residence",
			"Phone_No___Home_",
			"Phone_No___Business_",
			"Email_Address",
			"Gender_of_Proposed_Applicant",
			"Proposed_Applicants_Name",
			"Proposed_entry_level_",
			"_Proposed_year_of_entry",
			"Details_of_other_siblings",
			"How_did_you_hear_about_pittwater_house_"
		);
		
		// check empty fields
		
		var emptyFields = new Array("Family_Name", "Email_Address");
		var empty = false;
		
		$.each(emptyFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var txt = field.attr("value");
			if (!txt) {
				empty = true;
				field.parents("tr").find("label").attr("class", "error");
			}
			
		});
		
		if (empty) {
			$(errorEmptyFields).insertBefore("#KT_Custom2");
			return false;
		}
		
		// check emails
		
		var invalidEmailFields = new Array("Email_Address");
		var invalidEmails = false;
		
		$.each(invalidEmailFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var ok = isValidEmailAddress(field.attr("value"));
			if (!ok) {
				invalidEmails = true;
				field.parents("tr").find("label").attr("class", "error");
			}
		
		});
		
		if (invalidEmails) {
			$(errorEmailFields).insertBefore("#KT_Custom2");
			return false;
		}
		
		// send
		
		$(sendingEForm).insertBefore("#KT_Custom2");
		$("#KT_Custom2").hide();
		
		var dataValues = "c=SendEnrolemntForm&time=" + new Date();
		$.each(fieldNames, function(index, fieldName) {
			var field = $("[name=" + fieldName + "]");
			var value = "";
			if (field.attr("type")=="radio") {
				field = $("[name=" + fieldName + "]:checked");
			} 
			value = field.attr("value");	
			dataValues += "&" + fieldName + "=" + value;
		});		
		
		$.ajax({
			type: "GET",
			url: "Requests/Request.aspx",
			data: dataValues,
			success: function(data, textStatus){
				$("div.message").remove();
				if (data.match(/ok/)) {
					$(successEForm).insertBefore("#KT_Custom2");
				} else {
					$(errorEForm).insertBefore("#KT_Custom2");
					$("#KT_Custom2").show();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("div.message").remove();
				$(errorEForm).insertBefore("#KT_Custom2");
				$("#KT_Custom2").show();
			}
		});		
		
		return false;

	};	
	

	$("#KT_Custom2").click(sendEnrolementForm);

// RSVP


var sendRSVPForm = function() {


		$("label").removeClass("error");
		$("div.message").remove();

		var fieldNames = new Array(
			"First_Name",
			"Last_Name",
			"Email_Address",
			"Mobile",
			"Address",
			"Suburb",
			"State",
			"Postcode",
			"Country",
			"Comments"
		);
		
		// check empty fields
		
		var emptyFields = new Array("Email_Address");
		var empty = false;
		
		$.each(emptyFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var txt = field.attr("value");
			if (!txt) {
				empty = true;
				field.parents("tr").find("label").attr("class", "error");
			}
			
		});
		
		if (empty) {
			$(errorEmptyFields).insertBefore("#KT_Custom3");
			return false;
		}
		
		// check emails
		
		var invalidEmailFields = new Array("Email_Address");
		var invalidEmails = false;
		
		$.each(invalidEmailFields, function(index, fieldName) {
		
			var field = $("[name=" + fieldName + "]");
			var ok = isValidEmailAddress(field.attr("value"));
			if (!ok) {
				invalidEmails = true;
				field.parents("tr").find("label").attr("class", "error");
			}
		
		});
		
		if (invalidEmails) {
			$(errorEmailFields).insertBefore("#KT_Custom3");
			return false;
		}
		
		// send
		
		$(sendingEForm).insertBefore("#KT_Custom3");
		$("#KT_Custom3").hide();
		
		var dataValues = "c=sendRSVPForm&time=" + new Date();
		$.each(fieldNames, function(index, fieldName) {
			var field = $("[name=" + fieldName + "]");
			var value = "";
			if (field.attr("type")=="radio") {
				field = $("[name=" + fieldName + "]:checked");
			} 
			value = field.attr("value");	
			dataValues += "&" + fieldName + "=" + value;
		});		
		
		$.ajax({
			type: "GET",
			url: "Requests/Request.aspx",
			data: dataValues,
			success: function(data, textStatus){
				$("div.message").remove();
				if (data.match(/ok/)) {
					$(successEForm).insertBefore("#KT_Custom3");
				} else {
					$(errorEForm).insertBefore("#KT_Custom3");
					$("#KT_Custom3").show();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("div.message").remove();
				$(errorEForm).insertBefore("#KT_Custom3");
				$("#KT_Custom3").show();
			}
		});		
		
		return false;

	};	
	

	$("#KT_Custom3").click(sendRSVPForm);


	$("#main-image-container").click(function(){
		document.location="Enrolment-request-pack.aspx";
		return false;
	});

});

