/* jquery document read */
$(document).ready(function(){
	
	doPreview();
	$("#errorMsg").hide();
	
	
	/*** submit the checkout form ***/
	$("form#checkout_form").submit(function(){
			alert("Checkout");
			
			$("input").removeClass("inputError");
			$("textarea").removeClass("inputError");
			$("select").removeClass("inputError");

			
			/*** do the validations ***/
			var error = "";
			
			if ( !jQuery.trim ($("#ship_name").val()) ) { $("#ship_name").addClass('inputError'); error += "- Please enter recepient name\n"; }
			if ( !jQuery.trim ($("#ship_address").val()) ) { $("#ship_address").addClass('inputError'); error += "- Please enter recepient address\n"; }
			if ( !jQuery.trim ($("#ship_phone").val()) ) { $("#ship_phone").addClass('inputError'); error += "- Please enter recepient phone number\n"; }
			if ( !jQuery.trim ($("#ship_email").val()) ) { $("#ship_email").addClass('inputError'); error += "- Please enter recepient email\n"; }
			if ( !jQuery.trim ($("#bill_name").val()) ) { $("#bill_name").addClass('inputError'); error += "- Please enter your name\n"; }
			if ( !jQuery.trim ($("#bill_address").val()) ) { $("#bill_address").addClass('inputError'); error += "- Please enter your address\n"; }
			if ( !jQuery.trim ($("#country").val()) ) { $("#country").addClass('inputError'); error += "- Please enter your country\n"; }
			if ( !jQuery.trim ($("#bill_phone").val()) ) { $("#bill_phone").addClass('inputError'); error += "- Please enter your phone\n"; }
			if ( !jQuery.trim ($("#bill_email").val()) ) { $("#bill_email").addClass('inputError'); error += "- Please enter your email\n"; }
			if ( !jQuery.trim ($("#card_type").val()) ) { $("#card_type").addClass('inputError'); error += "- Please select card type\n"; }
			if ( !jQuery.trim ($("#card_name").val()) ) { $("#card_name").addClass('inputError'); error += "- Please enter name as on the card\n"; }
			if ( !jQuery.trim ($("#card_number").val()) ) { $("#card_number").addClass('inputError'); error += "- Please enter card number\n"; }	
			if ( !jQuery.trim ($("#card_ccv").val()) ) { $("#card_ccv").addClass('inputError'); error += "- Please enter CCV number\n"; }			
			if ( !jQuery.trim ($("#month").val()) ) { $("#month").addClass('inputError'); error += "- Please select card expiry month\n"; }
			if ( !jQuery.trim ($("#year").val()) ) { $("#year").addClass('inputError'); error += "- Please select card expiry year\n"; }			
			
			if (error)
				{
					alert("Check these errors:\n"+ error);
					return false;
				}
			
			var dataStr = $("form#checkout_form").serialize();

			//send the data to the payment page
			$.ajax({
				
				//send the data to the payment page
				//1st to save it 
				//then do the payament api call 
				//and get the results
				
				//presuming it always return true
				type: "POST",
	   			url: "doCheckOut.php",
				data: dataStr,
				success: function(data){
						alert(data); return false;
						
						var ret = jQuery.trim(data).split("#");
						
						if ( ret[0] == 'error')
							{
								alert( "Check Errors:\n" + ret[1]);
								return false;
							} else {
								window.location = "confirm.php?orderId="+ ret[1];
								return false;								
								}						
					}
				});
	return false;										
	});

	
	/*** submit the login form ****/
	$("form#login_form").submit(function(){

		/*do validation */
		var error="";
		if ( !jQuery.trim ($("#username").val()) ) { $("#username").addClass('inputError'); error += "- Enter Username\n"; }
		if ( !jQuery.trim ($("#password").val()) ) { $("#password").addClass('inputError'); error += "- Enter Password\n"; }
		
		if (error)
			{
				alert( "Check Errors:\n"+ error);
				return false;
			}
		
		var dataStr = $("form#login_form").serialize();
		
		$.ajax({
	  		type: "POST",
	   		url: "login.php",
			data: dataStr,
			success: function(data){
				
				//alert(data); return false;
				
				var ret = data.split("#");
				
				if (jQuery.trim( ret[0] ) == 'error')
					{
						alert("Check these errors:\n" + "- "+ret[1]);
						return false;
						
					} else {
						window.location = "checkout.php";
						return false;
						}
				} 
		});		
	
	return false;	
	});
	
	
	
	
	
	
	/*** do the registration form submit ***********/
	$("form#registration_form").submit(function(){
		
		$("input").removeClass("inputError");
		$("textarea").removeClass("inputError");
		

		/* do validation */
		var error = "";

		if ( !jQuery.trim ($("#f_name").val()) ) { $("#f_name").addClass('inputError'); error += "- Enter Your name\n"; }
		if ( !jQuery.trim ($("#month").val()) ) { $("#month").addClass('inputError'); error += "- Enter Your Date of Birth (Month)\n"; }
		if ( !jQuery.trim ($("#date").val()) ) { $("#date").addClass('inputError'); error += "- Enter Your Date of Birth (date)\n"; }
		if ( !jQuery.trim ($("#year").val()) ) { $("#year").addClass('inputError'); error += "- Enter Your Date of Birth (tear)\n"; }
		if ( !jQuery.trim ($("#email").val()) ) { $("#email").addClass('inputError'); error += "- Enter Your Email Address\n"; }
		if ( !jQuery.trim ($("#address").val()) ) { $("#address").addClass('inputError'); error += "- Enter Your Postal Address\n"; }
		if ( !jQuery.trim ($("#city").val()) ) { $("#city").addClass('inputError'); error += "- Enter Your City\n"; }
		if ( !jQuery.trim ($("#zip_code").val()) ) { $("#zip_code").addClass('inputError'); error += "- Enter Your Zip Code\n"; }
		if ( !jQuery.trim ($("#country").val()) ) { $("#country").addClass('inputError'); error += "- Selecy Your Country\n"; }
		if ( !jQuery.trim ($("#username").val()) ) { $("#username").addClass('inputError'); error += "- Enter Your Username\n"; }
		if ( !jQuery.trim ($("#password").val()) ) { $("#password").addClass('inputError'); error += "- Enter Your Password\n"; }
		if ( !jQuery.trim ($("#c_password").val()) ) { $("#c_password").addClass('inputError'); error += "- Enter Confim Password\n"; }
		
		var password = jQuery.trim ($("#c_password").val());

		if (password)
			{
			if ( password.length < 4  || password.length > 16 ) 
				{
					$("#password").addClass('inputError'); 
					error += "- Password between 4 - 16 chars only\n"; 
				}
		
			if ( jQuery.trim ($("#c_password").val()) != jQuery.trim ($("#password").val())  ) 
				{
					$("#c_password").addClass('inputError'); 
					$("#password").addClass('inputError'); 
					error += "- Confirm Password & Password should be same\n"; 
				}
			}

		
		if ( error)
			{
				alert( "Check Errors:\n"+ error);
				return false;
			}
			
		var dataStr = $("form#registration_form").serialize();
		//alert(dataStr);
		
		$.ajax({
	  		type: "POST",
	   		url: "registration.php",
			data: dataStr,
			success: function(data){
				
				//alert(data); return false;
				
				var ret = data.split("#");
				
				if (jQuery.trim( ret[0] ) == 'error')
					{
						alert("Check these errors:\n" + ret[1]);
						return false;
						
					} 
				else {
						
					window.location = "checkout.php";
					return false;
				}
				
			} 
		});
		return false;										
	});


	/****** function to copy billing details to shippin on check box**********/
	$("input#checkout_copy").click(function(){	
		if ( $('#checkout_copy').is(':checked') )
			{
				$("#ship_name").val( $("#bill_name").val() );
				$("#ship_address").val( $("#bill_address").val() );
				$("#ship_phone").val( $("#bill_phone").val() );
				$("#ship_email").val( $("#bill_email").val() );
				
						
			} else {
				$("#ship_name").val( '' );
				$("#ship_address").val( '' );
				$("#ship_phone").val( '' );
				$("#ship_email").val( '' );
				}			
		});
	
	$("#toggle").click(function(){
		alert("I got u he he"); return false;
	});




});





this.doPreview = function(){	
	/* CONFIG */
		
		xOffset = 10;
		yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("img.preview").hover(function(e){
		
		var source = this.src;
		this.t = this.title;
		this.title = "";	
		
		
		//the path is 
		//folder/thumb_folder/tn_imgName
		var image_name = source.replace("thumbnail/tn_", "");

		
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='preview'><img src='"+ image_name +"' alt='Image preview' height='250' />"+ c +"</p>");	
		
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("slow");						
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("img.preview").mousemove(function(e){
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};



function removeCartItem(id){
	
			$.post("cart.php", { id: id, mm_action: "doDelete" },
				function(data){

					//alert("Data Loaded: " + data); return false;
					if (jQuery.trim(data) == 'success')
						{
						window.location="cart.php";
						}
					}
		  );
		
};


function doUpdateCart(formId){
	
	var dataStr = $("form#"+formId).serialize();
	 $.ajax({
	   type: "POST",
	   url: "updateCart.php",
	   data: dataStr,
	   success: function(data){
		 
		 if (jQuery.trim(data) == 'success')
		 	{
			window.location = "cart.php";	
			}
		return false;		 
	   }
	 });
}

function toggleMe(){
	alert("Yeaaa I got it");	return false;
	
}


