//********************************************************
// dependencies.js
//
// Last Modified: 24 July 2006 11:00pm
//
// Description: Javascript provided to support identifier dependencies
// Copyright NetOptions Pty Ltd 2006
//
// Author: Tim Cromwell  Created On: 31 July 2006
//
//********************************************************

// update a particular dependency
function UpdateDependency(Children, Parents, URLPrefix, IdPrefix, OtherUpdates)
{
	// show the ajax throbber
	ShowThrobber();

	// get the value from the parents and update the child div with the new field..
	// do a split on the ',' character, iterating through the results..
	var ParentParts = new Array();
	ParentParts = Parents.toString().split(',');	

	var ParentValue = '';
	var ParentsURL = '';
	
	var NewID = '';
	var NewValue = '';
	
	// iterate through the parents and get the appropriate data
	for (var i = 0; i < ParentParts.length; i++)
	{
		// reset the newid and newvalue
		NewID = '';
		NewValue = '';
		
		// only append the parent to the url if it exists..
		if (document.getElementById(ParentParts[i]))
		{
			// if the current parent is the year, get the year2 field too.. if it exists..
			if (ParentParts[i].indexOf('year') != -1)
			{
				// create the year2 id
				NewID = ParentParts[i].replace(/year/, 'year2');
				
				// only continue if there is a value for it...
				if (document.getElementById(NewID) && document.getElementById(NewID).value && document.getElementById(NewID).value != '%')
				{
					// get the value and append to the url..
					NewValue = document.getElementById(NewID).value;
				}
			}
			
			// get the parent value and append it to the url
			ParentValue = document.getElementById(ParentParts[i]).value;
			
			// append the additional value if required..
			if (NewValue)
				ParentValue += '|##|' + NewValue;
			
			// append to the url..
			ParentsURL += '&ParentData[' + ParentParts[i].replace(IdPrefix,'') + ']=' + escape(encode64(ParentValue));
		}
	}

	// process other updates..
	DoOtherUpdates(OtherUpdates, ParentsURL);

	// split on the children and operate for each child..
	var ChildrenParts = new Array();
	ChildrenParts = Children.split(',');

	// debug the parent's url..
//	insertAtCaret(document.getElementById('debug_field'), '\n-------------- ' + GetTimeStamp() + '\n' + 'Processing Field' + '\n--------------\n');
//	insertAtCaret(document.getElementById('debug_field'), '\n--------------\n' + ParentsURL + '\n--------------\n');
//	alert(ParentsURL);
//	alert(ChildrenParts);

	// begin the iteration through the children and the appending of data..
//	ProcessChild (0, ChildrenParts, ParentsURL, URLPrefix, IdPrefix);
	
	// iterate through the children and get the appropriate data
	for (var i = 0; i < ChildrenParts.length; i++)
	{
		// create a new copy of the url
		var URL = URLPrefix + ParentsURL;
		var ChildField = ChildrenParts[i];

		// append the child to the url prefix
		ChildName = ChildField.replace(IdPrefix,'');
		URL += '&Child=' + ChildName;

		//24-08-2006::MZ
		//Retain the child value if possible!
		if(!document.getElementById(ChildField))
		{
			continue;
		}

		// set the child value...
		ChildValue = document.getElementById(ChildField).value;
		URL += '&ChildValue=' + encode64(ChildValue);
		ParentsURL += '&ParentData[' + ChildName + ']=' + encode64(ChildValue);
		URL += '&FormPrefix=' + IdPrefix;

		// handles the on success state
		var handlerfunc = function(t)
		{			
			// after a second close the throbber..
			setTimeout(function() 
			{
				// hide the ajax throbber
				HideThrobber();
			}, 1000);
		}
				
		// set the debug...
//		document.getElementById('debug_field').value += '\n-------------- ' + GetTimeStamp() + ' --------------\n' + 'Processing Field: ' + ChildField + '\nParents URL: ' + ParentsURL + '\n--------------\n';

		// update the child data..
//		if (i == (ChildrenParts.length - 1))
//			new Ajax.Updater(ChildField.concat('_wrapper'), URL, {evalScripts: true, onSuccess:handlerfunc});
//		else
//			new Ajax.Updater(ChildField.concat('_wrapper'), URL, {evalScripts: true});
			
//		if (i == (ChildrenParts.length - 1))
//			new Ajax.Updater(ChildField.concat('_wrapper'), URL, {evalScripts: true, onSuccess:handlerfunc});
//		else
		
		var handlerfunc2 = function(t)
		{
			if (t.responseText)
			{
//				alert(t.responseText);
				
				// ascertain the field div..
				var FieldParts = t.responseText.split("|###|");
				var FieldDiv = FieldParts[1];
				FieldDiv += '_wrapper';
				
				// set the content into the innerHTML..
				document.getElementById(FieldDiv).innerHTML = t.responseText;

				// evaluate and run all the internal scripts..
				setTimeout(function()
				{
					var Scripts = document.getElementById(FieldDiv).getElementsByTagName("script");
					for (var j = 0; j < Scripts.length; j++)
					{
						var ScriptItem = Scripts[j].innerHTML;
						eval(ScriptItem);
					}
				}, 500);
			}
		}	
		
		// if the field is a select item, count how many options are currently within it..
//		alert(document.getElementById(ChildField).tagName);
		if (document.getElementById(ChildField).tagName.toLowerCase() == 'select')
		{
			// get the count of options for the field..
			var ChildOptions = document.getElementById(ChildField).getElementsByTagName('option');
			
			// append to the url..
			URL += '&ChildFieldCount=' + ChildOptions.length;
		}
		
//		var FieldDiv = ChildField.concat('_wrapper');
		// make the request only - depending on the result we will update the div or not..
		new Ajax.Request(URL, {evalScripts:true, onSuccess:handlerfunc2});
	}
	
	// hide the throbber in case it isn't..
	if (!ThrobberHidden())
	{
		setTimeout(function() 
		{
			// hide the ajax throbber
			HideThrobber();
		}, 500);
	}
}

