
//Función para revisar si un campo es igual al valor adiciona el mensajeError al mensaje, 
//que es el que se mostrara cuando haya errores en el formulario
//comprobar navegador
var nav4 = window.Event ? true : false;

var timeCrono; 
var hor = 0;
var min = 0;
var seg = 0;
var startTime = new Date(); 
var start = startTime.getSeconds();

var campoCronno;

var cambioSelect = 0;

function desabilitarCamposReinicioReloj(valorSelect, valorForma){
	
		
	//alert("valorSelect: "+valorSelect+" valorForma: "+valorForma.name)
	var elementsForma = new Array();
	elementsForma = valorForma.elements;
	//alert(elementsForma.length)
 
	for(var i=0; i<elementsForma.length; i++ ){
		//	alert(elementsForma[i].name);
		elementsForma[i].disabled=false;
	}
	//alert("cambioSelect :"+cambioSelect)
	if(cambioSelect==0){
		//alert("entre al if del onchange()");
		//valorForma.tx_cronom.value="";
		//timeCrono = 0;
		//hor = 0;
		//min = 0;
		//seg = 0;
		//window.location.reload()
		//startTime = new Date(); 
		//start = startTime.getSeconds();
		InicializarCrono ();
	}
	  //MostrarCrono();
	  //cambioSelect++;
}



function StartCrono() {
	if (seg + 1 > 59) { 
		min+= 1 ;
	}
	if (min > 59) {
		min = 0;
		hor+= 1;
	}
	var time = new Date(); 
	if (time.getSeconds() >= start) {
		seg = time.getSeconds() - start;
	} 
	else {
		seg = 60 + (time.getSeconds() - start);
	}
	timeCrono= (hor < 10) ? "0" + hor : hor;
	timeCrono+= ((min < 10) ? ":0" : ":") + min;
	timeCrono+= ((seg < 10) ? ":0" : ":") + seg;
	campoCronno.value = timeCrono;
	setTimeout("StartCrono()",1000);
} //--> 


function validarEmail(campo, msgError) {
	if (!esEmail(campo.value)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            _campoError = campo;
        }
        return false;
	}
	return true;
}


function esEmail(str) {
  var indexLT = str.indexOf('<');
  var indexGT = str.indexOf('>');
  var email = str;
  if (indexGT > indexLT && indexGT > -1 && indexLT > -1) {
  	email = email.substring(indexLT+1, indexGT);
  }
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(email) && r2.test(email));
}

function campoTextoIgual(campo, valor, msgError, longitudMinima, longitudMaxima){
    if (!longitudMinima  && longitudMinima != 0) {
        longitudMinima = 1
    }
    if (!longitudMaxima ) {
		longitudMaxima = Number.MAX_VALUE
    }
    var value = campo.value
	value =value.replace(/[ ]+/g," ");//para varios espacios
	value = value.replace(/^ /,"");//para el primer espacio
    if (value.length < longitudMinima || value.length > longitudMaxima || value==valor){
		appendErrorMessage(msgError)
		if (_campoError == null) {
            _campoError = campo;
        }	
		return false;
	}
	return true;
}

function campoTextoDiferente(campo, valor, msgError, longitudMinima, longitudMaxima){
    if (!longitudMinima  && longitudMinima != 0) {
        longitudMinima = 1
    }
    if (!longitudMaxima ) {
		longitudMaxima = Number.MAX_VALUE
    }
    var value = campo.value
	if(value!=""){
		if (value.length < longitudMinima || value.length > longitudMaxima || value!=valor){
			appendErrorMessage(msgError)
			if (_campoError == null) {
            _campoError = campo;
        	}
			return false;
		}
	}
	return true;
}


function validarLista(lista, msgError, indiceInicial) {
    if (!indiceInicial && indiceInicial != 0) {
        indiceInicial = 1;
    }
    if (lista.selectedIndex < indiceInicial ){
		appendErrorMessage(msgError)
		if (_campoError == null) {
            _campoError = lista;
        }
        return false;
    }
    return true;
}

function llenarListaMultiple(lista,msgError){
	if(lista.length==0){
		appendErrorMessage(msgError)
		if (_campoError == null) {
            _campoError = lista;
        }
        return false;
	}else{
		for (i=0; i<lista.length;i++){
				lista[i].selected=true;
		}
		return true;
	}
}

function validarFecha(mes,dia,msgError){
	var fecha;
	if(nav4){
		fecha=new Date(104,mes-1,dia)
	}else{
		fecha=new Date(2004,mes-1,dia)
	}
	if(fecha.getMonth()!=mes- 1){
		appendErrorMessage(msgError)
		return false
	}else{
		return true
	}	
}	

function validarRangoFecha(mes,dia,mesI,diaI,mesF,diaF, msgError, msgError2){
	var retorno=true;
	retorno&=validarFecha(mes,dia,msgError)
	if(retorno){
		var fecha;
		var fechaI;
		var fechaF;
		var fechaActual=new Date();
		//var fechaActual=new Date(2005,0,12)
		

		if(nav4){	
		if(mes==1){		
			fecha=new Date(2005,mes-1,dia)
		}else{
			fecha=new Date(2004,mes-1,dia)
		}
			fechaI=new Date(2004,mesI-1,diaI)
			fechaF=new Date(2005,mesF-1,diaF)
		}else{
			fecha=new Date(2004,mes-1,dia)
			fechaI=new Date(2004,mesI-1,diaI)
			fechaF=new Date(2005,mesF-1,diaF)
		}
		if(fechaActual>fecha){
		
			if(fechaI<fecha && fecha<fechaF){
			
				return true
			}else{
				appendErrorMessage(msgError)
				return false
			}
		
		}else{
			appendErrorMessage(msgError2)
			return false;
		}	
	}else{
		appendErrorMessage(msgError)
		return false
	}

}



