/* *******************************************************************
   File:          cacFunctions.js
   Written by:    CAC, Nov 03
   Last revision:
   
   Notes:         i discovered satyam's function file after i'd  
                  already written a number of functions for the same 
                  purposes. Since their code is flawed (using loop 
                  vars outside loop, work with form fields not str 
                  vars, ...) i created this file.

  ********************************************************************/

  
/* ******************************************************************
   Trims trailing and preceding spaces
   RETURNS trimmed string          
   ******************************************************************/
function trimStr(trim_str)
{
   var firstChar = 0 ;
   var lastChar = trim_str.length - 1 ;

   // get first character position, skipping spaces
   for ( var j = 0; j < trim_str.length ; j++ )
   {
      if (trim_str.charAt(j) != ' ' && trim_str.charAt(j) != '\t')
      {
         firstChar = j ;
         break ;
      }
   }

   // if string contains more than one char, get last character position   
   if ( firstChar <  lastChar )
      for ( var k = lastChar ;  k >= 0 ; k-- )
         if ( (trim_str.charAt(k) != ' ') && trim_str.charAt(k) != '\t')
         {
            lastChar = k ;
            break ;
         }         
   
   trim_str = trim_str.substring(firstChar, (lastChar + 1) ) ; 
   return trim_str;
}


/* ******************************************************************
   tests for empty string
   RETURNS true for empty string          
   ******************************************************************/
function isEmpty (test_str)
{   
   if (test_str.length == 0 ) 
      return true ;   // string is empty
   
   for (var i=0; i < test_str.length; ++i)
      if (test_str.charAt(i) != ' ' && test_str.charAt(i) != '\t')
         return false ;

   return true ;   // string isn't empty but contains no valid chars
}


/* ******************************************************************
   tests the string for spaces, replacing internal spaces with 
   underscore and trimming preceding and trailing spaces.  multiple
   spaces are replaced with a single underscore.
   NOTE: Code should check if the string is empty BEFORE calling this
   function
   RETURNS: the string trimmed and with '_' replacing internal spaces 
   ******************************************************************/
function replaceSpaces (spaceTest_str)
{ 
   spaceTest_str = trimStr( spaceTest_str ) ;
   // search trimmed string for spaces and replace with underscore
   for (var n = 0 ; n <= spaceTest_str.length ; n++ )
   {
      currentChar = spaceTest_str.charAt(n) ;
      if ( currentChar == ' ' )
      {
         if ( n > 0 )  // if preceding char is already an underscore skip to next char
         {
            if ( spaceTest_str.charAt(n - 1) != '_' )
               spaceTest_str =  spaceTest_str.replace(spaceTest_str.charAt(n), "_");
            else
            {
               spaceTest_str = spaceTest_str.substr( 0, n ) + 
                                spaceTest_str.substr( (n + 1), (spaceTest_str.length - (n + 1) ) );
               n = n - 1 ;                          
            }
         }
         else     // first char, throw away
         {
            spaceTest_str =  spaceTest_str.substr( 1 , spaceTest_str.length - 1 );
            n = n =1 ;
         }        
         
      }
   }

   return spaceTest_str ;   
}


/* ******************************************************************
   tests the string for non-numeric characters.
   NOTE: Code should check if the string is empty BEFORE calling this
   function
   RETURNS: true if all numeric, else false
   ******************************************************************/
function isnumeric(numTest_str)
{
   for (var i = 0; i < numTest_str.length; i++ )
   {  
      if (numTest_str.charAt(i) < "0" || numTest_str.charAt(i) > "9")
         return false;
    }     
   return true;
}


/* ******************************************************************
   tests the string for non alphanumeric characters and replaces
   them with underscore.  trims preceding and trailing spaces 
   multiple spaces are replaced with a single underscore.
   NOTE: Code should check if the string is empty BEFORE calling this
   function
   RETURNS: the string trimmed and with '_' replacing non aphanum chars 
   ******************************************************************/
