// JavaScript Document
function showStateShipp(){
	country=g("s_country").value;
	url="action=2&country="+country;
	//alert(url)
	myConn.connect("controller/CountriesController.php","POST",url,end_showStateShipp);
}
var end_showStateShipp=function (oXML){
	var data = oXML.responseText.split("***");
	g("StateShipping").innerHTML=data[0];
	
	g("chkIncrease").checked=false;
	
	var tax=data[1];  // esto es el %tax
	if(tax=='sin'){
		g("taxCost").value='-1';
		g("totalAmount").value=g("totalAmount0").value;
		g("totalAmount2").value=g("totalAmount0").value;
	}else{ // Canada
		g("totalAmount2").value=g("totalAmount0").value;
		calculateTax(tax);
	}
	
}

function showState(){
	country=g("country").value;
	url="action=1&country="+country;
	//alert(url)
	myConn.connect("controller/CountriesController.php","POST",url,end_showState);		
}
var end_showState=function (oXML){
	var data = oXML.responseText;
	g("StateBilling").innerHTML=data;
}

function checkCompetition(){
	frm=document.forms["frmAction"];	
	var	r=validaForm(frm);
	if(r!=false){
		
		if(g("chkTerms").checked!=true){
			alert( "You must Agree to the Terms and Conditions of the Competition" );
		}else{
			frm.submit();	
		}	
	}	
}

function save_common(msg_btn){
	frm=document.forms["frmAction"];	
	var	r=validaForm(frm);
	
	if(r!=false){		
		document.getElementById("btnSave").value=msg_btn;
		document.getElementById("btnSave").disabled=true;
		frm.submit();
	}else{
		return false;	
	}
}

function unsubscribe(){
	frm=document.forms["frmAction"];	
	var	r=validaForm(frm);
	if(r!=false){
		varUserEmail=g("varUserEmail").value;
		url="action=2&varUserEmail="+varUserEmail;		
		myConn.connect("controller/SentEmailsController.php","POST",url,endUnsubscribe);	
	}else{
		return false;	
	}
}

var endUnsubscribe=function (oXML){
	var rpta = oXML.responseText;
	g("showMensajeUnsubscribe").style.display="";
	g("showMensajeUnsubscribe").innerHTML=rpta;
}

function sendForm(){
	frm=document.forms["frmAction"];	
	var	r=validaForm(frm);
	if(r!=false){
		frm.submit();	
	}else{
		return false;	
	}
}

function fromBilling() {
	//validar si seleccion monto a pagar
	if(g("totalAmount").value != "$ 0.00")
	{	if(g("chkSameInfo").checked==true)
		{	state=g("state").value;	
			country=g("country").value;
			url="action=2&stateAbbrv="+state+"&country="+country+"&origen=bill";		
			myConn.connect("controller/StateController.php","POST",url,fromShipping);
		}else{	clearForm();	}
	}
	else
	{	alert("Please Select Amount, CLUB or QUICKIE");
		clearForm();
		document.location.href='#amount'; 
	} 
}

var fromShipping=function (oXML){
	var rpta = oXML.responseText.split("|");
	var sub_total=0;
	if(rpta[0]=="ok"){
		
		g("StateShipping").innerHTML=rpta[2];
		
		g('s_name').value=g('name').value;
		g('s_lastname').value=g('lastname').value;		
		g('s_address').value=g('address').value;
		g('s_state').value=g('state').value;
		g('s_city').value=g('city').value;
		g('s_country').value=g('country').value;
		g('s_zip').value=g('zip').value;
		g('s_phone').value=g('phone').value;
		g('s_email').value=g('email').value;
		// Calculamos el TAX
		tax=rpta[1];
		
		g("totalAmount").value=g("totalAmount0").value;
		g("totalAmount2").value=g("totalAmount0").value;
		
		calculateTax(tax);
	}else{
		alert("At this time we are unable to ship to your state. If possible, please choose a different state for the shipping. \nSorry for the inconvenience");
		clearForm();
	}
}

function clearForm(){
	g('s_name').value="";
	g('s_lastname').value="";
	g('s_address').value="";
	g('s_state').value="";
	g('s_city').value="";
	g('s_country').value="";
	g('s_zip').value="";
	g('s_phone').value="";
	g('s_email').value="";
	g('chkSameInfo').checked=false;
	taxCost=(g("taxCost").value*1);
	if(taxCost>0){
		sub_total=g("totalAmount0").value;		
		gt=g("totalAmount2").value;
		ngt=(gt*1)-(taxCost*1);
		gt=g("totalAmount").value="$ "+ currencyConvert(ngt);
		g("totalAmount2").value=currencyConvert(ngt);
	}
	g("taxCost").value=-1;
	g("tax").value=0;
}

