var adminFolder = '/icadmin/';

function addOnload(newFunction) {
	var oldOnload = window.onload;
	
	if (typeof oldOnload == "function") {
		window.onload = function() {
			if (oldOnload) {
				oldOnload();
			}
			newFunction();
		}
	}
	else {
		window.onload = newFunction;
	} 
}

var xmlhttp = false; 
try 	{ 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e) 
			{ 
			try	{ 
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
					} 
					catch (E) { 
						xmlhttp = false; 
						} 
			} 

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
	xmlhttp = new XMLHttpRequest(); 
}

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

document.getElementsByClassName = getElByClass;

document.getTimeStamp = getTS;

function getTS() {
	return new Date().getTime();
}

function getElByClass(obj, cName) {
	var allObjects = document.getElementsByTagName(obj);
	var allValidOb = [];
	for (i=0; i<allObjects.length; i++)
		if (allObjects[i].className.indexOf(cName) > -1)
			allValidOb.push(allObjects[i]);
	return allValidOb;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function showhide(obj) {
	if (obj.style.display == "none")
		obj.style.display = "block";
	else
		obj.style.display = "none";
}

addOnload(getAllSwapImages);

function getAllSwapImages() {
	var allImages = document.getElementsByClassName("img","swapImage");
	for (i=0; i<allImages.length; i++) {
		allImages[i].onmouseover = swapSrc;
		allImages[i].onmouseout = swapSrc;
	}
}

function swapSrc() {
	var newSrc = this.src.substr(0, this.src.lastIndexOf("/")+1);
	var currentSrc = this.src.substr(this.src.lastIndexOf("/")+1);
	if (currentSrc.indexOf("_") == 0) newSrc += currentSrc.substr(1);
		else newSrc += "_" + currentSrc;
	this.src = newSrc;
}

function swapObSrc(ob) {
	var newSrc = ob.src.substr(0, ob.src.lastIndexOf("/")+1);
	var currentSrc = ob.src.substr(ob.src.lastIndexOf("/")+1);
	if (currentSrc.indexOf("_") == 0) newSrc += currentSrc.substr(1);
		else newSrc += "_" + currentSrc;
	ob.src = newSrc;
}

function getURLVar(urlVarName) {
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
		var urlVars = urlHalves[1].split('&');
		for(i=0; i<=(urlVars.length); i++) {
			if(urlVars[i]){
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	return urlVarValue;   
}

function doAlert() {
	alert("this is test");
}

addOnload(getCloseButtons);

function getCloseButtons() {
	allCloseButtons = document.getElementsByClassName("img", "closeBox");
	for (i=0; i<allCloseButtons.length; i++) {
		allCloseButtons[i].onclick = doClose;
	}
}

function doClose() {
	this.parentNode.style.visibility = "hidden";
}

function urldecode( str ) {
    var histogram = {}, histogram_r = {}, code = 0, str_tmp = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    ret = decodeURIComponent(ret);
 
    return ret;
}

function displayStatus(msg, obj) {
	var statusOb = document.getElementById('statusBox');
	statusOb.innerHTML = msg;
	if (obj) {
		var xy = findPos(obj);
		statusOb.style.top = xy[1] + obj.clientHeight + "px";
		statusOb.style.left = xy[0] + "px";
	}
	statusOb.style.visibility = "visible";
	setTimeout('hideStatus()', 2000);
}

function hideStatus() {
	opacity('statusBox',100,0,1000);
	setTimeout('restoreStatus()', 1000);
}

function restoreStatus() {
	document.getElementById('statusBox').style.visibility = "hidden";
	changeOpac(100,'statusBox');
}

function opacity(id, opacStart, opacEnd, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

	if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
    if(document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
}

try {
Node.prototype.swapNode = function (node) {
  var nextSibling = this.nextSibling;
  var parentNode = this.parentNode;
  node.parentNode.replaceChild(this, node);
  parentNode.insertBefore(node, nextSibling);  
} } catch(err) {
//Handle errors here
}