/*
	Opens a popup window
*/
function OpenNewWindow(openURL,WindowName,width,height) 
{
    var newwindow = window.open(openURL,WindowName,'toolbar=no,location=no,status=no,menubar=no,resizable=yes,width='+width+',height='+height+',scrollbars=yes,top=30,left=30');
    newwindow.focus();
}

/*
	Adds a function to the windows onload event
*/
function addOnLoadFunction(func)
{
	var oldOnLoad = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldOnLoad();
			func();
		}
	}
}

/*
	Reloads ActiveX controls for IE
*/
if(document.all) addOnLoadFunction(reloadActiveXTags);

var activeXTagsReloaded = false;
function reloadActiveXTags()
{
	if(activeXTagsReloaded) return;
	activeXTagsReloaded = true;

	tagTypeArray = new Array("embed","applet","object");

	for(h=0;h < tagTypeArray.length;h++)
	{
		tags = document.getElementsByTagName(tagTypeArray[h]);

		ids = new Array(tags.length);
		for(i = 0;i < tags.length;i++)
		{
			ids[i] = tags[i].getAttribute("id");
		}

		for(i = 0; i < ids.length; i++)
		{
			if(!ids[i]) return;
			tag = getE(ids[i]);

			paramStrings = new Array(tag.childNodes.length);
			for (j = 0; j < tag.childNodes.length; j++)
			{
				if (tag.childNodes[j].tagName == "PARAM")
				{
					paramStrings[j] = tag.childNodes[j].outerHTML;
				}
			}

			parNode = tag.parentNode;
			tagHTML = parNode.innerHTML;

			tagName = tagTypeArray[h];

			regStr = "(<" + tagName + "[\\s\\S]*?id=['\\\"]?" + ids[i] + "['\\\"]?[\\s\\S]*?>)([\\s\\S]*?</" + tagName + ">)";
			reg = new RegExp(regStr,"gi"); //the innerHTML does not contain the param tags...

			reg.input = tagHTML;

			splitObjectTag = reg.exec(tagHTML);

			outputHtml = splitObjectTag[1];
			for(k = 0;k < paramStrings.length;k++)
			{
				outputHtml += paramStrings[k];
			}
			outputHtml += splitObjectTag[2];

			span = document.createElement("span");
			span.innerHTML = outputHtml;

			parNode.insertBefore(span,tag);
			tag.style.display = "none";
			parNode.removeChild(tag);
		}
	}
}

function SetMultiCookie(sCookieName, sCookieSubName, value, expires, path, domain, secure )
{
	if(sCookieSubName == null)
		SetCookie(sCookieName, value, expires, path, domain, secure);
	else
	{
		var bFound = false;
		var newValue = '';
		var completeValue = GetCookie(sCookieName,null);

		var aSubList = completeValue.split('&');
		for(var i = 0;i < aSubList.length;i++)
		{
			if(i!=0)
				newValue += '&';

			var sNameValuePair = aSubList[i];
			var aNameValueCombo = sNameValuePair.split('=')

			var sNamePart = aNameValueCombo[0];
			if(sNamePart == sCookieSubName)
			{
				newValue +=	sNamePart +'=' + escape(value);
				bFound = true;
			}
			else
			{
				newValue += sNameValuePair;
			}	
		}
		
		if(!bFound)
		{
			//new sub cookie
			if(newValue != '')
				newValue += '&';	
			newValue +=	sCookieSubName +'=' + escape(value);
		}
		
		SetCookie(sCookieName, newValue, expires, path, domain, secure, true)	
	}
}

function SetCookie(name, value, expires, path, domain, secure, bDontEscapeValue)
{
	var curCookie = name + "=" + 
	(((bDontEscapeValue) && bDontEscapeValue == true) ? value : escape(value)) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	"; path=" + ((path) ? path : "/") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "") + ";";
	document.cookie = curCookie;
}

