function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
    ((expiredays == null) ? "" : ";expires = " + exdate.toGMTString());
}

function getCookie(c_name) {
    if (document.cookie.length > 0) {
    c_start = document.cookie.indexOf(c_name + "=");
    if (c_start != -1) {
        c_start = c_start + c_name.length + 1;
        c_end = document.cookie.indexOf(";", c_start);
        if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    return "";
}

function getTheme() {
    theme = getCookie('rtgrid-theme');
    if (theme != null && theme != "") {
        return theme;
    } else {
        setCookie('rtgrid-theme', 'rtgrid', 365);
        return theme;
    }
}

function loadTheme() {
    var links = document.getElementsByTagName("link");
	var themeName = getTheme();
	links[0].href = "../css/" + themeName + "/" + themeName + ".css";
    /* Set the correct theme in theme selector. */
    links = document.getElementsByTagName("select");
    for (i = 0; i < links.length; i++) {
        if (links[i].name == "theme") {
            links[i].value = themeName;
        }
    }
}

function changeTheme(selection) {
    newTheme = selection.options[selection.options.selectedIndex].value;
    setCookie('rtgrid-theme', newTheme, 365);
    loadTheme();
}


/* This functions generates the theme selector and banner which will be
   displayed on the top of the pages. This will make inserting new
   themes and changing banner easier and less of a chore. Nice trick ;-)
   
   To insert a new theme, just add the name of the theme to the
   themeElements array. The theme files must reside in css directory.

   Written by: G. Yaikhom
*/
function insertThemeSelector() {
	/* Please change the following information. */
	var elementSeparator = "|";
	var themeElements = "rtgrid";

	/* Please leave the following untouched. */
    var themeArray = themeElements.split(elementSeparator);
	var banner = "<table class='banner' cellpadding='0' cellspacing='0'><tbody>" +
		"<tr><td class='theme-select'>Theme: " +
		"<select name='theme' onchange='changeTheme(this);'>";
    for (i = 0; i < themeArray.length; i++) {
		banner += "<option value='" + themeArray[i] + "'>" +
			themeArray[i] + "</option>";
	}
	banner += "</select><br><br><br><br></td></tr>" +
		"<tr class='banner'><td class='banner'></td></tr></tbody></table>";
    document.write(banner);
}

