//---------------------------------------------------------------------------

function rSubmitThisForm ( ) {
     
    //return rfIsNetscape() ? document.layers["BaseForm"].document.aForm : document.aForm;
    if (rfIsNetscapeUnder5())
    {
        return document.layers["BaseForm"] ? document.layers["BaseForm"].document.aForm : document.aForm;
    }
    else
    {
        return document.aForm;
    }
    //return document.aForm;
}

//---------------------------------------------------------------------------

function rSetImg ( ButtonName, GIFName ) {

    ButtonName.src = GIFName;
}

//---------------------------------------------------------------------------

function fIsOKEMailAddress ( EMailAddr ) {

    if ( EMailAddr.length < 5 || EMailAddr.indexOf("@") < 0) {

        alert("Please enter a valid e-mail address.");
        return false;
    }
    return true;
}

//---------------------------------------------------------------------------
var gMissingFields = "";
var gMissingValues = "";

//---------------------------------------------------------------------------

function rDeleteCookie ( WhichCookie ) {
    if ( confirm("\n\nRemove your e-mail address?\n\n") ) {
        var ExpDate = new Date();
        ExpDate.setTime( ExpDate.getTime() - (1000 * 60 * 60 * 24 * 365) );
        document.cookie = WhichCookie + "=; path=/; expires=" + ExpDate.toGMTString();
        document.cookie = WhichCookie + "=; path=/getnews; expires=" + ExpDate.toGMTString();
        alert("\n\nOK -- removed e-mail address.  You will need to re-enter it.\n\n");
    }
}    
//-------------------------------------------------------------------
function rfTestAValue ( WhichValue, WhichName, MinLength, MaxLength ) {

    var TheValue;
    var Cmd;
    var aForm = rSubmitThisForm();
    
    Cmd = "TheValue = aForm." + WhichValue + ".value;";
    eval(Cmd);
    
    TheValue = trim(TheValue);
    
    if ( TheValue.length < MinLength || TheValue.length > MaxLength ) {
    
        if ( aForm.setfocus.value.length < 2 ) {
             aForm.setfocus.value = WhichValue;
        }
        gMissingFields += "\t" + WhichName + "\n";
        gMissingValues += WhichValue + ";";
        return false;
    }
    return true;
}

//-------------------------------------------------------------------
function rfTestAValueExclude ( WhichValue, WhichName, MinLength, MaxLength, BadValue) {

    var TheValue;
    var Cmd;
    var aForm = rSubmitThisForm();
    
    Cmd = "TheValue = aForm." + WhichValue + ".value;";
    eval(Cmd);
    
    TheValue = trim(TheValue);
    
    if ( TheValue.length < MinLength || TheValue.length > MaxLength || TheValue == BadValue) {
    
        if ( aForm.setfocus.value.length < 2 ) {
             aForm.setfocus.value = WhichValue;
        }
        gMissingFields += "\t" + WhichName + "\n";
        gMissingValues += WhichValue + ";";
        return false;
    }
    return true;
}

function rfValidateIfEnabled ( WhichValue, WhichName, MinLength, MaxLength ) {
	var TheValue;
    var Cmd; 
    var ctrl;
    var aForm = rSubmitThisForm();
    
    
    ctrl = "TheValue = aForm." + WhichValue;
    eval(ctrl);
    TheValue = trim(TheValue);
    
    if (TheValue.disabled){
      // ignore controll
      return true;
    }
    
    TheValue = TheValue.value;
    //Cmd = "TheValue = document.aForm." + WhichValue + ".value;";
    //eval(Cmd);
    
    if ( TheValue.length < MinLength || TheValue.length > MaxLength ) {
    
        if ( aForm.setfocus.value.length < 2 ) {
             aForm.setfocus.value = WhichValue;
        }
        gMissingFields += "\t" + WhichName + "\n";
        gMissingValues += WhichValue + ";";
        return false;
    }
    return true;
}

