var formFields = new Object;
var formSubmitActions = new Array();

function ajaxFieldUpdate (fieldID, newFieldID, url, params) {
	
	var field = xGetElementById(fieldID + '_dataArea');
	field.innerHTML = "<div style=\"font-weight:500; text-size:13pt; height: 27px; padding-left: 5px;\"> <img src=\"/sData/img/spin.gif\"> Loading.....</div>"
	var newField = xGetElementById(newFieldID);
	
	var thisdate = new Date();
	var randString = Math.random() + '.' + thisdate.getTime();
	
//	alert('Url : ' + url);
//	alert('Params : ' + params);
	
	new Ajax.Updater(fieldID + '_dataArea', url,
	{
		parameters: params+ '&rand='+ randString + '&newValue=' + newField.value,
		evalScripts:true, asynchronous:true
	});
	
	
}

function submitTheForm (inForm) {

	var formOK = true;

	// Run through the formSubmitActions array and execute any actions
	for ( i=0; i < formSubmitActions.length; i++)
	{
		var current = formSubmitActions[i];

		//alert('Found Item Type : ' + current.type );

		if (current.type == 'multiSelectAll') {
			var List = xGetElementById(current.name);
			for (i=0;i<List.length;i++)
			{
				List.options[i].selected = true;
			}
		}

	}

	if (formOK && inForm != '') {
		xGetElementById(inForm).submit();
	}

	return formOK;
}

function checkForModField (fieldID) {
	var newValue = xGetElementById(fieldID).value;
	var oldValue = xGetElementById(fieldID + "_ovalue").value;

	//alert("New Value : " + newValue + " Old Value : " + oldValue );

	if (newValue != oldValue) {
		formNeedsSaving = 1;
	}
}

function manageSelectListAdd (fieldID, dupesOK) {
	var outTextArea = xGetElementById(fieldID);
	var inField = xGetElementById(fieldID + "_add");

	//alert("In Field : " + inField.value );

	if (inField.value == null || inField.value == '') {
		alert('Please enter some text in the Add New box then click the "Add New" button.');
	}
	else {
		var foundDupe = false;
		for ( i=0; i < outTextArea.options.length; i++) {
			if (inField.value == outTextArea.options[i].value) {
				foundDupe = true;
			}
		}

		if (foundDupe && dupesOK != 1) {
			alert('The item "' + inField.value +'" already exists in the list.');
			inField.value = '';
		}
		else {
			outTextArea.options[outTextArea.length] = new Option(inField.value,inField.value);
			inField.value = '';
		}
	}

	return false;
}

function manageSelectListRemove (fieldID) {
	var outTextArea = xGetElementById(fieldID);

	var sel = false;

	for ( i=0; i < outTextArea.options.length; i++)
	{
		var current = outTextArea.options[i];
		if (current.selected)
		{
			outTextArea.options[i] = null;
			i--;
			sel = true;
		}
	}

	if (! sel) {
		alert('To remove an item please first select it from the list then click "Remove Selected"');
	}
}

function allHelpExpand () {

	var sleep = 200;
	for (var i in formFields)
	{
		if (formFields[i].hasDesc == 1 || formFields[i].hasErrors == 1) {
			setTimeout("showHelp(\'" + i + "\')", sleep);
			setTimeout("helpButtonOn(\'" + i + "\')", sleep);
			sleep = sleep + 350;
		}
	}
}

function allHelpHide () {

	var sleep = 200;
	for (var i in formFields)
	{
		if (formFields[i].isDown == 1) {
			formFields[i].isDown = 0;
			setTimeout("helpButtonOff(\'" + i + "\')", sleep);
			setTimeout("hideNow(\'" + i + "\')", sleep + 50);
			sleep = sleep + 350;
		}
	}
}

function helpButtonOn (fName) {
	formFields[fName].forceOn = 1;
	but = xGetElementById(fName + "_helpButton");
	if (but) {
		but.setAttribute("className", "htmlHelpButtonActive");
		but.setAttribute("class", "htmlHelpButtonActive");
	}
}

