var jsDebug;
var dataEntityList;
var dataEntityHashTable;

function dataEntityHashTable_toString()
{
	string = "";
	separator= "";
	for (var key in dataEntityHashTable)
	{
		debug(3, "key:<" + key + ">");
		string += separator + key + "=" + dataEntityHashTable[key].id;
		separator = ", ";
	}
	return string;
}
function transformDataEntitiesToPostString()
{
	debug(1, 'dataEntities:<' + dataEntityHashTable_toString() + '>');
	postString = "";
	separator = "";
	for (var key in dataEntityHashTable)
	{
		postString +=  separator + key + "=" + encodeURI(dataEntityHashTable[key].value);
		separator = "&";
	}
	debug(1, "postString:<" + postString + ">");
	return postString;
}
function loadDataEntityList()
{
	dataEntityList = new Array();
	dataEntityHashTable = new Object();

	var formFieldsList = document.getElementsByName("formFieldsList");

	if (formFieldsList == undefined || formFieldsList == null || formFieldsList.length == 0)
	{
		throw "DataEntityList Exception: formFieldsList Does Not Exist in web page!";
	}
	debug(2, "loadDataEntityList()->formFieldList.length:<" + formFieldsList.length + ">");

	if (formFieldsList.length == 1)
	{
		var formFieldsList = document.getElementById("formFieldsList").value;
		var formFieldsArray  = formFieldsList.split(",");

		for (var i = 0;  i < formFieldsArray.length; i++)
		{
			quartet = formFieldsArray[i];
			myDataEntity = new DataEntity(quartet);
			dataEntityList[i] = myDataEntity;
			dataEntityHashTable[myDataEntity.id] = myDataEntity;
			debug(2, "i:<" + i + ">, dataEntityList.length:<" + dataEntityList.length + ">");
		}
	}
	else
	{
		for (var i = 0; i < formFieldsList.length; i++)
		{
			quartet = formFieldsList[i].value;
			debug(2, "loadDataEntityList()->quartet:<" + quartet + ">");
			myDataEntity = new DataEntity(quartet);
			dataEntityList[i] = myDataEntity;
			dataEntityHashTable[myDataEntity.id] = myDataEntity;
			debug(2, "i:<" + i + ">, dataEntityList.length:<" + dataEntityList.length + ">");
		}
	}
	debug(1, "dataEntityHashTable:<" + dataEntityHashTable_toString() + ">");
}
function reloadDataEntityValues()
{
	for (var i = 0;  i < dataEntityList.length; i++)
	{
		myDataEntity = dataEntityList[i];
//		myDataEntity.value = document.getElementById(myDataEntity.id).value;
		myDataEntity.getValue();	//	myDataEntity.value = document.getElementById(myDataEntity.id).value;
		debug(2, "reloadDataEntityValues()->myDataEntity.id:<" + myDataEntity.id + ">, myDataEntity.value:<" + myDataEntity.value + ">");
	}
}
function revertAllToOriginalDisplayState()
{
	for (var i = 0; i < dataEntityList.length; i++)
	{
		dataEntityList[i].revertToOriginalDisplayState();
	}
}
function updateDataEntityList(requiredFieldListArray)
{
	if (dataEntityHashTable == undefined || dataEntityHashTable == null)
	{
		throw "DataEntityHashTable Exception: Does Not Exist!";
	}
	debug(1, "dataEntityHashTable.length:<" + length + ">")
	
	revertAllToOriginalDisplayState();
	for (var i = 0; i < requiredFieldListArray.length; i++)
	{
		var requiredField = requiredFieldListArray[i];
		myDataEntity = dataEntityHashTable[requiredField];
		if (myDataEntity == undefined || myDataEntity == null)
		{
			throw "DataEntity Exception: Not Found:<" + requiredField + ">";
		}
		myDataEntity.enableField();
	}
	for (i = 0; i < dataEntityList.length; i++)
	{	
		myDataEntity = dataEntityList[i];

		debug(1,"myDataEntity.id:<" + myDataEntity.id + ">, " +
			"disabled:<" + myDataEntity.isCurrentlyDisabled() + ">, " +
			"value:<" + myDataEntity.value + ">");

		if (myDataEntity.isCurrentlyDisabled())
		{
			myDataEntity.initializeValue();
		}
	}
		
}

