//Funcion que remueve los espacios en blancos a la derecha y a la izquierda 
function trim(inputString) {
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; 
} 


//Autor = Alejandro Garcia - alejandro.garcia@ingeneo.biz
//Funcion que valida que un campo sea requerido
//Parametros form = nommbre del formulario
//			 campo = nombre del campo dentro del formulario
//			 descCampo = Descripcion del campo, utilizado en los mensajes
//Retorno true si el campo fue digitado
//		  false  en caso contrario
function validaCampoRequerido(form, campo, descCampo){
    if (   (eval("document." + form + "." + campo + ".value.length") == 0)    ||    (trim(eval("document." + form + "." + campo + ".value")).length == 0)    ){
        alert("El campo " + descCampo + " es requerido.");
        eval("document." + form + "." + campo + ".focus()");
	location.href="#";
        return false;
    }
    return true;
}


//Autor = Alejandro Garcia - alejandro.garcia@ingeneo.biz
//Funcion que valida que un campo sea num�rico
//Parametros form = nommbre del formulario
//			 campo = nombre del campo dentro del formulario
//			 descCampo = Descripcion del campo, utilizado en los mensajes
//Retorno true si el campo es numerico
//		  false  en caso contrario
function validaCampoNumerico(form, campo, descCampo){
    if (isNaN(eval("document." + form + "." + campo + ".value"))){
        alert("El campo " + descCampo + " debe ser numerico.");
        eval("document." + form + "." + campo + ".focus()");
	location.href="#";
        return false;
    }
    return true;
}



//Autor = Alejandro Garcia - alejandro.garcia@ingeneo.biz
//Funcion que valida que un campo sea num�rico positivo
//Par�metros form = nommbre del formulario
//			 campo = nombre del campo dentro del formulario
//			 descCampo = Descripcion del campo, utilizado en los mensajes
//Retorno true si el campo es numerico positivo
//		  false  en caso contrario
function validaCampoNumericoPositivo(form, campo, descCampo){
    if (isNaN(eval("document." + form + "." + campo + ".value"))){
        alert("El campo " + descCampo + " debe ser num�rico.");
        eval("document." + form + "." + campo + ".focus()");
	location.href="#";
        return false;
    }
    else{
        //Valido que sea positivo
	if (parseInt(eval("document." + form + "." + campo + ".value")) < 0){
            alert("El campo " + descCampo + " debe ser positivo.");
            eval("document." + form + "." + campo + ".focus()");
	    location.href="#";
            return false;
        }
    }
    return true;
}


//Autor = Alejandro Garcia - alejandro.garcia@ingeneo.biz
//Funcion que valida que un campo no sobrepase la dimension especificada, util para los textarea
//Par�metros form = nommbre del formulario
//			 campo = nombre del campo dentro del formulario, normalmente un textArea
//			 descCampo = Descripcion del campo, utilizado en los mensajes
//Retorno true si el campo no sobrepasa la longitud
//		  false  en caso contrario
function validaLongitudCampo(form, campo, descCampo, longMaxCampo){
    if (  eval("document." + form + "." + campo + ".value.length") >  longMaxCampo ){
        alert("El campo " + descCampo + " tiene " + eval("document." + form + "." + campo + ".value.length") + " caracteres, y debe tener menos de " +  longMaxCampo + "  caracteres.");
        eval("document." + form + "." + campo + ".focus()");
	location.href="#";
        return false;
    }
    return true;
}



//Funci�n que valida que un email sea v�lido
//Retorna true si el email es valido y false en caso contrario
function valida_email(email){
    var arraySufijosEmail = new Array();
    var contieneSufijo = false;
    var sufijo;
    arraySufijosEmail[0] = ".com";
    arraySufijosEmail[1] = ".org";
    arraySufijosEmail[2] = ".edu";
    arraySufijosEmail[3] = ".pro";
    arraySufijosEmail[4] = ".biz";
    arraySufijosEmail[5] = ".nam";
    arraySufijosEmail[6] = ".gov";
    arraySufijosEmail[7] = ".mil";
    arraySufijosEmail[8] = ".net";
    
    for(i = 0; i < arraySufijosEmail.length ; i ++){
        sufijo = arraySufijosEmail[i];
        if (email.toLowerCase().indexOf(sufijo) > 4){
            contieneSufijo = true;
            i = arraySufijosEmail.length;
        }
    }
    
    if (  (email.indexOf("@")<2) || (contieneSufijo == false) ){
        location.href = "#";
        return false;
    }
    else{
        return true;
    }
}

//Funcion que permite validar si un campo tiene o no un caracter
//Par�metros form = nommbre del formulario
//			 campo = nombre del campo dentro del formulario, normalmente un textArea
//Retorno true si el campo no sobrepasa la longitud
//		  false  en caso contrario
function tieneCaracter(form, campo, caracter){
    num = eval("document." + form + "." + campo + ".value");
    if ( num.indexOf(caracter) != -1 ){
        return true;
    }
    else{
	location.href="#";
        return false;
    }
}
function salir(urlRetorno){
    top.window.close();
}

function inicio(path){
    
    location.href=path;
}

function getIndexByValue_Opcion(valor, selector) {
     for (var i = 0; i < selector.length; i++){
            var selectorOpcionValue = selector.options[i].value;
            selectorOpcionValue = selectorOpcionValue.substring(0,selectorOpcionValue.indexOf(','));
            if (selectorOpcionValue == valor) {
                return i;
           }
        }
     return 0;
}


var http_request = false;

function Generic_makeRequest(url, functionNameParameters, functionResults) {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    var urlParameters = "";
    
    if (functionNameParameters != 'null'){
        urlParameters = eval(functionNameParameters);
    }

    http_request.onreadystatechange = functionResults;
    
    if (urlParameters == ""){
        http_request.open('GET', url, true);
    }
    else{
        http_request.open('GET', url+urlParameters, true);
    }
    http_request.send(null);
}

function getIndexByValue(info, selector) {
    for (var i = 0; i <= selector.length; i++) {        
        if (selector.options[i].value == info) {
            return i;
        }
    }
    return 0;
}

