﻿//Created By:   ltello, 9/6/07
//Modified By:  sreichert, 9/10/07. Created statesList object to encapsulate all States dropdown methods.
//Modified By:  sreichert, 9/13/07. Added ajax.net function calls.
//Modified By:  sreichert, 9/25/08. Added calendar table formatting code to PageLoaded funciton
//*** Note: Some methods within this script depend on the ajax.net script includes, 
//          i.e. $get (document.getElementById shorthand)

// instantiate a variable to track the state filter, if present.
var stateFilter = null;

//instantiate a new StatesList object
//var statesList = new StatesList;

//instantiate variable to store the clicked element
var elemClicked;
var CalendarEventId = "";

//Summary:      Ajax.net Library function;
//              - Raised before the processing of an asynchronous postback starts and the 
//                postback request is sent to the server.
//              - Stores the clicked element
//Created By:   sreichert, 9/13/07
function BeginRequestHandler(sender, args){
    var elem = args.get_postBackElement();
    elemClicked = $get(elem.id);
    
    //set calendar element to eventargument value
    CalendarEventId = theForm.__EVENTARGUMENT.value;
}

//Summary:      Ajax.net Library function; 
//              - Raised after an asynchronous postback is finished and control has been returned to the browser.
//              - Collpases or expands the states dropdown based on the clicked element
//Created By:   sreichert, 9/13/07
//Modified By:  sreichert, 9/25/07
function EndRequestHandler(sender, args){
//    if(elemClicked.id.endsWith('APPLY_BTN')){
//        statesList.expand();
//    }
//    else{
//        statesList.collapse();
//    }
//    
//    //if CalendarEventId is an id of an event (4 characters long), scroll page to event
//    if(CalendarEventId !=""){
//      if(CalendarEventId.length==4){
//        //Get browser version
//        var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
//        
////        if (version < 7) {
////          // IE6, older browsers
////          window.scrollTo(0,320);
////        }
////        else{
////          // IE 7, mozilla, safari, opera 9
////          window.scrollTo(0,150);
////        }
//        window.scrollTo(0,3000);
//      }
//    }
}

//Summary:      Ajax library function;
//              - If page is loaded for the first time, all states are checked and dropdown will open.
//Created by:   sreichert, 9/7/07
//Modified by:  sreichert, 9/13/07
function PageLoaded(sender, args){
    
    //Summary: This code removes white border from calendar header. Will be called on all postbacks.
    //Created by ltello, implemented by sreichert on 9/25/07
    var myArr = document.getElementsByTagName("table");
    var len = myArr.length;
    for(var i=0; i<len; i++){
      if(myArr[i].id.indexOf('EventCalendar') != -1){
         var myCalendar = myArr[i];
         var ie6=document.all
         var ff=document.getElementById&&!document.all
         if(!ie6){
            var test = myCalendar.childNodes[1].childNodes[0].childNodes[0];
            test.style.background = "";
         }
         
      }
    }
    
    //Get instance of the page requestm manager
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    
    //If not postback  
    if (!prm.get_isInAsyncPostBack()) {
        
        // If the state filter is null, select all the states.  If the state filter
        // was set, respect it and simply expand the state selection.
        
     //   if (stateFilter == null){
    //      statesList.checkall(true);
    //    }
    //    else{
       //   statesList.expand();
    //    }
    }
    
    //display no results message if no events are loaded
    if($get("cal_results").innerHTML == "" || $get("cal_results").innerText == ""){
      $get("lblNoResults").style.display="block";
    }
    else{
      $get("lblNoResults").style.display="none";
    }
        
} 

//Add the ajax.net javascript event handlers
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(PageLoaded);

//Summary:      The StatesList object will retain all methods associated with the states dropdown
//Created by:   sreichert, 9/10/2007
function StatesList()
{   
    //Summary:      Expands the dropdown 
    //Created by:   ltello 9/6/07 
    //Modified by:  sreichert, 9/11/07
    this.expand = function(){
        $get("collapse").style.display = 'none';
        $get("expand").style.display = 'block';
    }
    
    //Summary:      Collapses the dropdown
    //Created by:   ltello 9/6/07
    //Modified by:  sreichert, 9/11/07
    this.collapse = function(){
        $get("expand").style.display = 'none';
        $get("collapse").style.display = 'block';
    }
    
    //Summary:      Either checks off or unchecks all states in the states table based on true or false
    //Created by:   sreichert, 9/7/07
    //inputs:       boolean value for checked or unchecked
    this.checkall = function(checked){
        var states = this.getAllStates();
        var states_len = states.length;
    
        for (var i = 0; i < states_len; i++) {
            states[i].checked = checked;
        }
    }
    
    //Summary:      Retrieves all states from the table and returns an array of all state inputs
    //Created By:   sreichert, 9/7/07
    //Returns:      states array
    this.getAllStates = function(){
        var tables = document.getElementsByTagName('table');
        var tables_len = tables.length;
        var states;
        for(i=0;i<tables_len;i++){
            if(tables[i].id.indexOf('chkStates') != -1){
                var states = tables[i].getElementsByTagName('input');
            }
        }
        
        return states;
    }
}