function rfValidateIfExists ( WhichValue, WhichName, MinLength, MaxLength ) {
	var TheValue;
    var Cmd; 
    var ctrl;
    var aForm = rSubmitThisForm();
    
    
    ctrl = "TheValue = aForm." + WhichValue;
    eval(ctrl);
        
    if (!TheValue){
      // ignore controll
      return true;
    }
    
    TheValue = trim(TheValue.value);
    //Cmd = "TheValue = document.aForm." + WhichValue + ".value;";
    //eval(Cmd);
    //alert( "length = " + TheValue.length)
    if ( TheValue.length < MinLength || TheValue.length > MaxLength ) {
    
        if ( aForm.setfocus.value.length < 2 ) {
             aForm.setfocus.value = WhichValue;
        }
        gMissingFields += "\t" + WhichName + "\n";
        gMissingValues += WhichValue + ";";
        return false;
    }
    return true;
}

//---------------------------------------------------------------------------

    function rfTestAValue2 ( WhichValue, WhichName, BadValue) {
    
        var TheValue;
        var Cmd;
        var ctl;
        var aForm = rSubmitThisForm();
        
        Cmd = "ctl = aForm." + WhichValue + ";"//".selectedIndex;";
        eval(Cmd);
        //ctl.options[inx]
        if (TheValue == 0){
         gMissingFields += "\t" + WhichName + "\n";
         gMissingValues += WhichValue + ";";
         return false;
        } 
        
        return true;
        
    }
//---------------------------------------------------------------------------
    // test e-mail 
    function rfTestAValue3 ( WhichValue, WhichName, MinLength, MaxLength, TestChar ) {
    
        var TheValue;
        var Cmd;
        var xchar;
        var NotValid;
        var aForm = rSubmitThisForm();
        
        Cmd = "TheValue = aForm." + WhichValue + ".value;";
        eval(Cmd);
        TheValue = trim(TheValue);
                
        // test TheValue for each char in TestChar string
        for(i=0;i < TestChar.length;i++){
         xchar = TestChar.substring(i,i+1)
         if (TheValue.indexOf(xchar,0) == -1){
           NotValid = true;
           break;
         }
        }
        
        if ( NotValid || TheValue.length < MinLength || TheValue.length > MaxLength)
           {
             if ( aForm.setfocus.value.length < 2 ) {
              aForm.setfocus.value = WhichValue;
             }
            gMissingFields += "\t" + WhichName + "\n";
            gMissingValues += WhichValue + ";";
            aForm.WhichValue = ""
            return false;
        }
      

        return true;
    }
    
//---------------------------------------------------------------------------
    function rfTestAValue4 ( WhichValue, WhichName, BadValue) {
    // validate option control
    
        var Cmd;
        var ctl;
        var inx;
        var opt;
        var aForm = rSubmitThisForm();
            
       Cmd = "ctl = aForm." + WhichValue + ";";
       eval(Cmd);
       
       inx = ctl.selectedIndex;
	   if ((inx != -1) && (ctl.options[inx].value != BadValue) ){
		  opt = ctl.options[inx].value;
		  return true;
	   }
	   else {
	    gMissingFields += "\t" + WhichName + "\n";
	    gMissingValues += WhichValue + ";";
         return false;
	   }
      
    }
    //----------- test integer -------------
    function rfTestAValueInt ( WhichValue, WhichName, ExcludeChars, NumberOfDigits ) {
    
        var TheValue;
        var Cmd;
        var xchar;
        var NotValid;
        var xasc;
        var aForm = rSubmitThisForm();
        
      
        Cmd = "TheValue = aForm." + WhichValue;
        eval(Cmd);
        TheValue = trim(TheValue);
        
        if (!TheValue){
         // controll does not exist
         return true;
        }
        
        TheValue = TheValue.value;
        if(TheValue.length != NumberOfDigits){
          NotValid = true;
        } 
        else
        {     
			NotValid = true;
        
			for(i=0;i < TheValue.length;i++){
				NotValid = false;
				xchar = TheValue.substring(i,i+1);
				xasc = xchar.charCodeAt(0);
				if (!(
					(xasc >= 48) && 
					(xasc <= 57) && 
					(ExcludeChars.indexOf(xchar,0) == -1)
					)){
				  NotValid = true;
				  break;
				}
         
			}  
        }
        if ( NotValid)
           {
             if ( aForm.setfocus.value.length < 2 ) {
                  aForm.setfocus.value = WhichValue;
            }
            gMissingFields += "\t" + WhichName + "\n";
            gMissingValues += WhichValue + ";";
            aForm.WhichValue = ""
            return false;
        }
        return true;
    }
