///////////////////////////////////////////////////////////////
//At the time of final testing, it became necessary to combine
//four different .js files into this one file, hence the name
//cwFRtool_ALL.js
///////////////////////////////////////////////////////////////


//cwFRtool_io.js
//(c) David D'Amico 2004

//OF GREAT IMPORTANCE:
//here are the paths to the stuff you want:
//this.overframe.main.pageID
//this.overframe.main.document.cwf_inputForm
//...and so on


///////////////////////////////////////////////////////////////
//
// Function Name: killCookies()
//
// Description: As the name implies, this function kills the
//				cookies for the entire tool.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Sets document.cookie to a blank string, with
//				 an expiration date in the past.
//
///////////////////////////////////////////////////////////////
//TODO this could be a bit more graceful, ESPECIALLY
//if there are cookies used else on the CWF site
///////////////////////////////////////////////////////////////
function killCookies()
{
	//alert("FUNCTION: killCookies()");
	//set a date in the past
	var kill_date = new Date("January 1, 1970");
	
	//then set the cookie data to "", and its expiration
	//date to the date in the past that we just created.
	document.cookie = ";expires=" + kill_date;
}


///////////////////////////////////////////////////////////////
//
// Function Name: checkCookie()
//
// Description: Generic function for checking if there is already
//				data in the cookie, and if that data pertains to
//				the current page.
//
// Inputs:		none
//
// Outputs:		Boolean
//
// Side Effects: none
//
///////////////////////////////////////////////////////////////
function checkCookie()
{
	//alert("FUNCTION: checkCookie() " + document.cookie);
	//if the cookie exists AND the cookie is not blank AND
	//the cookie contains the current page ID, then return
	//true
	if (document.cookie && (document.cookie !== ""))
	{
		if (document.cookie.indexOf(pageID) > -1 || document.cookie.indexOf("CWF_bookmark") > -1)
		{ return true; }
	}
	
	//if not, then return false.
	else
	{ return false; }
}


//



///////////////////////////////////////////////////////////////
//
// Function Name: page_onLoad()
//
// Description: Runs when a page first loads.  If there is
//				already cookie data, user is prompted to choose
//				between returning where they left off, or starting
//				fresh.  if not, we go to the first page.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: If user requests data restoration, calls
//				 populateFormFromCookie().  Calls killCookies()
//				 if user requests it.  Calls setFieldEvents().
//
///////////////////////////////////////////////////////////////
//TODO see below
///////////////////////////////////////////////////////////////
function page_onLoad()
{
	var temp_bookmarkHolder, confirmText;
	var target = eval(pathToContentFromTop + containerFormName);
	//alert("FUNCTION: page_onLoad | pageID = " + pageID + " | firstPageID = " + firstPageID);
	//alert(top.document.cookie);
	//alert(this.overframe.main.pageID);

	//if this is the first page...
	if (this.overframe.main.pageID == firstPageID)
	{
		//alert("FIRST PAGE!");
		this.CWF_dataObject = new dataObject();
		this.CWF_dataObject.importCookie();
		//alert(this.CWF_dataObject.getValue("CWF_bookmark"));
		
		//if there is a bookmark
		if (this.CWF_dataObject.getValue("CWF_bookmark") != false && this.CWF_dataObject.getValue("CWF_bookmark").indexOf("/index.html") == -1)
		{
			confirmText = "It looks like you've been here before.\nWould you like to try to restore your data\nand go to the page where you left off,\nor would you like to start from the beginning?";
			//alert("FIRST PAGE: " + checkCookie());
			
			//if there's already cookie data, check whether
			//the user wants to return to the place where they
			//left off.
			if (confirm(confirmText))
			{ returnToBookmark(); }

			//if they do not, kill the cookies
			//TODO we would also go to the first page here
			else
			{ killCookies(); }

		}

		//NO ELSE, we just don't do anything else here if there's no cookie
		
	}


	//okay, so maybe we're not on the first page, maybe it's just a content
	//page.  in that case, first we need to make sure that we're not on
	//the tail end of a returnToBookmark request...
	else
	{
		//alert("!!! " + this.CWF_dataObject);
		if (this.CWF_dataObject.tempBookmark)
		{
			temp_bookmarkHolder = this.CWF_dataObject.tempBookmark;
			delete this.CWF_dataObject.tempBookmark;
			this.overframe.main.document.location = temp_bookmarkHolder;
			return;
		}

		//alert(checkCookie());
		//if we're not, then we've gotta populate the forms
		if (checkCookie())
		{ this.CWF_dataObject.refreshDataObjectFromCookie(); populateFormFromCookie(); }

		//set the bookmark
		setBookmark();

		//set the onchange event for the input fields
		setFieldEvents();
	}


}