/*
// an iteration through the children data - this will set the appropriate ajax data..
function ProcessChild (ChildIndex, ChildrenParts, ParentsURL, URLPrefix, IdPrefix)
{
	// set the length of the ChildrenParts array.
	var ChildrenPartsLength = ChildrenParts.length;

	// create a new copy of the url
	var URL = URLPrefix + ParentsURL;
	var ChildField = ChildrenParts[ChildIndex];

	// append the child to the url prefix
	var ChildName = ChildField.replace(IdPrefix,'');
	URL += '&Child=' + ChildName;

	//24-08-2006::MZ
	//Retain the child value if possible!
	if (!document.getElementById(ChildField) && ChildIndex != ChildrenPartsLength)
		ProcessChild ((ChildIndex + 1), ChildrenParts, ParentsURL, URLPrefix, IdPrefix);
	else if (!document.getElementById(ChildField) && ChildIndex == ChildrenPartsLength)
		return;

	// set the child value
	ChildValue = document.getElementById(ChildField).value;
	URL += '&ChildValue=' + encode64(ChildValue);
	ParentsURL += '&ParentData[' + ChildName + ']=' + encode64(ChildValue);
	URL += '&FormPrefix=' + IdPrefix;

	// handles the on success state
	var handlerfunc = function(t)
	{			
		setTimeout(function() { 
			// hide the ajax throbber
			HideThrobber(); }, 1000);
	}
	
	// handles the next child process
	var nexthandlerfunc = function(t)
	{
		// process the next child
		ChildIndex += 1;
		ProcessChild (ChildIndex, ChildrenParts, ParentsURL, URLPrefix, IdPrefix);
	}

	document.getElementById('debug_field').value += '\n-------------- ' + GetTimeStamp() + ' --------------\n' + 'Processing Field: ' + ChildField + '\n--------------\n';

	// update the dependency field using ajax.. if the last field in dependency list.. don't do again!
	if (ChildIndex == ChildrenPartsLength)
		new Ajax.Updater(ChildField.concat('_wrapper'), URL, {evalScripts: true, onSuccess:handlerfunc});
	else
		new Ajax.Updater(ChildField.concat('_wrapper'), URL, {evalScripts: true, onSuccess:nexthandlerfunc});
}
*/


function DoOtherUpdates(OtherUpdates, ParentsURL)
{
	if(ParentsURL=='')
	{
		var ParentParts = new Array();
		var ParentsURL = '';

		// only append the parent to the url if it exists..
		if (document.getElementById('redbook_code'))
		{
			// get the parent value and append it to the url
			ParentValue = document.getElementById("redbook_code").value;
			ParentsURL += '&ParentData[redbook_code]=' + escape(encode64(ParentValue));
		}
		
	}
	
	//On some forms we need to do other ajax updates with this same data.
	//OtherUpdates will be a comma seperated list of div id's and url's seperated by pips.
	var OtherUpdates = OtherUpdates.toString().split(',');	
	for (var i=0; i< OtherUpdates.length; i++)
	{
		//Run the ajax update for each extra update item.
		UpdateItems = OtherUpdates[i].toString().split('|');
		new Ajax.Updater(UpdateItems[0], UpdateItems[1]+ParentsURL);
	}
}




function GetIdentifiersForNewspaperStory(DivName,URL)
{
	StoryType = document.getElementById('story_type').value;

//	alert(URL);
	URL += '&FormData[story_type]='+StoryType;
	new Ajax.Updater(DivName, URL);


	if(StoryType == 'REVIEW' || StoryType == 'HINT')
	{
		//Effect.BlindDown(DivName, {duration: 0.5});
		document.getElementById(DivName).style.display = 'block';

	}
	else
	{
		//Effect.BlindUp(DivName, {duration: 0.5});
		document.getElementById(DivName).style.display = 'none';

	}
}

function UpdateRange(Type, SourceDivName, TargetDivName)
{
	if(Type == "DropDown")
	{
		if(document.getElementById(TargetDivName).selectedIndex == 0)
			document.getElementById(TargetDivName).selectedIndex =  document.getElementById(SourceDivName).selectedIndex ;
	}
	else if(Type == "Text")
	{
		if(!document.getElementById(TargetDivName).value)
			document.getElementById(TargetDivName).value = document.getElementById(SourceDivName).value;
	}
}