//---------------------------------------------------------------------------
    function rfTestAValue5 ( WhichValue, WhichName, BadValue) {
    // validate radio array control
    
        var Cmd;
        var ctl;
        var checkedButton;
        var aForm = rSubmitThisForm();
        
        checkedButton = "";    
       Cmd = "ctl = aForm." + WhichValue + ";";
       eval(Cmd);
       
       //for (var i in ctl) {
       for (var i=0; i < ctl.length;i++) {
         if (ctl[i].checked) {
            checkedButton=ctl[i].value
         }
       }
   
       if ((checkedButton != "") && (checkedButton != BadValue) ){
		  return true;
	   }
	   else {
	    gMissingFields += "\t" + WhichName + "\n";
	    gMissingValues += WhichValue + ";";
         return false;
	   }
      
    }
    
//---------------------------------------------------------------------------
    function GetSelectedSelect (ctl) {
       var opt;
       var inx;
       
       inx = ctl.selectedIndex;
	   if (inx != -1){
		  opt = ctl.options[inx].value;
		  return opt;
	   }
	   
	   return false;
    }
//------------------------------------------------------    
    function TextInSelectList (ctl, xname) {
       var i;
       var count;
       
       count = ctl.length;
       
       for (i=0;i<count;i++){
         if (xname == ctl.options[i].text){
           return true;  
         }  
       }
       return false;  
    }
//------------------------------------------------------    
    function ValueInSelectList (ctl, xvalue) {
       var i;
       var count;
       
       count = ctl.length;
       
       for (i=0;i<count;i++){
         if (xvalue == ctl.options[i].value){
           return true;  
         }  
       }
       return false;  
    }    
//---------------------------------------------------------------------------
    function GetSelectedRadio (ctl) {
       var i;
       var checkedButton;
       
       for (var i=0; i < ctl.length;i++) {
         if (ctl[i].checked) {
            checkedButton=ctl[i].value
            return checkedButton;
         }
       }
       return false
    }
    

//---------------------------------------------------------------------------
    // switch between select and radio array validation
    function rfTestAValue6 ( WhichValue, WhichName, BadValue) {
        var Cmd;
        var ctl;
        var res;
        var xtype;
        var xtag;
        res = "";
        var aForm = rSubmitThisForm();
        
        Cmd = "ctl = aForm." + WhichValue + ";";
        eval(Cmd);
        
        if (!(ctl && ctl[0])){
          return false;
        }
        
        xtag = ctl.tagName; // not defined in Netscape
        xtype = ctl[0].type;
        if (ctl.options){
          res = GetSelectedSelect (ctl); 
        }
        else {
          if (xtype && (xtype.toUpperCase() == "RADIO")){
             res = GetSelectedRadio (ctl);
          }
          else {
            window.alert(xtype + ' ' + xtag);
          }
        }  
        if (res && (res != BadValue) ){
		  return true;
	    }
	    else {
	     gMissingFields += "\t" + WhichName + "\n";
	     gMissingValues += WhichValue + ";";
         return false;
	    }  
    }
    
    //---------------------------------------------------------------------------
    // switch between "text" and "select" controll validation
    function rfTestAValue7 (WhichValue, WhichName, SelBadValue, MinLength, MaxLength ) {
        var Cmd;
        var ctl;
        var res;
        var aForm = rSubmitThisForm();
        
        Cmd = "ctl = aForm." + WhichValue + ";";
        eval(Cmd);
        
        if (!ctl){
          alert("\n\nControll " + WhichValue + " not found!");
          return false;
        }
        else {
          if (ctl.options){
            res = rfTestAValue4(WhichValue, WhichName, SelBadValue);
            return res;
          }
          else {
            res = rfTestAValue(WhichValue, WhichName, MinLength, MaxLength);
            return res;
          }
        }
    }
    
    //--------------------------------------------------
    function rFillInStyles(source_ctr, select_ctr) {
    
      var oStyleSheet = document.styleSheets[0];
      var oRules = oStyleSheet.rules;
      var xlen = oRules.length;
      var xstyle;
      var oSelect;
      var xoption;
      var cstyle; 
      var tmp;
      var was_selected;
      
      was_selected = false;
      var aForm = rSubmitThisForm();
      
      
      tmp = "oSelect = aForm." + select_ctr + ";"
      eval(tmp);
      tmp = "cstyle = aForm." + source_ctr + ".value;"
      eval(tmp);
      if (oSelect){
        for (var i=0; i < xlen; i++) {
          xstyle = oRules[i].selectorText;
          if (xstyle.substr(0, 1) == ".") {
			xstyle = xstyle.substr(1);
			// create option  
			xoption = document.createElement("OPTION");
			oSelect.add(xoption);
			xoption.value = xstyle;
			xoption.innerText = xstyle;
          
			if (xstyle == cstyle ){
	            xoption.selected = 1;
	            was_selected = true;
		    }
          } 
        }
      }
      

	  xoption = document.createElement("OPTION");
	  oSelect.add(xoption);
	  xoption.value = "";
	  xoption.innerText = "";
			
	  // select blank option in no match was found		
	  if (!was_selected){		
			xoption.selected = 1;
      }
    }

