
var g_shipping_type = 0;
var g_which_address = 0;
var g_shipping_method_id = 0;
var g_shipping_method_cost = 0;
var g_payment_method = 0;
var g_insurance_cost = 0;
var g_insurance_skipped = 0;

function showLoadIcon(targetDiv) {
	var tdiv = document.getElementById(targetDiv);
	tdiv.innerHTML = '<br/></br><img src="/imagenes/layout/ajaxload.gif">';
}

function ConfirmOrder(orderID, targetUrl) {

	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	ajax.callPage("/orders.php",orderConfirmed,"POST","do=confirm&id_order="+orderID);
	var tdiv = document.getElementById("order_brief_confirm_inner");
	tdiv.style.display = 'none';
	tdiv = document.getElementById("order_brief_confirm_header");
	tdiv.innerHTML = '<span class="order_brief_confirm_header_msg"><blink>Confirmando compra...aguarde por favor...</blink></span>';
}

function orderConfirmed(response) {
	var tdiv = document.getElementById("order_brief_confirm_header");
	tdiv.style.background = 'url(/imagenes/layout/order_brief_paso1_bar2.jpg)';
	tdiv.innerHTML = response;
	tdiv = document.getElementById("order_brief_payment");
	if (tdiv) {
		tdiv.style.display = 'block';
	}
	tdiv = document.getElementById("order_brief_step3_inner");
	if (tdiv) {
		tdiv.style.display = 'block';
	}
	tdiv = document.getElementById("order_brief_step4_inner");
	if (tdiv) {
		tdiv.style.display = 'block';
	}
}

function submitOrder() {

	var finishBtn = document.getElementById("checkout_finish");
	finishBtn.style.visibility = "hidden";
	var finishBtn = document.getElementById("processing_message");
	finishBtn.style.visibility = "visible";

	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	if (g_shipping_type == 1) {
		ajax.callPage("/cart.php",orderSubmitted,"POST","do=submit_order&type=1");
	}
	if (g_shipping_type == 2) {
		var submituri = '';
		submituri = "do=submit_order&type=2&which_address="+g_which_address+"&shipping_method_id="+g_shipping_method_id+"&shipping_method_cost="+g_shipping_method_cost+"&insurance_skipped="+g_insurance_skipped;
		if (g_payment_method > 0) submituri += "&payment_method="+g_payment_method;
		if (g_which_address == 2) {
			var edit_address = document.getElementById("edit_address");
			var edit_zipcode = document.getElementById("edit_zipcode");
			var combo_cities = document.getElementById("basic_cities");
			var combo_cityletters = document.getElementById("basic_cityletters");
			var combo_provinces = document.getElementById("basic_provinces");
			var combo_countries = document.getElementById("basic_countries");
			var edit_among_streets = document.getElementById("edit_among_streets");
			submituri += "&address="+edit_address.value+"&zipcode="+edit_zipcode.value+"&id_city="+combo_cities.value+"&among_streets="+edit_among_streets.value;
		}
		ajax.callPage("/cart.php",orderSubmitted,"POST",submituri);
	}
	return false;
}

function orderSubmitted(response) {
	var order_box = document.getElementById("order_box");
	order_box.innerHTML = response;
	window.scrollTo(0,0);
}

function disableCustomAddress(disable) {
	var edit_address = document.getElementById("edit_address");
	var edit_zipcode = document.getElementById("edit_zipcode");
	var combo_cities = document.getElementById("basic_cities");
	var combo_provinces = document.getElementById("basic_provinces");
	var combo_countries = document.getElementById("basic_countries");
	var edit_among_streets = document.getElementById("edit_among_streets");
	var backcolor;
	if (disable) backcolor = "#E2F1BB";
	else backcolor = "#FFFFFF";

	edit_address.style.background = backcolor;
	edit_zipcode.style.background = backcolor;
	combo_cities.style.background = backcolor;
	combo_provinces.style.background = backcolor;
	combo_countries.style.background = backcolor;
	edit_among_streets.style.background = backcolor;
	edit_address.disabled = disable;
	edit_zipcode.disabled = disable;
	combo_cities.disabled = disable;
	combo_provinces.disabled = disable;
	combo_countries.disabled = disable;
	edit_among_streets.disabled = disable;
}

function selectedAddressType(which_address) {

	var old_which_address = g_which_address;
	if (g_which_address == which_address) return;
	else g_which_address = which_address;

	var resumen_shipping_cost = document.getElementById("resumen_shipping_cost");
	resumen_shipping_cost.innerHTML = "<b>$ 0</b>";
	var resumen_insurance_cost = document.getElementById("resumen_insurance_cost");
	resumen_insurance_cost.innerHTML = "<b>$ 0</b>";
	var resumen_payment_method = document.getElementById("resumen_payment_method");
	resumen_payment_method.innerHTML = "No especificada";

	update_resumen_total();

	if (which_address == 1) {
		disableCustomAddress(true);
		doCheckout3(1);
	}
	else {
		disableCustomAddress(false);
		rollBackToStep2();
		if (old_which_address == 1) {
			var tdiv = document.getElementById("step3");
			tdiv.style.visibility = "hidden";
			//showGrayStep3();
		}
	}
}

