// Function list
// error(element, msg)					agumant passed is a form object and a string that is the text for the error message to be displayed in an alert
// isWhitespace()							arguemnt passed is a string. Returns true if it contains only whitespace characters
// isAlphanumeric()						argument passed is a string. Returns true if the string contains only alphanumeric characters
// isLetter()								argument passed is a char. Returns true if char is a-z or A-Z
// isPermitChar(c,permitChars)		arguments passed are a char and an array of "permitted" chars. Returns true if char is in the list of those permitted
// hasPermittedChars(s, okChars		arguments passed are a char and an array of "permitted"chars.  Returns true if char is in the list of those permitted
// isValidPassword(s)					argument passed is a string. Returns false if the string contains & ' "        
// hasSpecialCharOrDigit(s)			argument passed is a string. Returns true if the string contains a character that is either not alphanumeric or is a digit
// isEmail(s)								argument passed is a string. Returns true if string is of the form a@b.c
// parseEmail								parses email address delimited by semi-colons
// isValidEmail(s)						checks the email field to be sure that it is not whitespace and is of the correct form
// hasSpecialChars(s)					argument passed is a string. Returns true if string contains & " ' < >
// charsOutOfRange(s)					argument passed is a string. Returns true if character in the string are not in the ASCII range 32-125
// trim										argument passed is a string. Strips all whitespace and returns true.
// replace_bad_chars	
//argument passed is a string.
/*
function error(el,msg) {
	alert(msg);
	try { el.focus(); el.select(); } catch(e){;}
	return false;
}
*/
function error(el,msg) {
var currentError;
var previousClass;
if(msg){alert(msg)}
	try {
		if(currentError!=null) {
			currentError.className = previousClass;
		}
		if(el.className!=null) {
			currentClass = el.className;
			previousClass = currentClass;
			el.className = currentClass + ' inputError';
			el.focus();
			currentError = el;
		} else {
			el.className = 'inputError';
			el.focus();
		}
	}
	catch(e){;}
return false;
}

function clearErrorClass(el) {
var currentClass = el.className;
	if(currentClass.indexOf(' inputError') != -1) {
		el.className = '';
	}
}

var decimalPointDelimiter = '.';
var EmptyOK = true;
// Declaring required 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;
// Maximum no of digits in an international and national long-distance phone no. ex: +44-(0)1224-XXXX-XXXX.
var maxDigitsInIPhoneNumber = 15;
// valid white space characters
var whitespace = " \t\n\r";
function isWhitespace (s){
if((s == null) || (s.length == 0)) return true; for(var i = 0; i < s.length; i++){
       if (whitespace.indexOf(s.charAt(i)) == -1) return false;
  }
  return true;
}

function isLetter (c){return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) ) }
function isDigit (c){return ((c >= "0") && (c <= "9")) }
function isSpace (c){return (whitespace.indexOf(c) != -1)}
function isInteger(s) {
    for (var i = 0; i < s.length; i++) { 
        var c = s.charAt(i);
        if (!isDigit(c))
        return false;
    }
    return true;
}

function isAlphanumeric(s,permitChars) {  
    for (var i = 0; i < s.length; i++) { 
        var c = s.charAt(i);
        if(arguments.length == 2) {
        	if(!(isLetter(c)||isDigit(c)||isSpace(c)||isPermitChar(c,permitChars)))
        		return false;
        } else if (!(isLetter(c)||isDigit(c)||isSpace(c)))
        	return false;
    }
    return true;
}

function isPermitChar(c, permitChars) {
   var isPermitted = false;
   for( var i=0; i < permitChars.length; i++) {
      if(c == permitChars.charAt(i)) {
      	 isPermitted = true;
      	 break;
      }
   }
   return isPermitted;   
}

function hasPermittedChars(s,okChars) {
     var isValidStr = true;
     for(i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((isPermitChar(c,okChars) || isLetter(c) || isDigit(c)) && (isValidStr == true)){ 
           isValidStr = true;
        } 
        else{
           isValidStr = false; 
           break;   
        }
    }
  return isValidStr; 
}

function checkInternationalPhone(strPhone){
	var s = stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber && s.length <= maxDigitsInIPhoneNumber);
}

function isValidPassword(s) {   
	var i;
	var isValid = true;   
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(( c == "\"" ) || (c == "'" ) || ( c == "&")){
			isValid = false;
		}    
	}
	return isValid;
}

function hasSpecialCharOrDigit(s)
{
 	var i;
	var isValid = false;   
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(isDigit(c) || (!(isLetter(c)||isDigit(c)))){
          isValid = true;
        } 
	} 
    return isValid;
}

function isValidEmail(s) {
	//var emailRegxp = /[A-Za-z0-9._-]+@[^.]+\..+/;
	//var emailRegxp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	return emailRegxp.test(s);
}

function isValidURL(s) {
	//var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
    //var urlRegexp = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    //var urlRegexp = /^(http|https){1}:\/\/([\w]+)(.[\w]+)*(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/
    var urlRegexp = /^(http|https){1}:\/\/([\w]+)(.[\w]+)*(:[0-9]+)?\/?[\w#!:.?+=&%@!{}\-\/]*$/
    return urlRegexp.test(s);
}

function hasProtocol(s) {
	var urlRegxp = /^(http:\/\/|https:\/\/)/;
    return urlRegxp.test(s);
}

function isValidUSPhoneNumber(s) {
	var j = new RegExp();
    j.compile("[0-9]{3}-[0-9]{3}-[0-9]{4}");
    if (!j.test(s)) 
    	return false;
	return true;
}

function parseEmail(s){
    var emailArr = new Array();
    var j=0;
    emailArr[j] = '';
    for(i=0; i < s.length; i++){
       if (s.charAt(i) != ';'){
          emailArr[j] += s.charAt(i);
       }
       else{
          j++; 
          emailArr[j] = '';
       }
    }   
    return emailArr;
}

function hasSpecialCharsAmpOk(s)
{   
	var isDb = false;   
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(( c == "\"" ) || (c == "'" ) || (c == "<" ) || ( c == ">")){
			isDb = true;
		}    
	}
	return isDb;
}

