// ----------------------------------------------------------------------
// Javascript form validation routines.
// Author: Stephen Poley
//
// Simple routines to quickly pick up obvious typos.
// All validation routines return true if executed by an older browser:
// in this case validation must be left to the server.
//
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
//
// Update Oct 2005: minor tidy-up: unused parameter removed
//
// Update Jun 2006: minor improvements to variable names and layout
// ----------------------------------------------------------------------

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
var global_focus_field;	
var bjQueryAvailable = false;

if(typeof jQuery  != 'undefined') {
    bjQueryAvailable = true;
}

                                       
// --------------------------------------------
//                  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_focus_field.focus();
}

function setfocus(focus_field)
{
  // only setfocus if field is not hiddden and enabled
  if((focus_field.type != "hidden") && (!focus_field.disabled)) {
	  // save valfield in global variable so value retained when routine exits
	  global_focus_field = focus_field;
	  setTimeout( 'setFocusDelayed()', 100 );
  }
}


// --------------------------------------------
//                  msg
// Display warn/error message in HTML element.
// commonCheck routine must have previously been called
// --------------------------------------------

function msg(fld,     // element instance to display message in
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) { 
  	removeChildNodes(fld);
  } else {
  	removeChildNodes(fld);
  	var newdiv = document.createElement('div');
	newdiv.innerHTML = message;
	fld.appendChild(newdiv);
  }

}
function removeChildNodes(ctrl) {
  while (ctrl.childNodes[0]) {
    ctrl.removeChild(ctrl.childNodes[0]);
  }
  return true;
}

// --------------------------------------------
//            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,
                         focus_field)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  
  if (emptyString.test(valfield.value)) {
    if (required) {
      msg (infofield, "Please enter a value");  
      setfocus(focus_field);
      return false;
    }
    else {
      msg (infofield, "");   // OK
      return true;  
    }
  }
  return proceed;
}

// --------------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// --------------------------------------------

function validatePresent(valfield,   // element to be validated
                         infofield,
                         focus_field ) // id of element to receive info/error msg
{
  var stat = commonCheck (valfield, infofield, true, focus_field);
  if (stat != proceed) return stat;

  msg (infofield, "");  
  return true;
}

// --------------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEmail(valfield,   // element to be validated
                       infofield,  // id of element to receive info/error msg
                       required)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
  if (!email.test(tfld)) {
    msg (infofield, "Not a valid e-mail address");
    setfocus(focus_field);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/  ;
  if (!email2.test(tfld)) 
    msg (infofield, "warn", "Unusual e-mail address - check if correct");
  else
    msg (infofield, "warn", "");
  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 validateTelnr  (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required,
                       	 focus_field)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/  ;
  if (!telnr.test(tfld)) {
    msg (infofield, "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(focus_field);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (infofield, "ERROR: " + numdigits + " digits - too short");
    setfocus(focus_field);
    return false;
  }

  if (numdigits>14)
    msg (infofield, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (infofield, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (infofield, "warn", "");
  }
  return true;
}

// --------------------------------------------
//            validateInteger
// Validates a whole number (signed or unsigned, non-decimal)
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateNumber (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         allowdecimal,  // true if decimal places are allowed
                         allowsign,  // true if signs (+ or -) are allowed
                         required,
                       	 focus_field)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;
  
  var tfld = trim(valfield.value);  // value of field with whitespace trimmed off
  if(allowdecimal){
	  if(allowsign) {
	  	var telnr = /^[-+]?[0-9]+(\.[0-9]+)?$/;
	  	var errormsg = 'Only decimals with optional sign (+/-) allowed';
	  } else {
  		var telnr = /^[0-9]+(\.[0-9]+)?$/;
  		var errormsg = 'Only unsigned decimals allowed';
	  }
  } else {
  	  if(allowsign) {
	  	var telnr = /^[-+]?[0-9]+$/;
  		var errormsg = 'Only whole numbers with optional sign (+/-) allowed';
	  } else {
	  	var telnr = /^[0-9]+$/;
  		var errormsg = 'Only unsigned whole numbers allowed';
	  }     
  }
  
  if (!telnr.test(tfld)) {
    msg (infofield, errormsg);
    setfocus(focus_field);
    return false;
  }

  // clear existing error (if any)
  msg (infofield, "");
  return true;
}

// --------------------------------------------
//            validateEnum
// Validates an enum field is a valid option
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateEnum (valfield,   // element to be validated
                       infofield,  // id of element to receive info/error msg
                       options,  // array of possible values
                       required,
                       focus_field)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;

  var tfld = valfield.value;
  
  // loop through each option
  for (var i=0; i<options.length; i++) {
	if(options[i] === tfld) {
		// found valid value - clear existing error (if any)
		msg (infofield, "");
		return true;
	}
  }
  
  // not set to an enum member, but is not required and blank?
  if((!required) && (tfld == '')) {
  	msg (infofield, "");
	return true;  
  } else {
    msg (infofield, 'Select a valid option');
    setfocus(focus_field);
    return false;
  }
  
}

