var setFocusAllowed = true;
var dataInput = false;
var loseChangesConfirmationRequired = true;

function addHoverToElements(els) {
	for (var i=0; i<els.length; i++) {
		addHoverToElement(els[i]);
	}
}

function addHoverToElement(el) {
	el.onmouseover = function() {
		this.className += "on_hover";
	}
	el.onmouseout = function() {
		this.className = this.className.replace("on_hover", "");
	}
}

function addFocusToElements(els) {
	for (var i=0; i<els.length; i++) {
		addFocusToElement(els[i]);
	}
}

function addFocusToElement(el) {
	el.onfocus = function() {
		this.className += "on_focus";
	}
	el.onblur = function() {
		this.className = this.className.replace("on_focus", "");
	}
}

function addOnLoad(fn) {
	if (window.attachEvent) {
		window.attachEvent("onload", fn);
	} else if (window.addEventListener) {
		window.addEventListener("load", fn, false);
	}
}

function isIE() {
	return (window.attachEvent);
}

function onDataInput() {
	setDataInput(true);
}

function setDataInput(input) {
	dataInput = input;
}
function isDataInput() {
	return dataInput;
}

function confirmLoseChanges() {
	if (isDataInput() && confirmLoseChangesRequired) {
		return confirm(getLoseChangesText());
	} else {
		return true;
	}
}

function goPost(url) {
	if (confirmLoseChanges()) {
		submitForm(document.hidden_form, url);
	}
}

function closeWindow() {
	if (confirmLoseChanges()) {
		self.close();
	}
}

function closeWindowAndRefreshParent() {
	if (confirmLoseChanges()) {
		refreshParentWindow();
		self.close();
	}
}

function refreshParentWindow() {
	if (window.opener && !window.opener.closed) {
		var url = window.opener.location.href;
		if (window.isIE()) {
			// Setting focus causes problems in IE if refreshing
			// this page from a popup window
			if (!endsWith(url, '&blockFocus=y')) {
				url += '&blockFocus=y';
			}
		}
		window.opener.location.href = url;
	}
}
//********* kaew **********//
function closeWindowAndRefreshParent(url) {
	if (confirmLoseChanges()) {
		refreshParentWindow(url);
		self.close();
	}
}

function refreshParentWindow(url) {
	if (window.opener && !window.opener.closed) {
		//var url = window.opener.location.href;
		if (window.isIE()) {
			// Setting focus causes problems in IE if refreshing
			// this page from a popup window
			//if (!endsWith(url, '&blockFocus=y')) {
			//	url += '&blockFocus=y';
			//}
		}
		//alert(url);
		window.opener.location.href = url;
	}
}
//********* kaew **********//

function go(url) {
	if (confirmLoseChanges()) {
		document.location.href = url;
	}
}

function popup(name, url, width, height) {
	var attributes = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width=" + width + ",height=" + height + ",top=0,left=0";
	var popupWindow = window.open(url, name, attributes);
	popupWindow.focus();
}

function popupFullScreen(url) {
	var popupWindow = window.open(url);
	popupWindow.focus();
}

function setRedirectAndSubmit(form, url) {
	form.redirectTo.value = url;
	form.submit();
}

function submitForm(form, action) {
	if (action!=undefined) {
		form.action = action;
	}
	form.submit();
}

function resetForm(form) {
	if (confirmLoseChanges()) {
		setDataInput(false);
		form.reset();
	}
}

function populateCodeCallback(id) {
	return function(data) {
		populateCode(id, data);
	};
}

function populateCode(id, data) {
	DWRUtil.removeAllOptions(id);
	DWRUtil.addOptions(id, { '':' '});
	DWRUtil.addOptions(id, data, "id", "desc");
}

function clear(id) {
	if (id!='') {
		DWRUtil.removeAllOptions(id);
		DWRUtil.addOptions(id, { '':' '});
		getElement(id).value = '';
		getElement(id).onchange();
	}
}

function setFocusFirstField(fm) {
	if (setFocusAllowed) {
		if (fm.elements) {
			for (i=0; i<fm.length; i++) {
				var el = fm.elements[i];
				if (canSetFocus(el)) {
					if (!(el.type == "select-one")) {
						setFocus(el);
					}
					break;
				}
			}
		}
	}
}

