/* Copyright 2008., Croatiaknjiga d.o.o. */


var guiFormElements = {
	inputBox:function(iName, iParm) {
		var objId = document.getElementsByTagName("input");
		for (inx = 0; inx < objId.length; inx++) {
			if (objId[inx].type.toLowerCase() == "text" && objId[inx].className == "sElement") {
				var iData = {
					length:objId[inx].getAttribute("length"),
					xsize:objId[inx].getAttribute("xsize"),
					atext:objId[inx].getAttribute("atext"),
					limit:objId[inx].getAttribute("limit"),
					wlock:objId[inx].getAttribute("wlock")
				}
				objId[inx].removeAttribute("length");
				objId[inx].removeAttribute("xsize");
				objId[inx].removeAttribute("atext");
				objId[inx].removeAttribute("limit");
				objId[inx].removeAttribute("wlock");
				objId[inx].removeAttribute("class");
				objId[inx].removeAttribute("className");
				objId[inx].className = "frmInput";
				if (iData.xsize != null && parseInt(iData.xsize) > 0) objId[inx].style.width = parseInt(iData.xsize) + "px";
				if (iData.length != null && parseInt(iData.length) > 0) objId[inx].setAttribute("maxLength", parseInt(iData.length));
				if (iData.atext != null) {
					switch (iData.atext.toLowerCase()) {
						case "left":
						case "right":
						case "center":
							objId[inx].style.textAlign = iData.atext.toLowerCase();
							break;
						default:
							objId[inx].style.textAlign = "left";
							break;
					}
				}
				if (iData.wlock != null && iData.wlock.toLowerCase() == "true") {
					objId[inx].style.backgroundColor = "#F0F0F0";
					objId[inx].style.color = "#909090";
					objId[inx].readOnly = true;
				} else {
					objId[inx].onfocus = function() {
						this.style.backgroundColor = "#FAFAFA";
						this.style.color = "#D00000";
					}
					objId[inx].onblur = function() {
						this.style.backgroundColor = "#F0F0F0";
						this.style.color = "#606060";
					}
					if (iData.limit != null && iData.limit != "") {
						objId[inx].inLimit = iData.limit;
						objId[inx].onkeypress = function(e) {
							var evt = window.event ? window.event : e;
							var cCode = (evt.which) ? evt.which : evt.keyCode
							var rCode = this.inLimit;
							var rStatus = false;
							if (cCode == null || cCode == 0 || cCode == 8 || cCode == 9 || cCode == 13 || cCode == 27) rStatus = true;
							if (rCode.indexOf(String.fromCharCode(cCode)) != -1) rStatus = true;
							return rStatus;
						}
					} else {
						objId[inx].inLimit = null;
					}
				}	
			}
		}
	},
	checkBox:function() {
		var cbxId = document.getElementsByTagName("input");
		for (inx = 0; inx < cbxId.length; inx++) {
			if (cbxId[inx].type == "checkbox" && cbxId[inx].className == "sElement") {
				var iData = {
					inline:cbxId[inx].getAttribute("inline"),
					xsize:cbxId[inx].getAttribute("xsize"),
					label:cbxId[inx].getAttribute("label")
				}
				cbxId[inx].removeAttribute("inline");
				cbxId[inx].removeAttribute("label");
				cbxId[inx].removeAttribute("xsize");
				cbxId[inx].removeAttribute("class");
				cbxId[inx].removeAttribute("className");
				cbxId[inx].style.display = "none";
				var conId = document.createElement("div");
				conId.className = "rcbxContainer";
				if (iData.inline != null && iData.inline.toLowerCase() == "false") conId.style.clear = "both";
				conId.linkId = cbxId[inx];
				conId.ieFixNeed = this._ieFixCheck();
				conId.doMouseHover = this._cbxHover;
				conId.doMouseClick = this._cbxClick;
				conId.doAlphaImage = this._setImage;
				cbxId[inx].parentNode.insertBefore(conId, cbxId[inx]);
				if (cbxId[inx].checked) {
					conId.isChecked = true;
					conId.oldStatus = true;
					conId.doAlphaImage(conId, "img/core/cbx_checked_out.png");
				} else {
					conId.isChecked = false;
					conId.oldStatus = false;
					conId.doAlphaImage(conId, "img/core/cbx_empty_out.png");
				}
				if (iData.label != null && iData.label != "") {
					tmpId = document.createElement("p");
					tmpId.className = "label";
					tmpId.appendChild(document.createTextNode(iData.label));
					conId.appendChild(tmpId);
					if (iData.xsize != null && parseInt(iData.xsize) > 16) conId.style.width = parseInt(iData.xsize) + "px";
				} else {
					conId.style.width = "16px";
				}
				conId.onmouseover = function() {
					this.doMouseHover(this, 1);
				}
				conId.onmouseout = function() {
					this.doMouseHover(this, 0);
				}
				conId.onclick = function() {
					this.doMouseClick(this);
				}
			}
		}
	},
	radioBox:function() {
		var rbxId = document.getElementsByTagName("input");
		for (inx = 0; inx < rbxId.length; inx++) {
			if (rbxId[inx].type == "radio" && rbxId[inx].className == "sElement") {
				var iData = {
					inline:rbxId[inx].getAttribute("inline"),
					xsize:rbxId[inx].getAttribute("xsize"),
					label:rbxId[inx].getAttribute("label")
				}
				rbxId[inx].removeAttribute("inline");
				rbxId[inx].removeAttribute("label");
				rbxId[inx].removeAttribute("xsize");
				rbxId[inx].removeAttribute("class");
				rbxId[inx].removeAttribute("className");
				rbxId[inx].style.display = "none";
				var conId = document.createElement("div");
				conId.className = "rcbxContainer";
				conId.groupName = rbxId[inx].name;
				conId.linkId = rbxId[inx];
				conId.ieFixNeed = this._ieFixCheck();
				conId.doMouseHover = this._rbxHover;
				conId.doMouseClick = this._rbxClick;
				conId.doAlphaImage = this._setImage;
				rbxId[inx].parentNode.insertBefore(conId, rbxId[inx]);
				if (iData.inline != null && iData.inline.toLowerCase() == "false") conId.style.clear = "both";
				if (rbxId[inx].getAttribute("checked")) {
					conId.isChecked = true;
					conId.oldStatus = true;
					conId.doAlphaImage(conId, "img/core/rbx_checked_out.png");
				} else {
					conId.isChecked = false;
					conId.oldStatus = false;
					conId.doAlphaImage(conId, "img/core/rbx_empty_out.png");
				}
				if (iData.label != null && iData.label != "") {
					tmpId = document.createElement("p");
					tmpId.className = "label";
					tmpId.appendChild(document.createTextNode(iData.label));
					conId.appendChild(tmpId);
					if (iData.xsize != null && parseInt(iData.xsize) > 16) conId.style.width = parseInt(iData.xsize) + "px";
				} else {
					conId.style.width = "16px";
				}
				conId.onmouseover = function() {
					this.doMouseHover(this, 1);
				}
				conId.onmouseout = function() {
					this.doMouseHover(this, 0);
				}
				conId.onclick = function() {
					this.doMouseClick(this);
				}
			}
		}
	},
	textArea:function() {
		var objId = document.getElementsByTagName("textarea");
		for (inx = 0; inx < objId.length; inx++) {
			if (objId[inx].className == "sElement") {
				var iData = {
					length:objId[inx].getAttribute("length"),
					ysize:objId[inx].getAttribute("ysize"),
					xsize:objId[inx].getAttribute("xsize"),
					wlock:objId[inx].getAttribute("wlock")
				}
				objId[inx].removeAttribute("length");
				objId[inx].removeAttribute("ysize");
				objId[inx].removeAttribute("xsize");
				objId[inx].removeAttribute("wlock");
				objId[inx].removeAttribute("class");
				objId[inx].removeAttribute("className");
				objId[inx].className = "frmTArea";
				if (iData.ysize != null && parseInt(iData.ysize) > 10) objId[inx].style.height = parseInt(iData.ysize) + "px";
				if (iData.xsize != null && parseInt(iData.xsize) > 10) objId[inx].style.width = parseInt(iData.xsize) + "px";
				if (iData.wlock != null && iData.wlock.toLowerCase() == "true") {
					objId[inx].style.backgroundColor = "#F0F0F0";
					objId[inx].style.color = "#909090";
					objId[inx].readOnly = true;
				} else {
					objId[inx].onfocus = function() {
						this.style.backgroundColor = "#FAFAFA";
						this.style.color = "#D00000";
						this.select();
					}
					objId[inx].onblur = function() {
						this.style.backgroundColor = "#F0F0F0";
						this.style.color = "#A0A0A0";
					}
					if (iData.length != null && Number(iData.length) > 0) {
						objId[inx].inLength = Number(iData.length);
						objId[inx].onkeydown = objId[inx].onkeyup = function() {
							if (this.inLength > 0 && this.value.length > this.inLength)	this.value = this.value.substring(0, this.inLength);
						}
					} else {
						objId[inx].inLength = 0;
					}
				}	
			}
		}
	},
	resetAll:function(iName) {
		var frmId = document.forms[iName];
		if (typeof(frmId) != "undefined") {
			var objId = frmId.getElementsByTagName("div");
			for (var inx = 0; inx < objId.length; inx++) {
				if (objId[inx].className == "rcbxContainer" && objId[inx].linkId != undefined) {
					switch (objId[inx].linkId.type) {
						case "checkbox":
							if (objId[inx].oldStatus) {
								objId[inx].doAlphaImage(objId[inx], "img/core/cbx_checked_out.png");
								objId[inx].isChecked = true;
							} else {
								objId[inx].doAlphaImage(objId[inx], "img/core/cbx_empty_out.png");
								objId[inx].isChecked = false;
							}
							break;
						case "radio":
							if (objId[inx].oldStatus) {
								objId[inx].doAlphaImage(objId[inx], "img/core/rbx_checked_out.png");
								objId[inx].isChecked = true;
							} else {
								objId[inx].doAlphaImage(objId[inx], "img/core/rbx_empty_out.png");
								objId[inx].isChecked = false;
							}
							break;
					}
				}
			}
			frmId.reset();
		}
	},
	_cbxHover:function(objId, onoff) {
		if (onoff == 1) {
			if (objId.isChecked) this.doAlphaImage(objId, "img/core/cbx_checked_over.png")
			else this.doAlphaImage(objId, "img/core/cbx_empty_over.png");
		} else {
			if (objId.isChecked) this.doAlphaImage(objId, "img/core/cbx_checked_out.png")
			else this.doAlphaImage(objId, "img/core/cbx_empty_out.png");
		}
	},
	_cbxClick:function(objId) {
		if (objId.isChecked) {
			this.doAlphaImage(objId, "img/core/cbx_empty_over.png");
			objId.linkId.checked = false;
			objId.isChecked = false;
		} else {
			this.doAlphaImage(objId, "img/core/cbx_checked_over.png")
			objId.linkId.checked = true;
			objId.isChecked = true;
		}
	},
	_rbxHover:function(objId, onoff) {
		if (onoff == 1) {
			if (objId.isChecked) this.doAlphaImage(objId, "img/core/rbx_checked_over.png")
			else this.doAlphaImage(objId, "img/core/rbx_empty_over.png");
		} else {
			if (objId.isChecked) this.doAlphaImage(objId, "img/core/rbx_checked_out.png")
			else this.doAlphaImage(objId, "img/core/rbx_empty_out.png");
		}
	},
	_rbxClick:function(objId) {
		this.doAlphaImage(objId, "img/core/rbx_checked_over.png");
		objId.linkId.checked = true;
		objId.isChecked = true;
		var tmpId = document.getElementsByTagName("div");
		for (inx = 0; inx < tmpId.length; inx++) {
			if (tmpId[inx].groupName == objId.groupName && tmpId[inx].className == "rcbxContainer") {
				if (tmpId[inx] != objId) {
					this.doAlphaImage(tmpId[inx], "img/core/rbx_empty_out.png");
					tmpId[inx].isChecked = false;
					tmpId[inx].linkId.checked = false;
				}
			}
		}
	},
	_setImage:function(objId, iName) {
		if (iName != undefined && iName != null && iName != "") {
			if (this.ieFixNeed) {
				objId.style.background = "none";
				objId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iName + "', sizingMethod='crop')";
			} else {
				objId.style.background = "url(" + iName + ") 0px 0px no-repeat";
			}
		}
	},
	_ieFixCheck:function() {
		var uAgent = navigator.userAgent;
		var bVer = 0;
		var status = false;
		if ((uAgent.indexOf("MSIE") != -1) && (uAgent.indexOf("Opera") == -1)) {
			bVer = parseFloat(uAgent.substring(uAgent.indexOf("MSIE")+5));
			if (bVer < 7) status = true;
		}
		return status;
	}
}

