/* ================================================================ 
@Author : sora.yukinai@theknights
@Since : 20100414
@Ver 1.1.100708

	core :
		alert			(1.1.100614)
		attr			(1.1.100614)
		child			(1.1.100625)
		form			(1.1.100614)
		id				(1.1.100614)
		ie				(1.1.100614)
		
	ajax :
		ajaxGet			(1.1.100614)
		ajaxPost		(1.1.100614)
		ajaxResponse	(1.1.100614)
		http_request	(1.1.100614)
		makeRequest		(1.1.100614)
		
	css :
		css				(1.1.100614)
		
	html :
		cover			(1.1.100708)
		innerHtml		(1.1.100614)
		url				(1.1.100614)
	
	tag :
		tags			(1.1.100707)
		tagValue		(1.1.100707)
	
	xml :
		xmlAttr			(1.1.100707)
		xmlChilds		(1.1.100707)
		xmlDecode		(1.1.100614)
		xmlValue		(1.1.100707)
		
	form :
		checkField		(1.1.100618)
		fieldAlert		(1.1.100614)
		focus			(1.1.100614)
		isBlank			(1.1.100625)
		maxLength		(1.1.100625)
		updateField		(1.1.100614)
=================================================================== */

function $js(_fnName){
	_$ = function(){
	
//--------------------------------------- core  -----------------------------------------
		this.alert = function(msg){
			alert(msg);
		}
		
		this.attr = function(obj, attrName){
			try{
				if(typeof(obj)=='string'){
					obj = id(obj);
				}
				obj = obj.attributes.getNamedItem(attrName).nodeValue;
			}
			catch(err){
				this.alert(obj + ', ' + attrName + '\n' + err);
			}
			return obj;
		}
		
		this.child = function(obj, index){
			try{
				if(typeof(obj)=='string'){
					obj = this.id(obj);
				}
				obj = obj.childNodes;
				if(index!=null && index>-1){
					obj = obj[index];
				}
			}
			catch(err){
				this.alert(obj + '\n' + err);
			}
			return obj;
		}
		
		this.form = function(obj){
			try{
				if(typeof(obj)=='string'){
					obj = document.forms[obj];
				}
			}
			catch(err){
				this.alert(obj + '\n' + err);
			}
			return obj;
		}
		
		this.id = function(obj){
			try{
				if(typeof(obj)=='string'){
					obj = document.getElementById(obj);
				}
			}
			catch(err){
				this.alert(obj + '\n' + err);
			}
			return obj;
		}
		
		this.ie = function(){
			var result = false;
			try{
				var userAgent = navigator.userAgent.toLowerCase();
				result = (userAgent.indexOf('msie') > 0);
			}
			catch(err){
				this.alert(err);
			}
			return result;
		}
//--------------------------------------- core end -----------------------------------------

//--------------------------------------- ajax -----------------------------------------
		this.http_request = false;
		
		this.ajaxGet = function(url, param, returnType) {
			if(param==null){
				param = '';
			}
			else{
				if( (param.indexOf('?')==0) || (param.indexOf('&')==0) ){
					param = param.substring(1);
				}
			}
			param = encodeURI(param);
			this.makeRequest('GET', url, param, returnType);
		}
		
		this.ajaxPost = function(url, param, returnType) {
			if(param==null){
				param = '';
			}
			else{
				if( (param.indexOf('?')==0) || (param.indexOf('&')==0) ){
					param = param.substring(1);
				}
			}
			param = encodeURI(param);
			this.makeRequest('POST', url, param, returnType);
		}
		
		/*
			@ref : http://wiki.moztw.org
		*/
		this.ajaxResponse = function(returnType) {
			if (this.http_request.readyState == 4) {
				if (this.http_request.status == 200) {
					try{
						var responseText = this.http_request.responseText;
						if( responseText!='' && (responseText.indexOf('filterRedirect=')>-1) ){
							var url = this.http_request.responseText;
							responseText = responseText.substring(responseText.lastIndexOf('=')+1, responseText.length);
							location.href = responseText;
						}
						else{
							if(returnType=='xml'){
								var xmlDoc = this.http_request.responseXML;
								ajaxResponse(xmlDoc);
							}
							else{
								ajaxResponse(responseText);
							} 
						}
					}
					catch(err){}
				} else {
					this.alert("Connection Fail.");
				}
			}
		}
		
		/*
			@ref : http://wiki.moztw.org
		*/
		this.makeRequest = function(method, url, param, returnType) {
			this.http_request = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
				this.http_request = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) { // IE
				try {
					this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
					this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (err) {
						this.alert('Browser not support.');
					}
				}
			}
			if(this.http_request){
				if (this.http_request.overrideMimeType) {
					var mimeType = returnType=='xml' ? 'text/xml' : 'text/html';
					this.http_request.overrideMimeType(mimeType);
				}
				this.http_request.onreadystatechange = function(){
					eval(_fnName + '.ajaxResponse(returnType)');
				}
				if(param.length == 0){
					param = returnType=='xml' ? ('ajax=xml' + param) : ('ajax=text' + param);
				}
				else{
					param = returnType=='xml' ? ('ajax=xml&' + param) : ('ajax=text&' + param);
				}
				if(method=='GET'){
					url += '?' + param;
					param = null;
				}
				this.http_request.open(method, url, true);
				if(method=='POST'){
					this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					this.http_request.setRequestHeader("Content-length", param.length);
					this.http_request.setRequestHeader("Connection", "close");
				}
				this.http_request.send(param);
			}
			else{
				this.alert('Giving up :( Cannot create an XMLHTTP instance');
				return false;
			}
		}
//--------------------------------------- ajax end -----------------------------------------

//--------------------------------------- css -----------------------------------------
		this.css = function(obj, cssClass){
			try{
				obj = this.id(obj);
				if(this.ie()){
					obj.setAttribute('className', cssClass);
				}
				else{
					obj.setAttribute('class', cssClass);
				}
			}
			catch(err){
				this.alert(err);
			}
		}
//--------------------------------------- css end -----------------------------------------

//--------------------------------------- html -----------------------------------------
		/**
			@Dependences jquery.blockUI.js
		*/
		// cover(cmd, message)		block whole page with message / unblock whole page
		// cover(cmd, message, className)		block target className with message / unblock target className
		this.cover = function(){
			var _fn = 'cover\n';
			try{
				var args = this.cover.arguments;
				if(args==null){
					this.alert(_fn + 'input invalid\nargs : ' + args);
				}
				else{
					if(args.length==2){
						if(args[0]=='block'){
							$.blockUI({
								message : args[1],
								css : {
						        	cursor : 'normal'
								},
								overlayCSS : {
									backgroundColor : '#000',
						        	cursor : 'normal',
									opacity : 0.3
								}
							});
						}
						else if(args[0]=='unblock'){
							$.unblockUI();
						}
					}
					else if(args.length==3){
						if(args[0]=='block'){
							$('div.' + args[2]).block({
								message : args[1],
								css : {
						        	cursor : 'normal'
								},
								overlayCSS : {
									backgroundColor : '#000',
									opacity : 0.3
								}
							});
						}
						else if(args[0]=='unblock'){
							$('div.' + args[2]).unblock();
						}
					}
					else{
						this.alert(_fn + 'input invalid\nargs.length : ' + args.length);
					}
				}
			}
			catch(err){
				this.alert(_fn + 'err : ' + err);
			}
		}
		
		this.innerHtml = function(obj, content){
			try{
				obj = this.id(obj);
				obj.innerHTML = content;
			}
			catch(err){
				this.alert(err);
			}
		}
		
		this.url = function(obj, self){
			try{
				if(self){
					location.href = obj;
				}
				else{
					window.open(obj);
				}
			}
			catch(err){
				this.alert(err);
			}
		}
//--------------------------------------- html end -----------------------------------------

//--------------------------------------- tag -----------------------------------------
		this.tags = function(obj, tagName){
			try{
				if(obj==null){
					obj = document;
				}
				else if(typeof(obj)=='string'){
					obj = this.id(obj);
				}
				obj = obj.getElementsByTagName(tagName);
			}
			catch(err){
				this.alert(obj + ', ' + tagName + '\n' + err);
			}
			return obj;
		}
		
		// tagValue(tag, tagName, xmlDecode)	get tag value for unique tag
		this.tagValue = function(tag, tagName, xmlDecode){
			var _result = null;
			var _fn = 'tagValue\n';
			try{
				var args = this.tagValue.arguments;
				if(args==null){
					this.alert(_fn + 'input invalid\nargs : ' + args);
				}
				else{
					if(args.length==2 || args.length==3){
						_result = '';
						// args[0] - tag
						// args[1] - tagName
						if(args[0]==null){
							args[0] = this.tags(null, args[1])[0];
							_result = args[0].childNodes[0].nodeValue;
						}
						else{
							if(args[0].tagName!=null && args[0].tagName.indexOf(args[1])>-1){
								_result = args[0].childNodes[0].nodeValue;
							}
						}
						if(args.length==3){
							// args[2] - xmlDecode
							if(args[2]!=null && args[2]){
								_result = this.xmlDecode(_result);
							}
						}
					}
					else{
						this.alert(_fn + 'input invalid\nargs.length : ' + args.length);
					}
				}
			}
			catch(err){
				this.alert(_fn + 'err : ' + err);
			}
			return _result;
		}
//--------------------------------------- tag end -----------------------------------------

//--------------------------------------- xml -----------------------------------------
		// xmlAttr(xmlDoc, tagName, attrName)	get xml attr for unique tag
		// xmlAttr(xmlDoc, tagName, attrName, index)	get xml attr or attr list for non-unique tag
		this.xmlAttr = function(){
			var _result = null;
			var _fn = 'xmlAttr\n';
			try{
				var args = this.xmlAttr.arguments;
				if(args==null){
					this.alert(_fn + 'input invalid\nargs : ' + args);
				}
				else{
					if(args.length==3){
						// args[0] - xmlDoc
						// args[1] - tagName
						// args[2] - attrName
						_result = '';
						var _tags = this.tags(args[0], args[1]);
						if(_tags!=null){
							_result = this.attr(tags[0], args[2]);
						}
					}
					else if(args.length==4){
						// args[0] - xmlDoc
						// args[1] - tagName
						// args[2] - attrName
						// args[3] - index
						var _tags = this.tags(args[0], args[1]);
						if(args[3]==null){
							_result = new Array(_tags.length);
							if(_tags!=null){
								for(var i=0 ; i<_tags.length ; i++){
									_result[i] = this.attr(tags[i], args[2]);
								}
							}
						}
						else{
							_result = '';
							if(_tags!=null){
								_result[i] = this.attr(tags[args[3]], args[2]);
							}
						}
					}
					else{
						this.alert(_fn + 'input invalid\nargs.length : ' + args.length);
					}
				}
			}
			catch(err){
				this.alert(_fn + 'err : ' + err);
			}
			return _result;
		}
		
		// xmlChilds(xmlDoc, tagName)	get xml childs for unique tag
		// xmlChilds(xmlDoc, tagName, index)	get xml childs or childs list for non-unique tag
		this.xmlChilds = function(xmlDoc, tagName){
			var _result = null;
			var _fn = 'xmlChilds\n';
			try{
				var args = this.xmlChilds.arguments;
				if(args==null){
					this.alert(_fn + 'input invalid\nargs : ' + args);
				}
				else{
					if(args.length==2){
						// args[0] - xmlDoc
						// args[1] - tagName
						var _tags = this.tags(xmlDoc, tagName);
						if(_tags!=null){
							_result = _tags[0].childNodes;
						}
					}
					else if(args.length==3){
						// args[0] - xmlDoc
						// args[1] - tagName
						// args[2] - index
						var _tags = this.tags(args[0], args[1]);
						if(args[2]==null){
							_result = new Array(_tags.length);
							if(_tags!=null){
								for(var i=0 ; i<_tags.length ; i++){
									_result = _tags[i].childNodes;
								}
							}
						}
						else{
							_result = _tags[args[2]].childNodes;
						}
					}
					else{
						this.alert(_fn + 'input invalid\nargs.length : ' + args.length);
					}
				}
			}
			catch(err){
				this.alert(_fn + 'err : ' + err);
			}
			return _result;
		}
		
		/*
			ref : org.theknights.lib.util.Security
		*/
		this.xmlDecode = function(value){
			var result = '';
			try{
				value = value.replace(/\&lt;/g, '<');
				value = value.replace(/\&gt;/g, '>');
				value = value.replace(/\&amp;/g, '&');
				value = value.replace(/\&quot;/g, '\\');
				value = value.replace(/\&apos;/g, '\'');
				result = value;
			}
			catch(err){
				this.alert(err);
			}
			return result;
		}
		
		// xmlValue(xmlDoc, tagName)	get xml value for unique tag
		// xmlValue(xmlDoc, tagName, index)	get xml value or value list for non-unique tag
		this.xmlValue = function(){
			var _result = null;
			var _fn = 'xmlValue\n';
			try{
				var args = this.xmlValue.arguments;
				if(args==null){
					this.alert(_fn + 'input invalid\nargs : ' + args);
				}
				else{
					if(args.length==2){
						// args[0] - xmlDoc
						// args[1] - tagName
						_result = '';
						var _tags = this.tags(args[0], args[1]);
						if(_tags!=null){
							_result = _tags[0].childNodes[0].nodeValue;
						}
					}
					else if(args.length==3){
						// args[0] - xmlDoc
						// args[1] - tagName
						// args[2] - index
						var _tags = this.tags(args[0], args[1]);
						if(args[2]==null){
							_result = new Array(_tags.length);
							if(_tags!=null){
								for(var i=0 ; i<_tags.length ; i++){
									_result[i] = _tags[i].childNodes[0].nodeValue;
								}
							}
						}
						else{
							_result = '';
							if(_tags!=null){
								_result = _tags[args[2]].childNodes[0].nodeValue;
							}
						}
					}
					else{
						this.alert(_fn + 'input invalid\nargs.length : ' + args.length);
					}
				}
			}
			catch(err){
				this.alert(_fn + 'err : ' + err);
			}
			return _result;
		}
//--------------------------------------- xml end -----------------------------------------

//--------------------------------------- form -----------------------------------------
		this.checkField = function(obj, type, validator){
			try{
				var obj = this.id(obj);
				var value = obj.value;
				var isValid = !(this.isBlank(value));
				if(isValid){
					if(type!=null){
						if(type=='num'){
							isValid = !isNaN(value);
						}
						else if(type='email'){
							isValid = (/.+@.+\..+/.test(value));
						}
					}
				}
				return (isValid && validator);
			}
			catch(err){
				this.alert(err);
			}
		}
		
		this.fieldAlert = function(obj, isValid){
			var obj = this.id(obj);
			if(isValid){
				obj.style.border = '';
				obj.style.borderColor = '';
				obj.style.borderWidth = '';
			}
			else{
				obj.style.border = 'solid';
				obj.style.borderColor = 'red';
				obj.style.borderWidth = '1';
			}
		}
		
		this.focus = function(_id){
			this.id(_id).focus();
		}
		
		this.isBlank = function(obj){
			if(typeof(obj)!='string'){
				obj = obj.value;
			}
			return (obj==null || obj=='');
		}
		
		this.maxLength = function(obj, length){
			var objValue = this.id(obj).value;
			if(objValue.length > length){
				obj.value = objValue.substring(0, length);
			}
		}
		
		this.updateField = function(_id, value){
			try{
				this.id(_id).value = value;
			}
			catch(err){
				this.alert(id + ', ' + value + '\n' + err);
			}
		}
//--------------------------------------- form end -----------------------------------------
	
	}
	eval('window.' + _fnName + ' = new _$');
}
