/**
 * This file contains functions and vars to extend the document object.
 * 
 * 
 * 
 * NOTE: Global functions can also reside here.
 */

/**
 * A vd (var_dump) type wrapper for javascript
 * @param mixed object
 */
function vd(object) {
    alert(object);
}

/**
 * Load a url in a document 
 * 
 * @param string href - A url to the new page
 * @param string target - (optional) The target to load the url into
 */
function goTo(href)
{
    var target = arguments[2] ? arguments[2] : document;
	try {
		target.location = href;
	} catch (e) {
	}
}

/**
 * Submit a form from a input field.
 * EG:
 *   <input name="test" type="text" size=10 onKeyPress="return submitEnter(this,event)">
 * 
 */
function submitEnter(form, e)
{
    
    var keycode;
    if (window.event) {
        keycode = window.event.keyCode;
    } else if (e) {
        keycode = e.which;
    } else {
        return true;
    }
    if (keycode == 13)
    {
        form.submit();
        return false;
    } else {
        return true;
    }
}


/**
 * A call to get an element by its id.
 * All javascript should use this to ensure browser compatability.
 * 
 * @param string id - The id of the element.
 * @return DOMElement - 
 */
function getElementById(id) {
    try {
        if (document.getElementById(id)) {
            return document.getElementById(id);
        }/* else if (document.all(id)) {
            return document.all(id);
        }*/
    } catch (e) {}
}

/**
 *  Popup window.
 * 
 * @param string url - The url to show in the popup window
 * @param integer width - (optional) The width in pixels
 * @param integer height - (optional) The height in pixels
 * @param string scrollbars - (optional) 'yes'/'no' values
 * @param string targetName - (optional) Changes the target name of the window, use this for sub popups.
 * @param string modal - (optional) 'yes'/'no' values,  yes give the popup a modal dialog box effect.
 * @author stew@aot.com.au 2004-02-03
 */
var popupWin = null;

function popup(url) { 
    var width = arguments[1] ? arguments[1] : 455;
    var height = arguments[2] ? arguments[2] : 500;
    var scrollbars = arguments[3] ? arguments[3] : 'yes';
    var resizable = arguments[4] ? arguments[4] : 'yes';
    var targetName = arguments[5] ? arguments[5] : 'info';
    var modal = arguments[6] ? arguments[6] : 'no';
    
    if (popupWin != null && !popupWin.closed) {
        popupWin.close(); 
    }
    if(window.opener){
        LeftPosition =(window.opener.innerWidth-width) / 2;
        TopPosition =((window.opener.innerHeight-height)/2) + 60;
    } else if(window.innerWidth) {
        LeftPosition =(window.innerWidth-width) / 2;
        TopPosition =((window.innerHeight-height)/2) + 60;
    } else {
        LeftPosition =(parseInt(window.screen.width) - width)/2;
        TopPosition=((parseInt(window.screen.height) - height)/2) + 60;
    }
    popupWin = window.open(url,targetName,'scrollbars='+scrollbars+',width='+width+
        ',height='+height+',resizable='+resizable+',left=' + LeftPosition + 
        ',top=' + TopPosition+',modal='+modal);
    
    if (window.focus) {
        popupWin.focus();
    }
    return popupWin;
}

/**
 * Change the class name of an element in the HTML page.
 * 
 * @param id string - the id of the element
 * @param newClass string - The new css class name
 */
function changeClass(id, cssClass) {
    var identity = AOT_getElementById(id);
    if (identity) {
        identity.className = cssClass;
    }
}

/**
 * Submit a form once only, used for submitting a payment form.
 * Put a call to this function in an onSubmit event on a form.
 * 
 * @param DOMElement btn - 
 */
var wasSubmitted = false;
function submitOnce(btn)
{
    if (wasSubmitted == true) {
        alert("Request already submitted, please wait....");
     } else {
        btn.className = 'btn_disable';
        btn.blur();
        wasSubmitted = true;
     }
}

/**
 * javascript:turnOnHourglass() a simple function for ie5.5+ to have an hourglass cursor 
 * whilst waiting for the background page to begin loading
 * 
 */
