function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      	s = s.substring(0, i+1);
   }

   return s;
}
function Trim(str)
{
   return RTrim(LTrim(str));
}


var nav4 = window.Event ? true : false;
function acceptNum(evt){	
var key = nav4 ? evt.which : evt.keyCode;	
return (key <= 13 || key == 44 || (key >= 48 && key <= 57));
}

function ValidaFecha (dia,mes,ano,oblig){
	Error=true;
	if (oblig==true){
		if (dia=="" || mes=="" || ano=="")
			Error=false;
	}
	if ((mes>12) || (dia>31))
			{Error=false;}
	if (((mes==4)||(mes==6)||(mes==9)||(mes==11)) && (dia==31)){
			Error=false;}
	if ((mes==2) && (dia>28)){
			Error=false;}
	resto=ano%4;
	
	if ((resto==0) && (mes==2) && (dia==29)){
			Error=true;}
	if (ano<1880)
		{Error=false;}
	return Error;
}

function ValidaHora (hora,minuto,oblig){
	Error=true;
	if (oblig==true){
		if (hora=="" || minuto=="")
			{Error=false;}
	}
	if (hora>25 || minuto >60)
		{Error=false;}
	return Error;
}

function validarmail(pass)
{
    /*if (pass)
    {
       var tevaloresults = true;
       var index = 0;
       var filter=/^.+@.+\..{2,3}$/
       var filter2=/\.\./
       var rejected = false;
       var rejectedDomain=new Array();
       rejectedDomain[index]="";
       if (filter.test(pass))
       {
	    	var tempstring = pass.split("@");
            tempstring = tempstring[1].split(".")
            for ( i = 0; i < rejectedDomain.length; i++) 
              if (tempstring[0]==rejectedDomain[i])
                rejected=true
            if (rejected)
            {
              return false
            }
            if ( filter2.test(pass) )
              return false;
       }
       else
         return false;
   }
   else
     return false;
  return true;*/
  
  
    	var re = new RegExp();
    	re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    	if (!re.test(pass))
		{
			return false;
		}
    	else
		{
			return true;
		}
  
  
  
}

function dateDiff(p_Interval, p_Date1, p_Date2, p_firstdayofweek, p_firstweekofyear){
	if(!isDate(p_Date1)){return "invalid date: '" + p_Date1 + "'";}
	if(!isDate(p_Date2)){return "invalid date: '" + p_Date2 + "'";}

	var dt1 = new Date(p_Date1);
	var dt2 = new Date(p_Date2);

	//get ms between dates (UTC) and make into "difference" date
	var iDiffMS = dt2.valueOf() - dt1.valueOf();
	var dtDiff = new Date(iDiffMS);

	// calc various diffs
	var nYears = dtDiff.getUTCFullYear()-1970;
	var nMonths =	dtDiff.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
	var nQuarters = parseInt(nMonths/3);
	var nWeeks = parseInt(iDiffMS/1000/60/60/24/7);
	var nDays	= parseInt(iDiffMS/1000/60/60/24);
	var nHours = parseInt(iDiffMS/1000/60/60);
	var nMinutes = parseInt(iDiffMS/1000/60);
	var nSeconds= parseInt(iDiffMS/1000);
	var nMilliseconds = iDiffMS;

	// return requested difference
	var iDiff = 0;		
	switch(p_Interval.toLowerCase()){
		case "yyyy": return nYears;
		case "q": return nQuarters;
		case "m": return nMonths;
		case "y": 		// day of year
		case "d": return nDays;
		case "w": return nDays;
		case "ww":return nWeeks;		// week of year	// <-- inaccurate, WW should count calendar weeks (# of sundays) between
		case "h": return nHours;
		case "n": return nMinutes;
		case "s": return nSeconds;
		case "ms":return nMilliseconds;	// millisecond	// <-- extension for JS, NOT available in VBScript
		default: return "invalid interval: '" + p_Interval + "'";
	}
}

function isDate(p_Expression){
	return !isNaN(new Date(p_Expression));
}


function getFileName(fName)
{
	fullName = fName;
	shortName = fullName.match(/[^\/\\]+$/);
	return shortName
}
function checkCaracteres(str)
{
	bValidar=true;
	for (i=0;i<str.length;i++)
	{
		if (!((str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122) || (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) || str.charAt(i)=="." || str.charAt(i)=="-" || str.charAt(i)=="_")){
			bValidar=false;
		}
	}
	return bValidar;
}

function RUT(variable)
{
    // Primer paso: Obtener el rut que ingreso el usuario
    var rutCompleto = variable;
    // Eliminamos los caracteres raros, espacios, puntos, guiones.
    // Pasamos a minusculas, y separamos el rut y el digito verificador
    rutCompleto = rutCompleto.replace(/[ \.-]/g, "" );
    rutCompleto = rutCompleto.toLowerCase();
    var dv = rutCompleto.substring(rutCompleto.length - 1);
    var rut = rutCompleto.substring(0, rutCompleto.length - 1)
    

    var valido = true;
    // Primero comprobamos que el dv sea o un digito o una k
    valido = valido && dv.match(/^[\dk]$/);
    // luego vemos que el rut solo contenga digitos
    valido = valido && rut.match(/^\d+$/);
    // y por ultimo aplicamos la regla de calculo del DV
    valido = valido && verificarDV(rut, dv);

    if (valido){
        //document.frm.txtNroDoc.value = rut + "-" + dv;
        return true;
    }
    else{
        return false;
    }
}

function verificarDV(rut, dv)
{
    var multiplicador = 9;
    var aux = rut;
    var suma = 0;
    while(aux > 0){
        var unidades = aux % 10 ;
        aux = (aux - unidades) / 10;
        suma += (multiplicador * unidades);
        multiplicador--;
        if (multiplicador < 4){
            multiplicador = 9;
        }
    }
    digito = suma % 11;
    if (digito == 10){
        digito = "k";
    }
    if (dv == digito){
        return true;
    }
    else {
        return false;
    }
} 

function validoCPF(cpf)
{
	if(cpf.length == 11)
	{
		if(isNaN(cpf))
			return false;
		else
			return true;
	}
	else
		return false;
}

function validoRG(rg)
{
	if(rg.length == 9)
	{
		if(isNaN(rg))
			return false;
		else
			return true;
	}
	else
		return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validarTNumber(pTexto)
{	
        //var txt=new RegExp(pattern,attributes);
	var patron= new RegExp("[a-z]{2}[0-9]{4}$","i");
	if (pTexto.match(patron))
	    return true; 
	else
	    return false;
}