var panel_seven, panelCrear;



var ver_login = function(){
    var handleSubmit = function(){
        login = document.getElementById("txtUsuario").value;
        password = document.getElementById("txtPassword").value;
        var error = "";
        if (!login) {
            error += " \"Usuario\" ";
        }
        if (!password) {
            error += " \"Password\" ";
        }
        if (error) {
            document.getElementById("loginError").innerHTML = "falta de llenar los campos" + error
        }
        else {
            var callback = {
                success: function(o){
                    checarUsuario = YAHOO.lang.JSON.parse(o.responseText);
                    //checarUsuario = CallBack.ajaxCallBack("WebServices/globales_checar_usuario.php?login=" + login + "&pass=" + password);
                    if (checarUsuario[0].res == "si") {
                        panel_seven.hide();
                        window.location.reload();
                        panel_seven.destroy();
                    }
                    else {
                        document.getElementById("txtUsuario").value = "";
                        document.getElementById("txtPassword").value = "";
                        document.getElementById("loginError").innerHTML = "Usuario no encontrado";
                        document.getElementById("txtUsuario").focus();
                    }

                },
                failure: function(){

                }
            };
            var sUrl = "WebServices/globales_checar_usuario.php";
            var params = "login=" + login + "&pass=" + password;
            var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
        }

    };
    var handleCancel = function(){
        panel_seven.hide();
        panel_seven.destroy();
    };
    panel_seven = new YAHOO.widget.Dialog("panel_seven", {
        width: "300px",
        visible: true,
        constraintoviewport: true,
        fixedcenter: true,
        modal: true,
        buttons: [{
            text: "Aceptar",
            handler: handleSubmit,
            isDefault: true
        }, {
            text: "Cancel",
            handler: handleCancel
        }]
    });
    var kl = new YAHOO.util.KeyListener(document, {
        keys: 13
    }, {
        fn: handleSubmit,
        scope: panel_seven,
        correctScope: true
    });
    panel_seven.cfg.queueProperty("keylisteners", kl);
    panel_seven.setHeader('Login');
    panel_seven.setBody("<div style='padding-top:-10px;font-size:10px;'>No tienes una cuenta? <a href='#' onclick=' panel_seven.hide();panel_seven.destroy();ver_crear_cuenta();'>Creala Aqui</a></div><br><div class='cssLogin'><label>Usuario:</label><input type='text' id='txtUsuario'/></div><div class='cssLogin'><label>Password:</label><input type='password' id='txtPassword'/></div><div id='loginError'>&nbsp;</div>");
    panel_seven.setFooter('Gracias...');
    panel_seven.render(document.body);
    document.getElementById("txtUsuario").focus();
}
var ver_crear_cuenta = function(){
    var handleSubmit = function(){
        var error = "";
        login = document.getElementById("txtUsuario2").value;
        pass = document.getElementById("txtPassword1").value;
        nombre = document.getElementById("txtNombre").value;
        apellidos = document.getElementById("txtApellidos").value;
        domicilio = document.getElementById("txtDomicilio").value;
        cp = document.getElementById("txtCP").value;
        estado = document.getElementById("slcEstado").value;
        ciudad = document.getElementById("slcCiudad").value;
        email = document.getElementById("txtEmail").value;
        telefono1 = document.getElementById("txtTelefono1").value;
        telefono2 = document.getElementById("txtTelefono2").value;
        if (document.getElementById("chkCel1").checked) {
            cel1 = "1";
        }
        else {
            cel1 = "";
        }
        if (document.getElementById("chkCel2").checked) {
            cel2 = "1";
        }
        else {
            cel2 = "";
        }
        if (document.getElementById("statusLogin").value == "0") {
            error += " Usuario ";
        }
        if (document.getElementById("statusPassword").value == "0") {
            error += " Contraseña ";
        }
        if (!cp) {
            error += " Codigo Postal ";
        }
        if (!estado) {
            error += " Estado ";
        }
        if (!ciudad) {
            error += " Ciudad ";
        }
        if (!telefono1) {
            error += " Telefono1 ";
        }
        if (error) {
            document.getElementById("cuentaError").innerHTML = "favor de llenar los campos:<strong>" + error + "</strong>";
        }
        else {
            var callback = {
                success: function(o){
                    guardarUsuario = YAHOO.lang.JSON.parse(o.responseText);
                    panelCrear.hide();
                    panelCrear.destroy();
                    window.location.reload();
                },
                failure: function(){

                }
            };
            var sUrl = "WebServices/globales_usuario_altas.php";
            var params = "ideEmpresa=" + 1 + "&varUsuario=" + login + "&varPass=" + pass + "&varNombre=" + nombre + "&varApellido=" + apellidos + "&varDomicilio=" + domicilio + "&varCP=" + cp + "&varEmail=" + email + "&varTelefono1=" + telefono1 + "&varTelefono2=" + telefono2 + "&varCel1=" + cel1 + "&varCel2=" + cel2 + "&varEstado=" + estado + "&varCiudad=" + ciudad;
            var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
            //guardarUsuario = CallBack.ajaxCallBack("WebServices/globales_usuario_altas.php?ideEmpresa=" + 1 + "&varUsuario=" + login + "&varPass=" + pass + "&varNombre=" + nombre + "&varApellido=" + apellidos + "&varDomicilio=" + domicilio + "&varCP=" + cp + "&varEmail=" + email + "&varTelefono1=" + telefono1 + "&varTelefono2=" + telefono2 + "&varCel1=" + cel1 + "&varCel2=" + cel2 + "&varEstado=" + estado + "&varCiudad=" + ciudad);
        }
    };
    var handleCancel = function(){
        panelCrear.hide();
        panelCrear.destroy();
    };
    panelCrear = new YAHOO.widget.Dialog("panelCrear", {
        width: "400px",
        visible: true,
        constraintoviewport: true,
        fixedcenter: true,
        modal: true,
        buttons: [{
            text: "Grabar",
            handler: handleSubmit,
            isDefault: true
        }, {
            text: "Cancel",
            handler: handleCancel
        }]
    });
    panelCrear.setHeader('Crear Una Cuenta');
    panelCrear.setBody("<div align='right' style='line-height:2.0;'><div style='padding-top:-10px;font-size:10px;' align='left'>Ya tienes cuenta? <a href='#' onclick=' panelCrear.hide();panelCrear.destroy();verLogin();'>Log In</a></div><div><label><span style='color:red;'>*</span>Usuario: </label><input type='text' id='txtUsuario2' onkeyup='chkUsername(this.value)'/><span id='loginOk'><img src='assets/no.gif'/></span></div><div id='errorUsuario'>&nbsp;</div><div><label><span style='color:red;'>*</span>Contrase&ntilde;a: </label><input type='password' id='txtPassword1' onkeyup='chkPassword()'/><span id='pass1'><img src='assets/no.gif'/></span></div><div><label><span style='color:red;'>*</span>repetir Contrase&ntilde;a: </label><input type='password' id='txtPassword2' onkeyup='chkPassword()'/><span id='pass2'><img src='assets/no.gif'/></span></div><div><label>Nombre(s):<input type='text' id='txtNombre'/></label></div><div><label>Apellido(s):</label><input type='text' id='txtApellidos'/></div><div><label>Domicilio:</label><input type='text' id='txtDomicilio'/></div><div><label><span style='color:red;'>*</span>Codigo Postal:</label><input type='text' id='txtCP'/></div><div><label><span style='color:red;'>*</span>Estado:</label><select id='slcEstado' onchange='verCiudades(this.value);'></select><label><span style='color:red;'>*</span>Ciudad:</label><select id='slcCiudad'></select></div><div><label>Correo Electronico</label><input type='text' id='txtEmail'/></div><div><label><span style='color:red;'>*</span>Telefono1:</label><input type='text' id='txtTelefono1'/><input type='checkbox' id='chkCel1'/>&iquest;Cel?</div><div><label>Telefono2:</label><input type='text' id='txtTelefono2'/><input type='checkbox' id='chkCel2'/>&iquest;Cel?</div><div id='cuentaError'>&nbsp;</div></div><input id='statusLogin' value='0' type='hidden'/><input id='statusPassword' value='0' type='hidden'/>");
    panelCrear.setFooter('Gracias...');
    panelCrear.render(document.body);
    var callback = {
        success: function(o){
            listaEstados = YAHOO.lang.JSON.parse(o.responseText);
            llenarListaCatalogo(listaEstados, "slcEstado", "", "Sel. Estado");
            document.getElementById("txtUsuario2").focus();
        },
        failure: function(){

        }
    };
    var sUrl = "WebServices/globales_lista_estados.php";
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
    //listaEstados = CallBack.ajaxCallBack("WebServices/globales_lista_estados.php");

}
var logout = function(){
    var callback = {
        success: function(o){
            listaLogout = YAHOO.lang.JSON.parse(o.responseText);
            if (listaLogout[0].respuesta == "SI") {
                window.location.reload();
            }
            else {
                alert("favor de intentas mas tarde, gracias")
            }

        },
        failure: function(){

        }
    };
    var sUrl = "WebServices/globales_logout.php";
    var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}
