
/*	function SubmitNews
 *  Sets the values of the input tags before submiting the forms action
 *	input :	formName, string; name of the form containing input tags
 *			actionName, string; name of the action
 *			actionFieldName, string; name of the input tag whose value will be set
 *			propertyID, string; name of the property 
 *			propertyFieldName, string; name of the input tag whose value will be set
 */
 
var hasClicked = false;
var locked = false;
 
function SubmitNews(formName, actionName, actionFieldName, propertyID, propertyFieldName) {
	var tmpForm, propertyField, actionField;
	if(!hasClicked && !locked) {
		locked = true;
		if (ConfirmEmpty(formName, actionName)) {
			tmpForm = eval('document.' + formName);
			if (propertyFieldName != ""){
				propertyField = eval('tmpForm.' + propertyFieldName);
				propertyField.value = propertyID;
			}
			if (actionFieldName != ""){
				actionField = eval('tmpForm.' + actionFieldName);
				actionField.value = actionName;
			}
			tmpForm.submit();
			hasClicked = true;
		}
		locked = false;
	}
}

function UGSubmitForm(formName, actionName, actionFieldName, propertyID, propertyFieldName) {
	var tmpForm, propertyField, actionField;
	if(!hasClicked && !locked) {
		locked = true;
		if (ConfirmEmpty(formName, actionName)) {
			tmpForm = eval('document.' + formName);
			if (propertyFieldName != ""){
				propertyField = eval('tmpForm.' + propertyFieldName);
				propertyField.value = propertyID;
			}
			if (actionFieldName != ""){
				actionField = eval('tmpForm.' + actionFieldName);
				actionField.value = actionName;
			}
			tmpForm.submit();
			hasClicked = true;
		}
		locked = false;
	}
}

function ChooseTemplate(formName, propertyValue, propertyFieldName) {
	var tmpForm, propertyField;
	tmpForm = eval('document.' + formName);
	if (propertyFieldName != ""){
		propertyField = eval('tmpForm.' + propertyFieldName);
		propertyField.value = propertyValue;
	}
}



function ConfirmEmpty(formName, actionName){
		var tmpForm;
		tmpForm = eval('document.' + formName);
		if (tmpForm.confirmEmptyCompare)
			if (actionName != tmpForm.confirmEmptyCompare.value)
				return true;
			else
				return confirm(tmpForm.confirmEmptyMessage.value);
		else
			return true;
}

function MakeSelectedLinksList(formName,fieldName,listFieldName,actionName,actionFieldName,makeList) {
	var tmpForm, tmpField, listField, actionField, tmpString, i;
	tmpForm = eval('document.' + formName);
	actionField = eval('tmpForm.' + actionFieldName);
	if (makeList>0) {
		MakeList(formName,fieldName,listFieldName);
	}
	actionField.value = actionName;
	
	tmpForm.submit();
}

function UGMakeSelectedLinksList(formName,fieldName,listFieldName,actionName,actionFieldName,makeList,doSubmit,containerNumberFieldName) {
	var tmpForm, tmpField, listField, actionField, tmpString, i, containerNumberField, containerNumber;
	tmpForm = eval('document.' + formName);
	if (makeList>0) {
		containerNumberField = eval('tmpForm.' + containerNumberFieldName);
		if (containerNumberField) 
			containerNumber = containerNumberField.value;
		else
			containerNumber = 0;
		for (i=1;i<=containerNumber;i++)
			MakeList(formName,fieldName + i,listFieldName + i);
	}
	if (doSubmit > 0) {
		actionField = eval('tmpForm.' + actionFieldName);
		actionField.value = actionName;
		tmpForm.submit();
	}
}


function MakeList(formName, fieldName, listFieldName) {
	var tmpForm, tmpField, listField;
	tmpForm = eval('document.' + formName);		
	tmpField = eval('tmpForm.' + fieldName);
	listField = eval('tmpForm.' + listFieldName);
	tmpString = "";
	for (i=0; i<tmpField.options.length; i++) {
		tmpString = tmpString + tmpField.options[i].value + ",";
	}
	listField.value = tmpString.substring(0, tmpString.length - 1);
}



/*
 * MoveItem
 * Den här funktionen används då man vill flytta 
 * objekt från ett inputfält till ett annat.
 * Funktionen använder sig av funktinoen "SortField" 
 * för att tillse att de båda inputfälten är sorterade efter flytten.
 *
 */
function MoveItem(formName,fromFieldName,toFieldName) {
	var tmpOption, tmpValue, text, selectedIndex, fromField, toField, i, fieldStrang;
	fromField = eval('document.' + formName + '.' + fromFieldName);
	toField = eval('document.' + formName + '.' + toFieldName);
	selectedIndex = fromField.selectedIndex; 
	if (selectedIndex == -1) return;
	i=fromField.options.length-1;
	while (i >= 0){
		if(fromField.options[i].selected) {
			text = fromField.options[i].text;
			tmpValue = fromField.options[i].value;
			tmpOption = new Option(text, tmpValue, false, false);
			toField.options[toField.options.length] = tmpOption;
			fromField.remove(i);
		}
		i--;
	}
	SortField(formName,toFieldName);
}


