// Returns false if the field is empty, null, or has the string "null", and pops up
// the message passed to the function
function isNotNullOrEmptyString(fieldName, message) {	
	if (isNullOrEmpty(document.getElementById(fieldName).value)) {	
		alert(message);		
		return false;
	}
	return true;
}
// general purpose function to see if an input value has been
// entered at all or if the input value has a value "null"
function isNullOrEmpty(inputStr) {
	// trim; remove leading and trailing spaces
	var trimmedValue = trimString(inputStr);
	if (isEmpty(trimmedValue) || trimmedValue == "null") {
		return true;
	}
	return false;
}

//Remove leading and trailing spaces
function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

// general purpose function to see if an input value has been
// entered at all
function isEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		return true;
	}
	return false;
}

// Validates the email entered.
function validateEmail(fieldValue){
   // The invalid characters that should not be used in an email address
   var invalidChars = " /:,;"; 
   var emailAddress = fieldValue;
   
   var atPosition = emailAddress.indexOf("@",1);
   var periodPosition = emailAddress.indexOf(".",atPosition);
   
   if (isNullOrEmpty(emailAddress)){
      return false;
   }
   // Checks for the invalid characters listed above.
   for (var i=0; i<invalidChars.length; i++){
      badChar = invalidChars.charAt(i);
	  if (emailAddress.indexOf(badChar,0) > -1){
	     return false;		 
	  }
   }

   if (atPosition == -1){ // Checks for the @
      return false;
   }
   if (emailAddress.indexOf("@",atPosition + 1) > -1){ // Makes sure there is one @
      return false;
   }
   if (periodPosition == -1){ // Makes sure there is a period after the @ 
      return false;
   }
   // Makes sure there is at least 2 characters after the period
   if ((periodPosition + 3) > emailAddress.length){ 
      return false;
   }
   
   return true;
}

function validateEmailAddressList(emailAddressList, message){
	var pos = 0;
	var i = 0;
	var origin = 0;
	originalString = "";
	originalString = stringReplace(document.getElementById(emailAddressList).value, " ", "");
	// check if the original string is empty
	if (originalString == "") {
		// do nothing
		return true;
	} else {	
		pos = originalString.indexOf(',');
		while (pos != -1){
			// Get the position of '/'
			preString = originalString.substring(0, pos);
			// Get the item between two ','
			emailAddress = originalString.substring(origin, pos);
			//validate the email address
			if(validateEmailAddress(emailAddress, message)) {
			origin = pos+1;
			postString = originalString.substring(pos+1, originalString.length);
			originalString = preString + ' ' + postString;
			pos = originalString.indexOf(',');
			i++
			}
			else {
				return false;
			}
		}
			emailAddress = originalString.substring(origin, originalString.length);
		return validateEmailAddress(emailAddress, message);
	}
} 

// function used to check email and display message
function isValidEmail(fieldname, msg) {
	if (!validateEmail(document.getElementById(fieldname).value)) {
		alert(msg);
		return false;
	}
	return true;
}
//Compare two fields to ensure that if field1 value is not null or empty string
// then the field2 value must not be null or an empty string
function compareFieldValues(field1, field2, field3, message) {
	//Check whether field1 value is not null or empty string
	if(!isNullOrEmpty(document.getElementById(field1).value) || !isNullOrEmpty(document.getElementById(field2).value)) {
		//Check whether field2 value is null or empty string
		if(isNullOrEmpty(document.getElementById(field3).value)) {
			//Display error message
			alert(message + document.getElementById(field1).value + " " + document.getElementById(field2).value);
			return false;
		}	
	} 
	return true;
} 

// Returns false if the field has a value that is not a number and pops up
// the message passed to the function
function isNonNegativeNumber(fieldName, message) {
	if (isNumber(document.getElementById(fieldName).value)) {
		return true;
	}
	alert(message);
	document.getElementById(fieldName).value = "";
	return false;
}

// general purpose function to see if a suspected numeric input
// is a positive integer
function isPosInteger(fieldName, message) {
	inputStr = document.getElementById(fieldName).value;
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			// clear the number field
			document.getElementById(fieldName).value = "";
			alert(message);
			return false;
		}
	}
	return true;
}

// function to open a pop-up window with specific width
function openPopUpWindow(URL) {
aWindow=window.open(URL,"newwindow","toolbar=no,scrollbars=no,status=no,location=no,resizable=no,menubar=no,width=350,height=200,left=700,top=300");
}

// function to obsfuscate email addresses from spammers
function contactUnobsfuscate() {
	
	// find all links in HTML
	var link = document.getElementsByTagName && document.getElementsByTagName("a");
	var contact, e;
	
	// examine all links
	for (e = 0; link && e < link.length; e++) {
	
		// does the link have use a class named "contact"
		if ((" "+link[e].className+" ").indexOf(" contact ") >= 0) {
		
			// get the obfuscated contact address
			contact = link[e].firstChild.nodeValue.toLowerCase() || "";
			
			// transform into real contact address
			contact = contact.replace(/dot/ig, ".");
			contact = contact.replace(/\(at\)/ig, "@");
			contact = contact.replace(/\s/g, "");
			
			// is contact valid?
			if (/^[^@]+@[a-z0-9]+([_\.\-]{0,1}[a-z0-9]+)*([\.]{1}[a-z0-9]+)+$/.test(contact)) {
			
				// change into a real mailto link
				link[e].href = "mailto:" + contact;
				link[e].firstChild.nodeValue = contact;
		
			}
		}
	}
}
window.onload = contactUnobsfuscate;
