var WinCon, WinGest, WinLis, WinHelp, WinMain;

function FN_Now(){
	var Digital = new Date();
	var dia = Digital.getDate();
	var mes = (Digital.getMonth()+1);
	var year = Digital.getYear();
	var hours = Digital.getHours();
	var minutes = Digital.getMinutes();
	var seconds = Digital.getSeconds();
	if (dia <10) {dia = "0" + dia; }
	if (mes <10) {mes = "0" + mes; }
	if (hours > 12) {hours = hours - 12; }
	if (hours < 10) {hours = " " + hours; }
	if (hours == 0) { hours = 12; }
	if (minutes <= 9) { minutes = "0" + minutes; }
	if (seconds <= 9) { seconds = "0" + seconds; }
	return dia + "/" + mes + "/" + year + " " + hours + ":" + minutes + ":" + seconds;
}
function FN_Memo2(Fran){
	if (Fran.indexOf('<br>')) {
		Fran = Fran.replace('<br>','\n');
	}
	return Fran;
}
function FN_MaxLen(Control,Long){
	var Tecla = window.event.keyCode;
	var Campo = MM_findObj(Control);
	if(Campo){
		var val = Campo.value;
		Len = val.length;
		if(Len>Long){
			window.event.keyCode = 0;
		}
	}
}
function FN_Format0(Fran, Num){
	if (isNaN(parseInt(Fran)) == true) {
		return "";
	} else {
		var aux = "";
		for (var i=0; i<Num - Fran.length; i++){aux = aux + "0";}
		return aux + Fran ;
	}
}
function FN_Coin(Fran){
	if (isNaN(Fran)) {
		return "0.00";
	} else {
		return Number(Fran).toFixed(2);
	}
}
function FN_Case(Tipo){
	var Tecla = window.event.keyCode;
	if (Tipo == 'Ucase') { // ------------ Mayúsculas
//		if(Tecla == 34 || Tecla == 39) {window.event.keyCode = 0;}
//		if (Tecla >= 97 & Tecla <= 124 || Tecla >=225 & Tecla <= 250){
//			window.event.keyCode = Tecla - 32;
//		}
	} else if (Tipo == 'Lcase') { // ------------ Mayúsculas 
		if (Tecla >= 65 & Tecla <= 90){
			window.event.keyCode = Tecla + 32;
		}
	} else if (Tipo == 'Num') { // ----------- Número
		if (Tecla < 48 || Tecla > 57){
			window.event.keyCode = 0;
		}
	} else if (Tipo == 'Int') { // ----------- Número
		if (Tecla != 45 & (Tecla < 48 || Tecla > 57)){
			window.event.keyCode = 0;
		}
	} else if (Tipo == 'Coin') { // ----------- Moneda
		if (Tecla != 44 & Tecla != 46 & (Tecla < 48 || Tecla > 57)){
			window.event.keyCode = 0;
		}
	} else {
		window.event.keyCode = 0;
	}
}
function FN_Chk(NombreCampo, SiNo){
	var Campo = eval('document.form1.' + NombreCampo);
	if (Campo){
		if(SiNo == '-1' || SiNo == 'True' || SiNo == 'Verdadero'){
			Campo.checked = true;
		} else {
			Campo.checked = false;
		}
	}
}
function FN_Select(NombreCampo,NombreMatriz,TextIndice) {
	var Matriz = eval(NombreMatriz);
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = Matriz.length;
	for (var i=0; i < Matriz.length; i++) {
		Campo.options[i].text = Matriz[i];
		Campo.options[i].value = Matriz[i];
	}
	Campo.selectedIndex = 0;
	for (var i=0;i<Matriz.length;i++) {
		if (Matriz[i] == TextIndice) {Campo.selectedIndex = i;}
	}		
}
function FN_SelectV(NombreCampo,NombreMatriz,Index) {
	var Matriz = eval(NombreMatriz);
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = Matriz.length;
	for (var i=0; i < Matriz.length; i++) {
		Campo.options[i].text = Matriz[i];
		Campo.options[i].value = Matriz[i];
	}
	Campo.selectedIndex = Index;
}
function FN_Select2(NombreCampo,NombreMatrizValor,NombreMatrizOpcion,TextOpcion) {
	var MatrizV = eval(NombreMatrizValor);
	var MatrizO = eval(NombreMatrizOpcion);
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = MatrizV.length;
	for (var i=0; i < MatrizV.length; i++) {
		Campo.options[i].text = MatrizO[i];
		Campo.options[i].value = MatrizV[i];
	}
	Campo.selectedIndex = 0;
	for (var i=0;i<MatrizO.length;i++) {
		if (MatrizO[i] == TextOpcion) {Campo.selectedIndex = i;}
	}
}
function FN_Select2V(NombreCampo,NombreMatrizValor,NombreMatrizOpcion,ValIndice) {
	var MatrizV = eval(NombreMatrizValor);
	var MatrizO = eval(NombreMatrizOpcion);
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = MatrizV.length;
	for (var i=0; i < MatrizV.length; i++) {
		Campo.options[i].text = MatrizO[i];
		Campo.options[i].value = MatrizV[i];
		
	}
	Campo.selectedIndex = 0;
	for (var i=0;i<MatrizV.length;i++) {
		if (Campo.options[i].value == ValIndice) {
			Campo.options[i].selected = true;
			i = MatrizV.length;
		}
	}		
}
function FN_MultiSelect(NombreCampo,NombreMatriz,TextIndice) {
	var Matriz = eval(NombreMatriz), Indice = '',Sw = false;
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = Matriz.length;
	for (var i=0; i < Matriz.length; i++) {
		Campo.options[i].text = Matriz[i];
		Campo.options[i].value = Matriz[i];
	}
	//Campo.selectedIndex = 0;
	//alert(TextIndice);
	while (TextIndice != ''){
		if (TextIndice.indexOf(",") !=-1){
			Indice = TextIndice.substring(0,TextIndice.indexOf(","));
			TextIndice = TextIndice.substring(TextIndice.indexOf(",")+1,TextIndice.length);
		} else {
			Indice = TextIndice;
			TextIndice = '';
		}
		//alert(Indice);
		for (var i=0;i<Matriz.length;i++) {
			if (Matriz[i] ==Indice) {Campo.options[i].selected = true; Sw = true;}
		}
	}
	if (Sw == false) {Campo.selectedIndex = 0;}
}
function FN_MultiSelect2V(NombreCampo, NombreMatrizValor, NombreMatrizOpcion, ValOpcion) {
	var MatrizV = eval(NombreMatrizValor), Indice = '',Sw = false;
	var MatrizO = eval(NombreMatrizOpcion);
	var Campo = eval('document.form1.' + NombreCampo);
	Campo.length = MatrizV.length;
	for (var i=0; i < MatrizV.length; i++) {
		Campo.options[i].text = MatrizO[i];
		Campo.options[i].value = MatrizV[i];
	}
	while (ValOpcion != ''){
		if (ValOpcion.indexOf(",") !=-1){
			Indice = ValOpcion.substring(0,ValOpcion.indexOf(","));
			ValOpcion = ValOpcion.substring(ValOpcion.indexOf(",")+1,ValOpcion.length);
		} else {
			Indice = ValOpcion;
			ValOpcion = '';
		}
		//alert(Indice);
		for (var i=0;i<MatrizV.length;i++) {
			if (MatrizV[i] == Indice) {Campo.options[i].selected = true; Sw = true;}
		}
	}
	if (Sw == false) {Campo.selectedIndex = 0;}
}
function FN_Help(Msg){
	document.form1.Help.value = Msg;
}
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&&id.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
		
