// *******************************************
// * Javascript functions include file       *
// * for www.chain-metal.nl (Sept. 8, 2005)  *
// * (C) Symen Timmermans                    *
// *******************************************

// ======= COOKIE FUNCTIONS =======

// create a cookie on the client's computer
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

// read a cookie from the client's computer
function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// var for XMLHTTP object
var xmlhttp=false;

// cross-browser XMLHTTP object instantiator
function getXMLHTTP() {
    var xmlHTTPtemp = null;
    try {
        xmlHTTPtemp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHTTPtemp = new ActiveXObject("Msxml2.XMLHTTP")
        } catch(e) {
            var success = false;
            var MSXML_XMLHTTP_PROGIDS = new Array(
                'Microsoft.XMLHTTP',
                'MSXML2.XMLHTTP',
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0'
            );
            for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
                try {
                    xmlHTTPtemp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                    success = true;
                } catch (e) {
                    xmlHTTPtemp = null;
                }
            }
        }
    } 
    return xmlHTTPtemp;
}

// Prepare the XMLHTTP object.
xmlhttp = getXMLHTTP();

// Set default element to contain XMLHTTP response
responseElement = '';

// Request change processor for a named div element.
function processReqChange() {
    // only if req shows "loaded"
    if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
                contentdiv = new getObj(responseElement);
                contentdiv.obj.innerHTML = xmlhttp.responseText;
        } else {
            alert("There was a problem retrieving the data:\n" + xmlhttp.statusText);
        }
    }
    //alert('RS: ' + xmlhttp.readyState + '\nSt: ' + xmlhttp.status + '\nStT: ' + xmlhttp.statusText);
}

// cross-browser object getter
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

// Retrieves a php page and shows it in the content DIV
function retrievePage(page)
{
	if (!xmlhttp) { xmlhttp = getXMLHTTP(); }
	page = page + '.php?date='+escape(new Date()); //paste the date to prevent getting the cached page
	responseElement = 'content';
	xmlhttp.open("GET", page, true);
    xmlhttp.onreadystatechange = processReqChange;
    xmlhttp.send(null);
}

// Retrieves a php page and shows it in the content DIV - parameter version
function retrievePagePar(page, par)
{
	if (!xmlhttp) { xmlhttp = getXMLHTTP(); }
	page = page + '.php?date='+escape(new Date()) + '&' + par; //paste the date to prevent getting the cached page
	responseElement = 'content';
	xmlhttp.open("GET", page, true);
    xmlhttp.onreadystatechange = processReqChange;
    xmlhttp.send(null);
}

// Updates the menu with the new style
function menuStyle(style)
{
	//alert('changing menu style');
    var menuobj;
    var menuitems= new Array(
        'menubar_01',
        'menubar_02',
        'menubar_03',
        'menubar_04',
        'menubar_05',
        'menubar_06',
        'menubar_07',
        'menubar_08',
        'menubar_09',
        'menubar_10' );        
    for (var i=0; i < menuitems.length; i++)
    {
    	menuobj = new getObj(menuitems[i]);
    	menuobj.obj.src = "assets/"+style+"/"+menuitems[i]+".jpg";
    	menuobj.obj.onmouseover = function (e) { this.src = "assets/"+style+"/"+this.id+"_over_.jpg"; }
    	menuobj.obj.onmouseout = function (e) { this.src = "assets/"+style+"/"+this.id+".jpg"; }
    }
}

// To set the active stylesheet/theme
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if ((a.getAttribute("title") == title) || (a.getAttribute("title") == "general")) a.disabled = false;
     }
   }
   menuStyle(title);
}

// Select default theme because firefox will only load the general css file for layout
window.onload = function (e) {
    //setActiveStyleSheet("default");
    //retrievePage("news");
    //createCookie('test', 'blaat', 365);
    //alert(readCookie('test'));
}