var reWhitespace = /^\s+$/
var reLetter = /^[a-zA-Z]$/
var reAlphabetic = /^[a-zA-Záéíóú ]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/
var reDigit = /^\d/
var reLetterOrDigit = /^([a-zA-Z]|\d)$/
var reInteger = /^\d+$/
var reEmail = /^.+\@.+\..+$/
var defaultEmptyOK = false;
var daysInMonth = makeArray(12);
var reDireccion = /^[a-zA-Z0-9áéíóú#\-)( ]+$/

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

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;
//
/*var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
var reSignedFloat = /^(((+|-)?\d+(\.\d*)?)|((+|-)?(\d*\.)?\d+))$/
var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whitespace = " \t\n\r";
*/

function  isDireccion(s)
{return reDireccion.test(s)
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   // Is s empty?
    return (isEmpty(s) || reWhitespace.test(s));
}




function isLetter (c)
{   return reLetter.test(c)
}

function isDigit (c)
{   return reDigit.test(c)
}

function isLetterOrDigit (c)
{   return reLetterOrDigit.test(c)
}


function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}



function isSignedInteger (s)
//var reSignedInteger = /^(+|-)?\d+$/;
{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    
    else {
       return isInteger(s)
    }
}




function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    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];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number <= 0

    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);

    else {
       return reAlphabetic.test(s)
    }
}


function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    else {
       return reAlphanumeric.test(s)
    }
}


function isEmail (s)

{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    
    else {
       return reEmail.test(s)
    }
}


function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}



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);
    return ((num >= a) && (num <= b));
}



// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}



// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}




function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.

    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) 
	return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) 
	return false;
	
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) 
	return false;

    return true;
}

function validarContactenos(form){

	if (form.nombre.value=="")
	{
		alert ("Por favor llena el campo Nombre.");
		form.nombre.focus();
	}
	else if(!isAlphabetic(form.nombre.value))
	{
		alert ("Nombre inválido.");
		form.nombre.focus();
	}
	else if(!(isEmail(form.email.value)||isEmpty(form.email.value))){
		alert ("Email invalido.");
		form.email.focus();
	}
	else if(form.telefono.value==""){
		alert ("Por favor llena el campo Teléfono.");
		form.telefono.focus();
	}
	else if(!isInteger(form.telefono.value)||form.telefono.value.length!=7){
		alert ("Número de teléfono inválido.");
		form.telefono.focus();
	}
	else if(form.direccion.value==""){
		alert ("Por favor llena el campo Dirección.");
		form.direccion.focus();
	}
	else if(!isDireccion(form.direccion.value)){
		alert ("Dirección inválida.");
		form.direccion.focus();
	}
	else if(form.ciudad.value==""){
		alert ("Por favor llena el campo Ciudad.");
		form.direccion.focus();
	}
	else if(!isAlphabetic(form.ciudad.value)){
		alert ("Ciudad Inválida.");
		form.modelo.focus();
	}
	else if(!isAlphabetic(form.productos.value)){
		alert ("Producto Inválido.");
		form.producto.focus();
	}
	else if(!isAlphanumeric(form.referencia.value)&&form.referencia.length>0){
		alert ("Referencia Inválida.");
		form.referencia.focus();
	}
	else if(!isAlphanumeric(form.modelo.value)&&form.modelo.length>0){
		alert ("Modelo Inválido.");
		form.modelo.focus();
	}
	else if(form.inquietud.value==""){
		alert ("Describe tu inquietud por favor.");
		form.inquietud.focus();	
	}
	else if(form.reclamo.checked==false&&form.felicitacion.checked==false&&form.solicitudserv.checked==false&&form.venta.checked==false){
		alert ("Por favor elige un motivo de interés.");
		form.reclamo.focus();
	}
	else
	{
		form.submit();
	}

}