///////////////////////////////////////////////////////////////
//
// Function Name: setBookmark()
//
// Description: 
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function setBookmark()
{
	//alert("setBookmark");

	var bookmark_frameset = this.overframe.document.location;
	var bookmark_frame = this.overframe.main.document.location;

	var tempKey = "CWF_bookmark";
	var tempValue = escape(bookmark_frameset) + cookieDelimiter + bookmark_frame + attributesString;
	//var bookmarkCookieString = "CWF_bookmark=" + escape(bookmark_frameset) + cookieDelimiter + bookmark_frame + attributesString;

	this.CWF_dataObject.setValue(tempKey, tempValue);
	this.CWF_dataObject.commitCookie();
	//alert("setBookmark" + document.cookie);
}


///////////////////////////////////////////////////////////////
//
// Function Name: returnToBookmark()
//
// Description: 
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function returnToBookmark()
{
	var bookmarkArray = this.CWF_dataObject.getValue("CWF_bookmark").split(cookieDelimiter);
	//alert("returnToBookmark: " + bookmarkArray);
	this.CWF_dataObject.tempBookmark = bookmarkArray[1];
	this.overframe.location = bookmarkArray[0];
}


///////////////////////////////////////////////////////////////
//
// Function Name: multiPagePrint()
//
// Description: For printing pages whose input is spread over
//				several pages.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Pops up a window
//
///////////////////////////////////////////////////////////////
//TODO write it
///////////////////////////////////////////////////////////////
function multiPagePrint(url)
{
	window.open(url, "printerPopup");
}

///////////////////////////////////////////////////////////////
//
// Function Name: printSinglePage()
//
// Description: For printing a single page.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Pops up a window
//
///////////////////////////////////////////////////////////////
//TODO make sure printing straight from the page is okay with
//everybody.
///////////////////////////////////////////////////////////////
function printSinglePage()
{
	//alert("printSinglePage");
	this.overframe.main.print();
}


///////////////////////////////////////////////////////////////
//
// Function Name: populateFormFromCookie()
//
// Description: Populates page's forms with cookie data.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: none
//
///////////////////////////////////////////////////////////////
//TODO offload the actual write to the form onto a form setter
//function.
///////////////////////////////////////////////////////////////
function populateFormFromCookie()
{
	//alert("FUNCTION: populateFormFromCookie()");
	var tempKeyValueArray = [];
	var parsedCookieArray = readFromCookie();
	//alert("FUNCTION: populateFormFromCookie: " + parsedCookieArray.toString());
	
	if (parsedCookieArray.toString() != "false")
	{ writeForm(parsedCookieArray);	}

	else
	{ /*THEN there was an error, so make some error handling*/ }
}


