var ignoreFields = new Array("contact_name",
                             "contact_location",
                             "contact_phone",
                             "contact_phone_ext",
                             "contact_email");




////////////////////////////////////////////
// function: formReset( form [,ignore_fields_array] )
// author:   Stephen Reid
// written:  Mon Nov 21st, 2005
//
// usage:    var ifields = new Array( "fname", "lname" );
//           formReset( form, ifields );
//
// notes:    Erases text fields and set selection 
//           lists to their default selected values. 
//           It will also revert radio buttons and
//           check boxes to their default settings.
//           If for some reason, selection lists are
//           not being reset; call the function
//           "rebuildSelectionList( o );" and it will
//           wipe out the selection list and rebuild it.
//
// returns:  A logical true.
////////////////////////////////////////////
function formReset(form, ignore_fields){
   var length   = form.length;
   var f_exists = eval( "ignore_fields" );

   for(i=0;i<length;i++){

      //--------------------------------------------//
      // If an 'ignore fields' array exists, ignore //
      // those fields whoose names are found within //
      // it.                                        //
      //--------------------------------------------//
      if( f_exists ){
         if( aScan( form.elements[i].name, ignore_fields ) ){
            continue;
         }
      }

      //-------------------------------------//
      // Erase text field/textarea contents. //
      //-------------------------------------//
      var type = form.elements[i].type;
      if( (type == "text")  || (type == "textarea")){
         form.elements[i].value = "";
      }


      //--------------------------------------//
      // Set selection field to it's default. //
      //--------------------------------------//
      if( (type == "select-one") || (type == "select-multiple") ){
         var sel = form.elements[i];
         sel.options[0].selected;
      }


      //-----------------------//
      // Set a radio button to //
      // it's default setting. //
      //-----------------------//
      if( type == "radio" ){
         if( form.elements[i].defaultChecked ){
            form.elements[i].checked = true;
         }
      }


      //-----------------------------//
      // Set a checkbox to it's      //
      // default 'checked' settings. //
      //-----------------------------//
      if( type == "checkbox" ){
         var o = form.elements[i];
         o.checked = false;
         if( o.defaultChecked ){
            o.checked = true;
         }
      }

   }
   return true;
}






////////////////////////////////////////////
// function: rebuildSelectionList()
// author:   Stephen Reid
// written:  Mon Nov 21st, 2005
//
// usage:    o = document.myForm.elements["fruits"];
//           rebuildSelectionList(o);
//
// notes:    Saves, delets and rebuilds all options
//           on a selection list. This will have the
//           effect of setting it to it's default
//           selection item. Use this if all other
//           means to set the default value fails.
//
// returns:  A logical true.
////////////////////////////////////////////
function rebuildSelectionList( o ){
   var slength = o.length;
   var text    = new Array();
   var vals    = new Array();

   for(var j=0;j<slength;j++){
      text[j] = o.options[j].text;
      vals[j] = o.options[j].value;
   }

   o.length=0;

   for(var j=0;j<slength;j++){
      o.length = o.length+1;
      o.options[o.length-1].text  = text[j];
      o.options[o.length-1].value = vals[j];
   } 
}



////////////////////////////////////////////
// function: previewForm()
// author:   Stephen Reid
// written:  Mon Nov 21st, 2005
//
// usage:    previewForm(form);
//
// notes:    Opens a window, which is resized
//           to the maximum height and width of
//           the screen.
//
// returns:  A logical true.
////////////////////////////////////////////
function previewForm(form){
   var handle = openStatusWindow(form,screen.height, screen.width);

   handle.moveTo(0,0);
   handle.resizeTo(screen.width,screen.height);

   form.mode.value = "preview";

   //----------------------------------------//
   // Submit the form and reset the original //
   // values for the User Department and     //
   // User company selection lists that were //
   // changed.                               //
   //----------------------------------------//
   form.submit();

   form.mode.value = "";

}