//---------------------------------------------------------------------------

    function rGetSelectedOption (xcontrol){
      var ctl;
      var res;
      var xstr
      
      //xstr = "ctl=document.aForm." + xcontrol;
      xstr = "ctl=document." + xcontrol;
      eval(xstr);
      
      if (ctl && ctl.options){
          res = GetSelectedSelect (ctl); 
          return res;
       }
       return false;
    }    

//---------------------------------------------------------------------------
function SSNValidation  (TestValue,FieldPrompt )
 { var tstr;
   var FixedLength = 11;
   var dchar34;
   var dchar67;
   var Cmd;
   var stcanthavech = "Can't have characters in ";
   var aForm = rSubmitThisForm();
                 
   Cmd = "TheValue = aForm." + TestValue + ".value;";
   eval(Cmd);
   TheValue = trim(TheValue);
             
   if (TheValue.length != FixedLength){ 
         gMissingFields += "\t" + FieldPrompt +"\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
    }           
   
   dchar34 = TheValue.substring(3,4);
   dchar67 = TheValue.substring(6,7);
   if ((dchar34 != "-") || (dchar67 != "-")){
         gMissingFields += "\t" + FieldPrompt + " not in format XXX-XX-XXXX" + "\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
   }  
   else { 
     tstr = TheValue.substring(0,3)+ TheValue.substring(4,6)+ TheValue.substring(7,11);
     if (!rIsNumeric(tstr)){
       gMissingFields += "\t" + stcanthavech + FieldPrompt + "\n";
       gMissingValues += TestValue + ";";
       aForm.setfocus.value = TestValue;
       return false;
     } 
   }  
   return true;
 }


//---------------------------------------------------------------------------
function ZipValidation  (TestValue,FieldPrompt )
 { var tstr;
   var FixedLength = 5;
   var dchar34;
   var dchar67;
   var Cmd;
   var stcanthavech = "Can't have characters in ";
   var aForm = rSubmitThisForm();
                 
   Cmd = "TheValue = aForm." + TestValue + ".value;";
   eval(Cmd);
   TheValue = trim(TheValue);
             
   if (!((TheValue.length == 5) || (TheValue.length == 10))) { 
         gMissingFields += "\t" + FieldPrompt +"\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         //alert("Length Not 5 or 10 it is: '" + TheValue.length + "'");
         return false;
    }           
   
   if ((TheValue.length == 10) && (TheValue.substring(5,6) != "-")){
         gMissingFields += "\t" + FieldPrompt + " not in format XXXXX-XXXX" + "\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         //alert("Length 10 no '-'");
         return false;
   }
   
   
   tstr = TheValue.substring(0,4);
   if (TheValue.length == 10){
    tstr += TheValue.substring(6,9);
   }
   
   if (!rIsNumeric(tstr)){
       gMissingFields += "\t" + stcanthavech + FieldPrompt + "\n";
       gMissingValues += TestValue + ";";
       aForm.setfocus.value = TestValue;
       //alert("Not numeric: " +  tstr + "'");
       return false;
   } 
   
   return true;
 }
 
