
var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread

// --------------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// --------------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}


// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------


function showmessage(ElemId,Message,msgtype){
	
	switch (msgtype){
		case "error": 
			//document.getElementById(ElemId).style.backgroundColor="#FF0000";
			document.getElementById(ElemId).style.color= "#FF0000";
			document.getElementById(ElemId).innerHTML=Message;
			document.getElementById('MensajesError').style.display="";
			document.getElementById(ElemId).style.display="";
		break;
		case "warn": 
			//document.getElementById(ElemId).style.backgroundColor="#FFFF00";
			document.getElementById(ElemId).style.color= "##FF5500";
			document.getElementById(ElemId).innerHTML=Message;
			document.getElementById('MensajesError').style.display="";
			document.getElementById(ElemId).style.display="";
		break;
		case "normal" : 
			//document.getElementById(ElemId).style.backgroundColor="#FFFFFF";
			document.getElementById(ElemId).style.color= "#000000";
			document.getElementById(ElemId).innerHTML=Message;
			document.getElementById('MensajesError').style.display="none";
			document.getElementById(ElemId).style.display="none";
	}

}
// --------------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// --------------------------------------------

var proceed = 2;  

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  
  if (emptyString.test(valfield.value)) {
    if (required) {
	//showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td>"+MensajeTxt+FieldName+" es obligatorio</td></tr></table>","error");
      
      //setfocus(valfield);
	  valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
      return false;
    }
    else {
      //showmessage(infofield,"","normal");   // OK
	  valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validateTxt(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, required,FieldName,IdReq);
  if (stat != proceed){
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return stat;
  }else{
	showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
	return true;
  }
  
}

//Valida campos numericos solo acepta numeros de hasta 5 digitos
function validateNumeros(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, required,FieldName,IdReq);
  if (stat != proceed){
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return stat;
  }else{
	showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
  }
  
  var tfld = trim(valfield.value); 
  var pass = /^[0-9]{3,20}$/  ;
  if (!pass.test(tfld)) {
  showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>Solo se permiten números</td></tr></table>","error");
  valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
   return false;
  }else{
    showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
  return true;
  }
}

//Valida la contraseña, tiene que empezar con una letra mayuscula o 
//minuscula tener hasta 5 caracteres o mas despues de la primera letra

function validatePass(valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, required,FieldName,IdReq);
  if (stat != proceed){
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return stat;
  }else{
	showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
	return true;
  }

  var tfld = trim(valfield.value); 
  var pass = /^(?=.*\d)(?=.*[a-zA-Z])(?!.*\s).*$/;
  if (!pass.test(tfld)) {
	  showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>La contraseña debe tener al menos 1 letra y 1 número: A-Z, a-z, 0-9</td></tr></table>","warn");
    
	//setfocus(valfield);
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return false;
  } else{
   showmessage(infofield,"","normal");
   valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
  return true;
  }

}
//Valida Emails, que tengan @ y . pero que tengan letras entre esos signos

function validateEmail  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq)   // true if required
{
  var stat = commonCheck (valfield, infofield, required,FieldName,IdReq);
  if (stat != proceed){
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return stat;
  }else{
	showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
  }

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
	  showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>No es una dirección de correo válida</td></tr></table>","error");
   valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
    //setfocus(valfield);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) {
   showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>Dirección de correo inusual - verifique si está correcta</td></tr></table>","error");
   valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
   return false;
  }else{
    showmessage(infofield, "", "normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
  return true;
  }
}


// --------------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// --------------------------------------------

function validatePhone  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
						 FieldName,
						 IdReq)   // true if required
{
var stat = commonCheck (valfield, infofield, required,FieldName,IdReq);
if (stat != proceed){
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	return stat;
  }else{
	showmessage(infofield,"","normal");
	valfield.style.bordercolor="#89A9C7";
	valfield.style.background="#FFFFFF";
	document.getElementById('ReqField'+IdReq).style.color="#000000";
	return stat;
  }

var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
if (!telnr.test(tfld)) {
	showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>No es un numero telefónico válido. Solo se permiten digitos, espacios, (), - y un signo + al principio</td></tr></table>","error");
	valfield.style.bordercolor="#FF0000";
	valfield.style.background="#FFCCCC";
	document.getElementById('ReqField'+IdReq).style.color="#FF0000";
	//setfocus(valfield);
	return false;
}

var numdigits = 0;
for (var j=0; j<tfld.length; j++)
if (tfld.charAt(j)>='0' && tfld.charAt(j)<='7') numdigits++;
	
	if (numdigits<5) {
		showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>Error: " + numdigits + " digitos - muy corto</td></tr></table>","error");
		valfield.style.bordercolor="#FF0000";
		valfield.style.background="#FFCCCC";
		document.getElementById('ReqField'+IdReq).style.color="#FF0000";
		//setfocus(valfield);
		return false;
	}
	
	if (numdigits>20){
		
		showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>Verifique: " + numdigits + " digitos - muy grande</td></tr></table>","warn");
		return false;
		
	}else { 
	
		if (numdigits<8){
			showmessage(infofield,"<table width=400 border=0 cellspacing=0 cellpadding=0><tr><td class=SpanWarn>Solo " + numdigits + " digitos - muy corto</td></tr></table>","warn");
			valfield.style.bordercolor="#FF0000";
			valfield.style.background="#FFCCCC";
			document.getElementById('ReqField'+IdReq).style.color="#FF0000";
			return false;
		
		}else{
			showmessage(infofield,"","normal");
			valfield.style.bordercolor="#89A9C7";
			valfield.style.background="#FFFFFF";
			document.getElementById('ReqField'+IdReq).style.color="#000000";
			return true;
		}
	}
	
}
