/* 
Filename:	/scripts/form.js
Updated:	2004-10-07 14:51
Comments:	Included in form.xsl and used by form xsl-templates
*/

var _flowCheckFlag = false;
var _flowSecurityFlag = false;
var _flowSaveFlag = false;

var sInitColor = null;
var editDoc = null;
var editEl = null;

function _flowSwapMode() {
	el = editEl;
	if (el.getAttribute("viewMode") == "html") {
		bHtml = false;
		htmlStr = el.innerText;
		el.innerHTML = htmlStr;
		el.style.fontSize = "";
		el.style.fontFamily = "";
		el.style.color = "";
		el.setAttribute("viewMode", "richtext");
		el.focus();
	} else {
		bHtml = true;
		htmlStr = el.innerHTML;
		el.innerText = htmlStr;
		el.style.fontSize = "11px";
		el.style.fontFamily = "Courier";
		el.style.color = "#000000";
		el.setAttribute("viewMode", "html");
		el.focus();
	}
}

function _flowEditCheck(bControl) {
	typ = editDoc.selection.type;
	if (bControl == null)
		if ((typ != "None") && (typ != "Text")) return false;
	sel = editDoc.selection.createRange();
//	temp = sel.parentElement();
	temp = sel;
	if (temp.length != null) temp = temp(0);
	while ((temp != null) && (temp.tagName != "BODY")) {
		if (temp.contentEditable) return true;
		temp = temp.parentElement();
	}
	return false;
}

function _flowCallColorDlg(colorSet) {
	if (!_flowEditCheck()) return;
	if (colorSet == null) colorSet = "ForeColor";

	dlgEl = document.getElementById('dlgHelper');
	if (dlgEl == null) return;
	
	if (sInitColor == null) var sColor = dlgEl.ChooseColorDlg()
	else var sColor = dlgEl.ChooseColorDlg(sInitColor);

	sColor = sColor.toString(16);
	if (sColor.length < 6) {
		var sTempString = "000000".substring(0,6-sColor.length);
		sColor = sTempString.concat(sColor);
	}
	_flowExecAction(colorSet, sColor, false);
	sInitColor = sColor;
}

function _flowExecAction(execProp, execVal, bUI) {
	if (!_flowEditCheck(true)) return;
	var type = editDoc.selection.type;
	var oTarget = type == "None" ? editDoc : editDoc.selection.createRange();
	var r = oTarget.execCommand(execProp, bUI, execVal);
	if (type == "Text") oTarget.select();
//	return r;
}

function _flowRichFocus(el) {
	if (typeof(el) == "string") el = document.getElementById(el);
	editEl = el;
	editDoc = el.document;
}

function _flowInitRichText(id) {
	if (document.all) {
		localeId = 'swe';
		if (document.getElementById('dlgHelper') == null) document.writeln('<object id="dlgHelper" classid="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" style="width: 0px; height: 0px;"></object>');
		document.writeln('<div id="_flowRichEdit' + id + '" class="toolbarRichText" style="cursor:default; behavior:url(/flowserver/com/coolbuttons/coolbuttons.htc);"></div>');
		divEl = eval('_flowRichEdit' + id);
		divEl.xmlSrc = '/flowserver/settings/richedit_formfield_buttons.xml';
		divEl.xslSrc = '/flowserver/xsl/coolbuttons.xsl';
		divEl.localeSrc = '/flowserver/locales/' + localeId + '/richedit.xml';
		divEl.render();
		divEl.unselectable = "on";
		for (j=0; j<divEl.all.length; j++) {
			divEl.all(j).unselectable = "on";
		}
	}
}

function _flowFileLibrary(id) {
	_flowInputEl = document.getElementById(id);
	editUrl = "/flowserver/mod/files/default_files.htm?serviceId=" + _flowServiceId + "&companyId=" + _flowCompanyId + "&insertImage=true&preview=true";
	result = window.showModalDialog(editUrl, null, "dialogWidth:630px; dialogHeight:550px; center:yes; dialogHide:no; edge:raised; help:no; maximize:yes; resizable:yes; scroll:no; status:yes; unadorned:no;");
	if (result) {
		_flowInputEl.value = result;
	}
}