function FN_ValForm() { //v4.0
	var i,p,q,nm,test,test2,num,min,max,errors='',args=FN_ValForm.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		test=args[i+2]; test2=args[i+1]; val=MM_findObj(args[i]);
    	if (val) {
			nm=val.id;	if (nm == '' || !nm) {nm = val.name;}
			if (val.value!="") {
				if(test.indexOf('isDate')!=-1){
					if(FN_ValFecha(val.value) == false){
						errors+='- '+nm+' debe contener una fecha válida.\n';
					} else {
						p=test2.indexOf(':');
						if(p!=-1){
							min=test2.substring(0,p); max=test2.substring(p+1);
							var Fecha = (val.value.substr(4,4) +""+ val.value.substr(2,2)+""+val.value.substr(0,2));
							var Fmin = (min.substr(4,4) +""+ min.substr(2,2)+""+min.substr(0,2));
							var Fmax = (max.substr(4,4) +""+ max.substr(2,2)+""+max.substr(0,2));
							if(Fecha<Fmin || Fecha>Fmax){
								errors+='- '+nm+' debe estar comprendida entre '+min+' y '+max+'.\n';
							}
						}
						p=test2.indexOf('<');
						if(p!=-1){
							max=test2.substring(p+1);
							var Fecha = (val.value.substr(4,4) +""+ val.value.substr(2,2)+""+val.value.substr(0,2));
							var Fmax = (max.substr(4,4) +""+ max.substr(2,2)+""+max.substr(0,2));
							if(Fecha>Fmax){
								errors+='- '+nm+' debe ser menor que '+FN_FormatFecha(max)+'.\n';
							}
						}
						p=test2.indexOf('>');
						if(p!=-1){
							min=test2.substring(p+1);
							var Fecha = (val.value.substr(4,4) +""+ val.value.substr(2,2)+""+val.value.substr(0,2));
							var Fmin = (min.substr(4,4) +""+ min.substr(2,2)+""+min.substr(0,2));
							if(Fecha<Fmin){
								errors+='- '+nm+' debe ser mayor que '+FN_FormatFecha(min)+'.\n';
							}
						} 
					}
				} else if (test.indexOf('isPath')!=-1) {
	       			if (val.value.substr(0,1) != "/" || val.value.substr(val.value.length-1) !== "/"){
						errors+='- '+nm+' debe contener una ruta válida (/ ... /).\n';
					}
				} else if (test.indexOf('isUrl')!=-1) {
	       			if (val.value.substr(0,7) != "http://" || val.value.substr(val.value.length-1) !== "/"){
						errors+='- '+nm+' debe contener una url válida (http:// ... /).\n';
					}
				} else if (test.indexOf('isEmail')!=-1) {
	       			if (val.value.indexOf('@')<1 || val.value.indexOf('@')==(val.length-1)){
						errors+='- '+nm+' debe contener un e-mail válido.\n';
					}
				} else if (test.indexOf('isJpeg') != -1) {
					if (val.value.substr(val.value.length - 3,3).toUpperCase() != "JPG"){
						errors+='- '+nm+' debe ser formato JPG.\n';
					}
			    } else if (test.indexOf('isNum')!=-1) {
	       			if (isNaN(parseFloat(val.value))) {
						errors+='- '+nm+' debe ser un número.\n';
					} else {
						p=test2.indexOf('<');
						if(p!=-1){
							max=parseFloat(test2.substring(p+1));
							if(parseFloat(val.value)>max){
								errors+='- '+nm+' debe ser menor que '+ max +'.\n';
							}
						}
						p=test2.indexOf('>');
						if(p!=-1){
							min=parseFloat(test2.substring(p+1));
							if(parseFloat(val.value)<min){
								errors+='- '+nm+' debe ser mayor que '+ min +'.\n';
							}
						}
					}
				} else if (test.indexOf('inRange') != -1) {
					if (isNaN(parseFloat(val.value))) {
						errors+='- '+nm+' debe ser un número.\n';
					} else {
						p=test2.indexOf(':'); min=test2.substring(0,p); max=test2.substring(p+1);
						if (val.value<min || val.value>max) {
							errors+='- '+nm+' debe ser un número entre '+min+' y '+max+'.\n';
						}
					}
				}
			} else if (test.charAt(0) == 'R') {
					errors += '- '+nm+' es obligatorio.\n';
			}
		}
	}

	if (errors) {
  		alert('Se ha(n) producido la(s) siguiente(s) incidencia(s):\n\n'+errors);
		return (false);
	} else {
    	return (true);
	}
}