/*
// depending on the field type selected - display or hide the drop down options..
function DisplayFieldTypeOptions()
{
// set the vars
var field_type = document.getElementById('field_type').value;
var option_div = document.getElementById('DropDownOptions');
var length_div = document.getElementById('FieldLengthOptions');

// if the selected field type is drop down and it is currently hidden - show
if (field_type == 'drop down')
{
Effect.BlindUp('FieldLengthOptions', {duration: 0.5});
Effect.BlindDown('DropDownOptions', {duration: 0.5});
option_div.style.display = 'block';
length_div.style.display = 'none';
}
// if the selected field type is not drop down and it is currently in view - hide
else if (field_type != 'drop down' && field_type != '')
{
if (option_div.style.display == 'block')
Effect.BlindUp('DropDownOptions', {duration: 0.5});
if (length_div.style.display == 'none')
Effect.BlindDown('FieldLengthOptions', {duration: 0.5});

option_div.style.display = 'none';
length_div.style.display = 'block';
}
else
{
Effect.BlindUp('DropDownOptions', {duration: 0.5});
Effect.BlindUp('FieldLengthOptions', {duration: 0.5});
option_div.style.display = 'none';
length_div.style.display = 'none';
}
}



function DisplayDropDownOptions()
{
// set the vars
var list_type = document.getElementById('drop_down_list').value;
var option_div = document.getElementById('CustomDropDownOptions');

// if the selected field type is drop down and it is currently hidden - show
if (list_type == 'identifier_list')
Effect.BlindDown('CustomDropDownOptions', {duration: 0.5});
// if the selected field type is not drop down and it is currently in view - hide
else if (list_type != 'identifier_list')
Effect.BlindUp('CustomDropDownOptions', {duration: 0.5});
}


function GetNextTableSequence(TableID)
{
// get the listing
var Listing = document.getElementById(TableID);

// ensure that this all happens within the table body tag
var ListingBody = Listing.getElementsByTagName('tbody');

// get elements within listing of type input..
var RowElements = ListingBody[0].getElementsByTagName("tr");
var CellElements = ListingBody[0].getElementsByTagName("td");

// default new key..
var NewKey = 0;
var ElementIndex = 0;

// test row elements..
if (RowElements.length)
{
	for (ElementIndex; ElementIndex < RowElements.length; ElementIndex++)
		if (RowElements[ElementIndex].id.substring(0, 13) == 'generated_row')
			NewKey = parseInt(RowElements[ElementIndex].id.replace('generated_row_', ''));
}

// iterate and get new key..
if (NewKey == 0 && CellElements.length)
{
	ElementIndex = 0;		
	for (ElementIndex; ElementIndex < CellElements.length; ElementIndex++)
		NewKey = parseInt(CellElements[ElementIndex].className.replace('generated_cell_', ''));
}

// increment the key
NewKey++;

return (NewKey);
}

function AddNewTableRow(NewSequence, TableID, Action, DataType, URLSuffix)
{
	// create new table row..
	var Table = document.getElementById(TableID);
	var TableBody = Table.getElementsByTagName('tbody');
	var NewRow = document.createElement('tr');
	NewRow.id = 'generated_row_'.concat(NewSequence);
	TableBody[0].appendChild(NewRow);

	// put contents (from ajax)
	var URL = 'index.htm?Action=' + Action + '&DataType=' + DataType + '&Input=' + NewSequence;
	if (URLSuffix)
		URL += URLSuffix;

	// run the ajax update
	new Ajax.Updater('generated_row_'.concat(NewSequence), URL, {});
}


// Process the sortable sequencing page
function ProcessSortables(URLPrefix)
{			
	var SortList = document.getElementById('sort_list');
	var TrashList = document.getElementById('trash_list');

	// get all the sortable items and their order
	var RowElements = SortList.getElementsByTagName('li');
	var ElementIndex = 0;

	// if there are any items in the sortable list
	if (RowElements.length)
	{
		for (ElementIndex; ElementIndex < RowElements.length; ElementIndex++)
			URLPrefix += '&Sorted[' + ElementIndex + ']=' + parseInt(RowElements[ElementIndex].id.replace('sort_item_', ''));
	}

	// get all the trash items
	var RowElements = TrashList.getElementsByTagName('li');
	var ElementIndex = 0;

	// if there are any items in the sortable list
	if (RowElements.length)
	{
		for (ElementIndex; ElementIndex < RowElements.length; ElementIndex++)
			URLPrefix += '&Trashed[' + ElementIndex + ']=' + parseInt(RowElements[ElementIndex].id.replace('sort_item_', ''));
	}

	// make the ajax request to send the data..
	//	new Ajax.Request(URLPrefix, {});
	new Ajax.Updater('sortable_result', URLPrefix, {});
	// send the data to the database save script.. 
	//alert(URLPrefix);
}
*/



/* end of file */
