// JavaScript Document
var ErrMsg_Login = "Invalid Username/Password.";

// For Add cart without quantity

function addCart( id ){

document.getElementById('pid').value= id;
document.getElementById('productfrm').action = "checkout/cart/add";
document.productfrm.submit();

}

// For Add cart with quantity
function addQCart( id ){

document.getElementById('pid').value= id;
//document.getElementById('qty').value= document.getElementById('quantity').value;
document.getElementById('productfrm').action = "checkout/cart/add";
document.productfrm.submit();

}

function validateShipping(form){
	
	if ( ( form.shipping[0].checked == false ) && ( form.shipping[1].checked == false ) ){
       alert("Please check the shipping method.")  
	   document.getElementById("shipping").focus();
       return false;
   
    }else{
         return true;
    }
}
function validatePayment(form){
	
	if ( ( form.payment[0].checked == false ) && ( form.payment[1].checked == false ) ){
       alert("Please check the payment method.")  
	   document.getElementById("payment").focus();
       return false;
   
    }else{
         return true;
    }
}
function validateAddress(){

if(!is_char(document.getElementById("txtFirstName").value)){
	 alert("Please enter the first name.");
	 document.getElementById("txtFirstName").focus();
	 return false;
    }else if(!is_char(document.getElementById("txtLastName").value)){
	 alert("Please enter the last name.");
	 document.getElementById("txtLastName").focus();
	 return false;
   }else if(trim(document.getElementById("txtAddress").value) == ""){
	 alert("Please enter the address.");
	 document.getElementById("txtAddress").focus();
	 return false;
   }else if(!is_char(document.getElementById("txtCity").value)){
	 alert("Please enter the city name.");
	 document.getElementById("txtCity").focus();
	 return false;
   }else if(trim(document.getElementById("txtState").value) == ""){
	 alert("Please enter the state name.");
	 document.getElementById("txtState").focus();
	 return false;
   }else if(!is_validZip(document.getElementById("txtZipcode").value)){
	 alert("Please enter the valid number.Only numeric value allow.");
	 document.getElementById("txtZipcode").focus();
	 return false;
   }else{
         return true;
    }
	
}
function validateBillingAddress(){

if(!is_char(document.getElementById("txtFirstName").value)){
	 alert("Please enter the first name.");
	 document.getElementById("txtFirstName").focus();
	 return false;
    }else if(!is_char(document.getElementById("txtLastName").value)){
	 alert("Please enter the last name.");
	 document.getElementById("txtLastName").focus();
	 return false;
   }else if(trim(document.getElementById("txtAddress").value) == ""){
	 alert("Please enter the address.");
	 document.getElementById("txtAddress").focus();
	 return false;
   }else if(!is_char(document.getElementById("txtCity").value)){
	 alert("Please enter the city name.");
	 document.getElementById("txtCity").focus();
	 return false;
   }else if(trim(document.getElementById("txtState").value) == ""){
	 alert("Please enter the state name.");
	 document.getElementById("txtState").focus();
	 return false;
   }else if(!is_validZip(document.getElementById("txtZipcode").value)){
	 alert("Please enter the valid number.Only numeric value allow.");
	 document.getElementById("txtZipcode").focus();
	 return false;
   }else{
         return true;
    }
	
}
function validatereg(){

    if(!is_char(document.getElementById("txtFirstName").value)){
	 alert("Please enter the first name.");
	 document.getElementById("txtFirstName").focus();
	 return false;
    }else if(!is_char(document.getElementById("txtLastName").value)){
	 alert("Please enter the last name.");
	 document.getElementById("txtLastName").focus();
	 return false;
   }else if(!is_email(document.getElementById("txtEmail").value)){
	 alert("Please enter the valid email id.");
	 document.getElementById("txtEmail").focus();
	 return false;
  }else if(trim(document.getElementById("txtAddress").value) == ""){
	 alert("Please enter the address.");
	 document.getElementById("txtAddress").focus();
	 return false;
   }else if(!is_char(document.getElementById("txtCity").value)){
	 alert("Please enter the city name.");
	 document.getElementById("txtCity").focus();
	 return false;
   }else if(trim(document.getElementById("txtState").value) == ""){
	 alert("Please enter the state name.");
	 document.getElementById("txtState").focus();
	 return false;
   }else if(!is_validZip(document.getElementById("txtZipcode").value)){
	 alert("Please enter the valid number.Only numeric value allow.");
	 document.getElementById("txtZipcode").focus();
	 return false;
   }else if(!is_validZip(document.getElementById("txtTelephone").value)){
	 alert("Please enter the contact number.Only numeric value allow.");
	 document.getElementById("txtTelephone").focus();
	 return false;
   }else if(trim(document.getElementById("txtPassword").value) == ""){
   alert("Please enter the password.");
   document.getElementById("txtPassword").focus();
    return false;
  }else if(trim(document.getElementById("txtCPassword").value) == ""){
   alert("Please enter the confirm password.");
   document.getElementById("txtCPassword").focus();
    return false;
  }else if( (trim(document.getElementById("txtPassword").value)) != (trim(document.getElementById("txtCPassword").value)) ){
   alert("Password not match.Please enter again.");
   document.getElementById("txtPassword").value ="";
   document.getElementById("txtCPassword").value ="";
   document.getElementById("txtPassword").focus();

    return false;
  }else if(!(document.getElementById("terms").checked)){
       alert("Please check the term and condition checkbox.")  
	   document.getElementById("terms").focus();
       return false;
   
    }else{
         return true;
    }
}
function validateaccount(){

    if(!is_char(document.getElementById("txtFirstName").value)){
	 alert("Please enter the first name.");
	 document.getElementById("txtFirstName").focus();
	 return false;
    }else if(!is_char(document.getElementById("txtLastName").value)){
	 alert("Please enter the last name.");
	 document.getElementById("txtLastName").focus();
	 return false;
   }else if(!is_email(document.getElementById("txtEmail").value)){
	 alert("Please enter the valid email id.");
	 document.getElementById("txtEmail").focus();
	 return false;
  }else if(!is_validZip(document.getElementById("txtTelephone").value)){
	 alert("Please enter the contact number.Only numeric value allow.");
	 document.getElementById("txtTelephone").focus();
	 return false;
   }else{
         return true;
    }
}

