// JavaScript Document

function cellOver(color, id){
	document.getElementById('cell'+id).style.cursor = 'hand';
	document.getElementById('cell'+id).bgColor = color;		
	}
	
function cellOut(color, id){
	document.getElementById('cell'+id).style.cursor = 'hand';
	document.getElementById('cell'+id).bgColor = color;		
	}	
	
function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
	}	
	
function resizeFrame() {
	hg = parent.document.getElementById('opFrame').contentWindow.document.body.scrollHeight;
	if (hg < 400) {
		hg = 400;
	}
	parent.document.getElementById('opFrame').height = hg; 
	setTimeout ('clearMsg_()', 5000);	
	}
	
function msg(txt) {
	txt_ = "<div class='msgLbl_' align='center'>"+txt+"</div>";
	document.getElementById('alertDiv').innerHTML = txt_
	if (txt != "") {
		setTimeout ('clearMsg()', 5000);
		}
	}

function clearMsg() {
	document.getElementById('alertDiv').innerHTML = " ";	
	}

function clearDynaMsg() {
	document.getElementById('dynaBox').innerHTML = " ";
	document.getElementById('dynaBox').className = "";
}

function clearMsg_() {
	if(document.getElementById('dynaBox')) {
		document.getElementById('dynaBox').innerHTML = " ";	
		}	
	}
	
function validateForm(formDesc) {
	var formObj = 'document.'+formDesc.name;		
	arrayLen = formElement.length;
	//validation subroutine
	for (a = 0; a < arrayLen; a++) {
		if (eval(formObj+'.'+formElement[a]) && !document.getElementById(formElement[a]).disabled) {
			if (eval(formObj+'.'+formElement[a]).name.indexOf("mail") > -1){
				 if (validateEmail(eval(formObj+'.'+formElement[a]).value) == false) {
				 	return false;
				 	}
				}
			if (!eval(formObj+'.'+formElement[a]).value){
				alert(alertMsg[a]);
				eval(formObj+'.'+formElement[a]).focus();
				return false;				
				}
			}
		}
	//submitting data
	eval(formObj).submit();
	}

function validateEmail(email){
	var error;
	error = false;
	posicion = email.indexOf("@");
	switch (posicion) {
		case -1:
			error = true;
		case 0:
			error = true;
		case email.length-1:
			error = true;
		default:
			email = email.substr(posicion+1);
			posicion = email.indexOf("@");
			if (posicion>-1) {
				error = true;
				}
		posicion=email.indexOf(".");
		if (posicion==-1)
		{
//			window.alert("La parte del servidor debe contener un carácter ''.''");
			error = true;
		}
		if (posicion==0)
		{
//			window.alert("El carácter ''.'' no puede estar al principio de la parte del servidor");
			error = true;
		}
		while (posicion>-1)
		{				
			if (posicion==email.length-1)
			{
//				window.alert("El carácter ''.'' no puede estar al final de la parte del servidor");
				error = true;
			}
			email=email.substr(posicion+1);
			posicion=email.indexOf(".");
		}
	}
	if (error == true){ 
		alert("Debe de proveer una dirección de correo electrónico válida.")
		return false;
		}
		else return true;
		}
	


function setArt(obj, id) {
	var ash_ = ""
	if (obj.checked == true) {
		parent.document.getElementById('nl_art').value = parent.document.getElementById('nl_art').value+'@'+obj.value;
		} else {
		ash = parent.document.getElementById('nl_art').value.split('@');
		for (a = 1; a < ash.length; a++) {
				if (ash[a] != obj.value && ash[1] != null) {
					ash_ = ash_+'@'+ash[a];
					}
				}
	
		parent.document.getElementById('nl_art').value = ash_;
		}			
	}