function replaceSpChars (specCharTest_str)
{  // search trimmed string for spaces or non alpha chars in middle
   // and replace with underscore
   for (var m = 0 ; m < specCharTest_str.length ; m++ )
   {
      currentCharCode = specCharTest_str.charCodeAt(m) ;
      
      // 46 = '.' ; 45 = '-' ; 48 = '0' ; 122 = 'z' ; 57 = '9' ; 
      // 65 = 'A' ; 90 = 'Z' ; 97 = 'a'      
      if ( ( ( currentCharCode < 48 ) && ( currentCharCode != 45 ) && 
             ( currentCharCode != 46 )  ) || 
           ( currentCharCode > 122 ) ||
           ( ( currentCharCode > 57 ) && ( currentCharCode < 65 ) ) ||
           ( ( currentCharCode > 90 ) && ( currentCharCode < 97 ) ) 
          )
      {  
         if ( m > 0 )  // if preceding char is already an underscore skip to next char
         {
               if ( (specCharTest_str.charAt(m - 1) != '_') && 
                    (m != specCharTest_str.length - 1 ) )
               specCharTest_str =  specCharTest_str.replace(specCharTest_str.charAt(m), "_");
            else
                  if ( ( m == specCharTest_str.length - 1 ) && 
                       ( (specCharTest_str.charAt(m - 1)) != '_' ) )
                     specCharTest_str = specCharTest_str.substr( 0, m ) ;
                  else
                     if ( ( m == specCharTest_str.length - 1 ) && 
                          ( (specCharTest_str.charAt(m - 1)) == '_' ) )
                        specCharTest_str = specCharTest_str.substr( 0, m - 1 ) ;
                     else
            {
               specCharTest_str = specCharTest_str.substr( 0, m ) + 
                                specCharTest_str.substr( (m + 1), (specCharTest_str.length - (m + 1) ) );
               m = m - 1 ;                          
            }
         }
         else     // first char 
         {
            specCharTest_str =  specCharTest_str.substr( 1, specCharTest_str.length - 1 );
            m = m - 1 ;
         }        
      }
   }
   return specCharTest_str ;   
}

/* ******************************************************************
	handler for form textarea.
   called ONCHANGE and ONSUBMIT 
   count the number of characters entered and trigger an alert if over
   maxChar characters.  The extra character is for the CR-LF that gets 
	inserted at the end of the text 
	Returns TRUE if within the limited number of characters, else FALSE
   ******************************************************************/

function testTxtLength (frmTextObj, maxChar, frmFieldLabel) 
{  /*Validate textarea length before submitting the form for product features*/
   if ( frmTextObj.value.length > maxChar ) 
   {  diff = frmTextObj.value.length - maxChar ;
      if ( diff > 1 )
         diff = diff + " characters" ;
      else
         diff = diff + " character" ;
      
      alert("Your " + frmFieldLabel + " is limited to " + maxChar + " characters. \nIt currently " 
             + "exceeds the maximum number of characters. Please \nreduce the text by " + diff + ".");
      frmTextObj.focus();
      frmTextObj.select();
            
      return false;
   }  /* end if */
	
	else
		return true;
}


var canadaProvinces_str = new String("MB,BC,AB,NT,NF,NB,ON,NS,PE,PQ,SK,YT");

/* ******************************************************************
   ONBLUR handler for state to auto set corresponding country. 
   To use this function the country form field must be named 
   'country' (id also). 
   ******************************************************************/

   function getCountry(form, selectedStateProvince)   
   {          
      if ( canadaProvinces_str.indexOf( selectedStateProvince ) >= 0 )          
      {
         form.country.options[0].value = "Canada" ;
                     form.country.options[0].text = "Canada" ; 
                     form.country.selectedIndex=0; 
                     return true;
      }

      else  if ( selectedStateProvince == "" )
               return true;   // this occurs if they select Not Applicable-do nothing

            else  if ( selectedStateProvince == "Not Applicable" )
                  {  // this is a bug if it occurs something has gone wrong.
							// clear out value and replace with empty string
							form.stateProvince.options[0].value = "";            
							form.stateProvince.options[0].text = "Not Applicable";    
							form.stateProvince.selectedIndex = 0;      
							return true;
                  }
			
                  else
                  {  
                     form.country.options[0].value = "United States" ;
                     form.country.options[0].text = "United States" ; 
                     form.country.selectedIndex=0; 
                     return true;
      }               

   }   

/* ******************************************************************
   ONBLUR handler for country.  if country is not US or
   Canada or if US state with Canada, or U.S. with province then 
   the state value is set to not applicable.
   To use this function the state form field must be named 
   'stateProvince' (id also). 
   ******************************************************************/

   function checkState (form, selectedCountry, selectedState)   
   {    
      if ((selectedCountry != "United States") && (selectedCountry != "Canada"))
         {  
            form.stateProvince.options[0].value = "";            
            form.stateProvince.options[0].text = "Not Applicable";    
            form.stateProvince.selectedIndex = 0;      
         }
      else
         if ( ( selectedCountry == "Canada" ) && ( selectedState != "" ) )
         {
            if ( canadaProvinces_str.indexOf( selectedState ) == -1 )
               {  // country is canada but u.s. state
                  form.stateProvince.options[0].value = "";            
                  form.stateProvince.options[0].text = "Not Applicable";    
                  form.stateProvince.selectedIndex = 0;                  
               }
         }
         else
            if ( ( selectedState != "" ) && ( canadaProvinces_str.indexOf( selectedState ) >= 0 ) )
               {  // country is U.S. but canadian province
                  form.stateProvince.options[0].value = "";            
                  form.stateProvince.options[0].text = "Not Applicable";    
                  form.stateProvince.selectedIndex = 0;                  
               }
   }