// $Header: /hsphere/local/home/hszeto/cvsrep/avire/lib/ikit-js.js,v 1.3 2006/12/20 04:19:51 hszeto Exp $
var feature_no_cookie = 'n';

function chgArtType() {
  if(document.getElementById('articletype').value=='Article') {
    document.getElementById('isreview').style.display="none";
  } else {
    document.getElementById('isreview').style.display="block";
  }
}

function chgTrkFld() {
  if(document.getElementById('trkfldtype').value=='d') {
    document.getElementById('trkfldoptions').style.display="block";	
  } else {
    document.getElementById('trkfldoptions').style.display="none";		
  }
}


function setMenuCon(foo1,foo2) {
  document.getElementById('menu_url').value=foo1;
  document.getElementById('menu_name').value=foo2;
}

function genPass(w1,w2,w3){
  vo="aeiouAEU";
  co="bcdfgjklmnprstvwxzBCDFGHJKMNPQRSTVWXYZ0123456789_$%#";
  s=Math.round(Math.random());
  l=8;
  p='';
  for (i=0; i < l; i++)
  {
    if (s)
    {
      letter=vo.charAt(Math.round(Math.random() * (vo.length - 1) ));
      s=0;
    } else
    {
      letter=co.charAt(Math.round(Math.random() * (co.length - 1) ));
      s=1;
    }
    p=p + letter;
  }
  document.getElementById(w1).value=p;
  document.getElementById(w2).value=p;
  document.getElementById(w3).value=p;
}

function setUserModule(foo1) {
  document.getElementById('usermoduledata').value=foo1;
}

function setSomeElement(fooel,foo1) {
if (foo1!="")
  document.getElementById(fooel).value=foo1;
//  document.getElementById(fooel).value=document.getElementById(fooel).value +foo1;
  
  //"\r\n"
}

function replaceSome(fooel,what,repl) {
  document.getElementById(fooel).value = document.getElementById(fooel).value.replace(what,repl);
}

function replaceLimon(vec) {
  document.getElementById(vec[0]).value = document.getElementById(vec[0]).value.replace(vec[1],vec[2]);
}

function setUserModuleFromCombo(id) {
  document.getElementById('usermoduledata').value=document.getElementById('usermoduledata').value + document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
  //document.getElementById('usermoduledata').value='das';
}
function setUserModuleFromCombo_tinymce(id) {
  tinyMCE.execCommand('mceInsertContent',false,document.getElementById(id).options[document.getElementById(id).selectedIndex].value);
  //document.getElementById('usermoduledata').value='das';
}

function show(foo,f,section) {
	document.getElementById(foo).style.display = "block";
	if (f) { setCookie(foo, "o", section); }
}

function hide(foo,f, section) {
	if (document.getElementById(foo)) {
		document.getElementById(foo).style.display = "none";
		if (f) {
			var wasnot = getCookie(foo, section, 'x') == 'x';
			setCookie(foo, "c", section);
			if (wasnot) {
				history.go(0);
			}
		}
	}
}

/*
function flip(foo,style) {
	showit = 'show_' + escape(foo);
	if (!style) style = "block";
	if (document.getElementById(foo).style.display == "none") {
		document.getElementById(foo).style.display = style;
		setSessionVar(showit,'y');
	} else {
		if (document.getElementById(foo).style.display == style) {
			document.getElementById(foo).style.display = "none";
			setSessionVar(showit, 'n');
		} else {
			document.getElementById(foo).style.display = style;
			setSessionVar(showit, 'y');
		}
	}
}
*/

function flip(foo) {
	if (document.getElementById(foo).style.display == "none") {
		show(foo);
	} else {
		if (document.getElementById(foo).style.display == "block") {
			hide(foo);
		} else {
			show(foo);
		}
	}
}

function toggle(foo) {
  if( document.getElementById(foo).style.display == "none") {
    show(foo, true, "menu");
   // setCookie(foo, "o");
    HS_setArrowState(foo);
  } else {
  	if( document.getElementById(foo).style.display == "block") {
	    hide(foo, true, "menu");
	   // setCookie(foo, "c");
	    HS_setArrowState(foo);
	} else {
	    show(foo, true, "menu");
	    //setCookie(foo, "o");
	    HS_setArrowState(foo);
	}
  }
}

