var szValChrStrEspacios       = " \t\n\r";
var szValChrStrCaracaeresMail = "_-.@";
var szValChrStrCaracaeresExtendidos  = ".,_-/#[]=&ºª+()";
var szValChrStrLetrasMinusculasBase  = "abcdefghijklmnopqrstuvwxyz";
var szValChrStrLetrasMayusculasBase  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var szValChrStrLetrasMinusculasTodas = szValChrStrLetrasMinusculasBase + "áéíóúñü";
var szValChrStrLetrasMayusculasTodas = szValChrStrLetrasMayusculasBase + "ÁÉÍÓÚÑ";
var szValChrStrNum          = "01234567890";
var szValChrStrAlfa         = szValChrStrEspacios + szValChrStrLetrasMayusculasTodas + szValChrStrLetrasMinusculasTodas;
var szValChrStrAlfaExt      = szValChrStrEspacios + szValChrStrLetrasMayusculasTodas + szValChrStrLetrasMinusculasTodas + szValChrStrCaracaeresExtendidos;
var szValChrStrAlfaNum      = szValChrStrEspacios + szValChrStrLetrasMayusculasTodas + szValChrStrLetrasMinusculasTodas + szValChrStrNum;
var szValChrStrAlfaNumExt   = szValChrStrEspacios + szValChrStrLetrasMayusculasTodas + szValChrStrLetrasMinusculasTodas + szValChrStrNum + szValChrStrCaracaeresExtendidos;
var szValChrStrMail         = szValChrStrLetrasMayusculasBase + szValChrStrLetrasMinusculasBase + szValChrStrNum + szValChrStrCaracaeresMail;

var szAletMsgBase       = "Este campo solo acepta\n";
var szAletMsgCharsAlfa  = " - Letras   : a, b, c, d ...\n";
var szAletMsgCharsNum   = " - Numeros  : 0,1,2,3,4,5,6,7,8 o 9\n";
var szAletMsgCharsExt   = " - Simbolos : . , _ - / # [ ] = & º ª + ( ) \n";
var szAletMsgMail       = szAletMsgBase + "una direccion de mail\nEj : usuario@dominio.com";
var szAletMsgSelect     = "En este campo debe seleccionarse alguna opcion para seguir";
var szAletMsgAlfa       = szAletMsgBase + szAletMsgCharsAlfa;
var szAletMsgAlfaExt    = szAletMsgBase + szAletMsgCharsAlfa + szAletMsgCharsExt;
var szAletMsgAlfaNum    = szAletMsgBase + szAletMsgCharsAlfa + szAletMsgCharsNum;
var szAletMsgAlfaNumExt = szAletMsgBase + szAletMsgCharsAlfa + szAletMsgCharsNum + szAletMsgCharsExt;
var szAletMsgNum        = szAletMsgBase + szAletMsgCharsNum;

/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
var RETENER_CARACTERES  	= 1;
var ELIMINAR_CARACTERES   = 2;
function szLimpiarCadena (szStr, szFilterStr, inAccion)
{
	var inCt;
	var szRet = "";
	for (inCt = 0; inCt <= szStr.length; inCt++)
		{
		var chChr = szStr.charAt(inCt);
		switch (inAccion)
			{
			case ELIMINAR_CARACTERES : if (szFilterStr.indexOf(chChr) == -1) szRet += chChr;	break;
			case RETENER_CARACTERES  : if (szFilterStr.indexOf(chChr) >=  0) szRet += chChr;	break;
			}
		}

	return szRet;
}


/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
function fValidEmail (szEmail)
{
	var i = 1;
	var inCt = 0;
	var inLen = szEmail.length;

	if (szLimpiarCadena (szEmail, szValChrStrMail, ELIMINAR_CARACTERES) != "")
		return false;
	while ((inCt < inLen) && (szEmail.charAt(inCt) != "@"))
		{inCt++}
	if ((inCt >= inLen) || (szEmail.charAt(inCt) != "@"))
		return false;
	else
		inCt += 2;
	while ((inCt < inLen) && (szEmail.charAt(inCt) != "."))
		{inCt++}
	if ((inCt >= inLen - 1) || (szEmail.charAt(inCt) != "."))
		return false;
	return true;
}

/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
function fValidCuit (szCuit)
{
	var inCt;
  var inSumaDig   = 0;
  var inAcumAct   = 0;
  var inAcumPrev  = 0;
  var inDigitoVer = 0;
	var szStr = szCuit.value;

	for (inCt = 1; inCt < szStr.length; inCt++)
		{
    inAcumPrev = inAcumAct;
    inAcumAct  = szStr.substring(0, inCt);
    inSumaDig  = inSumaDig + ((inAcumAct - 10 * inAcumPrev) * (((10 - inCt) % 6) + 2));
  	}

  inDigitoVer = 11 - (inSumaDig % 11)
  if (inDigitoVer == 11)
		{
    inDigitoVer = 0;
  	}
  else
		{
    if (inDigitoVer == 10)
			{
      DigitoVer = 9;
    	}
		}

  if (inDigitoVer == szStr.substring(0, 10))
		return (true);

	return (false);
}