///////////////////////////////////////////////////////////////
//
// Function Name: readFromCookie()
//
// Description: Reads the cookie data, parses it most of the way
//				(though not all the way to key/value pairs), then
//				passes an array back to whoever asked for info from
//				the cookie.
//
// Inputs:		none
//
// Outputs:		tempDataArray: Array
//				Array containing cookie data transformed into
//				indices	each containing a k/v pair delimited by
//				keyValuePairDelimiter
//
// Side Effects: none
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function readFromCookie()
{
	//alert("FUNCTION: readFromCookie()");

	//declaring the container for the "raw cookie"
	var pageValues = "";
	var cookieDoughArray = [];
	var tempDataArray = [];
	var cookieName = this.overframe.main.pageID;

	if (checkCookie())
	{
		//first we get the cookie value for this page from the
		//data object.
		//alert("asking for: " + cookieName);
		pageValues = this.CWF_dataObject.getValue(cookieName);

		//and then unescape it and split it at the cookieDelimiter
		//(located in "/js/cwfFRtool_variables.js") to get a series
		//of key/value pairs.
		tempDataArray = unescape(pageValues).split(cookieDelimiter);
		//alert("2: tempDataArray = " + tempDataArray);

		return tempDataArray;
	}

	else
	{ return false; }
}



///////////////////////////////////////////////////////////////
//
// Function Name: writeToCookie()
//
// Description: Writes user input to the cookie(s).
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Calls readForm, writes data to a cookie.
//
///////////////////////////////////////////////////////////////
//TODO keep in mind if this starts breaking that scope may be
//the issue... since the function may have been sent down to
//page scope when it was assigned to the form field in
//setFieldEvents
///////////////////////////////////////////////////////////////
function writeToCookie()
{
	//alert("FUNCTION: writeToCookie()");

	var cookieName = this.overframe.main.pageID;
	var attributes = attributesString;
	var dataArray = readForm();
	//alert("dataArray: " + dataArray);
	var dataString = "";

	for (i = 0; i < dataArray.length; i++)
	{
		//if commented out for now because the printer stuff requires
		//that even blank fields have a value
		dataString += escape(dataArray[i]) + cookieDelimiter;
	}
	
	dataString = dataString.substring(0, dataString.lastIndexOf(cookieDelimiter));
	//alert("dataString: " + dataString);

	this.CWF_dataObject.setValue(cookieName, dataString);
	this.CWF_dataObject.commitCookie();
	//document.cookie = cookieName + "=" + dataString + attributesString;
	//alert("writeToCookie(): COOKIE: " + document.cookie);

}


///////////////////////////////////////////////////////////////
//
// Function Name: setFieldEvents()
//
// Description: crawls the container form and sets all the
//				fields' onchange events to writeToCookie()
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Adds an "onchange" attribute to input nodes.
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function setFieldEvents()
{
	//alert("FUNCTION: setFieldEvents");
	var formTarget = eval(pathToContentFromTop + containerFormName);
	//alert(this.overframe.main.document.cwf_inputForm);
	
	if (formTarget.length)
	{
		for (n = 0; n < formTarget.length; n++)
		{
			//alert(formTarget[n]);
			if (formTarget[n].name)
			{
				//alert(formTarget[n].name);
				if (formTarget[n].name.indexOf(inputNamePrefix) > -1)
				{ formTarget[n].onchange = fieldOnChange }
			}
		}
	}
}


///////////////////////////////////////////////////////////////
//
// Function Name: fieldOnChange()
//
// Description: since the onchange event for the form fields
//				has local scope to that frame, we can't call
//				the writeToCookie function directly, but must
//				instead call it in parent.parent via this
//				function.
//
// Inputs:		none
//
// Outputs:		none
//
// Side Effects: Calls writeToCookie
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function fieldOnChange()
{
	//alert("fieldOnChange");
	parent.parent.writeToCookie();
}