var validar_busqueda1 = function(){
    marca = document.getElementById("lista_marcas1").value;
    modelo = document.getElementById("lista_modelos1").value;
    if (marca){
        window.location.href = 'busquedas.php?estilo=&marca='+marca+'&modelo='+modelo+'&a1=&a2=&p1=&p2=&p=1&max=10';
    }else{
        alert("Favor de Seleccionar una marca, gracias...")
    }
}
var lista_modelos = function(op){
    var marca
    if (op==1){
        marca = document.getElementById("lista_marcas1").value;
    }else{
        marca = document.getElementById("lista_marcas2").value;
    }

    var callback = {
        success: function(o){
            listaDatos = YAHOO.lang.JSON.parse(o.responseText);
            if (op==1){
                llenarListaCatalogo(listaDatos.modelos,"lista_modelos1","","Sel. Modelo")
            }else{
                llenarListaCatalogo(listaDatos.modelos,"lista_modelos2","","Sel. Modelo")
            }
        },
        failure: function(){

        }
    };
    var sUrl = "WebServices/lista_modelos.php";
    var params = 'marca='+marca;
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
}
var verCiudades = function(valor){
    var callback = {
        success: function(o){
            listaCiudades = YAHOO.lang.JSON.parse(o.responseText);
            llenarListaCatalogo(listaCiudades, "slcCiudad", "", "Sel. Ciudad");
        },
        failure: function(){

        }
    };
    var sUrl = "WebServices/globales_lista_ciudades.php";
    var params = "ideEstado=" + valor;
    var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
}

