﻿var PageCookie = function(pageName)
{
    this.base = Cookie;
    this.base(pageName);
    
	this.PageName = pageName;

	var exp = new Date();  
	exp.setDate(365);
	this.Expires = exp.toGMTString();
}
PageCookie.prototype = new Cookie();
PageCookie.prototype.constructor = PageCookie;

PageCookie.prototype.GetValue = function(id)
{
	if( this.Value == null ) return null;
	
	var i = this.Value.indexOf("|id|" + id);
	if( i < 0 ) return null;

	var vi = this.Value.indexOf("|value|", i);
	var vie = this.Value.indexOf("|disabled|", vi);
	
	var v = this.Value.substring(vi+7, vie);
	return v;
}

PageCookie.prototype.GetDisabled = function(id)
{
	if( this.Value == null ) return null;

	var i = this.Value.indexOf("|id|" + id);
	if( i < 0 ) return null;

	var vi = this.Value.indexOf("|disabled|", i);
	var vie = this.Value.indexOf("|id|", vi);
	if( vie == -1 ) vie = this.Value.length;
	
	var v = this.Value.substring(vi+10, vie);
	return v;
}

PageCookie.prototype.$Append = Cookie.prototype.Append;
PageCookie.prototype.Append = function(id, value, disabled)
{
	this.$Append("|id|" + id + "|value|" + value + "|disabled|" + disabled);
}

PageCookie.prototype.GetPageState = function(psAIBlock)
{
	if( psAIBlock == null ) 
		psAIBlock = "all";
	else
		psAIBlock = psAIBlock.toLowerCase();

	var oFields = document.getElementsByTagName("input");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "text" )
				{
					if( oFields[i].id != null )
						this.Append(oFields[i].id, oFields[i].value, oFields[i].disabled);
				}
				else if( oFields[i].type == "checkbox" || oFields[i].type == "radio" )
				{
					if( oFields[i].id != null )
						this.Append(oFields[i].id, oFields[i].checked, oFields[i].disabled);
				}
			}
		}
	}

	var oFields = document.getElementsByTagName("textarea");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "textarea" )
				{
					if( oFields[i].id != null )
						this.Append(oFields[i].id, oFields[i].value, oFields[i].disabled);
				}
			}
		}
	}
	
	var oFields = document.getElementsByTagName("select");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "select-one" )
				{
					if( oFields[i].id != null )
						this.Append(oFields[i].id, oFields[i].selectedIndex, oFields[i].disabled);
				}
			}
		}
	}
}

PageCookie.prototype.RestorePageState = function(psAIBlock)
{
	if( this.Value == null ) return;

	if( psAIBlock == null ) 
		psAIBlock = "all";
	else
		psAIBlock = psAIBlock.toLowerCase();
	
	var oFields = document.getElementsByTagName("input");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "text" )
				{
					var id = oFields[i].id;
					if( id )
					{
						var v = this.GetValue(id);
						var d = this.GetDisabled(id);
						if( v != null )
							oFields[i].value = v;

						//needed because in JS "false" evalutes to true (WTF?!?)
						if( d == "true" )
							oFields[i].disabled = true;
						else
							oFields[i].disabled = false;
					}
				}
				else if( oFields[i].type == "checkbox" || oFields[i].type == "radio" )
				{
					var id = oFields[i].id;
					if( id )
					{
						var v = this.GetValue(id);
						var d = this.GetDisabled(id);
						
						//needed because in JS "false" evalutes to true (WTF?!?)
						if( v == "true" )
							oFields[i].checked = true;
						else
							oFields[i].checked = false;
							
						//needed because in JS "false" evalutes to true (WTF?!?)
						if( d == "true" )
							oFields[i].disabled = true;
						else
							oFields[i].disabled = false;
					}
				}
			}
		}
	}

	var oFields = document.getElementsByTagName("textarea");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "textarea" )
				{
					var id = oFields[i].id;
					if( id )
					{
						var v = this.GetValue(id);
						var d = this.GetDisabled(id);
						if( v != null )
							oFields[i].value = v;

						//needed because in JS "false" evalutes to true (WTF?!?)
						if( d == "true" )
							oFields[i].disabled = true;
						else
							oFields[i].disabled = false;
					}
				}
			}
		}
	}
	
	var oFields = document.getElementsByTagName("select");

	if( oFields != null )
	{
		for( i=0; i < oFields.length; i++ )
		{
			var sAIBlock = oFields[i].getAttribute('ai_block');
			
			if( sAIBlock != null ) 
				sAIBlock = sAIBlock.toLowerCase();
				
			if( psAIBlock == "all" || sAIBlock == psAIBlock )
			{
				if( oFields[i].type == "select-one" )
				{
					var id = oFields[i].id;
					if( id )
					{
						var v = this.GetValue(id);
						var d = this.GetDisabled(id);
						if( v != null )
							oFields[i].selectedIndex = v;

						//needed because in JS "false" evalutes to true (WTF?!?)
						if( d == "true" )
							oFields[i].disabled = true;
						else
							oFields[i].disabled = false;
					}
				}
			}
		}
	}
}