function GetCookie(key1,key2)
{
	var sCookie = new String(document.cookie);
	//alert(sCookie)
	if(key1 != null)
	{
		var aCList = sCookie.split('; ');
		for(var i = 0;i < aCList.length;i++)
		{
			sCookie = aCList[i];
			var oReg = new RegExp("(^"+key1+"=)(.*)","ig");
			var aResult = oReg.exec(sCookie); //Do not use: sCookie.match(oReg) Netscape fails
			if(aResult != null)
			{
				sCookie = RegExp.$2;
				if(key2 != null)
				{
					aCList = sCookie.split("&");
					for(i = 0;i < aCList.length;i++)
					{
						sCookie = aCList[i];
						oReg = new RegExp("(^"+key2+"=)(.*)","ig");
						aResult = oReg.exec(sCookie);  //Do not use: sCookie.match(oReg) Netscape fails
						if(aResult != null)
						{
							sCookie = unescape(RegExp.$2);
							break;
						}else {
							sCookie = "";
						}
					}
				}
				break;
			} else {
				sCookie = "";
			}
		}
	}
	return sCookie;
}

function setImg(img, imgPath)
{
	if(imgPath.substring(0,0) != "/") // if not root path
	{
		var src = img.src;
		var lastIdx = 0;
		var prevIdx = src.indexOf("/") + 1;
		while (prevIdx != 0)
		{
			lastIdx += prevIdx;
			prevIdx = src.substring(lastIdx, src.length).indexOf("/") + 1;
		}
		imgPath = src.substring(0, lastIdx) + imgPath;
	}
	img.src = imgPath;
}

function getE(id)
{
	return document.getElementById(id);
}

// HBX helpers
//function GetFileName()
//{
//	var href = window.location.pathname;
//	return href.substring(href.lastIndexOf("/") + 1, href.length);
//}

function _hbxStrip(a)
{
	a = a.split("|").join("");
	a = a.split("&").join("");
	a = a.split("'").join("");
	a = a.split("#").join("");
	a = a.split("$").join("");
	a = a.split("%").join("");
	a = a.split("^").join("");
	a = a.split("*").join("");
	a = a.split(":").join("");
	a = a.split("!").join("");
	a = a.split("<").join("");
	a = a.split(">").join("");
	a = a.split("~").join("");
	a = a.split(";").join("");
	a = a.split(" ").join("+");
	return a;    
}

function SetDisplayByName(name, display)
{
	var elements = document.getElementsByName(name);
	for (var i = 0; i < elements.length; i++)
	{
		elements[i].style.display = display;
	}
}

function getTbodyDisplayOn()
{
	return document.all ? "block" : "table-row-group";
}

function writeDivHdrImg()
{
	var subLevel = getFileSafeSubLevelFromCookies();
	var lvl = subLevel || fileSafeLevel;
	var alt = levelName;

	if (subLevel)
	{
		var subLevelName = GetCookie("Localization", "SubLevelName");
		if (subLevelName && subLevelName != '')
			alt = unescape(subLevelName).replace("+", " ");
	}
	
	document.write("<img src='/images/gen/divheaders/"+lvl+".gif' alt='"+alt+"' title='"+alt+"'/>");
}

function writeMenuJsInclude()
{
	HasMenu = true;
	var menuVersion = getFileSafeSubLevelFromCookies() || fileSafeLevel;
//	<script type="text/javascript" src="{df:AddHash('/Includes/js/Javascript.js')}">&#160;</script>
//	document.write('<script type="text/javascript" src="{df:AddHash(&quot;/Includes/js/MenuArrays/MenuArray_'  + fileSafeLevel+ '__' + menuVersion+ '.js&quot;)}">&#160;</script>');
	document.write('<script type="text/javascript" src="/Includes/js/MenuArrays/MenuArray_' + fileSafeLevel + '__' + menuVersion+ '.js">&#160;</script>');
}

function limit(field, chars) {
       if (field.value.length > chars) {
              field.value = field.value.substr(0, chars);
              alert('You are only allowed to enter '+chars+' characters in the '+field.name+' field!');
       }
}

function getFileSafeSubLevelFromCookies()
{
	var lvl;
	var subLevelId = GetCookie("Localization", "SubLevelId");
	if (subLevelId != null && subLevelId != "")
	{
		var subLevelNumber = GetCookie("Localization", "SubLevelNumber");
		lvl = subLevelNumber + "_" + subLevelId;
	}
	return lvl;
}

