function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		return false;
	} else {
		return true;
	}
}

function HideAllErrors() {
	$$('span.requiredField').each(function(item, index){
		item.style.display = 'none';
	});
}


function CheckFocus(obj_set)
{
	if(has_focus == false)
	{
		obj_set.focus();
		object_to_focus = obj_set;
		has_focus = true;
	}
}

function AfterMessage()
{
	object_to_focus.focus();
}

function AppendError(error, Text)
{
	if(error != "")
		error += "\n"

	return error += Text;
}
	
function OnlyNumeric(evt) 
{    
	var charCode = 0;
	evt = (window.event)? event : evt;
	charCode = (evt.keyCode)? evt.keyCode: evt.charCode;
	if (charCode == 8)
		return true;
	if (charCode > 31 && (charCode < 48 || charCode > 57))       
		return false;    
	return true;
}

function OnlyFloat(evt, okdecimal) 
{    
	var rtn = true;	
	var e = (window.event)? event : evt;
	charCode = (evt.keyCode)? evt.keyCode: evt.charCode; 

	if (charCode >= 48 && charCode <= 57)     
	{
		return true;
	}
	else if(charCode == 46 && okdecimal == false)
	{
		return false;
	}
	else if (charCode == 46 || charCode == 44 || charCode == 8)
	{
		return true;   
	}	
	return false;
}

function OnlyZip(evt, value) 
{    
	var rtn = true;
	var e = (window.event)? event : evt;
	charCode = (evt.keyCode)? evt.keyCode: evt.charCode;  
	if (charCode >= 48 && charCode <= 57)     
	{
		return true;
	}
	else if(charCode == 189 )
	{
		return true;    
	}
	
	if(charCode == 45 && value.trim().length == 5)
	{
		return true;
	}
	return false;
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		return false;
	} else {
		return true;
	}
}

function isValidNumber(inpString)
{
	var IndexC = inpString.lastIndexOf(",");
	var IndexD = inpString.lastIndexOf(".");
	if(IndexD == -1)
	{
		return /^[-+]?\d+(\.\d+)?$/.test(inpString.ReplaceAll(",",""));
	}
	if(IndexD != -1 || IndexC != -1)
	{
		if(IndexD == -1 && IndexC > 1)
		{
			return /^[-+]?\d+(\.\d+)?$/.test(inpString.ReplaceAll(",",""));	
		}
		else if(IndexD < IndexC && IndexD != -1)
		{
			return false;
		}
		else
		{
			return /^[-+]?\d+(\.\d+)?$/.test(inpString.ReplaceAll(",",""));	
		}
	}	
}

function IsUNumeric(input)
{   
	var temp = input.replace(/\,/g,'');  
	return (temp - 0) == temp && temp.length > 0;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
/*
<input type="text" value="" onMouseDown="whichButton(event)"  onKeyDown="return noCTRL(event)"/>
*/
function whichButton(event) {
	if (event.button==2)//RIGHT CLICK
	{
	  return false;
	}
}

function noCTRL(e) {
	var code = (document.all) ? event.keyCode:e.which;

	var msg = "Sorry, this functionality is disabled.";
	if (parseInt(code)==17) //CTRL
	{
		//alert(msg);
		window.event.returnValue = false;
	}
}


// Remove all spaces from a string
function removeSpaces(string) {
   var newString = '';
   for (var i = 0; i < string.length; i++) {
      if (string.charAt(i) != ' ') newString += string.charAt(i);
   }
   return newString;
}

// Check that a US or Canadian phone number is valid
function isValidUSPhoneNumber(areaCode, prefixNumber, suffixNumber) {
   if (arguments.length == 1) {
      var phoneNumber = arguments[0];
      phoneNumber = phoneNumber.replace(/\D+/g, '');
      var length = phoneNumber.length;
      if (phoneNumber.length >= 7) {
         var areaCode = phoneNumber.substring(0, length-7);
         var prefixNumber = phoneNumber.substring(length-7, length-4);
         var suffixNumber = phoneNumber.substring(length-4);
      }
      else return false;
   }
   else if (arguments.length == 3) {
      var areaCode = arguments[0];
      var prefixNumber = arguments[1];
      var suffixNumber = arguments[2];
   }
   else return true;

   if (areaCode.length != 3 || !isNumeric(areaCode) || prefixNumber.length != 3 || !isNumeric(prefixNumber) || suffixNumber.length != 4 || !isNumeric(suffixNumber)) return false;
   return true;
}

// Check that a US zip code is valid
function isValidZipcode(zipcode) {
   zipcode = removeSpaces(zipcode);
   if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10)) return false;
   if ((zipcode.length == 5 || zipcode.length == 9) && !isNumeric(zipcode)) return false;
   if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
   return true;
}