//
//	define validation object plus functions
//
//function DataEntity(name_, value_)
function DataEntity(quartet)
{
	function validateIt() 
	{ 
// last		this.valid = true;

		if (this.validateFn == 0)
			return true;
		else if (this.element.disabled == true)
			return true;

		this.enableField();	
		this.element.style.backgroundColor = "#FFFFFF";

		this.valid =  this.validateCB(this); 
		return this.valid;
	}
	function isValid() 
	{ 
		return this.valid; 
	}
	function setValidationError(error_)
	{
//		this.validationError = " - \"" + this.id + "\" " + error_;
		this.validationError = " - \"" + this.labelText + "\" " + error_;
		this.valid = false;
	}
	function revertToOriginalDisplayState(resetToo)
	{
		if (this.originallyEnabled)	//	(this.enabled)
			this.enableField();		//	this.element.disabled = false;
		else 
			this.disableField();	//	this.element.disabled = true;
	}
	function isOriginallyEnabled()
	{
		return this.originallyEnabled;		// this.enabled
	}
	function isCurrentlyEnabled()
	{
		return this.enabled;				//	(this.element.disabled ? false : true);
	}
	function isOriginallyDisabled()
	{
		return (this.originallyEnabled ? false : true);	//	(this.enabled ? false : true);	
	}
	function isCurrentlyDisabled()
	{
		return (this.enabled ? false : true);	//	(this.element.disabled ? true : false);
	}
	function reset(count)
	{
		debug(2, 
			"reset()->count:<" + count + ">, this.id:<" + this.id + ">, this.type:<" + this.type + ">, hidden:<" + this.hidden + ">, " +
			"this.type.indexOf(select):<" + this.type.indexOf("select") + ">, " +
			"this.isOriginallyEnabled():<" + this.isOriginallyEnabled() + ">, " +
			"this.isOriginallyDisabled():<" + this.isOriginallyDisabled() + ">");

		if (this.type == 'hidden')
			return;
		if (count == 1)
			this.element.focus();

		if (this.type.indexOf("select") == 0)
		{
			this.element.selectedIndex = -1;	//	0;
		}
		else if (this.type == 'text')
		{
			this.element.value = "";
			this.value = "";
		}
		else if (this.type == 'submit' || this.type == 'button')
		{
			if (this.isOriginallyDisabled())
				this.element.style.color="grey";
			else
				this.element.style.color="blue";
		}
		else
		{
			debug(1, "reset(count:>" + count + ">, what to set value of this type:<" + this.type + ">?");
		}
/*
		this.element.style.backgroundColor = "#FFFFFF";
*/
		if (this.isOriginallyEnabled())
			this.enableField();
		else if (this.isOriginallyDisabled())
			this.disableField();
	}
	function getValidationError() 
	{ 
		return this.validationError; 
	}
	function enableField()
	{
		this.enabled = true;
		debug(4, "DataEntry::enableField()->enabling:<" + this.id + ">");
		this.element.disabled = false;
		this.element.style.color="#000000";
		this.element.className='forminputcolor';
		if (this.promptElement != undefined)
		{
			if (this.id == 'otherPlatform')
			{
				debug(1, 'l_otherPlatform->enableField()->' +
					'this.valid:<' + this.valid + '>\\n' +
					'originallyDisabled:<' + this.isOriginallyDisabled() + '>\\n' +
					'promptElementOriginalClassName:<' + this.promptElementOriginalClassName + '>' );
			}
			if (this.id == 'facebookurl')
			{
				debug(1, 'l_facebookurl->enableField()->' +
					'this.valid:<' + this.valid + '>\\n' +
					'originallyDisabled:<' + this.isOriginallyDisabled() + '>\\n' +
					'promptElementOriginalClassName:<' + this.promptElementOriginalClassName + '>' );
			}
//			if (this.valid)
			{
				if (this.isOriginallyDisabled())
				{
					if (this.id == 'otherPlatform') 
						debug(1, 'l_otherPlatform->enableField()->setting this.promptElement.className to "attributename"');
					this.promptElement.className =  "attributename";
				}
				else
				{
					className  = (this.promptElementOriginalClassName == null ? "attributename" : this.promptElementOriginalClassName);
					if (this.id == 'otherPlatform') 
						debug(1, 'l_otherPlatform->enableField()->setting this.promptElement.className to className:<' + className + '>');
					this.promptElement.className = className;
				}
			}	
/*
			this.promptElement.style.fontWeight = "bold";
			this.promptElement.style.color = "#000000";
*/
		}
	}
	function disableField()
	{
		debug(4, "DataEntry::disableField()->disabling:<" + this.id + ">");
		this.enabled = false;
		this.element.disabled = true;
//		this.element.style.color="grey";
		this.element.className='formdisabledinputcolor';
//
//	reset value to nothing!
//
		this.element.value = "";
		this.value = "";

		if (this.promptElement != undefined)
		{
			this.promptElement.className="attributenamedisabled";
/*
			this.promptElement.style.fontWeight = "normal";
			this.promptElement.style.color = "#736F6E";
*/
		}
	}
	function setError(errorCount)
	{
		debug(4, 'setError()');
		if (errorCount == 1)
		{
			this.element.focus();
			if (this.type == "text")
				this.element.select();
		}
//		this.promptElement.style.color = "#FF0000";
		this.promptElement.className = "attributenameerror";
	}
	function setValid()
	{
		debug(4, 'setValid()');
		this.isValid = true;
		this.promptElement.className = // "attributename";
					(this.promptElementOriginalClassName == null ? "attributename" : this.promptElementOriginalClassName);
	}
	function getValue()
	{
		this.value = "";

		debug(1, "getValue()->id:<" + this.id + ">, this.type:<" + this.type + ">");

		if (this.type == "checkbox")
		{
			debug(1, "getValue()->id:<" + this.id + ">, this.type:<" + this.type + "> in checkbox processing....");
			var nodeList = document.getElementsByName(this.id);

			for (var loop = 0; loop < nodeList.length; loop++)
			{
                        	debug(1, "getElementValues()->id:<" + this.id + ">nodeList[loop]:<" + nodeList[loop].checked + ">");

				if (nodeList[loop].checked)
				{
					if (this.value == "")
					{
						this.value = nodeList[loop].value;
					}
					else
					{
						this.value += "," + nodeList[loop].value;
					}
				}
				else
				{
					if (this.value == "")
					{
						this.value = 'false';
					}
					else
					{
						this.value += "," + 'false';
					}
				}
			}
			debug(1, "getValue()->id:<" + this.id + ">, this.type:<" + this.type + ">, this.value:<" + this.value + ">");
		}
		else if (this.type == "select-multiple")
		{
			debug(2, "getValue()->id:<" + this.id + ">, this.type:<" + this.type + "> in select-multiple processing....");
			for (var loop = 0; loop < this.element.length; loop++)
			{
				if (this.element[loop].selected)
				{
					if (this.value == "")
					{
						this.value = this.element[loop].value;
					}
					else
					{
						this.value += "," + this.element[loop].value;
					}
				}
			}
		}
		else    //      if (type == "text")
		{
			debug(2, "getValue()->id:<" + this.id + ">, type:<" + this.type + "> in OTHER-TYPE processing....");
			this.value = this.element.value;
		}
		debug (2, "getValue()->id:<" + this.id + "> value:<" + this.value + ">");
		return this.value;
	}
	function isHidden() 
	{ 
		return this.hidden; 
	}
	function initializeValue() 
	{
		if (this.type.indexOf("select") == 0)
		{
			this.element.selectedIndex = 0;
			this.value = "";
		}
		else if (this.type == 'text')
		{
			this.element.value = "";
			this.value = "";
		}
		else if (this.type == "checkbox")
		{
			debug(1, "initializeValue()->id:<" + this.id + "> in checkbox processing....");
			var nodeList = document.getElementsByName(this.id);
			for (var loop = 0; loop < nodeList.length; loop++)
			{
				this.value = nodeList[loop].value = "";
				nodeList[loop].checked = false;
			}
		}
		else
		{
			debug(1, "initializeValue()->what to set value of this:<" + this.id + ">, type:<" + this.type + ">?");
		}

	}
	function setFocus()
	{
		debug(3, "setFocus()->for this.id:<" + this.id  + ">");
		this.element.focus();
	}
//
//	assign functions to object functions
//
	this.validateIt = validateIt;
	this.isValid = isValid;
	this.setValidationError = setValidationError;
	this.isOriginallyEnabled = isOriginallyEnabled;
	this.isCurrentlyEnabled = isCurrentlyEnabled;
	this.isOriginallyDisabled = isOriginallyDisabled;
	this.isCurrentlyDisabled = isCurrentlyDisabled;
	this.revertToOriginalDisplayState = revertToOriginalDisplayState;
	this.reset = reset;
	this.getValidationError = getValidationError;
	this.enableField = enableField;
	this.disableField = disableField;
	this.setError = setError;
	this.setValid = setValid;
	this.getValue = getValue;
	this.isHidden = isHidden;
	this.initializeValue = initializeValue;
	this.setFocus = setFocus;

	this.promptElementOriginalClassName = null;
	this.quartet = quartet;
	this.hidden = false;
//
//	added 20081006
//
	this.originallyEnabled = true;

	array = this.quartet.split("|");
	debug(2, "quartet:<" + quartet + ">, array.length:<" + array.length  +">");
	if (array.length != 4)
		throw "DataEntity Exception: Not a Quartet:<" + quartet + ">";

	this.id = array[0];
	this.validateFn = array[1];
	this.promptId =  array[2];
//
//	enabled/disabled values for a field list entry are as follows
//		enabled = now AND originally enabled 
//		disabled = now AND originally disabled 
//		originallyEnabled = now disabled and originally enabled
//		originallyDisabled = now enabled and originally disabled
//
	if (array[3] == "enabled")
	{
		this.enabled = true;
		this.originallyEnabled = true;
	}
	else if (array[3] == "disabled")
	{
		this.enabled = false;
		this.originallyEnabled = false;
	}
	else if (array[3] == "originallyEnabled")
	{
		this.enabled = false;
		this.originallyEnabled = true;
	}
	else if (array[3] == "originallyDisabled")
	{
		this.enabled = true;
		this.originallyEnabled = false;
	}

	this.element = document.getElementById(this.id);

	if (this.element == undefined)
		throw "DataEntity Exception: Element Not Found:<" + this.id + ">, quartet:<" + quartet + ">";

	this.type =  this.element.type;
	this.value = this.getValue();	//	this.element.value;


	this.validateCB = eval(this.validateFn);
	debug(3, "this.validateCB:<" + this.validateCB + ">");

	if (this.type == 'hidden')
		this.hidden = true;

	if (!this.hidden)
	{
		this.promptElement = document.getElementById(this.promptId);
		var str = trim(this.promptElement['innerHTML']);
		var strA = new Array();
		strA = str.split('<');
		this.labelText = trim(strA[0]);
/*
		for (var prop in this.promptElement)
		{
			str += prop + ':<' + this.promptElement[prop] + ">\\n";
		alert ('str:<' + str  + ">\\n" +  "strA[0]:<" + trim(strA[0]) + ">\\n");
		}
*/
				

		if (this.promptElementOriginalClassName == null)
		{
			this.promptElementOriginalClassName = this.promptElement.className;
		}
		if (this.type != "submit" && this.type != "button")
		{
//			this.prompt = replaceLineBreakWithSpace(this.promptElement.innerHTML);
			this.prompt = stripHtml(this.promptElement.innerHTML);
//			alert("this.prompt:<" + this.prompt + ">");
		}
		else
			this.prompt = undefined;
		if (this.enabled) // ???????????????/
			this.enableField();
		else 
			this.disableField();
	}
	

	this.validationError="";
	this.valid = true;
	debug(2, "this.id/validateFn/promptId/prompt/type/enabled/value:<" + this.id + ">\\n<" + 
		this.validateFn + ">\\n<" + 
		this.promptId + ">\\n<"  +
		this.prompt + ">\\n<" +
		this.type + ">\\n<" + 
		this.enabled + ">\\n<" + 
		this.originallyEnabled + ">\\n<" + 
		this.value + ">");
}
