function trimString(str)
{
	if (!(str == "")) {
	while('' + str.charAt(0) == ' '){
		str=str.substring(1,str.length);
	}
	}
	//take out trailing spaces
	if (!(str == "")) {
	while (str.charAt(str.length - 1) == ' '){
    	str = str.substring(0, str.length - 1);
	}
	}
	return str;
}
function validateSize(obj, limit){
	var val = trimString(obj.value);
	if (val.length > limit){
		alert("You have exceeded the field limit of " + limit + " characters.");
		obj.value = val.substring(0, limit);
	}
}
function validateMulti(obj, limit){
    var sel = 0;
    var isValid = true;
    for (var i = 0; i < obj.length; i++){
        if (obj[i].selected){
            sel++;
            if(sel > limit){
                if (isValid){
                    //only show message once
                    alert("You can only select up to " + limit + " items to search on");
                    isValid = false;
                }
                obj[i].selected = false;
            }
        }
    }
}
function replaceDiacriticals(obj) {
    text = obj.value;
	text = replace(text,unescape('%u2018'),unescape('%27'));
	text = replace(text,unescape('%u2019'),unescape('%27'));
    text = replace(text,unescape('%u201c'),unescape('%22'));
	text = replace(text,unescape('%u201d'),unescape('%22'));
	text = replace(text,unescape('%u2014'),'-');//replace em dash
	text = replace(text,unescape('%u2013'),'-');//replace en dash
	text = replace(text,unescape('%u2003'),' ');//replace em space
	text = replace(text,unescape('%u2003'),' ');//replace en space
    obj.value = text;
}
function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length;
    var txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
