
function CheckBrowser(blnPopup) {
	if (!(document.getElementById && document.designMode && document.execCommand)) {
		if (blnPopup) document.location.href = '/admin/noscript.pop.asp';
		else document.location.href = '/admin/noscript.asp' + '';
	}
}

function ChangeImage(strName, strSource) {
	document.images[strName].src = strSource;
}

function PreloadImages() {
	var objArgs = PreloadImages.arguments;
	var objImg = new Image;
	for (var intCount = 0; intCount < objArgs.length; intCount++) {
		objImg.src = objArgs[intCount];
	}
}

function FirstFieldFocus(strForm) {
	var objForm, intField, objField;
	var blnValid = false;
	if (strForm && document.forms[strForm]) {
		objForm = document.forms[strForm];
		blnValid = true;
	}
	else if (document.forms[0]) {
		objForm = document.forms[0];
		blnValid = true;
	}
	if (blnValid) {
		for (intField = 0; intField < objForm.elements.length; intField++) {
			objField = objForm.elements[intField];
			if (objField.type != 'hidden') {
				objField.focus();
				break;
			}
		}
	}
}

function Popup(strURL, strName, lngWidth, lngHeight, strStatus, strScroll, strResize, strToolbar) {
	if (!strURL) {
		alert('A link must be specified when using the Popup function.');
	}
	else {
		if (!strName) strName = 'popup';
		if (!lngWidth) lngWidth = 600;
		if (!lngHeight) lngHeight = 400;
		if (!strStatus) strStatus = 'yes';
		if (!strScroll) strScroll = 'yes';
		if (!strResize) strResize = 'yes';
		if (!strToolbar) strToolbar = 'no';
		var objWindow = window.open(strURL, strName, 'width=' + lngWidth + ',height=' + lngHeight + ',status=' + strStatus + ',scrollbars=' + strScroll + ',resizable=' + strResize + ',toolbar=' + strToolbar);
		objWindow.focus();
	}
}

function Popdown(strURL, blnLeaveOpen) {
	if (!strURL) {
		alert('A link must be specified when using the Popdown function.');
	}
	else {
		if (top.opener) {
			var objParent = top.opener;
			objParent.location.href = strURL;
			if (!blnLeaveOpen) {
				objParent.focus();
				top.close();
			}
		}
		else {
			alert('Unfortunately the link could not be followed.\nThe window that opened this page may have\nbeen closed, or your browser may not\nsupport this function.');
		}
	}
}

function LaunchEditor(strForm, strField, strAction, intMode, intWidth, intHeight) {
	if (document.forms[strForm]) {
		var objForm = document.forms[strForm];
		var intTemplate
		if (objForm['nwsHTMLTemplateID']) intTemplate = objForm["nwsHTMLTemplateID"].value;
		else intTemplate = 0
		if (!intMode) intMode = 0;
		Popup('/admin/shared/editor/?form=' + strForm + '&field=' + strField + '&action=' + escape(strAction) + '&mode=' + intMode + '&template=' + intTemplate, '_blank', intWidth, intHeight, 'no', 'no', 'no');
	}
}

function LaunchCalendar(strForm, strField, strSource) {
	var strValue = document[strForm][strField].value;
	if (strValue) strSource = strValue;
	Popup('/admin/shared/date.pop.asp?form=' + strForm + '&field=' + strField + '&date=' + strSource, 'date', 188, 188, 'no', 'no', 'no', 'no', 'no');
}


function SetCursorSelection(objInput) {
	if (document.selection) objInput.cursorSelection = document.selection.createRange().duplicate();
}

function CurrentSelection(objInput) {
	if (objInput.cursorSelection) return objInput.cursorSelection.text;
	else if (objInput.selectionStart || objInput.selectionStart == 0) return objInput.value.substring(objInput.selectionStart, objInput.selectionEnd);
}

function ReplaceSelection(objInput, strNew) {
	if (objInput.cursorSelection) {
		objInput.cursorSelection.text = strNew;
		objInput.focus;
	}
	else if (objInput.selectionStart || objInput.selectionStart == 0) {
		var intStart = objInput.selectionStart;
		var intEnd = objInput.selectionEnd;
		var intCursor = (objInput.value.substring(0, intStart) + strNew).length;
		objInput.value = objInput.value.substring(0, intStart) + strNew + objInput.value.substring(intEnd);
		objInput.focus();
		objInput.selectionStart = intCursor;
		objInput.selectionEnd = intCursor;
	}
	else objInput.value += strNew;
	objInput.focus();
}

