/*
 * Expands and contracts sub-list items
 */

function toggleDisplay(list) {
	var element=document.getElementById(list);

	element.style.display=(element.style.display=='block')?'none':'block';
}

function subButtonPushed(e) {
	var img = document.getElementById(e);
	img.style.listStyleImage = "url(../images/sub_button_pushed.gif)";
}

function subButton(e) {
	var img = document.getElementById(e);
	img.style.listStyleImage = "url(../images/sub_button.gif)";
}

function buttonPushed(e) {
	var img = document.getElementById(e);
	img.style.listStyleImage = "url(../images/button_blk_pushed.gif)";
}

function button(e) {
	var img = document.getElementById(e);
	img.style.listStyleImage = "url(../images/button_blk.gif)";
}

/*
 * This causes the named image to be displayed in the element named id
 * and updates the style information
 */
function showImage(doc,filename,newStyle, newDescr, newSizes, newColors, newPrices) {

	showInfo(doc);
//	alert("filename: " + filename + "\nstyle: " + newStyle + "\ndescr: " + newDescr + "\nsizes: " + newSizes + "\ncolors: " + newColors + "\nprices: " + newPrices);

	var img = doc.getElementById('bigimage').setAttribute('src',filename);
	doc.getElementById('style').innerHTML = newStyle;
	doc.getElementById('description').innerHTML = newDescr;
	doc.getElementById('sizes').innerHTML = newSizes;
	doc.getElementById('colors').innerHTML = newColors;
	if(newPrices != "") { doc.getElementById('pricelist').innerHTML = newPrices; }

	return(true);
}

/*
function hideInfo(doc) {
	doc.getElementById('info').style.display = "none");
}
*/

function showInfo(doc) {
	var infoStyle = doc.getElementById('info').style;
	infoStyle.display = "block";
}

function setStripOrientation(orientation) {
	var strip = document.getElementById('strip');
	var bigimage = document.getElementById('bigimage');
	var blowup = document.getElementById('blowup');
	var info = document.getElementById('info');
	var instructions = document.getElementById('instructions');
	var filmstrip = document.getElementById('filmstrip');

	if(orientation == "portrait") {
		strip.setAttribute('height',480);
		strip.setAttribute('width',135);

		blowup.style['top'] = "0px";
		blowup.style['left'] = "160px";

		filmstrip.style['top'] = "0px";
		filmstrip.style['left'] = "0px";

		instructions.style['top'] = "500px";
		instructions.style['left'] = "0px";

		info.style['top'] = "5px";
		info.style['left'] = "525px";

		bigimage.setAttribute('height',480);
		bigimage.setAttribute('width',360);
/*
	} elsif(orientation == "search") {
		strip.setAttribute('width',640);
		strip.setAttribute('height',135);

		blowup.style['top'] = "0px";
		blowup.style['left'] = "0px";

		filmstrip.style['top'] = "520px";
		filmstrip.style['left'] = "0px";

		instructions.style['top'] = "500px";
		instructions.style['left'] = "0px";

		info.style['top'] = "5px";
		info.style['left'] = "650px";

		bigimage.setAttribute('height',480);
		bigimage.setAttribute('width',480);
*/
	} else {
		strip.setAttribute('width',480);
		strip.setAttribute('height',135);

		blowup.style['top'] = "0px";
		blowup.style['left'] = "0px";

		filmstrip.style['top'] = "380px";
		filmstrip.style['left'] = "0px";

		instructions.style['top'] = "515px";
		instructions.style['left'] = "0px";

		info.style['top'] = "5px";
		info.style['left'] = "490px";

		bigimage.setAttribute('height',360);
		bigimage.setAttribute('width',480);
	}
	return(false);
}

function checkLoggedinStatus() {
	var welcome = parent.frames["header"].document.getElementById('welcome');

	var authcookie = getCookie("auth");
	if(!authcookie) { welcome.innerHTML = "Not logged in | <a href=\"cgi-bin/login.cgi\" target=\"body\">Login</a>"; return; }

	var decauth = Base64.decode(authcookie);
	var auth = decauth.split(":");
	if(auth != "") {
		var userid = auth.shift();
		var password = auth.shift();
		var name = auth.shift();
		var lastlogin = auth.shift();

		welcome.innerHTML = "Welcome, " + name + "| <a href=\"cgi-bin/logout.cgi\" target=\"body\">Logout</a>";
	} else {
		welcome.innerHTML = "Not logged in | <a href=\"cgi-bin/login.cgi\" target=\"body\">Login</a>";
	}
}

function getCookie(name) {
	var allcookies = document.cookie;
	if(!allcookies) { return(""); }

	var pos = allcookies.indexOf(name + "=");

	if(pos != -1) {
		var start = pos + name.length + 1;
		var end = allcookies.indexOf(";", start);
		if(end == -1) end = allcookies.length;

		var value = allcookies.substring(start, end);
		value = unescape(value);

		return(value);
	}

	return("");
}

function expireAuth() {
	// Set the date to an hour in the past
	var expires = "; expires=Fri, 02-Jan-1970 00:00:00 GMT";

	var allcookies = document.cookie;

	document.cookie = allcookies + expires;

	checkLoggedinStatus();
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

/*
// Show or hide info and tooltip sections
function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "block";
}

function showElements(doc) {
//	var elements=document.getElementsById;
	var e = doc.getElementById('background');
	var f = e.getElementsByTagName('img');

	alert(f.length);

	var msg = "";
	for(var i=0;i<f.length;i++) {
		msg = msg + f[i].getAttribute('src') + "\n";
	}
	alert(msg);
}
*/