//---------------------------------------------------------------------------
function DateValidation  (TestValue,FieldPrompt )
 { var Cmd;
   var arDate;
   var aForm = rSubmitThisForm();
                 
   Cmd = "TheValue = aForm." + TestValue + ".value;";
   eval(Cmd);
   TheValue = trim(TheValue);
             
   if (TheValue.length < 7 || TheValue.length > 8){ 
         gMissingFields += "\t" + FieldPrompt +"\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
    }           
   
   arDate = TheValue.split("/",3);
   
   if (arDate.length != 3){
      gMissingFields += "\t" + FieldPrompt +"\n";
      gMissingValues += TestValue + ";";
      aForm.setfocus.value = TestValue;
      return false;
   }
   
   if ((arDate[0] <= 12) && (arDate[1] <= 31) && (arDate[2].legth = 4)){
     return true;
   }
   else {
     gMissingFields += "\t" + FieldPrompt +"\n";
     gMissingValues += TestValue + ";";
     aForm.setfocus.value = TestValue;
     return false;
   }
 }

//---------------------------------------------------------------------------    
function CCHAccountValidation(TestValue,FieldPrompt)
 { var tstr;
   var FixedLength = "9";
   var TheValue;
   var ctrl;
   var aForm = rSubmitThisForm();
                
   ctrl = "TheValue = aForm." + TestValue;
   eval(ctrl);
   TheValue = trim(TheValue);
   
   // ignore this control  if disabled
   if (TheValue.disabled){
       return true;
   }
    
   TheValue = TheValue.value;
      
   if (TheValue.length < FixedLength || TheValue.length > FixedLength){ 
         gMissingFields += "\t" + FieldPrompt + "\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
    }           
   
   tstr = TheValue.substring(0,9)
   if (!rIsNumeric(tstr)){
       gMissingFields += "\t" + "Wrong format for " + FieldPrompt + "\n";
       gMissingValues += TestValue + ";";
       aForm.setfocus.value = TestValue;
       return false;
   } 
  
   return true;
 }
//---------------------------------------------------------------------------    

function PhoneFaxValidation(TestValue,FieldPrompt)
 { var tstr;
   var FixedLength = "12";
   var dchar34;
   var dchar78;
   var TheValue;
   var Cmd;
   var stcanthavech = "Can't have characters in ";             
   var aForm = rSubmitThisForm();
   
   Cmd = "TheValue = aForm." + TestValue + ".value;";
   eval(Cmd);
   TheValue = trim(TheValue);
         
   if (TheValue.length < FixedLength || TheValue.length > FixedLength){ 
         gMissingFields += "\t" + FieldPrompt + " <XXX-XXX-XXXX>\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
    }           
   
   dchar34 = TheValue.substring(3,4);
   dchar78 = TheValue.substring(7,8);
   
   if ((dchar34 != "-") || (dchar78 != "-")){
        gMissingFields += "\t" + FieldPrompt +  " not in format XXX-XXX-XXXX" + "\n";
        gMissingValues += TestValue + ";";
        aForm.setfocus.value = TestValue;
        return false;
   }  
   else { 
        tstr = TheValue.substring(0,3)+ TheValue.substring(4,7)+ TheValue.substring(8,12)
        if (!rIsNumeric(tstr)){
          gMissingFields += "\t" + stcanthavech + FieldPrompt + " <XXX-XXX-XXXX>\n";
          gMissingValues += TestValue + ";";
         // alert("gMissingValues before='" + gMissingValues + "'") 
          aForm.setfocus.value = TestValue;
         // alert("gMissingValues after='" + gMissingValues + "'") 
          return false;
        } 
   }  
   return true;
}
//---------------------------------------------------------------------------    

function CreditCardExpValidation (TestValue,FieldPrompt)
 { var dchar;
   var tstr;
   var FixedLength = "5";
   var TheValue;
   var ctrl;
   var stcanthavech = "Can't have characters in ";
   var aForm = rSubmitThisForm();
           
   ctrl = "TheValue = aForm." + TestValue;
   eval(ctrl);
   TheValue = trim(TheValue);
   
   // ignore this control  if disabled
   if (TheValue.disabled){
       return true;
   }
    
   TheValue = TheValue.value;

   if (TheValue.length < FixedLength || TheValue.length > FixedLength){ 
         gMissingFields += "\t" + FieldPrompt + "\n";
         aForm.setfocus.value = TestValue;
         return false;
   }           
   dchar = TheValue.substring(2,3);
   if (dchar != "/"){
         gMissingFields += "\t" + FieldPrompt + " not in format MM/YY" + "\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
   }  
   else { 
     tstr = TheValue.substring(0,2)+ TheValue.substring(3,5)
     if (!rIsNumeric(tstr)){
       gMissingFields += "\t" + stcanthavech + FieldPrompt + "\n";
       gMissingValues += TestValue + ";";
       aForm.setfocus.value = TestValue;
       return false;
     } 
   }  
   return true;
 }
