

var spanUserFeedback = null
var spanPageControls = null
var divScroll = null
var strReturnClass = null
var imgPageGraphic = null

var strActiveCellStyle  =  "";

// Context menu global variables
var blnContextMenuActive = false;
var objContextMenu = null;
var objSelectedCell = null;

// Ajax variables
var objXMLHttp = null


function initialisePage()
   {
   spanUserFeedback = document.getElementById("span_user_feedback")
   spanPageControls = document.getElementById("span_page_controls")
   divScroll = document.getElementById("div_scroll")
   imgPageGraphic = document.getElementById("img_page_graphic")

   setDimensions()
   }

function setClass(strClassName)
   {
   strReturnClass  = event.srcElement.className
   event.srcElement.className = strClassName
   }

function returnClass()
   {
   if(strReturnClass!=null)
      {
      event.srcElement.className = strReturnClass

      strReturnClass = null
      }
   }

function setDimensions()
   {
   divScroll.style.height = (450- (spanPageControls.offsetHeight+3+spanUserFeedback.offsetHeight));

   if(document.body.clientHeight <= document.getElementById("div_content_general").offsetHeight)
      {
      document.body.scroll="yes"
      }
   else if(document.body.scroll="yes")
      {
      document.body.scroll="no"
      }
   
   }
function highlightRow(objRow, blnApply)
   {
   if(blnApply)
      {
      objRow.style.backgroundColor = "#416341"
      for(i=0;i<objRow.cells.length;i++)
         {
         objRow.cells(i).style.color = "#ffffff"
         }
      }
   else
      {
      objRow.style.backgroundColor = ""
      for(i=0;i<objRow.cells.length;i++)
         {
         objRow.cells(i).style.color = ""
         }
      }
   }

function formDirty(blnValue)
   {
   if(blnValue == true || blnValue == false)      
      {
      m_blnFormDirty = blnValue
      }
   return m_blnFormDirty
   }


window.onbeforeunload = function()
{
if(m_blnCatchUnload && formDirty())
   {
   event.returnValue = "You have unsaved changes which will be lost if you continue.  Are you sure you want to exit?"
   }
}

window.onresize = function()
   {
   setDimensions()
   }


// generic XMLHttp function
// ---------------------------------------------------------------------

function doAJAX(strURL, strMethod, blnAsynch, a_objNameValues, strResponseFunction)
   {
   // create an XMLHTTPRequest object
   objXMLHttp = getXMLHttp();

   // check object existence
   if(objXMLHttp)
      {
      // set function to monitor the status of the object
      if(strResponseFunction!=null)
         {
         objXMLHttp.onreadystatechange = eval(strResponseFunction);
         }

      objXMLHttp.open(strMethod, strURL, blnAsynch);

      // set request header if using the post method
      if(strMethod == "POST")
         {
         objXMLHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
         }

      // loop over the parameter array
      for(intParamIndex=0;intParamIndex<a_objNameValues.length;intParamIndex++)
         {
         // todo:  replace the setQueryStringParam function with a custom written function
         setQueryStringParam(a_objNameValues[intParamIndex][0], a_objNameValues[intParamIndex][1])                  
         }

      objXMLHttp.send(m_strQuerystring.replace("?", "")) 
      }
   }


// ajax global variables
// ---------------------
var intActionID = null;
var objScheduleDate = null;

function doBookingAction(intActionID, strDate, intBookingID, intVenueID)
   {

   // remove underscores from date
   while(strDate.indexOf("_") > 1)
      {
      strDate = strDate.replace("_", " ")
      }
 
   objScheduleDate = new Date(strDate);

   // create a parameter array
   var a_varParamArray = new Array()

   a_varParamArray[0] = ["txt_action_id", intActionID]
   a_varParamArray[1] = ["txt_booking_id", intBookingID]      
   a_varParamArray[2] = ["lst_year", objScheduleDate.getYear()]
   a_varParamArray[3] = ["lst_month", objScheduleDate.getMonth()+1]
   a_varParamArray[4] = ["lst_date", objScheduleDate.getDate()]
   a_varParamArray[5] = ["lst_time_on", "21:30"]
   a_varParamArray[6] = ["lst_time_off", "00:00"]
   a_varParamArray[7] = ["chk_reserved", "true"]
   a_varParamArray[8] = ["blnAjax", "true"]

   doAJAX("/grc/admin/schedule_admin.htm.asp", "POST", true, a_varParamArray, "doBookingActionResponse")
   }

