/*
 * Writes a cookie named with the user's preferred language
 *
 * lang - two letter ISO 639 code of the language
 */
function setLangCookie(lang){
    // expires in six months
    var exp = new Date((new Date()).getTime() + 6 * 30 * 24 * 60 * 60 * 1000);
    var cookie = "language=" + escape(lang) + "; "+
	         "expires=" + exp.toGMTString() +"; "+
	         "path=/;";
    document.cookie = cookie;
}

/*
 * Reads the cookie and returns the ISO 639 code of the language
 */
function getLangCookie(){
    var language = null;
    var name = "language";
    var cookie = document.cookie;
    var langPos = cookie.indexOf(name);
    if(langPos >= 0){
	langPos += name.length+1;
	var end = cookie.indexOf(";", langPos);
        end = (end == -1) ? cookie.length : end;
	if(end - langPos >= 2){
	    language = unescape(cookie.substring(langPos, end));
	    if(language.length != 2){
		language = null;
	    }
	}
    }
    return language;
}

/*
 * Deletes the cookie.
 */
function deleteLangCookie(){
    var cookie = "language=; expires=" + (new Date(0)).toGMTString() + "; "+
	         "path=/;";
    document.cookie = cookie;
}

/*
 * Reads the cookie and redirects the user to the pages with that language.
 */
function redirect(){
    lang = getLangCookie();
    if(lang != null){
	location=lang+"/index.html";
    }
}

/*
 * Deletes the cookie and redirects the user to the language selection page.
 */
function redirectLangSel(){
    deleteLangCookie();
    location="http://ganzua.sourceforge.net/index.html";
}
