/* BEGIN: common_mediafly */

// IE 6.0 fix
if (!Array.indexOf) {
    Array.prototype.indexOf = function(obj) {
        for (var i=0; i<this.length; i++) {
            if (this[i] == obj){
                return i;
            }
        }
        
        return -1;
    }
}
	
String.prototype.trim = function() 
{ 
	return this.replace(/^\s+|\s+$/g, ''); 
}

function navigate(url)
{
	window.location = url;
}

function popup(url, width, height, target)
{
	var id = (target == null) ? new Date().getTime() : target;
	
	if(this.win) this.win.close();

	var x = (screen.width - width) / 2;
	var y = (screen.height - height) / 2;
	
	var params = '';
	
	params += 'toolbar=0';
	params += ',scrollbars=0';
	params += ',location=0';
	params += ',statusbar=0';
	params += ',menubar=0';
	params += ',resizable=1';
	params += ',width=' + width;
	params += ',height=' + height;
	params += ',left=' + x;
	params += ',top=' + y;
	
	this.win = window.open(url, id, params);
	
	win.focus();
	
	return win;
}

function hidePopup()
{
	AjaxControlToolkit.PopupControlBehavior.__VisiblePopup.hidePopup();
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function setDimmensions(beginMarker, endMarker, doPostback, adjustedWidth, adjustedHeight, postbackControl)
{
    var begin = document.getElementById(beginMarker);
    var end = document.getElementById(endMarker);
    
    if (begin == null || end == null)
        return;
        
    document.getElementById(adjustedWidth).value = parseInt( findPosX(end) - findPosX(begin) );
    document.getElementById(adjustedHeight).value = parseInt( findPosY(end) - findPosY(begin) );
    
    if (doPostback)
    {
        document.getElementById(postbackControl).value = "true";
        __doPostBack(postbackControl, '');
    }
}

function registerEvent(obj, eventName, handler)
{
    if (obj.addEventListener)
        obj.addEventListener(eventName, handler, false);
    else if (obj.attachEvent)
        obj.attachEvent('on'+eventName, handler);
}

// NOTE: http://www.quirksmode.org/viewport/compatibility.html
function getPageHeight()
{
    var y;
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
	    y = document.body.scrollHeight;
    }
    else // Explorer Mac;
         //would also work in Explorer 6 Strict, Mozilla and Safari
    {
	    y = document.body.offsetHeight;
    }
    
    return y;
}

function getPageWidth()
{
    var x;
    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
	    x = document.body.scrollWidth;
    }
    else // Explorer Mac;
         //would also work in Explorer 6 Strict, Mozilla and Safari
    {
	    x = document.body.offsetWidth;
    }
    
    return x;
}