function validarGarantia(form){
	if (form.pnombre.value=="")
	{
		alert ("Por favor llena el campo Primer Nombre.");
		form.pnombre.focus();
	}
	else if(!isAlphabetic(form.pnombre.value))
	{
		alert ("Nombre inválido.");
		form.pnombre.focus();
	}
	else if(!(isAlphabetic(form.snombre.value)||isEmpty(form.snombre.value)))
	{
		alert ("Nombre inválido.");
		form.snombre.focus();
	}
	else if (form.papellido.value=="")
	{
		alert ("Por favor llena el campo Primer Apellido.");
		form.papellido.focus();
	}
	else if(!isAlphabetic(form.papellido.value))
	{
		alert ("Apellido inválido.");
		form.papellido.focus();
	}
	else if(!(isAlphabetic(form.sapellido.value)||isEmpty(form.sapellido.value)))
	{
		alert ("Apellido inválido.");
		form.sapellido.focus();
	}
	else if(form.telefono.value==""){
		alert ("Por favor llena el campo Telefono.");
		form.telefono.focus();
	}
	else if(!isInteger(form.telefono.value)||form.telefono.value.length!=7){
		alert ("Número de teléfono inválido.");
		form.telefono.focus();
	}
	else if(!(isInteger(form.extension.value)||isEmpty(form.extension.value))){
		alert ("Extensión inválida.");
		form.telefono.focus();
	}
	else if(!((isInteger(form.celular.value)&&isInteger(form.prefijocel.value)&&form.celular.value.length==7&&form.prefijocel.value.length==3)||(isEmpty(form.celular.value)&&isEmpty(form.prefijocel.value)))){
		alert ("Número de teléfono celular inválido.");
		form.celular.focus();
	}
	else if(form.direccion.value==""){
		alert ("Por favor llena el campo Dirección.");
		form.direccion.focus();
	}
	else if(!isDireccion(form.direccion.value)){
		alert ("Dirección inválida.");
		form.direccion.focus();
	}
	else if(form.barrio.value==""){
		alert ("Por favor llena el campo barrio.");
		form.barrio.focus();
	}
	else if(form.ciudad.value==0){
		alert ("Elige una ciudad.");
		form.ciudad.focus();
	}
	else if(!(isEmail(form.email.value)||isEmpty(form.email.value))){
		alert ("Email invalido.");
		form.email.focus();
	}
	else if(form.fnacimy.value==""){
		alert ("Por favor elige el año de Fecha de Nacimiento.");
		form.fnacimy.focus();
	}
	else if(form.fnacimy.value.length!=4){
		alert ("Año de nacimiento inválido.");
		form.fnacimy.focus();
	}
	else if(form.fnacimm.value==0){
		alert ("Por favor elige el mes Fecha de Nacimiento.");
		form.fnacimm.focus();
	}
	else if(form.fnacimd.value==0){
		alert ("Por favor elige el día Fecha de Nacimiento.");
		form.fnacimd.focus();
	}
	else if(!isDate(form.fnacimy.value,form.fnacimm.value,form.fnacimd.value)){
		alert ("Fecha de nacimiento invalida.");
		form.fnacimy.focus();
	}
	else if(form.ccompra.value==0){
		alert ("Por favor elige la Ciudad de Compra.");
		form.ccompra.focus();
	}
	else if(form.lcompra.value==""){
		alert ("Por favor llena el campo Lugar de Compra.");
		form.lcompra.focus();
	}
	else if(form.modelo.value==""){
		alert ("Por favor llena el campo Modelo.");
		form.modelo.focus();
	}
	else if(!isAlphanumeric(form.modelo.value)){
		alert ("Modelo Inválido.");
		form.modelo.focus();
	}
	else if(form.serial.value==""){
		alert ("Por favor llena el campo Serial.");
		form.serial.focus();
	}
	else if(!isAlphanumeric(form.serial.value)){
		alert ("Serial Inválido.");
		form.serial.focus();
	}
	else if(form.fventay.value==""){
		alert ("Por favor elige el año de Fecha de Venta.");
		form.fventay.focus();
	}
	else if(form.fventay.value.length!=4){
		alert ("Año de Fecha de Venta inválido.");
		form.fventay.focus();
	}
	else if(form.fventam.value==0){
		alert ("Por favor elige el mes Fecha de Venta.");
		form.fventam.focus();
	}
	else if(form.fventad.value==0){
		alert ("Por favor elige el día Fecha de Venta.");
		form.fventad.focus();
	}
	else if(!isDate(form.fventay.value,form.fventam.value,form.fventad.value)){
		alert ("Fecha de venta invalida.");
		form.fventay.focus();
	}
	else
	{
		form.cod_area.value=form.ciudad.id;
		form.submit();
	}
}