function hasSpecialChars(s)
{   
	var isDb = false;   
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(( c == "\"" ) || (c == "'" ) || ( c == "&")|| (c == "<" ) || ( c == ">")){
			isDb = true;
		}    
	}
	return isDb;
}

function hasSpecialCharswComma(s)
{   
	var isDb = false;   
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(( c == "\"" ) || (c == "'" ) || ( c == "&")|| (c == "<" ) || ( c == ">") || (c == "," )){
			isDb = true;
		}    
	}
	return isDb;
}
function isCampaignNumberValid(s)
{   
	var isDb = true;   
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if(( c == "\"" ) || (c == "'" ) || ( c == "&")|| (c == "!" ) || ( c == "#") || (c == "$" ) || (c == "%" ) || ( c == "^")|| (c == "*" ) || ( c == ":") || (c == "|") || (c == "\\" ) || ( c == "/")|| (c == ">" ) || ( c == "<") || (c == "~") || ( c == ";") || (c == "=") || (c == "," )){
			isDb = false;
		} 
	}
	return isDb;
}
function hasWhitespace(s){
	for (var i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if((c == " ") || (c == "\\t") || (c == "\\n") || (c == "\\r")){
		   return true;	
		}	
	}
}
function charsOutOfRange(s){
    for(var j=0; j < s.length; j++)
    {
       var c = s.charCodeAt(j)
       if((c < 32) || (c > 126))
       {
          return true;
       } 
    }
    return false;
}

// isFloat (STRING s [, BOOLEAN emptyOK]) True if string s is an unsigned floating point (real) number. Also returns true for unsigned integers.
function isFloat (s, e_ok) { 
    var i;
	if(e_ok == null || e_ok == 'undefined')
	   e_ok = true;
    var seenDecimalPoint = false;
	if(isWhitespace(s) && (e_ok == true))
	  return true;
    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}
function isInteger (s, e_ok) { 
    var i;
	if(e_ok == null || e_ok == 'undefined')
	   e_ok = true;
    if(isWhitespace(s) && (e_ok == true))
	  return true;
    for (i = 0; i < s.length; i++) {   // Check that current character is number.
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter)) return false;
        else if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

function isWithinMaxMinLimit(input,max,min) {
	max = defaultValue(max,null);
	min = defaultValue(min,null);
	if(isFloat(input.value, false)) {
		if(max != null && parseFloat(input.value) > max) {
			return error(input,over_maximum_allowed + " [ " + max + " ]");
		}
		if(min != null && parseFloat(input.value) < min) {
			return error(input,below_minimum_allowed + " [ " + min + " ]");
		}
	}
	return true;
}

function trim(s) {
   var retval = "";
   if(s == null || s=="")
      return retval;
   if(s.indexOf(' ') < 0 &&
      s.indexOf('\t') < 0 &&
      s.indexOf('\r') < 0 &&
      s.indexOf('\n') < 0)
      return s;

   var start=0,end=s.length;
   for( var i=0; i<s.length; i++ ) {
      if(' \t\r\n'.indexOf(s.charAt(i))>-1)
         start++;
      else
         break;
   }
   for( var i=s.length-1; i>0; i-- ) {
      if(end>=start&&' \t\r\n'.indexOf(s.charAt(i))>-1)
         end--;
      else
         break;
   }
   return s.substring(start,end);
}

function charInString (c, s){ 
  for (i = 0; i < s.length; i++){
    if (s.charAt(i) == c) return true;
  }
  return false
}
function rTrim(s){
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}
function lTrim(s) {
var i = 0;
	while ( (i < s.length) && charInString(s.charAt(i), whitespace) )
		i++;
	return s.substring(i, s.length);
}
function trim(s) {
      return lTrim(rTrim(s));
}

function _rpl(s,b,n) {
     var idx = (""+s).indexOf(b);
     var retval='';
     while(idx!=-1){
        retval+=s.substring(0,idx)+n;
        s=s.substring(idx+1,s.length);
        idx=s.indexOf(b);
     } if(retval) {
        return retval + s.substring(0,s.length);
     } else {
        return s;
     }
}
function replace_bad_chars(s) {
   return _rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(_rpl(
   s,' ','+'),')',''),'(',''),'*',''),'&',''),'^',''),'$',''),'#',''),'@',''),'!',''),"'",''),'!',''),'"',''),'\/','\\');}

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;
}
// Removes all whitespace characters from s.Global variable. var = whitespace defines which characters are considered whitespace.
function stripWhitespace (s) {
return stripCharsInBag (s, whitespace)
}

function stripBadChs(id,status) {
var bad = 0
	for (i = 0; i < id.value.length; i++) {
		ch = id.value.substring(i, i + 1);
		if ( status == "letters or numbers" ) { if ( (ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) && (ch < "0" || "9" < ch) ) { bad = 1; } }
		if ( status == "letters" ) { if ( (ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch) ) { bad = 1; } }
		if ( status == "dollar value" ) { if ((ch == "$") || (ch == ",")) { bad = 1 } }
		if ( status == "numbers" ) { if (ch < "0" || "9" < ch) { bad = 1 } }
		if ( status == "," ) { if (ch == ",") { bad = 1 } }
		if ( bad == 1 ) {	
			id.value = id.value.substring(0,i)+id.value.substring(i+1,id.value.length)
			i=i-1
			bad = 0
		}
	}
}