///////////////////////////////////////////////////////////////
//
// Function Name: writeForm()
//
// Description: This function is the access point for writing
//				data to cwf_inputForm.
//
// Inputs:		dataToWrite: Array
//				expects an array where each index contains
//				a string containing a k/v pair delimited by
//				keyValuePairDelimiter
//
// Outputs:		none
//
// Side Effects: Sets the values of form elements.  Calls
//				 checkBoxSetState, radioSetState
//				 
//
///////////////////////////////////////////////////////////////
//TODO add error handling to deal with keys that do not exist
//in the target form
///////////////////////////////////////////////////////////////
function writeForm(dataToWrite)
{
	//alert("dataToWrite: " + dataToWrite);

	//initialize variables.
	var formTarget = eval(pathToContentFromTop + containerFormName);
	var inputType, nodeType, tempKey, tempValue;
	var tempArray = [];

	//now loop through dataToWrite, and set cwf_inputForm.KEY-X to
	//cwf_inputForm.VALUE-X
	for (n = 0; n < dataToWrite.length; n++)
	{
		//alert("1: " + dataToWrite[n]);
		//alert("2: " + dataToWrite[n].split(keyValuePairDelimiter));
		
		//set temp values for this target (key, value, and reset tempArray to
		//blank, as the checkbox stream expects a blank array there.
		tempKey = dataToWrite[n].split(keyValuePairDelimiter)[0];
		tempValue = dataToWrite[n].split(keyValuePairDelimiter)[1];
		tempArray = [];

		//alert("dataToWrite[" + n + "]: tempKey = " + tempKey + " | tempValue = " + tempValue);
		//alert(overframe.main.document.cwf_inputForm);

		//depending on whether we're looking at a straight input or a
		//series of checkboxes/radiobuttons, we get the type and nodeName
		//values from different places.
		//SO, if .type exists...
		if (formTarget[tempKey].type)
		{
			inputType = formTarget[tempKey].type;
			nodeType = formTarget[tempKey].nodeName;
		}
		
		//but if it DOESN'T, then just take type and nodeName from element
		//0 in that series.
		else /*if (formTarget[tempKey].type == "undefined")*/
		{
			//alert("!!! undefined");
			inputType = formTarget[tempKey][0].type;
			nodeType = formTarget[tempKey][0].nodeName;
		}

		//alert("inputType = " + inputType + " | nodeType = " + nodeType);

		//now we stream through this switch, doing the different things we
		//need to do to set the different types of inputs.
		switch (nodeType)
		{
			case "INPUT":

				switch (inputType)
				{
					case "radio":
						//write radio
						//alert("RADIO: tempKey = " + tempKey + " | tempValue = " + tempValue);
						radioSetState(tempKey, tempValue);
						break;
					
					case "checkbox":
						//write checkbox
						//alert("checkbox: tempKey = " + tempKey + " | tempArray = " + tempArray);
						tempArray = tempValue.split(",");
						checkBoxSetState(tempKey, tempArray);
						break;

					case "text":
						//write as a textfield
						formTarget[tempKey].value = tempValue;
						break;
					
					default:
						//default case
						//this would be an error, wouldn't it?
				}
			break;
			
			case "SELECT":
				//parse as a select
				formTarget[tempKey].value = tempValue;
				break;

			case "TEXTAREA":
				//parse as a textarea
				formTarget[tempKey].value = tempValue;
				break;

			default:
				//default case
				//this would be an error, wouldn't it?
				alert("ERROR COLLECTING DATA FROM FORM!!!");
		}
	}
}


