// $Header: /hsphere/local/home/hszeto/cvsrep/avire/lib/ikit-js.js,v 1.2 2006/11/03 23:06:40 hszeto Exp $

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) {
    document.getElementById(foo).style.display="block";
}

function hide(foo) {
    document.getElementById(foo).style.display="none";
}

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);
    setCookie(foo, "o");
    HS_setArrowState(foo);
  } else {
  	if( document.getElementById(foo).style.display == "block") {
	    hide(foo);
	    setCookie(foo, "c");
	    HS_setArrowState(foo);
	} else {
	    show(foo);
	    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) {
	pic = new Image();
	if (getCookie(foo) == "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()

// 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 setCookie(name, value, expires, path, domain, secure) {
  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
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  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, 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(the_form,elements_name,switcher_name) { 
  // checkboxes need to have the same name elements_name
  // e.g. <input type="checkbox" name="my_ename[]">, will arrive as Array in php.
  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;
} // switchCheckboxes()

//
// 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, 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");
});
