//File: ajaxfunctions.js
//Author: Eric Trivette
//Date: July 6, 2007
//Description: Contains javascript functions used by the application to pull data for pages at runtime.
//Javascript for creating the XMLHttpRequest object from W3C article: http://www.w3schools.com/ajax/default.asp
//Modifications:
//
//Javascript functions for the Ajax functionality of the Interested in participating and maps pages.
var xmlHttp;

//Creates an xml http object for sending requests to the server. 
function CreateAjaxObject()
{  
   var xmlObject;
   try
   {    
      // Firefox, Opera 8.0+, Safari    
      xmlObject=new XMLHttpRequest();    
   }
   catch (e)
   {
      // Internet Explorer    
      try
      {      
         xmlObject=new ActiveXObject("Msxml2.XMLHTTP");      }
      catch (e)
      {      
         try
         { 
            xmlObject=new ActiveXObject("Microsoft.XMLHTTP");        }
         catch (e)
         { 
             alert("Your browser does not support functionality required by this website.");        
             return null;        
         }
      }    
    }
    return xmlObject;  
}

//Takes the server response for the interested in participating page and fills the industry drop down list.
//It also saves the response in a hidden field for usage later on the server side to populate the drop down list objects.
function FillInterestedInParticipatingData()
{
   if(xmlHttp.readyState==4)
   {
        // Get the data from the server's response and place it in the correct place (the industry drop down list)
        document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList").disabled = false;
	    //document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList").innerHTML=xmlHttp.responseText;
	    ClearOptions(document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList"));
	    //Parse the response text to build new options
	    
	    //Array to hold pairs of text/value
	    var Pairs = new Array();
	    //Get set of pairs of text/value by splitting at semi colon
	    Pairs = xmlHttp.responseText.split(";");
	    document.getElementById("ctl00__pageContentContentPlaceHolder__indusrtyListHiddenField").value = xmlHttp.responseText;
	    var DropDown = document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList");
	    
	    //Cycle through all pairs, creating new options.
	    for(i = 0; i < Pairs.length; i++)
	    {
	        //Split the text/value pairs.
	        pair = new Array();
	        pair = Pairs[i].split(":");
	        //Add a new option.
	        DropDown.options[i] = new Option(pair[0], pair[1], false, false);
	    }
   }
}



//Function: ClearOptions
//Description: Clears out all options of a drop down list by setting all of the options to null.
//Parameters: SelectToClear - the drop down list to clear out.
function ClearOptions(SelectToClear)
{
    for(i = 0; i < SelectToClear.options.length; i++)
    {
        SelectToClear.remove(i);
        i--;
    }
}

//Handles the server response for the maps page
function GenericMapsEventHandler()
{
    if(xmlHttp.readyState == 4)
    {
        //Get the list of values from the response text, the values are delimited by semicolons
        var Values = new Array();
        Values = xmlHttp.responseText.split(";");
        
        //The first value is the client ID of the drop down that caused the event.
        var MapDropDown = document.getElementById(Values[0]);
        //Enable the correct drop down lists and return the drop down list that is to be filled with data.
        MapDropDown = SetDropDownEnabledStates(MapDropDown);
        //Clear the one that is about to be filled
        ClearOptions(MapDropDown);
        //Add an empty option so the user doesn't default to empty
        MapDropDown.options[0] = new Option("", "", false, false);
        //Add the new options to the drop down
        for(i = 1; i < Values.length; i++)
        {
            MapDropDown.options[i] = new Option(Values[i], Values[i], false, false);    
        }
        //Enable the drop down
        MapDropDown.disabled = false;
        
        //Stop the loader image for the drop down list.
        StopLoaderImage(MapDropDown.id + "_Loader");
    }
}


//Sets the enabled/disabled states of the drop down lists of the page to the correct settings. All drop downs above the current on that is to be filled
//should be already filled and enabled. The ones below should be disabled.
function SetDropDownEnabledStates(DropDownListChanged)
{
    //variable to hold the drop down list that will be returned. The one below the drop down that was just changed.
    //The drop down that is to be filled on this event cycle
    var DropDownToReturn
    //Get all select tags on the page
    var DropDowns = document.getElementsByTagName("select");
    //Cycle through all select tags, if the current one is equal to the one that was changed...
    for(i = 0; i < DropDowns.length; i++)
    {
      if(DropDowns[i] == DropDownListChanged)
      {
          //Then save the next drop down in line as the one to return
          DropDownToReturn = DropDowns[i + 1];
          //Disable all drop downs after that one
          for(j = i + 2; j < DropDowns.length; j++)
          {
              DisableDropDown(DropDowns[j]);
          }
          break;
      }
   }
   //Return the saved drop down list
   return DropDownToReturn;
}


//Calls the server for data for the reports page. DropDownListChanged contains the drop down list javascript DOM object that was
//changed.
function PullMapsData(DropDownListChanged)
{
   //Hide the error message if it was displaying since the user has changed their choice
   HideErrorMessage();
   
   //Get all select tags on the page so we can disable the ones below the one that got changed
   var DropDowns = document.getElementsByTagName("select");
   //Disable all drop downs below the one that was changed.
   for(i = 0; i < DropDowns.length; i++)
   {
      if(DropDowns[i] == DropDownListChanged)
      {
          if(!(DropDownListChanged.options[DropDownListChanged.selectedIndex].text == ""))
          {
              StartLoaderImage(DropDowns[i + 1].id + "_Loader");
          }
          else
          {
              StopLoaderImage(DropDowns[i + 1].id + "_Loader");
          }
          for(j = i + 1; j < DropDowns.length; j++)
          {
              DisableDropDown(DropDowns[j]);
              //Hide all loader images below the one that is currently running.
              if(j != i + 1)
              {
                StopLoaderImage(DropDowns[j].id + "_Loader");
              }
          }
          break;
      }
   }
   
   //If the drop down was reset to an empty value, do nothing.
   if(DropDownListChanged.options[DropDownListChanged.selectedIndex].text == "")
        return; 
      
   xmlHttp = CreateAjaxObject();  //Create the Ajax object
   xmlHttp.onreadystatechange = GenericMapsEventHandler; //Set the ready event handler
   var query = "mapDDLChanged=" + DropDownListChanged.id + "&" + BuildFormValueString();  //Build the Post data
   
   xmlHttp.open("POST", "../AjaxHandler.aspx",true); //Open the object
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //Set the encoding so .NET can read the post data
   xmlHttp.send(query); //Send the request.
}

function StartLoaderImage(LoaderImageID)
{
    document.getElementById(LoaderImageID).style.display = "";
}

function StopLoaderImage(LoaderImageID)
{
    document.getElementById(LoaderImageID).style.display = "none";
}

//Calls the server for the list of industries for the state that the user chooses from the state drop down list.
function PullIndustryData(stateDropDownList)
{
   //Check for the user selecting the empty item in the drop down list.
   if(stateDropDownList.options[stateDropDownList.selectedIndex].text == "")
   {
       //If they did, clear the drop down list and disable it.
       document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList").options.selectedIndex = 0;
       //ClearOptions(document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList"));
       document.getElementById("ctl00__pageContentContentPlaceHolder__industryDropDownList").disabled = true;
       return;
   }
   xmlHttp = CreateAjaxObject();  //Create the Ajax object
   xmlHttp.onreadystatechange = FillInterestedInParticipatingData; //Set the ready event handler
   var query = "state=" + stateDropDownList.options[stateDropDownList.selectedIndex].text;  //Build the Post data
   xmlHttp.open("POST", "../AjaxHandler.aspx",true); //Open the object
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //Set the encoding so .NET can read the post data
   xmlHttp.send(query); //Send the request.
}



//Disables a drop down by setting its selected index to 0 and disabling it.
function DisableDropDown(DropDownToDisable)
{
    DropDownToDisable.selectedIndex = 0;
    DropDownToDisable.disabled = true;
}

//Shows the error label on the maps page with the message passed in.
function ShowErrorMessage(message)
{
    var Label = document.getElementById("ctl00__pageContentContentPlaceHolder__searchErrorLabel");
    Label.value = message;
    Label.display = "block";
}

//Hides the error label on the maps page
function HideErrorMessage()
{
    var Label = document.getElementById("ctl00__pageContentContentPlaceHolder__searchErrorLabel");
    Label.style.display = "none";
}

//Calls the server for data for the reports page. DropDownListChanged contains the drop down list javascript DOM object that was
//changed.
function PullReportsData(DropDownListChanged)
{
   //Get all select tags on the page so we can disable the ones below the one that got changed
   var DropDowns = document.getElementsByTagName("select");
   for(i = 0; i < DropDowns.length; i++)
   {
      if(DropDowns[i] == DropDownListChanged)
      {
          DisableDropDown(DropDowns[i + 1]);
          if(!(DropDownListChanged.options[DropDownListChanged.selectedIndex].text == ""))
          {
            StartLoaderImage(DropDowns[i + 1].id + "_Loader");
          }
          else
          {
            StopLoaderImage(DropDowns[i + 1].id + "_Loader");
          }
      }
   }
   
   //Checks to see if the user just reset the drop down list to the empty item.
   if(DropDownListChanged.options[DropDownListChanged.selectedIndex].text == "")
        return; 
      
   xmlHttp = CreateAjaxObject();  //Create the Ajax object
   xmlHttp.onreadystatechange = GenericReportsEventHandler; //Set the ready event handler
   var query = "reportDDLChanged=" + DropDownListChanged.id + "&" + BuildFormValueString();  //Build the Post data
   
   xmlHttp.open("POST", "../AjaxHandler.aspx",true); //Open the object
   xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); //Set the encoding so .NET can read the post data
   xmlHttp.send(query); //Send the request.
}


