var xmlHttp;
var v_responseObject;
var v_Provincia;


//--------------------------------------------------------------------------------------------
function GetXmlHttpObject(handler)
{
	var objXmlHttp=null;

	if (navigator.userAgent.indexOf("MSIE")>=0)
	{
	    var strName="Msxml2.XMLHTTP";
	    if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
    	    strName="Microsoft.XMLHTTP";
	    }
	    try {
	        objXmlHttp=new ActiveXObject(strName);
	        objXmlHttp.onreadystatechange=handler;
	        return objXmlHttp;
	    }
	    catch(e){
	        alert("Error. Scripting for ActiveX might be disabled");
	        return;
	    }
	}
	if (navigator.userAgent.indexOf("Mozilla")>=0) {
	    objXmlHttp=new XMLHttpRequest();
	    objXmlHttp.onload=handler;
	    objXmlHttp.onerror=handler;
	    return objXmlHttp;
    }
}
//---------------------------------------------------------------------------------------------------------
function EjecutarxmlHttP(v_url, object) {

    xmlHttp=GetXmlHttpObject(object);
	xmlHttp.open("GET", v_url , true);
	xmlHttp.setRequestHeader("charset", "iso-8859-1");
	xmlHttp.send(null);
}
//--------------------------------------------------------------------------------------------
function GuardarObjectResponse () {
    v_responseObject = document.getElementById('p_ObjResponse');
}


//--------------------------------------------------------------------------------------------
function BuscarZonas() {    
    
    var Provincia = document.getElementById('ddlProvincias').value;
    
    
    
    if(Provincia == '-1') return;
    
    v_Provincia = Provincia;
	var v_url="Via_CargarDatos.asp?Accion=Zonas&Provincia=" + Provincia;
	
    if(document.getElementById('ddlProvinciasDomicilio') != undefined)
        document.getElementById('ddlProvinciasDomicilio').value = Provincia;
        
    MostraLoading('divLoadingZonas', true);
	GuardarObjectResponse();    
	if(document.getElementById('modificoDomicilio') != undefined)
        document.getElementById('modificoDomicilio').value = '1';
    //setTimeout(function() {EjecutarxmlHttP(v_url, stateChangedBuscarZonas);},800);
    document.getElementById('divPaquetes').innerHTML =  '';
    document.getElementById('divResultadoPromo').innerHTML =  '';
    document.getElementById('divPromocionesPrecios').style.display = "none"
    document.getElementById('divFormasDePago').style.display = "none"
    document.getElementById('divDatosContacto').style.display = "none"
    
    EjecutarxmlHttP(v_url, stateChangedBuscarZonas);
}
//--------------------------------------------------------------------------------------------
function stateChangedBuscarZonas() {

    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        v_responseObject.value = xmlHttp.responseText;       
        var sResult = v_responseObject.value.split("/*/")
        
        
       document.getElementById('divResultado').innerHTML =  sResult[0];
       if(document.getElementById('divZonasDomicilio') != undefined)
            document.getElementById('divZonasDomicilio').innerHTML =  sResult[3];
            
        //diferenciar si es Capital y gba no mostrar combo de Zona
        //No muestra el combo de Zonas y setea la primer zona para buscar las promos
        if(v_Provincia == '2' || v_Provincia == '164') {
            document.getElementById('divResultado').style.display = "none";
            document.getElementById('ddlZonas').selectedIndex = 1;
            BuscarPromoZonas(document.getElementById('ddlZonas').value);
            
           if(document.getElementById('divZonasDomicilio') != undefined)                          
                document.getElementById('ddlZonasDomicilio').disabled = false;
            } else {
            document.getElementById('divResultado').style.display = "block";
            if(document.getElementById('divZonasDomicilio') != undefined)                          
                document.getElementById('ddlZonasDomicilio').disabled = true;
        }
            
        if(sResult[1] == 1){
            document.getElementById('ddlZonas').selectedIndex = 1;
            BuscarPromoZonas(document.getElementById('ddlZonas').value);
        } 
        
        if(sResult[2] != null && sResult[2] != '') {            
            document.getElementById('ddlZonas').selectedValue = sResult[2];
            BuscarPromoZonas(document.getElementById('ddlZonas').value);
        }
        if (document.getElementById('hdnZonas') != undefined) {            
            if(document.getElementById('hdnZonas').value != '') {
                document.getElementById('ddlZonas').value = document.getElementById('hdnZonas').value;                
                BuscarPromoZonas(document.getElementById('ddlZonas').value);
                if (document.getElementById('divZonasDomicilio') != undefined) {
                    if (v_Provincia == '164')                        
                        document.getElementById('ddlZonasDomicilio').value = -1;
                    else
                        document.getElementById('ddlZonasDomicilio').value = document.getElementById('hdnZonas').value;
                }                    
            }   
        }       
       MostraLoading('divLoadingZonas', false);
      }
}

