// Class Definition of HttpCookie
var HttpCookie = function( name, value, expires, path, domain )
{
	if( name ) this.Name = name;
	if( value ) this.Value = value;
	if( expires ) this.Expires = expires;
	if( path ) this.Path = path;
	if( domain ) this.Domain = domain;
};

// Property and Function of HttpCookie Class
HttpCookie.prototype = 
{
	Name:'', Value:'', Expires:'', Path:'/', Domain:'',
	toCookie: function()
	{
		var NewCookie = this.Name + '=' + this.Value;
		if( this.Expires )	NewCookie += (';expires=' + this.Expires);
		if( this.Path )		NewCookie += (';path=' + this.Path);
		if( this.Domain )	NewCookie += (';domain=' + this.Domain);
		return NewCookie;
	}
}

var CookieHelper = function(){};

// Time Convert Function
// Step1. Convert Houre to Millisecond
// Step2. Convert time format to UTC String
CookieHelper.ConvertToUTCString = function( hourNumber )
{
	var Timestamp = ( !hourNumber || hourNumber == 0 ) ? (0) : (new Date().getTime() + (hourNumber * 1000 * 60 * 60));
	return new Date(Timestamp).toUTCString();
};

// Setting Cookie ( Name, Value, ExpireHour, Path, Domain )
CookieHelper.Set = function( cookieName, cookieValue, expireHour, path, domain )
{
	// Add a new HttpCookie, Set Cookie Name and Value
	var HC = 
	new HttpCookie
	(
		cookieName,
		escape(cookieValue),							// Encode value by escape)
		CookieHelper.ConvertToUTCString(expireHour),	// Convert Hour to Milliseconds
		path,
		domain
	);	
	
	// Assign Cookie value to Document by HttpCookie.toCookie
	document.cookie = HC.toCookie();
};

// Get Cookie value by Name
CookieHelper.Get = function( cookieName )
{
	// 因為document.cookie是一連串的字串(所有Cookie都在這邊)
	// 所以用正規表達式來取得我們要的值
	var regex = new RegExp( ("(^| )" + cookieName + "=([^;]*)(;|$)") );
	var Matchs = document.cookie.match( regex );
	if( Matchs ) return unescape( Matchs[2] );	// Decode value by unescape
	return null;								// Return null when the cookie value is not exist
};

// Delete Cookie by Name, Path and Domain
CookieHelper.Delete = function( cookieName, path, domain )
{
	// 
	if( !CookieHelper.Get(cookieName) ) return;
	
	// Add a new HttpCookie, Value is empty and expire is 0
	var HC =
	new HttpCookie
	(
		cookieName,
		null,
		CookieHelper.ConvertToUTCString(0)
	);
	
	document.cookie = HC.toCookie();
};

function GetCookie(id)
{
	return CookieHelper.Get(id);
}

function InsertCookie(id, value)
{
	CookieHelper.Set(id, value, 24*7, '/', '');
	
}
				
function DeleteCookie(id)
{
	CookieHelper.Delete(id, '/', '');		
		
}


function testCookie()
{
	var seed = Math.round(Math.random()*10000)
	InsertCookie('CookieFlag', seed);
	var tmp = CookieHelper.Get('CookieFlag');
	//window.alert('seed:'+seed+' & tmp:'+tmp);
	DeleteCookie('CookieFlag');
	if (seed != tmp)
	{
		return false;
	}
	else
	{
		return true;
	}
}
			
function _Switch(obj, status)
{
	if (obj.style.visibility!=status)
		obj.style.visibility=status;
}

function DisableByElementTag(TagName)
{
	var objs = document.all.tags(TagName)
	for (i = 0 ; i < objs.length ; i++)
		objs[i].disabled = true;
}

function PreviousCookie(model, path, catalog, url)
{
	if (model != "" && path != "" && catalog != "" && url != "")
	{
		var ExpaireHour = 24;
		var myDate = new Date();
		myDate.setTime(myDate.getTime() + 1000 * 60 * 60 * ExpaireHour);
		var CookieName = model + "_upc";
		var CookieValue = model;
		CookieValue = CookieValue + "#@#" + path;
		CookieValue = CookieValue + "#@#" + catalog;
		CookieValue = CookieValue + "#@#" + url
		CookieValue = CookieValue + "#@#" + myDate.toLocaleString();
		//alert(CookieValue);
		CookieHelper.Set(CookieName, CookieValue, ExpaireHour, '/', '');
	}
}