﻿function alertAjax(nMsg) {
    peticionAjax("alert(_)" + nMsg, alertAjaxVuelta);
}

function alertAjaxVuelta(res, context) {
    alert(res);
}

function esAlfaNumerico(campo) {
    var esAlfaNum = true;
    var i = 0;
    for (i = 0; i < campo.length && esAlfaNum; i++) {
        var c = campo.substring(i, i + 1);
        if (!esDigito(c) && !esLetra(c)) {
            esAlfaNum = false;
        }
    }
    return esAlfaNum;    
}

function esCampoObligatorio(idCtl, nError) {
    var esValido = false;
    if (document.getElementById(idCtl).value.length > 0) {
        esValido = true;
    }
    if (!esValido) {
        alertAjax(nError);
    }
    return esValido;
}

function esDigito(c) {   
    return ((c >= "0") && (c <= "9"))
}

function esLenCampoValido(nomTxt, lenTxt, nError) {
    var esValido = false;
    if (document.getElementById(nomTxt).value.length == lenTxt) {
        esValido = true;
    }
    if (!esValido) {
        alertAjax(nError);
    }
    return esValido;
}

function esLetra(c) {
    return (((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z"))) 
}

function esMailValido(mail) {  
  var valido = true;
  var posArroba = mail.indexOf("@");
  var len = mail.length;
  if (posArroba > 0 && posArroba < len - 5) {
    if (mail.indexOf("@", posArroba + 1) > 0) {
      valido = false;
    }
    if (valido) {
      posUltPunt = mail.lastIndexOf(".");
      posPriPunt = mail.indexOf(".");      
      if (posUltPunt > len - 3 || posPriPunt == 0 || posPriPunt == -1) {
        valido = false;
      } else {
        if (mail.indexOf("..") >= 0 || mail.indexOf("@.") >= 0 || mail.indexOf(".@") >= 0) {
          valido = false;
        }
      }    
    }
  } else {
    valido = false;
  }  
  return valido;
}

function getCtl(idCtl) {
	return document.getElementById(idCtl);
}

function getValoresForm(nomsCtl, separador) {    
    var ctl = nomsCtl.split(separador);
    var resul = "";
    var nCtl = ctl.length;
    for (var i = 0; i < nCtl; i++) {
        var tipo = ctl[i].substring(0, 3);
        var control = document.getElementById(ctl[i]);
        if (control != null) {
            if (tipo == "sel" || tipo == "txt" || tipo == "hid" || tipo == "tar") {
                resul += control.value;        
            } else if (tipo == "chk") {
                if (control.checked) {
                    resul += "true";
                } else {
                    resul += "false";
                }                
            } else if (tipo == "spa" || tipo == "div") {
					resul += control.innerHTML;
            }
            if (i < nCtl - 1) {
                resul += separador;
            }
        }
    }      
    return resul;
}

function peticionAjax(parametros, funcVuelta) {
    WebForm_DoCallback("__Page", "main" + sepPar + parametros, funcVuelta, null, expirarSesion, true);
}

function ponValoresForm(nomsCtl, valsCtl, separador) {
    var ctl = nomsCtl.split(separador);
    var val = valsCtl.split(separador);
    var resul = "";
    var nCtl = ctl.length;
    for (var i = 0; i < nCtl; i++) {
        var tipo = ctl[i].substring(0, 3);
        var control = document.getElementById(ctl[i]);        
        if (control != null) {
            if (tipo == "txt" || tipo == "hid" || tipo == "tar") {
                control.value = val[i];       
            } else if (tipo == "sel") {
                control.options.selectedIndex = parseInt(val[i]);
            } else if (tipo == "chk") {
                control.checked = pasaABool(val[i]); 
            } else if (tipo == "img") {
                control.src = val[i];
            } else if (tipo == "spa" || tipo == "div") {
					control.innerHTML = val[i];
            }           
        }
    }        
}

function resetValoresForm(nomsCtl, separador) {
    var ctl = nomsCtl.split(separador);    
    var resul = "";
    var nCtl = ctl.length;
    for (var i = 0; i < nCtl; i++) {
        var tipo = ctl[i].substring(0, 3);
        var control = document.getElementById(ctl[i]);        
        if (control != null) {
            if (tipo == "txt" || tipo == "hid" || tipo == "tar") {
                control.value = "";       
            } else if (tipo == "sel") {
                control.options.selectedIndex = 0;
            } else if (tipo == "chk") {
                control.checked = false; 
            } else if (tipo == "img") {
                control.src = "img/sinImagen.gif";
            }            
        }
    }        
}

function soloAlfaNumericos(evt) {				
  // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = window.event ? evt.keyCode : evt.which; 
	return (key <= 13 || (key >= 48 && key <= 57) || (key >= 65 && key <=90) || (key >= 97 && key <= 122));
}

function soloTextoMail(evt) {
  // NOTA: Backspace=8, Enter=13, '0'=48, '9'=57, 'A'=65, 'Z'=90, 'a'=97, 'z'=122, '.'=46, '_'=95, '@'=64, '-'=45
  var key = window.event ? evt.keyCode : evt.which;  
  return ((key >= 48 && key <= 57)|| (key >= 65 && key <=90) || (key >= 97 && key <= 122) || key == 46 || key == 95 || 
    key == 64 || key == 45);
}

function validaAlfaNumerico(id, nError) {
    var esValido = false;
    if (esAlfaNumerico(document.getElementById(id).value)) {
        esValido = true;
    }
    if (!esValido) {
        alertAjax(nError);
    }
    return esValido;
} 

function validaEmail(id, nError) {
    var esValido = false;
    if (esMailValido(document.getElementById(id).value)) {
        esValido = true;
    }
    if (!esValido) {
        alertAjax(nError);
    }
    return esValido;    
} 

