/* Los manejadores de la toolbar */
m_LstMouseOver = new Array();
m_LstMouseOut = new Array();
m_LstOnClick = new Array();
m_LstDisabledButton = new Array();
m_LstMouseOverOLD = new Array();
m_LstMouseOutOLD = new Array();
m_LstOnClickOLD = new Array();

/*
	Comprueba que ciertos+  datos esten rellenos en algunos 
	formularios
*/

function checkForm(formName)
{
	var oForm = document.forms[formName];
	var i = 0;
	for(i=0;i<oForm.elements.length;i++){
		var oElement = oForm.elements[i];
		//1.- Comprobar valores obligatorios
		if(oElement.getAttribute("isRequired") == "true"){
			if(oElement.value == ""){
				var oLabel = document.getElementById(oElement.name+"_label");
				alert("El campo '"+ trim(oLabel.textContent)+ "' es de obligada cumplimentación"); 
				return false;
			}
		}
		//Si tiene numero de validacion
		if(oElement.getAttribute("subType") == "KEY"){
				var Elem  = document.getElementById("SPAN_KEY");
				if(Elem){
					var curKey = Elem.getAttribute("kvalue");
					if(oElement.value != curKey){
						alert(msgError[31]);					
						return false;
					}
				}
				else{
						alert(msgError[31]);					
						return false;			
				}
			}		
	}
	return true;
}

var count = 0;
function OnOcultarFrame(sUbicImg)
{
	var colFrame = document.getElementById("finicio");
	var oImagen = window.f0.document.getElementById("logo_baratz");
	if( colFrame )
	{
		if( count == 0 )
			{
			oImagen.src = sUbicImg + "img_baratz_off.gif";
			oImagen.title = "Mostrar el menú";
			oImagen.id = "logo_baratz";
			colFrame.cols = "15,*";
			count = 1;
			return false;
			}
		else if( count == 1 )
			{
			oImagen.src = sUbicImg + "img_baratz_on.gif";
			oImagen.title = "Ocultar el menú";
			oImagen.id = "logo_baratz";
			colFrame.cols = "194,*";
			count = 0;
			return false;
			}
	}
	else
		alert('Error de acceso al frame padre!!!');
}