function tikitabs(focus,max) {
	for (var i = 1; i < max; i++) {
		var tabname = 'tab' + i;
		var content = 'content' + i;
		//if (document.getElementById(tabname) != 'undefined') {
			if (i == focus) {
				//show(tabname);
				show(content);
				setCookie('tab',focus);
				//document.getElementById(tabname).style.borderColor="black";
				document.getElementById(tabname).className="tabmarkon";
			} else {
				//hide(tabname);
				hide(content);
				//document.getElementById(tabname).style.borderColor="white";
				document.getElementById(tabname).className="tabmark";
			}
		//}
	}
}

function ikit_tabs(tabprefix,focus,max) {
	for (var i = 1; i < max; i++) {
		var tabname = tabprefix + i;
		var content = tabprefix+'content' + i;
		
		//if (document.getElementById(tabname) != 'undefined') {
			if (i == focus) {
				//show(tabname);
				show(content);
				setCookie(tabprefix,focus);
				//document.getElementById(tabname).style.borderColor="black";
				document.getElementById(tabname).className="tabmarkon";
			} else {
				//hide(tabname);
				hide(content);
				//document.getElementById(tabname).style.borderColor="white";
				document.getElementById(tabname).className="tabmark";
			}
		//}
	}
}


// From
// http://www.webmasterworld.com/forum91/296.htm				
function select(idname) {
	if(document.getElementById) { 
		document.getElementById(idname).className="tablink2on"; 
	} 
		
	else if(document.all) { 
		document.all[idname].className="tablink2on"; 
	} 
}
function unselect(idname) {
	if(document.getElementById) { 
		document.getElementById(idname).className="tablink2"; 
	} 
	else if(document.all) { 
		document.all[idname].className="tablink2";
	} 
}

function showzone(foo) {
	show(foo+"-zone");
	select(foo+"-tab");
}
function hidezone(foo) {
	hide(foo+"-zone");
	unselect(foo+"-tab");
}

function setfoldericonstate(foo) {
	pic = new Image();
	if (getCookie(foo) == "o")
 	{
		pic.src = "img/icons/ofo.gif";
	} else {
		pic.src = "img/icons/fo.gif";
	}
	document.getElementsByName(foo+'icn')[0].src = pic.src;
}
function HS_setArrowState(foo) {
	if (typeof(document.getElementById(foo).parentNode.getElementsByTagName("a")[0].getElementsByTagName("img")[0]) != "undefined") {
	pic = new Image();
	if (getCookie(foo, "menu", "o") == "o")
 	{
		pic.src = "/images/t-o.gif";
	} else {
		pic.src = "/images/t-c.gif";
	}
	document.getElementById(foo).parentNode.getElementsByTagName("a")[0].getElementsByTagName("img")[0].src = pic.src;
	}
}

function setfolderstate(foo) {
	if (getCookie(foo) == "o")
 	{
		show(foo);
	} else {
		hide(foo);
	}
	setfoldericonstate(foo);
}

function icntoggle(foo) {
  if( document.getElementById(foo).style.display == "none") {
    show(foo);
    setCookie(foo, "o");
  } else {
    hide(foo);
    setCookie(foo, "c");
  }
  setfoldericonstate(foo);
}

//
// set folder icon state during page load
//
function setFolderIcons() { 
  var elements      = document.forms[the_form].elements[elements_name];
  var elements_cnt  = (typeof(elements.length) != 'undefined') ? elements.length : 0;
  if (elements_cnt) {
    for (var i = 0; i < elements_cnt; i++) {
      elements[i].checked = document.forms[the_form].elements[switcher_name].checked;
    } 
  } else {
    elements.checked        = document.forms[the_form].elements[switcher_name].checked;;
  } // end if... else
  return true;
} // setFolderIcons()