function turnOnHourglass()
{
    document.body.style.cursor = "wait";
}


/**
 * The offsetTop and offsetLeft properties tell you the distance between the top of 
 *  an element and the top of its offsetParent. *  But what is offsetParent? Well, it varies 
 *  widely for different elements and different browsers. Sometimes it's the 
 *  immediate containing element; other times it's the html element; at other times it's nonexistent.
 * 
 * Thankfully, the solution is to follow the trail of offsetParents and add up their 
 *  offset positions -- a method that will give you the element's accurate absolute 
 *  position on the page in every browser.
 * 
 * If the element in question has no offsetParent, then the offset position of the 
 *  element itself is enough; otherwise, we add the offsets of the element to those 
 *  of its offsetParent, then repeat the process for its offsetParent (if any):
 */
function getPosition(theElement)
{
 var positionX = 0;
 var positionY = 0;

 while (theElement != null)
 {
   positionX += theElement.offsetLeft;
   positionY += theElement.offsetTop;
   theElement = theElement.offsetParent;
 }

 return [positionX, positionY];
}
//////////////// Document Extensions ////////////////

/**
 * All event handlers should not take parameters, and the add...()
 *   methods all accept strings NOT the function itself.
 * EG:
 *  o WRONG: document.addOnClickHandler(someHandler);
 *  o RIGHT: document.addOnClickHandler('someHandler'); <---- Note the quotes ('...')
 * 
 * 
 * Add/remove a mouse move event function: 
 *   o document.addOnMouseMoveHandler('dragEvent');
 *   o document.removeOnMouseMoveHandler('dragEvent');
 * Add/remove a mouse click event function:
 *   o document.addOnClickHandler('DhtmlLayer.hideAll');
 *   o document.removeOnClickHandler('DhtmlLayer.hideAll');
 * 
 * NOTE: Add has an accumulative effect on multiple calls, where as 
 *       the remove methods, remove all handlers containing the 
 *       matching function name.
 */ 

document.mouseXPos = 0;
document.mouseYPos = 0;
document.onMouseMoveHandlers = new Array();
document.onClickHandlers = new Array();
document.onKeyUpHandlers = new Array();

document.KEY_LEFT = 37;
document.KEY_UP = 38;
document.KEY_RIGHT = 39;
document.KEY_DOWN = 40;
document.KEY_ENTER = 13;
document.KEY_TAB = 9;

/**
 * Method to add a document mouseMove event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.addOnMouseMoveHandler = function(handler) {
    this.onMouseMoveHandlers[this.onMouseMoveHandlers.length] = handler;
}

/**
 * Method to add a document onClick event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.addOnClickHandler = function(handler) {
    this.onClickHandlers[this.onClickHandlers.length] = handler;
}

/**
 * Method to add a document onKeyUp event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.addOnKeyUpHandler = function(handler) {
    this.onKeyUpHandlers[this.onKeyUpHandlers.length] = handler;
}

/**
 * Method to remove a document mouseMove event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.removeOnMouseMoveHandler = function(handler) {
    var newArray = new Array();
    for(i = 0; i < this.onMouseMoveHandlers; i++) {
        if (this.onMouseMoveHandlers[i] != handler) {
            newArray[newArray.length] = this.onMouseMoveHandlers[i];
        }
    }
    this.onMouseMoveHandlers = newArray;
}

/**
 * Method to remove a document onClick event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.removeOnClickHandler = function(handler) {
    var newArray = new Array();
    for(i = 0; i < this.onClickHandlers; i++) {
        if (this.onClickHandlers[i] != handler) {
            newArray[newArray.length] = this.onClickHandlers[i];
        }
    }
    this.onClickHandlers = newArray;
}

/**
 * Method to remove a document onKeyPress event handler.
 * 
 * @param string handler - A callback event handler.
 */
document.removeOnKeyUpHandler = function(handler) {
    var newArray = new Array();
    for(i = 0; i < this.onKeyUpHandlers; i++) {
        if (this.onKeyUpHandlers[i] != handler) {
            newArray[newArray.length] = this.onKeyUpHandlers[i];
        }
    }
    this.onKeyUpHandlers = newArray;
}

