// JavaScript Document

/*
* runOnLoad.js: portable registration for onload event handlers.
* 
* This module defines a single runOnLoad() function for portably registering
* functions that can be safely invoked only when the document is fully loaded
* and the DOM is available.
*
* Functions registered with runOnLoad() will not be passed any arguments when
* invoked. They will not be invoked as a method of any meaningful object, and
* the this keyword should not be used.  Functions registered with runOnLoad()
* will be invoked in the order in which they were registered.  There is no
* way to deregister a function once it has been passed to runOnLoad().
*
* In old browsers that do not support addEventListener() or attachEvent(),
* this function relies on the DOM Level 0 window.onload property and will not
* work correctly when used in documents that set the onload attribute
* of their <body> or <frameset> tags.
*/
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }

    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;

/*------------------------ funciones de validacion y otros------------------*/

runOnLoad(function(){
    $("input#x_nombre").select().focus();
});

function redirection(sSeccion){  
    if (sSeccion =='bzClub'){
        window.location ="mi_cuenta.php";
    }else{
        window.location ="cat_contenidos.php";
    }
};

function BuscaLetra(x){
    var numero=0;
    for(i=0;i<x.length;i++)
        {
        if(x.charAt(i)=="k") numero++;
    }
    return numero;
}


function registroMayoristas(){


    // validate and process form
    // first hide any error messages
    $('div[class*="inputs_txt_error"]').removeClass("inputs_txt_error");
    $('div[id*="inputs_txt"]').addClass("inputs_txt");
    var control=0;
    var name = $("input#x_nombre").val();
    if (name == "") {
        $("#inputs_nombre").removeClass("inputs_txt");
        $("#inputs_nombre").addClass("inputs_txt_error");
        $("input#x_nombre").focus();
        control=1;
        //return false;
    }
    var apellido = $("input#x_apellido").val();
    if (apellido == "") {
        $("#inputs_apellido").removeClass("inputs_txt");
        $("#inputs_apellido").addClass("inputs_txt_error");
        $("input#x_apellido").focus();
        control=1;
        //return false;
    }
    var email = $("input#x_email").val();
    if (email == "") {
        $("#inputs_email").removeClass("inputs_txt");
        $("#inputs_email").addClass("inputs_txt_error");
        $("input#x_email").focus();
        control=1;
        //return false;
    }
    var codArea = $("input#x_codarea").val();
    if (codArea == "") {
        $("#inputs_fijo").removeClass("inputs_txt");
        $("#inputs_fijo").addClass("inputs_txt_error");
        $("input#x_codarea").focus();
        control=1;
        //return false;
    }
    var telefono = $("input#x_numtel").val();
    if (telefono == "") {
        $("#inputs_fijo").removeClass("inputs_txt");
        $("#inputs_fijo").addClass("inputs_txt_error");
        $("input#x_numtel").focus();
        control=1;
        //return false;
    }
    var codAreaCel = $("input#x_codareacel").val();
    if (codAreaCel == "") {
        $("#inputs_celular").removeClass("inputs_txt");
        $("#inputs_celular").addClass("inputs_txt_error");
        $("input#x_codareacel").focus();
        control=1;
        //return false;
    }
    var codServicioCel = $("select#x_codservicio").val();
    if (codServicioCel == "") {
        $("#inputs_celular").removeClass("inputs_txt");
        $("#inputs_celular").addClass("inputs_txt_error");
        $("select#x_codservicio").focus();
        control=1;
        //return false;
    }
    var celular = $("input#x_celular").val();
    if (celular == "") {
        $("#inputs_celular").removeClass("inputs_txt");
        $("#inputs_celular").addClass("inputs_txt_error");
        $("input#x_celular").focus();
        control=1;
        //return false;
    }
    var localidad = $("input#x_localidad").val();
    if (localidad == "") {
        $("#inputs_localidad").removeClass("inputs_txt");
        $("#inputs_localidad").addClass("inputs_txt_error");
        $("input#x_localidad").focus();
        control=1;
        //return false;
    }
    var provincia = $("select#x_provincia").val();
    if (provincia == "") {
        $("#inputs_provincia").removeClass("inputs_txt");
        $("#inputs_provincia").addClass("inputs_txt_error");
        $("select#x_provincia").focus();
        control=1;
        //return false;
    }
    if(control==1){
        return false;
    }
    var dataString = 'x_nombre='+ name + '&x_apellido=' + apellido + '&x_email=' + email + '&x_codarea=' + codArea + '&x_numtel=' + telefono + '&x_codareacel=' + codAreaCel + '&x_codservicio=' + codServicioCel + '&x_celular=' + celular + '&x_localidad=' + localidad + '&x_provincia=' + provincia + '&x_estado="reg"';
    //alert (dataString);return false;

    $.ajax({
        type: "POST",
        url: "registro_may.php",
        data: dataString,
        beforeSend: function(objeto){
            $('.ct_section_content_form').html("<img src='images/body/loading.gif' alt='Por favor espere...'/>");
        },

        success: function(r) {
            //alert (datos);
            var obj = eval("("+r+")");
            $('#contact_form').html("<div class='ct_section_content_form'></div>");
            if(obj.success){
                var mensaje = name + " " + apellido + ", gracias por registrarte. Se ha enviado a " + email + " un mensaje con un enlace en el cual debes hacer clic para que actives tu cuenta mayorista. Compra por mayor en línea activando tu cuenta mayorista.<br/>";
                $('.ct_section_content_form').html("<div class='ct_section_content_form_sended'>"+ mensaje +"</div>").hide().fadeIn(1500, function() {
                    $('.ct_section_content_form_sended').append("<img id='checkmark' src='images/body/check.png' />");
                });
            }else{
                $('.ct_section_content_form').html("<div class='ct_section_content_form_sended'>"+ obj.error +"</div>").hide().fadeIn(1500);

            }
        }
    });
    return false;

}