function helpButtonOff (fName) {
	formFields[fName].forceOn = 0;
	but = xGetElementById( fName + "_helpButton");
	if (but) {
		but.setAttribute("className", "htmlHelpButton");
		but.setAttribute("class", "htmlHelpButton");
	}
}


function helpButtonClick (fName) {
	if (formFields[fName].forceOn == 1 && formFields[fName].hasErrors != 1) {
		helpButtonOff(fName);
		hideHelp(fName, "0");
	}
	else {
		helpButtonOn(fName);
		showHelp(fName);
	}
}
function showHelp (fName) {
	if (formFields[fName].isDown != 1) {
		formFields[fName].isDown = 1;
    $('#' + fName + "_help").show(400);
	}
}
function hideHelp (fName,force) {
	if (formFields[fName].isDown == 1) {
		if (formFields[fName].forceOn != 1 || force == 1 ) {
			formFields[fName].isDown = 0;
			formFields[fName].forceOn = 0;
			setTimeout("hideNow(\'" + fName + "\')", 500);
		}
	}
}
function hideNow (fName) {
	if (formFields[fName].isDown != 1 && formFields[fName].forceOn != 1) {
    $('#' + fName + "_help").hide('fast');
	}
}

function getHTMLFieldValue (fieldID) {
	field = xGetElementById(fieldID);
	return field.value;
}

function setHTMLFieldValue (fieldID, newValue) {
	//Set the new value
	field = xGetElementById(fieldID);
	field.value = newValue;

	//Mark the field with the needs to be saved message
	errDis = xGetElementById(fieldID + "_err");
	var cerrMesage = errDis.innerHTML;
	errDis.innerHTML = '<div class="warn">This field has been edited please save the form to store the changes.</div>';
	formNeedsSaving = 1;
	showHelp(fieldID);
  $('#' + fieldID + "_err").show(500);
}

function htmlEditorPopup (loc, fieldID) {
	htmlPopWin=window.open(loc,fieldID + "_win",'menubar=no,toolbar=no,height=510,width=840,scrollbars=no,status=no,titlebar=no,dependent=yes,left=60,top=60');

	if(htmlPopWin==null){
		alert("Error: The HTML editor tried to open in a new window but failed to do so. This is usually caused by a popup blocker on your machine. Please add this site to the list of sites allowed to use popups and try the link again.");
	}

	return false;
}

function textEditorPopup (loc, fieldID) {
	textPopWin=window.open(loc,fieldID + "_win",'menubar=no,toolbar=no,height=510,width=840,scrollbars=no,status=no,titlebar=no,dependent=yes,left=60,top=60');

	if(textPopWin==null){
		alert("Error: The Text editor tried to open in a new window but failed to do so. This is usually caused by a popup blocker on your machine. Please add this site to the list of sites allowed to use popups and try the link again.");
	}

	return false;
}

function htmlFormDo (fieldID, scriptPath) {
	var sSkin = "silver";
	var oFCKeditor = new FCKeditor( 'editArea' ) ;
	oFCKeditor.BasePath = '/siteScripts/FCKeditor/' ;
	oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/' + sSkin + '/' ;
	oFCKeditor.Width = "100%" ;
	oFCKeditor.Height = "89%" ;
	oFCKeditor.Config['ForceSimpleAmpersand'] 	= true ;
	oFCKeditor.Config['ImageBrowser'] 		= false ;
	oFCKeditor.Config['CustomConfigurationsPath'] = oFCKeditor.BasePath + 'fckconfig_htmlFormObj.js' ;
	oFCKeditor.ToolbarSet = 'htmlFormObj' ;
	oFCKeditor.Value = window.opener.getHTMLFieldValue(fieldID);
	oFCKeditor.Create();
}


