/********************************************************************
* Cookie Function
* ===================================================================
* var cookie = new Cookie();
* cookie.Read("name") ==> read
* cookie.Save("name", "nphur"); ==> save
* cookie.Delete("name") ==> delete;
********************************************************************/
var Cookie = function()
{
}

Cookie.prototype.MaxSize = 4000; //size in KB

Cookie.prototype.$GetValue = function( startIndex )
{  
	var endIndex = document.cookie.indexOf( ";", startIndex );  
	if( endIndex == -1 )    
		endIndex = document.cookie.length;
	var cookieValue = document.cookie.substring(startIndex, endIndex);
	if( cookieValue == "" )
		return null;
	else
		return unescape( cookieValue );
}

Cookie.prototype.Read = function(key)
{  
	var arg = key + "=";  
	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 this.$GetValue(j);
		}
		i = document.cookie.indexOf( " ", i ) + 1;    
		if( i == 0 ) break;
	}  
	
	return null;
}

Cookie.prototype.Save = function(key, value, path, expires, domain, secure) {
    var newCookie = key + "=" + escape(value) +
	((expires == null) ? "" : ("; expires=" + expires)) +
	((path == null) ? "; path=/" : ("; path=" + path)) +
	((domain == null) ? "; domain=yes24.vn" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
   
    if (newCookie.length > Cookie.MaxSize)
        throw Error("Cookie length was " + newCookie.length + "kb but cookies cannot exceed " + Cookie.MaxSize + "kb");

    document.cookie = newCookie;
}

Cookie.prototype.Delete = function(key)
{  
	var exp = new Date();  
	exp.setTime(exp.getTime() - 1);
	document.cookie = key + "=null;expires=" + exp.toGMTString();
}


/********************************************************************
* QueryString Function
* ===================================================================
* var qry = new QueryString();
* qry.GetValue("pid"); ==> read
* qry.SetValue("seq", "1234"); => Save
* qry.Load() ==> required
* qry.ToString();
********************************************************************/
var QueryString = function()
{
	this.Data = [];
	this.Load();
}

QueryString.prototype.Load = function()
{  
	var aPairs, aTmp;
	var queryString = new String(window.location.search);
	queryString = queryString.substr(1, queryString.length); //remove "?"
	aPairs = queryString.split("&");	

	for (var i=0 ; i<aPairs.length; i++)
	{
		aTmp = aPairs[i].split("=");
		this.Data[aTmp[0]] = aTmp[1];
	}
}

QueryString.prototype.GetValue = function(key)
{
	return this.Data[key];
}

QueryString.prototype.SetValue = function(key, value) {
    if (value == null)
        delete this.Data[key];
    else
        this.Data[key] = value;
}

QueryString.prototype.Clear = function()
{
	delete this.Data;
	this.Data = [];
}

QueryString.prototype.ToString = function()
{
	var queryString = new String(""); 
	
	for (var key in this.Data)
	{	
		if (queryString != "")
			queryString += "&"
		if (this.Data[key])
			queryString += key + "=" + this.Data[key];		
	}
	if (queryString.length > 0)
		return "?" + queryString;
	else
		return queryString;
}

stringFormat = function(format, arguments) {
    var str = format;
    for (i = 0; i < arguments.length; i++) {
        str = str.replace('{' + i + '}', arguments[i]);
    }
    return str;
};
