/*HSN devuelve un objeto fecha. la entrada es del tipo DD-MM-YYYY HH:MM Ej '10-01-2005 23:20' */
function ConvertToDateTime(FechaCompleta)
{
             Ttiempo = (FechaCompleta.split(" "))[1].split(":")
             Thora = Ttiempo[0]
             Tminuto = Ttiempo[1]
             Tfecha = (FechaCompleta.split(" "))[0].split("-")
             Tdia = Tfecha[0]
             Tmes =  Tfecha[1] - 1
             Tano = Tfecha[2]
             return (new Date( Tano,Tmes, Tdia,Thora,Tminuto,0));
}



/*HSN Valida fechas del tipo DD-MM-YYYY HH:MM Ej '10-01-2005 23:20'*/
function ValidarFechaConHoraCorta(fecha){
op =  fecha 
var fecha2;
var Validador =  /\d{2}-\d{2}-\d{4} \d{2}:\d{2}/;         
        m = Validador.test(op)
        if (m){
                            tiempo = (op.split(" "))[1].split(":")
                            //hora = parseInt(tiempo[0])
                            hora = tiempo[0]
                            //minuto = parseInt(tiempo[1])
                            minuto = tiempo[1]
                            fecha = (op.split(" "))[0].split("-")
                            //dia = parseInt(fecha[0])
                            dia = fecha[0]
                            mes =  fecha[1] - 1
                            ano = parseInt( fecha[2])
                            var date = new Date( ano,mes, dia,hora,minuto,0);
                          //  alert(date)
                            ano2 = date.getFullYear()
                            if (date.getMonth() + 1 < 10){
                            mes2 = (("0" + (date.getMonth() + 1) ).toString())
                            }else{
                            mes2 = date.getMonth() + 1
                            }
                            dia2=""
                            if (date.getDate() <10) {
                          
                                                    dia2 = (("0" + ((date.getDate()).toString() ) ).toString())
                            }else{
                                                    dia2 =  date.getDate().toString() 
                            }
                            //alert(dia2)
                            if (date.getHours() < 10 ){
                            hora2 = ("0" + (date.getHours()  ).toString())
                            }else{
                            hora2 = (date.getHours())  
                            }

                                if (date.getMinutes() < 10 ){
                               
                            minuto2 = ("0" + (date.getMinutes()  ).toString())
                            }else{
                            minuto2 = date.getMinutes()  
                            }
                            fecha2 = dia2.toString()  +"-" +  mes2.toString() + "-"  + ano2.toString()  +  " " + 

hora2.toString() + ":" +  minuto2.toString()
                          //  alert(fecha2)
                 }

//alert(fecha2)
if (fecha2 == op)     { return true};
return false
}



/* valida la hora que cumpla con el siguiente formato HH:MM*/
function ValidarHora(Valor)
{
	var Validador= /\d{2}:\d{2}/;
	m = Validador.test(Valor)
	
	if (m) {
		 tiempo = (Valor.split(":"))
		
		 hora = parseInt(tiempo[0])
		 minuto = parseInt(tiempo[1])
		
		if (hora >=0 && hora<=24)
			if (minuto >=0 && minuto<=59)
				return true 
			else 
				return false 
		else 
		return false 
	    }
return false 
}

/*
Funcion que valida el formato de la fecha,
la variable "conhora" indica si la fecha viene con hora, es una variable opcional
*/