function htmlFormDoFm (fieldID, scriptPath) {
	var sSkin = "silver";
	var oFCKeditor = new FCKeditor( 'editArea' ) ;
	oFCKeditor.BasePath = '/siteScripts/FCKeditor/' ;
	oFCKeditor.Config['SkinPath'] = oFCKeditor.BasePath + 'editor/skins/' + sSkin + '/' ;
	oFCKeditor.Width = "100%" ;
	oFCKeditor.Height = "89%" ;
	oFCKeditor.Config['ForceSimpleAmpersand'] 	= true ;
	oFCKeditor.Config['ImageBrowser'] 		= true ;
	oFCKeditor.Config['ImageBrowserURL'] = '/cp/index.html?Haction=viewFileManagerFCK&Hlocation=fileManager&' + htmlFormDoFmParams;
	oFCKeditor.Config['ImageBrowserWindowWidth']  = 780 ;
	oFCKeditor.Config['ImageBrowserWindowHeight'] = 700 ;
	oFCKeditor.Config['CustomConfigurationsPath'] = oFCKeditor.BasePath + 'fckconfig_htmlFormObj.js' ;
	oFCKeditor.ToolbarSet = 'htmlFormObj' ;	
	oFCKeditor.Value = window.opener.getHTMLFieldValue(fieldID);
	oFCKeditor.Create();
}


function FCKeditor_OnComplete( editorInstance ){
	//window.stop();
}

function CloseWithoutSave (editor) {
	var oEditor = FCKeditorAPI.GetInstance(editor) ;
	if (oEditor.IsDirty()) {
		if (confirm("Are you sure you want to close editor and loose your changes?")) {
			CloseOK = 1;
			self.close();
		}
		else {
			return false;
		}
	}
	else {
		CloseOK = 1;
		self.close();
	}
}

function CloseWithSaveHTML (editor,fieldID) {
	var oEditor = FCKeditorAPI.GetInstance(editor) ;
	if (oEditor.IsDirty()) {
		window.opener.setHTMLFieldValue(fieldID, oEditor.GetXHTML( true ));
		CloseOK = 1;
		self.close();
	}
	else {
		CloseOK = 1;
		self.close();
	}
}

var hasChanged = 0;

function CloseWithoutSaveText (editor) {
	if (hasChanged == 1) {
		if (confirm("Are you sure you want to close editor and loose your changes?")) {
			CloseOK = 1;
			self.close();
		}
		else {
			return false;
		}
	}
	else {
		CloseOK = 1;
		self.close();
	}
}

function CloseWithSaveText (editor,fieldID) {
	window.opener.setHTMLFieldValue(fieldID, xGetElementById(editor).value);
	CloseOK = 1;
	self.close();
}

function setTextAreaAsDirty () {
	hasChanged = 1;
}


function insertAtCursor(fieldID, myValue) {
	//IE support

	field = xGetElementById(fieldID);
	hasChanged = 1;

	if (document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos)
		+ myValue
		+ field.value.substring(endPos, field.value.length);
		} else {
		field.value += myValue;
	}
}


function fileManagerPopup (fieldID, path) {
	
	var mylink = '/cp/index.html?Haction=viewFileManagerFCK&Hlocation=fileManager&htmlFormFieldID='+ fieldID + '&path=' + path;
	
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
	   href=mylink;
	else
	   href=mylink.href;
	window.open(href, "File Selector", "width=760,height=670,scrollbars=yes,top=50,left=50");
	return false;
	
}

function htmlFormSetUrl (fileID, fieldID) {
	xGetElementById(fieldID).value = fileID;
	xGetElementById(fieldID+'_thumb').innerHTML='<img src="?Haction=viewImagePreview&Hlocation=fileManager&fileid='+fileID+'&m=preview" alt="Image '+fileID+'"><a href="#" onClick="return htmlFormRemoveFileID(\''+fieldID+'\')">Remove File</a>';
}

function htmlFormRemoveFileID (fieldID) {
	xGetElementById(fieldID).value = '';
	xGetElementById(fieldID+'_thumb').innerHTML='';
}