// JavaScript Document
// 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;
}

// 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;
}

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

// 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 used to check email and display message
function isValidEmail(fieldname, msg) {
	if (!validateEmail(document.getElementById(fieldname).value)) {
		alert(msg);
		return false;
	}
	return true;
}

// Function to validate a comma delimited list of email addresses
function validateEmailAddressList(emailAddressList, message){
	var pos = 0;
	var i = 0;
	var origin = 0;
	originalString = stringReplace(document.getElementById(emailAddressList).value, " ", "");
	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 to check whether an email address is well formed
// Validates the email entered.
function validateEmailAddress(inputValue, message){
   // The invalid characters that should not be used in an email address
   var invalidChars = " /:,;"; 
   var emailAddress = inputValue;
   var atPosition = emailAddress.indexOf("@",1);
   var periodPosition = emailAddress.indexOf(".",atPosition);
   
   // 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){
	     alert(message);
		 return false;
	  }
   }
   // Checks for the @
   if (atPosition == -1){ 
      alert(message);
	  return false;
   }
   // Makes sure there is one @
   if (emailAddress.indexOf("@",atPosition + 1) > -1){ 
      alert(message);
	  return false;
   }
   // Makes sure there is a period after the @ 
   if (periodPosition == -1){ 
      alert(message);
	  return false;
   }
   // Makes sure there is at least 2 characters after the period
   if ((periodPosition + 3) > emailAddress.length){ 
      alert(message);
	  return false;
   }
   
   return true;
}

// Function that replaces alll instances on a value in the string
function stringReplace(originalString, findText, replaceText) {
	var pos = 0;
	pos = originalString.indexOf(findText);
	while (pos != -1) {
		preString = originalString.substring(0,pos);
		postString = originalString.substring(pos+1, originalString.length);
		originalString = preString + replaceText + postString;
		pos = originalString.indexOf(findText);
	}
	return originalString;
}
// open a window to display a list so that it can be printed
// open window
function openPrintList(redundantcolumns, pagetitle) { 
  fileName = "../core/printerfriendly.php?columncheck=" +redundantcolumns + "&printoption=" + document.getElementById("printselect").value + "&pagetitle="+ pagetitle;
  // To specify the window characteristics edit the "features" variable below:
  // width - width of the window
  // height - height of the window
  // scrollbar - "yes" for scrollbars, "no" for no scrollbars
  // left - number of pixels from left of screen
  // top - number of pixels from top of screen
  features = "width=600,height=400,left=100,top=130,resizable=1, scrollbars=1,alwaysRaised=1";
  printwindow = window.open(fileName,"printWin", features);
  printwindow.focus();   
}

function openDetailedPrintList(redundantcolumns, pagetitle) { 
  fileName = "../core/detailedprinterfriendly.php?columncheck=" +redundantcolumns + "&printoption=" + document.getElementById("printselect").value + "&pagetitle="+ pagetitle;
  // To specify the window characteristics edit the "features" variable below:
  // width - width of the window
  // height - height of the window
  // scrollbar - "yes" for scrollbars, "no" for no scrollbars
  // left - number of pixels from left of screen
  // top - number of pixels from top of screen
  features = "width=600,height=400,left=100,top=130,resizable=1, scrollbars=1,alwaysRaised=1";
  printwindow = window.open(fileName,"printWin", features);
  printwindow.focus();   
}

function downloadDetailedList(pagetitle) { 
  fileName = "../core/downloaddetailedbrowse.php?pagetitle="+ pagetitle;
  // To specify the window characteristics edit the "features" variable below:
  // width - width of the window
  // height - height of the window
  // scrollbar - "yes" for scrollbars, "no" for no scrollbars
  // left - number of pixels from left of screen
  // top - number of pixels from top of screen
  features = "width=600,height=400,left=100,top=130,resizable=1, scrollbars=1,alwaysRaised=1";
  printwindow = window.open(fileName,"printWin", features);
  printwindow.focus();   
}