function FN_ValFecha(Fecha){
	var chk = 0; mDay = 0;
	
	var dd = Fecha.slice(0,2); 
	var mm = Fecha.slice(2,4);
	var yyyy = Fecha.slice(4,8);
	
	if((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)){ 
		mDay = 30;
	} else if(mm == 2) {
		mDay = isLeapYear(yyyy) ? 29 : 28;    
	} else {
		mDay = 31;
	}
			
	if((dd <= 0) || (dd > mDay)) {
		chk = 1;
	} else if ((mm <= 0) || (mm > 12)){
		chk = 1;
	} else if((yyyy <= 0)){
		chk = 1;
	}

	if(chk == 1){
		return (false);
	} else {
		return (true);
	}
}
function isLeapYear(yr){
	if (yr % 2 == 0) 
	   return (true);
	   return (false);
}
function FN_FormatFecha(Fecha){
	return (Fecha.substr(0,2)+"/"+ Fecha.substr(2,2)+"/"+Fecha.substr(4,4));
}
function FN_WinOpen(url,Ancho,Alto){
	var kk = FN_WinClose(); 
	var x = (screen.availWidth - Ancho)/2;
	var y = (screen.availHeight - Alto)/2;
	WinGest = open(url,'WinGest','toolbar=no,location=no,status=yes,menubar=no,scrollbars=auto,resizable=no,width=' + Ancho + ',height=' + Alto +',left=' + x + ',top=' + y);
}
function FN_Consulta(url,Ancho,Alto){
	var x = (screen.availWidth - Ancho)/2;
	var y = (screen.availHeight - Alto)/2;
	WinCon = open(url,'WinCon','toolbar=no,location=no,status=yes,menubar=no,scrollbars=auto,resizable=no,width=' + Ancho + ',height=' + Alto +',left=' + x + ',top=' + y);
	return WinCon.name;
}
function FN_Consulta2(url,Ancho,Alto){
	var x = (screen.availWidth - Ancho)/2;
	var y = (screen.availHeight - Alto)/2;
	WinCon2 = open(url,'WinCon2','toolbar=no,location=no,status=yes,menubar=no,scrollbars=auto,resizable=no,width=' + Ancho + ',height=' + Alto +',left=' + x + ',top=' + y);
}
function FN_WinLis(url){
	if(url == ''){url = '/Privado/Ejecutando.htm';}
	WinLis = open(url,'WinLis','toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=no,width=' + screen.availWidth + ',height=' + screen.availHeight + ',left=0,top=0');
}
function FN_WinHelp(){
	var x = (screen.availWidth - 650);
	if (WinHelp){if (WinHelp.closed != true) {WinHelp.close();}}
	Winhelp = open('/Privado/Help/Ayuda.asp','WinHelp','toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=650,height=475,top=0,left='+x);
}
function FN_WinClose(){
	if (WinCon){if (WinCon.closed != true) {WinCon.close();}}
	if (WinGest){if (WinGest.closed != true) {WinGest.close();}}
	if (WinLis){if (WinLis.closed != true) {WinLis.close();}}
	if (WinHelp){if (WinHelp.closed != true) {WinHelp.close();}}
}
function FN_Foco(){
	if (WinGest){if (WinGest.closed != true) {WinGest.focus();}}
	if (WinLis){if (WinLis.closed != true) {WinLis.focus();}}
	if (WinCon){if (WinCon.closed != true) {WinCon.focus();}}
}