//--------------- navigation items ------------------//
var agt = navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie4up = (is_ie && (is_major >= 4));

var tID = null;
var mb = null;

if (document.all && !document.getElementById) {document.getElementById = function(id) {return document.all[id];}}

//----------------------------------------Utility functions----------------------------------------//
function openWindow(url,width,height,givefocus){
   width = (width == null ? 700 : width);
   height = (height == null ? 700 : height);
   var newWin = window.open(url,"newWin","width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
   if (givefocus)newWin.focus();
};

function swap_faq_div(id){
	document.getElementById("faq"+id).style.display = (document.getElementById("faq"+id).style.display == "block") ? "none" : "block";
};

function setStatus(txt) {
  window.status = txt;
  return true;
}

function repeatString(str,cnt) {
  var res = "";
  for (i=0;i<cnt;i++) res += str;
  return res;
}

function ReplaceString(txt,srch,rep) {
  var p;
	if (txt.indexOf(srch) != -1) {
	  do {
			p=txt.indexOf(srch);
			txt=txt.substr(0,p)+rep+txt.substr(p+srch.length,txt.length);
		}
  	while(txt.indexOf(srch) != -1);
	}	
  return(txt);
}

function makeSafePathString(plaintext) {
	var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "_/";
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";
		}
		else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		}
	}
	return encoded.toLowerCase();
}

function UrlEncode(plaintext) {
	var SAFECHARS = "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "-_.!~*'()";
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";
		}
		else if (SAFECHARS.indexOf(ch) != -1) {
			encoded += ch;
		}
		else {
			var charCode = ch.charCodeAt(0);
			encoded += "%";
			encoded += HEX.charAt((charCode >> 4) & 0xF);
			encoded += HEX.charAt(charCode & 0xF);
		}
	}
	return encoded;
}

function UrlDecode(encoded) {
	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		}
		else if (ch == "%") {
			if (i < (encoded.length-2) && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			}
			else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		}
		else {
			plaintext += ch;
			i++;
		}
	}
	return plaintext;
}

function getUrlParam(paramName) {
	var currentUrl = window.location.search;
	if (currentUrl.indexOf(paramName + "=") == -1) return "";
	var strBegin = currentUrl.indexOf(paramName) + (paramName.length+1);
	var strEnd = currentUrl.indexOf("&",strBegin);
	if (strEnd==-1)	strEnd = currentUrl.length;
	return UrlDecode(currentUrl.substring(strBegin,strEnd));
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
  return null;
}
  
function SetCookie(name,value,path,expires,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function findCookieString(stringS,fullS) {
	for (var i=0; i<fullS.length; i++) {
		if (fullS.substring(i,i+stringS.length) == stringS) {
			return true;
			break;
		}
	}
	return false;
}

function replaceCookieString(oldS,newS,fullS) {
	for (var i=0; i<fullS.length; i++) {
		if (fullS.substring(i,i+oldS.length) == oldS) {
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length);
		}
	}
	return fullS;
}

//----------------------------------------Navigation Menus----------------------------------------//

function isItem(itm) {return ((!!itm) && (itm.nodeType == 1));}
function resetTimer() {if (!!tID) clearTimeout(tID); tID = setTimeout("executeTimer()",600);}
function executeTimer() {hideSubMenus();}
function getAbsLeft(elm) {var lft = 0; while (elm) {if (elm.tagName == 'BODY') break; lft += elm.offsetLeft; elm = elm.offsetParent;} return lft;}
function getAbsTop(elm) {var tp = 0; while (elm) {if (elm.tagName == 'BODY') break; tp += elm.offsetTop; elm = elm.offsetParent;}	return tp;}

function hideSubMenus(itm,clearSelected) {
	var mnu,subitm,subitm2 = null;
	if (clearSelected == null) clearSelected = true;
	if (!mb) return;
	if (!isItem(itm)) itm = mb;
	for (var i=0;i<itm.childNodes.length;i++) {
		subitm = itm.childNodes[i];
		if (!isItem(subitm)) continue;
		if (clearSelected == true) {
			if (subitm.className == "o") subitm.className = "";
		}	
		mnu = document.getElementById(subitm.getAttribute("m"));
		if (mnu) {
			mnu.style.visibility = "hidden";
			if (!!document.all && !!document.createElement && !!document.appendChild && !!is_ie4up) {
				var ienavfix = document.getElementById(mnu.id + "fix");
				if (!!ienavfix) {ienavfix.style.display = "none";}
			}	
			hideSubMenus(mnu,true);
		}
	}
}

function showSubMenu(itm) {
	var pitm,mnu = null;
	pitm = itm.parentNode;
	mnu = document.getElementById(itm.getAttribute("m"));
	if (mnu) {
		mnu.style.display = "block";
		mnu.style.visibility = "visible";
		if (pitm == mb) {
			if (is_ie4up)
				mnu.style.top = getAbsTop(itm) + pitm.offsetHeight + 2 + "px"
			else	
				mnu.style.top = getAbsTop(itm) + pitm.offsetHeight + 20 + "px";
			mnu.style.left = getAbsLeft(itm) + "px";
		} else {
			mnu.style.top = getAbsTop(itm) + "px";
			mnu.style.left = getAbsLeft(itm) + pitm.offsetWidth - 5 + "px";
		}
		if (!!document.all) {
			var ienavfix = document.getElementById(mnu.id + "fix");
			if (!!ienavfix) {
				ienavfix.style.display = "block";			
			}
			else if (!!document.all && !!document.createElement && !!document.appendChild && !!is_ie4up && !!mb) {
				var ienavfix = document.createElement("iframe");
				ienavfix.id = mnu.id + "fix";
				ienavfix.frameBorder = "no";
				ienavfix.style.position = "absolute";
				ienavfix.style.zIndex = "1";
				ienavfix.style.display = "block";
				ienavfix.scrolling = "no";
				ienavfix.style.top = mnu.style.top;
				ienavfix.style.left = mnu.style.left;
				ienavfix.style.width = mnu.offsetWidth + "px";
				ienavfix.style.height = mnu.offsetHeight + "px";
				mb.appendChild(ienavfix);
			}		
		}
	}
}