/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
var NO_ACEPTA_VACIO			      = 0;
var SI_ACEPTA_VACIO           = 1;

var CAMPO_TIPO_ALFA       		=	1;
var CAMPO_TIPO_ALFAEXT        =	2;
var CAMPO_TIPO_ALFANUM        =	3;
var CAMPO_TIPO_ALFANUMEXT     =	4;
var CAMPO_TIPO_NUMERO         =	5;
var CAMPO_TIPO_CUIT           =	6;
var CAMPO_TIPO_MAIL           =	7;
var CAMPO_TIPO_SELECT         =	8;
var CAMPO_TIPO_LIBRE          =	9;

/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
function fValidateFormField (field, fVacio, inTipo, inMin, inMax)
{
	var szAlertMsg = "";

	/* -----------------16/02/2006 15:40-----------------
	 * Si es valido un campo vacio y viene vacio no hago mas nada
	 * --------------------------------------------------*/
	if (  (field.value == null)
		 || (field.value == "")
		 || (field.value.length < 1)
		 || (  (field.value == 0)
		    && (inTipo ==  CAMPO_TIPO_SELECT)
				)
     )
		{
		if (fVacio == SI_ACEPTA_VACIO)
			{
			return (true);
			}
		else
			{
			field.value = "COMPLETAR !!!!";
			szAlertMsg = "Este campo no puede estar vacio";
			}
		}

	/* -----------------16/02/2006 16:42-----------------
	 * A algunos campos me encargo de filtrarles los espacios blancos
	 * xq si los ingresaron es por error.
	 * --------------------------------------------------*/
	if (szAlertMsg == "")
		{
		switch (inTipo)
			{
			case CAMPO_TIPO_MAIL       :
			case CAMPO_TIPO_CUIT       :
			case CAMPO_TIPO_NUMERO     :  szLimpiarCadena (field.value, szValChrStrEspacios,   ELIMINAR_CARACTERES);	break;
			}
		}

	/* -----------------16/02/2006 15:45-----------------
	 * Caso contrario lo primero es validar la longuitud
	 * --------------------------------------------------*/
	if (szAlertMsg == "")
		{
		if (inTipo != CAMPO_TIPO_SELECT)
			{
			if (field.value.length < inMin)
				szAlertMsg = "Este campo no puede tener menos de " + inMin + " caracteres";
			if (field.value.length > inMax)
				szAlertMsg = "Este campo no puede tener mas de " + inMax + " caracteres";
			}
		}

	/* -----------------16/02/2006 15:50-----------------
	 * Ahora hago un switch para validar el dato que me llega en cada campo
	 * --------------------------------------------------*/
	if (szAlertMsg == "")
		{
		switch (inTipo)
			{
			case CAMPO_TIPO_ALFA       :  if (szLimpiarCadena (field.value, szValChrStrAlfa,       ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgAlfa;					break;
			case CAMPO_TIPO_ALFAEXT    :  if (szLimpiarCadena (field.value, szValChrStrAlfaExt,    ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgAlfaExt;			break;
			case CAMPO_TIPO_ALFANUM    :  if (szLimpiarCadena (field.value, szValChrStrAlfaNum,    ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgAlfaNum;			break;
			case CAMPO_TIPO_ALFANUMEXT :  if (szLimpiarCadena (field.value, szValChrStrAlfaNumExt, ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgAlfaNumExt;		break;
			case CAMPO_TIPO_NUMERO     :  if (szLimpiarCadena (field.value, szValChrStrNum,        ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgNum;		      break;
			case CAMPO_TIPO_CUIT       :  if (szLimpiarCadena (field.value, szValChrStrNum,        ELIMINAR_CARACTERES) != "")  szAlertMsg = szAletMsgNum;		      break;
			case CAMPO_TIPO_MAIL       :  if (fValidEmail (field.value) == false)   																									szAlertMsg = szAletMsgMail;					break;
			case CAMPO_TIPO_SELECT     :	if (field.value == 0)       																													szAlertMsg = szAletMsgSelect;   		break;
			}
		}

	if (szAlertMsg != "")
		{
		field.focus();
		alert (szAlertMsg);
		return (false);
		}
	return (true);
}






/****************************************************************************
 * Funcion :
 *
 * Descrip :
 ****************************************************************************/
function vdPreValidate(stForm, stField, fVacio, inTipo, inMin, inMax)
{
    var szClass = "Normal";
    var fDisabled, fRet;
    var stElements;

    fDisabled = stForm[stField].disabled;
    stForm[stField].disabled = false;

    fRet = fValidateFormField (stForm[stField], fVacio, inTipo, inMin, inMax);
    if (fRet == false)
      szClass = "Warning";

    stElements = document.getElementsByName (stField);
    for (inCt = 0; inCt < stElements.length; inCt++)
      if (  (stElements[inCt].tagName == "TD")
         || (stElements[inCt].tagName == "DIV")
         || (stElements[inCt].tagName == "IMG")
         )
        {
        stElements[inCt].className = szClass;
        }

    stForm[stField].disabled = fDisabled;
    return (fRet);
}