/**
 * Stop the event from bubbling up to the browser level.
 * 
 */
document.stopBubble = function(e) {
    // Cancel browser events
    if (document.all) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }else if(e.preventDefault){
        e.cancelBubble = true;
        e.stopPropagation();
        e.preventDefault();
    }
    return false; 
}

/////////////////////// Event Methods ///////////////////////

/**
 * Callbacks for document.oncmousemove
 */
document.onmousemove = function () {
    if ((sniffer.is_ie5up || sniffer.is_nav6up || sniffer.is_gecko)) {
        if (arguments[0]) {
            document.mouseXPos = arguments[0].pageX;
            document.mouseYPos = arguments[0].pageY;
        } else {
            if (sniffer.is_ie6 || sniffer.is_ie6up){
               if(document.body) {
                  document.mouseXPos = event.clientX+document.body.scrollLeft+document.documentElement.scrollLeft;
                  document.mouseYPos = event.clientY+document.body.scrollTop+document.documentElement.scrollTop;
              }
            } else {
                document.mouseXPos = event.clientX + document.body.scrollLeft;
                document.mouseYPos = event.clientY + document.body.scrollTop;
            }
            arguments[0] = null;
        }
        
        for(i = 0; i < this.onMouseMoveHandlers.length; i++) {
            eval(this.onMouseMoveHandlers[i]+'();');
        }
    }
}

/**
 * Callbacks for document.onclick
 */
document.onclick = function () {
    if ((sniffer.is_ie5up || sniffer.is_nav6up || sniffer.is_gecko)) {
        for(i = 0; i < this.onClickHandlers.length; i++) {
            eval(this.onClickHandlers[i]+'();');
        }
    }
}


/**
 * 
 * 
 * NOTE: Instead of e.keyCode OR e.which use the defined e.bcKeyCode
 */
document.onkeyup = function (e) {
    if (!e) {
        e = event;
        if (!e) {return true;}
    }
    /*if (!e.keyCode) 
      e.bcKeyCode = e.which;*/
    
    if ((sniffer.is_ie5up || sniffer.is_nav6up || sniffer.is_gecko)) {
        for(i = 0; i < this.onKeyUpHandlers.length; i++) {
          if (!eval(this.onKeyUpHandlers[i]+'(e);')) {
              return false;  // Be sure event handler returns true or false.
          }
        }
    }
}

//////////////////////// Browser Sniffer Replacement ////////////////////////

/**
 * Browser Sniffer (In class format... Whoo)
 * Turn the browser sniffer into an object that is present all the time.
 * 
 * 
 * 
 */