function _flowXmlEncode(str) {
	if (str == null) return "";
//	str = str.replace(/&/gi, "&amp;");
	str = str.replace(/</gi, "&lt;");
	str = str.replace(/>/gi, "&gt;");
	str = str.replace(/"/gi, '&quot;');
	str = str.replace(/'/gi, "&apos;");
	return str;
}

function _flowSetRichValue(frm, fieldName) {
	if (event == null) richEl = document.getElementById('_flowRich_' + fieldName)
	else richEl = event.srcElement;
	toField = eval("frm.elements('" + fieldName + "')");
	toField.value = _flowXmlEncode(richEl.innerHTML);
//	toField.value = richEl.innerHTML;
}

function _flowSelectFile(id, bMultiple, _flowFolderId) {
	if (_flowFolderId == null) _flowFolderId = 21;
	_flowInputEl = document.getElementById(id);
	_flowUrl = '/flowserver/mod/files/fileuploads.aspx?folder=' + _flowFolderId + '&multiple=' + bMultiple + '&files=' + _flowInputEl.value;
	if (document.all) {
		result = window.showModalDialog('/flowserver/mod/files/modalDialog.htm', _flowUrl, "dialogWidth:450px; dialogHeight:310px; center:yes; dialogHide:no; edge:raised; help:no; resizable:no; scroll:auto; status:no; unadorned:no;");
		if (result != null) {
			_flowInputEl.value = result;
		}
	}
}

function _flowChecked(bChecked) {
	if (bChecked == "true") return " checked "
	else return " ";
}

function _flowSecuritySet() {
	_flowInputEl = document.getElementById(_flowSecurityId);

	_flowStr = '<data><securityitems>';
	for (j=0; j<_flowSecurity.length; j++) {
		_flowSecEl = _flowSecurity[j];
		_flowStr += '<securityitem securityid="" id="' + _flowSecEl.id + '" permissioncreate="' + _flowSecEl.permissioncreate + '" permissiondelete="' + _flowSecEl.permissiondelete + '" permissiondeleteown="' + _flowSecEl.permissiondeleteown + '" permissionread="' + _flowSecEl.permissionread + '" permissionreadown="' + _flowSecEl.permissionreadown + '" permissionwrite="' + _flowSecEl.permissionwrite + '" permissionwriteown="' + _flowSecEl.permissionwriteown + '" />';
	}
	_flowStr += '</securityitems></data>';

	_flowInputEl.value = _flowStr;
}

function _flowSecurityRow(label) {
	return '<tr><td><label>' + eval('L_Permission' + label + '_Text') + '</label></td><td><input type="checkbox"' + eval("_flowChecked(_flowSecEl.permission" + label.toLowerCase() + ")") + 'onclick="_flowSecurityEdit(this, ' + idx + ', \'permission' + label.toLowerCase() + '\')"></td></tr>';
}

function _flowSecurityChange(el) {
	idx = el.selectedIndex;
	if (idx >= 0) {
		_flowSecEl = _flowSecurity[idx];
		_flowDivEl = document.getElementById("divSecurity");

		_flowStr = '';
		_flowStr += '<label class="label">' + L_PermissionLabel_Text + ' <b>[' + _flowSecEl.name + ']</b>:</label>';
		_flowStr += '<table width="100%" cellpadding="0" cellspacing="1">';
		_flowStr += _flowSecurityRow('Read');
		_flowStr += _flowSecurityRow('WriteOwn');
		_flowStr += _flowSecurityRow('Write');
		_flowStr += _flowSecurityRow('Delete');
		_flowStr += _flowSecurityRow('DeleteOwn');
		_flowStr += _flowSecurityRow('Create');
		_flowStr += '</table>';
		_flowDivEl.innerHTML = _flowStr;
	}
}

function _flowSecuritySave() {
	_flowStr = '<data>';
	for (j=0; j<_flowSecurity.length; j++) {
		_flowStr += '<securityitem id="' + _flowSecurity[j].id + '" permissioncreate="' + _flowSecurity[j].permissioncreate + '" permissiondelete="' + _flowSecurity[j].permissiondelete + '" permissiondeleteown="' + _flowSecurity[j].permissiondeleteown + '" permissionread="' + _flowSecurity[j].permissionread + '" permissionreadown="' + _flowSecurity[j].permissionreadown + '" permissionwrite="' + _flowSecurity[j].permissionwrite + '" permissionwriteown="' + _flowSecurity[j].permissionwriteown + '" />';
	}
	_flowStr += '</data>';
}

function _flowSecurityEdit(el, idx, str) {
	eval('_flowSecurity[idx].' + str + ' = "' + el.checked + '"');
}

function _flowSecurityRemove(id) {
	selEl = document.getElementById(id + '_list');
	for (j=0; j<selEl.length; j++) {
		if (selEl.options[j].selected) {
			for (i=j;i<_flowSecurity.length-1;i++) {
				_flowSecurity[i] = _flowSecurity[i+1];
			}
			_flowSecurity.length--;
		}
	}
	_flowSecurityList(id);
}

function _flowSecurityAdd(id) {
	_flowUrl = '/flowserver/mod/users/userlist.aspx';
	if (document.all) {
		_flowUsers = window.showModalDialog('/flowserver/mod/users/modalDialog.htm', _flowUrl, "dialogWidth:420px; dialogHeight:300px; center:yes; dialogHide:no; edge:raised; help:no; resizable:no; scroll:auto; status:no; unadorned:no;");
		if (_flowUsers != null) {
			for (j=0; j<_flowUsers.length; j++) {
				bFound = false;
				for (i=0; i<_flowSecurity.length; i++) {
					if (_flowUsers[j].id == _flowSecurity[i].id) {
						bFound = true;
						break;
					}
				}
				if (!bFound) _flowSecurity[_flowSecurity.length] = new _flowSecurityObject(_flowUsers[j].id, _flowUsers[j].name, _flowUsers[j].typ, 'false', 'false', 'false', 'true', 'false', 'false', 'false');
			}
			_flowSecurityList(id);
		}
	}
}

function _flowSecurityList(id) {
	selEl = document.getElementById(id + '_list');
	selEl.length = 0;
	for (j=0; j<_flowSecurity.length; j++) {
		selEl.length++;
		selEl.options[j].value = _flowSecurity[j].id;
		selEl.options[j].text = _flowSecurity[j].name;
	}
	divSecurity.innerHTML = '';
}

function _flowSecurityObject(id, name, typ, pcreate, pdelete, pdeleteown, pread, preadown, pwrite, pwriteown) {
	this.id = id;
	this.name = name;
	this.typ = typ;
	this.permissioncreate = pcreate;
	this.permissiondelete = pdelete;
	this.permissiondeleteown = pdeleteown;
	this.permissionread = pread;
	this.permissionreadown = preadown;
	this.permissionwrite = pwrite;
	this.permissionwriteown = pwriteown;
}

function _flowGetField(frm, fieldName) {
	if (document.all) return eval("frm.elements('" + fieldName + "')")
	else if (document.getElementById) return document.getElementById(fieldName)
	else return eval("frm.elements['" + fieldName + "']");
}

function _flowGetValue(fromField) {
	if (fromField.type == null) fromType = fromField[0].type
	else fromType = fromField.type;

	switch (fromType) {
		case "select-multiple" :
			str = "";
			for (j=0; j<fromField.length; j++) {
				if (fromField.options[j].selected) str += fromField.options[j].value + ", ";
			}
			if (str.lastIndexOf(",") == (str.length-2)) str = str.substring(0, str.length-2);
			return str;
			break;
		case "radio" :
			for (j=0; j<fromField.length; j++) {
				if (fromField[j].checked) return fromField[j].value;
			}
			return '';
			break;
		case "checkbox" :
			str = "";
			for (j=0; j<fromField.length; j++) {
				if (fromField[j].checked) str += fromField[j].value + ", ";
			}
			if (str.lastIndexOf(",") == (str.length-2)) str = str.substring(0, str.length-2);
			return str;
			break;
		default :
			return fromField.value;
			break;
	}
}

function _flowSetValue(toField, value) {
	toType = toField.type;

	switch (toType) {
		case "select-one" :
		case "select-multiple" :
			str = value.split(",");
			for (j=0; j<toField.length; j++) {
				for (i=0; i<str.length; i++) {
					if (toField.options[j].value == str[i]) toField.options[j].selected = true;
				}
			}
			break;
		case "checkbox" :
			break;
		default :
			toField.value = value;
			break;
	}
}

function _flowFormBack(frm) {
	self.history.go(-1);
}

function _flowFormChangeValue(frm, flag, toField, value, fromField) {
	switch (flag) {
		case "FROM_DEFAULT" :
			toField = _flowGetField(frm, toField);
			_flowSetValue(toField, value);
			break;
		case "FROM_FIELD" :
			fromField = _flowGetField(frm, fromField);
			toField = _flowGetField(frm, toField);
			_flowSetValue(toField, _flowGetValue(fromField));
			break;
		default :
			break;
	}
}

function _flowFormAddGroup(frm, str) {
	frm.fs__addgroup.value = str;
	_flowFormSubmit(frm);
}

function _flowFormDelete(frm) {
	if (confirm(L_ConfirmDelete_Text)) {
		frm.fs__delete.value = 'true';
		_flowFormSubmit(frm);
	}
}

function _flowFormSubmit(frm) {
//	frm.fs__referer.value = window.location.href;
	_flowSaveFlag = true;
	bSubmit = true;
	if (_flowSecurityFlag) _flowSecuritySet();
	if (_flowCheckFlag) bSubmit = _flowFormCheck(frm);
	if (bSubmit) frm.submit();
}

function _flowFormClear(frm) {
	frm.reset();
}
