// Copyright (c) 1996-1997 Tomer Shiran. All rights reserved.
// Permission given to use the script provided that this notice remains as is.
// Additional scripts can be found at http://www.geocities.com/~yehuda/

// Boolean variable specified if alert should be displayed if cookie exceeds 4KB
var caution = false

// 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" : "")
        if (!caution || (name + "=" + escape(value)).length <= 15000)
                document.cookie = curCookie
        else
                if (confirm("Cookie exceeds 4KB and will be cut!"))
                        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 prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// 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)) {
                
				thedate=Date();
				thedate1=thedate.split(" ");
				
				document.cookie = name + "=" + 
                ((path) ? "; path=" + path : "") +
                ((domain) ? "; domain=" + domain : "") +
                "; expires="+thedate1[0]+", "+thedate1[2]+"-"+thedate1[1]+"-"+thedate1[4]+" 23:59:59 GMT"
        }
}

// date - any instance of the Date object
// * you should hand all instances of the Date object to this function for "repairs"
// * this function is taken from Chapter 14, "Time and Date in JavaScript", in "Learn Advanced JavaScript Programming"
function fixDate(date) {
        var base = new Date(0)
        var skew = base.getTime()
        if (skew > 0)
                date.setTime(date.getTime() - skew)
}

// constructor function to create an entry (parent or child)
function item(parent, text, depth) {
        this.parent = parent // is this item a parent?
        this.text = text // text for link (may include HTML)
        this.depth = depth // nested depth
		}

// constructor function to create array (compatible with all browsers)
function makeArray(length) {
        this.length = length // length of array (integer)
}


function setStates(namecookie) {
        // assign current cookie to local variable
        var storedValue = getCookie(namecookie)
        
        // if desired cookie not found (null)
        if (!storedValue) {
                // set states to default if no cookie found
                for (var i = 0; i < outline.length; ++i) {
                        // only topmost level is visible by default
                        if (outline[i].depth == 0)
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        } else {
                // extract current states from cookie (0 => false, 1 => true)
                for (var i = 0; i < outline.length; ++i) {
                        if (storedValue.charAt(i) == '1')
                                outline[i].state = true
                        else
                                outline[i].state = false
                }
        }
}

function setImages() {
        // loop through all elements of the outline "array" (object)
        for (var i = 0; i < outline.length; ++i) {
                //if (outline[i].state)
                        //if (outline[i].parent) // outline[i] is a parent
                                if (outline[i + 1])
									if (outline[i + 1].state) // outline[i] is exploded
											if(outline[i].depth<outline[i+1].depth){
												outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="img/arrow_menu.gif" BORDER=0></A>'
												}
											else{
												outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="img/punto_menu.gif" BORDER=0></A>'
												}
									else // outline[i] is collapsed
											outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="img/arrow_menu.gif" BORDER=0></A>'
                        		else outline[i].pic = '<A HREF="javascript:toggle(' + i + ')"><IMG SRC="img/punto_menu.gif" BORDER=0></A>'
						//else // outline[i] is only a child (not a parent)
                                //outline[i].pic = '<IMG SRC="img/punto_menu.gif" BORDER=0>'
        }
}

// change from expanded to collapsed and vice versa
function toggle(num,namecookie) {
        // loop starts at item following argument
        // terminate loop when:
        //   a) last element of outline "array" reached
        //   b) current item (outline[i]) is not deeper than toggled item (outline[num])
        for (var i = num + 1; i < outline.length && outline[i].depth >= outline[num].depth + 1; ++i) {
                // if current item (outline[i]) is a direct child of outline[num]
                if (outline[i].depth == outline[num].depth + 1)
                        outline[i].state = !outline[i].state // toggle state
        }

        // store new states in cookie
        setStorage(namecookie)

        // reload page
        history.go(0)
}

function setStorage(namecookie) {
        // initialize local variable to empty string
        var text = ""

        // loop through all properties of outline "array"
        for (var i = 0; i < outline.length; ++i) {
                // use "1" character to represent true state, and "0" for false state
                text += (outline[i].state) ? "1" : "0"
        }

        // create cookie named "outline" with "binary" string
        setCookie(namecookie, text)
}

// update database
//makeDatabase()



// use <PRE> to enable indentation via spaces
function writemenu(){	
	document.write('<table width="158" border="0" cellspacing="0" cellpadding="0" bgcolor="#6785A7">\n<tr><td height="8" colspan="3" width=""></td></tr>')
	
	// loop through elements of outline "array"
	for (var i = 0; i < outline.length; ++i) {
		// if current item's state is true
		if (outline[i].state) {
			//---------------
			bg='';
			//bg='bgcolor="#587598"';
			if (outline[i + 1])
				if (outline[i + 1].state) // outline[i] is exploded
					if(outline[i].depth<outline[i+1].depth){
						bg='#587598';
						}
					else{
						bg='';
						}
				else // outline[i] is collapsed
					bg='#587598';
            else bg='';
			//---------------
			spazio='';
			dim=0;
			if(outline[i].depth==0){
				dim=3;
				}
			else{
				dim=10*outline[i].depth;
				}
			//---------------
			color=' onMouseOver="this.bgColor=\'#2E6099\'" onMouseOut="this.bgColor=\''+bg+'\'"';
			
			document.write('<tr><td>\n');
			document.write('<table width="158" border="0" cellspacing="0" cellpadding="0" bgcolor="'+bg+'" '+color+'>\n');
			document.write('<tr>\n<td width="'+dim+'"></td>\n<td height="25" width="15" align="center">'+outline[i].pic+'</td>\n<td>'+outline[i].text+'</td>\n</tr>\n');
			document.write('</table>\n');
			document.write('</td></tr>\n');
			}
		else{
			// current item's state is false (skip all its children)
			var previous = i
			for (var k = i + 1; k < outline.length && outline[k].depth >= outline[previous].depth; ++k) {
				++i
				}
			}
		}
	
	// end <PRE> to return to normal formatting
	document.write('</Table>')
	}
									
// -->