var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var mPrefix = "No se pueden quedar campos vacios, "
var mSuffix = "es un dato necesario."
var typePrefix = "Puso un caracter no permitido en "
var sNombre = "Nombre "
var sApellidoPat = "Apellido Paterno "
var sApellidoMat = "Apellido Materno "
var sUsuario = "Usuario  "
var sClave = "Clave "
var sCuentaPro = "Cuenta Profuturo  "
var sCuenta = "Cuenta Banco "
var sPlaza = "Plaza  "
var sSucursal = "Sucursal  "
var sSeguro = "Seguro Social "
var sPassword = "el primer Password "
var sPasswordnum = "el segundo Password "
var sCompania = "Compañia "
var sRFC = "RFC "
var sCURP = "CURP "
var sPais = "País "
var sDia = "Día "
var sMes = "Mes "
var sYear = "Year "
var sEntidad = "Entidad "
var sPuesto = "Puesto "
var sTelefono = "Teléfono "
var sFaxExt = "Extensión Fax "
var sFaxLada = "Lada Fax "
var sFaxTelefono = "Teléfono Fax "
var sLadaOfi = "Lada de Oficina "
var sExtOfi = "Extensión Oficina "
var sLada = "Lada "
var sExt = "Extensión "
var sCantidad = "Cantidad "
var sTelefonoOfi = "Teléfono de Oficina "
var sCalle = "Calle y Número "
var sColonia = "Colonia "
var sCodigoPostal = "Código Postal "
var sCodigoPostalOfi = "Código Postal de Oficina "
var sDelegacion = "Delegación "
var sEstado = "Estado "
var sCiudad = "Ciudad "
var sEmail = "E-mail "
var sRespuesta1 = "Respuesta1 "
var sRespuesta2 = "Respuesta2 "
var sRespuesta3 = "Respuesta3 "
var sSiefore1 = "SIEFORE1 "
var sSiefore2 = "SIEFORE2 "
var sSiefore3 = "SIEFORE3 "
var iEmail = "El campo de email debe ser una dirección valida"
var pEntryPrompt = "Please enter a "
var defaultEmptyOK = false;

function makeArray(n) {

   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;


    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}


function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";


    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}


function stripCharsNotInBag (s, bag)

{   var i;
    var returnString = "";

    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }

    return returnString;
}


function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}


function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}


function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}



function isLetter (c) 
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) || c == "ñ" || c == "ó" || c == "í" || c == "á" || c == "é" || c == "ú" || c == "ü" || c == "Ñ" || c == "Ó" || c == "Í" || c == "Á" || c == "É" || c == "Ú" || c == "Ü")
}


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}


function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}



function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isIntegerPlus (s)

{   var i;

    if (isEmpty(s)) 
       if (isIntegerPlus.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerPlus.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);

        if (!isDigit(c) && c != "(" && c != ")" && c != "+" && c != "-") return false;
    }

    return true;
}

function isIntegerPoint (s)

{   var i;

    if (isEmpty(s)) 
       if (isIntegerPlus.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerPlus.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);

        if (!isDigit(c) && c != ".") return false;
    }

    return true;
}


function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}


function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}


function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}


function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];


    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}



function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {   

        var c = s.charAt(i);

      if (isAlphabetic.arguments.length == 3)
		{
			if (!(isLetter(c)) && c!=" ")
			return false;
		}
		else
		{
        	if (!(isLetter(c)) && c!=" ")
       		return false;
    	}
    }

    return true;
}


function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);


    for (i = 0; i < s.length; i++)
    {   
	
        var c = s.charAt(i);
		
		if (isAlphanumeric.arguments.length == 3)
		{
			if (!(isLetter(c) || isDigit(c) ) && c!="/" && c!="(" && c!=")" && c!="," && c!="." && c!=" ")
			return false;
		}
		else
		{
        	if (! (isLetter(c) || isDigit(c) ) && c!=" ")
       		return false;
    	}
	}

    return true;
}


function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   

    if (isWhitespace(s)) return false;
    
    var i = 1;
    var sLength = s.length;

    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}



function prompt (s)
{   window.status = s
}


function promptEntry (s)
{   window.status = pEntryPrompt + s
}



function warnEmpty (theField, s)
{   theField.focus()
    alert(mPrefix + s + mSuffix)
    return false
}


function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}


function checkStringLength (theField,s)
{
	if (theField.value.length < 4)
	{
		alert ("El primer " + s + " debe ser mínimo de 4 caracteres.")
		return false;
	}
	else
	{
		return true;
	}
}

function checkStringLength2 (theField,s)
{
	if (theField.value.length < 6)
	{
		alert ("El segundo " + s + " debe ser mínimo de 6 caracteres.")
		return false;
	}
	else
	{
		return true;
	}
}

function checkString (theField, s, emptyOK)
{   
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return true;
}

function checkType (theField, charType, s)
{
	if (charType == "num")
	{
		if(isInteger(theField.value,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres numéricos son permitidos en este campo.")
			theField.focus();
			return false;
		}
	}
	
	
	if (charType == "nump")
	{
		if(isIntegerPoint(theField.value,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres numéricos son permitidos en este campo.")
			theField.focus();
			return false;
		}
	}
	
	
	if (charType == "phone")
	{
		if(isIntegerPlus(theField.value,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres numéricos, más los caracteres \"(\", \")\" ,\"-\", y \"+\" son permitidos en este campo.")
			theField.focus();
			return false;
		}
	}
	if (charType == "alpha")
	{
		if(isAlphabetic(theField.value,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres alfabéticos son permitidos en este campo.")
			theField.focus();
			return false;
		}
	}
	else if (charType == "alphaNum")
	{
		if(isAlphanumeric(theField.value,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres alfanuméricos son permitidos en este campo.")
			theField.focus();
			return false;
		}
	}
	else if (charType == "alphaNumPlus")
	{
		if(isAlphanumeric(theField.value,true,true))
		{
			return true;
		}
		else
		{
			alert(typePrefix + s + ". Solamente caracteres alfanuméricos mas puntos, comas, parentesis, y diagonales son permitidos en este campo.")
			theField.focus();
			return false;
		}
	
	}
}


function checkConfirmPW(pwd,pwd_confirm)
{
	if (pwd_confirm.value == pwd.value)
	{
			return true;	
	}
	else
	{
		alert("Su password no coincide, por favor cheque que este correcto");
		pwd_confirm.focus();
		return false;
	}
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}
