function Ajax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
function getMensajeCombo(msg){
	return "<div style=\"background-color:#2d70af;color:#ffffff;padding:3px; width:227px;\">"+msg+"</div>";
}
function getCargador(){
	return "<div style='text-align:center;'><img src='../img/cargador.gif' width='29' height='28' style='margin-top:20px;margin-bottom:20px;'/></div>";
}
// DOMICILIO
function comboDepartamentoDom(defecto){
	var ajax = Ajax();
	var idPais = document.us_fr.idPais.value;
	ajax.open("GET","/data/ajax_combo_departamentos.php?idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("Dep").innerHTML = ajax.responseText;
		}else{
			document.getElementById("Dep").innerHTML = getMensajeCombo("Cargando departamentos...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadPaisDom(defecto,idDep){
	
	var ajax = Ajax();
	var idPais = document.us_fr.idPais.value;
	ajax.open("GET","/data/ajax_combo_localidades.php?idDepartamento="+idDep+"&idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("Loc").innerHTML = ajax.responseText;
		}else{
			document.getElementById("Loc").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadDom(defecto){
	var ajax = Ajax();
	var idDepartamento = document.us_fr.idDepartamento.value;
	ajax.open("GET","/data/ajax_combo_localidades.php?idDepartamento="+idDepartamento+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText!=""){
				document.getElementById("Loc").innerHTML = ajax.responseText
			}else{
				document.getElementById("Loc").innerHTML = "Ninguno";
			}
		}else{
			document.getElementById("Loc").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}

// ENVIO

function comboDepartamentoDomEnv(defecto){
	var ajax = Ajax();
	var idPais = document.us_fr.idPaisEnv.value;
	ajax.open("GET","/data/jax_combo_departamentos_env.php?idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("DepEnv").innerHTML = ajax.responseText;
		}else{
			document.getElementById("DepEnv").innerHTML = getMensajeCombo("Cargando departamentos...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadPaisDomEnv(defecto,idDep){
	
	var ajax = Ajax();
	var idPais = document.us_fr.idPaisEnv.value;
	ajax.open("GET","/data/ajax_combo_localidades_env.php?idDepartamento="+idDep+"&idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("LocEnv").innerHTML = ajax.responseText;
		}else{
			document.getElementById("LocEnv").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadDomEnv(defecto){
	var ajax = Ajax();
	var idDepartamento = document.us_fr.idDepartamentoEnv.value;
	ajax.open("GET","/data/ajax_combo_localidades_env.php?idDepartamento="+idDepartamento+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText!=""){
				document.getElementById("LocEnv").innerHTML = ajax.responseText
			}else{
				document.getElementById("LocEnv").innerHTML = "Ninguno";
			}
		}else{
			document.getElementById("LocEnv").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}

// CARRITO ////////////////////////////////////////////////////////////////////////////////////////////////

function mostrarFoto(foto,id){
	var ajax = Ajax();
	var cont = document.getElementById(id);
	if(cont.style.visibility == "hidden"){
		cont.style.visibility="visible";
		cont.style.display="block";
		ajax.open("GET","/data/ajax_foto.php?foto="+foto,true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				document.getElementById(id).innerHTML = ajax.responseText;
			}else{
				document.getElementById(id).innerHTML = "<br/><br/><br/><br/><br/><br/>Cargando imagen...aguarde.";
			}
		}
		ajax.send(null);
	}else{
		cont.style.visibility="hidden";
		cont.style.display="none";	
	}
}

function agregarACarrito(){
	var cantidad = document.producto.cantidad;
	if(cantidad){
		if(cantidad.value==""){
			alert("Enter the amount you want to buy.");
		}else{
			if(isNaN(cantidad.value)){
				alert("The amount must be a numeric value.");		
			}else{
				if(cantidad.value<1){
					alert("The amount should be a positive value.");
				}else{
					var id = document.producto.idPro.value; 
					var ajax = Ajax();
					ajax.open("GET","/data/ajax_carrito.php?idPro="+id+"&cantidad="+cantidad.value,true);
					ajax.onreadystatechange=function() {
						if (ajax.readyState==4) {
							document.getElementById("resultado_carrito").innerHTML = ajax.responseText;
						}else{
							document.getElementById("resultado_carrito").innerHTML = getCargador();
						}
					}
					ajax.send(null);
				}
			}
		}
	}
}
// COMPRA //////////////////////////////////////////////////////////////////////////////////////////
function mostrarTotal(costo_envio,monto_minimo,mostrar_mensaje){
	var ajax = Ajax();
	ajax.open("GET","ajax_compra_total.php?costo_envio="+costo_envio+"&monto_minimo="+monto_minimo+"&mostrar_mensaje="+mostrar_mensaje,true);
	ajax.onreadystatechange=function() {
		if(ajax.readyState==4){
			document.getElementById("compra_total").innerHTML = ajax.responseText;
		}else{
			document.getElementById("compra_total").innerHTML = "<div style=\"widht:486px; height:99px;border:1px solid #c4c0c0;\">"+getCargador()+"</div>";
		}
	}
	ajax.send(null);
	
	var ajax_pie = Ajax();
	ajax_pie.open("GET","/data/ajax_compra_total_pie.php?costo_envio="+costo_envio+"&monto_minimo="+monto_minimo,true);
	ajax_pie.onreadystatechange=function() {
		if(ajax_pie.readyState==4){
			document.getElementById("compra_total_pie").innerHTML = ajax_pie.responseText;
		}
	}
	ajax_pie.send(null);
}

function mostrarEnvios(idPais,idDep,idLoc){
	var ajax = Ajax();
	ajax.open("GET","/data/ajax_compra_envio.php?idPais="+idPais+"&idDep="+idDep+"&idLoc="+idLoc,true);
	ajax.onreadystatechange=function() {
		if(ajax.readyState==4){
			document.getElementById("compra_envio").innerHTML = ajax.responseText;
		}else{
			document.getElementById("compra_envio").innerHTML = getCargador();
		}
	}
	ajax.send(null);
}

// ENVIO //////////////////////////////////////////////////////////////////////////////////////////////////////////////

function comboDepartamentoCompra(defecto){
	var ajax = Ajax();
	var idPais = document.us_fr.idPaisEnv.value;
	ajax.open("GET","/data/ajax_combo_departamentos_compra.php?idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("DepEnv").innerHTML = ajax.responseText;
		}else{
			document.getElementById("DepEnv").innerHTML = getMensajeCombo("Cargando departamentos...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadPaisCompra(defecto,idDep){
	
	var ajax = Ajax();
	var idPais = document.us_fr.idPaisEnv.value;
	ajax.open("GET","/data/ajax_combo_localidades_compra.php?idDepartamento="+idDep+"&idPais="+idPais+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			document.getElementById("LocEnv").innerHTML = ajax.responseText;
		}else{
			document.getElementById("LocEnv").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}
function comboLocalidadCompra(defecto){
	var ajax = Ajax();
	var idDepartamento = document.us_fr.idDepartamentoEnv.value;
	var idPais = document.us_fr.idPaisEnv.value;
	ajax.open("GET","/data/ajax_combo_localidades_compra.php?idPais="+idPais+"&idDepartamento="+idDepartamento+"&defecto="+defecto,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText!=""){
				document.getElementById("LocEnv").innerHTML = ajax.responseText
			}else{
				document.getElementById("LocEnv").innerHTML = "Ninguno";
			}
		}else{
			document.getElementById("LocEnv").innerHTML = getMensajeCombo("Cargando localidades...aguarde.");
		}
	}
	ajax.send(null);
}

// COMPRA RAPIDA ///////////////////////////////////////////////////////////////////////////////////////////////////////
function verificarDisponibilidad(id){
	var codigo = document.getElementById("codigo_"+id).value;
	var subcodigo = document.getElementById("subcodigo_"+id).value;
	var cantidad = document.getElementById("cantidad_"+id).value;
	if(codigo>0 & subcodigo>0 &cantidad>0){
	var ajax = Ajax();
	ajax.open("GET","/data/ajax_compra_verificar_producto.php?codigo="+codigo+"&subcodigo="+subcodigo+"&cantidad="+cantidad,true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			if(ajax.responseText==1){
				var html="";
				html+="<a href=\"javascript:verificarDisponibilidad("+id+");\">";
				html+="<img src=\"/img/icono_verificar2.gif\" alt=\"Verificar disponibilidad\" width=\"25\" height=\"23\" />";
				html+="</a>";
				document.getElementById("verificar_"+id).innerHTML = html;
			}else{
				var html="";
				html+="<a href=\"javascript:verificarDisponibilidad("+id+");\">";
				html+="<img src=\"/img/icono_verificar3.gif\" alt=\"Verificar disponibilidad\" width=\"25\" height=\"23\" />";
				html+="</a>";
				document.getElementById("verificar_"+id).innerHTML = html;
			}
		}else{
			document.getElementById("verificar_"+id).innerHTML = "Verificando...";
		}
	}
	ajax.send(null);
	}else{
		getMensaje("Los valores ingresados para este artículo no son correctos.");	
	}
}