var Client = {
	Engine: {'name': 'unknown', 'version': ''},
	Features: {}
};

Client.Features.xhr = !!(window.XMLHttpRequest);
Client.Features.xpath = !!(document.evaluate);

if (window.opera) Client.Engine.name = 'opera';
else if (window.ActiveXObject) Client.Engine = {'name': 'ie', 'version': (Client.Features.xhr) ? 7 : 6};
else if (!navigator.taintEnabled) Client.Engine = {'name': 'webkit', 'version': (Client.Features.xpath) ? 420 : 419};
else if (document.getBoxObjectFor != null) Client.Engine.name = 'gecko';
Client.Engine[Client.Engine.name] = Client.Engine[Client.Engine.name + Client.Engine.version] = true;


$ = function(){
	return document.getElementById(arguments[0]);
}

Function.prototype.bind = function(obj) {
	var method = this,
	temp = function() {
		return method.apply(obj, arguments);
	};
	
	return temp;
}

function show(id) {
	$(id).style.display = '';
}

function hide(id) {
	$(id).style.display = 'none';
}

function cancelBubble(ev) {
	ev = ev || window.event;
	isIE ? ev.cancelBubble = true : ev.stopPropagation();
}


function makeFormValueStr(form) {
	var str = '';
	var e = null;
	for (var i = 0; i < form.elements.length; i++) {
		e = form.elements[i];
		if (e.type != 'button' && e.type != 'submit')
			str += e.name + '=' + e.value + '&';
	}
	str = str.substr(0, str.length - 1);

	return str;
}

function makeFormValueStr_(obj) {
	var str = '';
	for (i = 0; i < obj.childNodes.length; i++) {
		if (obj.childNodes[i].tagName == 'INPUT') {
			if (obj.childNodes[i].type == 'text') {
				str += obj.childNodes[i].name + '=' + obj.childNodes[i].value + '&';
			}
			if (obj.childNodes[i].type == "checkbox") {
				if (obj.childNodes[i].checked) {
					str += obj.childNodes[i].name + '=' + obj.childNodes[i].value + '&';
				} else {
					str += obj.childNodes[i].name + '=&';
				}
			}
			if (obj.childNodes[i].type == "radio") {
				if (obj.childNodes[i].checked) {
					str += obj.childNodes[i].name + '=' + obj.childNodes[i].value + '&';
				}
			}
		}
		if (obj.childNodes[i].tagName == 'SELECT') {
			var sel = obj.childNodes[i];
			str += sel.name + '=' + sel.options[sel.selectedIndex].value + '&';
		}
		
	}
	return str;
}

/**
 * The object should set maxOpacity(0~100)
 */
function showFade(id, speed) {
	show(id);
	var opacity = $(id).style.opacity * 100;
	if (opacity <= $(id).maxOpacity) {
		opacity += speed
		_setOpacityI(id, opacity);
		$(id).tm = setTimeout('showFade("' + id + '",' + speed + ')', speed);
	} else {
		clearTimeout($(id).tm);
		//$(id).style.opacity = $(id).maxOpacity;
	}
}

/**
 * The object should set minOpacity(0~100)
 */
function hideFade(id, speed) {
	var opacity = $(id).style.opacity * 100;
	if (opacity > $(id).minOpacity) {
		opacity -= speed
		_setOpacityI(id, opacity);
		$(id).tm = setTimeout('hideFade("' + id + '",' + speed + ')', speed);
	} else {
		clearTimeout($(id).tm);
		hide(id);
		//$(id).style.opacity = $(id).minOpacity;
	}
}


function _setOpacityO(o, value) {
	o.style.opacity = value / 100;
	o.style.filter = 'alpha(opacity=' + value + ')';
}

function _setOpacityI(id, value) {
	$(id).style.opacity = value / 100;
	$(id).style.filter = 'alpha(opacity=' + value + ')';
}

function _setOpacityI_A(id, value) {
	var n = $(id).childNodes;
	for(var i = 0; i < n.length; i++) {
		if (n[i].nodeName) {
			_setOpacityO(n[i], value);
		}
	}
	//$(id).style.filter = 'alpha(opacity=' + value + ')';
}

function setTitle(str) {
	document.title = str;
}

function fixHeight(targetId, height) {
	if ($(targetId).clientHeight < height)
		$(targetId).style.height = height + 'px';	
}

function changeCSS(theClass, element, value) {
	var cssRules;
	if (document.all) {
		cssRules = 'rules';
	} else if (document.getElementById) {
		cssRules = 'cssRules';
	}
	for (var S = 0; S < document.styleSheets.length; S++) {
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
				document.styleSheets[S][cssRules][R].style[element] = value;
			}
		}
	}
	//changeCSS('.exampleA','color','black')
}

function loadJavascript(path, jsFiles) {
	var jsFiles = typeof jsFiles == 'object' ? jsFiles : [jsFiles];
	for (var i = 0; i < jsFiles.length; i++) {
		var newScript = document.createElement('script');
		newScript.setAttribute('type', 'text/javascript');
		newScript.setAttribute('src', path + jsFiles[i]);
		document.getElementsByTagName('head')[0].appendChild(newScript);
	}
}

function loadCSS(path, cssFiles) {
	var cssFiles = typeof cssFiles == 'object' ? cssFiles : [cssFiles];
	for (var i = 0; i < cssFiles.length; i++) {
		var newCSS = document.createElement('link');
		newCSS.setAttribute('type', 'text/css');
		newCSS.setAttribute('rel', 'stylesheet');
		newCSS.setAttribute('media', 'all');
		newCSS.setAttribute('href', path + cssFiles[i]);
		document.getElementsByTagName('head')[0].appendChild(newCSS);
	}
}

function getEvtOwner(e) {
	if (!e) e = window.event;
	if (e) {
		var srcEl = e.target;
		if (!srcEl)
			srcEl = e.srcElement ? e.srcElement : e.originalTarget.parentNode;
	}
	return srcEl;
}