//This function will handle all events for the reports page.
function GenericReportsEventHandler()
{
    if(xmlHttp.readyState == 4)
    {
        //Retrieve the values from the response text, they are delimited by semicolons
        var Values = new Array();
        Values = xmlHttp.responseText.split(";");
        
        //The first value is the client ID of the drop down that was changed
        var ReportDropDown = document.getElementById(Values[0]);
        //Get the drop down that is to be filled and enable the correct drop down lists
        ReportDropDown = GetReportDropDown(ReportDropDown);
        //Clear out the drop down to be filled
        ClearOptions(ReportDropDown);
        //Add a blank option to the drop down to be filled so the user doesn't default to nothing
        ReportDropDown.options[0] = new Option("", "", false, false);
        //Add the values to the drop down list
        for(i = 1; i < Values.length; i++)
        {
            ReportDropDown.options[i] = new Option(Values[i], Values[i], false, false);    
        }
        //Enable the drop down list.
        ReportDropDown.disabled = false;
        
        StopLoaderImage(ReportDropDown.id + "_Loader");
    }
}

//retrieves the drop down list of exits on the reports page.
function GetReportDropDown(DropDownListChanged)
{
    //variable to hold the drop down list that will be returned. The one below the drop down that was just changed.
    //The drop down that is to be filled on this event cycle
    var DropDownToReturn
    //Get all select tags on the page
    var DropDowns = document.getElementsByTagName("select");
    //Cycle through all select tags, if the current one is equal to the one that was changed...
    for(i = 0; i < DropDowns.length; i++)
    {
      if(DropDowns[i] == DropDownListChanged)
      {
          //Then save the next drop down in line as the one to return
          DropDownToReturn = DropDowns[i + 1];
          break;
      }
   }
   //Return the saved drop down list
   return DropDownToReturn;
}

// displays the DOT pdf in a new window with no postback
function DisplayDOTDocument(PathToDocument) {

    window.open(PathToDocument, 'Fundamentals_of_the_Logo_Signing_Program');

    return false;
}