//---------------------------------------------------------------------------    

function CreditCardValidation(TestValue,FieldPrompt)
 { var tstr;
   var FixedLength = "19";
   var dchar45;
   var dchar910;
   var dchar1415;
   var TheValue;
   var ctrl;
   var stcanthavech = "Can't have characters in ";
   var aForm = rSubmitThisForm();
             
   ctrl = "TheValue = aForm." + TestValue;
   eval(ctrl);
   TheValue = trim(TheValue);
   
   // ignore this control  if disabled
   if (TheValue.disabled){
       return true;
   }
    
   TheValue = TheValue.value;
         
   if (TheValue.length < FixedLength || TheValue.length > FixedLength){ 
         gMissingFields += "\t" + FieldPrompt + "\n";
         gMissingValues += TestValue + ";";
         aForm.setfocus.value = TestValue;
         return false;
    }           
   
   dchar45   = TheValue.substring(4,5);
   dchar910  = TheValue.substring(9,10);
   dchar1415 = TheValue.substring(14,15);
   
   if ((dchar45 != " ") || (dchar910 != " ") || (dchar1415 != " ")){
        gMissingFields += "\t" + FieldPrompt +  " not in format XXXX XXXX XXXX XXXX" + "\n";
        gMissingValues += TestValue + ";";
        aForm.setfocus.value = TestValue;
        return false;
   }  
   else { 
        tstr = TheValue.substring(0,4)+ TheValue.substring(5,9)+ TheValue.substring(10,14) + TheValue.substring(15,19)
        if (!rIsNumeric(tstr)){
          gMissingFields += "\t" + stcanthavech + FieldPrompt + "\n";
          gMissingValues += TestValue + ";";
          aForm.setfocus.value = TestValue;
          return false;
        } 
   }  
   return true;
}
//---------------------------------------------------------------------------    

function rIsNumeric (tstr) {
   var xchar;
   var i;
   var xasc;
   
   for(i=0;i<tstr.length;i++){
     xchar = tstr.substr(i,1)
     xasc = xchar.charCodeAt(0);
	 if (!((xasc >= 48) && (xasc <= 57)))
         {return false;} 
   }
   return true;
}
//---------------------------------------------------------------------------    
 function rRedoEMail ( DefaultValue ) {

    var  EMailAddr;
    var  aForm = rSubmitThisForm();

    EMailAddr = prompt("\nPlease enter your e-mail address.\n", DefaultValue );

    if ( EMailAddr == null || EMailAddr.length < 1 ) {
    
    }
    else if ( !fIsOKEMailAddress(EMailAddr) ) {

    }
    else {

        aForm.ForceEMail.value = EMailAddr;
        
        aForm.CustomerName.value    = "";																																					                                                                                                                                                                                                                                              
        aForm.CustomerCompany.value = "";
        aForm.CustomerZipcode.value = "";
        aForm.CustomerStreet1.value = "";
        aForm.CustomerStreet2.value = "";
        aForm.CustomerCity.value    = "";
        aForm.CustomerState.value   = "";
        aForm.CustomerPhone.value   = "";
        aForm.CustomerTitle.value   = "";
        aForm.CustomerEMail.value   = "";
        
		rResubmitThisForm();
    }
}

//----------------- trim -----------------
function trim (xstr) {
  var xchar;
  var i;
  var start_pos;
  var end_pos;
  
  xchar = " ";
  
  end_pos = xstr.length - 1;
  start_pos = end_pos;
  
  for(i=0;i<xstr.length;i++){
     if (xstr.substr(i,1) != xchar){
       start_pos = i;
       break;  
      } 
  }

  xstr =  xstr.substr(start_pos,end_pos - start_pos + 1);
   
  end_pos = xstr.length - 1;
  for(i=xstr.length-1;i>-1;i--){
     if (xstr.substr(i,1) != xchar){
       end_pos = i;
       break;  
      } 
  }
  xstr =  xstr.substr(0,end_pos + 1);
  return xstr;
}

