/* Main javascript functions for MegamagZone.com */

function ShowDiv(id) {
  var item = null;
  if (document.getElementById) {
		item = document.getElementById(id);
  } else if (document.all){
		item = document.all[id];
  } else if (document.layers){
		item = document.layers[id];
  }
  if (item) {
		if (item.style.display == "none") {
			item.style.display = "";
			item.visibility = "show";
		}	else {
			item.style.display = "none";
			item.visibility = "hide";
		}
 	}
}

function ShowOrHideCat(idtrig,idstr) {
  var a = new Array;
  var id;
  var item;
  var isdisp = "0";
  var itemtrig = document.getElementById(idtrig);
  id = idstr;
  name = idstr.substr(0,3);
  if (name == "cat") catid = idstr.substr(3,idstr.length);
  item = null;
  if (document.getElementById) {
    item = document.getElementById(id);
  } else if (document.all) {
    item = document.all[id];
  }
  if (item) {
    if (item.style.display == "none"){ 
      item.style.display = "";
      itemtrig.src = templatepath + "/images/minus.gif";
      isdisp = "1";
    } else {
      item.style.display = "none";
      itemtrig.src = templatepath + "/images/plus.gif";
    }
    setMenuCookie(catid,isdisp);
  }
}

function ShowOrHideAll(showit) {
  var idx, idstr, idimg;
  var item, itemimg;
  var maxnum = 13;
  for (var i=0; i<maxnum; i++) {
    idx = i;
    if (i<10) idx = "0"+i;
    idstr = "cat"+idx;
    idimg = "trig"+idx;
    item = null;
    itemimg = null;
    if (document.getElementById) {
      item = document.getElementById(idstr);
      if (item) {
        if (showit){ item.style.display = ""; isdisp = "1"; }
        else {item.style.display = "none"; isdisp = "0"; }
				setMenuCookie(i,isdisp);
      }
      itemimg = document.getElementById(idimg);
      if (itemimg) {
        if (showit) {
          itemimg.src = templatepath + "/images/minus.gif";
        } else {
          itemimg.src = templatepath + "/images/plus.gif";
        }
      }
    }
  }
}

function selectAll(id) {
  var item = null;
  if (document.getElementById) {
    item = document.getElementById(id);
  } else if (document.all) {
    item = document.all[id];
  } else if (document.layers){
    item = document.layers[id];
  }
  if (!item) {
  }
  else if ((item.type == "text") || (item.type == "textarea")) {
    item.focus();
    item.select();
  }else{
  }
	return false;
}

function setMenuCookie( catid, isdisp ) {
  var a, mymenu;
	mymenu = Get_Cookie( "mainmenu" );
  if (!mymenu) {
  	mymenu = "0,0,0,0,0,0,0,0,0,0,0,0,0";
  }
  a=mymenu.split(",");
  catid = catid / 1;
  a[catid]=isdisp;
  mymenu = a.join(",");
	Set_Cookie( "mainmenu", mymenu, 5, "/" );
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Below expires will make the number of days the cookie lasts.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Load_Cookie() {
  var a, mymenu;
  var item, itemimg;
	mymenu = Get_Cookie( "mainmenu" );
  if (!mymenu) {
  	mymenu = "0,0,0,0,0,0,0,0,0,0,0,0,0";
  }

  a = mymenu.split(",");
  for (var i=0; i<a.length; i++) {
    isopen = a[i];
    idx = i;
    if (i<10) idx = "0"+i;
    idstr = "cat"+idx;
    idimg = "trig"+idx;
    item = null;
    itemimg = null;
    if (document.getElementById) {
      item = document.getElementById(idstr);
      itemimg = document.getElementById(idimg);
      if (item) {
        if (isopen == "1"){ 
          item.style.display = "";
          itemimg.src = templatepath + "/images/minus.gif";
        } else {
          item.style.display = "none";
          itemimg.src = templatepath + "/images/plus.gif";
        }
      }
    }
  }
}

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

addLoadEvent(Load_Cookie);