var chkUsername = function(valor){
    if (valor.length > 5) {
        var callback = {
            success: function(o){
                checarLogin = YAHOO.lang.JSON.parse(o.responseText);
                if (checarLogin[0].resultado == "SI") {
                    document.getElementById("errorUsuario").innerHTML = "&nbsp;";
                    document.getElementById("statusLogin").value = "1";
                    document.getElementById("loginOk").innerHTML = "<img src='assets/global/ok.gif'/>";
                }
                else {
                    document.getElementById("errorUsuario").innerHTML = "Usuario no disponible";
                    document.getElementById("statusLogin").value = "0";
                    document.getElementById("loginOk").innerHTML = "<img src='assets/global/no.gif'/>";
                }
            },
            failure: function(){

            }
        };
        var sUrl = "WebServices/usuarios_checar_login.php";
        var params = "valor=" + valor;
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
    }
    else {
        document.getElementById("errorUsuario").innerHTML = "minino 5 caracteres";
        document.getElementById("statusLogin").value = "0";
        document.getElementById("loginOk").innerHTML = "<img src='assets/global/no.gif'/>";
    }
}

var chkPassword = function(){
    password1 = document.getElementById("txtPassword1").value;
    password2 = document.getElementById("txtPassword2").value;
    if (password1 == password2 && password1 != "") {
        document.getElementById("pass1").innerHTML = "<img src='assets/global/ok.gif'/>";
        document.getElementById("pass2").innerHTML = "<img src='assets/global/ok.gif'/>";
        document.getElementById("statusPassword").value = "1";

    }
    else {
        document.getElementById("pass1").innerHTML = "<img src='assets/global/no.gif'/>";
        document.getElementById("pass2").innerHTML = "<img src='assets/global/no.gif'/>";
        document.getElementById("statusPassword").value = "0";
    }
}
var validar_ide = function(){
    ide = document.getElementById("txt_ide").value;
    if (ide) {
        var callback = {
            success: function(o){
                lista_ide = YAHOO.lang.JSON.parse(o.responseText);
                if (lista_ide){
                    window.location.href = "anuncio.php?ide="+ide;
                }else{
                    alert("No se encontro el IDE especificado")
                }
            },
            failure: function(){
            }
        };
        var sUrl = "WebServices/globales_checar_ide.php";
        var params = 'ide=' + ide;
        var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, params);
    }else{
        alert("Favor de especificar un IDE, gracias...")
    }
}
