
// Issues: 
// Would like to print instructions
// High level US map displays before route map displays

var VisibleContainer = 'WorkingArea';
var AlertSourceID;

var Loader ;

var Title = "  Angeles Shooting Ranges\n12651 Little Tujunga Cyn Rd\n        Sylmar, CA 91342";

var OrientationsMapContainer = "OrientationMap";
var DirectionsMapContainer = "DirectionsMap";
var DirectionsInstructionsContainer = "DirectionsInstructions" ;

var MapWidth ;
var MapHeight ;
var Latitude = 34.309097;
var Longitude = -118.343435 ;
var ZoomLevel = 12;
var AddressContainer = "TheStartAddress" ;
var AddressPrefix = "from: ";
var AddressSuffix = " to: 12651 Little Tujunga Cyn Rd, Sylmar, CA 91342";

function LoadPageElements(ID)
{
   window.clearTimeout(Loader) ;

   switch(ID)
   {
       case 1:
        DoResize() ;
        Loader = window.setTimeout('LoadPageElements(2)',3000) ;
       break ;
       case 2:
       LoadMap(OrientationsMapContainer, Title, MapWidth, MapHeight, Latitude, Longitude, ZoomLevel,"hidden") ;
       break ;
   }
}

function PreloadPageElements() 
{  
      SetWindowSizeHandler(DoResize) ;
       
      Loader = window.setTimeout('LoadPageElements(1)',1000) ;
      MapWidth = GetPropertyFromStyleSheet(0, OrientationsMapContainer, 'width') ;
      MapHeight = GetPropertyFromStyleSheet(0, OrientationsMapContainer, 'height') ;
}

function UnloadPageElements()
{
  GUnload() ;
}

function OnClick(Element)
{   
    if ( Element.id == 'Header' )
        SwitchVisibleArea('WorkingArea') ;

    if ( Element.id == 'Logo' )
        SwitchVisibleArea('WorkingArea') ;
    if ( Element.id == 'Title' )
        SwitchVisibleArea('WorkingArea') ;
    if ( Element.id == 'SubTitle' )
        SwitchVisibleArea('WorkingArea') ;
      
   if ( Element.id == 'MenuItem11' )
        SwitchVisibleArea('ContactContainer') ;
    if ( Element.id == 'SendMessage' )
        SendTheMessage() ;
  
    if ( Element.id == 'MenuItem12' )
        SwitchVisibleArea('DirectionsContainer') ;
     if ( Element.id == 'MenuItem13' )
        SwitchVisibleArea('LinksContainer') ;
     if ( Element.id == 'MenuItem14' )
        SwitchVisibleArea('OrderContainer') ;
     if ( Element.id == 'MenuItem15' )
        SwitchVisibleArea('DocumentationContainer') ;
     if ( Element.id == 'MenuItem16' )
        SwitchVisibleArea('FeesContainer') ;
     if ( Element.id == 'MenuItem17' )
        SwitchVisibleArea('ArticlesContainer') ;

     if ( Element.id == 'MenuItem21' )
          LoadMap(OrientationsMapContainer, Title, MapWidth, MapHeight, Latitude, Longitude, ZoomLevel,"visible") ;
     if ( Element.id == 'MenuItem22' )
        LoadDirections("Map", DirectionsMapContainer, Title, Latitude, Longitude, AddressContainer) ;
     if ( Element.id == 'MenuItem23' )
         LoadDirections("Instructions", DirectionsInstructionsContainer, Title, Latitude, Longitude, AddressContainer) ;
 
     if ( Element.id == 'SendOrder' )
        SendTheOrder() ;
   
    if ( Element.id == 'AlertButton' )
        HandleAlertButton() ;
}

