$(function() {
	$('.error').hide();
	/*
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function(){
		$(this).css({color:"#1A63A5", border:"solid 1px #EC5C27"});
	});
	$('input.text-input').blur(function(){
		$(this).css({color:"#999999", border:"solid 1px #CCCCCC"});
	});
	*/

	$(".button").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();
			
		var Title = document.getElementById('registration').Title.options[document.getElementById('registration').Title.selectedIndex];
		if ((Title.value == "BLANK") || (Title.value == "")) {
			$("label#Title_Error").show();
			$("input#Title").focus();
			return false;
		}
			
		var Name = $("input#Name").val();
		if (Name == "") {
			$("label#Name_Error").show();
			$("input#Name").focus();
			return false;
		}
	
		var Address = $("input#Address").val();
		if (Address == "") {
			$("label#Address_Error").show();
			$("input#Address").focus();
			return false;
		}
			
		var City = $("input#City").val();
		if (City == "") {
			$("label#City_Error").show();
			$("input#City").focus();
			return false;
		}
			
		var Province = document.getElementById('registration').Province.options[document.getElementById('registration').Province.selectedIndex];
		if ((Province.value == "BLANK") || (Province.value == "")) {
			$("label#Province_Error").show();
			$("input#Province").focus();
			return false;
		}
			
		var Postal_Code = $("input#Postal_Code").val();
		if (Postal_Code == "") {
			$("label#Postal_Code_Error").show();
			$("input#Postal_Code").focus();
			return false;
		}

		var Phone = $("input#Phone").val();
		if (Phone == "") {
			$("label#Phone_Error").show();
			$("input#Phone").focus();
			return false;
		}
			
		var Email = $("input#Email").val();
		if (!checkEmail(Email)) {
			$("label#Email_Error").show();
			$("input#Email").focus();
			return false;
		}
	
		var Delegate_Type = document.getElementById('registration').Delegate_Type.options[document.getElementById('registration').Delegate_Type.selectedIndex];
		if ((Delegate_Type.value == "BLANK") || (Delegate_Type.value == "")) {
			$("label#Delegate_Type_Error").show();
			$("input#Delegate_Type").focus();
			return false;
		}
			
		var Institution = $("input#Institution").val();
		if (Institution == "") {
			$("label#Institution_Error").show();
			$("input#Institution").focus();
			return false;
		}
			
		var Specialty = $("input#Specialty").val();
		if (Specialty == "") {
			$("label#Specialty_Error").show();
			$("input#Specialty").focus();
			return false;
		}
		
		var Notes = document.getElementById('registration').Notes;
			
		var dataString = 'Title=' + Title.value + '&Name=' + Name + '&Address=' + Address + '&City=' + City + '&Province=' + Province.value + '&Postal_Code=' + Postal_Code + '&Phone=' + Phone + '&Email=' + Email + '&Delegate_Type=' + Delegate_Type.value + '&Institution=' + Institution + '&Specialty=' + Specialty + '&Notes=' + Notes.value;

		//alert(dataString); return false;
			
		$.ajax({
			type: "POST",
			url: "registration-processor.php",
			data: dataString,
			success: function() {
				$('#contact_form').html("<div id='message'></div>");
				$('#message').html("<h2>Registration Form Submitted!</h2>")
				.append("<p><strong>NOTE:</strong> If you require accomodation during your stay, please <a href='https://reservations.ihotelier.com/crs/g_reservation.cfm?groupID=563320&hotelID=13128' target='_blank'><strong>click here</strong></a> to register with the Pillar and Post hotel.</p>")
				.hide()
				.fadeIn(1500, function() {
					$('#message').show();
				});
			}
		});
		return false;
	});
});


function checkEmail(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
	   return false
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	
	 return true					
}


function activateField(field, str) {

	field.style.color = '#666666';
	field.style.border = 'solid 1px #EC5C27';
	if (str != '') {
		if (field.value == str) {
			field.value = '';
		}
	}
}


function deactivateField(field, str) {

	field.style.border = 'solid 1px #CCCCCC';
	if ((field.value == '') || (field.value == 'BLANK')) {
		field.style.color = '#999999';
		field.value = str;
	}
}

