var supported = navigator.userAgent.indexOf("Mozilla")==0 && navigator.userAgent.substring(8,9) >= 3;

      function openWin(HelpFile) {
      if(supported)
        {
         window.open(HelpFile,"howto","resizable=no,scrollbars=yes,status=no,height=500,width=550");
         return;
        }
      else
         return;
      }


function openPopupWin(PopupFile, Title, width, height, Left, Top, scrollbar) 
{
	if(supported)
	{
		if (width==null)
			width=395;
	
		if (height==null)
			height=395;
	
		if (Left==null)
			Left= (screen.width-width)/2;
	
		if (Top==null)
			Top= (screen.height-height)/2;

		if (scrollbar==null)
			scrollbar=0

		window.open(PopupFile, Title,"direction=no,menubar=no,location=no,toolbar=no,resizable=no,status=no,scrollbars="+scrollbar+",height="+height+",width="+width+",left="+Left+",ScreenX="+Left+",top="+Top+",ScreenY="+Top);
		return;
	}
	else
		return;
}

	var sErrMsg				   //Error message variable	

function Compute(sSubmitVar)
{
 form = document.frmEntry 
 if (sSubmitVar.toUpperCase() == "CLEAR")  
   {
    form.OriginZip.value = ""
    form.DestZip.value = ""
    nSubmitFlag = 0   
    return 
   }
  else
   { 
    if (!ValidateZips())
      {
       alert(sErrMsg)
       return  
      }
     else
      {
       SetCountry()                          //Set country codes  
       form.Button.value = "SUBMIT"   
       form.submit()
      }    
   }
}

function HelpWin(HelpFile)
{
 // Bring up help window with appropriate helpfile:
 apopup = window.open("/rating/rtt/" + HelpFile,"note","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=225,height=200");

 // Bring help window to front of z-order for Netscape browser:
 if ("Netscape" == navigator.appName)
   apopup.focus();
}

function IsAlpha(sAlphaVar)       
{
 //Test for alpha character
 var sAlphaCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"   
 var X

 sAlphaVar = sAlphaVar.toUpperCase() 
 for (X=0; X < sAlphaVar.length; X++)
    {  
     if (sAlphaCharSet.lastIndexOf(sAlphaVar.charAt(X)) == -1)
       {
        return false
       }
    } 
 return true
}

function IsNumeric(sNumVar)       
{
 //Test for numeric character
 var sNumCharSet = "0123456789"                     
 var X 

 for (X=0; X < sNumVar.length; X++)
    {
     if (sNumCharSet.lastIndexOf(sNumVar.charAt(X)) == -1)
        {
         return false
        }
    }
 return true 
}

function SetCountry()
{
 //Set Origin country code
 if (IsAlpha(form.OriginZip.value.charAt(0)))
   {
    form.OriginCountry.value = "CA"
   }       
  else
   {
    form.OriginCountry.value = "US"
   }

 //Set Destination country code
 if (IsAlpha(form.DestZip.value.charAt(0)))
   {
    form.DestCountry.value = "CA"
   }     
  else
   {
    form.DestCountry.value = "US"
   }
 return
}

function ValidateUSZips()
{
  sErrMsg = ""
  if (form.OriginZip.value == "" || form.DestZip.value == "")
     {
      sErrMsg = "Origin ZIP/Postal code and destination ZIP/Postal code fields are required!"
      return false
     }
  if (form.OriginZip.value.length != 5 || !IsNumeric(form.OriginZip.value))
      {
       sErrMsg = "An invalid US origin ZIP/Postal code was entered!"
       return false
      } 
  if (form.DestZip.value.length != 5 || !IsNumeric(form.DestZip.value))
      {
       sErrMsg = "An invalid US destination ZIP/Postal code was entered!"
       return false
      }
  return true
}


function ValidateZips()
{
 sErrMsg = ""
 if (form.OriginZip.value == "" || form.DestZip.value == "")
   {
    sErrMsg = "Origin ZIP/Postal code and destination ZIP/Postal code fields are required!"
    return false
   }
 //Validate for canadian origin postal code if first character is alpha. 
 if (IsAlpha(form.OriginZip.value.charAt(0)))
   {
    if (form.OriginZip.value.length != 6 && form.OriginZip.value.length != 7)
      {
       sErrMsg = "An invalid origin ZIP/Postal code was entered!"
       return false
      } 
    //Validate A1A1A1 format 
    if (form.OriginZip.value.length == 6) 
      {
       if (!IsNumeric(form.OriginZip.value.charAt(1)) || !IsAlpha(form.OriginZip.value.charAt(2)) || !IsNumeric(form.OriginZip.value.charAt(3)) || !IsAlpha(form.OriginZip.value.charAt(4)) || !IsNumeric(form.OriginZip.value.charAt(5)))
         {
          sErrMsg = "An invalid origin ZIP/Postal code was entered!" 
          return false
         } 
      } 
    //Validate A1A 1A1 format
    if (form.OriginZip.value.length == 7) 
      {
       if (!IsNumeric(form.OriginZip.value.charAt(1)) || !IsAlpha(form.OriginZip.value.charAt(2)) || form.OriginZip.value.charAt(3) != " " || !IsNumeric(form.OriginZip.value.charAt(4)) || !IsAlpha(form.OriginZip.value.charAt(5)) || !IsNumeric(form.OriginZip.value.charAt(6)))
         {
          sErrMsg = "An invalid origin ZIP/Postal code was entered!" 
          return false
         }
      } 
   }
  else 
   {
    if (form.OriginZip.value.length != 5 || !IsNumeric(form.OriginZip.value))
      {
       sErrMsg = "An invalid origin ZIP/Postal code was entered!"
       return false
      }      
   }
 //Validate for canadian destination postal code if first character is alpha.
 if (IsAlpha(form.DestZip.value.charAt(0)))
   {
    if (form.DestZip.value.length != 6 && form.DestZip.value.length != 7)
      {
       sErrMsg = "An invalid destination ZIP/Postal code was entered!"
       return false
      } 
    //Validate for A1A1A1 format
    if (form.DestZip.value.length == 6)
      { 
       if (!IsNumeric(form.DestZip.value.charAt(1)) || !IsAlpha(form.DestZip.value.charAt(2)) || !IsNumeric(form.DestZip.value.charAt(3)) || !IsAlpha(form.DestZip.value.charAt(4)) || !IsNumeric(form.DestZip.value.charAt(5)))
         {
          sErrMsg = "An invalid destination ZIP/Postal code was entered!" 
          return false
         }       
      }
    //Validate for A1A 1A1 format 
    if (form.DestZip.value.length == 7)
      { 
       if (!IsNumeric(form.DestZip.value.charAt(1)) || !IsAlpha(form.DestZip.value.charAt(2)) || form.DestZip.value.charAt(3) != " " || !IsNumeric(form.DestZip.value.charAt(4)) || !IsAlpha(form.DestZip.value.charAt(5)) || !IsNumeric(form.DestZip.value.charAt(6)))
         {
          sErrMsg = "An invalid destination ZIP/Postal code was entered!" 
          return false
         }       
      }
   } 
  else
   {   
    if (form.DestZip.value.length != 5 || !IsNumeric(form.DestZip.value))
      {
       sErrMsg = "An invalid destination ZIP/Postal code was entered!"
       return false
      }
   } 
 return true
}