function LoadMap(Container, Title, MapWidth, MapHeight, Latitude, Longitude, ZoomLevel, Visibility)
{
      if (GBrowserIsCompatible()) 
      {
        SetElementVisibility(CurrentDiv,"hidden") ;
        PutInnerHTML(CurrentDiv,"");

        var Map = new GMap2(document.getElementById(Container),{ size: new GSize(MapWidth, MapHeight) } );
        Map.setCenter(new GLatLng( Latitude, Longitude), ZoomLevel);
        var TopRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
        Map.addControl(new GLargeMapControl(),TopRight);
        var Marker = new GMarker( Map.getCenter(), { title: Title } );
        Map.addOverlay(Marker);
        
        CurrentDiv = Container;
        SetElementVisibility(CurrentDiv,Visibility) ;
      }
}

var CurrentDiv = OrientationsMapContainer;

function LoadDirections(OutputType, OutputContainer, Title, atitude, Longitude, AddressContainer)
{
      if (GBrowserIsCompatible()) 
      {
        var StartAddress = document.getElementById(AddressContainer).value;
        if (StartAddress != "")
        {
           SetElementVisibility(CurrentDiv,"hidden") ;
           PutInnerHTML(CurrentDiv,"");
                
           var Directions ;
            
            var Container = document.getElementById(OutputContainer);   
            if (OutputType == "Map")
            {
                var DirectionsMap = new GMap2(Container,{ size: new GSize(MapWidth, MapHeight) } );
                DirectionsMap.setCenter(new GLatLng( Latitude, Longitude), 3);
                Directions = new GDirections(DirectionsMap,null);
            }
            if (OutputType == "Instructions")
               Directions = new GDirections(null,Container);
               
            StartAddress = AddressPrefix + StartAddress + AddressSuffix ;
            Directions.load(StartAddress);
            
            CurrentDiv = OutputContainer;
            SetElementVisibility(CurrentDiv,"visible") ;
     }
   }
}
function HandleAlertButton()
{    
    HideAlertBox() ;
    
    if (AlertSourceID == 1)
    {
      SwitchVisibleArea('WorkingArea') ;
      DisableContactGUI(false) ;
      ClearContactGUI() ;
   }
     if (AlertSourceID == 2)
    {
      SwitchVisibleArea('WorkingArea') ;
      DisableOrderGUI(false) ;
      ClearOrderGUI() ;
   }
   
    AlertSourceID = -1 ;
}

