﻿function GetCookieVal(offset)
//获得Cookie解码后的值
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function SetCookie(name, value)
//设定Cookie值
{
var expdate = new Date();
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ))
document.cookie = name + "=" + escape (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
+((secure == true) ? "; secure" : "");
}
function DelCookie(name)
//删除Cookie
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
function GetCookie(name)
//获得Cookie的原始值
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return GetCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function clic(id,n) {
	wc=GetCookie('wc')
	if(wc!=id) {
		if(confirm('把该城市作为您的默认城市吗？\n设为默认城市后,该城市天气即在首页显示\n\n点确定定制该城市天气,点取消浏览该城市天气')) { addc(id,n) }
		else { whis(id)}
		return true
	} else { whis(id)}

}
function addc(id,n) {
	whis(id)
	DelCookie('wc')
	SetCookie('wc',id,5184000,'/',top_domain())
	SetCookie('wc_n',n,5184000,'/')
	alert("您选择的城市已经定制成功!\n下次访问,该城市天气即在首页显示.")
}
function top_domain() {
	var domain
	var tmp=location.host.split('.')
	var len=tmp.length
	if(len>2 && len!=4)
	domain = '.'+tmp[len-2]+'.'+tmp[len-1]
	else domain=location.host
	return domain
}
function show_city() {
	wsc=GetCookie('wc')
	wc_name=GetCookie('wc_n')
	if(wc_name) {
		document.write('<td valign="bottom"><img src="/img/right.gif" alt="">当前默认城市:<a href="/city/'+wsc+'.htm">'+wc_name+'</a> <a href="/" onclick="DelCookie(\'wc\')">更改</a></td>')
	}
}
function whis(id) {
	cook=GetCookie('wh')
	if(cook) {
		arr=cook.split('@')
		narr=new Array()
		for(i in arr) {
			if(arr[i]!='' && arr[i]!=id) narr[i]=arr[i]
		}
		if(narr.length>4) narr.length=4
		if(narr.length<1) str=id
		else str=id+'@'+narr.join('@')
		SetCookie('wh',str,5184000,'/')
	}
	else SetCookie('wh',id,5184000,'/')
}