/**
 * @author david.ames
 */

/*
 * Global variable to track dirty components.
 * A 'dirty' component is one who's value has been changed by the user (via onchagne event)
 * 
 * If a component is not regerested as having a
 */

var g_dirtyComponents = {};
var g_contentItems = {};
g_contentItems.byControlRef = {};


 function setComponentDirty(componentReference, isDirty)
 {
 	g_dirtyComponents[componentReference] = isDirty;	
 }
 
 function isComponentDirty(componentReference)
 {
 	return g_dirtyComponents[componentReference];	
 }
 
 function contentLoadingSelectControl_onChange(caller, componentReference, answerFormat)
 {
 	var oForm = caller.form;
 	if (caller.value!='')
	{
		if (isComponentDirty(componentReference)&&getComponentValue(oForm, componentReference, answerFormat))
		{
				if (confirm('This will override your text updates below. Do you wish to continue?'))
				{			
					setComponentValue(oForm, componentReference,  getContentItem(componentReference, caller.value), answerFormat);
					setComponentDirty(componentReference, false);	
				}			
		}
		else
		{
			setComponentValue(oForm, componentReference,  getContentItem(componentReference, caller.value), answerFormat);
			setComponentDirty(componentReference, false);			
		}
		
	}
 }
 
 
  function contentLoadingRadioControl_onChange(caller, componentReference, answerFormat)
  {
 	var oForm = caller.form;
 	if (caller.value!='')
	{
		if (isComponentDirty(componentReference)&&getComponentValue(oForm, componentReference, answerFormat))
		{
				if (confirm('This will override your text updates below. Do you wish to continue?'))
				{			
					setComponentValue(oForm, componentReference,  getContentItem(componentReference, caller.value), answerFormat);
					setComponentDirty(componentReference, false);	
				}			
		}
		else
		{
			setComponentValue(oForm, componentReference,  getContentItem(componentReference, caller.value), answerFormat);
			setComponentDirty(componentReference, false);			
		}
		
	}
 }
 
 
   function contentLoadingCheckboxControl_onChange(caller, componentReference, answerFormat)
  {
 	var oForm = caller.form;
	var newValue = '';
	
 	if (caller.value!='')
	{
		if (isComponentDirty(componentReference)&&getComponentValue(oForm, componentReference, answerFormat))
		{
				if (confirm('This will override your text updates below. Do you wish to continue?'))
				{	
					if (caller.checked)	
					{
						appendTextToComponent(oForm, componentReference, getContentItem(componentReference, caller.value), answerFormat);							
					}
					else
					{
						removeTextFromComponent(oForm, componentReference, getContentItem(componentReference, caller.value), answerFormat);												
					}
					
					setComponentDirty(componentReference, false);	
				}			
		}
		else
		{
			if (caller.checked)	
			{
				appendTextToComponent(oForm, componentReference, getContentItem(componentReference, caller.value), answerFormat);							
			}
			else
			{
				removeTextFromComponent(oForm, componentReference, getContentItem(componentReference, caller.value), answerFormat);												
			}
			setComponentDirty(componentReference, false);			
		}
		
	}
 }
 
function getContentItem(componentReference, contentItemID)
{
	return g_contentItems.byControlRef[componentReference][contentItemID]; 
}

 
 
function appendTextToComponent(oForm, reference, value, answerFormat)
{
	var tmpValue = getComponentValue(oForm, reference, answerFormat) ;
	if (tmpValue!='')
	{
		if (answerFormat == 'paragraph') 
		{
			tmpValue = tmpValue + '\r\r';
		}
		if (answerFormat == 'wysiwyg') 
		{
			tmpValue = tmpValue + '<br>';
		}		
	}	
	tmpValue = tmpValue + value;
	setComponentValue(oForm, reference, tmpValue, answerFormat);
}

function removeTextFromComponent(oForm, reference, value, answerFormat)
{	
	var componentValue = getComponentValue(oForm, reference, answerFormat);
	var newValue = componentValue.replace(value, '');


	if (answerFormat=='paragraph')
	{
		var componentValue = getComponentValue(oForm, reference, answerFormat);
		var newValue = componentValue.replace(value, '');		
	}
	if (answerFormat == 'wysiwyg') 
	{
		//String extra line feeds off the existing value and the string we want to search for
		//to improve reliability.
		var nwValue = value.replace(/(\r\n)/g, "");
		var nwComponentValue = componentValue.replace(/(\r\n)/g, "");
		
		if (nwComponentValue.indexOf(nwValue)==-1)
		{
			alert('Could not automaticly remove the selected item from the text box.  Please remove it manually.');
		}
		var newValue = nwComponentValue.replace(nwValue, '');
	}
	
	setComponentValue(oForm, reference, newValue, answerFormat);
	
}

function setComponentValue(oForm, reference, value, answerFormat)
{
	
	if (answerFormat=='paragraph')
	{
		oForm[reference].value = value;		
	}
	if (answerFormat == 'wysiwyg') 
	{
	 	var oFCKeditor = FCKeditorAPI.GetInstance(reference);
		oFCKeditor.SetHTML(value); 
	}
	
}
 

function getComponentValue(oForm, reference, answerFormat)
{
	if (answerFormat=='paragraph')
	{		
		return oForm[reference].value;		
	}
	
	if (answerFormat == 'wysiwyg') 
	{
	
	 var oFCKeditor = FCKeditorAPI.GetInstance(reference);	 
	 return oFCKeditor.GetData();
    	
	}


}