/*
 * This function is a helper to the MoveItems-function above.
 * It checks if an element exists in a given array.
 */
 function Exists(array, testValue){
 	var t;
 	while(array[t]){
 		if(t == testValue)
 			return true;
 		t++;	
 	}
 	return false;
 }





/*
 * SortField
 * Funktionen sorterar objekten i ett fält i bokstavsordning. 
 */
function SortField(formName,fieldName) {
	var tmpOption, tmpValueArray, sortField, sortedField, i, sortArray;
	sortField = eval('document.' + formName + '.' + fieldName);
	i=0;
	sortArray=new Array();
	
	// Bygg en array bestående av {text:value}-strängar. Töm select fältet samtidigt.
	while (i < sortField.options.length) {
		sortArray[i] = sortField.options[i].text + "§" + sortField.options[i].value;
		i++;
	}
	
	// Sortera arrayen.
	sortArray.sort();
	
	// Bygg upp select-fältet med värdena från den sorterade arrayen.
	for (i=0;i < sortArray.length; i++) {
		tmpValueArray = sortArray[i].split("§");
		tmpOption = new Option(tmpValueArray[0], tmpValueArray[1], false, false);
		sortField.options[i] = tmpOption;
	}
}

function CompareFields(formName, mainFieldName, comparedFieldName) {
	var tmpForm, mainField, comparedField, mainFieldArray, i, j;
	tmpForm = eval('document.' + formName);
	mainField = eval('tmpForm.' + mainFieldName);
	comparedField = eval('tmpForm.' + comparedFieldName);
	i=0;
	mainFieldArray = new Array();
	while (mainField.options[0]) {
		mainFieldArray[i] = mainField.options[0].value;
		mainField.remove(0);
		i++;
	}
	for (i=0;i<mainFieldArray.length; i++) 
		for(j=0;j<comparedField.options.length;j++)
			if(comparedField.options[j].value == mainFieldArray[i])
				comparedField.remove(j);
	
}

function FixDays(formName, fieldName) {
	var d, yfield, mfield, dfield, inputField, i, tempd;
	yfield = eval('document.' + formName + '.' + fieldName + 'Year');
	mfield = eval('document.' + formName + '.' + fieldName + 'Month');
	dfield = eval('document.' + formName + '.' + fieldName + 'Day');
	inputField = eval('document.' + formName + '.' + fieldName);
	d = new Date(yfield.value,mfield.value,0).getDate();
	if (d != 1) {
		tempd = dfield.value>d?d:dfield.value;
		while (dfield.options.length>d)
			dfield.options[dfield.options.length-1] = null;
		while (dfield.options.length<d)
			dfield.options[dfield.options.length] = new Option(dfield.options.length+1, dfield.options.length+1, true, true);
		/*if (isMac) 
			dfield.value = tempd-1;
		else
			dfield.value = tempd;
		*/
		inputField.value = yfield.value + "-" + mfield.value + "-" + dfield.value;
	}
}

function FixTime(formName, fieldName) {
	var hfield, mfield, dfield, inputField;
	hfield = eval('document.' + formName + '.' + fieldName + 'Hour');
	mfield = eval('document.' + formName + '.' + fieldName + 'Minute');
	inputField = eval('document.' + formName + '.' + fieldName);
	inputField.value = hfield.value + ":" + mfield.value;
}


/*
 * SetSelected
 * Funktionen sätter samtliga options i ett select-fält till "selected". 
 */
function SetSelected(formName,fieldName) {
	var selectField, i;
	selectField = eval('document.' + formName + '.' + fieldName);
	i = 0;
	while (selectField.options[i]){
		selectField.options[i].selected="true";
		i++;
	}	
}

function transferAppletText(appletCount, appletBaseName, appletPropertyFieldName, formName) {
	var n, appletName, stringValue, appletElement, propertyName, propertyField;
	for(n = 0; n < appletCount; n++) {
		appletName = appletBaseName + n;
		appletElement = document.getElementById(appletName);
		stringValue = appletElement.getContent();
		propertyName = eval("document" + "." + formName + "." + appletPropertyFieldName + n + ".value");
		propertyField = eval("document" + "." + formName + "." + propertyName);
		propertyField.value = stringValue;
	}
}

function saveJavaScriptEditors(editorCount, editorBaseName) {
	var n;
	for(n = 0; n < editorCount; n++) {
		var editorName = editorBaseName + n;
		var editorElement = document.getElementById(editorName).contentWindow;
		if(!editorElement || !editorElement.saveIt) {
			var iFrameName = document.getElementById(editorName).name.substr(5);
			inpVal = eval('document.all.'+iFrameName+'.value');
			eval('document.all.'+iFrameName+'.value = unescape(inpVal);');
		} else {
			editorElement.saveIt();
		}
	}
}

function transferFocusToChildIframe(parentIframe) {
	parentIframe.contentWindow.document.idEdit.focus();
}
	
