/*<![CDATA[*/
/*****************************************************************************
//-> START General Functions
*****************************************************************************/

// pre-submit callback
function showRequest(formData, jqForm, options) {

	var form = jqForm[0];

	if (!form.name.value) {
	
		$('.msg').slideDown(500, removeMsg).html("<img class='png_bg' src='/images/attention.png' width='11' height='11' alt='' /> Please fill in your name.");
		form.name.focus();
		
		$('#orderform img.loader').fadeOut('slow',function(){$(this).remove()});
		
		return false;
	}

	if (!echeck(form.email.value)) {
		$('.msg').slideDown(500, removeMsg).html("<img class='png_bg' src='/images/attention.png' width='11' height='11' alt='' /> Please fill in a valid email address.");
		form.email.focus();
		
		$('#orderform img.loader').fadeOut('slow',function(){$(this).remove()});
		
		return false;
	}

	if (!form.comments.value) {
	
		$('.msg').slideDown(500, removeMsg).html("<img class='png_bg' src='/images/attention.png' width='11' height='11' alt='' /> Please fill in the project details.");
		form.comments.focus();
		
		$('#orderform img.loader').fadeOut('slow',function(){$(this).remove()});
		
		return false;
	}

	if (!verify(form.verify.value)) {
		$('.msg').slideDown(500, removeMsg).html("<img class='png_bg' src='/images/attention.png' width='11' height='11' alt='' /> Please answer the question, we need to check that you'r human...");
		form.verify.focus();
		
		$('#orderform img.loader').fadeOut('slow',function(){$(this).remove()});
		
		return false;
	}

	$('.msg').fadeIn(500).html("<img src='/images/loadinfo.gif' width='16' height='16' alt='' /> Please wait, processing form...");
	$('#orderform').css({"display":"none"});
	
}

// --> post-submit callback
function showResponse()  {
	$('.msg').fadeIn(500).html('<img class="png_bg" src="/images/success.png" width="16" height="16" alt="" /> <strong>Order Accepted!</strong><br />If you are not being redirected in 5 seconds, please <a href="/payment/">press here</a>.');
	
	$('#orderform img.loader').fadeOut('slow',function(){$(this).remove()});
	//$(this).find("input#submit").attr('disabled','disabled');
	
	
	$('#orderform').oneTime(5000,function() {
		document.location.href="/payment/";
	});
	
}

// --> removing the status message with a 5sec timer activated
function removeMsg() {
	$('.msg').oneTime(4000,function() {
		$(this).slideUp(500);
	});
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   //alert("Invalid E-mail ID")
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   //alert("Invalid E-mail ID")
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		//alert("Invalid E-mail ID")
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		//alert("Invalid E-mail ID")
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		//alert("Invalid E-mail ID")
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		//alert("Invalid E-mail ID")
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		//alert("Invalid E-mail ID")
		return false;
	 }
	 return true;					
}

// --> START CAPTCHA
var flag = false;
var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);

function verify(){

	if(!flag){
		var c = a + b;
		var d = $("#verify:input").val();
		
		$('label.verify').append("What is "+ a + " + " + b +"? ");
		flag = true;
		return false;
	}
	
	var c = parseInt($("#aVal:input").val()) + parseInt($("#bVal:input").val());
	var d = $("#verify:input").val();

	if (flag && c == d) {
		return true; 
	}else{
		return false;
	}
	return false;	
}

// --> START Date Picker
var d = new Date();
var day = d.getDay();
var interval = 3;
	if(day > 2 && day < 7) interval+= 2;

/*****************************************************************************
//-> END General Functions
*****************************************************************************/

/*****************************************************************************
//-> START Window Load
*****************************************************************************/

$(window).load(function () {
						 
// --> START CONTACT FORM
	var options = {
		url:"/process-order.asp",
		beforeSubmit:  showRequest,  // pre-submit callback
		success:       showResponse,  // post-submit callback
		clearForm: true,        // clear all form fields after successful submit
		resetForm: true        // reset the form after successful submit
	};

	$('#orderform').submit(function() { 
		$(this).find("input#submit").after('<img src="/images/ajax-loader.gif" class="loader" />');
		
		//--> replacing the "enter" with "<br />" on the textarea
		var txtStr = document.getElementById("comments").value;
		document.getElementById("comments").value = txtStr.replace(/\n/gi, "<br />");

		$(this).ajaxSubmit(options);
		
  		//console.log('clearing cookies...')
  		$.cookie('pageQty',null)
  		$.cookie('totalPrice',null)
		
		 return false;
	}); 
	// --> END Contact form
	
// --> Init CAPTCHA
	$('#orderform').find("fieldset").append('<input type="hidden" name="aVal" id="aVal" value="" /><input type="hidden" name="bVal" id="bVal" value="" />');
	$("#aVal:input").val(a);
	$("#bVal:input").val(b);
	
	verify()		
	
});

/*****************************************************************************
//-> END Window Load
*****************************************************************************/

/*]]>*/
