function setCookie(name, value, days)
{
    var expires = "";
    if (days) {
        var d = new Date();
        d.setTime(d.getTime() + days * 24 * 60 * 60 * 1000);
        expires = "; expires=" + d.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function getCookie(name)
{

    var re = new RegExp("(\;|^)[^;]*(" + name + ")\=([^;]*)(;|$)");
    
    var res = re.exec(document.cookie);
    return res != null ? res[3] : null;
}

//return page url
function reThiePageUrl()
{
	var url = window.location.href;
	var urlList = url.split('/');
	var urlStr = urlList[urlList.length-1];
	if(urlStr.indexOf('.')<0)
	{
		urlStr = "index.html";
	}	
	return urlStr ;
}

//load
function aOnclickInputCookieLoad(panelId)
{	

	var thisUrl = reThiePageUrl().replace('.','').replace('#','');
	var objList = document.getElementById(panelId).getElementsByTagName('a');
			
	if(thisUrl.indexOf('index')>-1)
	{	
		for(var i = 0;i<10;i++)
		{
			doesEachAHaveOnclick(objList[i],i,'list');//add onclick event for each A with "For"
		}
		
		for(var i = 10;i<objList.length;i++)
		{
			doesEachAHaveOnclick(objList[i],i,thisUrl);//add onclick event for each A with "For"
		}
	}
	else
	{
		for(var i = 0;i<14;i++)
		{
			doesEachAHaveOnclick(objList[i],i,'list');//add onclick event for each A with "For"
		}
		
		for(var i = 14;i<objList.length;i++)
		{
			doesEachAHaveOnclick(objList[i],i,thisUrl);//add onclick event for each A with "For"
		}
	}
	
	changeAStyleInCookie(objList,thisUrl);//change the style of A if there is the index value of A in cookie value
}

function doesEachAHaveOnclick(theOne,index,url)
{
	//if A has not the onclick event,take a new onclick event to A.
	if(theOne.onclick == null)
	{
		theOne.onclick = function(){
			forAOnclick(theOne,index,url);
		}
	}
	else // if A has the onclick event,a new event is used before the old event.
	{
		var onclickEvent = theOne.onclick;
		theOne.onclick = function(){			
			forAOnclick(theOne,index,url);
			onclickEvent;
		};
	}	
}

//A's onclick event.
function forAOnclick(theOne,index,url)
{
	
	var cookieValue = "";
	if( getCookie(url) != null && getCookie(url)!= "")
	{	
		cookieValue = getCookie(url);
	}
	
	var cookieValueList = cookieValue.split(',');
	
	var cookieValueFirst = "";
	
	if(cookieValueList.length>0)
	{	
		cookieValueFirst = cookieValueList[0];
		
		if(cookieValueFirst != index)
		{
			if(cookieValue.indexOf(','+index)<0)
			{
				if(cookieValue=="")
				{
					cookieValue = index;					
				}
				else
				{
					cookieValue = cookieValue + ","+index;	
				}
						
				setCookie(url,cookieValue,1);//input cookie
			}			
		}		
	}
	else
	{
		setCookie(url,index,1);//input cookie
	}
	
	changeAObjectVisited(theOne);//change the Object's style
	
}

function changeAStyleInCookie(list,url)
{
	var cookieValue = "";
	if( getCookie(url) != null && getCookie(url)!= "")
	{	
		cookieValue = getCookie(url);
	}
	
	if(cookieValue!="")
	{
		var cookieValueList = cookieValue.split(',');
		
		for(var i = 0;i<cookieValueList.length;i++)
		{
			if(cookieValueList[i] != "")
			{
				changeAObjectVisited(list[cookieValueList[i]]);
			}
		}
	}
	
	var cookieListValue = "";
	if( getCookie('list') != null && getCookie('list')!= "")
	{	
		cookieListValue = getCookie('list');
	}
	
	if(cookieListValue!="")
	{
		var cookieListValueList = cookieListValue.split(',');
		
		for(var i = 0;i<cookieListValueList.length;i++)
		{
			if(cookieListValueList[i] != "")
			{
				if(url.indexOf('index')>-1 && cookieListValueList[i]<10)
				{
					changeAObjectVisited(list[cookieListValueList[i]]);
				}
				else if(url.indexOf('index')<0 && cookieListValueList[i]<14)
				{
					changeAObjectVisited(list[cookieListValueList[i]]);
				}
			}
		}
	}	
}

function changeAObjectVisited(obj)
{
	obj.style.color = "#810081";
	obj.style.textDecoration = "underline";
}


function changeAObjectLink(obj)
{
	obj.style.color = "#095876";
	obj.style.textDecoration = "none";
}



