
/**
 * Creates new client functions
 * Namespace: Client
 */
Client = {
	/** 
	 * Returns true if client is internet explorer
	 * @return boolean
	 */ 
	isIE: function() {
			if(document.all) { return true; } else { return false; }		
		},
	
	/** 
	 * Returns true if client is netscape 6 or higher
	 * @return boolean
	 */ 
	isNS6: function() {
			if(document.getElementById) { return true; } else { return false; }
		},
	
	/** 
	 * Returns true if client is netscape 4
	 * @return boolean
	 */ 
	isNS4: function() {
			if(document.layers) { return true; } else { return false; }
		},

	/** 
	 * Returns the body height
	 * @return integer
	 */ 
	GetBodyHeight: function() {
		var body = document.getElementsByTagName("body")[0];
		return Util.GetHeight(body);
	},

	/** 
	 * Returns the body width
	 * @return integer
	 */ 
	GetBodyWidth: function() {
		var body = document.getElementsByTagName("body")[0];
		return Util.GetWidth(body);
	},
	
	/** 
	 * Returns the page offset y from scrolling
	 * @return integer
	 */
    getPageOffsetY: function() {
		var offsetY = window.pageYOffset || document.body.scrollTop || 0;
		
		return offsetY;
	},
	
    /** 
	 * Returns the current page size
	 * @return array
	 */
    getPageSize: function() {
	    var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}

		return [pageWidth,pageHeight];
	},
		
    /** 
	 * Returns the current page size
	 * @return array
	 */
    getViewportSize: function() {
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		return [windowWidth,windowHeight];
	},
	
	CLASS_NAME: 'Client'
};

/**
 * Creates new utilities functions
 * Namespace: TBView.Util
 */