function doBookingActionResponse()
   {
   if(objXMLHttp.readyState==4)
      {
      // request has been completed
      if(objXMLHttp.status==200)
         {

         
         /*
         var intActionID      = extract(objXMLHttp.responseText, ",", 0)         
         var intErrorCode     = extract(objXMLHttp.responseText, ",", 1)
         var strDescription   = extract(objXMLHttp.responseText, ",", 2)
         var intBookingID     = extract(objXMLHttp.responseText, ",", 3)
         var intVenueID       = extract(objXMLHttp.responseText, ",", 4)

         // request has reported a successful response
         if(intErrorCode==0)
            {
            
            var strCellID = padChar(objScheduleDate.getDate().toString(), "0", 2) + "_" + a_MonthNames[objScheduleDate.getMonth()] + "_" + objScheduleDate.getYear()
            var strNewCellID = padChar(objScheduleDate.getDate().toString(), "0", 2) + "_" + a_MonthNames[objScheduleDate.getMonth()] + "_" + objScheduleDate.getYear()
            
            if(intActionID==1)
               {
               strCellID += "_0"
               strNewCellID += "_" + intBookingID
               }

            if(intActionID==3)
               {
               strCellID += "_" + intBookingID
               strNewCellID += "_0"
               }
         
            var objSelectedCell = document.getElementById(strCellID)
                 
            if(objSelectedCell)
               {
               // modify the cells oncontextmenu event handler arguments
               objSelectedCell.id = strNewCellID

               objSelectedCell.onmouseover = null;
               objSelectedCell.onmouseout = null;

               if(intActionID == 2 || intActionID == 1)
                  {
                  // date has been inserted or updated...

                  if(intVenueID < 0 && intBookingID>0)
                     {

                     // date reserved!
                     // cannot get the element to accept a new class name;  set properties individually in the meantime.
                     // objSelectedCell.className = "reservedDate";
                     
                     objSelectedCell.style.backgroundColor = "#666699";
                     objSelectedCell.style.border = "1px solid #000066";
                     objSelectedCell.style.color = "#ffffff";
                     objSelectedCell.style.cursor = "hand";

                     objSelectedCell.title = "This date has been reserved";
                     }   
                  }
               else if(intActionID == 3)
                  {
                  // date has been deleted
                  if(intVenueID < 0 && intBookingID>0)
                     {
                     // reservation has been deleted!

                     if((objScheduleDate.getDate()+1)%2==0)
                        {
                        objSelectedCell.style.backgroundColor = "#ccccff";
                        }
                     else
                        {
                        objSelectedCell.style.backgroundColor = "#ffffff";                        
                        }

                        objSelectedCell.style.border = "1px solid #ffffff";
                        objSelectedCell.style.color = "#666666";
                        objSelectedCell.style.cursor = "hand";                        

                     objSelectedCell.title = "";
                     }
                  }
               }
            else
               {
               setFeedback("An error has occurred.  Please refresh the page to determine whether reservation has been saved.")
               }
            }
         else
            {
            setFeedback("Operation failed!")
            }
         */
         }
      else
         {
         setFeedback(objXMLHttp.statusText + "<br>" + objXMLHttp.responseText)
         }
      }
   
   cancelContextMenu()   
   }


function showBookingControl(strDate, intPerformanceID)
   {
   if(objContextMenu==null)
      {
      objContextMenu = document.createElement("DIV")
      objContextMenu.style.position = "absolute";

      // prevent button actions on the menu bubbling up to the document;
      objContextMenu.onclick = cancelBubble;

      // style the menu
      objContextMenu.style.border = "1px solid #666666";
      objContextMenu.style.backgroundColor = "#fcfcfc";
      objContextMenu.style.padding = "2px";
      objContextMenu.style.display = "none";
      
      // add element to the document
      document.body.appendChild(objContextMenu)
      }

   // remove any previous context menu
   cancelContextMenu()

   blnContextMenuActive = true;

   // position the context menu div
   objContextMenu.style.top = event.clientY
   objContextMenu.style.left = event.clientX   
   
   var a_varParamArray = new Array();
   a_varParamArray[0] = ["date", strDate]      

   doAJAX("ajax_schedule_admin.htm.asp", "POST", true, a_varParamArray, "monitorStateChange")
   }


function cancelContextMenu()
   {
   // 'kill' the context menu
   objContextMenu.style.display = "none";
   objContextMenu.innerHTML = "";

   // flag the context menu as inactive
   blnContextMenuActive = false;      

   // reset the style on the selected cell
   highlightCell(objSelectedCell, false);
   }