// by Mike Paolucci YRMG 2007
// mpaolucci@yrmg.com

function sentenceCase(obj) {
var newStr = removeSpace(obj.value);
var arrStr = newStr.split(/[.?!]/);
var capMax = Math.floor(newStr.length/3);
var capMin = arrStr.length-1;  // alert("Max: " + capMax + " Min: " + capMin);
var arrCaps = newStr.match(/[A-Z]/g);
var iCaps = 0;
	if (arrCaps != null) {
		iCaps = arrCaps.length; // alert(iCaps);
	}
	if ((iCaps <= capMin) || (iCaps >= capMax)){
		newStr = newStr.toLowerCase().replace(/(^\s*\w|[\.\!\?]\s*\w)/g,function(c){return c.toUpperCase()});
	}
	obj.value = specifiedCase(newStr);
}

function specifiedCase(str) {
var strList = " I ,I'm,I'd,I've,I'll,Aurora,Ballantrae,Concord,East Gwillimbury,Georgina,Gormley,Holland Landing,Keswick,Kettleby, King ,King City,Kleinburg,Maple,Markham,Milliken,Mount Albert,Musselman's Lake,Newmarket,Nobleton,Pefferlaw,Pottageville,Queensville,Richmond Hill,Schomberg,Sharon,Stouffville,Sutton,Unionville,Vandorf,Vaughan,Whitchurch,Woodbridge,York Region,ASAP,BTW,YRMG"; // add to this comma separated list as required
var arrWords = strList.split(",");
var good;
var bad;
	for (i=0; i < arrWords.length; i++) {
		good = arrWords[i];
		bad = new RegExp(arrWords[i], "gi"); 
		str = str.replace(bad, good);
	}
	return str;
}	

function properCase(obj) {
	var index;
	var tmpStr;
	var tmpChar;
	var preStr;
	var postStr;
	var strLen;
	tmpStr = obj.value.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  {
		for (index = 0; index < strLen; index++)  {
	if (index == 0)  {
		tmpChar = tmpStr.substring(0,1).toUpperCase();
		postStr = tmpStr.substring(1,strLen);
		tmpStr = tmpChar + postStr;
	}
	else {
		tmpChar = tmpStr.substring(index, index+1);
		if (tmpChar == " " && index < (strLen-1))  {
			tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
			preStr = tmpStr.substring(0, index+1);
			postStr = tmpStr.substring(index+2,strLen);
			tmpStr = preStr + tmpChar + postStr;
			}
		 }
	  }
	}
	obj.value = removeSpace(tmpStr);
}

function upperCase(theObj) {
	var tmpStr = theObj.value.toUpperCase();
	theObj.value = Trim(tmpStr);
}

function lowerCase(theObj) {
	var tmpStr = theObj.value.toLowerCase();
	theObj.value = Trim(tmpStr);
}

function regEmailIs_valid(text){
	var oneOnly = text.split("@"); // prevent multiple email address injections
	if (oneOnly.length != 2){ // email address split on '@' is only 2 strings
		return false;
	}
	var myRegExp = /\S+\@\S+\.\S+/i;
	return(myRegExp.test(text));
}

// test Postal Code for CA format
function regPostalIs_valid(text) {
	var myRegExp = /([a-z]\d[a-z]\s?\d[a-z]\d)/i; //match postal format - ignore case
	return(myRegExp.test(text));
}

// test Phone for 10 digits
function regPhoneIs_valid(text)	{
	var myRegExp = /\(?\d{3}\)?\d{3}\-?\d{4}/;
	return(myRegExp.test(text));
}

function purgePhone(str) { // Remove common phone extras
   str = str.toLowerCase();
	var strClean = "";
	var strBad = " -().,;:_\"/|\\'abcdefghijklmnopqrstuvwxyz"; // Characters to be removed
    for (var i = 0; i < str.length; i++) { 
        current = str.charAt(i)  
       if (strBad.indexOf(current) == -1) { 
           strClean += current;
       }
    }
    return strClean   // Return the numbers only
}

// call with: onblur="formatPhone(this)"
function formatPhone(obj) {
	var str = Trim(obj.value);
	str = purgePhone(str);
	if (str.length == 10) {
		str = ("(" + str.substr(0,3) + ") " + str.substr(3,3) + "-" + str.substr(6,4));
	}
	else if ((str.length == 11) && (str.charAt(0) == 1)) {
		str = (str.substr(0,1) + "-" + str.substr(1,3) + "-" + str.substr(4,3) + "-" + str.substr(7,4));
	}
	obj.value = str;
}

function Trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}
function LTrim(str) {
	return str.replace(/^\s+/,"");
}
function RTrim(str) {
	return str.replace(/\s+$/,"");
}

function removeSpace(str) {
	// alert("Fired");
  	str = str.replace(/\r/g, " ");
  	str = str.replace(/\n/g, " ");
	str = str.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'":\?\/\.,]/g, "");
	str = str.replace(/</g, "");
	str = str.replace(/>/g, "");
	str = str.replace(/ +/g, " ");  
	str = str.replace(/^\s/g, "");
	str = str.replace(/\s$/g, "");	
	if (str == ' '){str = ''};
return str
}

function countCopy(obj, limit) {
	var str = obj.value;
	var intCnt = str.length;
	document.frmContest.Count.value = intCnt;
	if (intCnt > limit) {
		alert("You have exceeded the character limit!");
	}
}

function countWords(obj, limit) {
	var str = obj.value;
	var intCnt = 0;
	str = str.replace(/\s/g,' ');
	str = str.split(' ');
	intCnt = str.length;
	// alert(intCnt);
	document.frmContest.Count.value = intCnt;
	if (intCnt > limit) {
		alert("You have exceeded the word limit!");
	}
}