function SendTheMessage()
{
   var Name = GetTheTextContents('Name') ;
   var Address = GetTheTextContents('Address') ;
   var Message = GetTheTextContents('Message') ;

   var Status = CheckMessageValues(Name, Address, Message);
   if ( Status == 'OK')
       FFLMain.EventHandler.SendMail(Name, Address, Message, SendMessageSucceededCallback) ; 
}
function CheckMessageValues(Name, Address,Message)
{
   var Status = 'OK';
   
   if ((Name === undefined) || (Name == ""))
   {
        DisplayAlertBox('Message Incomplete','Name Empty',270,220,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
   if ((Address === undefined) || (Address == ""))
   {
        DisplayAlertBox('Incomplete','Address Empty',250,225,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
   if ((Message === undefined) || (Message == ""))
   {
        DisplayAlertBox('Incomplete','Message Empty',250,225,200,125) ;
        Status = 'NotOK' ;
        return Status ;
  }

   return Status ;
}

function SendMessageSucceededCallback(Result)
{
   DisableContactGUI(true) ;
   AlertSourceID = 1 ;

   DisplayAlertBox('MessageSent',Result,250,225,200,125) ;
}

function ClearContactGUI()
{
      ClearTextBox('Name') ;
      ClearTextBox('Address') ;
      ClearTextBox('Message') ;
}

function DisableContactGUI(Mode)
{
    document.getElementById('Name').disabled = Mode ;
    document.getElementById('Address').disabled = Mode ;
    document.getElementById('Message').disabled = Mode ;
}

function SendTheOrder()
{
   var Name = GetTheTextContents('TheOrderName') ;
   var Address = GetTheTextContents('TheOrderAddress') ;
   var Phone = GetTheTextContents('TheOrderPhone') ;
   var MakeAndModel = GetTheTextContents('TheMakeAndModel');
   var Caliber = GetTheTextContents('TheCaliber') ;
   var OtherFeatures = GetTheTextContents('TheOtherFeatures') ;
   
   var Status = CheckOrderValues(Name, Address, Phone, MakeAndModel,Caliber, OtherFeatures);
   if ( Status == 'OK')
       FFLMain.EventHandler.SendOrder(Name, Address, Phone, MakeAndModel,Caliber, OtherFeatures, SendOrderSucceededCallback) ; 
}


function CheckOrderValues(Name, Address, Phone, MakeAndModel,Caliber, OtherFeatures)
{
   var Status = 'OK';
   
   if ((Name === undefined) || (Name == ""))
   {
        DisplayAlertBox('Message Incomplete','Name Empty',270,220,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
   if ((Address === undefined) || (Address == ""))
   {
        DisplayAlertBox('Incomplete','Address Empty',250,225,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
   if ((Phone === undefined) || (Phone == ""))
   {
        DisplayAlertBox('Incomplete','Phone Empty',250,225,200,125) ;
        Status = 'NotOK' ;
        return Status ;
  }
   if ((MakeAndModel === undefined) || (MakeAndModel == ""))
   {
        DisplayAlertBox('Message Incomplete','Make And Model Empty',270,220,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
   if ((Caliber === undefined) || (Caliber == ""))
   {
        DisplayAlertBox('Incomplete','Caliber Empty',250,225,200,125) ;
        Status = 'NotOK' ;
        return Status ;
   }
// Other Features May Be Empty

   return Status ;
}

function SendOrderSucceededCallback(Result)
{
   DisableOrderGUI(true) ;
   AlertSourceID = 2 ;

   DisplayAlertBox('OrderSent',Result,250,225,200,125) ;
}

function ClearOrderGUI()
{
      ClearTextBox('TheOrderName') ;
      ClearTextBox('TheOrderAddress') ;
      ClearTextBox('TheOrderPhone') ;
      ClearTextBox('TheMakeAndModel');
      ClearTextBox('TheCaliber') ;
      ClearTextBox('TheOtherFeatures') ;
}

function DisableOrderGUI(Mode)
{
    document.getElementById('TheOrderName').disabled = Mode ;
    document.getElementById('TheOrderAddress').disabled = Mode ;
    document.getElementById('TheOrderPhone').disabled = Mode ;
    document.getElementById('TheMakeAndModel').disabled = Mode ;
    document.getElementById('TheCaliber').disabled = Mode ;
    document.getElementById('TheOtherFeatures').disabled = Mode ;
}

/*********************** Standard Functions ************************/

function DoResize()
{
   var Width = GetPropertyFromStyleSheet(0, 'Window', 'width');
   var Height = GetPropertyFromStyleSheet(0, 'Window', 'height');

   window.resizeTo(Width,Height);   
}

function MenuItemMouseOver(Element)
{
	SetElementClass(Element,'MenuItemBtnOn', Element.id);
}
function MenuItemMouseOut(Element)
{
	SetElementClass(Element,'MenuItemBtnOff', Element.id);
}

/*********************** Support Functions ************************/


function SwitchVisibleArea(NewArea)
{
  if (VisibleContainer == 'WorkingArea')
  {
     SetWorkingAreaVisibility('hidden') ;
  }
  else
  {
     if (VisibleContainer == 'DirectionsContainer')
        SetElementVisibility(CurrentDiv,'hidden');
       
     SetElementVisibility(VisibleContainer,'hidden') ;
  }
  if ( NewArea == 'WorkingArea' )
  {
     SetWorkingAreaVisibility('visible') ;
     VisibleContainer = 'WorkingArea';
  }
  else
  {
    if (NewArea == 'DirectionsContainer')
        SetElementVisibility(CurrentDiv,'visible');
        
    SetElementVisibility(NewArea,'visible') ;
    VisibleContainer = NewArea ;
  }
}

function SetWorkingAreaVisibility(Status)
{
   SetElementVisibility('PageBody',Status) ;
}
