//*******************************************************************************************
//* Desc: Standard Javascript include file that contains functions for a variety of
//*       validation, such as isnumeric and trim for removing spaces.
//* LastUpdated: JP, 10/2002 Made it standard :)
//* Version: 1.00
//*
//********************************************************************************************

	// Declaring required phone number variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;

	 // CONSTANTS used for number format
	 var separator = ",";  // use comma as 000's separator
	 var decpoint = ".";  // use period as decimal point
	 var percent = "%";
	 var currency = "$";  // use dollar sign for currency
 	
	function isInteger(s)
	{   var i;
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	}
	
	function stripCharsInBag(s, bag)
	{   var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++)
		{   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	}
	
	function checkInternationalPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}
	
	function ValidatePhone(ph){
		var Phone=ph
		if ((Phone==null)||(Phone=="")){
			return false
		}
		if (checkInternationalPhone(Phone)==false){
			return false
		}
		return true
	 }
		// 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;
		}
		
		// 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 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;
		}
				
		// Check that a US zip code is valid
		function isValidZipcode(zipcode) {
			//zipcode = removeSpaces(zipcode);
			//if (!(zipcode.length == 5) || !isNumeric(zipcode)) return false;
			//return true;
			var valid = "0123456789-";
			var hyphencount = 0;
			
			if (zipcode.length!=5 && zipcode.length!=10) {
			return false;
			}
			for (var i=0; i < zipcode.length; i++) {
			temp = "" + zipcode.substring(i, i+1);
			if (temp == "-") hyphencount++;
				if (valid.indexOf(temp) == "-1") {
					return false;
				}
				if ((hyphencount > 1) || ((zipcode.length==10) && ""+zipcode.charAt(5)!="-")) {
					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(/^\w\d\w\d\w\d$/) != -1) return true;
				else if (postalcode.length == 7 && postalcode.search(/^\w\d\w\-d\w\d$/) != -1) return true;
				else return false;
			}
			return true;
		}
		
		//Returns true if it is good or false if it is bad
		function email_val(emailStr){	
			// *********Crazy complex E-mail validation routine**************************
			if (emailStr != null && emailStr != "") {
				a = emailStr.lastIndexOf("@");
				b = emailStr.lastIndexOf(".");
				c = emailStr.indexOf(":");
				d = emailStr.indexOf("/");
				e = emailStr.substring(0,a);
				f = e.indexOf("@");
				g = emailStr.substring(a+1,emailStr.length);
				h = g.indexOf("[");
				i = g.indexOf("]");
				j = g.indexOf("<");
				k = g.indexOf(">");
				l = emailStr.substring(a+1,b);
				m = emailStr.substring(b+1,emailStr.length);
				n = emailStr.substring(0,a);
				o = 0;
				if (a > b) {o++};
				if (c != -1) {o++};
				if (d != -1) {o++};
				if (f != -1) {o++};
				if (h != -1) {o++};
				if (i != -1) {o++};
				if (j != -1) {o++};
				if (k != -1) {o++};
				if (l.length < 3) {o++};
				if (m.length < 1) {o++};
				if (n.length < 1) {o++};
				if (o == 0) {
					return true;
				}
				else
					return false;
			}
			else {
				return false;
			}
		}

		// *********Trim Functions**************************				
		function ltrim ( s )
		{
			return s.replace( /^\s*/, "" );
		}		
		function rtrim ( s )
		{
			return s.replace( /\s*$/, "" );
		}
		function trim ( s )
		{
			return rtrim(ltrim(s));
		}


		// limit the amount of chars that go to a textarea box
		// calling function tht allows 40 characters could be used as follows:
		// <textarea cols="20" rows="2" value="" name="test" id="test" onkeyup="textLimit(this.form.test, 40, 'msg here');"></textarea>
		function textLimit(field, maxlen, msg) 
		{
			if (field.value.length > maxlen) {
				field.value = field.value.substring(0, maxlen);
				alert(msg);
			} 
		}


		function removeCurrency( strValue ) {
			/************************************************
			DESCRIPTION: Removes currency formatting from 
			  source string.
			  
			PARAMETERS: 
			  strValue - Source string from which currency formatting
				 will be removed;
			
			RETURNS: Source string with commas removed.
			*************************************************/
		  var objRegExp = /\(/;
		  var strMinus = '';
		 
		  //check if negative
		  if(objRegExp.test(strValue)){
			strMinus = '-';
		  }
		  
		  objRegExp = /\)|\(|[,]/g;
		  strValue = strValue.replace(objRegExp,'');
		  if(strValue.indexOf('$') >= 0){
			strValue = strValue.substring(1, strValue.length);
		  }
		  return strMinus + strValue;
		}
		
		
		// Format functions **********************************************************************
		  function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
			if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");
		
			if (number - 0 != number) return null;  // if number is NaN return null
			var useSeparator = format.indexOf(separator) != -1;  // use separators in number
			var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
			var useCurrency = format.indexOf(currency) != -1;  // use currency format
			var isNegative = (number < 0);
			number = Math.abs (number);
			if (usePercent) number *= 100;
			format = strip(format, separator + percent + currency);  // remove key characters
			number = "" + number;  // convert number input to string
		
			 // split input value into LHS and RHS using decpoint as divider
			var dec = number.indexOf(decpoint) != -1;
			var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
			var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";
		
			 // split format string into LHS and RHS using decpoint as divider
			dec = format.indexOf(decpoint) != -1;
			var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
			var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";
		
			 // adjust decimal places by cropping or adding zeros to LHS of number
			if (srightEnd.length < nrightEnd.length) {
				  var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
				  nrightEnd = nrightEnd.substring(0, srightEnd.length);
				  if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up
			
				  while (srightEnd.length > nrightEnd.length) {
					nrightEnd = "0" + nrightEnd;
				  }
			
				  if (srightEnd.length < nrightEnd.length) {
					nrightEnd = nrightEnd.substring(1);
					nleftEnd = (nleftEnd - 0) + 1;
				  }
			} else {
				  for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
					if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
					else break;
				  }
			}
		
			 // adjust leading zeros
			sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
			while (sleftEnd.length > nleftEnd.length) {
			  nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
			}
		
			if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
			var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
			output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
			if (isNegative) {
			  output = (useCurrency) ? "(" + output + ")" : "-" + output;
			}
			return output;
		  }
		
		  function strip(input, chars) {  // strip all characters in 'chars' from input
			var output = "";  // initialise output string
			for (var i=0; i < input.length; i++)
			  if (chars.indexOf(input.charAt(i)) == -1)
				output += input.charAt(i);
			return output;
		  }
		
		  function separate(input, separator) {  // format input using 'separator' to mark 000's
			input = "" + input;
			var output = "";  // initialise output string
			for (var i=0; i < input.length; i++) {
			  if (i != 0 && (input.length - i) % 3 == 0) output += separator;
			  output += input.charAt(i);
			}
			return output;
		  }		
		// END Format functions **********************************************************************		

		// -->
		
