﻿// JScript Utils File
// 
// This file should be used with map.js and must be loaded before map.js.
// 
// To include this in your project, the best way is to include it
// with the virtual earth API via the ScriptManager, i.e.:
//   <asp:ScriptManager ID="ScriptManager1" 
//          EnablePartialRendering="true" 
//          EnablePageMethods="true" runat="server">
//      <Scripts>
//         <asp:ScriptReference 
//            Path="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6" />
//         <asp:ScriptReference Path="script/utils.js" />
//         <asp:ScriptReference Path="script/map.js" />  
//      </Scripts>        
//   </asp:ScriptManager>
//
//

// Querystring helpers

// PageQuery   
// <summary>
// Gets the value or keyvalue pair for the PageQuery object
// </summary>
// <param name="q">A string representing the querystring</param>
// <returns></returns> 
function PageQuery(q) {
   if(q.length > 1) this.q = q.substring(1, q.length);
   else this.q = null;
   // if specified, the keyvalue pairs are instantiated from the query string
   this.keyValuePairs = new Array();
   if(q) {
      for(var i = 0; i < this.q.split("&").length; i++) {
         this.keyValuePairs[i] = this.q.split("&")[i];
         }
      }
   // if requested, the entire keyvaluePairs array is returned
   this.getKeyValuePairs = function() {
      return this.keyValuePairs;
      }
   
   // if requested, a value is returned for a given key s
   this.getValue = function(s) {
      for(var j = 0; j < this.keyValuePairs.length; j++) {
         if(this.keyValuePairs[j].split("=")[0] == s) return this.keyValuePairs[j].split("=")[1];
         }
      return false;
      }
   // if requested, all parameters are returned
   this.getParameters = function() {
      var a = new Array(this.getLength());
      for(var j = 0; j < this.keyValuePairs.length; j++) {
         a[j] = this.keyValuePairs[j].split("=")[0];
         }
      return a;
      }
   
   // returns the number of keyValuePairs
   this.getLength = function() {
      return this.keyValuePairs.length;
      }
   }
   
// queryString   
// <summary>
// gets a value of a querystring parameter
// </summary>
// <param name="q">A string representing the querystring</param>
// <returns></returns> 
function queryString(key) {
   var page = new PageQuery(window.location.search);
   return unescape(page.getValue(key));
   }

// Dictionary helpers

// mLookup   
// <summary>
// Looks up the value of a key
// </summary>
// <param name="strKeyName">The key to search for in our dictionary</param>
// <returns></returns> 
  function mLookup(strKeyName) {
    return(this[strKeyName]);
  }  
 
// mAdd   
// <summary>
// Add's a key and value to our dictionary
// </summary>
// <returns></returns>  
  function mAdd() {
    for (c=0; c < mAdd.arguments.length; c+=2) {
      this[mAdd.arguments[c]] = mAdd.arguments[c+1];
    }
  }  
  
// mDelete   
// <summary>
// Deletes a value from our dictionary
// </summary>
// <param name="strKeyName">The key to search for in our dictionary</param>
// <returns></returns>   
  function mDelete(strKeyName) {
    for (c=0; c < mDelete.arguments.length; c++) {
      this[mDelete.arguments[c]] = null;
    }
  }  
  
// oDictionary   
// <summary>
// Class object representing a dictionary
// </summary>
// <returns></returns>     
function oDictionary() {
  this.Add = mAdd;
  this.Lookup = mLookup;
  this.Delete = mDelete;
}  

// String format   
// <summary>
// Replicates the C# String.Format function
// </summary>
// <returns></returns> 
function format(str) {
  for(i = 1; i < arguments.length; i++) {str = str.replace('{' + (i - 1) + '}', arguments[i]);}
  return str;
}


// Validate Drop off stop
// <summary>
// Validates the ddlStart dropdown vs a given source stop to make sure that
// the start stop is before the drop off stop
// </summary>
// <returns></returns> 
function ClientStopValidate(source, arguments)
{
    var l_oPickUpStop = document.getElementById("ctl00_ddlStart");
    var l_oDropOffStop = document.getElementById(source.controltovalidate); 
    var l_oRouteValue = document.getElementById("ctl00_ddlRoute").value; 
   
    ValidateStopOrder(l_oPickUpStop, l_oDropOffStop, arguments, l_oRouteValue);
}

// Validate pick up stop
// <summary>
// Validates the ddlStop dropdown vs a given source stop to make sure that
// the start stop is before the drop off stop
// </summary>
// <returns></returns> 
function ClientStopValidate2(source, arguments)
{
    var l_oPickUpStop = document.getElementById(source.controltovalidate);
    var l_oDropOffStop = document.getElementById("ctl00_ddlStop");
    var l_oRouteValue = document.getElementById("ctl00_ddlRoute").value; 
   
    ValidateStopOrder(l_oPickUpStop, l_oDropOffStop, arguments, l_oRouteValue);   
}

// Validate stop order
// <summary>
// Make sure that the start stop is before the drop off stop
// </summary>
// <returns></returns> 
function ValidateStopOrder(l_oPickUpStop, l_oDropOffStop, arguments, l_oRouteValue)
{
   if (l_oPickUpStop.selectedIndex > 0 && l_oDropOffStop.selectedIndex > 0)
   {
        if (l_oDropOffStop.selectedIndex > l_oPickUpStop.selectedIndex)
        {
            arguments.IsValid = true;
        }
        else if (l_oRouteValue.indexOf('LOOP') > -1 && l_oDropOffStop.selectedIndex < l_oPickUpStop.selectedIndex)
        {
            arguments.IsValid = true;
        }        
        else
        {
            arguments.IsValid = false;     
        }
   }  
   else
   {
        arguments.IsValid = true;
   }
}

// Check ready state
// <summary>
// Checks the objects readystate to see if the response object is successfully complete
// </summary>
// <returns></returns> 
function checkReadyState(obj)
{
  if(obj.readyState == 4)
  {
    if(obj.status == 200)
    {
    return true;
    }
    else
    {
    alert("Problem retrieving XML data");
    }
  }
}
   
// GetForeGroundColor   
// <summary>
// Interpretes the correct foreground color to place on a given background color.
//
// Get text foreground color based off backgroundRGB
// On a darker background, foreground should be white
// On a lighter background, foreground should be black
// </summary>
// <returns>White or Black depending on the given background RGB value</returns> 
function GetForeGroundColor(sBackgroundRGB)
{
    var l_sTextColor;
    var l_nR;
    var l_nG;
    var l_nB;
    var l_nBrightness;
    
    // split the RGB
    var l_oRGBArray = new Array();
    l_oRGBArray = sBackgroundRGB.split(',');        
    
    l_nR = parseInt(l_oRGBArray[0], 16);
    l_nG = parseInt(l_oRGBArray[1], 16);
    l_nB = parseInt(l_oRGBArray[2], 16);
    
    // These are some magical constants that help us figure out the brightness
    // I have no idea how they were discovered
    l_nBrightness = (l_nR * 0.299 + l_nG * 0.587 + l_nB * 0.114);
    
    // If our brightness is too high, we're going to use a black color
    if (l_nBrightness < 200)
        l_sTextColor = "White";
    else
        l_sTextColor = "Black";
        
    return l_sTextColor;
}