function WrapSelection(objInput, strStart, strEnd) {
	ReplaceSelection(objInput, strStart + CurrentSelection(objInput) + strEnd);
}

function CheckAll(strForm, strField, strIgnore) {
	if (document.forms[strForm]) {
		var objForm = document.forms[strForm];
		var blnChecked = objForm[strField].checked;
		var strIgnore = ',' + strIgnore + ','
		var intField, objField;
		for (intField = 0; intField < objForm.elements.length; intField++) {
			objField = objForm.elements[intField];
			if (objField.type == 'checkbox' && objField.name != strField && strIgnore.indexOf(',' + objField.name + ',') == -1) {
				objField.checked = blnChecked;
			}
		}
	}
}

function FormChanged(strForm) {
	var blnChanged = false;
	if (document.forms[strForm]) {
		var objForm = document.forms[strForm];
		var objField, intField, objOption, intOption, blnDefault;
		for (intField = 0; intField < objForm.elements.length; intField++) {
			objField = objForm.elements[intField];
			if (objField.type == 'select-one' || objField.type == 'select-multiple') {
				// check whether there is a default selection
				blnDefault = false;
				for (intOption = 0; intOption < objField.options.length; intOption++) {
					objOption = objField.options[intOption];
					if (objOption.defaultSelected) {
						blnDefault = true;
						break;
					}
				}
				for (intOption = 0; intOption < objField.options.length; intOption++) {
					objOption = objField.options[intOption];
					// first check if it's been deselected, then check if it's been selected without being the default option
					if ((objOption.defaultSelected && !objOption.selected) || (!objOption.defaultSelected && objOption.selected && (blnDefault || intOption > 0))) {
						blnChanged = true;
						break;
					}
				}
				if (blnChanged) break;
			}
			else if (objField.type == 'checkbox' || objField.type == 'radio') {
				if ((objField.defaultChecked && !objField.checked) || (!objField.defaultChecked && objField.checked)) {
					blnChanged = true;
					break;
				}
			}
			// text, textarea, hidden, submit, reset, button, password, file
			else {
				// fix for IE, default value missing line feed characters
				var strDefault = objField.defaultValue;
				var objRX = new RegExp('\r', 'gim');
				if (document.all) strDefault = strDefault.replace(objRX, '\r\n');
				if (objField.value != strDefault) {
					blnChanged = true;
					break;
				}
			}
		}
	}
	return blnChanged;
}

function BrowseImages(strForm, strField, strWidth, strHeight, strSource) {
	var strURL = '/admin/shared/images/'
	if (strForm && strField) {
		strURL += '?form=' + strForm + '&field=' + strField;
		if (document[strForm][strField].value) strURL += '&source=' + document[strForm][strField].value;
		else if (strSource) strURL += '&source=' + strSource;
		if (strWidth) strURL += '&width=' + strWidth;
		if (strHeight) strURL += '&height=' + strHeight;
	}
	Popup(strURL, 'image', 600, 450);
}

function SubmitTab(strForm, strRedirect, blnForward) {
	if (document.forms[strForm]) {
		var objForm = document.forms[strForm];
		var blnChanged = false;
		if (objForm['changed']) blnChanged = ((objForm['changed'].value == 1) ? true : false);
		if (!blnChanged) blnChanged = FormChanged(strForm);
		if (blnChanged) {
			if (confirm('If you browse away from this form, the changes you have made will be lost.\nWould you like to save the changes and continue now?\n\nClick \'OK\' to save and continue or \'Cancel\' to return to the form.')) {
				var strButton = blnForward ? 'btnForward' : 'btnBack';
				if (document.getElementById(strButton)) document.getElementById(strButton).click();
				else {
					objForm['next'] = strRedirect + '&redirect=' + escape(objForm['redirect'].value);
					objForm.submit();
				}
			}
		}
		else document.location.href = strRedirect + '&redirect=' + escape(objForm['redirect'].value);
	}
}

function BrowseTab(strForm, strRedirect, blnEditor) {
	if (document.forms[strForm]) {
		var objForm = document.forms[strForm];
		if (FormChanged(strForm) || blnEditor) {
			if (confirm('There may be changes in the form. \nWould you like to save now?\n\nClick \'OK\' to save.\nClick \'Cancel\' to continue without saving.')) {
				objForm['next'].value = strRedirect;
				var objButton = document.getElementById('btnBack');
				objButton.click(); } 
			else document.location.href = strRedirect;
		}
		else document.location.href = strRedirect;
	}
}
