var istarcookie = "PISTARPREF";
var lang = "", svc = "";

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

function get_svc_lang() {
	var istarpref = getCookie(istarcookie);
	if (istarpref == null)
		return false;

	var pairs = istarpref.split(":");
	
	for (i = 0; i < pairs.length; i++) {
		nv = pairs[i].split("=");
		if (nv.length != 2)
			continue;
		if (nv[0] == "svc")
			svc = nv[1];
		else if (nv[0] == "lang")
			lang = nv[1];
	}
	
	//alert("svc=<" + svc + ">,lang=<" + lang + ">");
	return true;
}

function do_restore(obj) {
	get_svc_lang();
	for (i=0; i<mainform.lang.length; i++) {		
		if (mainform.lang[i].value == lang) {
			mainform.lang[i].checked = true;
			break;
		}
	}	
	for (i=0; i<obj.length; i++) {
		if (obj[i].value == svc) {
			obj[i].checked = true;
			break;
		}
	}
}

function do_save(curpath, svc, lang) {
/*	var lang, svc;
	for (i=0; i<mainform.lang.length; i++) {
		if (mainform.lang[i].checked) {
			lang = mainform.lang[i].value;
			break;
		}
	}
	
	for (i=0; i<obj.length; i++) {
		if (obj[i].checked) {
			svc = obj[i].value;
			break;
		}
	}*/
	
	var cookie = istarcookie + "=";
	if (lang != null) {
		cookie += ":lang=" + lang;
	}
	if (svc != null) {
		cookie += ":svc=" + svc;
	}

	path = location.pathname.substring(0, location.pathname.indexOf(curpath) + 1);
	document.cookie = cookie + "; path=" + path + "; expires=Mon, 31 Dec 2030 23:59:59 UTC";
	//document.cookie = cookie + "; path=/" + "; expires=Mon, 31 Dec 2030 23:59:59 UTC";
	//path = "index.html"
	//location.replace(path);
}

function set_goback_path(){
	var args = location.pathname.split("/");
	var path, newcookie;

	path = args[args.length-1] + location.search;
	var oldCookie = document.cookie.split(":");

	if(oldCookie.length >= 3){
		//cookie has been set
		newcookie = oldCookie[0] + ":" + oldCookie[1] + ":" + oldCookie[2] + ":path=" + path;
	}else{
		//cookie has NOT been set
		//newcookie = istarcookie + "=:lang=:svc=:path=" + path;
		newcookie = istarcookie + "=:::path=" + path;
	}
	document.cookie = newcookie + "; expires=Mon, 31 Dec 2030 23:59:59 UTC";
}

function back(){
	try{
		var cookie = getCookie(istarcookie);
		var path_index = cookie.indexOf("path=");
		if(path_index != -1){
			var path = cookie.substr(path_index + 5);
			location.replace(path);
		}
	}catch(e){}
}