function statusreport1() {
//status='offsetX : '+event.offsetX+', offsetY : '+event.offsetY;
	document.getElementById('x1Abs').value = event.clientX;
	document.getElementById('y1Abs').value = event.clientY;
	
	document.getElementById('x1Rel').value = event.offsetX;
	document.getElementById('y1Rel').value = event.offsetY;
	
	document.getElementById('Layer3').style.top = document.getElementById('y1Abs').value;
	document.getElementById('Layer3').style.left = document.getElementById('x1Abs').value;
	document.getElementById('Layer3').style.width = 0;
	document.getElementById('Layer3').style.height = 0;
	}

function statusreport2() {	
	factor = document.getElementById('fct').value;
	
	document.getElementById('x2Abs').value = event.clientX;
	document.getElementById('y2Abs').value = event.clientY;
	
	document.getElementById('x2Rel').value = event.offsetX;
	document.getElementById('y2Rel').value = event.offsetY;
	
	document.getElementById('Layer3').style.top = document.getElementById('y1Abs').value;
	document.getElementById('Layer3').style.left = document.getElementById('x1Abs').value;
	
	w = document.getElementById('x2Abs').value-document.getElementById('x1Abs').value
	h = document.getElementById('y2Abs').value-document.getElementById('y1Abs').value
	
	len = w
	if (len < h) {
		len = h;
		}
	
	if (len < 100*factor) {
		len = 100*factor
		}
	
	document.getElementById('Layer3').style.width = len;
	document.getElementById('Layer3').style.height = len;
	}

function setArea() {
	len = event.clientX-document.getElementById('x1Abs').value
	
	document.getElementById('Layer3').style.width = len;
	document.getElementById('Layer3').style.height = len;
	}

function setCursor(obj) {
	obj.style.cursor='crosshair'
	}

function switchImg(obj) {
	var maxW = 600
	var maxH = 400
	var img = new Image()
	img.src = obj.value;
	w = img.width
	h = img.height	
	factor = 1
	
	if( w > maxW || h > maxH){
		if (w > h) {
			factor = maxW/w
			} else {
			factor = maxH/h
			}		
		}

	w = w*factor
	h = h*factor

	document.getElementById('fct').value = factor
	//document.getElementById('pic').src = img.src
	//document.getElementById('pic').width = w
	//document.getElementById('pic').height = h
	
	document.getElementById('Layer3').style.width = 0;
	document.getElementById('Layer3').style.height = 0;
	document.getElementById('Layer3').style.top = 0;
	document.getElementById('Layer3').style.left = 0;
	}

//function addItem(obj, lyr, fld) {
//	if (obj.value == "0") {
//		document.getElementById(lyr).innerHTML = "<input name='"+fld+"' type='text' class='textBox' id='"+fld+"' />";	
//		} else {
//		document.getElementById(lyr).innerHTML = "";
//		}
//	}



	
function getPage(url, cont) {
	
	var xmlhttp=false;
	
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
				xmlhttp = false;
				}
			}
					
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
		}
		
		xmlhttp.open("GET", url, true);
		
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
				
			Element.setInnerHTML(cont, "<strong>reciviendo datos...</strong>");		
				if(xmlhttp.status==200){
					Element.setInnerHTML(cont, xmlhttp.responseText);
					}
				}
			}
		xmlhttp.send(null);		
	}
function setToken(fld, obj) {
	document.getElementById(fld).value = MD5(obj.value);	
}

function getToken(obj, targetFld) {
	var xmlhttp=false;
	
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
				xmlhttp = false;
				}
			}
				
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
		}		
		xmlhttp.open("GET", 'getToken.asp?txt='+obj.value,true);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
			//Element.setInnerHTML(cont, "<br><div align='center'><img src='../bina/"+imgPath+"' /></div><br>");		
				if(xmlhttp.status==200){
					//Element.setInnerHTML(cont, xmlhttp.responseText);
					document.getElementById(targetFld).value =  xmlhttp.responseText
					}
				}
			}
		xmlhttp.send(null);
	}