///////////////////////////////////////////////////////////////
//
// Function Name: readForm()
//
// Description: This function is the access point for reading
//				data from cwf_inputForm.
//
// Inputs:		none
//
// Outputs:		resultsArray: Array
//				returns an array containing the data that was
//				collected from the form.
//
// Side Effects: Reads the values of form elements.  Calls
//				 checkBoxGetState, radioGetState
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function readForm()
{
	
	//initialize some variables.
	//formTarget is "document" + the default name for the container
	//form, which is set in the variables .js file.
	var formTarget = eval(pathToContentFromTop + containerFormName);
	//alert("formTarget = " + formTarget);
	//alert(eval(formTarget));

	//inputType and nodeType are going to be holders for those
	//particular values for each form element as we loop through
	//the form.
	var inputType, nodeType;

	//
	var temp_keyValueStringToAppend = "";
	var resultsArray = [];
	var tempResultsCheckArray = [];
	var resultsCheckCounter = 0;

	for (n = 0; n < formTarget.length; n++)
	{
		inputType = formTarget[n].type;
		nodeType = formTarget[n].nodeName;
		temp_keyValueStringToAppend = "";

		//alert(formTarget[n].name + " | " + inputType + " | " + nodeType);

		switch (nodeType)
		{

			case "INPUT":

				switch (inputType)
				{
					case "radio":
						//alert(formTarget[n].name + " is a radiobutton series");

						//parse as a radio button via getter
						temp_keyValueStringToAppend = radioGetState(formTarget[n].name);
						break;
					
					case "checkbox":
						//alert(formTarget[n].name + " is a checkbox series");

						//parse as a checkbox via getter
						temp_keyValueStringToAppend = checkBoxGetState(formTarget[n].name);	
						break;

					case "text":
						//parse as a textfield
						//just rip out the value and serve it up
						//alert(formTarget[n].name + " is a texfield");
						temp_keyValueStringToAppend  = formTarget[n].value;
						break;
					
					default:
						//default case
						//this would be an error, wouldn't it?
				}
			break;
			
			case "SELECT":
				//parse as a select
				//rip out value and serve it up
				//alert(formTarget[n].name + " is a select");
				temp_keyValueStringToAppend  = formTarget[n].value;
				break;

			case "TEXTAREA":
				//parse as a textarea
				//rip out value and serve it up
				//alert(formTarget[n].name + " is a textarea");

				//INSERT LENGTH CHECK HERE
				if (this.overframe.main.textAreaMaxLength)
				{ temp_keyValueStringToAppend = textAreaLengthCheck(formTarget[n].value); }

				else
				{ temp_keyValueStringToAppend  = formTarget[n].value; }

				break;

			default:
				//default case
				//this would be an error, wouldn't it?
				alert("ERROR COLLECTING DATA FROM FORM!!!");
		}

		resultsArray.push(formTarget[n].name + keyValuePairDelimiter + temp_keyValueStringToAppend);
		
	}

	//alert("resultsArray before: " + resultsArray);
	return resultsArray = cleanDuplicatesFromFormResultsArray(resultsArray);
}


///////////////////////////////////////////////////////////////
//
// Function Name: textAreaLengthCheck()
//
// Description: Checks the length of a user's input
//
// Inputs:		value: String
//				the value whose length we're going to check
//
// Outputs:		none
//
// Side Effects: an alert
//				 
//
///////////////////////////////////////////////////////////////
//TODO discuss what should happen for user here.
///////////////////////////////////////////////////////////////
function textAreaLengthCheck(value)
{
	var textAreaMaxLength = this.overframe.main.textAreaMaxLength;
	var escapedValue = escape(value);
	var textSnippet = value.slice(0, 10);
	var confirmText = "You have entered more text in one of the text fields\nthan we have the capacity to store for you.\nYour entry in the field which begins '" + textSnippet + "' will not be saved until its length is less than " + textAreaMaxLength + " characters."

	if(escapedValue.length > textAreaMaxLength)
	{
		alert(confirmText);
		return value.slice(0, textAreaMaxLength);
	}

	return value;
}


///////////////////////////////////////////////////////////////
//
// Function Name: cleanDuplicatesFromFormResultsArray()
//
// Description: Because a radio/cb series is coded as a series
//				of inputs with the same name attribute, the
//				form reading function above (readForm) will
//				produce a resultsArray which contains duplicate
//				data.  This function runs through the array, and
//				removes those duplicates so that we're not wasting
//				cookie space on duplicate data.
//
// Inputs:		resultsArray: Array
//				expects an array where each index contains
//				a string containing a k/v pair delimited by
//				keyValuePairDelimiter
//
// Outputs:		newResultsArray: Array
//				a new copy of the resultsArray, with the
//				duplicates removed
//
// Side Effects: none
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function cleanDuplicatesFromFormResultsArray(resultsArray)
{

	var n = 0;
	var secondCounter = 0;
	var newResultsArray = [];

	while (n < resultsArray.length)
	{
		
		if (n > 0)
		{
			if (resultsArray[n] != resultsArray[(n - 1)])
			{
				//transfer to second array
				newResultsArray[secondCounter] = resultsArray[n];

				//increment secondCounter
				secondCounter++;
			}
		}
		else if (n == 0)
		{
			//transfer to second array
			newResultsArray[secondCounter] = resultsArray[n];

			//increment secondCounter
			secondCounter++;
		}

		n++;
		
	}

	return newResultsArray;
	
}