function ParserHTML(txt)
{
	//Sustituyo el & por el $ para poder enviar por URL
	/*----*/
	txt = txt.replace(/À/g, "$Agrave;"); 	
	txt = txt.replace(/Á/g, "$Aacute;");	
	txt = txt.replace(/Â/g, "$Acirc;"); 	
	txt = txt.replace(/Ã/g, "$Atilde;"); 	
	txt = txt.replace(/Ä/g, "$Auml;"); 		
	txt = txt.replace(/Å/g, "$Aring;"); 	
	txt = txt.replace(/Æ/g, "$AElig;"); 	
	txt = txt.replace(/Ç/g, "$Ccedil;"); 	
	txt = txt.replace(/È/g, "$Egrave;"); 	
	txt = txt.replace(/É/g, "$Eacute;"); 	
	txt = txt.replace(/Ê/g, "$Ecirc;"); 	
	txt = txt.replace(/Ë/g, "$Euml;"); 		
	txt = txt.replace(/Ì/g, "$Igrave;"); 	
	txt = txt.replace(/Í/g, "$Iacute;"); 	
	txt = txt.replace(/Î/g, "$Icirc;"); 	
	txt = txt.replace(/Ï/g, "$Iuml;"); 		
	txt = txt.replace(/Ð/g, "$ETH;"); 		
	txt = txt.replace(/Ñ/g, "$Ntilde;"); 	
	txt = txt.replace(/Ò/g, "$Ograve;"); 	
	txt = txt.replace(/Ó/g, "$Oacute;"); 	
	txt = txt.replace(/Ô/g, "$Ocirc;"); 	
	txt = txt.replace(/Õ/g, "$Otilde;"); 	
	txt = txt.replace(/Ö/g, "$Ouml;"); 		
	txt = txt.replace(/Ø/g, "$Oslash;"); 	
	txt = txt.replace(/Ù/g, "$Ugrave;"); 	
	txt = txt.replace(/Ú/g, "$Uacute;"); 	
	txt = txt.replace(/Û/g, "$Ucirc;"); 	
	txt = txt.replace(/Ü/g, "$Uuml;"); 		
	txt = txt.replace(/Ý/g, "$Yacute;"); 	
	txt = txt.replace(/Þ/g, "$THORN;"); 	
	txt = txt.replace(/ß/g, "$szlig;"); 	
	txt = txt.replace(/à/g, "$agrave;"); 	
	txt = txt.replace(/á/g, "$aacute;"); 	
	txt = txt.replace(/â/g, "$acirc;"); 	
	txt = txt.replace(/ã/g, "$atilde;"); 	
	txt = txt.replace(/ä/g, "$auml;"); 		
	txt = txt.replace(/å/g, "$aring;"); 	
	txt = txt.replace(/æ/g, "$aelig;"); 	
	txt = txt.replace(/ç/g, "$ccedil;"); 	
	txt = txt.replace(/è/g, "$egrave;"); 	
	txt = txt.replace(/é/g, "$eacute;"); 	
	txt = txt.replace(/ê/g, "$ecirc;"); 	
	txt = txt.replace(/ë/g, "$euml;"); 		
	txt = txt.replace(/ì/g, "$igrave;"); 	
	txt = txt.replace(/í/g, "$iacute;"); 	
	txt = txt.replace(/î/g, "$icirc;"); 	
	txt = txt.replace(/ï/g, "$iuml;"); 		
	txt = txt.replace(/ð/g, "$eth;"); 		
	txt = txt.replace(/ñ/g, "$ntilde;"); 	
	txt = txt.replace(/ò/g, "$ograve;"); 	
	txt = txt.replace(/ó/g, "$oacute;"); 	
	txt = txt.replace(/ô/g, "$ocirc;"); 	
	txt = txt.replace(/õ/g, "$otilde;"); 	
	txt = txt.replace(/ö/g, "$ouml;"); 		
	txt = txt.replace(/ø/g, "$oslash;"); 	
	txt = txt.replace(/ù/g, "$ugrave;"); 	
	txt = txt.replace(/ú/g, "$uacute;"); 	
	txt = txt.replace(/û/g, "$ucirc;"); 	
	txt = txt.replace(/ü/g, "$uuml;"); 		
	txt = txt.replace(/ý/g, "$yacute;"); 	
	txt = txt.replace(/þ/g, "$thorn;"); 	
	txt = txt.replace(/ÿ/g, "$yuml;"); 		
	txt = txt.replace(/¡/g, "$iexcl;"); 	
	txt = txt.replace(/¢/g, "$cent;"); 		
	txt = txt.replace(/£/g, "$pound;"); 	
	txt = txt.replace(/¤/g, "$curren;"); 	
	txt = txt.replace(/¥/g, "$yen;"); 		
	txt = txt.replace(/¦/g, "$brvbar;"); 	
	txt = txt.replace(/§/g, "$sect;"); 		
	txt = txt.replace(/¨/g, "$uml;"); 		
	txt = txt.replace(/©/g, "$copy;"); 		
	txt = txt.replace(/ª/g, "$ordf;"); 		
	txt = txt.replace(/«/g, "$laquo;"); 	
	txt = txt.replace(/¬/g, "$not;"); 		
	txt = txt.replace(/­/g, "$shy;"); 		
	txt = txt.replace(/®/g, "$reg;"); 		
	txt = txt.replace(/¯/g, "$macr;"); 		
	txt = txt.replace(/°/g, "$deg;"); 		
	txt = txt.replace(/±/g, "$plusmn;"); 	
	txt = txt.replace(/²/g, "$sup2;"); 		
	txt = txt.replace(/³/g, "$sup3;"); 		
	txt = txt.replace(/´/g, "$acute;"); 	
	txt = txt.replace(/µ/g, "$micro;"); 	
	txt = txt.replace(/¶/g, "$para;"); 		
	txt = txt.replace(/·/g, "$middot;"); 	
	txt = txt.replace(/¸/g, "$cedil;"); 	
	txt = txt.replace(/¹/g, "$sup1;"); 		
	txt = txt.replace(/º/g, "$ordm;"); 		
	txt = txt.replace(/»/g, "$raquo;"); 	
	txt = txt.replace(/¼/g, "$frac14;"); 	
	txt = txt.replace(/½/g, "$frac12;"); 	
	txt = txt.replace(/¾/g, "$frac34;"); 	
	txt = txt.replace(/¿/g, "$iquest;"); 	
	txt = txt.replace(/×/g, "$times;"); 	
	txt = txt.replace(/÷/g, "$divide;"); 	
	
	
	return txt;
}

