﻿/// <reference name="MicrosoftAjax.debug.js" />

// JScript File

var UnexpectedErrorURL = 'error.aspx';
var SessionTimeOutUrl = 'timeout.aspx';

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endRequest);

function endRequest(sender, e) {
    if (e.get_error()) {
        e.set_errorHandled(true); //to ensure that the system error msg does not show up

        if (e._error.httpStatusCode == 500)
            document.location = SessionTimeOutUrl;
        else
            document.location = UnexpectedErrorURL;
    }
}



function getQueryStringValue(strRequested)
{          
  var strQuery = location.search.substring(1, location.search.length);
  var args = strQuery.split('&');

  for (var i=0; i<args.length; i++)
  {
    var nameValue = args[i].split('=');
    var name = unescape(nameValue[0]);
    var value;

    if(nameValue.length == 2)
      value = unescape(nameValue[1]);    

    if(name == strRequested)
    {
      if(value == undefined)
        return undefined;
      else
        return value;
    }
  }
}  

// this is because Node object doesn't exist in IE
// (it does exist in firefox 1.0+, Opera 7.0+, Safari 1.0+)
if (typeof Node === 'undefined')
{
  var Node = 
    {
      ELEMENT_NODE: 1,
      ATTRIBUTE_NODE: 2,
      TEXT_NODE: 3,
      CDATA_SECTION_NODE: 4,
      ENTITY_REFERENCE_NODE: 5,
      ENTITY_NODE: 6,
      PROCESSING_INSTRUCTION_NODE: 7,
      COMMENT_NODE: 8,
      DOCUMENT_NODE: 9,
      DOCUMENT_TYPE_NODE: 10,
      DOCUMENT_FRAGMENT_NODE: 11,
      NOTATION_NODE: 12
    }        
}

function trim(str, chars) {
    return ltrim(rtrim(str));
}

function ltrim(str)
{
    return str.replace(new RegExp("^[\\s]+", "g"), "");
}

function rtrim(str)
{
    return str.replace(new RegExp("[\\s]+$", "g"), "");
}

function cleanWhitespace( element )
{
    try
    {

        // if no element is provided, do the whole HTML document
        element = element || document;
        
        // use the first child as a starting point
        var cur = element.firstChild;
        
        // got until there are no more child nodes
        while( cur != null )
        {
            // if the node is a text node, and it contains nothing but whitespace
            if( cur.nodeType == Node.TEXT_NODE && ! /\S/.test(cur.nodeValue) )
            {
                // remove the text node
                element.removeChild( cur );
            }   
            // otherwise, if it's an element
            else if ( cur.nodeType == Node.ELEMENT_NODE )
            {
                // recurse down through the document
                cleanWhitespace( cur );
            }
            
            cur = cur.nextSibling; // move through the child nodes        
        }
    
    }
    catch(err)
    {
        alert('error');   
    }
}

function getPostDataValue(strKeyName, strPostData)
{
  // example usage
  //  getPostDataValue('ghi', 'abc=def&ghi=lmn&opq=rst'); // returns lmn
  //  getPostDataValue('abc', 'abc=def&ghi=lmn&opq=rst'); // returns def
  //  getPostDataValue('opq', 'abc=def&ghi=lmn&opq=rst'); // returns rst

  var strKey = new String(strKeyName);
  var arr = strPostData.split('&');  
  
  for(var i=0; i<arr.length; i++)
  {
    var arrNameVal = arr[i].split('=');
    var strName = new String(arrNameVal[0]);
    if(strName.toLowerCase() === strKey.toLowerCase())
        return arrNameVal[1];            
  }    
  return null;
}

if( typeof(Sys) !== 'undefined' )
  Sys.Application.notifyScriptLoaded();