﻿var PageCookie = function(pageName)
{
    this.base = Cookie;
    this.base(pageName);
    
	this.PageName = pageName;
}
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);
				}
				else //select-multiple
				{
					if( oFields[i].id != null )
					{
						var selectedOptions = "";
						for(var y=0; y<oFields[i].childNodes.length;y++)
						{
							var opt = oFields[i].childNodes[y];
							if(opt.nodeType!=1 || opt.selected != true) continue;
							selectedOptions += "&i=" + y;
						}
						this.Append(oFields[i].id, selectedOptions, 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 )
			{
					var id = oFields[i].id;
					var indexChanged = false;
					if( id )
					{
						var v = this.GetValue(id);
						var d = this.GetDisabled(id);

						if( oFields[i].type == "select-one" )
						{
							if( v != null ) {
								if(oFields[i].selectedIndex != v) {
									oFields[i].selectedIndex = v;
									indexChanged = true;
								}
							}
						}
						else //select-multiple
						{
							var sValues = new String(v);
							for(var y=0; y<oFields[i].childNodes.length;y++)
							{
								var opt = oFields[i].childNodes[y];
								if(opt.nodeType!=1) continue;
								var rg = new RegExp("&i=" + y);
								if(sValues.match(rg))
									opt.selected = true;
							}
					
						}


						//needed because in JS "false" evalutes to true (WTF?!?)
						if( d == "true" )
							oFields[i].disabled = true;
						else
							oFields[i].disabled = false;
							
						//handle the country field
						if (id == 'country' && indexChanged) {
							populateState();
						}
						
						//handle the state field
						if (id == 'state' && indexChanged) {
							populateCounty();
						}
							
						//handle the county field
						if (id == '00N70000002EkJh' && indexChanged) {
							oFields[i].parentNode.parentNode.style.display = '';
						}
					}
				
			}
		}
	}
}
