function lp_unesc(str){return window.decodeURIComponent?decodeURIComponent(str):unescape(str)}
function lp_esc(str){return window.encodeURIComponent?encodeURIComponent(str):escape(str)}

//enum urlCookieOrder
lpSettings.URL_ONLY = 1;
lpSettings.URL_COOKIE = 2;
lpSettings.COOKIE_ONLY = 3;
lpSettings.COOKIE_URL = 4;
lpSettings.COOKIE_EXPIRATION = 1000*60*60*24*365;
	
function lpSettings(cookieName, urlCookieOrder, c_expires, c_path, c_domain ){
			
	this.cookieN = cookieName;
	this.parseOrder = urlCookieOrder;
	this.cookieData = "";
	this.params = new Object();
	this.cookie_expires = c_expires;
	this.cookie_path = c_path;
	this.cookie_domain = c_domain;
	
	this.getString = function(name){
		var lp_a=null;
		var lp_b=this.getParam(name,lp_a);
		return lp_b!=null?lp_b+"":lp_a;
	};
	
	this.getInt= function(name){
		var lp_a=null;
		var lp_b=parseInt(this.getParam(name,lp_a));
		return isNaN(lp_b)?lp_a:lp_b;
	};
	
	this.getBool = function(name) {
		
		return this.getInt(name)?true:false;
	};
	
	this.setParam = function(name, value){
	
		this.params[name] = value;		
		this.saveAll();	
	};
	
	this.saveAll = function() {
		
		var s = "";
		for(var lp_ in this.params){			
			s = s + "&" + lp_ + "=" + lp_esc(this.getParam(lp_));
		}
		this.set_Cookie(cookieName, s);
	}
	
	this.getParam = function(name, defaulValue) {
	
		if(typeof defaulValue=="undefined"){
			defaulValue=null;
		}
		var lp_b=this.params[name];
		return typeof lp_b!="undefined"?lp_b:defaulValue;
	}
	
	this.getCookieData = function(){
		return this.cookieData;
	};
	
	this.init = function() {
		
		if (this.parseOrder == lpSettings.URL_ONLY) {			
			this.parseURL();			
		}
		else if (this.parseOrder == lpSettings.URL_COOKIE) {
			//parse the cookie and overwrite with the url
			this.parseCookie();
			this.parseURL();			
		}
		else if (this.parseOrder == lpSettings.COOKIE_ONLY) {
			this.parseCookie();			
		}
		else if (this.parseOrder == lpSettings.COOKIE_URL) {
			//parse url and overwrite with cookie
			this.parseURL();
			this.parseCookie();			
		}
	}
	
	this.parseURL = function() {
		
		try {
			var lp_a=window.location.search.substring(1).split("&");
			this.parseParams(lp_a);		
		} catch (ee) {}
	}
	
	this.parseCookie = function() {
		try {
			this.cookieData = this.get_Cookie(this.cookieN);
			var lp_a = this.cookieData.substring(1).split("&");			
			this.parseParams(lp_a);
		} catch (ew) {}
	}
	
	this.parseParams = function(lp_a){
			
		for(var lp_b=0;lp_b<lp_a.length;lp_b++){
			var lp_c=lp_a[lp_b].indexOf("=");
			if(lp_c==-1)continue;
			var lp_e=lp_a[lp_b].substring(0,lp_c);
			lp_e=lp_e.replace(/\+/g," ");
			var lp_f=lp_a[lp_b].substring(lp_c+1);
			lp_f=lp_f.replace(/\+/g," ");
			lp_f=lp_unesc(lp_f);
			this.params[lp_e] = lp_f;
		}
	}
	
	this.dump = function() {
		document.write("<pre>");
		for(var lp_ in this.params){			
			document.writeln(lp_+" = "+this.getParam(lp_));
		}
		document.write("</pre>");
	}
	
	this.set_Cookie = function( name, value ) 
	{
	    var expires = this.cookie_expires;
	    var path = this.cookie_path;
	    var domain = this.cookie_domain;
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		
		var expires_date = new Date( today.getTime() + (lpSettings.COOKIE_EXPIRATION) );

		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" );
	}

	this.get_Cookie = function( name ) {

		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) &&
			( name != document.cookie.substring( 0, name.length ) ) )
		{
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	}
	
	this.init();	
}