/* jquery document read */
$(document).ready(function(){
						  
	//doPreview();
	$("#errorMsg").hide();
	$("#errorBox").hide();
		
	/*** submit the login form ****/
	$("form#login_form").submit(function(){
		
	/*do validation */
		var error="";
		var username = $("#username").val();
		var password = $("#password").val();
		
		if (!username){
			error += "- Enter Username\n"; 
		}
		if (!password){
			error += "- Enter Password\n"; 
		}
		if (error){
			alert("- Please correct the following errors:\n" + error + "");
			return false;
			}
		
		var dataStr = $("form#login_form").serialize();
		
		$.ajax({
	  		type: "POST",
	   		url: "login.php",
			data: dataStr,
			success: function(data){
				var ret = data.split("#");
				if (jQuery.trim( ret[0] ) == 'error')
					{
						alert(ret[1]);
						return false;
						//$("#errorBox").html("<b>Please correct the following errors:</b><br>" + ret[1] + "");
						//$("#errorBox").show();
						//return false;
						
					} else {
						window.location = "checkout.php";
						return false;
						}
				} 
		});		
	
	return false;	
	});

	/*** do the registration form submit ***********/
	
	$("form#registration_form").submit(function(){
		/* do validation */
		var error = "";
		var f_name = $("#f_name").val();
		var dob_year = $("#dob_year").val();
		var dob_month = $("#dob_month").val();
		var dob_date = $("#dob_date").val();
		var email = $("#email").val();
		var address = $("#address").val();
		var city = $("#city").val();
		var zip_code = $("#zip_code").val();
		var country = $("#country").val();
		var username = $("#username").val();
		var password = $("#password").val();
		var c_password = $("#c_password").val();
		var chkTerm = $('#terms:checked').val();
		
		if ( !f_name) { 
			error += "- Enter your name<br>"; 
			} else {
				var validname = /^[a-zA-Z.' _]*$/;
				if(!validname.test(f_name)) {
					error += "- Invalid name<br>";
					$("#f_name").val('');
				}
		}
		if((!dob_month) || (!dob_date) || (!dob_year)){
			error += "- Enter date of birth<br>";
			} else {
				if(!validDate(dob_date,dob_month,dob_year)){
					error += "- Enter a valid date of birth<br>";
				}
		}
			
		if (!email) {
			error += "- Enter your email address<br>";
			} else {	
				if(!isValidEmailAddress(email)) {
					error += "- Enter a valid email address<br>";
					$("#email").focus();
					$("#email").val('');
					}
				}
		if (!address) {
			error += "- Enter your postal address<br>"; 
			} else {
			 	var validAdd = /^[a-zA-Z0-99\/#. -,\/\n]*$/;
				if (!validAdd.test(address)){
					error += "- Invalid address<br>";
					$("#address").val('');
				 }
		}
		if (!city) { 
			error += "- Enter city name<br>"; 
			} else {
				var validname = /^[a-zA-Z]*$/;
				if(!validname.test(city)) {
					error += "- Invalid city name<br>";
					$("#f_name").val('');
				}
		}
		if (!zip_code){ 
			error += "- Enter your zip code<br>"; 
			} else {
				var validZip = /^[0-9]*$/;
				var textlength = zip_code.length;
     			var limit = 4; 
				if (!validZip.test(zip_code)){ 
					error += "- Invalid zip code<br>";
					$("#zip_code").val('');
				 	}else if (limit >= textlength) {
					error += "- Enter a valid zip code<br>"; 
				}
		}
		
		
		if (!country){
			error += "- Selecy your country<br>"; 
		}
			
		if (!username){
			error += "- Enter your username<br>"; 
		}
		if (!password){
			error += "- Enter your password<br>"; 
		}
		if (!c_password){
			error += "- Enter confim password<br>"; 
		}
		if (password){
			if (password.length < 4  || password.length > 16 ){
				error += "- Password between 4 - 16 chars only<br>"; 
			}
			if (c_password != password){
				error += "- Password & confirm password should be same<br>"; 
			}
		}
		if(!chkTerm){
			error += "- Click on the checkbox and agree the terms and condition.";
		}
		if (error)
			{
				$("#errorBox").html("<b>Please correct the following errors:</b><br>" + error + "");
				$("#errorBox").show();
				//alert("Inside error");return false;
				return false;
			}
			
		var dataStr = $("form#registration_form").serialize();
		
		
		$.ajax({
	  		type: "POST",
	   		url: "registration.php",
			data: dataStr,
			success: function(data){
				
						
				var ret = data.split("#");
								
				if (jQuery.trim( ret[0] ) == 'error')
					{
						$("#errorBox").html("<b>Check the following errors:</b><br>" + ret[1] + "");
						return false;
						
					} 
				else {
						
					window.location = "checkout.php";
					return false;
				}
				
			} 
		});
		return false;										
	});
	 
	// checkout page validation
	$('#checkout_copy').click(function (){
		var chkBox = $('#checkout_copy:checked').val();
		if(chkBox){
			$("#ship_details").hide();	
			} else {
				$("#ship_details").show();
		}	
	});
	
	$("input[name='payMode']").click(function (){
										
		var payMode = $("input[name='payMode']:checked").val()
		//alert(payMode);
		//return false;
		if(payMode == "crediCard"){
			$("#paymentInfo").show();	
			} else {
				$("#paymentInfo").hide();
		}	
	});
	
	$("form#myCart").submit(function (){
		
		var billName = $("#bill_fname").val();
		var billAdd = $("#bill_address").val();
		var billCity = $("#bill_city").val();
		var billState = $("#bill_state").val();
		var billZip = $("#bill_zip").val();
		var billPhone = $("#bill_phone").val();
		var billEmail = $("#bill_email").val();
		
		var chkShip = $('#checkout_copy:checked').val();
		var payMode = $("input[name='payMode']:checked").val()
		
		
		var error = '';
		if(!billName){
			error +="Enter your full name <br />";
			} else {
				if(!validFirstCharacter("bill_fname")){
					error +="Name first character cannot be space <br />"
				}
		}
		if(!billAdd){
			error +="Enter your address <br />";
		}
		if(!billCity){
			error +="Enter your city <br />";
		}
		if(!billState){
			error +="Select your state <br />";
		}
		if(!billZip){
			error +="Enter your zip/postal code <br />";
			} else {
				if(!isValidZip("bill_zip")){
					error +="Enter a valid zip/postal code <br />";
				}
		}
		if(!billPhone){
			error +="Enter your phone number <br />";
			} else {
				if(!validPhone("bill_phone")){
					error +="Enter a valid phone number <br />";
				}
		}
		if(!billEmail){
			error +="Enter your email address <br />";
			} else {
				if(!isValidEmailAddress(billEmail)){
					error +="Enter a valid email <br />";
				}
		}
		
		if(!chkShip){
			var shipName = $("#ship_fname").val();
			var shipAdd = $("#ship_address").val();
			var shipCity = $("#ship_city").val();
			var shipState = $("#ship_state").val();
			var shipZip = $("#ship_zip").val();
			var shipPhone = $("#ship_phone").val();
			var shipEmail = $("#ship_email").val();
			
			if(!shipName){
			error +="Enter shipping full name <br />";
			} else {
				if(!validFirstCharacter("ship_fname")){
					error +="Shipping Name first character cannot be space <br />"
				}
			}
			if(!shipAdd){
				error +="Enter shipping address <br />";
			}
			if(!shipCity){
				error +="Enter shipping city <br />";
			}
			if(!shipState){
				error +="Select shipping state <br />";
			}
			if(!shipZip){
				error +="Enter shipping zip/postal code <br />";
				} else {
					if(!isValidZip("ship_zip")){
						error +="Enter a valid shipping zip/postal code <br />";
					}								
			}
			if(!shipPhone){
				error +="Enter shipping phone number <br />";
				} else {
				if(!validPhone("ship_phone")){
					error +="Enter a valid shipping phone number <br />";
				}
			}
			if(!shipEmail){
				error +="Enter shipping email address <br />";
				} else {
					if(!isValidEmailAddress(billEmail)){
						error +="Enter a valid email <br />";
					}				
			}	
		}
		if(!payMode){
			error +="Select your payment type <br />";
		}
		// if paymode is credit card validate credit card info
		if(payMode == "crediCard"){
			var card_name = $("#card_name").val();
			var card_type = $("#card_type").val();
			var card_number = $("#card_number").val();
			var card_ccv = $("#card_ccv").val();
			var exp_month = $("#month").val();
			var exp_year = $("#year").val();
			
			if(!card_name){
				error +="Enter the name on the credit card <br />";
				} else {
					if(!validFirstCharacter("ship_fname")){
						error +="Shipping Name first character cannot be space <br />"
					}
			}
			if(!card_type){
				error +="Select your card type <br />";
			}
			if(!card_number){
				error +="Enter the credit card number <br />";
			}
			if(!card_ccv){
				error +="Enter CCV number <br />";
			}
			if(!exp_month){
				error +="Select card expiry month <br />";
			}
			if(!exp_year){
				error +="Select card expiry year <br />";
			}	
			
			
		}	
		
		if(error){
			$("#errorBox").html("<b>Please correct the following errors:</b><br>" + error + "");
			$("#errorBox").show();
			return false;
		}
		
		/*
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		var billName = $("#bill_fname").val();
		*/
		
		
		var dataStr = $("form#myCart").serialize();
				
		$.ajax({
			type: "POST",
			url: "order.php",
			data: dataStr,
			success: function (data){
				alert(data);
				return false;
			}
		});
		return false;
	});
	// End of the checkout page validation
	
	
	
});		// End of the DOM

var valid = {
  alpha:/^[a-zA-Z\s]*$/,
  alpha_with:/^([a-zA-Z\s\.\''])*$/,	// Alphabets with ''','.'
  alphanumeric_except:/^([a-zA-Z0-9\s\/\.\''\(\)\-\_\{\}\#\,\[\&\""\]])*$/,	// Residenced address regular expression
  alphanumeric:/^[a-zA-Z0-9\-_/$]+[a-zA-Z0-9\s]*$/,
  alphanumeric_pwrd:/^[a-zA-Z0-9]+[a-zA-Z0-9\s]*$/, 
  password: /(?!^[a-z]*$)(?!^[\x21-\x40\x5B-\x60\x7B-\x7E]*$)^([\x21-\x7E]{5,10})$/i,
  // password_inv: /^[\x21-\x7E]*$/,
  password_inv: /^([a-zA-Z0-9\@\_\#\.]*)$/,
  // pass_new: ^([a-zA-Z0-9@*#]{4,8})$/,
  email: /^[a-z0-9\-\_\+]+(\.[a-z0-9\-\_\+]+)*\@(([a-z0-9\-\_\+]+(\.[a-z0-9\-\_\+]+)*)(\.[a-z]{2,3})|([0-9]+\.){3}[0-9]+)$/i,
  int_float:/^([0-9]{1,6}|[0-9]{1,4}\.[0-9]{1,2})$/,  
  //zip:/^([0-9]{5,6})$/,
  zip:/^\d{5}$/,
  phone_no:/^[0-9]\d{2}-\d{3}-\d{4}$/,
  
  numb:/^([0-9])$/,
  ssn:/^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/, // SSN format 879-89-8989;
  end:0

};

// Function to validate text field - First Character should not be blank
function validFirstCharacter(fieldName){
	var txtVal = $("#"+fieldName).val();
	
	if(txtVal.substring(0,1)==' '){		
		//addCss(fieldName);
		return false;
		} else {
			return true;
	}
}

// Function to validate the zip code
function isValidZip(fieldName){
	var txtVal = $("#"+fieldName).val();
	if(!txtVal.match(valid.zip)){			
		return false;			
		} else {
			return true;
	}
}

// Function to validate phone number
function validPhone(fieldName){
	var txtVal = $("#"+fieldName).val();
	if(!txtVal.match(valid.phone_no)){			
			return false;			
			} else {
				return true;
	}	
}

// Email Validation
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
return pattern.test(emailAddress);
}

// Function to validate the Date of Birth
function validDate(dob_date,dob_month,dob_year){
	if(dob_month == 'FEB' && dob_date > 29){		
		return false;
		} else if (dob_month == 'FEB' && dob_date == 29){
			if(dob_year%4){
				return false;
			}
	}
	return true;
}

// Function to validate the leap year
function validLeapYear(dob_year){
	var month = jQuery.trim($("#dob_month").val());
	var date = $("#dob_date").val();
	var year = $("#dob_year").val();
	if(month == 'FEB' && date == 29){
		if(year%4){
			return "Please enter a valid date of birth";
			}
	}
}


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 = "my-cart.php";	
			}
		return false;		 
	   }
	 });
}