



//---------------------------------------------------
// AJAX FUNCTIONS
//---------------------------------------------------

magRadar.AjaxError = function (callback) {
    // Show the error message from a failed AJAX call.
    if(true)
	   return;
    var err = document.createElement("div");
    err.className = "errorPopup";
    err.innerHTML = "AJAX error: " + callback.status + ": " + callback.statusText + "\n" + callback.responseText;
 
    document.body.appendChild(err);
};

magRadar.AjaxError_Close = function () {
    
};

//---------------------------------------------------
// EVENT-HANDLING FUNCTIONS
//---------------------------------------------------

magRadar.stopPropagation = function (e) 
{ 
    e = e ? e : window.event; /* get IE event ( not passed ) */ 
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true; 
    }
}; 

//---------------------------------------------------
// POSITIONING FUNCTIONS
//---------------------------------------------------

magRadar.getPageScrollPosition = function () {
    var pos;
    if (document.documentElement && document.documentElement.scrollTop)	{
		pos = document.documentElement.scrollTop;
	}
	else if (document.body)	{
		pos = document.body.scrollTop;
	}
	return pos;
};

//---------------------------------------------------
// CONTROL-MANIPULATION FUNCTIONS
//---------------------------------------------------
magRadar.setSelectedValue = function (selObj, value, allowMultiple) {
    // If an option with the value [value] exists, select it
    for (var i = 0; i < selObj.options.length; i++) {
        if (selObj.options[i].value == value) {
            selObj.options[i].selected = true;
            if (!allowMultiple)
                break;
        }
    }
};

magRadar.clearSelect = function (selObj) {
    // alert('options.length = ' + selObj.options.length);
    while (selObj.options.length !== 0) {
        selObj.options[0] = null;
    }
    // alert('options.length = ' + selObj.options.length);
      
};

//---------------------------------------------------
magRadar.RemoveAllChildren = function (element) {
    if (element) {
        if (!(element && typeof element === 'object')) {
            element = document.getElementById(element);
        }
            
       while (element.firstChild) {
            element.removeChild(element.firstChild);
       }    
    }
};

/**
 * Akin to setting a virtual innerHTML property.
 */
magRadar.setInnerHTML = function(elementId, text)
{
	var obj = document.getElementById(elementId);
	while(obj.firstChild) obj.removeChild(obj.firstChild);
	obj.appendChild(document.createTextNode(text+ " ")); //space is for IE.
};

//---------------------------------------------------
// CONFIG FUNCTIONS
//---------------------------------------------------

magRadar.imagePathPrefix = "/images/issues/";
magRadar.GetImgFilePath = function (pubIssueId, filename, size) {
    // Return a full relative path to the image

    if (size == "small") {
        return magRadar.imagePathPrefix + pubIssueId + "/200/" + filename;
    } else if (size == "medium") {
        return magRadar.imagePathPrefix + pubIssueId + "/550/" + filename;
    } else if (size == "large") {
        return magRadar.imagePathPrefix + pubIssueId + "/800/" + filename;
    } else if (size == "special") {
        return magRadar.imagePathPrefix + pubIssueId + "/special/" + filename;
    }
};

//---------------------------------------------------
// OTHER FUNCTIONS
//---------------------------------------------------

magRadar.getCacheBuster = function (isOnlyParameter) {

    // Stick a timestamp on the end of the URL to prevent caching
    
    var d = new Date();
    if (isOnlyParameter) {
        return "?cb=" + d.getTime();
    } else {
        return "&cb=" + d.getTime();
    }
};

//---------------------------------------------------
magRadar.debugWrite = function (msg) {
    var td = document.getElementById("txtDebug");
    if (td) {
        td.innerHTML += ("\n" + msg);
    }
};

//---------------------------------------------------
magRadar.getMousePos = function (e) {
    
    var posx = 0;
	var posy = 0;
	    
	if (!e)  {
	    e = window.event;
	}
	if (e.pageX || e.pageY)	{
		posx = e.pageX;
		posy = e.pageY;
	} else if (e.clientX || e.clientY) {
		posx = e.clientX + document.body.scrollLeft;
		posy = e.clientY + document.body.scrollTop;
	}

	return new mousePos(posx, posy);
};

//---------------------------------------------------
magRadar.pluralize = function(singular, plural, zero, num) {
    var result = '';
    
    if (num === 0) {
        result = zero;
    } else if (num === 1) {
        result = singular;
    } else {
        result = magRadar.sprintf(plural, num);
    }
    
    return result;
};

//========================================================//
//Start image prefix refreshing for LinkDeny
//========================================================//

magRadar.imagePathRefresh_Load = function ()
{
    var imagePath_refreshAt = 60 * 1000;
    setInterval('magRadar.refreshImagePath()', imagePath_refreshAt);
};

magRadar.ajax_onRefreshImage = function(o)
{
   magRadar.imagePathPrefix = o.responseText;
};

magRadar.refreshImagePath = function ()
{
   var sUrl = "ajax-get-valid-image-prefix.aspx" + magRadar.getCacheBuster(true);
   var callback = {
	  success:magRadar.ajax_onRefreshImage,
	  failure:magRadar.AjaxError
   };                
   var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback, null);
};

//Change to DOMReady when YUI upgraded
// We should not need LinkDeny now that throttling has been implemented (see case #2537, http://mammoth/fogbugz/default.asp?2537)
//YAHOO.util.Event.addListener(window, "load", magRadar.imagePathRefresh_Load);

//========================================================//
//End image prefix refreshing for LinkDeny
//========================================================//

magRadar.sprintf = function() {
    var stringOutput = arguments[0];
    for(var i = 0; i < arguments.length - 1; i++) {
        var r = new RegExp("\\x7B" + i + "\\x7D", "g"); //{0}
        stringOutput =  stringOutput.replace(r, arguments[i+1]);
	}
    return stringOutput;
};

/**
 * Lexicographical compare
 */
magRadar.alphaCompare = function(a, b) 
{
    var sa = a[1].toUpperCase();
    var sb = b[1].toUpperCase();
    
    if (sa < sb) {
        return -1;
    } else if (sa > sb) {
        return 1;
    } else {
        return 0;
    }
};