//Check Email Format function
function CheckEmailFormat(sEmail) {
//verifies that email format is x@y.z, a@b.c.d, etc.
	var re = new RegExp("^[a-zA-Z0-9\-\.\_\']+@([a-zA-Z0-9\-]+[\.])+[a-zA-Z]{2,3}$");
	var re2 = new RegExp("xp[\_]|[\.][\.]|[\-][\-]|[0][x]","gi");
	if (sEmail=='' || (re.test(sEmail) && (sEmail.length <=80) && !re2.test(sEmail)))
		return true;
	else
		return false;
}

//Check Zip Code function
//verifies that the string matches US/Canada Zip Code format.  
//If not, it would check if the string has just numerals
function IsZipCode(sString) {
	var re_canada = new RegExp("^[A-Z|a-z][0-9][A-Z|a-z][ ][0-9][A-Z|a-z][0-9]$"); //canadian postal format
	var re_us = new RegExp("^[0-9]{5}$"); //us postal format
	var re = new RegExp("^[0-9]+$");
	return (sString=='') || re_canada.test(sString) || re_us.test(sString) || re.test(sString);
}

//Check Date Format function
//verifies that the string only contain valid date format
// either dd/mm/yy or dd/mm/yyyy should be specified
function CheckDateFormat(sString) {
	//modified this regexp as below to fix defect #3161
	//var re_year2digit = new RegExp("^[0-1]?[0-2][/\][0-3]?[0-9][/\][0-9]{2}$");
	//var re_year4digit = new RegExp("^[0-1]?[0-2][/\][0-3]?[0-9][/\][0-2][0-9]{3}$");
	//return re_year2digit.test(sString) || re_year4digit.test(sString);
	//var re_datecheck = new RegExp("^([0]?[1-9]|[1][0-2])[\/]([0]?[1-9]|[1-2][0-9]|[3][0-1])[\/]([1-2][0-9][0-9][0-9]|[0-9][1-9]|[1-9][0])$")
	var re_datecheck = new RegExp("^([0]?[1-9]|[1][0-2])[\/]([0]?[1-9]|[1-2][0-9]|[3][0-1])[\/]([1-2][0-9][0-9][0-9]|[0-9][0-9])$")

	return re_datecheck.test(sString);
}

//Check Number Input Format function
//verifies that the string only contains numbers and letters (A-Z)
function IsAlphaNum(sString) {
	var re = new RegExp("^[a-zA-Z0-9]+$");
	return (sString=='') || re.test(sString);
}

//Check Number and Letters Input Format function
//verifies that the string only contains numbers and letters, including letter-like upper-ASCII characters
function IsAlphaNumExt (sString){
var re = new RegExp("^[a-zA-Z0-9À-ÿŠOEŽšoežŸ]+$");
return (sString=='') || re.test(sString);
}

//Check Integer Number Input Format function
//verifies that the expression is a valid integer
function IsInteger(nInteger) {
	return (nInteger=='' || nInteger==parseInt(nInteger,10));
}

//Check Phone Number Format function
//verifies that the expression is a valid phone number format
function IsPhoneNumber(sString) {
//verifies that the expression is a valid phone number format
	//var re = new RegExp("^([\(][0-9]+[\)] ?)*([0-9]+[ \-\.])*[0-9]+$");  -- old reg exp.
	//var re = new RegExp("^([\(][0-9]+[\)][ ]*)*([0-9]+[ ]*[\-\.]?[ ]*)*([A-Za-z\*]*[ ]*)?([0-9]+)?$");  -- modified this regexp as below to fix defect #3151
	var re = new RegExp("^([\(]?[\+]?[0-9]+[\)]?[\-\.]?[ ]*)+[A-Za-z\-\.\:\* ]*([\(]?[0-9 ]+[\)]?[\.]?)?$");
	return (sString=='') || re.test(sString);
}

//Check Invalid SQL String Format function
//verifies that the string contains no prohibited SQL clauses as provided by D&T
function IsProhibString(sString) {
	if (CheckEmailFormat(sString)) {
	return false;
	}
	var re = new RegExp("[\*]|[\+]|[\?]|[\_]|[\|]|[\[]|[\]]|[\/][\/]|[\@][\@]|[\.][\.]|[\'][\;]|[\)][\)]|[\(][\(]|xp[\_]|[\<]|[\>]|[\%]|[\=]|[\-][\-]|[\=][\/]|[\:][\:]|[0][x]","gi");
	var re2 = /\bexec\b.|\:\\/gi
	return (re.test(sString) || re2.test(sString));
}

function IsChecked(field,Name) {
//verifies whether the radio option has been checked, if not return the field name.
	var radioSelected = false;
	for (i = 0;  i < field.length ;  i++)
	{
		if (field[i].checked)
			radioSelected = true;
	}
	if (!radioSelected)
	{
		return ('- ' + Name+'\n');
	}
	return ('');;
}

function Localize(string) {
    return string;
}

function trim(inputString) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function CheckName(sText) {
//verifies that name format is limited
	//var re = new RegExp("[a-zA-Z]+[a-zA-Z\.\']*[ |']*[a-zA-Z\.]*$");
	var re = new RegExp("^[A-Za-zÀ-ÿŠŒŽšœžŸ]+(([\-\`\' ]|[\.\,][ ]?)[A-Za-zÀ-ÿŠŒŽšœžŸ]+)*[\.]?$")
	return re.test(sText);
}

function CheckCompany(sText){
	var re = new RegExp("^[a-zA-Z0-9À-ÿŠŒŽšœžŸ\#]+[a-zA-Z0-9À-ÿŠŒŽšœžŸ\+\-\.& \!\#\@\/]+$");
	if(sText =="0x") 
		return false; 
	else 
		return (re.test(sText));
}

function CheckTitle(sText){
	var re = new RegExp("^[a-zA-ZÀ-ÿŠŒŽšœžŸ]+[a-zA-ZÀ-ÿŠŒŽšœžŸ\ ]*$");
	return re.test(sText);
}

function CheckNumber(sText){//Fix for Defect #3159
	var re = new RegExp("^[0-9]+$");
	return re.test(sText);
}

function IsNumber(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsIPNumber(sText)
{
   var ValidChars = "0123456789,";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
