// SUGC Javascript Library
// Yes, it needs some work.

// Preload menu images - needs overhaul
function preloadImages() {
     imageObj = new Image();
     images = new Array();
     images[0]="/images/i_menuselect.gif";
     images[1]="/images/i_menuselectdown.gif";
     images[2]="/images/i_linkbuttonWhiteOnBlue.gif";
     for(var i = 0; images.length; i++) imageObj.src = images[i];
}

function checkMailListForm(form) {
	at = form.email.value.indexOf("@") + 1;
	dot =  form.email.value.length - form.email.value.lastIndexOf(".") - 1;
	if (at < 2 || at >= (form.email.value.length - 5) || dot < 0 || dot > 4) {
		alert("Please enter a valid email address.");
		form.email.focus();
		return false;
	}
}

function checkProfileForm(form) {
	
	var numbers = /(^\d+$)|(^\d+\.\d+$)/;
	var letters = /[a-z A-Z]/;
	var dates = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	
	with(form) {
		var alertMsg = "We need these fields filled in, please:\n";
		if (firstname.value == "") alertMsg += "\nPrename";
		if (surname.value == "") alertMsg += "\nSurname";
		if (email.value == "") alertMsg += "\nEmail address";
		if (university_id.value == "" || isInteger(university_id.value) == false) alertMsg += "\nStudent ID Number";
		
		if (term_address_1.value == "") alertMsg += "\nTerm-time Address";
		if (term_city.value == "") alertMsg += "\nTerm-time City";
		if (term_county.value == "") alertMsg += "\nTerm-time County";
		if (term_postcode.value == "") alertMsg += "\nTerm-time Postcode";
		//alert("checking term address");
		if (home_address_1.value == "") alertMsg += "\nHome Address";
		if (home_city.value == "") alertMsg += "\nHome City";
		if (home_county.value == "") alertMsg += "\nHome County";
		if (home_postcode.value == "") alertMsg += "\nHome Postcode";
		
		if (home_address_1.value == "") alertMsg += "\nHome Address";
		if (home_city.value == "") alertMsg += "\nHome City";
		if (home_county.value == "") alertMsg += "\nHome County or Country";
		if (home_postcode.value == "") alertMsg += "\nHome Postcode";
		//alert("checking numbers");
		if (mobile.value == "") alertMsg += "\nMobile Number";
		if (nok_name.value == "") alertMsg += "\nNext-of-Kin Name";
		if (nok_phone.value == "") alertMsg += "\nNext-of-Kin Phone Number";
		//alert("checking NoK address");
		if(nok_is_home.checked == false) {
			if (nok_address_1.value == "") alertMsg += "\nNoK Home Address";
			if (nok_city.value == "") alertMsg += "\nNoK Home City";
			if (nok_county.value == "") alertMsg += "\nNoK Home County or Country";
			if (nok_postcode.value == "") alertMsg += "\nNoK Home Postcode";
		}
		
		if(car_owner.checked == true) {
			if (car_reg.value == "") alertMsg += "\nCar Reg Number";
			if (car_colour.value == "") alertMsg += "\Car Colour";
			if (car_description.value == "") alertMsg += "\nCar Make and Model";
		}
		
		if (alertMsg != "We need these fields filled in, please:\n") {
			alert(alertMsg);
			return false;
		} else {
			return confirm("Please click OK to signify that you will read the red sections within the Lasham Manual and our Airfield Safety section.\nYou must read these to fly with SUGC.");
		}
	}
}

$(document).ready(function(){
	
	
	$("#drivingConfirmation > div a").bind("click", function() {
		var id = $(this).parents("div").attr("id").replace("t", "");
		var drove = $(this).attr("class").replace("drove", "");
		$.getJSON("/ajax.php?q=confirmDriving&drove="+drove+"&id="+id, function(data) {
			if(data.success == 1) {
				$("div#t"+id).slideUp();
			} else {
				alert(data.message);
			}
		});
		return false;
	});
	
	$("#txt_verify").bind("keyup", function() {
		if($(this).attr("value") == "I AGREE") {
			$('#delete-button').removeAttr("disabled");
		} else {
			$('#delete-button').attr("disabled", "disabled");
		}
	});
	
	$('#delete-button').bind("click", function() {
		return confirm("Are you sure you want to delete your account?");
	});
	
	if($("#form-nok_address_different").attr("checked") == false) $(".form-member-nok").hide();
	$("#form-nok_address_different").bind("click", function() {
		$(".form-member-nok").toggle();
	});
	
	if($("#form-car_owner").attr("checked") == false) $(".form-member-car").hide();
	$("#form-car_owner").bind("click", function() {
		$(".form-member-car").toggle();
	});
	
	$('#searchBox').bind("focus blur", function() {
		var placeholder = 'search';
		if ($(this).attr("value") == placeholder) {
			$(this).css('font-style','normal').css('color','#fff').attr("value", "");
		} else if ($(this).attr("value") == "") {
			$(this).css('font-style','italic').css('color','#999').attr("value", placeholder);
		}
	});
	
	$('#input-email-list').bind("focus blur", function() {
		var placeholder = 'e-mail address';
		if ($(this).attr("value") == placeholder) {
			$(this).attr("value", "");
		} else if ($(this).attr("value") == "") {
			$(this).attr("value", placeholder);
		}
	});
});
