
function r2iClass()
{
	function CookieClass()
	{
		this.getVal=function(offset){
			var endstr=document.cookie.indexOf(";",offset);
			if(endstr==-1)
				endstr=document.cookie.length;
			return unescape(document.cookie.substring(offset,endstr));
		};
		this.get=function(name){
			var arg=name+"=";
			var alen=arg.length;
			var clen=document.cookie.length;
			var cookiei=0;
			while(cookiei<clen)
			{
				var j=cookiei+alen;
				if(document.cookie.substring(cookiei,j)==arg)
					return this.getVal(j);
				cookiei=document.cookie.indexOf(" ",cookiei)+1;
				if(cookiei==0)
					break;
			}
			return null;	
		};
		this.set=function(name,value,expires,path,domain,secure)
		{
			if (typeof expires=='undefined') expires=null;
			if (typeof path=='undefined') path=null;
			if (typeof domain=='undefined') domain=null;
			if (typeof secure=='undefined') secure=false;
			document.cookie=name+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+((path==null)?"":("; path="+path))+((domain==null)?"":("; domain="+domain))+((secure==true)?"; secure":"");		
			//var img=new Image();
			//img.src=document.location;
		};
		this.clear=function()
		{
			var exp=new Date;
			exp.setTime(exp.getTime()-1000000000);
			var cval=this.get('DemoName');
			document.cookie='DemoName'+"="+cval+"; expires="+exp.toGMTString();
		};
	}
	function TrackClass()
	{
		this.branch=function(urlSegment,message)
		{
			if (this.check(urlSegment,true)) alert(message);
		}
		this.check=function(urlSegment,autoset)
		{
			var key = 'r2i.url.track.check.';
			if (typeof autoset==null) autoset=false;
			if ((document.location+'').toLowerCase().match(urlSegment)==null)
			{ //not in the urlSegment
				if (r2i.cookie.get(key+urlSegment)!=null&&r2i.cookie.get(key+urlSegment)!='')
				{
					if (autoset) r2i.cookie.set(key+urlSegment,'',null,'/');
					return true;
				}
			}
			else {
				if (autoset) r2i.cookie.set(key+urlSegment,true,null,'/');
			}
			return false;		
		}
	}
	function URLClass()
	{
		this.track=new TrackClass();
		this.history = function(hashname,callback_refresh,callback_set,parameters)
		{
			this.history = new HistoryClass(hashname,callback_refresh,callback_set,parameters);
			this.history.load();
		}		
		this.query = function(q)
		{
			return new PageQueryClass(q);
		}
	}
	function PageQueryClass(q) {
		if(q.length > 1) 
			this.q = q.substring(0, q.length);
		else 
			this.q = null;
		this.keyValuePairs = new Array();
		if(q) {
			for(var i=0; i < this.q.split("&").length; i++) {
				this.keyValuePairs[i] = this.q.split("&")[i];
			}
		}
		this.getKeyValuePairs = function() { return this.keyValuePairs; }
		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if(this.keyValuePairs[j].split("=")[0] == s)
					return this.keyValuePairs[j].split("=")[1];
			}
			return "";
		}
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
			return a;
		}
		this.getLength = function() { return this.keyValuePairs.length; }
	}		
	function HistoryClass(hashname,callback_set,callback_refresh,parameters)
	{
		this.hashname = hashname; 
		this.onRefresh = callback_refresh;
		this.onSet = callback_set;
		this.parameters = parameters;

		this.load=function()
		{
			//GET THE CURRENT URL HISTORY PARAMETERS
				this.parameters = this.getUrl();
			//CALLBACK THE SET OF THE PARAMETERS
				this.onSet(this.parameters);
			//CALLBACK THE LOAD
				this.onRefresh();
		}
		this.getUrl = function()
		{
			//USE THE CURRENT URL TO GET THE PARAMETERS
			var params = this.parameters;
			
			//POPULATE THE parameters...
			var str = window.location+'';
			var pIndex = str.indexOf(this.hashname);
			
			if (pIndex>0)
			{
				var searchString = str.substring(pIndex+this.hashname.length,str.length);
				
				var page = r2i.url.query(searchString);
				var key = '';
				for (var key in params) {
				  if (params.hasOwnProperty(key)) {
					params[key]=unescape(page.getValue(key));
				  }
				}
			}
			return params; 
		} 	
		this.setUrl = function(params)
		{
			var str = window.location+'';
			var pIndex = str.indexOf('#');
			if (pIndex > 0)
			{
				str = str.substring(0,pIndex);
			}
			var hashcode = new Array();
			var b = 0;
			for (var key in params) {
			  if (params.hasOwnProperty(key)) {
				if (b==0)
					hashcode.push(this.hashname+key+'='+params[key]);
				else
					hashcode.push(key+'='+params[key]);
				b=1;
			  }
			}	
			window.location.replace(str+hashcode.join('&'));
			var strHash = window.location.hash + '&return=1';
			window.location.hash = strHash;

        }
        //clears out the search criteria and all of it's parameters from the URL
        this.clear = function() {
            var str = window.location + '';
            var pIndex = str.indexOf('#');
            if (pIndex > 0) {
                str = str.substring(0, pIndex);
            }
            window.location.replace(str);
            var strHash = window.location.hash;
            window.location.hash = strHash;
        }

	/*
		Date: 6.1.2010
		Modified By: Ali Khakpouri.
		Description: the clear parameter was added as to toggle the parameter  as requested per user's pereference. Usual case of use: checkboxes and radio buttons.			
	*/
        this.save = function(name, value, append,clear) {
            if (typeof append == 'undefined' || append == null)
                append = false;
		if(typeof clear == 'undefined' || clear == null)
			clear= false;

            //SET THE parameters object
            var params = this.getUrl();

            //CHECK FOR APPEND, WHEN TRUE - DO THE FOLLOWING, WHEN FALSE, REMOVE IT
            if (append) {
                if (params[name] != "" && params[name].indexOf(value) == -1) {
                    params[name] = params[name] + ',' + value;
                }
                else if (params[name] == "") {
                    params[name] = value;
                }
            }
            else {
                if (params[name] != "" && params[name].indexOf(value) >= 0) {
                    if (params[name].indexOf(',' + value) >= 0) {
                        value = ',' + value;
                        params[name] = params[name].replace(value, '');
                    }
                    else if (params[name].indexOf(value + ',') >= 0) {
                        value = value + ',';
                        params[name] = params[name].replace(value, '');
                    }
                    //remove the parameter if the value is either null or empty.
                    else if (value == null || value.length == 0) {
                        params[name] = '';
                    }
                    else if (clear){
                        params[name] = params[name].replace(value, '');
                    }

                }
                else
                    params[name] = value;
            }
            this.setUrl(params);
        }
	}
	this.url=new URLClass();
	this.cookie = new CookieClass();	
}
var r2i=new r2iClass();

function loadForm(parameters){
	var tab = parameters.tab;
	
	if(tab != ""){
		$(".tabname").each(function(){
			var text = $(this).text().toLowerCase();
			tab = tab.toLowerCase();
			
			if(tab == text){
				$(this).addClass("selected");
				$(this).click();
			}
			else{
				$(this).removeClass("selected");
			}		
		});
	}	
}

function loadGrid(){
}

function loadPage(){
	 r2i.url.history('#tabselected/', loadForm, loadGrid, { 'tab': '' });
	 
	 $(".tabbutton").each(function(){
		$(this).click(function(){
			var text = $(this).text();
			r2i.url.history.save('tab',text);
			return false;
		});
	 });
}