// Initialize a cross-browser XMLHttpRequest object.
// The object return has to be sent using send(). More parameters can be
// given.
// callback - The function that will be called when the response arrives
//		First parameter will be the status 
//		(HTTP Response Code [200,403, 404, ...])
// method - GET or POST
// url - The URL to open
function getHttpRequest( method, url )
{
	var request;

	if( window.XMLHttpRequest )
		request = new XMLHttpRequest();
	else if( window.ActiveXObject )
	{
		try
		{
			request = new ActiveXObject( "Microsoft.XMLHTTP" );
		}
		catch( ex )
		{
			request = new ActiveXObject("MSXML2.XMLHTTP");
		}
	}
	else
		return false;

	if( !request )
		return false;

	request.open( method, url, false );

	return request;
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setSessionVar(name,value) {
	var request = getHttpRequest( "GET", "tiki-cookie-jar.php?" + name + "=" + escape(value));
	request.send('');
	tiki_cookie_jar[name] = value;
}

function setCookie(name, value, section, expires, path, domain, secure) {
    if (!expires) {
        expires = new Date();
        expires.setFullYear(expires.getFullYear() + 1);
    }
	if (feature_no_cookie == 'y') {
		var request = getHttpRequest( "GET", "tiki-cookie-jar.php?" + name + "=" + escape( value ) )
		try {
			request.send('');
			//alert("XMLHTTP/set"+request.readyState+request.responseText);
			tiki_cookie_jar[name] = value;
			return true;
		}
		catch( ex )	{
			setCookieBrowser(name, value, section, expires, path, domain, secure);
			return false;
		}
	}
	else {
		setCookieBrowser(name, value, section, expires, path, domain, secure);
		return true;
	}
}
function setCookieBrowser(name, value, section, expires, path, domain, secure) {
	if (section) {
		valSection = getCookie(section);
		name2 = "@" + name + ":";
		if (valSection) {
			if (new RegExp(name2).test(valSection))
				valSection  = valSection.replace(new RegExp(name2 + "[^@;]*"), name2 + value);
			else
				valSection = valSection + name2 + value;
			setCookieBrowser(section, valSection, null, expires, path, domain, secure);
		}
		else {
			valSection = name2+value;
			setCookieBrowser(section, valSection, null, expires, path, domain, secure);
		}
		
	}
	else {
		var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "")
			+ ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
		document.cookie = curCookie;
	}
}

// name - name of the desired cookie
// section - name of group of cookies or null
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name, section, defval) {
	if( feature_no_cookie == 'y' && (window.XMLHttpRequest || window.ActiveXObject) && typeof tiki_cookie_jar != "undefined" && tiki_cookie_jar.length > 0) {
		if (typeof tiki_cookie_jar[name] == "undefined")
			return defval;
		return tiki_cookie_jar[name];
	}
	else {
		return getCookieBrowser(name, section, defval);
	}
}
function getCookieBrowser(name, section, defval) {
	if (section) {
		var valSection = getCookieBrowser(section);
		if (valSection) {
			var name2 = "@"+name+":";
			var val = valSection.match(new RegExp(name2 + "([^@;]*)"));
			if (val)
				return unescape(val[1]);
			else
				return null;
		} else {
			return defval;
		}
	} else {
		var dc = document.cookie;

		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);

		if (begin == -1) {
			begin = dc.indexOf(prefix);

			if (begin != 0)
				return null;

		} else begin += 2;

		var end = document.cookie.indexOf(";", begin);

		if (end == -1)
			end = dc.length;

		return unescape(dc.substring(begin + prefix.length, end));
	}
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, section, expires, path, domain, secure) {
	if (section) {
		valSection = getCookieBrowser(section);
		name2 = "@" + name + ":";
		if (valSection) {
			if (new RegExp(name2).test(valSection)) {
				valSection  = valSection.replace(new RegExp(name2 + "[^@;]*"), "");
				setCookieBrowser(section, valSection, null, expires, path, domain, secure);
			}
		}
	}
	else {

//	if( !setCookie( name, '', 0, path, domain ) ) {
//		if (getCookie(name)) {
			document.cookie = name + "="
				+ ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
//		}
	}
}


// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

//
// Expand/collapse lists
//
function flipWithSign(foo) {
 if( document.getElementById(foo).style.display == "none") {
  show(foo);
  collapseSign("flipper"+foo);
  setCookie(foo,"o");
 } else {
  hide(foo);
  expandSign("flipper"+foo);
  setCookie(foo,"c");
 }
}

// set the state of a flipped entry after page reload
function setFlipWithSign(foo)
{
  if (getCookie(foo) == "o") {
    collapseSign("flipper"+foo);
    show(foo);
  }
  else {
    expandSign("flipper"+foo);
    hide(foo);
  }
}

function expandSign(foo) {
 document.getElementById(foo).firstChild.nodeValue="[+]";
}

function collapseSign(foo) {
 document.getElementById(foo).firstChild.nodeValue="[-]";
} // flipWithSign()