///////////////////////////////////////////////////////////////
//
// Function Name: checkBoxGetState()
//
// Description: Getter for checkboxes.
//
// Inputs:		inputName: String
//				expects a string containing the name of the input
//				that is its target.
//
// Outputs:		resultsArray: String
//				Used in the function as an array, then returned
//				as a string of comma delimited values.
//
// Side Effects: none
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function checkBoxGetState(inputName)
{
	var target = eval(pathToContentFromTop + containerFormName + "." + inputName);
	var resultsArray = [];

	for (i = 0; i < target.length; i++)
	{
		if (target[i].checked)
		{ resultsArray.push(target[i].value) }
	}

	//alert("checkbox resultsArray = " + resultsArray.length);
	return resultsArray.toString();
}


///////////////////////////////////////////////////////////////
//
// Function Name: checkBoxSetState()
//
// Description: Setter for checkboxes.
//
// Inputs:		inputName: String
//				expects a string containing the name of the input
//				that is its target.
//				
//				newStateArray: Array
//				expects an array containing the desired new state.
//				Values in this array are compared against the values
//				of the actual form elements, and matches are then
//				set to "checked".
//
// Outputs:		none
//
// Side Effects: none
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function checkBoxSetState(inputName, newStateArray)
{
	var target = eval(pathToContentFromTop + containerFormName + inputName);

	//reset everything to unchecked
	for (i = 0; i < target.length; i++)
	{ target[i].checked = false; }

	//loop through array, check the boxes whose value matches
	//an element in the array
	for (j in newStateArray)
	{
		for (k = 0; k < target.length; k++)
		{
			if (newStateArray[j] == target[k].value)
			{ target[k].checked = true; }
		}
	}
}


///////////////////////////////////////////////////////////////
//
// Function Name: radioGetState()
//
// Description: Getter for radiobuttons.
//
// Inputs:		inputName: String
//				expects a string containing the name of the input
//				that is its target.
//
// Outputs:		foundValue: String
//				The value attribute of the radiobutton which
//				is checked.
//
// Side Effects: none
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function radioGetState(inputName)
{
	//alert(inputName);
	
	var foundValue;
	var target = eval(pathToContentFromTop + containerFormName + "." + inputName);

	for (i = 0; i < target.length; i++)
	{
		if (target[i].checked == true)
		{ foundValue = target[i].value; }
	}

	if (foundValue != null)
	{
		//alert("the value of this target is " + foundValue);
		return foundValue;
	}

	else
	{
		//alert("none checked");
		return null;
	}
}


///////////////////////////////////////////////////////////////
//
// Function Name: radioSetState()
//
// Description: Setter for radiobuttons.
//
// Inputs:		inputName: String
//				expects a string containing the name of the input
//				that is its target.
//
//				newState: String
//				A string containing the value that we would like
//				to set to "checked".
//
// Outputs:		none
//
// Side Effects: none
//				 
//
///////////////////////////////////////////////////////////////
//TODO
///////////////////////////////////////////////////////////////
function radioSetState(inputName, newState)
{
	target = eval(pathToContentFromTop + containerFormName + "." + inputName);
	if (newState != null && newState != "null")
	{
		for (i = 0; i < target.length; i++)
		{
			if (target[i].value == newState)
			{ target[i].checked = true; }
		}
	}
}




//cwfFRtool_variables.js
//(c) David D'Amico 2004

//this will go in front of every cookie name we use,
//to ensure that our names never conflict with names
//elsewhere in the site.
var cookieNamePrefix = "CWFFRtool_";


//as an extra line of defense against crossover between
//OUR data and data elsewhere in the site, the cookie
//will be limited to the path where the tool is located.
//this string contains that path
var domainLimiterPath = ""//;path=/cwftool-test";