function sendCom(id) {
	
	document.getElementById('buffer').value = document.getElementById('comunicado').innerHTML;
	
		
		
	var xmlhttp=false;
	
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}catch(E){
				xmlhttp = false;
				}
			}
				
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
		}		
		xmlhttp.open("GET", 'sendCom.asp?id='+id,true);
		xmlhttp.onreadystatechange=function(){
			if(xmlhttp.readyState==4){
			document.getElementById('comunicado').innerHTML = "<strong>ENVIANDO...</strong>"		
				if(xmlhttp.status==200){
					//Element.setInnerHTML(cont, xmlhttp.responseText);
					//document.getElementById(targetFld).value =  xmlhttp.responseText
					document.getElementById('comunicado').innerHTML = document.getElementById('buffer').value;
					alert('El comunicado ha sido enviado con éxito.');
					}
				}
			}
		xmlhttp.send(null);
	}

function validateExt(obj, ext) {
	filename = obj.value.toLowerCase();
	extAsh = ext.split("/")
	flag = 0

	for (a = 0; a < extAsh.length; a++) {
		if (filename.indexOf('.'+extAsh[a]) > 0) {
			flag = 1;
			}
		}
	
	if (flag == 0) {
		alert('Solo archivos con extensiones '+ext+' son permitidos');
		obj.value = "";
		}
 	
}

function esDigito(sChr){
var sCod = sChr.charCodeAt(0);
return ((sCod > 47) && (sCod < 58));
}
function valSep(oTxt){
var bOk = false;
bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
return bOk;
}
function finMes(oTxt){
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
var nRes = 0;
switch (nMes){
case 1: nRes = 31; break;
case 2: nRes = 29; break;
case 3: nRes = 31; break;
case 4: nRes = 30; break;
case 5: nRes = 31; break;
case 6: nRes = 30; break;
case 7: nRes = 31; break;
case 8: nRes = 31; break;
case 9: nRes = 30; break;
case 10: nRes = 31; break;
case 11: nRes = 30; break;
case 12: nRes = 31; break;
}
return nRes;
}
function valDia(oTxt){
var bOk = false;
var nDia = parseInt(oTxt.value.substr(0, 2), 10);
bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
return bOk;
}
function valMes(oTxt){
var bOk = false;
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
bOk = bOk || ((nMes >= 1) && (nMes <= 12));
return bOk;
}
function valAno(oTxt){
var bOk = true;
var nAno = oTxt.value.substr(6);
bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
if (bOk){
for (var i = 0; i < nAno.length; i++){
bOk = bOk && esDigito(nAno.charAt(i));
}
}
return bOk;
}
function valFecha(oTxt){
var bOk = true;
if (oTxt.value != ""){
bOk = bOk && (valAno(oTxt));
bOk = bOk && (valMes(oTxt));
bOk = bOk && (valDia(oTxt));
bOk = bOk && (valSep(oTxt));
if (!bOk){
alert("Fecha inválida");
oTxt.value = "";
oTxt.focus();
}
}
}