function Sniffer() {
    
    this.agt           = navigator.userAgent.toLowerCase();
    // *** BROWSER VERSION ***
    // Note: On IE5, these return 4, so use is_ie5up to detect IE5.
    this.is_major      = parseInt(navigator.appVersion);
    this.is_minor      = parseFloat(navigator.appVersion);
    // Note: Opera and WebTV spoof Navigator.  We do strict client detection.
    // If you want to allow spoofing, take out the tests for opera and webtv.
    this.is_nav        = ((this.agt.indexOf('mozilla')!=-1) && (this.agt.indexOf('spoofer')==-1)
                         && (this.agt.indexOf('compatible') == -1) && (this.agt.indexOf('opera')==-1)
                         && (this.agt.indexOf('webtv')==-1) && (this.agt.indexOf('hotjava')==-1));
    this.is_nav2       = (this.is_nav && (this.is_major == 2));
    this.is_nav3       = (this.is_nav && (this.is_major == 3));
    this.is_nav4       = (this.is_nav && (this.is_major == 4));
    this.is_nav4up     = (this.is_nav && (this.is_major >= 4));
    this.is_navonly    = (this.is_nav && ((this.agt.indexOf(";nav") != -1) ||
                         (this.agt.indexOf("; nav") != -1)) );
    this.is_nav6       = (this.is_nav && (this.is_major == 5));
    this.is_nav6up     = (this.is_nav && (this.is_major >= 5));
    this.is_gecko      = (this.agt.indexOf('gecko') != -1);


    this.is_ie         = ((this.agt.indexOf("msie") != -1) && (this.agt.indexOf("opera") == -1));
    this.is_ie3        = (this.is_ie && (this.is_major < 4));
    this.is_ie4        = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 4")!=-1) );
    this.is_ie4up      = (this.is_ie && (this.is_major >= 4));
    this.is_ie5        = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.0")!=-1) );
    this.is_ie5_5      = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 5.5") !=-1));
    this.is_ie5up      = (this.is_ie && !this.is_ie3 && !this.is_ie4);
    this.is_ie5_5up    = (this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5);
    this.is_ie6        = (this.is_ie && (this.is_major == 4) && (this.agt.indexOf("msie 6.")!=-1) );
    this.is_ie6up      = (this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5 && !this.is_ie5_5);

    // KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
    // or if this is the first browser window opened.  Thus the
    // variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
    this.is_aol        = (this.agt.indexOf("aol") != -1);
    this.is_aol3       = (this.is_aol && this.is_ie3);
    this.is_aol4       = (this.is_aol && this.is_ie4);
    this.is_aol5       = (this.agt.indexOf("aol 5") != -1);
    this.is_aol6       = (this.agt.indexOf("aol 6") != -1);

    this.is_opera      = (this.agt.indexOf("opera") != -1);
    this.is_opera2     = (this.agt.indexOf("opera 2") != -1 || this.agt.indexOf("opera/2") != -1);
    this.is_opera3     = (this.agt.indexOf("opera 3") != -1 || this.agt.indexOf("opera/3") != -1);
    this.is_opera4     = (this.agt.indexOf("opera 4") != -1 || this.agt.indexOf("opera/4") != -1);
    this.is_opera5     = (this.agt.indexOf("opera 5") != -1 || this.agt.indexOf("opera/5") != -1);
    this.is_opera5up   = (this.is_opera && !this.is_opera2 && !this.is_opera3 && !this.is_opera4);

    this.is_webtv      = (this.agt.indexOf("webtv") != -1); 

    this.is_TVNavigator = ((this.agt.indexOf("navio") != -1) || (this.agt.indexOf("navio_aoltv") != -1)); 
    this.is_AOLTV      = this.is_TVNavigator;

    this.is_hotjava    = (this.agt.indexOf("hotjava") != -1);
    this.is_hotjava3   = (this.is_hotjava && (this.is_major == 3));
    this.is_hotjava3up = (this.is_hotjava && (this.is_major >= 3));
    
    // Javascript version
    this.is_js = this.getJavascriptVersion();
    
}

/**
 * Get the javascript version
 * 
 * 
 */
Sniffer.prototype.getJavascriptVersion = function() {
    
    // *** JAVASCRIPT VERSION CHECK ***
    var is_js;
    if (this.is_nav2 || this.is_ie3) is_js = 1.0;
    else if (this.is_nav3) is_js = 1.1;
    else if (this.is_opera5up) is_js = 1.3;
    else if (this.is_opera) is_js = 1.1;
    else if ((this.is_nav4 && (this.is_minor <= 4.05)) || this.is_ie4) is_js = 1.2;
    else if ((this.is_nav4 && (this.is_minor > 4.05)) || this.is_ie5) is_js = 1.3;
    else if (this.is_hotjava3up) is_js = 1.4;
    else if (this.is_nav6 || this.is_gecko) is_js = 1.5;
    // NOTE: In the future, update this code when newer versions of JS
    // are released. For now, we try to provide some upward compatibility
    // so that future versions of Nav and IE will show they are at
    // *least* JS 1.x capable. Always check for JS version compatibility
    // with > or >=.
    else if (this.is_nav6up) is_js = 1.5;
    // NOTE: ie5up on mac is 1.4
    else if (this.is_ie5up) is_js = 1.3
    // HACK: no idea for other browsers; always check for JS version with > or >=
    else is_js = 0.0;
    
    return is_js;
}
// Create a Global instance of the sniffer
var sniffer = new Sniffer();