﻿//relatively "standard" shortcut function for document.getElementById
//Example: $('myDiv').style.display = 'none';
function $(id) {return document.getElementById(id);}

var azblue = {
    //array of HTML element references to ASP.NET controls 
    //using their assigned ID values rather than the 
    //"ClientID" values created internally by ASP.NET
    //Example:
    //   <asp:TextBox ID="TelephoneNumber" runat="server" />
    //   <script type="text/javascript">
    //      azblue.aspxControls.TelephoneNumber.value = '(555) 555-5555';
    //   </script>
   aspxControls : [],
   //path to the application's root
   //*always with a trailing forward slash (whack!)
   //Examples:
   // - localhost/AzBlue2/ = "/AzBlue2/"
   // - azblue.com/ = "/"
   apppath : '/',
   init : function() {
      //search for the search controls
      var searchBtn = $('headerSearchButton');
      var searchQ = $('headerSearchTextbox');
      //check if the references are valid
      if (searchBtn != null && searchQ != null) {
         if (searchQ.value != "") searchQ.style.backgroundImage = 'none';
         
         searchQ.onfocus = function() {
            if (this.style) this.style.backgroundImage = 'none';
            if (this.value != "") this.select();
         };
         searchQ.onblur = function() {
            if (this.value == "") this.style.backgroundImage = 'url(' + azblue.apppath + '_shared/img/google_custom_search_watermark.gif)';
         };
         //setup a search button click event
         searchBtn.onclick = function() {
            document.location.href = azblue.apppath + 'Search/Results.aspx?cx=' + encodeURI($('cx').value) + '&cof=' + encodeURI($('cof').value) + '&q=' + encodeURI($('headerSearchTextbox').value);
         };
         //setup a search text input keypress event
         searchQ.onkeypress = function(e) {
            //get the key-code
            var kp = (e ? e.which : window.event.keyCode);
            //check if it is "Return" ("Enter")
            if (kp == 13) {
               //manually call a search button click
               searchBtn.onclick();
               return false; //do not input the key
            }
            return true;
         };
      }
   },
   //collection of DOM helper routines
   dom : {
      removeChildren : function(parent) {
         while (parent.firstChild) {
            parent.removeChild(parent.firstChild);
         }
      },
      toggleDisplay : function(el) {
         if (el != null) {
            if (el.style != null)
               el.style.display = (el.style.display == 'block') ? 'none' : 'block';
         }
      }
   },
   //collection of AJAX helpers
   ajax : {
      //all HTTP status codes are listed here...
      HTTP_STATUS : {
         INFORMATIONAL : { CONTINUE : 100, SWITCHING_PROTOCOLS : 101 },
         SUCCESS : {
            OK : 200,
            CREATED : 201,
            ACCEPTED : 202,
            NONAUTHORITATIVE_INFORMATION : 203,
            NO_CONTENT : 204,
            RESET_CONTENT : 205,
            PARTIAL_CONTENT : 206
         },
         REDIRECT : {
            MULTIPLE_CHOICES : 300,
            MOVED_PERMANENTLY : 301,
            FOUND : 302,
            SEE_OTHER : 303,
            NOT_MODIFIED : 304,
            USE_PROXY : 305,
            UNUSED : 306,
            TEMPORARY_REDIRECT : 307
         },
         CLIENT_ERROR : {
            BAD_REQUEST : 400,
            UNAUTHORIZED : 401,
            PAYMENT_REQUIRED : 402,
            FORBIDDEN : 403,
            NOT_FOUND : 404,
            METHOD_NOT_ALLOWED : 405,
            NOT_ACCEPTABLE : 406,
            PROXY_AUTHENTICATION_REQUIRED : 407,
            REQUEST_TIMEOUT : 408,
            CONFLICT : 409,
            GONE : 410,
            LENGTH_REQUIRED : 411,
            PRECONDITION_FAILED : 412,
            REQUEST_ENTITY_TOO_LARGE : 413,
            REQUEST_URI_TOO_LONG : 414,
            UNSUPPORTED_MEDIA_TYPE : 415,
            REQUEST_RANGE_NOT_SATISFIABLE : 416,
            EXPECTATION_FAILED : 417
         },
         SERVER_ERROR : {
            INTERNAL : 500,
            NOT_IMPLEMENTED : 501,
            BAD_GATEWAY : 502,
            SERVICE_UNAVAILABLE : 503,
            GATEWAY_TIMEOUT : 504,
            HTTP_VERSION_NOT_SUPPORT : 505
         }
      },
      //internal AJAX instance class
      _instance : function() {
         //the response callback function after a request has been made
         this.callback = new Function();
         this.setCallback = function(value) { this.callback = value; }
         //the url to request
         var _url = null;
         this.setUrl = function(value) { _url = value; }
         this.getUrl = function() { return _url; }
         //the xml from the response
         var _xml = null;
         this.setResponseXml = function(value) { _xml = value; }
         this.getResponseXml = function() { return _xml; }
         //the text from the response
         var _txt = null;
         this.setResponseText = function(value) { _txt = value; }
         this.getResponseText = function() { return _txt; }
         //the headers from the response
         var _headers = [];
         this.setResponseHeaders = function(value) { _headers = value; }
         this.getResponseHeaders = function() { return _headers; }
         //the status code of the response
         //see: azblue.ajax.HTTP_STATUS
         var _status = null;
         this.setStatus = function(value) { _status = value; }
         this.getStatus = function() { return _status; }
         //send a request using the specified method and data
         this.sendRequest = function(method, data) {
            //create an AJAX instance
            var http = null;
            //standard AJAX object
            if (window.XMLHttpRequest) http = new XMLHttpRequest();
            //IE-specific AJAX object
            else if (window.ActiveXObject) http = new ActiveXObject("Microsoft.XMLHTTP");
            //point to this custom AJAX instance within the browser's AJAX object
            http._instance = this;
            //setup the state-change event
            http.onreadystatechange = function() {
               //if the response is ready...
               if (http.readyState == 4) {
                  //set the properties of the custom AJAX instance
                  http._instance.setStatus(http.status);
                  http._instance.setResponseXml(http.responseXML);
                  http._instance.setResponseText(http.responseText);
                  http._instance.setResponseHeaders(http.getAllResponseHeaders().split('\n'));
                  //perform the response callback
                  http._instance.callback();
               }
            };
            //open the request
            http.open(method, this.getUrl());
            //set request header(s)
            if (method == 'POST') {
               http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
               if (data != null) http.setRequestHeader('Content-Length', data.length);
            }
            //send the request (and optional data)
            http.send(data);
         };
         //do a GET request
         this.requestGet = function() { this.sendRequest('GET', null); };
         //do a POST request
         this.requestPost = function(data) { this.sendRequest('POST', data); };
         //do a HEAD request
         this.requestHead = function() { this.sendRequest('HEAD', null); };
      },
      //create an AJAX instance
      create : function() { return new this._instance(); }
   }
};

//handle all javascript errors
window.onerror = function(msg, url, lineNum) {
    try
    {
      //create an AJAX call
      var errorAjax = azblue.ajax.create();
      //make the request to the Logger web service
      errorAjax.setUrl(azblue.apppath + 'WebServices/Logger.asmx/LogJavascriptError');
      //send the error data to the web server
      errorAjax.requestPost('url=' + url + '&lineNumber=' + lineNum.toString() + '&errorMessage=' + msg);
    }
    //catch any errors while attempting to log the error
    catch(err){alert(err);}
    
    //alert the user about the error
    alert("ERROR at line #" + lineNum.toString() + ":\nURL: " + url + "\n" + msg);
};