function chkIncrese()
{	if(g("chkIncrease").checked==true)
	{	if(g("totalAmount").value != "$ 0.00")
		{	total_sin =g("totalAmount0").value;
//			totalamount2=g("totalAmount2").value;
			incremento=g("increse").value;
			incrementoamount=(total_sin*incremento)/100; 
			total=(incrementoamount*1)+(total_sin*1);
			//alert(total);
			tax=g("tax").value;
			stax=g("taxCost").value;
			if(stax>=0)
			{	g("totalAmount2").value=currencyConvert(total);
				g("totalAmount").value="$ "+ (currencyConvert(total));
				calculateTax(tax);			
			}
			else
			{	g("totalAmount").value="$ "+ (currencyConvert(total));
				g("totalAmount2").value=currencyConvert(total);
			}
			g("chkIncreaseper").value =incremento;
			g("chkIncreaseamount").value = incrementoamount;
		}
		else
		{ 	g("chkIncrease").checked=false;
			alert( "Please Select Amount, CLUB or QUICKIE" ); 
			document.location.href='#amount';
		}
	}else
	{	g("chkIncreaseper").value = 0;
		g("chkIncreaseamount").value = 0;
		tax=g("tax").value;
		stax=g("taxCost").value;
//		alert(stax);
		if(stax>=0)
		{	total=g("totalAmount0").value;
			g("totalAmount2").value=currencyConvert(total);
			calculateTax(tax);			
		}
		else
		{	total=g("totalAmount0").value;
			g("totalAmount").value="$ "+ (currencyConvert(total));
			g("totalAmount2").value=currencyConvert(total);
		}
	}
}

function checkFields(){
	frm=document.forms["frmAction"];	
	var	r=validaForm(frm);
	if(r!=false){
		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{
			frm.submit();	
		}	
	}	
}

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;
}

function calculateTax(tax){
	gt=g("totalAmount2").value;
	sub_total=(gt*1);//-(taxCost*1);	
	stax=(sub_total*tax)/100;	
	// para guardar registro en la BD
	g("tax").value=tax;
	g("taxCost").value=stax;
	totgrand=total=(sub_total*1)+(stax*1);

	g("totalAmount").value="$ "+ (currencyConvert(totgrand));
	g("totalAmount2").value=currencyConvert(totgrand);	
}

function calculateTaxStateShipping(){
	state=g("s_state").value;
	country=g("s_country").value;
	
	url="action=2&stateAbbrv="+state+"&country="+country+"&origen=shipp";		
	myConn.connect("controller/StateController.php","POST",url,fromShippingTax);	
}

var fromShippingTax=function (oXML){
	var rpta = oXML.responseText.split("|");
	if(rpta[0]=="ok"){
		tax=rpta[1];
		taxCost=g("taxCost").value;
		if(taxCost>0)
		{	total=g("totalAmount2").value;
			total=(total*1)-(taxCost*1);
			g("totalAmount2").value=currencyConvert(total);	
			calculateTax(tax);
		}
		else calculateTax(tax);
	}else{
		alert("At this time we are unable to ship to your state. If possible, please choose a different state for the shipping. \nSorry for the inconvenience");	
	}
}


function currencyConvert(n){
	n = Math.round( (n*100) )/100;
	strN = new String( n );
	if( strN.indexOf( "." ) == -1 )
		strN += ".00";
	else if( strN.indexOf( "." ) == strN.length-2 )
		strN += "0";
	return strN;
}

function getamountByall(val){
  g("totalAmount").value="$ "+ (currencyConvert(val));
  g("totalAmount2").value=currencyConvert(val);
  g("totalAmount0").value=val;
  
  g("type").value=1;
  g("chkIncreaseamount").value=0;
  g("chkIncrease").checked=false;	  
  document.location.href='#total';	
}

function getamountSetUp(val)
{ g("totalAmount").value="$ "+ (currencyConvert(val));
  g("totalAmount2").value=currencyConvert(val);
  g("totalAmount0").value=val;	  	  
  g("type").value=2;
  g("chkIncreaseamount").value=0;	  
  g("chkIncrease").checked=false; 
  document.location.href='#total';	   	  
}

function getamountQuickie(val)
{ g("totalAmount").value="$ "+ (currencyConvert(val));
  g("totalAmount2").value=val;
  g("totalAmount0").value=val;	  	  
  g("type").value=3;
  g("chkIncreaseamount").value=0;
  g("chkIncrease").checked=false;	   
  document.location.href='#total';	   	  
}		