﻿// JScript File
var NOMBRE = "MainContent_";
var count;

function Selecciones(mensaje){
	ref = document.getElementById("referencia");
	if (ref != null)
		{
		    if (ref.value != "") {
		        document.getElementById("sReferencia").value = document.getElementById("referencia").value; 
		        return true;
		    }
		}

		if (!AreasZonasSeleccionadas()) {
		    alert(mensaje);
		    return false;
		}
		else {
		    var idTransaccion = 1;
		    if (document.getElementsByName("venta")[0].checked)
		        idTransaccion = 2;
		    if (document.getElementsByName("venta")[1].checked)
		        idTransaccion = 1;

		    document.getElementById("idTransaccion").value = idTransaccion;
		    document.getElementById("idProducto").value = document.getElementById(NOMBRE + "producto").value;
		    if (idTransaccion == 1)
		        document.getElementById("PrecioMax").value=document.getElementById(NOMBRE +"DlPriceMaxA").value;
            else
                document.getElementById("PrecioMax").value=document.getElementById(NOMBRE +"DlPriceMaxV").value;

            document.getElementById("idProvincia").value = document.getElementById(NOMBRE +"provincia").value;
            document.getElementById("idLocalidad").value = document.getElementById("localidad").value;
            document.getElementById("idZona").value = document.getElementById("zona").value;
		    return true;
		}
}



function AreasZonasSeleccionadas() {
    cbA = document.getElementById(NOMBRE +"provincia");
    hdZ1 = document.getElementById("idLocalidad");
    hdZ1.value = "";
    hdB1 = document.getElementById("idZona");
    hdB1.value = "";
    var i;
    var bReturn = false;

    for (i = 1; i < cbA.length; i++) {
        if (cbA[i].selected) {
            bReturn = true;
        }
    }
    return bReturn;
}

			
function CambiarPrecio(val)
{
    document.getElementById(NOMBRE + "DlPriceMaxV").style.display = 'inline';
}

function OptionsCounter(cbA){	
	count = 0;
	for (i = 0; i < cbA.length; i++){
			if (cbA[i].selected) {
				count=i;
			}
		}
	return count;
}

function LimpiarCombo(combo){
	for(i=combo.options.length;i>1;i--)
		combo.remove(1);
}

function ActualizarProvincias(idBuscador, idioma) {
    var idTransaccion = 1;
    if (document.getElementsByName("venta")[0].checked)
        idTransaccion = 2;
    if (document.getElementsByName("venta")[1].checked)
        idTransaccion = 1;
    var cmbProductos = document.getElementById(NOMBRE + "producto");
    //alert("{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cmbProductos.value + "idIdioma:" + idioma + "}");
    $.ajax({ url: "Index.aspx/CargarProvincias",
        data: "{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cmbProductos.value + ",idIdioma:" + idioma + "}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: CallBackProvincias,
        error: functionError
    });
    
}

function ActualizarLocalidades(cbA,idBuscador, idioma)
{
    count = OptionsCounter(cbA);
    var cmbProvincias = document.getElementById(NOMBRE + "provincia");
    var cmbProductos = document.getElementById(NOMBRE + "producto");
    var idTransaccion = 1;
    if (document.getElementsByName("venta")[0].checked)
        idTransaccion = 2;
    if (document.getElementsByName("venta")[1].checked)
        idTransaccion = 1;
    var cbProducto = document.getElementById(NOMBRE + "producto");
    //alert("{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cmbProductos.value + ", idProvincia:" + cmbProvincias.value + ", idIdioma:" + idioma + "}");
    $.ajax({ url: "Index.aspx/CargarLocalidades",
        data: "{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cmbProductos.value + ", idProvincia:" + cmbProvincias.value + ", idIdioma:" + idioma + "}",
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: CallBackLocalidades,
    error: functionError});
}

function ActualizarZonas(cbA, idBuscador, idioma) {

    count = OptionsCounter(cbA);
    var cmbLocalidad = document.getElementById("localidad");
    var idTransaccion = 1;
    if (document.getElementsByName("venta")[0].checked)
        idTransaccion = 2;
    if (document.getElementsByName("venta")[1].checked)
        idTransaccion = 1;
    var cbT = document.getElementById(NOMBRE + "producto");

    //alert("{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cbT.value + ",idLocalidad:" + cmbLocalidad.value + ",idIdioma:" + idioma + "}");
    $.ajax({ url: "Index.aspx/CargarZonas",
        data: "{idBuscadorWeb:" + idBuscador + ",idTransaccion:" + idTransaccion + ",intIdComboProducto:" + cbT.value + ",idLocalidad:" + cmbLocalidad.value + ",idIdioma:" + idioma + "}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: CallBackZonas,
        error: functionError
    });
}

function CallBackProvincias(response) {
    var cmbProvincias = document.getElementById(NOMBRE + "provincia");
    var cmbLocalidades = document.getElementById("localidad");
    var cmbZonas = document.getElementById("zona");
    LimpiarCombo(cmbProvincias);
    LimpiarCombo(cmbLocalidades);
    LimpiarCombo(cmbZonas);

    var ds = jQuery.parseJSON(response.d);
    rows = ds.Tables[0].Rows;

    cmbProvincias.options[0] = new Option('--' + document.getElementById("MI_Provincia").value + '--', "-1");
    cmbLocalidades.options[0] = new Option('--' + document.getElementById("MI_Localidad").value + '--', "-1");
    cmbZonas.options[0] = new Option('--' + document.getElementById("MI_Zona").value + '--', "-1");
    if (rows != null) {
        for (i = 0; i < rows.length; i++) {
            cmbProvincias.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idProvincia"]);
        }
    }
}

function CallBackLocalidades(response) {
    var cmbLocalidades = document.getElementById("localidad");
    var cmbZonas = document.getElementById("zona");
    LimpiarCombo(cmbLocalidades);
    LimpiarCombo(cmbZonas);

    var ds = jQuery.parseJSON(response.d);
    rows = ds.Tables[0].Rows;

    cmbLocalidades.options[0] = new Option('--' + document.getElementById("MI_Localidad").value + '--', "-1");
    cmbZonas.options[0] = new Option('--' + document.getElementById("MI_Zona").value + '--', "-1");
	if (rows != null){		
		for (i = 0; i < rows.length; i++){
		    cmbLocalidades.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idLocalidad"]);
		}
	}	
}

function CallBackZonas(response){
    var cmbZonas = document.getElementById("zona");
    LimpiarCombo(cmbZonas);
    
    var ds = jQuery.parseJSON(response.d);
    rows = ds.Tables[0].Rows;

    cmbZonas.options[0] = new Option('--' + document.getElementById("MI_Zona").value + '--', "-1");
    if (rows != null) {
        for (i = 0; i < rows.length; i++) {
            cmbZonas.options[i + 1] = new Option(rows[i]["sDescripcion"], rows[i]["idZona"]);
        }
    }
}