function validarSeleccion(campo, msgError, nroMin, nroMax, funcion) {
		if (nroMin == null)
			nroMin = 1;
		if (nroMax == null)
			nroMax = 99999999999999;
    if (!isChecked(campo, nroMin, nroMax) ||
        	(funcion ? !eval("funcion(campo)") : false)) {
        appendErrorMessage(msgError)
        if (_campoError == null) {
            if (campo.length) {
                _campoError = campo[0]
            } else {
                _campoError = campo;
            }
        }
        return false;
    }
    return true;
}

function isChecked(field, nroMin, nroMax) {
	if (!field) {
		return false;
	}
	if (nroMax == null)
		nroMax = 999999999;
	if (nroMin == null)
		nroMin = 1;
	if (field.length) {
		cont = 0;
		for (var i = 0; i < field.length; i++) {
			if (field[i].checked) {
				cont++;
			}
		}
		if (cont >= nroMin && cont <= nroMax){
			return true;
		} else {
			return false;
		}
	}
	return field.checked;
}

var _campoError = null;
var _errores = "";

function appendErrorMessage(msgError) {
    _errores += " \n- " + msgError;
}

function acceptNum(evt,campo){	

// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key <= 13 || (key >= 48 && key <= 57));
}

function noCaracter(campo, valor, e){
	return false;
}

function numberFormat(campo, valor, e){
	var key = '';
	var strCheck = '0123456789';
	
	var whichCode;
	if(window.event) {
		whichCode = e.keyCode;
	}
	else if(e.which) {
		whichCode = e.which;
	}
	
	if (whichCode == 8){
		return true;  // Enter
	}
	key = String.fromCharCode(whichCode);  // Get key value from key code
	
	if (strCheck.indexOf(key) == -1){
		return false;
	}
}

var contadorPuntos=0;
var totalPuntos = 0;

function acceptPuntoMoneda(evt,campo){
		
	 var partir =campo.value.split(".")
	 var nroPuntos = partir.length - 1
	 if(nroPuntos<contadorPuntos){
	 	contadorPuntos--;
	 }
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;
		if(key==46&&contadorPuntos<1){
			contadorPuntos++;
			//totalPuntos++;
			return (key <= 13 || (key >= 48 && key <= 57)|| key == 46);
		}else{
			
			if(key==46){
				//contadorPuntos++;
				alert("-  No puedes ingresar mas de un punto en el monto de compra");
			}
			return (key <= 13 || (key >= 48 && key <= 57));
		}
}


function acceptMoneda(evt,campo){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;	
	return (key <= 13 || (key >= 48 && key <= 57)|| key == 46);
}

function acceptTodos(evt,campo){	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 33 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) || key== 225 || key == 233 || key == 237 || key == 243 || key == 250 || key== 193 || key == 200 || key == 211 || key == 218 || key == 205 || key == 209 || key == 241 );
}

function acceptEmail(evt,campo){	
	
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
		
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 33 || (key >= 33 && key <= 64)|| (key >= 65 && key <= 96)|| (key >= 97 && key <= 125)|| key == 46 || key == 64 );
}

function changeWindowSize(x,y){
	anchoAltura = new Array(x,y)
	var _xSize=x+10;
	var _ySize=y+29;
	window.resizeTo(_xSize,_ySize);
	w = screen.availWidth;
	h = screen.availHeight;
	window.moveTo((w-x)/2, (h-y)/2);
	window.focus();
	return anchoAltura;
}

//Validacion Fecha formato dd/mm/aaaa
function validarFechaCampo( campo, valor, menError){ //Funcion que valida la fecha ingresada en un campo. 
   if (valor){  
      borrar = valor;
      if ((valor.substr(2,1) == "/") && (valor.substr(5,1) == "/")){      
         for (i=0; i<10; i++){	
            if (((valor.substr(i,1)<"0") || (valor.substr(i,1)>"9")) && (i != 2) && (i != 5)){
               borrar = '';
               break;  
			}  
         }
	     if (borrar){ 
	        a = valor.substr(6,4);
		    m = valor.substr(3,2);
		    d = valor.substr(0,2);
		    if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
		       borrar = '';
		    else{
		       if((a%4 != 0) && (m == 2) && (d > 28))	   
		          borrar = ''; // Año no viciesto y es febrero y el dia es mayor a 28
			   else{
		          if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
			         borrar = '';	      				  	 
			   }  // else
		    } // fin else
         } // if (error)
      } // if ((valor.substr(2,1) == "/") && (valor.substr(5,1) == "/"))			    			
	  else
	     borrar = '';
	  if (borrar == ''){
	      _campoError = campo;
	     appendErrorMessage(msgError);
	  }
   } // if (valor)   
} // FUNCION

function EscaparParametro(valorEsc){
	valor = valorEsc;
	if(valor) {
		while(valor.indexOf("+")>=0) {
			valor = valor.replace("+", "%2B");
		}
	}
	valor = escape(valor);		
	return valor;
}