function isDatable(sDate)
{
	var bRet = true;
	sDate 	 = sDate.replace(" ", "");
	for(var i = 0 ; i < sDate.length ; i++)
	{
		var cChar = sDate.charAt(i);
		if(cChar != "1" && cChar != "2" && cChar != "3" && cChar != "4"
				&& cChar != "5" && cChar != "6" && cChar != "7" && cChar != "8"
				&& cChar != "9" && cChar != "0" && cChar != "/")
				bRet = false;		
	}
	return bRet;
}

function CheckISADDates(objDate,curAnho,bGrid)
{
	if(objDate == null)
		return false;
	var sValue = objDate.value;
	var sName  = objDate.name;
	if(bGrid)
	{
		//Los nombres del grid son L1_TIXX
		var ar = sName.split("_");
		sName = ar[1];
	}
	sValue 		 = sValue.replace(" ", "");
	nLong 		 = sValue.length;	
	var nCont  = 0;
	var cChar  = "";
	var bRet 	 = true;
	
	if(sName == "TI11" || sName == "TI12" || sName == "TI18" || sName == "TI19")
	{
		if(sName == "TI11" || sName == "TI18")
		{
			if(isDatable(sValue))
			{
				switch(nLong)
				{
					case 4:			objDate.value = "01/01/" + sValue;								break;
					case 1:			objDate.value = "0" + sValue + "/01/" + curAnho;	break;
					case 2:			objDate.value = sValue + "/01/" + curAnho;				break;
					case 10:		objDate.value = sValue;														break;
					default:		objDate.value = "01/01/" + curAnho;								break;
				}
			}
			else
				bRet = false;
		}
		if(sName == "TI12" || sName == "TI19")
		{
			if(isDatable(sValue))
			{
				switch(nLong)
				{
					case 4:			objDate.value = "31/12/" + sValue;								break;
					case 1:			objDate.value = "0" + sValue + "/12/" + curAnho;	break;
					case 2:			objDate.value = sValue + "/12/" + curAnho;				break;
					case 10:		objDate.value = sValue;														break;
					default:		objDate.value = "31/12/" + curAnho;								break;
				}				
			}
			else
				bRet = false;
		}		
	}
	return bRet;
}