////////////////////////////////////////////
// function: openStatusWindow()
// author:   Stephen Reid
// written:  Tue Nov 20th, 2001
//
// usage:    openStatusWindow(this,"130","250");
//
// notes:    opens a window to receive output
//           from a CGI program.
//
// returns:  A logical true.
////////////////////////////////////////////
function openStatusWindow( form, height, width ){
   var options = "scrollbars,width="+width+",height="+height;
   var h = window.open('',"statusWindow", options );
   form.target = "statusWindow";
   return h;
}




/////////////////////////////////////////// 
// function: LTrim() 
// author:   Stephen Reid 
// written:  Fri Jul 20, 20001
// usage:    document.forms[0].comments.value = LTrim( document.forms[0].comments.value );
// 
// notes:    Trims the left of a string. 
//////////////////////////////////////////// 
function LTrim( str ){ 
  var i=0; 
  while( (i<str.length) && ( (str.charAt(i) == ' ') || (str.charAt(i) == '\n') || (str.charAt(i) == '\t')) ){ 
    i++; 
    continue; 
  } 
  return( str.substr( i, str.length )) ; 
} 


 
//////////////////////////////////////////// 
// function: RTrim() 
// author:   Stephen Reid
// written:  Fri Jul 20, 20001 
// 
// usage:    document.forms[0].comments.value = RTrim( document.forms[0].comments.value); 
// 
// notes:    Trims the right of a string. 
//////////////////////////////////////////// 
function RTrim( str ){ 
  var i=str.length-1; 
  while( (i>=0) && ( (str.charAt(i) == ' ') || (str.charAt(i) == '\n') || (str.charAt(i) == '\t')) ){ 
    i--; 
    continue; 
  } 
  return( str.substr( 0, i+1 ) ) ; 
} 



//////////////////////////////////////////// 
// function: AllTrim() 
// author:   Stephen Reid
// written:  Fri Jul 20, 2001 
// 
// usage:    document.forms[0].comments.value = AllTrim( document.forms[0].comments.value ); 
// 
// notes:    Trims the left and right side of a string. 
////////////////////////////////////////////
function AllTrim( str ){
  return( RTrim( LTrim( str )));
}



///////////////////////////////////////////
// function: isCharacter()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( isCharacter( 'companies - CBS' ) ){
//              alert( "This is a character string!");
//              return true;
//           }
//
// notes:    Determines if a string contains no numerics;
//           punctuation is allowed.
////////////////////////////////////////////
function isCharacter(val){
   for(var i=0;i<val.length;i++){
      if(isNumber(val.charAt(i))){
         return false;
      }
   }
   return true;
}


///////////////////////////////////////////
// function: isAlphabetic()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( !isAlphabetic( 'companies - CBS' ) ){
//              alert( "This is not alphabetic!");
//              return false;
//           }
//
// notes:    Determines if a string contains only letters.
////////////////////////////////////////////
function isAlphabetic(val){
   for(var i=0;i<val.length;i++){
      var c = val.charAt(i);
      if( !isAlpha(c) ){
         return false;
      }
   }
   return true;
}


///////////////////////////////////////////
// function: isAlpha()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( isAlpha( 'c' ) ){
//              alert( "This is alphabetic!");
//              return true;
//           }
//
// notes:    called from isAlphabetic(), it accepts a
//           single character and determines if it's
//           a letter or not.
//
// returns:  logical.
////////////////////////////////////////////
function isAlpha( c ){
   return (( c >= 'a' && c<= 'z') || (c>='A' && c<='Z'));
}



///////////////////////////////////////////
// function: isNumber()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( isNumber( '9' ) ){
//              alert( "This is numeric!");
//              return true;
//           }
//
// notes:    called from isCharacter(), it accepts a
//           single character and determines if it's
//           a number or not.
//
// returns:  logical.
////////////////////////////////////////////
function isNumber( c ){
   return (( c >= '0' && c <= '9' ))
}



