var prod_menus = [
  ['s4'],
  ['s6'],
  ['s10']
];
var value;

function init () {

    if (document.cookie) {
        // set menu state from cookie
        var allcookies = document.cookie;
        var pos = allcookies.indexOf("pstates=");

        // If we find a cookie by that name, extract and use its value
        if (pos != -1) {
            var start = pos + 8;
            var end = allcookies.indexOf(";", start);
            if (end == -1) end = allcookies.length;
            value = allcookies.substring(start, end);
            value = unescape(value);
            //ok alert(value);
        }
    }

    // split the string on the '|' char
    if (value) {
        //alert(value);
        var in_states = value.split("|");
        for (var i=0; i<in_states.length-1; i++) {
           prod_menus[i][1] = in_states[i];
        }
    } else {
        for (i=0; i<prod_menus.length; i++) {
            prod_menus[i][1] = 0;
        }
    }
        buildMenu();

    /*if (added) {
        var elm = document.getElementById('added');
        elm.style.display = "block";
        elm.style.zIndex = "100";
    }*/
}

function buildMenu () {
    for (var i=0; i<prod_menus.length; i++) {
        //ok alert(prod_menus[i][0]+prod_menus[i][1]);
        if(prod_menus[i][0] != undefined) {
            if (prod_menus[i][1] == 0) {
                //alert(prod_menus[i][0]+' yes');
                document.getElementById(prod_menus[i][0]).style.display = "none";
            } else {
                //alert(prod_menus[index][0]+' no');
                document.getElementById(prod_menus[i][0]).style.display = "block";
            }
        }
    }
}

function toggleMenu (index) {
    // ok alert(prod_menus[index][0]+prod_menus[index][1]);
    if (prod_menus[index][1] == 0) {
        document.getElementById(prod_menus[index][0]).style.display = "block";
        prod_menus[index][1] = 1;
/*        for (var i=0; i<prod_menus.length; i++) {
            if (i != index) {
                document.getElementById(prod_menus[i][0]).style.display = "none";
                prod_menus[i][1] = 0;
            }
        }
*/        preserveMenuState();
    } else {
        document.getElementById(prod_menus[index][0]).style.display = "none";
        prod_menus[index][1] = 0;
        preserveMenuState();
    }
}

function preserveMenuState () {
    var pms_states = '';
    for (var i=0; i<prod_menus.length; i++) {
        pms_states += prod_menus[i][1]+"|";
    }
    document.cookie = "pstates=" + pms_states + "; path=/";
}

function highlightLink (elm) {
    elm.style.fontWeight = 'bold';
}

function closeSubLinks () {
    for (var i=0; i<prod_menus.length; i++) {
        document.getElementById(prod_menus[i][0]).style.display = "none";
        prod_menus[i][1] = 0;
    }
    preserveMenuState();
}

var cur_letter = 'intro';

function showGlossarySection (letter) {
    var y = 'l'+cur_letter;
    document.getElementById(y).style.color = '#999999';
    var x = 'l'+letter;
    document.getElementById(x).style.color = '#000000';
    document.getElementById(cur_letter).style.display = 'none'
    document.getElementById(letter).style.display = 'block'
    cur_letter = letter;
}


function popWin (url, name, h, w, scroll){
  // open a new window with dimensions that of the image to be opened in it
     // format dimensions and window attributes
        dimensions = 'width='+h+',height='+w+',scrollbars='+scroll;
     // open window
        window.open(url, name, dimensions);
        return false;
}

function popWin2 (url, name, h, w, scroll, toolbar){
  // open a new window with dimensions that of the image to be opened in it
     // format dimensions and window attributes
        var attributes = 'width='+h+',height='+w+',scrollbars='+scroll+',toolbar='+toolbar;
     // open window
        window.open(url, name, attributes);
        return false;
}

/*function initGlossary () {
    // register events for the glossary alphabet
    var tabs = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
    if (IE || OP) {
        for (i=0; i<2; i++) {
            var x = tabs[i]+'letter';
            var elm = document.getElementById(x);
            elm.attachEvent('onclick', bringToTop);

        }
    } else if (NN || OTHER) {
        for (i=0; i<2; i++) {
            var x = tabs[i]+'letter';
            var elm = document.getElementById(x);
            elm.addEventListener('click', bringToTop, false);
        }
    }
}
*/
function bringToTop (event) {
    if (IE || OP) {
    alert(event.srcElement);
        document.getElementById(cur_letter).style.display = 'none';
        event.srcElement.style.display = 'block';
        cur_letter = event.srcElement;
    } else if (NN || OTHER) {
        document.getElementById(cur_letter).style.display = 'none';
        event.currentTarget.style.display = 'block';
        cur_letter = event.currentTarget;
    }

}


