function trim(strText) {
        // this will get rid of leading spaces
        while (strText.substring(0,1) == ' ')
            strText = strText.substring(1, strText.length);

        // this will get rid of trailing spaces
        while (strText.substring(strText.length-1,strText.length) == ' ')
            strText = strText.substring(0, strText.length-1);

       return strText;
    }

function checkIfValid(qn)
{
   qn.value = trim(qn.value);
   var queryValue = qn.value;
   for (var i = 0; i < queryValue.length; i++)
   {
      var oneChar = queryValue.charAt(i);
      if(oneChar == ' ')
      {
         continue;
      }
      if (isLetter(oneChar) == false && isDigit(oneChar) == false )
      {
         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 (c == " ");
}

function validateField(field,name)
{
   var val = field.value;
   for (var i = 0; i < val.length; i++)
      {
         var oneChar = val.charAt(i);
         if (isDigit(oneChar) == false &&  oneChar.charCodeAt(0)!= 44)
         {
         
        alert("Only numbers and commas are accepeted in this field.\n\nFor example: 1,000,000 or 1000000");
        break;
         }
      }
}
function extractSpecialCharactersField(field, defaultName)
{
   var val = field.value;
   for (var i = 0; i < val.length; i++)
      {
         var oneChar = val.charAt(i);
         if (!isLetter(oneChar) && !isDigit(oneChar) && !isSpace(oneChar) )		
         {
          alert("Only letters or numbers are accepeted in this field.\n\n The name of template will be replaced for '"+defaultName+"'");
		  field.focus();
        break;
         }
      }	  
}
function checkSpaceSize(txtObj) // a space cannot be > 1 or the SQL will bomb
{
   var val = txtObj.value;
   var len = val.length;
   var spaceFound = 0;
   var output = "";

   for(var i = 0; i < len; i++)
   {
      var oneChar = val.charAt(i);
      if(oneChar== ' ')
      {
         spaceFound = spaceFound+1 ;
         if( spaceFound > 1 )
         {
            // keep going
         }
         else
         {
            output = output + oneChar;
         }
      }
      else
      {
         output = output + oneChar;
         spaceFound = 0;
      }
   }

   txtObj.value = output;
}
/** ADD -- GEO STATE NAME 316 **/
function searchVariationUSA(htmlValue) {
    var cs=htmlValue;
    var countryString = "";
	if(cs!=null)
		countryString=cs.value;

    var countrySearchArray = countryString.split(" ");
    var val="";
    var bool=false;
    for (var i =0;i<countrySearchArray.length;i++)
        {
            var aCountry = countrySearchArray[i].toUpperCase().replace(/^\s*|\s*$/g,"");
            if(!bool) {
                if(aCountry=="U.S.A.") {
                    val+="U.S.A. ";
                    bool=true;
                } else if(aCountry=="U.S.") {
                    val+="U.S.A. ";
                    bool=true;
                } else if(aCountry=="USA") {
                    val+="U.S.A. ";
                    bool=true;
                } else if(aCountry=="US") {
                    val+="U.S.A. ";
                    bool=true;
                } else if(aCountry=="UNITED") {
                    if(countrySearchArray[i+1]!=null && countrySearchArray[i+1].toUpperCase()=="STATES") {
                        val+="U.S.A. ";
                        bool=true;
                    }else if(aCountry!=""){val+=aCountry+" ";}
                } else if(aCountry!=""){
                    val+=aCountry+" ";
                }
            } else if(aCountry!="U.S.A." && aCountry!="U.S."
                && aCountry!="USA" && aCountry!="US"
                && !(aCountry=="UNITED" && (countrySearchArray[i+1]!=null && countrySearchArray[i+1].toUpperCase()=="STATES"))
                && !(aCountry=="STATES" && (countrySearchArray[i-1]!=null && countrySearchArray[i-1].toUpperCase()=="UNITED"))
				&& aCountry!="") {
					val+=aCountry+" ";
                }
            
        }
	if(cs!=null)
		cs.value=val;
}
/**END -- GEO STATE NAME 316 **/