//this string will serve as the delimiter between variables
//in our cookie data.  it is designed to be a unique string
//that noone would ever type as their response.  What do YOU
//think?
var cookieDelimiter = "^CDL^";


var keyValuePairDelimiter = "#KV#";


//this section gets the 'expires' attribute of the cookie
//ready for placement.  The code here sets the cookie's
//expiration date one year from its initial write.
var now = new Date();
now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365);
var expiresString = ";expires=" + now;


//the full string of attributes that will be tacked onto the
//end of our cookie(s).
var attributesString = expiresString + domainLimiterPath;


//declaration of the data container array.  cookie data ends
//up here when it's pulled back.
var localDataArray = [];
var localDataObject = new Object();


//holds the text that's going to get written into the
//printer popup
var printerWindowString;


//prefix for the name of all the input fields
var inputNamePrefix = "cwf_input";
//var containerFormName = "cwf_inputForm";
var pathToContentFromTop = "this.overframe.main.document.";
var containerFormName = "cwf_inputForm";

//some variables that are involved in the "bookmark"
var firstPageID = "firstPage";
var bookmark_frameset = "";
var bookmark_frane = "";
var tempBookmark = "";




//printPopup.js REMOVED



//cwFRtool_dataObject.js
//(c) David D'Amico 2004

dataObject = function () { this.dataNode = new Object(); }
dataObject.prototype = new Object();


dataObject.prototype.importCookie = function ()
{
	//alert("DATA OBJECT FUNCTION: importCookie\nimporting this cookie: " + document.cookie);
	var temp_kvParseArray = [];
	var temp_cookieParseKey, temp_cookieParseValue;
	var temp_cookieDataArray = [];

	var cookieDough = this.getCookie();
	
	if (cookieDough !== false)
	{
		temp_cookieDataArray = cookieDough.split("; ");

		for (n = 0; n < temp_cookieDataArray.length; n++)
		{
			temp_kvParseArray = temp_cookieDataArray[n].split("=");
			temp_cookieParseKey = temp_kvParseArray[0];
			//alert(escape(temp_cookieParseKey));
			temp_cookieParseValue = unescape(temp_kvParseArray[1]);

			//alert(temp_cookieParseKey + " = " + temp_cookieParseValue);

			//TODO shift this off onto setValue
			this.setValue(temp_cookieParseKey, temp_cookieParseValue);
			//alert("importCookie: KEY: " + this.dataNode[temp_cookieParseKey][0]);
			//alert("importCookie: VALUE: " + this.dataNode[temp_cookieParseKey][1]);
			//alert("importCookie: this.dataNode[" + temp_cookieParseKey + "] = " + this.dataNode[temp_cookieParseKey]);
		}
	}

	else
	{ /*alert("no cookie found")*/ };
}


dataObject.prototype.refreshDataObjectFromCookie = function ()
{
	this.importCookie();
}


dataObject.prototype.commitCookie = function ()
{
	//alert(this.dataNode);
	for (x in this.dataNode)
	{
		//alert("!!commiting: " + this.dataNode[x]);
		document.cookie = this.dataNode[x][0] + "=" + this.dataNode[x][1] + attributesString;
	}
	//alert("COMMITTED COOKIE: " + document.cookie);
}


dataObject.prototype.getCookie = function ()
{
	if (document.cookie && (document.cookie !== ""))
	{
		if (document.cookie.indexOf(pageID) > -1 || document.cookie.indexOf("CWF_bookmark") > -1)
		{ return document.cookie; }
	}
	
	//if not, then return false.
	else
	{ return false; }
}



dataObject.prototype.setValue = function(key, value)
{
	this.dataNode[key] = new Array(key, value);
	//alert("SETVALUE: " + this.dataNode[key]);
}


dataObject.prototype.getValue = function(key)
{
	//alert("GETVALUE!: " + document.cookie);
	//alert(this.dataNode.section4_p02-1);

	if (this.dataNode[key])
	{ return this.dataNode[key][1]; }

	else
	{ return false; }
}