function itmMouseOver(evt) {
	var pitm, itm, subitm, mnuid, cmd = null;
	clearTimeout(tID);
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (!evt) return false;
	if (evt.target) {
		itm = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
	}
	else {
		itm = evt.srcElement;
	}
	if (!itm) return false;
	var pitm = itm.parentNode;
	for (var i=0;i<pitm.childNodes.length;i++) {
		subitm = pitm.childNodes[i];
		if (isItem(subitm)) subitm.className = "";	
	}
	itm.className = "o";
	cmd = itm.getAttribute("cmd");
	if (cmd) {
		if (cmd.charAt(0) == "/") {
			window.status = window.location.protocol + "//" + window.location.host + cmd;
		}
		else {
			window.status = cmd;
		}
	}	
	else {
		window.status = "";
	}
	hideSubMenus(pitm,false);
	mnuid = itm.getAttribute("m");
	if ((!!mnuid) && (mnuid.length > 0)) showSubMenu(itm);	
}

function itmClick(evt) {
	var cmd,elm = null;
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	if (!evt) return false;
	if (evt.target) {
		elm = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target;
	}
	else {
		elm = evt.srcElement;
	}	
	if (!elm) return false;
	cmd = elm.getAttribute("cmd");
	if (!cmd) {
		pelm = elm.parentNode;
		if (pelm.nodeType == 1) {
			if (pelm.className == "mb") {
				if (!!elm.getAttribute("m")) {
					cmd = "/" + elm.getAttribute("m").substr(2,999).toLowerCase();
				}
				else {
					cmd = "/" + elm.innerHTML.toLowerCase();
				}	
			}
			else if (pelm.className == "m") {
				if (!!elm.getAttribute("m")) {
					cmd = "/" + elm.getAttribute("m").substr(2,999).toLowerCase();
					cmd = ReplaceString(cmd,"_","/");
				}
				else {
					if (!!pelm.id) {
						cmd = "/" + pelm.id.substr(2,999).toLowerCase();
						cmd = ReplaceString(cmd,"_","/");
						cmd = cmd + "/" + ReplaceString(elm.innerHTML.toLowerCase(),"/","_");
					}
					else {
						cmd = "/" + ReplaceString(elm.innerHTML.toLowerCase(),"/","_");
					}	
				}	
			}
			else {
				cmd = null;
			}
		}
	}
	if (cmd) {
		cmd = ReplaceString(cmd," ","");
		cmd = ReplaceString(cmd,"&amp;","and");
		cmd = makeSafePathString(cmd);
		window.location = cmd;
	}
}

function initItem(itm) {
	var mnu,subitm = null;
	itm.onmouseout = resetTimer;
	itm.onmouseover = itmMouseOver;
	itm.onclick = itmClick;
	mnu = document.getElementById(itm.getAttribute("m"));
	if (!!mnu) {
		mnu.onmouseout = resetTimer;
		mnu.style.visibility = "hidden";
		for (var i=0;i<mnu.childNodes.length;i++) {
			subitm = mnu.childNodes[i];
			if (isItem(subitm)) initItem(subitm);
    }
  }
}

function initMenu() {
	var itm = null;
	if (!document.getElementById && !document.childNodes) return;
	mb = document.getElementById("mb");
	if (!mb) return;
	//document.documentElement.onclick = hideSubMenus;
	for (var i=0; i<mb.childNodes.length; i++) {
		itm = mb.childNodes[i];
		if (isItem(itm)) initItem(itm);
	}	
}

function doOnload() {initMenu();}


// -------------- columns fix ------------------------//
function scriptInit() {
		if (!document.getElementById) {
			return;
			}
		}
		function addEvent(elm, evType, fn, useCapture) {
			if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
			} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
			} else {
			elm['on' + evType] = fn;
			}
		}
		function setTall() {
			if (document.getElementById) {
			var divs = new Array(document.getElementById('middle'), document.getElementById('right'), document.getElementById('left'));
				var maxHeight = 0;
				for (var i = 0; i < divs.length; i++) {
					if (divs[i] != null && divs[i].offsetHeight > maxHeight) maxHeight = divs[i].offsetHeight;
				}
				for (var i = 0; i < divs.length; i++) {
					if (divs[i] != null){
						divs[i].style.height = maxHeight + 'px';
			
						if (divs[i].offsetHeight > maxHeight) {
							divs[i].style.height = (maxHeight - (divs[i].offsetHeight - maxHeight)) + 'px';
						}
					}
				}
			}
		}
		addEvent(window, 'load', setTall, false);
		addEvent(window, 'resize', setTall, false);