// JavaScript Document for common functions.
function ignoreSpaces(string) {
	var temp = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(j = 0; j < splitstring.length; j++) {
		temp += splitstring[j];
	}	
	return temp;
} //end ignoreSpaces

//Trim functions.
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

/***********************************************************************************
 *	function	:	launchWindow
 *	purpose		:	Creates a new child window
 *	parameters	:	file - The url of the file to be loaded into the new window
 *					name - The name of the new child window
 *					winwidth - The width of the new window
 *					winheight - The height of the new window
 *
 *  NOTE: The name variable can contain NO spaces!
 **********************************************************************************/
function launchWindow(file, name, winwidth, winheight) {
	// Variable declaration
	var windowAttributes;

	// Set the window attributes
	windowAttributes = "width="+winwidth+",height="+winheight+",toolbar=no, " +
	"directories=no,location=no,menubar=no,resizable=yes,dependent=yes,scrollbars=yes";
	//alert(file + "  " + name.replace(/ /g, "_"));
	// Open the new window
	hwnd = window.open(file, name, windowAttributes);

	// Make sure the new window has the focus
	hwnd.focus()
}

function CheckDate(dates, field)  {
	var REG = /^([1][0-2]|[1-9]|[0][1-9])(\/|\-)([1-2][0-9]|[1-9]|[0][1-9]|[3][0-1])(\/|\-)[1-2][0-9][0-9][0-9]$/;

	if (dates.match(REG))  {
		null;
	} //end if
	else { 
		alert("The date you entered is invalid!\r\nCorrect Form: mm/dd/yyyy");
		//field.value = "";
		field.focus();
		return false;	
	}  //  end else
}  //  end CheckDate()

function CheckEmail(emails, field)  {
	var REG = /^\S+\@\S+\.\S+$/;
	if (trim(emails).match(REG)) {
		null;
	} //end if
	else { 
		alert("The e-mail address you entered is invalid!\r\nPlease try again.");
		//field.value = "";
		field.focus();
		return false;
	} //end if	
}  //  end CheckEmail()

function CheckPhoneInt(phone, field) {
	//For international phone numbers. Does very little checks
	var REG = /^[^a-zA-Z][^a-zA-Z]+\d\d$/;   
	if (phone.match(REG))  {
		null;
	} //end if
	else { 
		alert("The phone number you entered is invalid!\r\nPlease try again.");
		//field.value = "";
		field.focus();
		return false;
	} //end if	
}  //  end CheckPhoneUS()

function CheckPhoneUS(phone, field) {
	//for US phone numbers, invalid for international numbers
	var REG = /^\(*\d{3}\)*( *|-*)\d{3}-\d{4}$/;
	if (phone.match(REG))  {
		null;
	} //end if
	else { 
		alert("The phone number you entered is invalid!\r\nIt must be in the following format: (234) 234-2344.");
		//field.value = ""
		field.focus();
		return false;
	} //end if	
}  //  end CheckPhoneInt()

function CheckNumeric(number, field) {
	var REG = /^\d\d*$/;
	if (number.match(REG)) {
		null;
	} //end if
	else {
		alert("The value you entered is invalid!\r\nIt must be an integer i.e. (1..2..3..).");
		//field.value = "";
		field.focus();
		return false;
	} //end else
} //end CheckNumeric

function CheckCellNumeric(number, field) {
	var REG = /^\d\d*$/;
	if (number.match(REG)) {
		null;
	} //end if
	else {
		alert("The value you entered is invalid!\r\nIt must only include numbers (5402302390).");
		//field.value = "";
		field.focus();
		return false;
	} //end else
} //end CheckCellNumeric

function CheckZip(number, field) {
	var REG = /^(\d\d\d\d\d|\d\d\d\d\d-\d\d\d\d)$/;
	if (number.match(REG)) {
		null;
	} //end if
	else {
		alert("The Zip Code you entered is invalid!\r\nIt needs to be in the following format:\r\n24060 or 24060-1234");
		//field.value = "";
		field.focus();
		return false;
	} //end else
} //end CheckZip

function CheckFloat(number, field) {
	var REG = /^\d+(\.\d*$|\d*$)/; 
	if (number.match(REG))  {
		null;
	} //end if
	else { 
		alert("The value you entered is invalid! It must be a numeric value i.e. (2.51).\r\nAnd it must contain a leading zero if it is a decimal only value i.e. (0.34)");
		//field.value = ""
		field.focus();
		return false;
	} //end if	
}  //  end CheckMoney()

function CheckMoney(number, field) {
	var REG = /^\d+(.\d\d$|\d*$)/; 
	if (number.match(REG))  {
		null;
	} //end if
	else { 
		alert("The value you entered is invalid!\r\nIt must be a monetary value.\r\nNote: Do not enter the dollar symbol ($).");
		//field.value = ""
		field.focus();
		return false;
	} //end if	
}  //  end CheckMoney()