// --------------------------------------------
//            validateURI
// Validates an URI field is a valid format
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateURI (valfield,   // element to be validated
                      infofield,  // id of element to receive info/error msg
                      required,
                      focus_field)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;

  if(valfield.value.substring(0,7) !== 'http://') {
  	valfield.value = ('http://' + valfield.value);
  }
  
  var tfld = trim(valfield.value);
  
  var telnr = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
  
  
  if (!telnr.test(tfld)) {
    msg (infofield, 'Not a valid URI');
    setfocus(focus_field);
    return false;
  }
  
  msg (infofield, "");
  return true;  
  
}

// --------------------------------------------
//            validateSlug
// Validates an Slug field is a valid format
// Returns true if so (and also if could not be executed because of old browser)
// --------------------------------------------

function validateSlug (valfield,   // element to be validated
                       infofield,  // id of element to receive info/error msg
                       required,
                       focus_field)   // true if required
{
  var stat = commonCheck (valfield, infofield, required, focus_field);
  if (stat != proceed) return stat;

  var tfld = trim(valfield.value);
  
  var telnr = /[a-z0-9-]+/;
  
  if (!telnr.test(tfld)) {
    msg (infofield, 'Not a valid slug (lowercase a-z, numbers and dashes only)');
    setfocus(focus_field);
    return false;
  }
  
  msg (infofield, "");
  return true;  
  
}

// validate a whole form at once
function validateForm(form,validators) {
    var elem;
    var errs=0;
    
    // trigger tinyMCE save so contents can be validated (if declared)
    if(typeof(tinyMCE) !== 'undefined') {
        tinyMCE.triggerSave();
    }
    
    // reverse array so that the focus is set to the first field in error
    validators.reverse();
	
    // loop through each validator instruction
    for (var i=0; i<validators.length; i++) {
    
    	// get element reference
    	oElement = $(validators[i].id);
    
    	// get message field reference
    	try{
    		oMessageElement = $('ve' + validators[i].message_field.substr(2));
    		if(!oMessageElement) {
    			oMessageElement = $('ve' + validators[i].id.substr(2));
    		}
		}catch(e){
			oMessageElement = $('ve' + validators[i].id.substr(2));
		}
    	
    	checkElem = true;
        
        // is jQuery enabled?
        if (bjQueryAvailable) {
            checkElem = (jQuery('#' + validators[i].id).is(":visible"));
        }
        
        
        if(checkElem) {
        
            // determine focus field (if different from value field)
    	    if(validators[i].focusfield) {
    		    var oFocusField = $(validators[i].focusfield);
		    } else {
			    var oFocusField = oElement;
		    }
    	    
    	    // determine validation mode	
    	    switch (validators[i].mode) {
    		    case 'integer':
    			    // check for explicit whole number (integer)
    			    if (!validateNumber(oElement,oMessageElement,false,validators[i].signed,validators[i].required,oFocusField))
    			    { errs += 1; }
    			    break;
    		    case 'decimal':
    			    // check for number with optional decimal places
    			    if (!validateNumber(oElement,oMessageElement,true,validators[i].signed,validators[i].required,oFocusField))
    			    { errs += 1; }
    			    break;
    		    case 'enum':
    			    // check value is in options array
    			    if (!validateEnum(oElement,oMessageElement,validators[i].options,validators[i].required,oFocusField))
    			    { errs += 1; }
    			    break;
    		    case 'uri':
    			    // check value is in options array
    			    if (!validateURI(oElement,oMessageElement,validators[i].required,oFocusField))
    			    { errs += 1; }
    			    break;
    		    case 'slug':
    			    // check value is in options array
    			    if (!validateSlug(oElement,oMessageElement,validators[i].required,oFocusField))
    			    { errs += 1; }
    			    break;
    		    case 'text': // basically any value goes, just check mandatory
    		    default:
				    // check if the field is required
    			    if(validators[i].required) {
    				    if (!validatePresent(oElement,oMessageElement,oFocusField))
    				    { errs += 1; }
				    }
				    break;
    	    }
        
        }
    	    
    }
    
    if (errs>1)  alert("There are " + errs + " fields which need correction!\n\nPlease correct the errors and try again.");
    if (errs==1) alert("There is a field which needs correction!\n\nPlease correct errors and try again.");

    // reverse validators back to original order
    validators.reverse();
    
    return (errs==0);

};