function getViewableHeight()
{
    var y;
    if (self.innerHeight) // all except Explorer
    {
	    y = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
	    // Explorer 6 Strict Mode
    {
	    y = document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers
    {
	    y = document.body.clientHeight;
    }
    
    return y;
}

function getViewableWidth()
{
    var x;
    if (self.innerWidth) // all except Explorer
    {
	    x = self.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
	    // Explorer 6 Strict Mode
    {
	    x = document.documentElement.clientWidth;
    }
    else if (document.body) // other Explorers
    {
	    x = document.body.clientWidth;
    }
    
    return x;
}
// END NOTE

function setWidth(elem, width)
{
    if (elem)
        elem.style.width = width + "px";
}

function setHeight(elem, height)
{
    if (elem)
        elem.style.height = height + "px";
}

function setTop(elem, top)
{
    if (elem)
        elem.style.top = top + "px";
}

function setLeft(elem, left)
{
    if (elem)
        elem.style.left = left + "px";
}

function setVisible(elem, doSetVisible)
{
    if (doSetVisible)
    {
        elem.style.visibility = 'visible';
        elem.style.display = 'inline';
    }
    else
    {
        elem.style.visibility = 'hidden';
        elem.style.display = 'none';
    }
}

function selectRadioButton(radioId) 
{
	var radio = document.getElementById(radioId);
	
	if (radio) 
	{
		radio.checked = true;
	}
}

function CancelBubbleForAnyBrowser(evt)
{
    if (typeof(evt.returnValue) !== "undefined") {
        evt.returnValue = false;
    }

    if (typeof(evt.cancelBubble) !== "undefined") {
        evt.cancelBubble = true;
    }

    if (typeof(evt.preventDefault) !== "undefined") {
        evt.preventDefault();
    }

    if (typeof(evt.stopPropagation) !== "undefined") {
        evt.stopPropagation();
    }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// --- see http://peter.michaux.ca/article/553 for discussion and the code.
// Usually we use something like ScriptManager.RegisterStartupScript if we
// need a javascript function to be run when the page loads. This doesn't
// work in all cases. In AJAX.NET, behaviors and extenders are created after 
// our registered script is called. In case our script needs to access the
// behavior or extender, it will not find it because it hasn't been created.
// We cannot really just attach to window.onload either, because of the quirks
// of that behavior that is discussed in the blog entry mentioned above.

var stack = [],
    interval,
    loaded; // has window.onload fired?

function doPoll() {
  var notFound = [];
  for (var i=0; i<stack.length; i++) {
    if ($find(stack[i].id)) {
        if (stack[i].callback != null) {
            stack[i].callback();
        }
    } else if ($get(stack[i].id)) {
        if (stack[i].callback != null) {
            stack[i].callback();
        }
    }
    else {
      notFound.push(stack[i]);
    }
  }
  stack = notFound;
  if (notFound.length < 1 || loaded) {
    stopPolling();
  }
}

function startPolling() {
  if (interval) {return;}
  interval = setInterval(doPoll, 100);
}

function stopPolling() {
  if (!interval) {return;}
  clearInterval(interval);
  interval = null;
}

function onAvailable(id, callback) {
  stack.push({id:id, callback:callback});  
  startPolling();
}

function movePopup(idToMove)
{
    if (typeof popup_insert_container == "undefined")
        return;
        
    var divToMove = $get(idToMove);
    var dest = $get(popup_insert_container);
    
    if (divToMove != null && dest != null) {
        divToMove.parentNode.removeChild(divToMove);
        dest.appendChild(divToMove);
    }    
}

function isNull(value) {
	return (value == null);
}

function isBlank(value){
	if (value == null) {
		return true;
	}
	
	for(var i=0; i<value.length; i++) {
		if ((value.charAt(i)!=' ')&&(value.charAt(i)!="\t")&&(value.charAt(i)!="\n")&&(value.charAt(i)!="\r")) {
			return false;
		}
	}
		
	return true;
}
	
function isDigit(num) {
	if (num.length > 1) {
		return false;
	}
	
	var string="0123456789";
	
	if (string.indexOf(num)!=-1) {
		return true;
	}

	return false;
}
	
function isInteger(value)  {
	if (isBlank(value)) {
		return false;
	}

	for (var i=0; i<value.length; i++) {
		if (!isDigit(value.charAt(i))) { 
			return false;
		}
	}
	
	return true;
}

function emptyHandler(){}

// TODO: see if this is available in ajax toolkit (in case we need localized versions of the below strings)
var _days = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
function day(index)
{
    return _days[index];
}

var _months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
function month(index)
{
    return _months[index];
}

function is_ssl()
{
	return (window.parent.document.location.protocol == 'https:');
}

function secure_include(host, path) 
{
	document.write('<script type="text/javascript" src="' + window.parent.document.location.protocol + '//' + host + path + '"/></script>');
}

function safeUrchinTracker(path) 
{
    if (typeof pageTracker != "undefined") {
        pageTracker._trackPageview(path);
    }
}

function toggleVisible(elem, targetId) {
	var targetElem = document.getElementById(targetId);
	
	if (targetElem) {
		if (elem.checked) {
			targetElem.style.display = 'inline';
		} else {
			targetElem.style.display = 'none';
		}
	}	
}



/******************  Detect Browser and Version  *******************/

var BROWSER = navigator.appName;
var b_version = navigator.appVersion;
var VERSION = parseFloat(b_version);

if(BROWSER == "Netscape")
{
    if(navigator.userAgent.indexOf("Safari") != -1)
    {
        BROWSER = "Safari"
    }
    else if(navigator.userAgent.indexOf("Firefox") != -1)
    {
        BROWSER = "Firefox";
    }
}
    
if (BROWSER == "Microsoft Internet Explorer")
{
    var i = b_version.indexOf("MSIE ");
    VERSION = parseFloat(b_version.slice(i + 5));
}

