// Copyright © 2009 Invoke Media

$(document).ready(function() {
	$('#skilength').focus(function () {
		$('#skiselect').attr('checked', 'checked');
	});
	$('#telelength').focus(function () {
		$('#teleselect').attr('checked', 'checked');
	});
	$("#formContainer .submit").click(function(action) {
	submitForm('formContainer','registrationForm');
								
// username - 4-10 chars, uc, lc, and underscore only.
function checkName(strng) {

   var illegalChars = /[^A-Za-z0-9 _-]/; // allow letters, numbers, and underscores
   
   if (strng != "") {
   var error = "";
	   if ((strng.length < 2) || (strng.length > 20)) {
	
		  error = "The name is the wrong length.<br />";
	   } else if (illegalChars.test(strng)) {
	
		  error = "The name contains illegal characters.<br />";
	   } 
   return error;
   }
}    

// email
function checkEmail(strng) {

   if (strng != "") {
   var error="";
	   var emailFilter=/^([\w]+)(.[\w]+)*@([\w-]+\.){1,5}([A-Za-z]){2,6}$/;
	   if (!(emailFilter.test(strng))) {
	
		  error = "Please enter a valid email address.<br />";
	   } else {
	
		  //test email for illegal characters
		  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			 if (strng.match(illegalChars)) {
	
				error = "The email address contains illegal characters.<br />";
			 }
	   }
	   return error;    
   }
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone(strng) {

   if (strng != "") {
   var error = "";

	   var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	   if (isNaN(parseInt(stripped))) {
	
		  error = "The phone number contains illegal characters.<br />";
	
	   }
	   if (!(stripped.length >= 10)) {
	
		  error = "The phone number is the wrong length.<br />Make sure you included an area code.<br />";
	   } 
	   return error;
   }
}



// non-empty textbox

function isEmpty(strng) {

   var error = "";
   if (strng.length == 0) {
	  error = "The mandatory text area has not been filled in.<br />"
   }
   return error;	  
}

function submitForm(e, form) {
	var theForm = $(form);
	var container = $(e);

	var why = "";
	
	var inputs = new Array('name', 'street', 'city', 'province', 'postal', 'country', 'phone', 'email', 'emcontact', 'emrelation', 'emphone', 'dates');
	
	for(i=0;i<=inputs.length-1;i++) {
		itest = inputs[i];
		
		if(!$("#"+itest).val()) {
			if($("label."+itest).length > 0) {
				$("label."+itest).css("color","#bb0000");
				why = (why.length) ? why : 'Please fill out all required fields.<br />';
			} else {
				why += 'You require a '+ itest +'.<br />';
			}
		} else {
			$("label."+itest).css("color","#8D8D8D");
		}
	}
	
	if($("#name").val()) {why += checkName($("#name").val())};
	if($("#email").val()) {why += checkEmail($("#email").val())};
	if($("#phone").val()) {why += checkPhone($("#phone").val())};
	
	if(!$('#cheque').attr('checked') && !$('#credit').attr('checked')) {
		$("label.payment").css("color","#bb0000");
		why += 'You must choose a payment method.<br />';
	} else {
		$("label.payment").css("color","#8D8D8D");
	}
	
	if(!$('#depositmonth').val() || !$('#depositday').val() | !$('#deposityear').val()) {
		$("label.submitted").css("color","#bb0000");
		why += 'You must enter a complete deposit date.<br />';
	} else {
		$("label.submitted").css("color","#8D8D8D");
	}
	
	if($('#skiselect').attr('checked') && $('#skilength').val() <= 1) {
		$("label.skiselect").css("color","#bb0000");
		why += 'You must enter a length for your skis.<br />';
	} else {
		$("label.skiselect").css("color","#8D8D8D");
	}
	if($('#teleselect').attr('checked') && $('#telelength').val() <= 1) {
		$("label.teleselect").css("color","#bb0000");
		why += 'You must enter a length for your telemark skis.<br />';
	} else {
		$("label.teleselect").css("color","#8D8D8D");
	}
	
	if(!$('#agreement').attr('checked')) {
		if(why == "") {
			$("label.agreement").css("color","#bb0000");
			why += 'You must read and agree to the terms at the bottom of the form.<br />';
		}
	} else {
		$("label.agreement").css("color","#8D8D8D");
	}

	if(why != "") {
		$("#formContainer #errorMsgBox").html(why);
		$("#formContainer .buttonAlert").html(' The form is incomplete.');
		return false;
	} else {
		$("#formContainer #errorMsgBox").html('Sending Form Information.');
		$("#formContainer .buttonAlert").html('Sending...');
	  /*$("#contactForm").send({
		 update: $("errorMsgBox"),
		 onRequest: function() {
			container.addClass('ajax-loading');
		 },
		 onComplete: function() {
			container.removeClass('ajax-loading');
		 }
	  });*/
	  
	var contactForm = $("#formContainer #registrationForm");
	
	$(':input', contactForm).each(function() {
		inputs.push(this.name + '=' + escape(encodeURIComponent(this.value)));
	})
var datums = contactForm.find(':input').serialize();
	jQuery.ajax({
		data: datums, //inputs.join('&'),
		url: contactForm.attr('action'),
		timeout: 10000,
		type: 'POST',
		error: function() {
			console.log("Something Failed...");
			},
		success: function(r) {
			//do something with the response 'r' text
			$("#formContainer #registrationForm").html("")
	  		$("#formContainer #errorMsgBox").html(r);
			}
		})

   }
}


/*$('#contactForm').submit(function() {
	return false;
})*/
	action.preventDefault();

	});
});