function checkWildCard(strng){
	if (strng.length > 0){
		var wildCard = "*";
		//var str = strng.replace(/%/g, "*");
		var arr = strng.split(wildCard);	
		if(arr.length > 1){			
			for (var y = 0; y < arr.length; y++){
				if ((arr[y].length < 3)&&(arr[y] != "")){
					alert("You must enter at least 3 concurrent characters when using the wildcard \"*\".");	
					return false;
				}
			}
		}
		if (strng == "*"){	
		    alert("You must enter at least 3 concurrent characters when using the wildcard \"*\".");	
		    return false;    
		}    
	}
	return true;
}
function valid_email(str){
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	if (!reg1.test(str) && reg2.test(str)) 
	{
		return true;
	}
	return false;
}
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
function validUserId(str){
	var myRegxp = /^[a-zA-Z0-9\(]+$/;	
	if ((str.length <= 6 || str.length >= 21)||(!(myRegxp.test(str))))
	{
		alert("The User ID you entered cannot be processed. Please enter a new User ID that is 7-20 characters long and has no punctuation, symbols, or spaces.");
		return false;
	}
	return true;
}
function validPwd(str){
	//var myRegxpw = /^[a-zA-Z0-9\!\*\@\#\_\$\&\(\)\+\,\.\/\:\;\=\?\~\[\"\^\¼]+$/;
	var myRegxpw = /^[a-zA-Z0-9\(]+$/;	
	if ((str.length <= 5 || str.length >= 21))
	{
		alert("The password you entered cannot be processed. Please enter a new password that is 6-20 characters long and has no punctuation, symbols, or spaces.");
		return false;
	}else if (!(myRegxpw.test(str))){
		alert("The password you entered cannot be processed. Please enter a new password.");
		return false;
	}
	return true;
}
function contactUsInit(){
    // this was added to handle back button issue with AJAX (DHTML)
    var doc = document.frmContactUS;
    if(doc.hidCountryId.value)
	    doc.ddlCountry.value = doc.hidCountryId.value;
    if(doc.hidStateId.value){
	    checkContactCountry();
	    doc.ddlState.value = doc.hidStateId.value;
	}
}
function validateContactUs(){
	var doc = document.frmContactUS;
	// trim all fields
	//user modified for update registration form
	doc.txtFirstName.value		= trimString(doc.txtFirstName.value);
	doc.txtLastName.value		= trimString(doc.txtLastName.value);
	doc.txtEmailAddress.value	= trimString(doc.txtEmailAddress.value);
	doc.txtZip.value	        = trimString(doc.txtZip.value);
	doc.txtPhone.value	        = trimString(doc.txtPhone.value);
	doc.txtComments.value	    = trimString(doc.txtComments.value);
	
	var fname       = doc.txtFirstName.value;
	var lname       = doc.txtLastName.value;
	var email       = doc.txtEmailAddress.value;
	var zip         = doc.txtZip.value;
	var phone       = doc.txtPhone.value;
	var comments    = doc.txtComments.value;
	var country     = doc.ddlCountry.value;
	var state       = doc.ddlState.value;
	var aop         = doc.ddlAreaOfLaw.value;
	var msg         = "";
	
	//set to empty for back button issues
	doc.hidCountryId.value = "";
	doc.hidStateId.value = "";
	doc.hidCountryName.value = ""; 
	doc.hidStateName.value = "";
	    
	doc.hidCountryId.value = country;
	doc.hidStateId.value = state;
	doc.hidCountryName.value = doc.ddlCountry.options[doc.ddlCountry.selectedIndex].text;
    if ((country == "1")||(country == "2")){
	    doc.hidStateName.value = doc.ddlState.options[doc.ddlState.selectedIndex].text;
    }else{
        doc.hidStateName.value = "";
    }	
	
	if (fname == "")
		msg += "First Name\r\n";
	if (lname == "")
		msg += "Last Name\r\n";
	if (email == "")
		msg += "Email\r\n";
	if (country == ""){
		msg += "Country\r\n";
	} else if ((country == "1")&& (state == "")){
	    msg += "State\r\n";
	} else if ((country == "2")&& (state == "")){
	    msg += "Province\r\n";
	}
	if (zip == "")
		msg += "Postal Code\r\n";
	if (phone == "")
		msg += "Phone\r\n";
	if(doc.ddlAreaOfLaw.length > 1){	
	    if (aop == "" || aop == "0")
		    msg += "Area of Law\r\n";
	}
	if (comments == "")
		msg += "Comments\r\n";
	
	if (msg != ""){
		msg = "The following are required fields.\r\n\r\n" + msg;
		alert(msg);
		return false;
	}
	if(!(valid_email(email))){
		alert("Please enter a valid email address.");
		doc.txtEmailAddress.focus();
		return false;
	}
	return true;
}
function checkContactCountry()
{
    var id = document.getElementById("ddlCountry").value;
    switch(id)
	{
		case "1" : 
			loadSelStates(document.getElementById("ddlState"));
			document.getElementById("divState").style.display 		    = "block";
			document.getElementById("divProvince").style.display 		= "none";
			document.getElementById("divStateDropdown").style.display 	= "block";
			break;
		case "2" : 
			loadSelProvinces(document.getElementById("ddlState"));
			document.getElementById("divState").style.display 		    = "none";
			document.getElementById("divProvince").style.display 		= "block";
			document.getElementById("divStateDropdown").style.display 	= "block";
			break;		
		default : 
		    document.getElementById("ddlState").options[0].selected     = true;
		    document.getElementById("divState").style.display 		    = "none";
			document.getElementById("divProvince").style.display 		= "none";
			document.getElementById("divStateDropdown").style.display 	= "none";
			break;
	}
    
}
function validateComment(obj, limit){
	var val = trimString(obj.value);
	if (val.length > limit){
		alert("You have exceeded the field limit of " + limit + " characters.");
		obj.value = val.substring(0, limit);
	}
	for (var i=0; i < val.length; i++){
		if (val.charAt(i)=="<" || val.charAt(i)==">"){
			alert("You have entered some invalid characters that will be removed.")
			obj.value = val.replace(/<|>/g,"");
			return;
		}
	}
}

function checkTextEmail(obj){
	if (document.getElementById(obj.id).value == "Email Address will not be displayed"){
		document.getElementById(obj.id).value = "";
		document.getElementById(obj.id).style.color = "#333333";
	}
	}
// window.onload = regInit;

	function SetMaskTextValue(obj, TextToSet) {
	    if (obj.value == "") {
	        obj.value = TextToSet;
	        obj.style.color = "#AAAAAA";
	    }
	}



	function RemoveMaskTextValue(obj, TextToCompare) {
	    if (obj.value == TextToCompare) {
	        obj.value = "";
	        obj.style.color = "#333333";
	    }
	}

	
	

	function RemoveMasks() 
	{
	    RemoveMaskTextValue(document.getElementById('nm'), 'For precise searching, use quotes: i.e. \"Cooper & Smith\"');
	    RemoveMaskTextValue(document.getElementById('kw'), 'Enter Other');
	}

