/*****************************
Desarrollador: Gonzalo Barrera Sáez
Contacto: gonzalo@gmod.cl
*****************************/
// Javascript

// Valida solo numeros
var nav4 = window.Event ? true : false;
function acceptNum (e) {
    var e = e || window.event,
    key = e.which || e.keyCode;
    return (key <= 13 || (key >= 48 && key <= 57));
}

// Elimina espacios en blanco
function trim(s) {
    while (s.substring(0,1) == ' ') {
        s = s.substring(1,s.length);
    }
    while (s.substring(s.length-1,s.length) == ' ') {
        s = s.substring(0,s.length-1);
    }
    return s;
}

// Valida correo electronico 
function validarEmail(valor) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
        return (true)
    } else {
        alert("La direccion de email es incorrecta.");
        return (false);
    }
}
 
// Validacion de RUT
function Valida_Rut( Objeto )
{
    var tmpstr = "";
    var intlargo = Objeto.value
    if (intlargo.length> 0)
    {
        crut = Objeto.value
        largo = crut.length;
        if ( largo <2 )
        {
            alert('rut inválido')
            Objeto.focus()
            return false;
        }
        for ( i=0; i <crut.length ; i++ )
            if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
            {
                tmpstr = tmpstr + crut.charAt(i);
            }
        rut = tmpstr;
        crut=tmpstr;
        largo = crut.length;
	
        if ( largo> 2 )
            rut = crut.substring(0, largo - 1);
        else
            rut = crut.charAt(0);
	
        dv = crut.charAt(largo-1);
	
        if ( rut == null || dv == null )
            return 0;
	
        var dvr = '0';
        suma = 0;
        mul  = 2;
	
        for (i= rut.length-1 ; i>= 0; i--)
        {
            suma = suma + rut.charAt(i) * mul;
            if (mul == 7)
                mul = 2;
            else
                mul++;
        }
	
        res = suma % 11;
        if (res==1)
            dvr = 'k';
        else if (res==0)
            dvr = '0';
        else
        {
            dvi = 11-res;
            dvr = dvi + "";
        }
	
        if ( dvr != dv.toLowerCase() )
        {
            alert('El Rut Ingreso es Invalido')
            Objeto.focus()
            return false;
        }
        //alert('El Rut Ingresado es Correcto!')
        Objeto.focus()
        return true;
    }
}

function isRut(rut){
    var regExp = /^\d{1,2}[\.|\s|\S]{0,1}\d{3}[\.|\s|\S]{0,1}\d{3}[-|\s|\S]{0,1}[0-9kK]{1}$/;
    var match = rut.match(regExp);
    if(match == null){
        return false;
    }else{
        return true;
    }
}


// paso entre select
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
function addOption(theSel, theText, theValue)
{
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{	
    var selLength = theSel.length;
    if(selLength>0)
    {
        theSel.options[theIndex] = null;
    }
}

function moveOptions(theSelFrom, theSelTo)
{
	
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
    var i;
    for(i=selLength-1; i>=0; i--)
    {
        if(theSelFrom.options[i].selected)
        {
            selectedText[selectedCount] = theSelFrom.options[i].text;
            selectedValues[selectedCount] = theSelFrom.options[i].value;
            deleteOption(theSelFrom, i);
            selectedCount++;
        }
    }
    for(i=selectedCount-1; i>=0; i--)
    {
        addOption(theSelTo, selectedText[i], selectedValues[i]);
    }
    if(NS4) history.go(0);
}

function selectAll(selectBox,selectAll) {
    // have we been passed an ID
    if (typeof selectBox == "string") {
        selectBox = document.getElementById(selectBox);
    }
    // is the select box a multiple select box?
    if (selectBox.type == "select-multiple") {
        for (var i = 0; i < selectBox.options.length; i++) {
            selectBox.options[i].selected = selectAll;
        }
    }
}
