/* ----------------------------------------------------------------------------
   Search Configuration -- arguments are case sensitive
   ---------------------------------------------------------------------------- */
    // Search Setup
    //   Id of the button to trigger the search
    //   it can be anything with a onclick event    
    var strSearchButton = 'searchButton';  
    
    //   Id of the search textbox
    //   the id can be anything but 'txtSearchTextbox'
    var strSearchTextbox = 'searchTextbox';
    
    //   Id of the container that the search 
    //   please use an inline style of display:none;
    var strSearchResultContaner = 'searchResults';
    
    // ClientId
    var clientId = 1458

/* ----------------------------------------------------------------------------
        Justin's Custom Search javascript
   ---------------------------------------------------------------------------- */
    var ssSearchTextBox  
    var ssSearchButton  
    var ssSearchContainer  
    var searchUrl =  "siteSearch/Search.ashx?"
    
    // run after window has loaded 
    addLoadEvent(function() {
        ssSearchTextBox = document.getElementById(strSearchTextbox);
        ssSearchButton = document.getElementById(strSearchButton);
        ssSearchContainer = document.getElementById(strSearchResultContaner); 
        
        ValidateSettings();   
              
         
        
        ssSearchButton.onclick = sSearch;
            
    });                   
           
    
    function sSearch() {
    	// debugger; 
    	document.body.style.cursor = 'wait';
        var url = searchUrl + "searchTerm=" + ssSearchTextBox.value 
                    + "&clientId=" + clientId;
            
            //debugger;                          
            ssSearchContainer.style.display = 'block';
            
            SendAJAXRequest(url, Search_Callback, null);
    }

    function ValidateSettings() {
        var msg = null;
        
        if(!ssSearchTextBox)
            msg = "strSearchTextbox not valid"
            
        if(!ssSearchButton)
            msg = "strSearchButton not valid"
            
        if(!ssSearchContainer)
            msg = "strSearchResultContaner not valid"
            
        if(clientId <= 0)
            msg = "Please add ClientId"
           
        if(msg != null)
            alert(msg);
    }
    
    function Button1_onclick() {
        var txtSearch = document.getElementById("Text1");
        var url = + txtSearch.value;
                
        SendAJAXRequest(url, Search_Callback, null)
    }
    
    function closeSearchWindow() {
        ssSearchContainer.style.display = 'none';
    }
    
    function Search_Callback(responseText) {         
        ssSearchContainer.innerHTML = responseText;         
        document.body.style.cursor = 'default';   
    }




//http://simonwillison.net/2004/May/26/addLoadEvent/
// fires a function after window loaded
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}



// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini
// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

/* ----------------------------------------------------------------------------
    Start
    json2.js
    2007-12-02
    See http://www.JSON.org/js.html
   ----------------------------------------------------------------------------*/