//
// Check / Uncheck all Checkboxes
//
function switchCheckboxes(tform, elements_name, state) {
	// checkboxes need to have the same name elements_name
	// e.g. <input type="checkbox" name="my_ename[]">, will arrive as Array in php.
	for (var i = 0; i < tform.length; i++) {
		if (tform.elements[i].name == elements_name) {
			tform.elements[i].checked = state
		}
	}
	return true;
}  

//
// Set client offset (in minutes) to a cookie to avoid server-side DST issues
// Added 7/25/03 by Jeremy Jongsma (jjongsma@tickchat.com)
//
var expires = new Date();
var offset = -(expires.getTimezoneOffset() * 60);
expires.setFullYear(expires.getFullYear() + 1);
setCookie("tz_offset", offset, null, expires, "/");


// function added for use in navigation dropdown
// example :
// <select name="anything" onchange="go(this);">
// <option value="http://tikiwiki.org">tikiwiki.org</option>
// </select>
function go(o){
	if (o.options[o.selectedIndex].value != "") {
		location = o.options[o.selectedIndex].value;
		o.options[o.selectedIndex] = 1;
	}
	return false;
}



function hs_highlightVerticalMenuItem(menuclassname) {
	var sublinks = document.getElementsByClassName(menuclassname);
	for(var i = 0, len = sublinks.length; i < len; i++) {		
		currentPath = window.location.pathname.substring(1);
//		if (sublinks[i].href == window.location.href) {
		if (window.location.href.indexOf(sublinks[i].href) >= 0) {
			sublinks[i].className = menuclassname+"on";
			menuid = sublinks[i].parentNode.id;
			if (menuid) {
				show(menuid);
				setCookie(menuid, "o");
				HS_setArrowState(menuid);
			}
		} else {
			sublinks[i].className = menuclassname;
		}	
	}
}

function hs_highlightHorizontalMenuItem(menuclassname) {
	var sublinksoff = document.getElementsByClassName(menuclassname);
	var sublinkson = document.getElementsByClassName(menuclassname+"on");
	var sublinks = sublinksoff.concat(sublinkson);
	// Looop thru every tab, on or off
	for(var i = 0, len = sublinks.length; i < len; i++) {		
//		currentPath = window.location.pathname.substring(1);
//		if (sublinks[i].href == window.location.href) {
		if (window.location.href.indexOf(sublinks[i].href) >= 0) {
			// We've found a matching tab to the window url ...
			// so turn all the 'on' tabs off first ...
			for(var j = 0, len2 = sublinkson.length; j < len2; j++) {
				sublinkson[j].className = menuclassname;
				setCookie(sublinkson[j].id, "c");
			//	alert(menuclassname+": turning off: "+ sublinkson[j].href+" windowhref: "+window.location.href )
			}
			// Now turn on the tab
			sublinks[i].className = menuclassname+"on";
			//alert(menuclassname+": turning on: "+ sublinks[i].href+" windowhref: "+window.location.href )
			setCookie(sublinks[i].id, "o");
			
		} else {
			// Disbal ethis otherwise all tabs will be turned off when
			// we visit a page that uses the tab but does not match
			// any url in the tabs. eg. tiki-browse_image.php
			//sublinks[i].className = menuclassname;
			//setCookie(sublinks[i].id, "c");
		}	
	}
}

