// JavaScript Document
function checkUpdateAccount(){
	
	frm=document.forms["frmUpdate"];
	var	r=validaForm(frm);
	if(r!=false){
		passw=g('passw_login').value;
		passw2=g('passw_login2').value;
		
		if(passw==passw2){
			frm.submit();
			//alert("OK");
		}else{
			alert("Please confirm your password");
			g('passw_login2').focus();
			return false;
		}
	}
}

function send_Payment(){
	frm=document.forms["frmPayment"];
	var	r=validaForm(frm);
	if(r!=false){
		var numCC=g("cardnumber").value;
		if(!validCreditCart(numCC)){
			alert( "The credit card number enter is not valid. \nPlease double check the numbers you've entered!" );
		}else{
			//if(g("checkingVerify").checked==true)
				frm.submit();
			//else
				//alert("You must agree to the terms and conditions");
		}
	}	
}

function copyShippingData(){
	if(g('copy_shipping').checked==true){
		g("name_bill").value=$("input#name_shipp").val();
		g("lastName_bill").value=$("input#lastName_shipp").val();
		g("company_bill").value=$("input#company_shipp").val();
		g("address1_bill").value=$("input#address1_shipp").val();
		g("address2_bill").value=$("input#address2_shipp").val();
		g("city_bill").value=$("input#city_shipp").val();
		g("stateprov_bill").value=$("#stateprov_shipp").val();
		g("zip_bill").value=$("input#zip_shipp").val();
		g("country_bill").value=$("#country_shipp").val();
		g("phono_bill").value=$("input#phono_shipp").val();
	}else{
		clearFormBilling();	
	}
}

function clearFormBilling(){
	g('name_bill').value="";
	g('lastName_bill').value="";
	g('company_bill').value="";
	g('address1_bill').value="";
	g('address2_bill').value="";
	g('city_bill').value="";
	g("stateprov_bill").value="";
	g('zip_bill').value="";
	g('country_bill').value="";
	g('phono_bill').value="";
}

function postFields(){
	var name_shipp=$("input#name_shipp").val();
	var lastName_shipp=$("input#lastName_shipp").val();
	var company_shipp=$("input#company_shipp").val();
	var address1_shipp=$("input#address1_shipp").val();
	var address2_shipp=$("input#address2_shipp").val();
	var city_shipp=$("input#city_shipp").val();
	var stateprov_shipp=$("#stateprov_shipp").val();
	var zip_shipp=$("input#zip_shipp").val();
	var country_shipp=$("#country_shipp").val();
	var phono_shipp=$("input#phono_shipp").val();
	var email_login=$("input#email_login").val();
	var passw_login=$("input#passw_login").val();
	
	url="name_shipp="+name_shipp+"&lastName_shipp="+lastName_shipp+"&company_shipp="+company_shipp+"&address1_shipp="+address1_shipp+"&address2_shipp="+address2_shipp+"&city_shipp="+city_shipp+"&stateprov_shipp="+stateprov_shipp+"&zip_shipp="+zip_shipp+"&country_shipp="+country_shipp+"&phono_shipp="+phono_shipp+"&email_login="+email_login+"&passw_login="+passw_login;

	myConn.connect("../controllers/SessionShipping.php","POST",url,endSessionShipp);
}
			
var endSessionShipp=function (oXML){
	var data = oXML.responseText;
	if(data=="OK"){
		$("#the_payment").show("slow");
		$("#the_shipping").hide("slow");
		$("#ChangeShipp").show(1000);
	}else{
		g("ErrorCreateAccount").style.display="";
		g("ErrorCreateAccount").innerHTML="<span class='labelRed'>The email address <strong>"+$("input#email_login").val()+"</strong> is already registered in our system. Please enter a different email address, or <span class='labelGrayMain'><a href='login.php#cards'>click here to sign in</a></span> to your account.</span>";	
	}
}

function validateShipping(logeado){
	frm=document.forms["frmShipping"];
	var	r=validaForm(frm);
	if(r!=false){
		
		if(logeado=="1"){
			return true
		}else{
			passw=g('passw_login').value;
			passw2=g('passw_login2').value;
			
			if(passw==passw2){
				return true;
			}else{
				alert("Please confirm your password");
				g('passw_login2').focus();
				return false;
			}
		}
	}
}


function checkSelected(){
	frm=document.forms["frmCard"];	
	var rdo_price=document.frmCard.price;
	var rdo_cards=document.frmCard.idCard;
	var i;
	var x;
	var c_card=0;
	var c_price=0;
	
	for(i=0; i<rdo_cards.length; i++){
		if(rdo_cards[i].checked){
			c_card++;
			break;
		}
	}
	
	for(i=0; i<rdo_price.length; i++){
		if(rdo_price[i].checked){
			c_price++;
			break;
		}
	}
	
	if(c_price>0 && c_card>0){
		frm.submit();
	}else{
		if(c_card==0){
			alert("Please, choose one gift card theme");
			return false;
		}
		if(c_price==0){
			alert("Please, choose amount");
			return false;
		}
	}
}

function validCreditCart(ccNumb) {
	var valid = "0123456789" //Digitos validos
	var len = ccNumb.length;
	var iCCN = parseInt(ccNumb); 
	var sCCN = ccNumb.toString(); 
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');
	var iTotal = 0;  
	var bNum = true; 
	var bResult = false; 
	var temp; 
	var calc; 
 
	for (var j=0; j<len; j++) {
	  temp = "" + sCCN.substring(j, j+1);
	  if (valid.indexOf(temp) == "-1"){bNum = false;}
	}
 
	if(!bNum){
	  bResult = false;
	}
 
	if((len == 0)&&(bResult)){
	  bResult = false;
	} else{
	  if(len >= 12){  // 15 or 16 for Amex or V/MC
		for(var i=len;i>0;i--){  
		  calc = parseInt(iCCN) % 10; 
		  calc = parseInt(calc);  
		  iTotal += calc;
		  i--;  
		  iCCN = iCCN / 10;
		  calc = parseInt(iCCN) % 10 ;
		  calc = calc *2; 
		  switch(calc){
			case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
			case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
			case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
			case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
			case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
			default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
		  }                                               
		iCCN = iCCN / 10;
		iTotal += calc;
	  } //end for
	  if ((iTotal%10)==0){
		bResult = true;
	  } else {
		bResult = false; 
		}
	  }
	}
	 return bResult;
}