var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/
var glb_vfld;      // retain vfld for timer thread


function validateForm(thisForm){

   	var invalid = 0;
   	with(thisForm){
   	
		var errMsg = "";
		postalCode.value = stripNonNum(postalCode.value);
		//a00N30000000sZSm.value = stripNonNum(a00N30000000sZSm.value);

		if ( !validateGeneral( 1, firstName.value, 0, 150 ) ){
			invalid++;
			msg ("firstName", "formError", "Invalid first name");
			errMsg = "First name\n";
		}
		else
			msg("firstName", "formError", "");
			
		if ( !validateGeneral( 1, lastName.value, 0, 150 ) ){
			invalid++;
			msg ("lastName", "formError", "Invalid last name");
			errMsg += "Last name\n";
		}
		else
			msg("lastName", "formError", "");
		
		
		if ( !validateGeneral( 1, street1.value, 0 ) ){
			invalid++;
			msg ("street1", "formError", "Invalid address");
			errMsg += "Address\n";
		}
		else
			msg("street1", "formError", "");
		
		if ( !validateGeneral( 1, city.value, 0 ) ){
			invalid++;
			msg ("city", "formError", "Invalid city");
			errMsg += "City\n";
		}
		else
			msg("city", "formError", "");

		if ( !validateGeneral( 1, stateID.value, 0 ) ){
			invalid++;
			msg ("stateID", "formError", "Invalid State/Province");
			errMsg += "State/Province\n";
		}
		else
			msg("stateID", "formError", "");
			
		if ( !validateGeneral( 5, postalCode.value, 0 ) ){
			invalid++;
			msg ("postalCode", "formError", "Zip/Postal code must be at least a 5 digit number");
			errMsg += "Zip Code\n";
		}
		else
			msg("postalCode", "formError", "");

	}
	
	if ( invalid > 0 ){
		alert( "The following fields have some errors:\n\n" + errMsg + "Please correct before submitting." );
		return false;
	}
	
};

function validateGeneral( minLength, thisVal, max ){
  	if ( thisVal.length < minLength )
    	return false;
	else if ( max != 0 && thisVal.length > max )
    	return false;
    else
    	return true;
};

// strip non-number characters
function stripNonNum( str ){
	return str.replace(/\D/gi, "");
}

// Trim leading/trailing whitespace off string
function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};

function cleanEmail( email ){
	return email.replace(/[\(\)\<\>\[\]\,\;\:\/\ ]/g, '');
}

function validateEmail(email){

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(email))) { 
	       return false;
	}
	else
		return true;


}



// -----------------------------------------
//                  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;   // set the CSS class to adjust appearance of message
}