///////////////////////////////////////////
// function: isInteger()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( !isInteger( str ) ){
//              alert( "This field must be numeric!");
//              return false;
//           }
//
// notes:    Determines is a string is numeric.
////////////////////////////////////////////
function isInteger(val){
   for(var i=0;i<val.length;i++){
      if(!isDigit(val.charAt(i))){return false;}
   }
   return true;
}



///////////////////////////////////////////
// function: isDigit()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( !isDigit( 'c' ) ){
//              alert( "This character is not numeric!");
//              return false;
//           }
//
// notes:    Determines if a character is numeric.
////////////////////////////////////////////
function isDigit( c ){
   return (( c >= '0' && c <= '9' ) || ( c == '.' ))
}




///////////////////////////////////////////
// function: isEmail()
// author:   Stephen Reid
// written:  Fri Oct 14, 2005
//
// usage:    if( !isEmail( 'steve/@reid.com' ) ){
//              alert( "This is not a valid email!");
//              return false;
//           }
//
// notes:    Determines is the format of an email address is correct.
//
// returns:  logical.
////////////////////////////////////////////
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}



////////////////////////////////////////////
// function: aScan()
// author:   Stephen Reid
// written:  Mon 9th, 2001
// notes:    Scans an array for a matching string.
// return:   returns a logical to indicate success/failure.
////////////////////////////////////////////
function aScan( string, array ){
   var i;
   for(i=0;i<array.length;i++){
      if( string == array[i] ){
         return true;
      }
   }
   return false;
}


////////////////////////////////////////////
// function: showStatus()
// author:   Stephen Reid
// written:  Fri Oct 19th, 2001
// notes:    Sets/clears the window status field.
//
// return:   returns a true.
////////////////////////////////////////////
function showStatus( string ) {
   window.status = string;
   return true;
}



////////////////////////////////////////////
// function: NewWindow()
// author:   Stephen Reid
// written:  Mon Dec 24th, 2001
//
// usage:    NewWindow('email.html','popwindow','500','360','custom','front','no');
//
// notes:    Generic popup window routine.
////////////////////////////////////////////
var win=null;
var scrollbars='no';
function NewWindow(mypage,myname,w,h,pos,infocus,scrollbars){

if( eval( "win") ){
   win.close();
}

if(pos=="random"){
  myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
  mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
}

if(pos=="center"){
 myleft=(screen.width)?(screen.width-w)/2:100;
 mytop=(screen.height)?(screen.height-h)/2:100;
}
else if((pos!='center' && pos!="random") || pos==null){
 myleft=0;
 mytop=20
}

settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=" +scrollbars+",location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
win=window.open(mypage,myname,settings);
win.focus();
}



////////////////////////////////////////////
// function: setFocus()
// author:   Stephen Reid
// written:  Sat 1st, 2005
// 
// usage:    onload="setFocus('FIRSTNAME');"
//           
// notes:    Sets focus to a form field, only
//           works on form '0'.
//
// return:   returns a true.
////////////////////////////////////////////
function setFocus( name ){
   var form = document.forms[0];
   form.elements[name].focus();
   return true;
}




////////////////////////////////////////////
// function: getArgs()
// author:   Stephen Reid
// written:  Sat 1st, 2005
// 
// usage:    <script>
//              var p = getArgs();
//              document.write('<img src='+p['IMAGEURL']+' alt="" border="0">');
//           </script>"
//           
//
// notes:    Pass parameters to an html page using the follow format:
//           popup_window.html?IMAGEURL=images/photo_album/20051126/1.jpg
//
// 
// return:   returns an array of key value pairs.
////////////////////////////////////////////
function getArgs(){

   var url    = document.location+'';
   var domain = url.split("?");

   if( domain[1] ){

      var params = new Array();
      var args   = domain[1].split('&');

      for (var i=0; i<args.length; i++){
          var arg = args[i].split('=', 2);
          params[arg[0]] = arg[1]
      }

      return params;
   }
}



