var whitespace = " \t\n\r";
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 10;	//99999-9999
var chrsInPostalCode = 6;

function isEmpty(s) {
	var x = Trim(s);
	return ((x == null) || (x.length == 0));
}

function stripWhitespace (s) {
	return stripChrs(s, whitespace);
}

function isWhitespace (s) {
	var i;
	if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripChrs(s, bag) {
    if (typeof(bag)=="undefined") {var bar = whitespace;}
    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 Trim(string) {
	var newStr = "";
    newStr = RTrim(LTrim(string));
    return (newStr);	
}

function LTrim(string) {
	var oldStr = string;
	var newStr = "";
	var notDone = true;
	for (i = 0;  i < oldStr.length;  i++) {
		ch = oldStr.charAt(i);
		if (ch==" " && notDone) {
		// remove spaces 
		} else {
			newStr = newStr + ch;
			notDone = false;
		}
	}
	return (newStr);	
}

function RTrim(string) {
	var oldStr = string;
	var newStr = "";
	var notDone = true;
	for (i = oldStr.length-1;  i >= 0;  i--) {
		ch = oldStr.charAt(i);
		if (ch==" " && notDone) {
			// remove spaces 
		} else {
			newStr = ch + newStr;
			notDone = false;
		}
	}
	return (newStr);	
}

strNameInvalid ="!@#$%^&*()_+|\={}][:;<>?/`~1234567890"
strJobtitleInvalid = "!@#$%^&*()_+|\={}][:;<>?`~"
strCompanyInvalid = "!@#$%^*()_+|\={}][:;<>?/`~"

strNamevalid ="' , . -"
strJobtitlevalid ="/ ' , . -1234567890"
strComapnyvalid ="& ' , . -1234567890"

function isValidName (s,strType) {
    var i;
	var p;
    var strInvalid;
    var strValid;
    var intValidcount;
    var intAlphaNumeric;
    var validChar;
    
    if (strType == 'firstname' || strType == 'lastname'){strInvalid = strNameInvalid; strValid = strNamevalid;}
    if (strType == 'jobtitle'){strInvalid = strJobtitleInvalid; strValid = strJobtitlevalid;}
    if (strType == 'company'){strInvalid = strCompanyInvalid; strValid = strComapnyvalid;}
        
    if (isWhitespace(s)) {
		return false;
	}
	
	 intValidcount =0;
	 intAlphaNumeric = 0;
	 
    for (i = 0; i < s.length; i++){   
        for (p = 0; p < strInvalid.length; p++){
			if (s.charAt(i) == strInvalid.charAt(p)||s.charAt(i) == '"'||s.charAt(i) == '\\') {
				return false;
			}
        }
        validChar = false;
        
        for (p = 0; p < strValid.length; p++){
			if (s.charAt(i) == strValid.charAt(p)) {
			    intValidcount++;
			    validChar = true;
			}
		}
        if (!validChar || isInteger(s.charAt(i))){intAlphaNumeric++;}
        
    }
   if (intValidcount == s.length){return false;}
      
   for (p = 0; p < strValid.length; p++){
	 if (strType == 'firstname' && s.length == 1){
		if (s.charAt(0) == strValid.charAt(p)){return false;}
	 }
	 
	 if (strType == 'lastname' && s.length == 2){
		if (s.charAt(0) == strValid.charAt(p) || s.charAt(1) == strValid.charAt(p)){return false;}
	 }
	 
  }
	if (strType == 'firstname' && s.length > 1 && intAlphaNumeric < 1){return false;}
	if (strType == 'lastname' && s.length > 2 && intAlphaNumeric < 2){return false;}
	if (strType == 'jobtitle' && s.length >= 2 && intAlphaNumeric < 2){return false;}
  	if (strType == 'company' && s.length >= 2 && intAlphaNumeric < 2){return false;}

    return true;
}
// checks fromat for "99999" or "99999-9999"
function isZIPCode(s){  
   if (isEmpty(s)) return false;
   if (s.length == digitsInZIPCode2 && s.charAt(5) !="-")  return false;
   return (isInteger(stripChrs(s,"-")) && 
           ((s.length == digitsInZIPCode1) ||
            (s.length == digitsInZIPCode2)))
}
function isEmail(s) {
    if (isWhitespace(s)) return false;
    if (s.indexOf(" ") != -1 || s.indexOf(",") != -1) return false;
    var i = s.indexOf("@");
    // has to be at least one @ symble
    if (i == -1) return false;
    else i += 2;
    // but not more then one @
    if (s.indexOf("@",i) != -1) return false;
    if (s.indexOf(".",i+1) == -1) return false;
    var nLastdot = s.lastIndexOf(".");
    if (nLastdot == -1 || nLastdot >= s.length-1) return false;
    
    return true;
}
// checks format for "999-999-9999"
function isPhoneValid(phone){
	if (isEmpty(phone)) return false;
	if (!isDigit(phone.charAt(0)) || 
	   !isDigit(phone.charAt(1)) || !isDigit(phone.charAt(2)) || 
	   !isDigit(phone.charAt(4)) || !isDigit(phone.charAt(5)) || 
	   !isDigit(phone.charAt(6)) || !isDigit(phone.charAt(8)) || 
	   !isDigit(phone.charAt(9)) || !isDigit(phone.charAt(10)) || 
	   !isDigit(phone.charAt(11)) || phone.charAt(3)!="-"||phone.charAt(7)!="-") {
		return false;
	}
	return true;
}
//checks format for "A9A9A9" after stripping out spaces
function isPostalCode(s) {
	if (isEmpty(s)) return false;
 var c = stripChrs(s," ");
 if (c.length != chrsInPostalCode) return false;
 if (!isChar(c.charAt(0)) || !isDigit(c.charAt(1)) ||
     !isChar(c.charAt(2)) || !isDigit(c.charAt(3)) || 
     !isChar(c.charAt(4)) || !isDigit(c.charAt(5))) {
   return false;
 }
 return true;
}

function isValidDate(cDate,tMin,tMax) {
	var re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/
	if (re.test(cDate) == false ) {
		return false;
	}

	var n1Ind	= 0;
	var cMonth	= "";
	var n2Ind	= 0;
	var cDay	= "";
	var n3Ind	= 0;
	var cYear	= "";
	var nMin	= tMin;
	var nMax	= tMax;
	
	n1Ind = cDate.indexOf("/",0);
	cMonth = cDate.substring(0,n1Ind);
	
	n2Ind = cDate.indexOf("/",n1Ind+1);
	cDay = cDate.substring(n1Ind+1,n2Ind);
	
	n3Ind = n2Ind+1;
	cYear = cDate.substring(n2Ind+1);

	if (!isValidYear(parseInt(cYear,10),nMin,nMax)) {
		return false;
	}
	return isValiDate(parseInt(cMonth,10),parseInt(cDay,10),parseInt(cYear,10));
}
function isMonth(n) {
	/*if (!isInteger(n)) {
		return false;
	}*/
	if (parseInt(n,10)<1 || parseInt(n,10)>12){
		return false;
	}
	return true;
}
function isValiDate(nMth,nDay,nYr) {
	if (isMonth(nMth) == false || daysInMonth(nMth,nYr) < nDay || nYr < 1900)
		return false;
	else
		return true;
}
/* retuns the number of days in a month */
function daysInMonth(nMonth, nYear) {
	var iDays;
	switch(nMonth) {
	case (4):	//"April"
	case (6):	//"June"
	case (9):	//"September"
	case (11): 	//"November"
		iDays = 30;
		break;
	case (2):	//"February"
		//leap year is divisible by 4 but not by 100 unless is divisible by 400
		if (nYear % 4 == 0 && (nYear % 100 != 0 || nYear % 400 == 0 ))
			iDays = 29;
		else
			iDays = 28;
		break;
	default:
		/* "Januay" "March" "May" "July" *
	 	 * "August" "October" "December" */
		iDays = 31;
		break;
	}
	return iDays;
}

function isDay(d,m) {
	switch (m) {
		case "2":
		case "02":
			var nMax = 28;
			break;
		case "4":
		case "04":
		case "6":
		case "06":
		case "9":
		case "09":
		case "11":
			var nMax = 30;
			break;
		default:
			var nMax = 31;
	}
	
	if (!isInteger(d)) {
		return false;
	}
	if (parseInt(d,10)<1 || parseInt(d,10)>nMax){
		return false;
	}
	return true;
}
function isValidYear(nYr,nMin,nMax) {
	/*if (!isInteger(nYr)) {
		return false;
	}*/
	if (parseInt(nYr,10)<nMin || parseInt(nYr,10)>nMax){
		return false;
	}
	return true;
}
function isExpired(cMth,cYr) {
	var m = Trim(cMth);
	if (!isMonth(m)) {return false;}
	var y = Trim(cYr);
	if (!isInteger(y)) {return false;}
	if (y.length == 2) {
		y = "20"+y;
	}
	if (parseInt(m,10)==12) {
		var nM = 0;
		var nY = parseInt(y,10)+1;
	} else {
		var nM = parseInt(m,10);
		var nY = parseInt(y,10);
	}
	var dExp = new Date(nY,nM,1);
	return !(Date.parse(dExp) > Date.parse(Date()));
}
function formatPCode (s) {
   var p = stripChrs(s.toUpperCase()," ");
   return (p.substr(0,3)+" "+p.substr(3));
}
function isDigit(c) {
   return ((c >= "0") && (c <= "9"))
}
function isChar(c) {
   var x = c.toUpperCase();
   return ((x >= "A") && (x <= "Z"))
}
function isInteger (s) {
   if (isEmpty(s)) return false;
   var i;
   for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function mkUpper(oFld) {
   var f = oFld.value.toUpperCase()
   if (f != oFld.value) {
	oFld.value = f;
   }
}
function isURL(strURL) {	
	var i = 1;
    var sLength = strURL.length;
    var strLastThree;

	if (isEmpty(strURL)) {
		return false;
	}
       
	if (isWhitespace(strURL)) {
	return false;
	}
    
    while ((i < sLength) && (strURL.charAt(i) != ",")){
     i++;
    }
    
    if (strURL.charAt(i) == ",") return false;
    
    i = 1;
    while ((i < sLength) && (strURL.charAt(i) != ".")) {
     i++;
    }

    if ((i >= sLength) || (strURL.charAt(i) != ".")) {
		return false;
	} else {
		i += 2;
	}

    while ((i < sLength) && (strURL.charAt(i) != ".")) {
     i++
    }
    if ((i >= sLength - 1) || (strURL.charAt(i) != ".")) {
		return false;
	}
	i++
	strLastThree = strURL.substring (i);
	strLastThree = strLastThree.toUpperCase();

	return true;
}