/* DF functions */
var DF = window.DF;

DF.isFF = navigator.userAgent.search(/firefox/i) > -1;
DF.isSafari = navigator.userAgent.search(/safari/i) > -1;

DF.namespace('DF.array');

DF.array.isArray = function(o){
	if (DF.isSafari)
		return (typeof(o)).toLowerCase() == 'object' && o.length && o.pop && o.push;
	else
		return o.constructor && o.constructor.toString().indexOf('Array') > -1;
}

DF.array.applyFunction = function(array, func, params){
	if (array){
		if (DF.array.isArray(array)){
			for (var i = 0; i < array.length; i++){
				func(array[i], params);
			}
		}else{
			func(array, params);
		}
	}
};

DF.namespace('DF.string');

DF.string.endsWith = function(src, sub){
	if (!src || !sub || src.length == 0 || sub.length == 0 || src.length < sub.length)
		return false;

	for (var i = 0; i < src.length && i < sub.length; i++){
		if (src.charAt(src.length - i - 1) != sub.charAt(sub.length - i - 1))
			return false;
	}
	return true;
};

DF.string.isNullOrEmpty = function(str){
	return (typeof(str)).toLowerCase() != 'string' || str.length == 0;
};

DF.namespace('DF.Dom');

DF.Dom.isEl = function(el){
	return !!(el.nodeType && el.tagName);
};

DF.Dom.getAllElements = function(rootEl, els){
	els = els || [];
	rootEl = $(rootEl) || document.body;
	for (var i = 0; i < rootEl.childNodes.length; i++){
		var child = rootEl.childNodes[i];
		if (DF.Dom.isEl(child)){
			els.push(child);
			DF.Dom.getAllElements(child, els);
		}
	}
	return els;
};

DF.Dom.getElementsByClassName = function(className, tagName, rootEl){
	var els = [];
	if (!DF.string.isNullOrEmpty(className)){
		rootEl = $(rootEl) || document;
		var searchEls = DF.string.isNullOrEmpty(tagName) ? DF.Dom.getAllElements() : rootEl.getElementsByTagName(tagName);
		for (var i = 0; i < searchEls.length; i++){
			var el = searchEls[i];
			if (DF.Dom.hasClass(el, className))
				els.push(el);
		}
	}
	return els;
};

DF.Dom.addClass = function(el, className){
	DF.array.applyFunction(el, function(el){
		el = $(el);
		if (!DF.string.isNullOrEmpty(className) && !DF.Dom.hasClass(el, className))
			el.className += ' ' + className;
	});
};

DF.Dom.removeClass = function(el, className){
	DF.array.applyFunction(el, function(el){
		el = $(el);
		if (!DF.string.isNullOrEmpty(className)){
		
			var r = new RegExp('(?:^|[ ]+)'+className+'(?:$|[ ]+)');
			el.className = el.className.replace(r, '');
		}
	});
};

DF.Dom.hasClass = function(el, className){
	if (!DF.string.isNullOrEmpty(className)){
		el = $(el);
		if (el){
			var classes = el.className.split(' ');
			for (var i = 0; i < classes.length; i++){
				if (classes[i] == className)
					return true;
			}
		}
	}
	return false;
};

DF.Dom.removeAttribute = function(el, attr){
	DF.array.applyFunction(el, function(el, attr){ // el array
		DF.array.applyFunction(attr, function(attr, el){ // attr array
			$(el).removeAttribute(attr);
		}, el);
	}, attr);
};

DF.Dom.removeEl = function(el){
	if (el){
		DF.array.applyFunction(el, function(el){
			el = $(el);
			if (el.parentNode) el.parentNode.removeChild(el);
		});
	}
};

DF.Dom.prependEl = function(child, parent){
	child = $(child);
	parent = $(parent);

	if (parent.childNodes.length > 0)
		parent.insertBefore(child, parent.childNodes[0]);
	else
		parent.appendChild(child);
};

//TODO: find out why this is here
//DF.namespace('Core');