function MostraLoading(div,Estado) {
    
    document.getElementById(div).style.display = Estado == true ? "block" : 'none';
}

function BuscarPromoZonas(Zona) {
  
	var v_url="Via_CargarDatos.asp?Accion=Promo&Zona=" + Zona;
	
	if(document.getElementById('hdnZonas') != undefined)
	    document.getElementById('hdnZonas').value = Zona;
	
	//if(document.getElementById('ddlZonasDomicilio') != undefined)
	    //document.getElementById('ddlZonasDomicilio').value = Zona;
	    
    if(document.getElementById('modificoDomicilio') != undefined)
        document.getElementById('modificoDomicilio').value = '1';
    MostraLoading('divLoadingZonas', true);
	GuardarObjectResponse();    

    //setTimeout(function() {EjecutarxmlHttP(v_url, stateChangedBuscarZonas);},800);
    EjecutarxmlHttP(v_url, stateChangedBuscarPromoZonas);
}

//--------------------------------------------------------------------------------------------
function stateChangedBuscarPromoZonas() {

    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        v_responseObject.value = xmlHttp.responseText;      
        if(sResult != "") { 
            var sResult = v_responseObject.value.split("/*/")
        
            if(v_Provincia == '2' || v_Provincia == '164') 
                document.getElementById('sAclaracionPrecios').style.display = 'none';
            else
                document.getElementById('sAclaracionPrecios').style.display = 'block';

           //document.getElementById('divResultado').innerHTML =  v_responseObject.value;
           document.getElementById('divPaquetes').innerHTML =  sResult[0];
           document.getElementById('divResultadoPromo').innerHTML =  sResult[1];
           
           document.getElementById('divPromocionesPrecios').style.display = "block";
           document.getElementById('divFormasDePago').style.display = "block";
           document.getElementById('divDatosContacto').style.display = "block";
           if(document.getElementById('ddlProvinciasDomicilio') != undefined)
                document.getElementById('ddlProvinciasDomicilio').disabled = true;
           if(document.getElementById('divZonasDomicilio') != undefined)
                //document.getElementById('ddlZonasDomicilio').value = document.getElementById('hdnZonas').value;
               if (v_Provincia == '164')
                   document.getElementById('ddlZonasDomicilio').value = -1;
               else
                   document.getElementById('ddlZonasDomicilio').value = document.getElementById('hdnZonas').value;
        }
       MostraLoading('divLoadingZonas', false);
      }
}
function CargarZonasByProvincia(Provincia, Nombre) {

	var v_url="Via_CargarDatos.asp?Accion=Cobertura&Prov=" + Provincia + "&Nombre=" + Nombre;
	
	GuardarObjectResponse();    
    EjecutarxmlHttP(v_url, stateChangedCargarZonasByProvincia);
}

function stateChangedCargarZonasByProvincia() {

    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        v_responseObject.value = xmlHttp.responseText;     
        
        var sResult = v_responseObject.value.split("/*/") 
        document.getElementById('sTituloZona').innerHTML = sResult[1];
        document.getElementById('divDetalleZonas').innerHTML = sResult[0];  
        document.getElementById('tZonas').style.display = 'block';
        
      }
}

function SelectCiudadesByProvincia(Provincia) {

	var v_url="Via_CargarDatos.asp?Accion=Delivery&Prov=" + Provincia;
	
	GuardarObjectResponse();    
    EjecutarxmlHttP(v_url, stateChangedSelectCiudadesByProvincia);
}

function stateChangedSelectCiudadesByProvincia() {

        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        v_responseObject.value = xmlHttp.responseText;     
                        
        document.getElementById('divZonas').innerHTML = v_responseObject.value;          
        
      }
}