// Check that a Canadian postal code is valid
function isValidPostalcode(postalcode) {
   if (postalcode.search) {
      postalcode = removeSpaces(postalcode);
      if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
      else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]-\d[a-zA-Z]\d$/) != -1) return true;
      else return false;
   }
   return true;
}

// Remove leading and trailing whitespace from a string
function trimWhitespace(string) {
   var newString  = '';
   var substring  = '';
   beginningFound = false;
   
   // copy characters over to a new string
   // retain whitespace characters if they are between other characters
   for (var i = 0; i < string.length; i++) {
      
      // copy non-whitespace characters
      if (string.charAt(i) != ' ' && string.charCodeAt(i) != 9) {
         
         // if the temporary string contains some whitespace characters, copy them first
         if (substring != '') {
            newString += substring;
            substring = '';
         }
         newString += string.charAt(i);
         if (beginningFound == false) beginningFound = true;
      }
      
      // hold whitespace characters in a temporary string if they follow a non-whitespace character
      else if (beginningFound == true) substring += string.charAt(i);
   }
   return newString;
}

// Check that a string contains only letters
function isAlphabetic(string, ignoreWhiteSpace) {
   if (string.search) {
      if ((ignoreWhiteSpace && string.search(/[^a-zA-Z\s]/) != -1) || (!ignoreWhiteSpace && string.search(/[^a-zA-Z]/) != -1)) return false;
   }
   return true;
}

// Check that a string contains only numbers
function isNumeric(string, ignoreWhiteSpace) {
   if (string.search) {
      if ((ignoreWhiteSpace && string.search(/[^\d\s]/) != -1) || (!ignoreWhiteSpace && string.search(/\D/) != -1)) return false;
   }
   return true;
}

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) {
   if (string.replace) {
      string.replace(/[<>\"\'%;\)\(&\+]/, '');
   }
   return string;
}

function openWindow(url)
{
	var height = 300;
	var width = 800;
	var top = (screen.height-height)/2;
	var left = (screen.width-width)/2;
	var newwindow;
	var options = "dialogHeight:" + height + "px;dialogWidth:" + width + "px;dialogTop:" + top + "px;dialogLeft:" + left + "px";
	if (false && window.showModalDialog)
	{
		window.showModalDialog(url,"name",options);
	}
	else
	{
		newwindow = window.open(url,"myname","height=" + height + ",width=" + width + ",top=" + top + ",left=" +
			left + ",toptoolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");
		if (window.focus)
		{
			newwindow.focus()
		}
	}
}


function MyPopUpWin(MyWidth, height, window_url) {
	var iMyWidth;
	var iMyHeight;
	//half the screen width minus half the new window width (plus 5 pixel borders).
	iMyWidth = (window.screen.width - MyWidth )/2;
	//half the screen height minus half the new window height (plus title and status bars).
	iMyHeight = (window.screen.height - height )/2;
	//Open the window.
	var win2 = window.open(window_url,"Window2","status=no,height=" + height + ",width=" + MyWidth + ",resizable=yes,left=" + iMyWidth + ",top=" + iMyHeight + ",screenX=" + iMyWidth + ",screenY=" + iMyHeight + ",toolbar=no,menubar=no,scrollbars=no,location=no,directories=no");
	win2.focus();
}
