/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

function getElementX( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
    iReturnValue += oElement.offsetLeft;
    oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getElementY( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getLetter(aNumber) {
    return String.fromCharCode(65 + aNumber);
}

function getHumanDistance(aDistanceInMeters) {
    var distance = aDistanceInMeters;
    var units = "m";
    
    if (distance > 1000) {
        distance /= 1000;
        units = "km";
    }
    
    return [distance, units];
}

function isString() {
	if (typeof arguments[0] == 'string') return true;
	if (typeof arguments[0] == 'object') {
		var theCriterion =  arguments[0].constructor.toString().match(/string/i); 
		return (theCriterion != null); 
	}
	return false;
}

function OnAjaxCallTimeOut(error) {
    if (getHBRA) {
        //alert("OnAjaxCallTimeOut getHBRA");
        getHBRA = false;
        return;
    }
    if (getBRA) {
        //alert("OnAjaxCallTimeOut getBRA");
        getBRA = false;
        return;
    }
    var dialogBox = $("#ajaxTimeoutDialog");
    if (dialogBox.length > 0) {
        dialogBox.find("#description").text(error.get_exceptionType() + ":" + error.get_message() + "<br />" + error.get_stackTrace());
        dialogBox.dialog("open");
    }
    else
        alert("Timeout exception " + error.get_exceptionType() + ":" + error.get_message() + error.get_stackTrace());
}

function OnAjaxCallError(error) {    
    if (getHBRA) {
        //alert("OnAjaxCallTimeOut getHBRA");
        getHBRA = false;
        return;
    }
    if (getBRA) {
        //alert("OnAjaxCallTimeOut getBRA");
        getBRA = false;
        return;
    }
    var dialogBox = $("#ajaxErrorDialog");
    if (error.get_timedOut()) {return false }
    if (dialogBox.length > 0) {
        dialogBox.find("#description").text(error.get_exceptionType() + ":" + error.get_message() + "<br />" + error.get_stackTrace());
        dialogBox.dialog("open");
    }
    else
        alert("Error exception " + error.get_exceptionType() + ":" + error.get_message() + error.get_stackTrace());
}

function localizeButtons(aObject) {
    $(aObject).find(".ui-dialog-buttonpane button").each(function () {
        $(this).text(eval($(this).text()));
    });
}

function setupMessageBox(aObject, aButtons, aWidth, aHeight) {
    $(aObject).dialog({
		bgiframe: true,
		modal: true,
		autoOpen: false,
		draggable: false,
		resizable: false,
		width: aWidth,
		height: aHeight,
		buttons: aButtons		
    });
    
    return $(aObject);
}

function isDefined(aObject) {
    return typeof (window[aObject]) != "undefined";
}

function log(aCategory, aFormat, aArg1, aArg2, aArg3, aArg4) {
    if (typeof (LogConsoleControl) != "undefined") {
        LogConsoleControl.log(aCategory, aFormat, aArg1, aArg2, aArg3, aArg4);
    }
}

function formatPriceResult(aPriceResult, aSelector) {
    var priceFormat = String.format(Resources_Global_PriceFormat, 
        aPriceResult == null ? Resources_Global_CannotCompute : aPriceResult.Key.toFixed(2),
        aPriceResult == null ? "" : aPriceResult.Value);

    if (aSelector) {
        $(aSelector).text(priceFormat);
    }

    return priceFormat;
}

function formatApproximateDistance(aDistanceInMeters, aSelector) {
    var d = getHumanDistance(aDistanceInMeters);
    var formatted = String.format(Resources_Global_DistanceFormat, d[0], d[1]);

    if (aSelector)
        $(aSelector).text(formatted);

    return formatted;
}

function formatDistance(aDistance, aSelector) {
    var formatted = String.format(Resources_Global_Distance_Format, aDistance.toFixed(2));

    if (aSelector)
        $(aSelector).text(formatted);

    return formatted;
}

function formatTime(aTime, aSelector) {
    var formatted = String.format(Resources_Global_Time_Format, aTime.toFixed(2));

    if (aSelector)
        $(aSelector).text(formatted);

    return formatted;
}

function formatWeight(aWeight, aSelector) {
    var formatted = String.format(Resources_Global_Weight_Format, aWeight.toFixed(2));

    if (aSelector)
        $(aSelector).text(formatted);

    return formatted;
}