function hs_highlightMenuItem(menuclassname) {
	var menus = document.getElementsByClassName(menuclassname);
	for(var i = 0, len = menus.length; i < len; i++) {
	//alert(menus[i].id);
		var links = menus[i].getElementsByTagName("a"); // all links
		var linkon_index = -1;
		for(var j = 0, len2 = links.length; j < len2; j++) {
		
			if (window.location.href.indexOf(links[j].href) >= 0) {
			//alert(menuclassname+": turning on: "+ links[j].href+" windowhref: "+window.location.href );
				links[j].className = "on";
				links[j].parentNode.className = "active"; // makes the parent <li> active
				
				// Check for sub-menus with ids of format 4.1. The 4 is the parent id.
				// Note the array size -1 as the last item is the unqie identifier.
				// Supports any number of nesting levels.
				// Note: must be using unordered lists.
				ids = links[j].id.split(".");
				currentId = ''; // Going to concatenate down the hierarchy
				for (var k=0, len3=ids.length;k<len3-1;k++) {
					if (currentId =='') {
						currentId = ids[k]; // First level
					} else {
						currentId = currentId +'.'+ ids[k];  // Subsequent levels.
					}
					//alert("parent menu id:"+ currentId);
					document.getElementById(currentId).className = "on";
					//alert("parent:"+document.getElementById(currentId).parentNode.nodeName);
					document.getElementById(currentId).parentNode.className = "active"; // makes the parent <li> active
					document.getElementById(currentId).parentNode.getElementsByTagName("ul")[0].style.display = "block"; //Makes the child <ul> visible
				}
				
				//setCookie(menus[i].id, links[j].id);
				linkon_index = j;
			}
		}
		if (linkon_index == -1) {		
			previousOnMenuItem = getCookie(menus[i].id) ;  //id
			//alert("no url match, trying cookie.."+previousOnMenuItem);
			for(var j = 0, len2 = links.length; j < len2; j++) {
				if (previousOnMenuItem == links[j].id) {
				//alert("Cookie:"+menuclassname+": turning on: "+ links[j].href+" windowhref: "+window.location.href );
					links[j].className = "on";
				}
			}		
		} // end if
	}//end for
}

// Progress box
var isDom = document.getElementById?true:false;
var isIE  = document.all?true:false;
var isNS4 = document.layers?true:false;
var cellCount = 10;

function setprogress(pIdent, pValue, pString, pDeterminate)
{
        if (isDom)
            prog = document.getElementById(pIdent+'installationProgress');
        if (isIE)
            prog = document.all[pIdent+'installationProgress'];
        if (isNS4)
            prog = document.layers[pIdent+'installationProgress'];
	if (prog != null) 
	    prog.innerHTML = pString;

        if (pValue == pDeterminate) {
	    for (i=0; i < cellCount; i++) {
                showCell(i, pIdent, "hidden");	
            }
        }
        if ((pDeterminate > 0) && (pValue > 0)) {
            i = (pValue-1) % cellCount;
            showCell(i, pIdent, "visible");	
        } else {
            for (i=pValue-1; i >=0; i--) {
                showCell(i, pIdent, "visible");	
            }
	}
}

function showCell(pCell, pIdent, pVisibility)
{
	if (isDom)
	    document.getElementById(pIdent+'progressCell'+pCell+'A').style.visibility = pVisibility;
	if (isIE)
	    document.all[pIdent+'progressCell'+pCell+'A'].style.visibility = pVisibility;
	if (isNS4)
	    document.layers[pIdent+'progressCell'+pCell+'A'].style.visibility = pVisibility;

}

function hideProgressBox() {
	var progressBox = document.getElementById("progressBox");
	progressBox.style.display = "none";
}
// End of progress bar
function detect() {
	// simplify things
	var agent 	= navigator.userAgent.toLowerCase();
	
	// detect platform
	this.isMac		= (agent.indexOf('mac') != -1);
	this.isWin		= (agent.indexOf('win') != -1);
	this.isWin2k	= (this.isWin && (
			agent.indexOf('nt 5') != -1));
	this.isWinSP2	= (this.isWin && (
			agent.indexOf('xp') != -1 || 
			agent.indexOf('sv1') != -1));
	this.isOther	= (
			agent.indexOf('unix') != -1 || 
			agent.indexOf('sunos') != -1 || 
			agent.indexOf('bsd') != -1 ||
			agent.indexOf('x11') != -1 || 
			agent.indexOf('linux') != -1);
	
	// detect browser
	this.isSafari	= (agent.indexOf('safari') != -1);
	this.isSafari2 = (this.isSafari && (parseFloat(agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).substring(0,agent.substring(agent.indexOf("applewebkit/")+"applewebkit/".length,agent.length).indexOf(' '))) >=  300));
	this.isOpera	= (agent.indexOf('opera') != -1);
	this.isNN		= (agent.indexOf('netscape') != -1);
	this.isIE		= (agent.indexOf('msie') != -1);
	
	// itunes compabibility
	this.isiTunesOK	= this.isMac || this.isWin2k;
}
var browser = new detect();


/*
window.onload = function() {
	hs_highlightMenuItem("itab");
	hs_highlightMenuItem("menu_vertical");
//	hideProgressBox();
}
*/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  /* more code to run on page load */ 
  hs_highlightMenuItem("itab");
//	hs_highlightMenuItem("menu_vertical");
});