var buscadortext= "Buscar ...";


$(document).ready(function() {
    document.getElementById('buscar').value = buscadortext;
});

function buscadorFocus()
{
    if(document.getElementById('buscar').value == buscadortext)
        document.getElementById('buscar').value = "";
}

function buscadorBlur()
{
    if(document.getElementById('buscar').value == "")
        document.getElementById('buscar').value = buscadortext;
}

function buscar(base, tipus)
{
    var text= document.getElementById('buscar').value;

    if(text != '' && text != buscadortext){
        window.location=base+'home/buscar/'+text;
    }
}

function favoritos()
{
    if((navigator.appName=="Microsoft Internet Explorer") && (parseInt(navigator.appVersion)>=4))
        window.external.AddFavorite('http://www.telejobs.com','Telejobs');
    else//Para Netscape y Firefox
    {
        if(navigator.appName=="Netscape" || navigator.appName=="Firefox")
            alert ("Presione Crtl+D para agregar a este sitio a sus Favoritos");//Puedes personalizar este mensaje
    }
}

function change(what, where, base_url)
{
//    if(where){
//        window.location.href = base_url + where;
//    }
    var logForm = document.createElement("form");
    logForm.method="post";

    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", 'mode');
    myInput.setAttribute("type", 'hidden');
    myInput.setAttribute("value", "change"+what);
    logForm.appendChild(myInput);

    var myInput = document.createElement("input") ;
    myInput.setAttribute("type", 'hidden');
    myInput.setAttribute("name", what);
    myInput.setAttribute("value", document.getElementById(what).value);
    logForm.appendChild(myInput);

    document.body.appendChild(logForm) ;
    logForm.submit();
    document.body.removeChild(logForm) ;
    
}

function fase(url,fase)
{
    var logForm = document.createElement("form");
    logForm.method="post";
    logForm.action=url;
	
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", 'stage');
    myInput.setAttribute("value", fase);
    logForm.appendChild(myInput);

    document.body.appendChild(logForm) ;
    logForm.submit();
    document.body.removeChild(logForm) ;
}

function buscatele(url)
{
    if(document.getElementById('fav_fk').value == -1)
    {
        tb_show("Buscar telejober", "#TB_inline?height=360&width=480&inlineId=eldiv", "",
            function(){
                if ($("#selected_id").val() > 0)
                {
                    var opts = $("#fav_fk").attr("options");
                    opts[opts.length] = new Option($("#selected_name").val(), $("#selected_id").val(), true, true);
                }
            });
    }
}

function save(){
    document.theform.submit();
}

function trim (myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function next(val_personal){
    if(val_personal == "empresa" || val_personal == "telejober"){
        document.getElementById('name').value = trim(document.getElementById('name').value);
        if(document.getElementById('name').value == ''){
            alert('Debe introducir su nombre y apellidos');
            return false;
        }
        document.getElementById('nif_cif').value = trim(document.getElementById('nif_cif').value);
        if(document.getElementById('nif_cif').value != ''){
            if(/^[A-Za-z0-9]{1}[0-9]{7}[A-Za-z0-9]{1}$/.test(document.getElementById('nif_cif').value)){
                if(val_personal == 'empresa'){
                    alert('CIF incorrecto, revisalo por favor.'+ "\n\r"+'Es necesario para registrarte como empresa');
                    return false;
                }
            }else{
                if(val_personal == 'telejober'){
                    alert('DNI incorrecto, revisalo por favor.'+ "\n\r"+'Es necesario para registrarte como telejober');
                    return false;
                }
            }
        }else{
            alert('Debes introducir tu NIF / CIF');
            return false;
        }
    }
    var foo = document.getElementById("regtype");
    if (foo == null){
        document.theform.submit();
    } else{
        if (foo.value == 0){
            alert("Debe seleccionar un tipo de registro.");
            foo.style.backgroundColor = "#E6878F";
            foo.focus();
        } else{
            document.theform.submit();
        }
    }
}
function skiptel(baseurl)
{
    document.theform.action = baseurl;
    document.theform.skipped.value = "1";
    document.theform.submit();
}
function skipemp(baseurl)
{
    document.theform.action = baseurl;
    document.theform.skipped.value = "1";
    document.theform.submit();
}
function end(){
    document.theform.submit();
}

function gotoregistro(base)
{
    var rute= base+"registro/";
	
    switch(document.getElementById('regtype').value)
    {
        case "1":
            window.location=rute+"telejober";
            break;
        case "2":
            window.location=rute+"empresa";
            break;
        default:
            break;
    }
}

function gotologin(base)
{
    var logForm = document.createElement("form");
    logForm.method="post";
    logForm.action = base+"home/login";

    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", 'username');
    myInput.setAttribute("value", document.getElementById('user').value);
    logForm.appendChild(myInput);
	
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", 'password');
    myInput.setAttribute("value", document.getElementById('pwd').value);
    logForm.appendChild(myInput);

    document.body.appendChild(logForm) ;
    logForm.submit();
    document.body.removeChild(logForm) ;
}

function gotologout(base)
{
    var rute= base+"home/logout/";
    window.location= rute;
}

function loadProvs(base, value)
{	
    $.ajax({
        type: "POST",
        url: base + "registro/getprovs/" + value,
        success: function(msg)
        {
            $("#html_prov_select").html(msg);
        }
    });
}

function busca(base)
{
    if(!document.theform.buscar.value)
    {
        document.theform.buscar.value="todos";
    }
    document.theform.action= base+"/"+document.theform.buscar.value+"/0";
    document.theform.submit();
}

function valida_contacto()
{
    var n = document.contacto.nombre.value;
    var m = document.contacto.mail.value;
    if ( Trim(n)=="" )
    {
        alert("El nombre introducido no es correcto.");
        document.contacto.nombre.focus();
    }
    else
    {
        //if(validarEmail(m))
        if(Trim(m)!="")
        {
            document.contacto.submit();
        }
        else
        {
            alert("La dirección de correo electrónico introducida no es correcta.");
            document.contacto.mail.focus();
        }
    }
}

function Trim( str ) 
{
    var resultStr = "";
    resultStr = TrimLeft(str);
    resultStr = TrimRight(resultStr);
    return resultStr;
}

function TrimLeft( str )
{
    var resultStr = "";
    var i = len = 0;
    if (str+"" == "undefined" || str == null)
        return "";
    str += "";

    if (str.length == 0)
        resultStr = "";
    else
    {
        len = str.length;
        while ((i <= len) && (str.charAt(i) == " "))
            i++;
        resultStr = str.substring(i, len);
    }
    return resultStr;
}

function TrimRight( str )
{
    var resultStr = "";
    var i = 0;
    if (str+"" == "undefined" || str == null)
        return "";
    str += "";
    if (str.length == 0)
        resultStr = "";
    else
    {
        i = str.length - 1;
        while ((i >= 0) && (str.charAt(i) == " "))
            i--;
        resultStr = str.substring(0, i + 1);
    }
    return resultStr;
}

function validarEmail(m) 
{
    //b = /^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/;
    //return (b.test(m));
    return true;
}
document.getElementsByClassName = function(cl) {
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var i = 0; i < elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)){
            retnode.push(elem[i]);
        }
    }
    return retnode;
};

function validate()
{
    elements = document.getElementsByClassName('revisa');
    var numerito = elements.length;
    for(i=0;i<numerito;i++){
        if(elements[i].value == ''){
            alert('Por favor, complete todos los campos');
            elements[i].focus();
            return;
        }
    }
    document.theform.submit();
}