if (!this.JSON2) {

    JSON2 = function () {

        function f(n) {    // Format integers to have at least two digits.
            return n < 10 ? '0' + n : n;
        }

        Date.prototype.toJSON = function () {

// Eventually, this method will be based on the date.toISOString method.

            return this.getUTCFullYear()   + '-' +
                 f(this.getUTCMonth() + 1) + '-' +
                 f(this.getUTCDate())      + 'T' +
                 f(this.getUTCHours())     + ':' +
                 f(this.getUTCMinutes())   + ':' +
                 f(this.getUTCSeconds())   + 'Z';
        };


        var m = {    // table of character substitutions
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        };

        function stringify(value, whitelist) {
            var a,          // The array holding the partial texts.
                i,          // The loop counter.
                k,          // The member key.
                l,          // Length.
                r = /["\\\x00-\x1f\x7f-\x9f]/g,
                v;          // The member value.

            switch (typeof value) {
            case 'string':

// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe sequences.

                return r.test(value) ?
                    '"' + value.replace(r, function (a) {
                        var c = m[a];
                        if (c) {
                            return c;
                        }
                        c = a.charCodeAt();
                        return '\\u00' + Math.floor(c / 16).toString(16) +
                                                   (c % 16).toString(16);
                    }) + '"' :
                    '"' + value + '"';

            case 'number':

// JSON numbers must be finite. Encode non-finite numbers as null.

                return isFinite(value) ? String(value) : 'null';

            case 'boolean':
            case 'null':
                return String(value);

            case 'object':

// Due to a specification blunder in ECMAScript,
// typeof null is 'object', so watch out for that case.

                if (!value) {
                    return 'null';
                }

// If the object has a toJSON method, call it, and stringify the result.

                if (typeof value.toJSON === 'function') {
                    return stringify(value.toJSON());
                }
                a = [];
                if (typeof value.length === 'number' &&
                        !(value.propertyIsEnumerable('length'))) {

// The object is an array. Stringify every element. Use null as a placeholder
// for non-JSON values.

                    l = value.length;
                    for (i = 0; i < l; i += 1) {
                        a.push(stringify(value[i], whitelist) || 'null');
                    }

// Join all of the elements together and wrap them in brackets.

                    return '[' + a.join(',') + ']';
                }
                if (whitelist) {

// If a whitelist (array of keys) is provided, use it to select the components
// of the object.

                    l = whitelist.length;
                    for (i = 0; i < l; i += 1) {
                        k = whitelist[i];
                        if (typeof k === 'string') {
                            v = stringify(value[k], whitelist);
                            if (v) {
                                a.push(stringify(k) + ':' + v);
                            }
                        }
                    }
                } else {

// Otherwise, iterate through all of the keys in the object.

                    for (k in value) {
                        if (typeof k === 'string') {
                            v = stringify(value[k], whitelist);
                            if (v) {
                                a.push(stringify(k) + ':' + v);
                            }
                        }
                    }
                }

// Join all of the member texts together and wrap them in braces.

                return '{' + a.join(',') + '}';
            }
        }

        return {
            stringify: stringify,
            parse: function (text, filter) {
                var j;

                function walk(k, v) {
                    var i, n;
                    if (v && typeof v === 'object') {
                        for (i in v) {
                            if (Object.prototype.hasOwnProperty.apply(v, [i])) {
                                n = walk(i, v[i]);
                                if (n !== undefined) {
                                    v[i] = n;
                                }
                            }
                        }
                    }
                    return filter(k, v);
                }


// Parsing happens in three stages. In the first stage, we run the text against
// regular expressions that look for non-JSON patterns. We are especially
// concerned with '()' and 'new' because they can cause invocation, and '='
// because it can cause mutation. But just to be safe, we want to reject all
// unexpected forms.

// We split the first stage into 4 regexp operations in order to work around
// crippling inefficiencies in IE's and Safari's regexp engines. First we
// replace all backslash pairs with '@' (a non-JSON character). Second, we
// replace all simple value tokens with ']' characters. Third, we delete all
// open brackets that follow a colon or comma or that begin the text. Finally,
// we look to see that the remaining characters are only whitespace or ']' or
// ',' or ':' or '{' or '}'. If that is so, then the text is safe for eval.

                if (/^[\],:{}\s]*$/.test(text.replace(/\\./g, '@').
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g, ']').
replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {

// In the second stage we use the eval function to compile the text into a
// JavaScript structure. The '{' operator is subject to a syntactic ambiguity
// in JavaScript: it can begin a block or an object literal. We wrap the text
// in parens to eliminate the ambiguity.
                    

                    j = eval('(' + text + ')');

// In the optional third stage, we recursively walk the new structure, passing
// each name/value pair to a filter function for possible transformation.

                    return typeof filter === 'function' ? walk('', j) : j;
                }

// If the text is not JSON parseable, then a SyntaxError is thrown.

                throw new SyntaxError('parseJSON');
            }
        };
    }();
}
/* ----------------------------------------------------------------------------
    End
    json2.js
    2007-12-02
    See http://www.JSON.org/js.html
   ----------------------------------------------------------------------------*/

var xmlhttp;     // xmlhttp request object
var myCallback;  // callback method used to recieve ajax response

// used to populate the xmlhttp object
function GetXmlHttp() {
    xmlhttp=null;
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else if (window.ActiveXObject) // code for IE5 and IE6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    
    if (xmlhttp == null) 
        alert("Your browser does not support XMLHTTP.");
} 


// url = the location we want to post the data to
// callback = a function that we want to execute upon a succesfull submission
// postData = optional parameter to send data to the server
function SendAJAXRequest(url, callback, postData)
{
    myCallback = null;
    myCallback = callback;   
    
    if (xmlhttp == null)
        GetXmlHttp();
        
    if (xmlhttp && xmlhttp.readyState != 0) 
        xmlhttp.abort();    
        
    if (xmlhttp != null)          
    {
        xmlhttp.onreadystatechange=state_Change;
        if(postData == null) {
            xmlhttp.open("GET",url,true);
            xmlhttp.send(null);
        } else {
            xmlhttp.open("POST", url, false);
		    xmlhttp.send(postData);
        }
    }
}

// Function used to recieve requests.  This function is used to call the callback function.
function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = OK
        myCallback(xmlhttp.responseText)
    }
  else
    {
        alert("Problem retrieving XML data\n\n  Status::" + xmlhttp.status);
    }
  }
}