//Ensures the login infomration is only alpha-numeric, no special characters
function CheckLogin(number, field) {
	//Needs to be at least Five non-space characters and include at least 1 digit
	var REG = /^[\w]{3,}$/;
	if (number.match(REG)) {
		null;
	} //end if
	else { 
		alert("The value entered is invalid!\r\nIt must be contain only alpha-numeric characters (0-9, A-Z)\r\nand must be at least 3 characters long.");
		field.value = ""
		field.focus();
		return false;
	} //end if	
}  //  end CheckPassword()

//Ensures the password has at least one digit
function CheckPassword(number, field) {
	//Needs to be at least Five non-space characters and include at least 1 digit
	var REG = /^\S{5}\S*$/;
	var REG2 = /\d/;
	if ((number.match(REG)) && (number.match(REG2))) {
		null;
	} //end if
	else { 
		alert("The password you entered is invalid!\r\nIt must be at least 5 characters long\r\nand contain at least 1 digit.");
		field.value = ""
		field.focus();
		return false;
	} //end if	
}  //  end CheckPassword()

//Ensures the value is only made up of letters a hypen or a single quote
function CheckLetter(number, field) {
	//Old regular expression
	//var REG = /^[a-zA-Z]+$/;
	var REG = /^([a-zA-Z]+|[a-zA-Z]+[']?[-]?[ ]?[a-zA-Z]+)$/;
	if (number.match(REG)) {
		null;
	} //end if
	else { 
		alert("The value you entered is invalid!\r\nIt must contain only letters, a single hyphen (-) or a single quote (').\r\nThe single quote or hyphen may not be the first or last characters.");
		field.value = ""
		field.focus();
		return false;
	} //end if	
}

//Ensure there is no white space entered. (ie space, tab, etc)
function CheckSpace(number, field) {
	var REG = /^\S+$/;
	if (number.match(REG)) {
		null;
	} //end if
	else { 
		alert("The value you entered is invalid!\r\nIt cannot contain any Spaces or Whitespace characters.");
		field.value = ""
		field.focus();
		return false;
	} //end if	
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { 
	  // if the button group is an array (one button is not an array)
	  for (var i=0; i<buttonGroup.length; i++) {
		 if (buttonGroup[i].checked) {
			return i
		 }
	  }
   } 
   else {
	  if (buttonGroup.checked) { 
		return 0; 
	  } 
	  // if the one button is checked, return zero
   }
   
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
	// returns the value of the selected radio button or "" if no button is selected
	var i = getSelectedRadio(buttonGroup);
	if (i == -1) {
	   return "";
	} 
	else {
	   if (buttonGroup[i]) { 
		  // Make sure the button group is an array (not just one button)
		  return buttonGroup[i].value;
	   } 
	   else { 
		  // The button group is just the one button, and it is checked
		  return buttonGroup.value;
	   }
	}
} // Ends the "getSelectedRadioValue" function 

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
	  for (var i=0; i<buttonGroup.length; i++) {
		 if (buttonGroup[i].checked) {
			retArr.length = lastElement;
			retArr[lastElement] = i;
			lastElement++;
		 }
	  }
   } else { // There is only one check box (it's not an array)
	  if (buttonGroup.checked) { // if the one check box is checked
		 retArr.length = lastElement;
		 retArr[lastElement] = 0; // return zero as the only array value
	  }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
	  retArr.length = selectedItems.length;
	  for (var i=0; i<selectedItems.length; i++) {
		 if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
			retArr[i] = buttonGroup[selectedItems[i]].value;
		 } else { // It's not an array (there's just one check box and it's selected)
			retArr[i] = buttonGroup.value;// return that value
		 }
	  }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

//Finds a specific value in a select list and returns it's index, if not found returns -1
function findValue(SelectList, Item) {
	//Determines if the item has been found
	var found;
	found = false;
	
	//Now loop through the select list and find a value that matches the value we're looking for
	for (var counter=0; counter < SelectList.length; counter++) {
		//If found set found = true
		if (SelectList[counter].value == Item) {
			//If found set found = true and exit the loop
			found = true;
			//alert(counter);
			break;
		}
	} //End for loop
	
	//Determine if the index was found
	if (found == true) {
		return counter;
	}
	else {
		return -1;
	}	
}

//Allows a user to search a single dimensional array for a specific value.
Array.prototype.exists = function(search){
  for (var i=0; i<this.length; i++)
    if (this[i] == search) return true;
		
  return false;
} 


//Allows layers to be hidden/visible on an html page
function toggleLayer(whichLayer, layerValue) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		//style2.display = style2.display = layerValue;
		//style2.display = style2.display? "":"block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		//style2.display = style2.display? "":"block";
		//style2.display = style2.display = layerValue;
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		//style2.display = style2.display? "":"block";
		//style2.display = style2.display = layerValue;
	}
	style2.display = style2.display = layerValue;
}

//Used for AJAX calls
function GetXmlHttpObject() {
	var xmlHttp = null;
	try
	{
	// Firefox, Opera 8.0+, Safari
	xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
	// Internet Explorer
	try
		{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

