function setcookie(name,value,duration)
{
	document.cookie = name+"="+escape(value)+";expires="+getnowGMTtime(duration) +";" ;
	return true;
}

function getcookie(cookiename) 
{
	var cookiestring=""+document.cookie;
	var index1=cookiestring.indexOf(cookiename);
	if (index1==-1 || cookiename=="") return ""; 	
	var index2=cookiestring.indexOf(';',index1);
	if (index2==-1) index2=cookiestring.length;
	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

//valid EXPIRES
function getnowGMTtime(duration) 
{
	var defduration = 1000 * 60 * 60 * 8 ;
	if ( duration == null ) duration = defduration ;
	if ( duration == "" ) duration = defduration ;
	now=new Date();
	now.setTime( now.getTime() + duration );
	return now.toGMTString();
}

//set CookieValue
function setcookieValue(cookievalue) 
{
	if ( cookievalue == null ) return "1" ;	
	else if ( cookievalue == "" ) return "1" ;	
	else return (parseInt(cookievalue) + 1 ).toString() ;
}


//set CookieValue
function setpushTimes(a,popUrl) 
{
	//popup的視窗大小
	var popupAttb = "location=no,menubar=no,toolbar=no,status=no,width=360,height=240,left=100,top=100" ;
	
	var defduration = 1000 * 60 * 60 * 8 ;
	var times = getcookie(a.name) ;
	if ( times == null ) times = "1";
	if ( times == "" ) times = "1";
	if ( parseInt(times) < 4 )
	{
		setcookie(a.name, setcookieValue(times), defduration) ;
		//要popup的地方
		window.open(popUrl,"presure",popupAttb);
	}
	
	return true;
}

function nextPage(a, nextUrl, popUrl)
{
	setpushTimes(a,popUrl);
	//window.location.replace(nextUrl);
	window.location.reload(nextUrl);
}