function changeAddress() {
	var checkout_change_address = document.getElementById("checkout_change_address");
	checkout_change_address.style.visibility = "hidden";
	disableCustomAddress(false);
	rollBackToStep2();
	var tdiv = document.getElementById("step3");
	tdiv.style.visibility = "hidden";
	//showGrayStep3();
}

function rollBackToStep2() {
	g_shipping_method_id = 0;
	g_payment_method = 0;
	var checkout_confirm_address = document.getElementById("checkout_confirm_address");
	var step3 = document.getElementById("step3");
	step3.innerHTML = "";
	checkout_confirm_address.style.visibility = "visible";
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function clickedInsurance() {
	var insuranceCheckbox = document.getElementById("insurance_checkbox");
	var insuranceCost = document.getElementById("insurance_cost");
	var resumen_insurance_cost = document.getElementById("resumen_insurance_cost");
	if (insuranceCheckbox.checked) {
		g_insurance_skipped = 1;
		g_insurance_cost = insuranceCost.innerHTML;
		insuranceCost.innerHTML = "<b>$ 0</b>";
		resumen_insurance_cost.innerHTML = insuranceCost.innerHTML;
	}
	else {
		g_insurance_skipped = 0;
		insuranceCost.innerHTML = g_insurance_cost;
		resumen_insurance_cost.innerHTML = insuranceCost.innerHTML;
	}
	g_payment_method = 0;
	update_resumen_total();
	showLoadIcon("step4");
	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	ajax.callPage("/cart.php",showStep4,"POST","do=checkout4&shipping_method_id="+g_shipping_method_id+"&shipping_cost="+g_shipping_method_cost+"&insurance_skipped="+g_insurance_skipped);
}

function showInsurance(response) {
	var insuranceBox = document.getElementById("insurance");
	insuranceBox.innerHTML = response;
	insuranceBox.style.visibility = "visible";
	var resumen_insurance_cost = document.getElementById("resumen_insurance_cost");
	var insurance_cost = document.getElementById("insurance_cost");
	if (response != '') {
		resumen_insurance_cost.innerHTML = insurance_cost.innerHTML;
	}
	else {
		resumen_insurance_cost.innerHTML = "<b>$ 0</b>";
	}
	update_resumen_total();
}

function update_resumen_total() {
	var resumen_products_pesos = document.getElementById("resumen_products_pesos");
	var resumen_total_pesos = document.getElementById("resumen_total_pesos");
	var resumen_shipping_cost = document.getElementById("resumen_shipping_cost");
	var resumen_insurance_cost = document.getElementById("resumen_insurance_cost");
	var resumen_kits_pesos = document.getElementById("resumen_kits_pesos");

	var digits_reg = /\d+\.?\d*/;
	var products_pesos = resumen_products_pesos.innerHTML.match(digits_reg);
	var shipping_pesos = resumen_shipping_cost.innerHTML.match(digits_reg);
	var insurance_pesos = resumen_insurance_cost.innerHTML.match(digits_reg);
	var kits_pesos = resumen_kits_pesos.innerHTML.match(digits_reg);
	var total = parseFloat(kits_pesos)+parseFloat(products_pesos)+parseFloat(shipping_pesos)+parseFloat(insurance_pesos);
	resumen_total_pesos.innerHTML = "<b>$ "+total+"</b>";
}

function showFinishButton() {
	var finishBtn = document.getElementById("checkout_finish");
	finishBtn.style.visibility = "visible";
}

function showStep4(response) {
	var step4 = document.getElementById("step4");
	step4.innerHTML = response;
}

function showStep3(response) {
	var step3 = document.getElementById("step3");
	step3.innerHTML = response;
}

function showStep2(response) {
	var step2 = document.getElementById("step2");
	step2.innerHTML = response;
	if (g_shipping_type == 2) {
		initLoadProc();
		disableCustomAddress(true);
	}
}

function showGrayStep3() {
	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	ajax.callPage("/cart.php",showStep3,"POST","do=checkout3gray");
}

function doCheckout5(payment_method) {
	var old_payment_method = g_payment_method;
	if (g_payment_method == payment_method) return;
	else g_payment_method = payment_method;
	//var box1 = document.getElementById("payment_method_box"+payment_method);
	//box1.style.background = "#E2F1BB";
	//box1.style.border = "1px solid #39630F";
	//if (old_payment_method > 0) {
//		var box2 = document.getElementById("payment_method_box"+old_payment_method);
//		box2.style.background = "#FFFFFF";
//		box2.style.border = "1px solid #FFFFFF";
//	}
	var resumen_payment_method = document.getElementById("resumen_payment_method");
	var payment_method_label = document.getElementById("payment_method_"+payment_method+"_label");
	resumen_payment_method.innerHTML = payment_method_label.innerHTML;

	showFinishButton();
}

function doCheckout4(shipping_method, shipping_cost) {
	var old_shipping_method = g_shipping_method_id;
	if (g_shipping_method_id == shipping_method) return;
	else g_shipping_method_id = shipping_method;
	g_payment_method = 0;
	g_shipping_method_cost = shipping_cost;
	g_insurance_cost = 0;
	g_insurance_skipped = 0;
	//var box1 = document.getElementById("shipping_method_box"+shipping_method);
	//box1.style.background = "#E2F1BB";
	//box1.style.border = "1px solid #39630F";
	//if (old_shipping_method > 0) {
		//var box2 = document.getElementById("shipping_method_box"+old_shipping_method);
		//box2.style.background = "#FFFFFF";
		//box2.style.border = "1px solid #FFFFFF";
	//}
	var resumen_shipping_cost = document.getElementById("resumen_shipping_cost");
	resumen_shipping_cost.innerHTML = "<b>$ "+shipping_cost+"</b>";
	var resumen_payment_method = document.getElementById("resumen_payment_method");
	resumen_payment_method.innerHTML = "No especificada";

	showLoadIcon("step4");
	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	ajax.callPage("/cart.php",showStep4,"POST","do=checkout4&shipping_method_id="+shipping_method+"&shipping_cost="+shipping_cost+"&insurance_skipped=0");
	var ajax2 = new GLM.AJAX();
	ajax2.onError = errorHandler;
	ajax2.callPage("/cart.php",showInsurance,"POST","do=get_insurance&shipping_method_id="+shipping_method);
}

function doCheckout3(which_address) {
	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;

	var checkout_form = document.getElementById("checkout_form");
	if (which_address == 1) {
		var checkout_confirm_address = document.getElementById("checkout_confirm_address");
		checkout_confirm_address.style.visibility = "hidden";
		var checkout_change_address = document.getElementById("checkout_change_address");
		checkout_change_address.style.visibility = "hidden";
		showLoadIcon("step3");
		ajax.callPage("/cart.php",showStep3,"POST","do=checkout3&which_address=1");
	}
	if (which_address == 2) {
		if (!validate_shipping_address(checkout_form)) { return; }
		else {
			var checkout_change_address = document.getElementById("checkout_change_address");
			var checkout_confirm_address = document.getElementById("checkout_confirm_address");
			disableCustomAddress(true);
			checkout_change_address.style.visibility = "visible";
			checkout_confirm_address.style.visibility = "hidden";
			var address = document.getElementById("edit_address");
			var zipcode = document.getElementById("edit_zipcode");
			var id_city = document.getElementById("basic_cities");
			var id_province = document.getElementById("basic_provinces");
			var id_country = document.getElementById("basic_countries");
			showLoadIcon("step3");
			ajax.callPage("/cart.php",showStep3,"POST","do=checkout3&which_address=2&id_city="+id_city.value+"&id_province="+id_province.value+"&id_country="+id_country.value);
		}
	}
	var tdiv1 = document.getElementById("step2_options");
	tdiv1.style.display = "none";
	var tdiv2 = document.getElementById("step3");
	tdiv2.style.visibility = "visible";
	var user_address = document.getElementById("data_address");
	var step2_data = document.getElementById("cart_step_address_data");
	if (which_address == 1)	step2_data.innerHTML = user_address.value;
	else {
		user_address= address.value+' ('+zipcode.value+') '+id_city.options[id_city.selectedIndex].innerHTML+' - '+id_province.options[id_province.selectedIndex].innerHTML+', '+id_country.options[id_country.selectedIndex].innerHTML;
		step2_data.innerHTML = user_address.substr(0,50)+'..';
	}
}

function doCheckout2(type) {

	if (g_shipping_type == type) return;
	else g_shipping_type = type;

	var resumen_shipping_cost = document.getElementById("resumen_shipping_cost");
	resumen_shipping_cost.innerHTML = "<b>$ 0</b>";
	var resumen_insurance_cost = document.getElementById("resumen_insurance_cost");
	resumen_insurance_cost.innerHTML = "<b>$ 0</b>";
	var resumen_payment_method = document.getElementById("resumen_payment_method");
	resumen_payment_method.innerHTML = "No especificada";

	update_resumen_total();

	g_which_address = 0;
	g_shipping_method_id = 0;
	var step1_options;
	var step1_data;
	step1_options = document.getElementById("step1_options");
	step1_options.style.display = 'none';
	step1_data = document.getElementById("cart_step_envio_data");
	if (type == 1) step1_data.innerHTML = 'Retirar en xGaming!';
	else step1_data.innerHTML = 'Envio a Domicilio';
	showLoadIcon("step2");
	var ajax = new GLM.AJAX();
	ajax.onError = errorHandler;
	ajax.callPage("/cart.php",showStep2,"POST","do=checkout2&type="+type);
}

function errorHandler(error) {
	alert(error.name);
	alert(error.message);
}