function checkFields(objForm){
	
	var bCorrect = false;
	if(objForm){
		var subType = objForm.getAttribute("subType");
		if(subType == "KEY"){
			var Elem  = document.getElementById("SPAN_KEY");
			if(Elem){
				var curKey = Elem.getAttribute("kvalue");
				if(objForm.value == curKey){
					bCorrect = true;
				}
				else{
					bCorrect = false;
					alert(msgError[31]);					
				}
			}
			else{
				bCorrect = false;
				alert(msgError[31]);				
			}
		}
		
		if(subType == "MAIL"){
			var mail = objForm.value;
			var filter=/^[A-Za-z0-9_]*@[A-Za-z0-9_]+.[A-Za-z0-9_.]+[A-za-z]$/;
			if (mail.length == 0 ) 
				bCorrect = true;
			else{
				if (filter.test(mail))
					bCorrect = true;
				else{
					bCorrect = false;
					alert(msgError[21]);
				}
			}
		}
		if(subType == "NICK"){
			var nick = objForm.value;
			if(nick.indexOf(" ") > 0){
				alert(msgError[23]);
				bCorrect = false;
			}
			else{
				if(nick.length > 8 || nick.length < 0){
					alert(msgError[30]);
					bCorrect = false;
				}				
				else
					bCorrect = true;
			}
		}		
		if(subType == "PASS"){
			var pass = objForm.value;
			if(pass.indexOf(" ") > 0){
				alert(msgError[23]);
				bCorrect = false;
			}
			else{
				if(pass.length > 8 || pass.length < 0){
					alert(msgError[30]);
					bCorrect = false;
				}				
				else
					bCorrect = true;
			}
		}
		if(subType == "BANK"){
			/* la seccion de campo referente al banco */
			if(objForm.id == "BANK"){
				if(objForm.value.length == 4){
					if(!isNumeric(objForm.value)){
						alert(msgError[25]);
						bCorrect = false;
					}
					else
						bCorrect = true;
				}
				else{
					alert(msgError[26]);
					bCorrect = false;					
				}
			}
			/* la seccion de campo referente a la sucursal */
			if(objForm.id == "SUCURSAL"){
				if(objForm.value.length == 4){
					if(!isNumeric(objForm.value)){
						alert(msgError[25]);
						bCorrect = false;
					}
					else
						bCorrect = true;
				}
				else{
					alert(msgError[26]);
					bCorrect = false;					
				}
			}
			/* la seccion de campo referente al dijito de control */
			if(objForm.id == "DC"){
				if(objForm.value.length == 2){
					if(!isNumeric(objForm.value)){
						alert(msgError[25]);
						bCorrect = false;
					}
					else
						bCorrect = true;
				}
				else{
					alert(msgError[27]);
					bCorrect = false;					
				}
			}
			/* la seccion de campo referente al numero de cuenta */
			if(objForm.id == "CUENTA"){
				if(objForm.value.length == 10){
					if(!isNumeric(objForm.value)){
						alert(msgError[25]);
						bCorrect = false;
					}
					else{
						//comprobar el CCC
						bCorrect = checkCCC();
					}
				}
				else{
					alert(msgError[28]);
					bCorrect = false;					
				}
			}										
		}
		if(subType == "TEXT"){
			bCorrect = true;
		}
	if(!bCorrect){
		objForm.setAttribute("isCorrect","false");
		objForm.value = "";
		
		if(window.event){
			event.returnValue = false;
		}
		else{
			window.setTimeout(function(){objForm.focus();},0);
		}
		objForm.focus();
		objForm.style.backgroundColor='#00E8E8';
		objForm.style.border='1px solid red';
	}
	else{
		if(objForm.getAttribute("isCorrect") == "false"){
			objForm.setAttribute("isCorrect","true");
			objForm.style.backgroundColor='';
			objForm.style.border='';
		}
	}		
	}
}


function GetCCC(valor){
  var valores = new Array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6);
  var control = 0;
  for (var f=0; f<=9; f++)
    control += parseInt(valor.charAt(f)) * valores[f];
  control = 11 - (control % 11);
  if (control == 11) control = 0;
  else if (control == 10) control = 1;
  return control;
}

function isNumeric(valor){
  var cad = valor.toString();
  for (var i=0; i<cad.length; i++) {
    var caracter = cad.charAt(i);
	if (caracter<"0" || caracter>"9")
	  return false;
  }
  return true;
}


function checkCCC() {
	var ret 			 	= false;
	var oBanco 			= null;
	var oSucursal 	= null;
	var oDc 				= null;
	var oCuenta 		= null;
	oBanco 		= document.getElementById("BANK");
	oSucursal = document.getElementById("SUCURSAL");
	oDc 			= document.getElementById("DC");
	oCuenta 	= document.getElementById("CUENTA");	
	
	if(oBanco && oSucursal && oDc && oCuenta){
 		if (!(GetCCC("00" + oBanco.value + oSucursal.value) == parseInt(oDc.value.charAt(0))) || !(GetCCC(oCuenta.value) == parseInt(oDc.value.charAt(1)))){
    	alert(msgError[29]);
    	ret = false;
    }
	  else
      ret = true;	
	}
	else
		ret = false;
		
		return ret;
}
