// -----------------------------------------
// This code is used in Upload form and used for validating fields                  
// Feb 16, 2005
// -----------------------------------------




var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
emptyString = /^\s*$/

// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             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)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;  
  
  elem.className = msgtype;
};

// -----------------------------------------
//            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    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(ifld);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg (ifld, "error", "required");  
      vfld.focus();
	  //alert(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// -----------------------------------------
//            validateSpkm
// Validate if something has been entered
// Returns true if so 
// -----------------------------------------

function validateSpkm(vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
	
	var elem1 = document.getElementById("spkmbreaker1");
	var elem2 = document.getElementById("spkmbreaker2");
//	alert(Number(elem1.value) + Number(elem2.value));
//	alert("Total you entered is " + Number(vfld.value));
	
	if ( (Number(elem1.value) + Number(elem2.value)) == Number(vfld.value) )
	{
		//alert("correct");
		msg (ifld, "warn", "");   // OK
		return true;
	}
	else	{
     vfld.focus();

		msg (ifld, "error", " Please try spam-killing math question again");
		return false;
	}

/*
alert(elem.value);
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  
  if (tfld.indexOf(".")== -1)
  
  {  vfld.focus();
     msg (ifld, "error", " No extension error !");

  return false;
  }
  else
  { return true;
  }
*/ 
}

// -----------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// -----------------------------------------

function validatePresent(vfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) {
  	if ( stat == false )
  		vfld.focus();
  	return stat;
  }

  msg (ifld, "warn", "");  
  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  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", " invalid");
    vfld.focus();
    return false;
  }



  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld)) 
// emails that start with a number causes this warning   
//    msg (ifld, "warn", "Unusual e-mail address - check if correct");
  
    msg (ifld, "warn", "");
  else
    msg (ifld, "warn", "");
	
  return true;
};


function validateUploadFile  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) {
    	if ( stat == false )    vfld.focus();
  		return stat;
 	}

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  if (tfld.indexOf(".")== -1)  {  
  	vfld.focus();
    msg (ifld, "error", " No extension error !");
  	return false;
  }
  else   return true; 
}


function validateComments  (vfld,   // element to be validated
                        	 ifld,   // id of element to receive info/error msg
	                         reqd)   // true if required
{
//  var stat = commonCheck (vfld, ifld, reqd);
//  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
//  alert (tfld);
  if (tfld == "" || tfld=="Please type your message here") {
	  
    msg (ifld, "error", "Please  type your message in the comments box.");
    vfld.focus();
    return false;
  }
  
  if (tfld.length >= 400 ) {
    msg (ifld, "error", "Please limit your email message to 400 characters");
    vfld.focus();
    return false;
  }
  
 
  
// To clean up the ifld left on the screen from the previous validation
msg (ifld, "warn", "");
  return true;
};




function validateAddress  (vfld)   // element to be validated
                        	    // id of element to receive info/error msg
	                            // true if required
{

var tfld = vfld.value;  // value of field with whitespace trimmed off
  if (tfld.indexOf(" box") != -1) {
    alert ("Please be advised that the courier cannot ship to PO Boxes!");
//    vfld.focus();
    return false;
  }
// To clean up the ifld left on the screen from the previous validation
//msg (ifld, "warn", "");
  return true;
};

/*
function validateUpload_File  (vfld,   // element to be validated
                       		  ifld,   // id of element to receive info/error msg
                    		     reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
	tfld = tfld.replace(/\+/g,"_")
	alert(tfld)
};
*/


// -----------------------------------------
//             validateAge
// Validate person's age
// Returns true if OK 
// -----------------------------------------
// 								NOT USED									   A TEST FUNCTION    
function validateName    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
 msg (ifld, "warn", ""); 
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var ageRE = /^[0-9]/
  if (!ageRE.test(tfld)) {
    msg (ifld, "error", "not valid Number");
    vfld.focus();
    return false;
  }

  if (tfld<=0) {
    msg (ifld, "error", "zero is not valid");
    vfld.focus();
    return false;
  }
/*
  if (tfld>110) msg (ifld, "warn", "Older than 110: check correct");
  else {
    if (tfld<7) msg (ifld, "warn", "Bit young for this, aren't you?");
    else        msg (ifld, "warn", "");
  }
*/  
  
  
  return true;
};


