var whitespace = " \t\n\r";
var phoneChars = "()-+ ";
var pPhoneNumber = "ingrese un número de teléfono";


function validar(xinput,tipval){
	var xkey=event.keyCode;
	//alert(xkey);
	if(tipval=="int")
		if ((xkey < 48) || (xkey > 57)) event.returnValue = false;
	if(tipval=="dec")
		if ((xkey < 46) || (xkey > 57)) event.returnValue = false;
	if(tipval=="str")
		if (((xkey != 32) && (xkey < 65)) || ((xkey > 90) && (xkey < 97))) event.returnValue = false;
	if(tipval=="tlf")
		if (((xkey != 32) && (xkey < 45)) || (xkey > 57)) event.returnValue = false;
	}

function validaRegistro(){
		var f = document.f;
       	if (f.nombre.value == ""){
			alert("El nombre no Valido");
			f.nombre.focus();
			return false;
			}
        if (f.direccion.value == ""){
			alert("Direccion no Valido");
   			f.direccion.focus();
			return false;
			}
        if (f.postal.value == ""){
			alert("Codigo Postal no Valido");
			f.postal.focus();
			return false;
			}
        if (f.email.value == ""){
			alert("Email no Valido");
			f.email.focus();
			return false;
			}
        if (f.pais.value == ""){
			alert("Pais no Valido");
			f.email.focus();
			return false;
			}			
	return true;
	}

function isPhoneNumber (s)
{   var modString;
    if (isEmpty(s))
       if (isPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isPhoneNumber.arguments[1] == true);
    modString = stripCharsInBag( s, phoneChars );
    return (isInteger(modString))
}
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}



// s es una direccion de correo valida
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 isNice(s)
{
        var i = 1;
        var sLength = s.length;
        var b = 1;
        while(i<sLength) {
                if( (s.charAt(i) == "\"") || (s.charAt(i) == "'" ) ) b = 0;
                i++;
        }
        return b;
}

var defaultEmptyOK = false
var whitespace = " \t\n\r";

function makeArray(n) {

   for (var i = 1; i <= n; i++) {
      this[i] = 0
   }
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

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];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function stripCharsInBag (s, bag){

	var i;
    var returnString = "";

    // Buscar por el string, si el caracter no esta en "bag",
    // agregarlo a returnString

    for (i = 0; i < s.length; i++)
    {   var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

// Quitar todos los espacios en blanco de un string
function stripWhitespace (s){
	return stripCharsInBag (s, whitespace)
}

function isEmpty(s)
{
	s = stripWhitespace(s);
	return ((s == null) || (s.length == 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 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( i != 0 ) {
            if (!isDigit(c)) return false;
        } else {
            if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}

function isIntegerInRange (s, a, b){

	if (isEmpty(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s,10); // se agregó la base 10 JFZ
    return ((num >= a) && (num <= b));
}






function isDigit (c){
   return ((c >= "0") && (c <= "9"))
}

function isNumber(s) {

    if (isEmpty(s)) return false;

    for (i = 0; i < s.length; i++){

        var c = s.charAt(i);

        if (!isDigit(c))
        return false;
    }

    // Todos son digitos.
    return true;
}





function validaRegistro(){

	var f = document.f;
       	if (f.empresa.value == ""){
			alert("El nombre empresa no Valido o ingrese su nombre");
			f.empresa.focus();
			return false;
			}
        if (f.percontacto.value == ""){
			alert("Persona contacto no Valido, ingrese su nombre");
   			f.percontacto.focus();
			return false;
			}

        if (f.direccion.value == ""){
			alert("direccion no Valido");
   			f.direccion.focus();
			return false;
			}

	// valida telefono
       if(!isPhoneNumber (f.telefono.value)) {
		alert('Telefono no valido');
		f.telefono.focus();
		return false;
	}

       // valida email
    	if (!isEmail (f.email.value)){
		alert('Email no valido');
		f.email.focus();
		return false;
	}

  	if (f.codigo.value == ""){
			alert("Seleccione categoria de su empresa");
			f.codigo.focus();
			return false;
			}
			
  	if (f.pais.value == ""){
			alert("Seleccione Pais de su empresa");
			f.codigo.focus();
			return false;
			}
	return true;
	}

    function enviarRegistros(){

      if (!validaRegistro()) return false;

	// Si llega hasta acá todo está OK y enviamos la página
			
	f.submit();
}