$(function(){
	$('#pnlForm .submission').append('<a href="#submit" class="submitAlt"></a>');
	$("#pnlForm").append("<div id='LdR4E' style='display:none'></div>");
	$('#pnlForm .submission a').click(function(){
		if(validateForm()==true){			
			var url='/en/processing/Pages/default.aspx';

			$(this).remove();	
			try{
				$.post(url, { name: SafeChars($("#pnlForm li input")[0].value), 
						telephone: SafeChars($("#pnlForm li input")[1].value), 
						email: SafeChars($("#pnlForm li input")[2].value), 
						address: SafeChars($("#pnlForm li input")[3].value), 
						country: SafeChars($("#pnlForm li input")[5].value), 
						province: SafeChars($("#pnlForm li input")[4].value), 
						zip: SafeChars($("#pnlForm li input")[6].value) },function(){
						$('#btnSubmit').trigger('onclick');
				});
			}
			catch(err){
 				window.location.assign("ThankYou.aspx");
  			}
			window.location.assign("ThankYou.aspx");

  		}
		return false;
	});
});

function validateForm()
{
	var a = true;
	var emsg = "<span class='ms-formvalidation'>You must specify a value for this required field.<br/></span>";
	$("#pnlForm .ms-formvalidation").remove(); 
	$("#pnlForm li input").each(function(){
		if(this.value == "" || this.value == null){
			if(this != $("#pnlForm li input")[2]){
				a = false;
				$(this).parent().append(emsg);
			}
		}			
		if(this == $("#pnlForm li input")[2]){
			if(this.value == "" || this.value == "" || !this.value.match(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,6}/i)){
				a = false;
				$(this).parent().append(emsg);	
			}		
		}
	});
	return a;
}

function SafeChars(str)
{
	str = str.replace(/=/gi, "");
	str = str.replace(/\%/gi, "-");
	str = str.replace(/\s/gi, "%20");
	str = str.replace(/&/gi, "and");
	return str.replace(/[\[\]\|\?\^\\`#~><\{\}'"]/gi,"");
}