var lastFieldValue;

function gotFocus(field) {
	lastFieldValue = field.value;
}

function nextFieldIfFull(field) {
	var l = field.value.length;
	if (l > lastFieldValue.length) {
		if (l >= field.maxLength) {
			var fm = field.form;
			var found = false;
			for (i=0; i<fm.length; i++) {
				var el = fm.elements[i];
				if (el==field) {
					found = true;
				} else if (found && canSetFocus(el)) {
					setFocus(el);
					break;
				}
			}
		}
	}
	lastFieldValue = field.value;
}

function canSetFocus(el) {
	if ((el.type == "text") || 
			(el.type == "textarea") ||
			(el.type == "select-one") ||
			(el.type == "password")) {
		return (!el.disabled) && (!el.readOnly);
	} else {
		return false;
	}
}

function setFocus(el) {
	el.focus();
}

function deleteIfEmpty(f, other) {
	if (f.value==0) {
		getElement(other).value = "";
	}
}

function getId(o) {

	if (typeof(o)=='number') {
		return o;
		
	} else {
		var id = getElement(o).value;
		if (id=='') {
			return 0;
		} else {
			// Remove the leading '_' from the id value
			return id.substring(1);
		}
	}
}

function getElement(el) {
	if (typeof(el)=='object') {
		return el;
	} else {
		return document.getElementById(el);
	}
}

function setStatus(t) {
	getElement('status').value = t;
}

function onSubmit(f) {
	dataInput = false;
}

function allSelected(selectAll, items, checked) {
	setCheckboxArray(items, checked);
	setCheckboxArray(selectAll, checked);
}

function itemSelected(selectAll, items, checked) {
	if (!checked) {
		setCheckboxArray(selectAll, false);
	} else if (numberOfItems(items, false)==0) {
		setCheckboxArray(selectAll, true);
	}
}

function numberOfItemsSelected(items) {
	return numberOfItems(items, true);
}

function numberOfItems(items, value) {
	return countCheckboxArray(items, value);
}

function setCheckboxArray(cb, value) {
	if (!isValidObject(cb)) {
	} else if (cb.length == null) {
		if (!cb.disabled) {
			cb.checked = value;	
		}
	} else {	
		for (var i=0; i<cb.length; i++) {
      		if (!cb[i].disabled) {
				cb[i].checked = value;
			}
		}
 	}
}

function countCheckboxArray(cb, value) {
	var count = 0;
	if (!isValidObject(cb)) {
	} else if (cb.length == null) {
		if (cb.checked==value) {
			count++;
		}
	} else {
		// create an associate array object
		var countArr = new Object();
		for (var i=0; i<cb.length; i++) {
			if (cb[i].checked==value) {
				if (typeof countArr[cb[i].value] == "undefined") {
					count++;
					countArr[cb[i].value] = cb[i].value;
				}
			}
		}
	}
	return count;
}

function isValidObject(objToTest) {
	if (null==objToTest) {
		return false;
	} else if ("undefined"==typeof(objToTest)) {
		return false;
	} else {
		return true;
	}
}

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxLength')) {
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxLength');
	var currentLength = this.value.length;
	if (currentLength>maxLength) {
		this.value = this.value.substring(0, maxLength);
	}
}

function endsWith(s, check) {
	if (s.length<check.length) {
		return false;
	} else {
		return (s.substring(s.length - check.length, s.length)==check);
	}
}

function enterPressed(e) {
	return (getKeyCode(e)==13);
}

function getKeyCode(e) {
	if (e && e.which) {
		return e.which;
	} else {
	 	return e.keyCode;
	}
}

function addDummyUrlParameter(url) {
	if (url.indexOf('?')>=0) {
		return url;
	} else {
		return url + '?1=1';
	}
}

function showElement(element, show) {
	var style = (show) ? '' : 'none';
	if (document.layers) {
		// Netscape 4
		element.display = style;
	} else {
		// IE 4
		element.style.display = style;
	}	
}