function cancelCheckout(){
	location.href="controllers/cartController.php?action=cancel_checkout";
}

function shipToAResidence(){
	
	var weight_order=g("weight_order").value;
	var residence="0";
	if(g("ship_to_residence").checked==true){
		residence="1";
	}
	
	var subtotal=g("subtotal").value;
	var amount_code_discount=g("amount_code_discount").value;
	var taxPercent=g("percent_tax").value;
	var addcost_ship=g("cost_additional").value;
	
	$.post('controllers/shippFedex.php', {weight_order:weight_order, residence:residence}, function(data){																																													
			var shipp_cost=data;
			var estimatedSubtotal;
			var montoTax;
			var totalAmountPayment;
			
			if(shipp_cost>0){				
				estimatedSubtotal=(1*subtotal)+(1*shipp_cost)-(1*amount_code_discount);
				
				montoTax=(estimatedSubtotal*taxPercent)/100;
				montoTax=currencyConvert(montoTax);
				
				totalAmountPayment=(1*estimatedSubtotal)+(1*montoTax)+(1*addcost_ship);
				totalAmountPayment=currencyConvert(totalAmountPayment);
				
				// para los hidden
				g("total_amount").value=totalAmountPayment;				
				g("monto_tax").value=montoTax;				
				g("price_shipping").value=currencyConvert(shipp_cost);
				
				// los id DIV				
				g("shipp_cost").innerHTML="$"+currencyConvert(shipp_cost);
				g("amount_tax").innerHTML="$"+montoTax;
				g("amount_payment").innerHTML="$"+totalAmountPayment;
			}
		});	
	
}

function estimatedShipping(country){
	var subtotal=g("subtotal_first").value;
	$.post('controllers/process.php', {action:"estimateShipping", model:"Countries", country:country, subtotal:subtotal}, function(data){																																													
		rpta=data.split("|");
		
		g("totalamount").value=rpta[0];
		g("subtotal").value=rpta[0];
		g("total-price").innerHTML="$ "+rpta[0];
		g("valueShipp").value=rpta[1];
		g("shipping-price").innerHTML="$ "+rpta[1];
		g("shippto").value=rpta[2];
		
	});
}

function updateCart(){
	var error;
	var total=g("total_item").value;
	
	for(i=0; i<total; i++){
		q=g("qty"+i).value;
		if(q>0){error=1;}
		else{
			alert("The quantity is required in your product");
			error=0;
			break;
		}
	}
	
	if(error){
		g("action_cart").value="update";
		
		frm=document.forms["frmCart"];
		frm.submit();
	}
}

function clearCart(){
	window.location.href="controllers/cartController.php?action=clear";	
}

function checkoutStepOne(){	
	g("action_cart").value="send_checkout";	
	frm=document.forms["frmCart"];
	frm.submit();
}

function selectedCountry(country_current,state_current,idDivState,objName_state){
	var country=country_current;
	var state=state_current
	var objState=objName_state;
	$.post('controllers/process.php', {state:state, country_:country,action:"showStateForm",model:"Countries",objState:objState}, function(data){
		g(idDivState).innerHTML=data;  
		//copyShippingData(1)
		//calculate_shipping(); 
	});
}

function calculateShippingAndGetState(country_current,state_current,idDivState,objName_state){
	var subtotal_without_shipp=g("subtotal_without_shipp").value;
	var discountCodeAmount=g("discountCodeAmount").value;
	
	var subtotal=g("subtotal").value;
	var total_amount=g("total_amount").value;
	
	var country=country_current;
	var state=state_current
	var objState=objName_state;
	$.post('controllers/process.php', {state:state, country_:country,action:"showStateForWholesaleAndShipping",model:"Countries",objState:objState,discountCodeAmount:discountCodeAmount,subtotal_without_shipp:subtotal_without_shipp}, function(data){
																																																												
		rpta=data.split("|");																																																											  		
		//g("total_amount").value=rpta[0];
		//g("subtotal").value=rpta[1];
		//g("valueShipp").value=rpta[2];
		
		g(idDivState).innerHTML=rpta[3];  
	});
}

function copyShippingData(state_copy){
	g("name_shipp").value=g("name_bill").value;
	g("lastName_shipp").value=g("lastName_bill").value;
	g("company_shipp").value=g("company_bill").value;
	g("address1_shipp").value=g("address1_bill").value;
	//g("address2_shipp").value=g("address2_bill").value;
	g("city_shipp").value=g("city_bill").value;
	if(state_copy==0){
		g("stateprov_shipp").value=g("stateprov_bill").value;
	}else{
		g("stateprov_shipp").value=g("stateprov_bill").value;	
	}
	g("zip_shipp").value=g("zip_bill").value;
	g("country_shipp").value=g("country_bill").value;
	g("phono_shipp").value=g("phono_bill").value;	
}

function clearFormBilling(){
	g('name_shipp').value="";
	g('lastName_shipp').value="";
	g('company_shipp').value="";
	g('address1_shipp').value="";
	//g('address2_shipp').value="";
	g('city_shipp').value="";
	g("stateprov_shipp").value="";
	g('zip_shipp').value="";
	g('country_shipp').value="";
	g('phono_shipp').value="";
}

function sendPayment(){
	frm=document.forms["formEnd"];	
		
	//if(g("terms").checked==true){
		g("checkout").style.display="none";
		g("checkout_cancel").style.display="none";
		frm.submit();
	//}else{
		//alert("You must agree to the terms and conditions");
		//return false;
	//}
}
function ChangeState(country,StateDiv){	
	var state=g("stateprov_bill").value
	$.post('../controllers/process.php', {model:"Countries",action:"showstate",country_:country,state:state}, function(data){
		g(StateDiv).innerHTML=data;																									
	});
}

$(document).ready(function(){
    $('#menu-nav li.link').mouseover(function(){
        $(this).addClass('over');
    }).mouseout(function(){
        $(this).removeClass('over');
    });
    $('.link-menu').mouseover(function(){
        $(this).find('.display-menu').show();
    }).mouseout(function(){
        $(this).find('.display-menu').hide();
    });


    ///button category    

});

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;
}
