// Funcion que abre otra ventana con una imagen
var mywindow;
var mycalendar;
var activedateTb;
function openWindow (page,type)
	{
	var sizeString;
	if (type==1)
		{
		sizeString="width=410, height=150"
		}
	else
		{
		sizeString="width=500, height=500"
		}
	mywindow=open(page,"ventana", sizeString)
	}


function muestraCalendario(stextBox)
	{
	mycalendar=open("../calendario.htm","calendario", "width=200, height=200")
	activedateTb=stextBox;
	//mycalendar.document.tbName=stextBox;
	//alert(mycalendar.document.tbName)
	}

function IsInt(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var ch;

   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      ch = sText.charAt(i);
      if (ValidChars.indexOf(ch) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsNumerical (sText)
{
   var ValidChars = "0123456789,";
   var IsNumber=true;
   var ch;

   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      ch = sText.charAt(i);
      if (ValidChars.indexOf(ch) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


// Funciones de Formulario


function doPage (nPage)
	{
	document.forms[0].hdn_currentpage_busq.value=nPage
	document.forms[0].hdnAction.value="1"
	doPost();
	}
function doPersonalPage (nPage)
	{
	document.forms[0].hdn_currentpage_busq_p.value=nPage
	document.forms[0].hdn_action.value="1"
	doPost();
	}
function doFind()
	{
	document.forms[0].hdn_action.value="1"
	if (document.forms[0].hdn_currentpage_busq)
		{document.forms[0].hdn_currentpage_busq.value="0"}
	if (document.forms[0].hdn_currentpage)
		{document.forms[0].hdn_currentpage.value="0"}
	if (document.forms[0].hdn_totalRegistros)
		{document.forms[0].hdn_totalRegistros.value="0"}
	doPost();
	}

function doPost()
		{
		document.forms[0].submit();
		return true;
		}

function DateFields(fields,messages,NumForm)
		{
		var msgInfo="";
                NumForm=NumForm || 0;
		for (x=0;x<fields.length;x++)
			{
			if (!eval("isValidDateField(document.forms["+NumForm+"]." + fields[x] + ".value)"))
				{
				msgInfo+="\n - " + messages[x];
				}
			}
		if (msgInfo.length>0)
			{
			alert ("Los siguientes campos no tienen un formato de fecha válido:" + msgInfo + "\nEl formato correcto sería dd/mm/aaaa.");
			return false;
			}
		else
			{return true;}
		}

function NumericFields(fields,messages,NumForm)
		{
		var msgInfo="";
                NumForm=NumForm || 0;
		for (x=0;x<fields.length;x++)
			{
			if (eval("isNaN(document.forms["+NumForm+"]." + fields[x] + ".value.replace(',','.'))"))
				{
				msgInfo+="\n - " + messages[x];
				}
			else
				{document.forms[0][fields[x]].value=document.forms[0][fields[x]].value.replace(',','.')}
			}
		if (msgInfo.length>0)
			{
			alert ("Los siguientes campos no tienen un formato numérico válido:" + msgInfo);
			return false;
			}
		else
			{return true;}
		}

function RequiredFields(fields,messages,NumForm)
		{
		var msgInfo="";
                NumForm=NumForm || 0;
		for (x=0;x<fields.length;x++)
			{
			if (eval("document.forms["+NumForm+"]." + fields[x] + ".value.length==0"))
				{
				msgInfo+="\n - " + messages[x];
				}
			}
		if (msgInfo.length>0)
			{
			alert ("Los siguientes campos son necesarios:" + msgInfo);
			return false;
			}
		else
			{return true;}
		}

function ValueBetween (fields,messages,values,NumForm)
		{
		var msgInfo="";
                NumForm=NumForm || 0;
		for (x=0;x<fields.length;x++)
			{
			if (!(document.forms[NumForm][ fields[x]].value>= values[x][0] && document.forms[NumForm][ fields[x]].value<= values[x][1]))
				{
				msgInfo+="\n - " + messages[x] + "  Rango(" +  values[x][0] + " y " +  values[x][1] + ")";
				}
			}
		if (msgInfo.length>0)
			{
			alert ("El valor de los siguientes campos no está dentro de su rango: " + msgInfo);
			return false;
			}
		else
			{return true;}
		}

function isValidDateField(svalue)
	{
	var valreturn=false;
	var oDate;
	if (svalue.length>=6)
		{
			oDate=new Date(svalue);
		}
	return !isNaN(oDate);
	}

function MinLengths(fields,messages,flengths,NumForm)
	{
	var msgInfo="";
        NumForm=NumForm || 0;
	for (x=0;x<fields.length;x++)
		{
		if (eval("document.forms["+NumForm+"]." + fields[x] + ".value.length<" + flengths[x]))
			{
			msgInfo+="\n - " + messages[x] + " ("+ flengths[x] + ")";
			}
		}
	if (msgInfo.length>0)
		{
		alert ("Los siguientes campos no tienen la longitud mínima:" + msgInfo);
		return false;
		}
	else
		{return true;}

	}
function DateIncremental (fields,messages,NumForm)
	{
		var msgInfo="";
                NumForm=NumForm || 0;
	/*
	for (x=0;x<fields.length;x++)
		{
		if (eval("document.forms[0]." + fields[x] + ".value.length<" + flengths[x]))
			{
			msgInfo+="\n - " + messages[x] + " ("+ flengths[x] + ")";
			}
		}
	if (msgInfo.length>0)
		{
		alert ("Los siguientes campos no tienen la longitud mínima:" + msgInfo);
		return false;
		}
	else
		{return true;}
		*/
	return true;
	}
function validarEmail (valor)
	{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
		{
		return true
		}
	else {
		return false;
		}
	}

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

function numerico(valor){
  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 validarCCC(banco,sucursal,dc,cuenta) {
	vreturn=false;
  if (banco == ""  || sucursal == "" ||
      dc == "" || cuenta == "")
    alert("Por favor, introduzca los datos de su cuenta");
  else {
    if (banco.length != 4 || sucursal.length != 4 ||
        dc.length != 2 || cuenta.length != 10)
      alert("Por favor, introduzca correctamente los datos de su cuenta;"
	    + " no están completos");
    else {
      if (!numerico(banco) || !numerico(sucursal) ||
          !numerico(dc) || !numerico(cuenta))
        alert("Por favor, introduzca correctamente los datos de su "
         + "cuenta; no son numericos");
      else {
        if (!(obtenerDigito("00" + banco + sucursal) ==
              parseInt(dc.charAt(0))) ||
            !(obtenerDigito(cuenta) ==
              parseInt(dc.charAt(1))))
          alert("La cuenta bancaria indicada no es correcta.");
	    else
         {vreturn=true;}
      }
    }
  }
  return vreturn;
}