function ValidarFecha(fecha,conhora){
op =  fecha 
var fecha2;
if (conhora) {
    var Validador =  /\d{2}-\d{2}-\d{4} \d{2}:\d{2}/;         
        m = Validador.test(op)
        if (m){
                            tiempo = (op.split(" "))[1].split(":")
                            hora = parseInt(tiempo[0])
                            minuto = parseInt(tiempo[1])
                            fecha = (op.split(" "))[0].split("-")
                            dia = parseInt(fecha[0])
                            mes =  fecha[1] - 1
                            ano = parseInt( fecha[2])
                            var date = new Date( ano,mes, dia,hora,minuto,0);
                            ano2 = date.getFullYear()
                            if (date.getMonth() + 1 < 10){
                            mes2 = (("0" + (date.getMonth() + 1) ).toString()).substr(-2,2)
                            }else{
                            mes2 = date.getMonth() + 1
                            }
                            if (date.getDate() <10) {
                                                    dia2 = (("0" + ((date.getDate() - 1).toString() ) ).toString()).substr(-2,2)
                            }else{
                                                    dia2 =  date.getDate().toString() 
                            }
                            if (date.getHours() < 10 ){
                            hora2 = ("0" + (date.getHours()  ).toString()).substr(-1,2)
                            }else{
                            hora2 = (date.getHours())  
                            }
                                if (date.getMinutes() < 10 ){
                            minuto2 = ("0" + (date.getMinutes()  ).toString()).substr(-1,2)
                            }else{
                            minuto2 = date.getMinutes()  
                            }
                            fecha2 = dia2.toString()  +"-" +  mes2.toString() + "-"  + ano2.toString()  +  " " + hora2.toString() + ":" +  minuto2.toString()
    }
}else{

    var Validador =  /\d{2}-\d{2}-\d{4}/;
        m = Validador.test(op)
    if (m ){
				fecha = op.split("-")
                dia = Number(fecha[0])
                mes = Number( fecha[1] ) - 1
                ano = Number( fecha[2])
               var date = new Date( ano,mes, dia);
                ano2 = date.getFullYear()
                if (Number(date.getMonth()) + 1 < 10){
                            mes2 = (("0" + (Number(date.getMonth()) + 1) ).toString()).substr(-2,2)
                }else{
                            mes2 = date.getMonth() + 1
                }
                if (Number(date.getDate()) <10) {
                            dia2 = (("0" + ((Number(date.getDate())).toString() ) ).toString()).substr(-2,2)
                }else{
                            dia2 =  (date.getDate() ).toString() 
                }
                fecha2 = dia2.toString()  +"-" +  mes2.toString() + "-"  + ano2.toString()  
}
}

if (fecha2 == op)     { return true};
return false
}




function CompararFechas(strFecha1,strFecha2)
{
			//Compara fecha con formato 29-05-2004
			
			if  (getDate(strFecha1) > getDate(strFecha2)	)
				return 1
			else if (getDate(strFecha1) < getDate(strFecha2))
				return -1
			else
				return 0;		
}
function CompararFechasNoEstricto(strFecha1,strFecha2)
{
			//Compara fecha con formato 29-05-2004
			
			if  (getDate(strFecha1) >= getDate(strFecha2)	)
				return 1
			else if (getDate(strFecha1) < getDate(strFecha2))
				return -1
			else
				return 0;		
}

function getDate(strFecha){
            fecha = strFecha.split("/") 
            fecha = strFecha.split("-") 
            dia = Number(fecha[0])
            mes = Number(fecha[1]) - 1
            ano = Number(fecha[2])
            return (new Date(ano,mes,dia));
}

function DateDiff(strFecha1, strFecha2){
    return (getDate(strFecha2) - getDate(strFecha1))/(24*60*60*1000);
}

function Limpiar(obj){
	obj = ''
}


function checkDV(crut){
 largo = crut.length;
  if(largo < 2){   
     return false;
  }
  if(largo > 2){
    rut = crut.substring(0, largo - 1);
  }
  else{
    rut = crut.charAt(0);
  }
  dv = crut.charAt(largo-1);
  if(!checkCDV(dv))
     return false;
  if(rut == null || dv == null){
      return false;
  }
  var dvr = '0';
  var suma = 0;
  var 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()){   
    return false;
  }
  return true;
}

function checkCDV(dvr){
  dv = dvr + "";
  if(dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K'){
    //alert("Debe ingresar un dígito verificador válido.");
    return false;
  }
  return true;
}

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;
}
//Conocida Función IsNumeric, Valida que el parámetro de entrada sea un número.

/*function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber = true;
   var Char;
    
   for (hhh = 0; hhh < sText.length && IsNumber == true; hhh++){ 
      Char = sText.charAt(hhh); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
         }
   }
   return IsNumber;
}*/


function esNumDocValido(numDoc){
	return /^[0-9]+\-[0-9]+\-[A-Za-z]{1}[0-9A-Za-z]{1}[0-9]{2}$/.test(numDoc)				
}

function mostrarAyuda(ancla){
	open("/informacion/AyudaPopup.html?a=" + ancla, "Rubros","width=500, height=400, status=no, scrollbars=yes, left=50, top=50");
}