Util = {
	
	/** 
	 * Returns true if object is empty
	 * @param object $object
	 * @return boolean
	 */ 
	isEmpty: function(object) {
		if(object == null || typeof object == 'undefined' || object == '') { return true; } else { return false; }
	},
		
	/** 
	 * Returns true if object is shown otherwise returns false
	 * @param object $object
	 * @return boolean
	 */ 
	isVisible: function(object) {
		if(object != null && typeof object != 'undefined' && object != '' && this.GetStyle(object, 'display') != 'none') { return true; } else { return false; }
	},
	
	/** 
	 * Returns true if needle is in text
	 * @param string $text
	 * @param string $needle
	 * @return boolean
	 */ 
	stristr: function(text, needle) {
		if(this.isEmpty(text)) { return false; }
		text = text.toLowerCase(); needle = needle.toLowerCase();
		if(text.indexOf(needle) > -1) { return true; } else { return false; }
	},
	
	/** 
	 * Returns an integer from str
	 * @param string $str
	 * @return integer
	 */ 
	intval: function(str) {
		str = parseInt(str);
		if(isNaN(str)) { return 0; } else { return str; }
	},
	
	/** 
	 * Returns the string stripped from html
	 * @param string $str
	 * @return string
	 */ 
	stripHTML: function(str) {
		str = str.replace('&nbsp;', ' ');
		str = str.replace(/<(?:.|\s)*?>/gi, '');
		str = str.replace(/^s+/, '').replace(/s+$/, '');
		if(str == ' ') str = '';
		return str;
	},
	
	/** 
	 * Returns the class name of the object
	 * @param object $obj
	 * @return string
	 */ 
	getObjectName: function(obj) {
		if (typeof obj != "object" || obj === null) {
			return '';
		}
		
		if(obj.CLASS_NAME && obj.CLASS_NAME != '') {
			return obj.CLASS_NAME;
		}
		
		return /(\w+)\(/.exec(obj.constructor.toString())[1];
	},
	
	/** 
	 * Returns the query with a glue for links
	 * @param string $query
	 * @return string
	 */ 
	getQueryGlue: function(query) {
		if(this.isEmpty(query) || query.substr(0,1) == '&') { return query; } else { return '&' + query; }
	},
	
	/** 
	 * Returns the object specified by id
	 * @param string $id
	 * @return object
	 */ 
	GetObject: function(id) {
		if(Client.isNS4()) { return null; } else { return document.getElementById(id); }
	},
	
	/** 
	 * Returns the link object specified by id
	 * @param string $id
	 * @return object
	 */ 
	GetLink: function(id) {
		if(Client.isNS4()) { return document.anchors[id]; } else { return document.getElementById(id); }
	},
	
	/** 
	 * Returns the layer object specified by id
	 * @param string $id
	 * @return object
	 */ 
	GetLayer: function(id) {
		if(Client.isNS4()) { return document.layers[id]; } else { return document.getElementById(id); }
	},
	
	/** 
	 * Returns the image object specified by id
	 * @param string $id
	 * @return object
	 */ 
	GetImage: function(id) {
		if(document.images && document.images[id]) { return document.images[id]; } else { return null; }
	},
	
	/** 
	 * Returns the x position specified by link object
	 * @param object $a
	 * @return integer
	 */ 
	GetAnchorX: function(a) {
		if(Client.isNS4()) { return a.x; } else { var val = a.offsetLeft; while (a && a.tagName != 'BODY') { val += a.offsetLeft; a = a.offsetParent; } return val; }
	},
	
	/** 
	 * Returns the y position specified by link object
	 * @param object $a
	 * @return integer
	 */ 
	GetAnchorY: function(a) {
		if(Client.isNS4()) { return a.y; } else { var val = a.offsetTop; while (a && a.tagName != 'BODY') { val += a.offsetTop; a = a.offsetParent; } return val; }
	},
	
	/** 
	 * Sets the style of an object
	 * @param object $object
	 * @param string $style
	 * @param string $value
	 */ 
	SetStyle: function(obj, st, val) {
		if(obj && st != '' && st != undefined && val != undefined) { if(Client.isNS4()) { try { eval('obj.'+st+'= "'+val+'"'); } catch(error) {}; } else { try { eval('obj.style.'+st+'= "'+val+'"'); } catch(error) {}; } }
	},
	
	/** 
	 * Sets the opacity of an object
	 * @param object $object
	 * @param integer $opacity
	 */ 
	SetOpacity: function (elem, opacity) {
			elem.style.opacity = (opacity / 100);
			elem.style.MozOpacity = (opacity / 100);
			elem.style.KhtmlOpacity = (opacity / 100);
			elem.style.filter = "alpha(opacity=" + opacity + ")";
	},
	
	/** 
	 * Returns the style value specified by style
	 * @param object $object
	 * @param string $style
	 * @return string
	 */ 
	GetStyle: function(obj, st) {
		if (obj && st != '' && st != undefined) { if(Client.isNS4()) { return eval('obj.'+st); } else { return eval('obj.style.'+st); } } else { return null; }
	},
	
	/** 
	 * Returns the width of specified object
	 * @param object $object
	 * @return integer
	 */ 
	GetWidth: function(obj) {
		if(Client.isNS4()) { return parseInt(obj.width); } else { return parseInt(obj.offsetWidth); }
	},
	
	/** 
	 * Returns the height of specified object
	 * @param object $object
	 * @return integer
	 */ 
	GetHeight: function(obj) {
		if(Client.isNS4()) { return parseInt(obj.height); } else { return parseInt(obj.offsetHeight); }
	},
	
	/** 
	 * Returns the content of the specified object
	 * @param object $object
	 * @return boolean
	 */ 
	GetContent: function(obj) {
		if(obj) { return new String(obj.innerHTML); } else { return ''; }
	},
	
	/** 
	 * Sets the content of an object
	 * @param object $object
	 * @param string $content
	 */ 
	SetContent: function(obj, content) {
		if(obj) { obj.innerHTML = content; }
	},
		
	/** 
	 * Sets the content of an object
	 * @param object $object
	 * @param string $content
	 */ 
	AddContent: function(obj, content) {
		if(obj) { obj.innerHTML += content; }
	},
	
	/** 
	 * Returns the x position of the specified object
	 * @param object $object
	 * @return integer
	 */ 
	GetX: function(obj) {
		if(Client.isNS4()) { return parseInt(obj.x); } else { var val = obj.offsetLeft; while (obj && obj.tagName != 'BODY') { val += obj.offsetLeft; obj = obj.offsetParent; } return parseInt(val); }
	},
	
	/** 
	 * Returns the y position of the specified object
	 * @param object $object
	 * @return integer
	 */ 
	GetY: function(obj) {
		if(Client.isNS4()) { return parseInt(obj.y); } else { var val = obj.offsetTop; while (obj && obj.tagName != 'BODY') { val += obj.offsetTop; obj = obj.offsetParent; } return parseInt(val); }
	},
	
	/** 
	 * Sets the styles of an object
	 * @param object $object
	 * @param string $styles
	 */ 
	SetStyles: function(obj, styles) {
		if(!obj) { return null; }; styles = styles.replace(/: /g, ":"); styles = styles.replace(/; /g, ";"); var arrStyles = styles.split(";"); for(var s=0;s<arrStyles.length;s++) { var style = arrStyles[s]; var arrStyle = style.split(":"); if(arrStyle[0] && arrStyle[1] && arrStyle[0] != '' && arrStyle[1] != '') { if(stristr(arrStyle[0], '-')) { var arrStyleName = arrStyle[0].split("-"); var FirstChar = arrStyleName[1].substr(0, 1); var LastChars = arrStyleName[1].substr(1); arrStyle[0] = arrStyleName[0] + FirstChar.toUpperCase() + LastChars; }; this.SetStyle(obj, arrStyle[0], arrStyle[1]); }; };
	},
	
	/** 
	 * Set the class name of an object
	 * @param string $name
	 */ 
	SetClass: function(obj, name) {
		if(!obj) { return null; };

		obj.className = name;		
	},
	
	/**
	 * Execute javascript in specified object text
	 * @param object $object
	 */
	ExecuteJavascript: function (object) {
		var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
		var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
		var bMoz = (navigator.appName == 'Netscape');
		var st = object.getElementsByTagName('script');
		var strExec;
		
		for(var i=0;i<st.length; i++) {
			if (bSaf) {
				strExec = st[i].innerHTML;
			} else if (bOpera) {
				strExec = st[i].text;
			} else if (bMoz) {
				strExec = st[i].textContent;
			} else {
				strExec = st[i].text;
			}
	
			try {
				eval(strExec);
			} catch(e) {
			}
		}
	
		return;
	},
		
	/**
	 * Creates a new text node
	 * @param string $text
	 * @return object
	 */
	CreateText: function (text) {
		return document.createTextNode(text);
	},
	
	/**
	 * Creates a new html element and returns it
	 * @param string $tag
	 * @param object $options
	 * @param object $styles
	 * @return object
	 */
	CreateElement: function (tag, options, styles) {
		var obj = document.createElement(tag);
		for(var property in options) {
        	var value = options[property];
            if(value !== undefined) {
                obj[property] = value;
            }
        }
		for(var property in styles) {
        	var value = styles[property];
            if(value !== undefined) {
                obj.style[property] = value;
            }
        }

		return obj;
	},
	
	/**
	 * Creates a div object and returns it
	 * @param string $id
	 * @param string $className
	 */
	CreateDiv: function (id, className) {
		return this.CreateElement('DIV', {
					'id': id,
					'name': name,
					'className': className
				}, {});
	},
	
	/**
	 * Creates a image object and returns it
	 * @param string $id
	 * @param string $className
	 */
	CreateImg: function (id, src, width, height, align, hspace, vspace) {
		return this.CreateElement('IMG', {
					'id': id,
					'name': name,
					'border': 0,
					'src': ((src != '') ? (TBView.getRoot() + 'layout/images/' + src) : ''),
					'width': width,
					'height': height,
					'align': align,
					'hspace': hspace,
					'vspace': vspace
				}, {});
	},
	
	CLASS_NAME: 'Util'
};


/***
 * Creates new switch functions
 * Namespace: Switch
 */
Switch = {
		
	/** 
	 * Switch the layer display mode
	 * @param object $lay
	 * @param boolean $on
	 */
	SwitchLayer: function(lay, on) {
		if (lay) { if(Client.isNS4()) { lay.visibility=on ? 'show' : 'hide'; lay.display=on ? 'block' : 'none'; return true; } else { lay.style.visibility=on ? 'visible' : 'hidden'; lay.style.display=on ? 'block' : 'none'; return true; } } else { return false; }
	},
		
	/** 
	 * Switch the image display mode (Add '_over' string to image source)
	 * @param string $id
	 * @param boolean $on
	 */
	SwitchImage: function(id, on) {
		var img = Util.GetImage(id); if(!img) { return; }; if(on == '1' || on == 1) { if(!stristr(img.src, '_over')) { img.src = img.src.replace(/\.gif/g, '_over.gif'); }; } else { if(stristr(img.src, '_over')) { img.src = img.src.replace(/_over/g, ''); }; };
	},
		
	/** 
	 * Switch th cursor style of a given object
	 * @param object $object
	 * @param integer $type can be ('-1' => 'default', '0' => 'auto', '1' => 'pointer', '2' => 'move')
	 */
	SwitchCursor: function(obj, type) {
		if(type == '-1') { Util.SetStyle(obj, 'cursor', 'default'); } else if(type == '1') { Util.SetStyle(obj, 'cursor', 'pointer'); } else if(type == '2') { Util.SetStyle(obj, 'cursor', 'move'); } else { Util.SetStyle(obj, 'cursor', 'auto'); }
	},
		
	/** 
	 * Switch the class name of given object. Checks for given active classname to replace it or not.
	 * @param object $classObj
	 * @param string $classActive
	 */
	SwitchClass: function(classObj, classActive) {
		var currentClass = classObj.className; var Result = currentClass.search(classActive); if(Result != -1) { classObj.className = currentClass.replace(classActive, ''); Util.SetStyle(classObj, 'cursor', 'auto'); } else { classObj.className = currentClass + classActive; Util.SetStyle(classObj, 'cursor', 'pointer'); }
	},
		
	/** 
	 * Switch an object specified by id to status checked or not
	 * @param string $id
	 */
	SwitchChecked: function(id) {
		var obj = Util.GetObject(id); if(obj != null) { if(obj.checked) { obj.checked = false; } else { obj.checked = true; } }
	},
		
	/** 
	 * Switch an array of fields specified by fieldname to status checked
	 * @param string $formname
	 * @param string $fieldname
	 */
	SwitchCheckedAll: function(formname, fieldname) {
		var form = document.forms[formname]; var obj = form.elements[fieldname]; for(var i=0;i<obj.length;i++) { if(obj[i].checked) { obj[i].checked = false; } else { obj[i].checked = true; } }
	},
		
	/** 
	 * Switch a field given by form an name to specified status
	 * @param string $formname
	 * @param string $fieldname
	 * @param boolean $on
	 */
	SwitchDisabled: function(formname, fieldname, on) {
		var form = document.forms[formname]; if(form) { var field = form[fieldname]; if(field) { if(on) { field.disabled = true; Util.SetStyle(field, 'color', '#808080'); } else { field.disabled = false; Util.SetStyle(field, 'color', ''); } } }
	},
		
	/** 
	 * Switch one or more form fields for checked select
	 * @param object $select
	 * @param formname $string
	 * @param object $field can be more arguments in function request
	 */
	SwitchFieldByChecked: function(select, formname) {
		if(switchFieldByChecked.arguments.length > 2) { for(var f=2; f<switchFieldByChecked.arguments.length; f++) { var field = switchFieldByChecked.arguments[f]; if(select.checked) { document.forms[formname][field].disabled = false; } else { document.forms[formname][field].disabled = true; } } }
	},
		
	/** 
	 * Switch display status of a box (layer)
	 * @param string $boxId
	 */
	SwitchBox: function(boxId) {
		var lay = Util.GetLayer(boxId); if(!lay) { return; }; var on = (Util.GetStyle(lay, 'display') == 'none') ? true : false; var withArg = switchBox.arguments[1]; if(typeof withArg != 'undefined') { on = (withArg == '1' || withArg == 1) ? true : false; }; if(on) { this.SwitchLayer(lay, 1); this.SwitchImage('img' + boxId, 1); } else { TBView.Switch.SwitchLayer(lay, 0); this.SwitchImage('img' + boxId, 0); };
	},
		
	/** 
	 * Switch a select from source field to target field by given attribute
	 * @param string $formName
	 * @param string $sourceField
	 * @param string $targetField
	 * @param string $targetAttribute
	 */
	SwitchSelect: function(formName, sourceField, targetField, targetAttribute) {
		var objSourceField = document.forms[formName][sourceField];
		var objTargetField = document.forms[formName][targetField];
		var originalOptions = objTargetField.originalOptions;
		if(Util.isEmpty(originalOptions)) { originalOptions = new Array(); for(var o=(objTargetField.length-1);o>=0;o--) { originalOptions.unshift(objTargetField.options[o]); objTargetField.options[o] = null; }; objTargetField.originalOptions = originalOptions;
		} else { for(var o=(objTargetField.length-1);o>=0;o--) { objTargetField.options[o] = null; }; };
		for(var o=0;o<originalOptions.length;o++) { var option = originalOptions[o]; if(objSourceField.value == '' || objSourceField.value == option.getAttribute(targetAttribute) || option.getAttribute(targetAttribute) == '') { objTargetField.options[objTargetField.length] = option; }; };
	},

	CLASS_NAME: 'Switch'
};