function validatepwd(){

  if(trim(document.getElementById("cpassword").value) == ""){
   alert("Please enter the current password.");
   document.getElementById("cpassword").focus();
    return false;
  }else if(trim(document.getElementById("npassword").value) == ""){
   alert("Please enter the new password.");
   document.getElementById("npassword").focus();
    return false;
  }else if(trim(document.getElementById("ncpassword").value) == ""){
   alert("Please enter the new confirm password.");
   document.getElementById("ncpassword").focus();
    return false;
  }else if( (trim(document.getElementById("npassword").value)) != (trim(document.getElementById("ncpassword").value)) ){
   alert("Password not match.Please enter again.");
   document.getElementById("npassword").value ="";
   document.getElementById("ncpassword").value ="";
   document.getElementById("npassword").focus();

    return false;
  }

}
////////////////////////////////     Customer Login ///////////////////////////////////////////////


/* The following function creates an XMLHttpRequest object... */

function createRequestObject() {

	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer") {
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}	else {
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}


/* The variable http will hold our new XMLHttpRequest object. */
var httpcust = createRequestObject(); //create the object here
/// Check Login Details
function callCustomerFn() {
    
	
	document.getElementById('loginmsg').innerHTML = "Please wait...";
	httpcust.open('get', 'cart/internal_request.php?mode=user&pass='+ document.getElementById("txtPassword").value +'&username='+ document.getElementById("txtEmail").value);
	
	httpcust.onreadystatechange = handlecustomer; 
	httpcust.send(null);
   
}
function handlecustomer()
{
	if(httpcust.readyState == 4){ 
	   var response = httpcust.responseText;
       //alert(response);
	     if(response == 1 ){

     	  document.getElementById('loginmsg').innerHTML = ""; 
		  if(document.getElementById('refer').value != ""){
		  location.href=document.getElementById('refer').value;
		  }else{
		  location.href='cart/checkout_shipping.php';
		  }
		 }else{
		      document.getElementById('loginmsg').innerHTML = ErrMsg_Login;
		 }
		 


	}
}
/// chk for current password
function validateCrtPasswordFn() {
    
	
	document.getElementById('display_error').innerHTML = "Please wait...";
	httpcust.open('get', 'cart/internal_request.php?mode=password&pass='+ document.getElementById("cpassword").value);
	
	httpcust.onreadystatechange = handlepassword; 
	httpcust.send(null);
   
}
function handlepassword()
{
	if(httpcust.readyState == 4){ 
	   var response = httpcust.responseText;
       //alert(response);
	     if(response == 1 ){

     	  document.getElementById('display_error').innerHTML = '';
		 
		 }else{
		    document.getElementById('display_error').innerHTML = 'Password not match in our database';
			 document.getElementById('cpassword').focus();
		 }
		 


	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////  Row Effwect ///////////////////////////////////////////////
var selected;

function selectRowEffect(object, buttonSelect) {
  if (!selected) {
    if (document.getElementById) {
      selected = document.getElementById('defaultSelected');
    } else {
      selected = document.all['defaultSelected'];
    }
  }

  if (selected) selected.className = 'moduleRow';
  object.className = 'moduleRowSelected';
  selected = object;

// one button is not an array
  if (document.frmPayment.payment[0]) {
    document.frmPayment.payment[buttonSelect].checked=true;
  } else {
    document.frmPayment.payment.checked=true;
  }
}

function rowOverEffect(object) {
  if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}
////////////////////////////// Payment Validation /////////////////////////////////////////////////

function check_form() {
  var error = 0;
  var error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";
  var payment_value = null;
  if (document.frmPayment.payment.length) {
    for (var i=0; i<document.frmPayment.payment.length; i++) {
      if (document.frmPayment.payment[i].checked) {
        payment_value = document.frmPayment.payment[i].value;
      }
    }
  } else if (document.frmPayment.payment.checked) {
    payment_value = document.frmPayment.payment.value;
  } else if (document.frmPayment.payment.value) {
    payment_value = document.frmPayment.payment.value;
  }

  if (payment_value == "cc") {
    var cc_owner = document.frmPayment.cc_owner.value;
    var cc_number = document.frmPayment.cc_number.value;
    if (cc_owner == "" || cc_owner.length < 3) {
      error_message = error_message + "* The owner's name of the credit card must be at least 3 characters.\n";
      error = 1;
    }
    if (cc_number == "" || cc_number.length < 10) {
      error_message = error_message + "* The credit card number must be at least 10 characters.\n";
      error = 1;
    }
  }

  if (payment_value == null) {
    error_message = error_message + "* Please select a payment method for your order.\n";
    error = 1;
  }

  if (error == 1) {
    alert(error_message);
    return false;
  } else {
    return true;
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////