function Comparar_Fecha(Obj1,Obj2) 
{
String1 = Obj1;
String2 = Obj2;
// Si los dias y los meses llegan con un valor menor que 10 
// Se concatena un 0 a cada valor dentro del string 
if (String1.substring(1,2)=="/") {
String1="0"+String1
}
if (String1.substring(4,5)=="/"){
String1=String1.substring(0,3)+"0"+String1.substring(3,9)
}

if (String2.substring(1,2)=="/") {
String2="0"+String2
}
if (String2.substring(4,5)=="/"){
String2=String2.substring(0,3)+"0"+String2.substring(3,9)
}

dia1=String1.substring(0,2);
mes1=String1.substring(3,5);
anyo1=String1.substring(6,10);
dia2=String2.substring(0,2);
mes2=String2.substring(3,5);
anyo2=String2.substring(6,10);


if (dia1 == "08") // parseInt("08") == 10 base octogonal
dia1 = "8";
if (dia1 == '09') // parseInt("09") == 11 base octogonal
dia1 = "9";
if (mes1 == "08") // parseInt("08") == 10 base octogonal
mes1 = "8";
if (mes1 == "09") // parseInt("09") == 11 base octogonal
mes1 = "9";
if (dia2 == "08") // parseInt("08") == 10 base octogonal
dia2 = "8";
if (dia2 == '09') // parseInt("09") == 11 base octogonal
dia2 = "9";
if (mes2 == "08") // parseInt("08") == 10 base octogonal
mes2 = "8";
if (mes2 == "09") // parseInt("09") == 11 base octogonal
mes2 = "9";

dia1=parseInt(dia1);
dia2=parseInt(dia2);
mes1=parseInt(mes1);
mes2=parseInt(mes2);
anyo1=parseInt(anyo1);
anyo2=parseInt(anyo2);

if (anyo1>anyo2)
{
return false;
}

if ((anyo1==anyo2) && (mes1>mes2))
{
return false;
}
if ((anyo1==anyo2) && (mes1==mes2) && (dia1>=dia2))
{
return false;
} 

return true;
}



function nif(dni) {
  numero = dni.substr(0,dni.length-1);
  let = dni.substr(dni.length-1,1);
  numero = numero % 23;
  letra='TRWAGMYFPDXBNJZSQVHLCKET';
  letra=letra.substring(numero,numero+1);
  if (letra!=let) {
    alert('El DNI introducido es erroneo.')
	return false;
  }
  else return true;
}

function ValidarTJ(numero_tarjeta,tipo) {
 var cadena = numero_tarjeta.toString();
 var longitud = cadena.length;
 var cifra = null;
 var cifra_cad=null;
 var msg=null;
 if (numero_tarjeta=='1111222233334444') {
	 
	 
 }
 else if (tipo=='Master Card') {
	if (!numero_tarjeta.match(/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/))
		msg = "No es un número de Visa correcto";
 }
 else if (tipo=='Visa') {
	if (!numero_tarjeta.match(/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/))
 		msg = "No es un número de Visa correcto";
 }
 
 if (msg==null) {
	 var suma=0;
	 for (var i=0; i < longitud; i+=2){
	   cifra = parseInt(cadena.charAt(i))*2;
	   if (cifra > 9){ 
		 cifra_cad = cifra.toString();
		 cifra = parseInt(cifra_cad.charAt(0)) + 
	parseInt(cifra_cad.charAt(1));
	   }
	   suma+=cifra;
	 }
	 for (var i=1; i < longitud; i+=2){
	   suma += parseInt(cadena.charAt(i));
	 }
		
	 if ((suma % 10) == 0){ 
	  return true;
	 } else {
	  alert("El número de tarjeta no es válido");
	  return false;
	 }
	 }
else {
	//alert(msg);
	alert("El número de tarjeta no es válido");
  	return false;
}
 
}


function getPageImage(url, cont) {



	var xmlhttp=false;	
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
			xmlhttp = false;
		}
	}
						
	if(!xmlhttp && typeof XMLHttpRequest!='undefined'){
		xmlhttp = new XMLHttpRequest();
	}
		
	xmlhttp.open("GET", url, true);
	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			document.getElementById(cont).innerHTML = "";		
			if(xmlhttp.status==200){
				document.getElementById(cont).innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null);		
	


}

	function IsNumeric(sText)
	{
   var ValidChars = "0123456789,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   
   }
   
   
   
function filtrar(x)
{
var y=document.getElementById(x).value;
if (y=='ñ' ) {
	document.getElementById(x).value='';
}
else {
	document.getElementById(x).value=y;
}
}

function upperCase(x)
{
var y=document.getElementById(x).value;
document.getElementById(x).value=y.toUpperCase();
}