function makeVisible (elm) {
 // make an element visible
    document.getElementById(elm).style.visibility = 'visible';
}

function swapImages (cur_img, new_img) {
 // simple image swap
    if (document.images) {
        cur_img.src = new_img.src;
    }
}

function swapBg (elm, state, name) {
 // swap the background colour of an element and color of
 // the A tag within it
    if (state) {
        elm.style.backgroundColor = '#ce1803';
        if (navigator.appName == "Netscape") {
            document.getElementById(name).style.color = '#ffffff';
        } else {
            elm.children.tags('A')[0].style.color='#ffffff';
        }
    } else {
        elm.style.backgroundColor = '#ffffff';
        if (navigator.appName == "Netscape") {
                document.getElementById(name).style.color = '#ce1803';
        } else {
            elm.children.tags('A')[0].style.color='#ce1803';
        }
    }
}

function autoTab (cur,next) {
 // make the cursor jump to a new form field once the
 // max length of the previous one has been reached
 // CALLED BY: onKeyup="autoTab(this, document.my_form.my_next_field)"
                               maxlength="max_length"
    if (cur.value.length == cur.getAttribute("maxlength")) {
        next.focus();
    }
}

function todaysDate () {
 // RETURN: formatted date like - 21st November 2002
    var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
    today = new Date();
    var d = today.getDate();
    var m = today.getMonth();
    var y = today.getFullYear();
    return (d+" "+months[m]+" "+y)
}


function clearField (field, message) {
 // clear a textfield of 'message'
    if(field.value == message) {
        field.value = '';
    }
}

function resetField (field, message) {
 // insert 'message' into texfield if it is empty
    if(field.value == '') {
        field.value = message;
    }
}

function openWin (url2, name, width, height, scroll) {
 // open a window
    attributes = 'width='+width+',height='+height+',scrollbars='+scroll;
    window.open(url2, name, attributes);
    return false;
}

function captureRightClick () {
 // captures a right click anywhere on the document
 // if the right click ocurred over an image, it calls
 // stopImageSave
    if (document.images) {
        for(i=0; i < document.images.length; i++) {
            document.images[i].onmousedown = stopImageSave;
        }
    }
}

function stopImageSave (e) {
 // pops an alert with a message and prevents the default
 // action from ocurring
    var message;
    message = "The images on this page are copyrighted to\n";
    message += "Company\n\n";
    message += "If you wish to use them please contact\n";
    message += "email.\n\n";
    message += "Thankyou.";
    var event_type = 0;
    if (navigator.appName == "Netscape") {
        event_type = e.which;
    } else {
        event_type = event.button;
    }
    if (event_type == 2 || event_type == 3) {
        alert(message);
        return false;
    }
    return true;
}

function fillMiddleCheckboxes (elm,index) {
 // if two checkboxes are checked, then this function
 // checks any other checkboxes that appear between them
 // in the form
    if (elm.checked) {
        for (i=0; i<index; i++) {
            if (document.getElementById('price'+i).checked) {
                break;
            }
        }
    }
    if (i < index) {
        for (; i<index; i++) {
            document.getElementById('price'+i).checked = 'checked';
        }
    }
}

function popUpImage (src, title, h, w) {
 // open a new window with dimensions that of the image to be
 // opened in it

 // format dimensions and window attributes
    dimensions = 'width='+h+',height='+w+',scrollbars=no';

 // format filename and query string
    url2 = 'image.php?src='+src+'&title='+title;

 // open window
    window.open(url2, 'picWin', dimensions);

    return false;
}

function maintain_menu(element_1)
{
	if(element_1.style.display == 'block')
	{
		element_1.style.display = 'none';
	} else {
		element_1.style.display = 'block';
	}
}

function open_image(url, w, h){
  // open a new window with dimensions that of the image to be opened in it
     // format dimensions and window attributes
	h = h + 40;
	w = w + 40;

	var attributes = 'width='+w+',height='+h+',scrollbars=yes,toolbar=no';
	// open window
	window.open(url, 'image', attributes);
	return;
}