// JavaScript by Chutinai Boosarakham
var j = {
	
	// --- GetElement By ID -------------------------------------------------------------------------------------------------- //
	 g : function(){		 
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
				elements.push(element);
		}
		return elements;
	},
	 gParent : function(name){
		return window.parent.document.getElementById(''+name+'');
	},	
	 gFrame : function(name,iframe){
		return window.frames[iframe].document.getElementById(''+name+'');
	},		
	// --- Redirect ------------------------------------------------------------------------------------------------------------- //
	goTo : function(url) {
		eval(" window.location.href = ' " + url + "'; ");
	},
	goToParent : function(url) {
		eval(" window.parent.location.href = ' " + url + "'; "); 
	},	
	goTo_iframe : function(url,iframe) {
		eval(" window."+iframe+".location.href = ' " + url + "'; "); 
	},		
	
	// --- Window Reload ---------------------------------------------------------------------------------------------------- //
	Reload : function() {
		window.location.reload();
	},
	ReloadParent : function() {
		window.parent.location.reload();
	},	
	
	// --- Convert Integer --------------------------------------------------------------------------------------------------- //
	int : function(num) {
		return parseInt(num,10);
	},	
	
	// --- Trim String ---------------------------------------------------------------------------------------------------------- //
	trim : function(str){
		for(i=0; i<str.length; i++){
			 str = str.replace(String.fromCharCode(160),"");
			 str = str.replace(/^\s*|\s*$/g, "");
		 }
		return (str.replace(/^\s*|\s*$/g, ""));
	},
	
	// --- Method Overloading ---------------------------------------------------------------------------------------------- //
	//  j.addMethod(this, "funcName", function(Value1, Value2){ process });
	//  funcName("Value1", "Value2");
	addMethod : function(object, name, fn){
		var old = object[ name ];
		object[ name ] = function(){
			if ( fn.length == arguments.length )
				return fn.apply( this, arguments );
			else if ( typeof old == 'function' )
				return old.apply( this, arguments );
		}
	},
	
	// --- addEventListener  ------------------------------------------------------------------------------------------------ //
	//	j.addEvent(window, 'load', function(){} );
	 addEvent : function( obj, type, fn ) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) 
			obj.attachEvent('on' + type, function() { return fn.apply(obj, new Array(window.event));} );
	},
	
	// --- gen String ----------------------------------------------------------------------------------------------------------- //
	genString : function (xlength,op){
		var keylist="abcdefghijklmnopqrstuvwxyz123456789"; 
		var str = "";
		for (i=0;i<xlength;i++){ str += keylist.charAt(Math.floor(Math.random()*keylist.length)); }
		return str;
	},	
	
	// --- window.open ------------------------------------------------------------------------------------------------------- //
	openWin : function(URL, N, w, h, status, scrollbars, menubar, toolbar, resizable){ 
		var x = (screen.availWidth-w)/2; 
		var y = (screen.availHeight-h)/2;
		var strOpton = ',toolbar='+arguments[7]+',location=0,directories=0,status='+arguments[4]+',resizable='+arguments[8]+',menubar='+arguments[6]+',scrollbars='+arguments[5]+',copyhistory=no';
		switch(navigator.appName.toLowerCase()){
			case "netscape" : winProp = "width="+w+",height="+h+",screenX="+x+",screenY="+y+strOpton+""; break;
			case "opera" : winProp =	"width="+w+",height="+h+",left="+x+",top="+(y/2)+strOpton+""; break;			
			default : winProp =	"width="+w+",height="+h+",left="+x+",top="+y+strOpton+"";break;  /*IE and  other */
		}		
		Win = window.open(URL, N, winProp);
		if(parseInt(navigator.appVersion,10) >= 4) { Win.window.focus(); }
	},
	
	// --- pageWidth ---------------------------------------------------------------------------------------------------------- //
	pageWidth : function(){ // on page load
		return (typeof( window.innerWidth ) == 'number') ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : (document.body && document.body.clientWidth) ? document.body.clientWidth : null;
	},
	
	// --- pageHeight --------------------------------------------------------------------------------------------------------- //
	pageHeight : function(){ // on page load
		return (typeof( window.innerHeight ) == 'number') ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight ) ? document.documentElement.clientHeight : (document.body && document.body.clientHeight ) ? document.body.clientHeight : null;
	},
	
	// --- top Position --------------------------------------------------------------------------------------------------------- //
	PosXleft : function(){
		return (typeof(window.pageXOffset) == 'number') ? window.pageXOffset : (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : (document.body.scrollLeft) ? document.body.scrollLeft : 0;
	},
	
	// --- left Position --------------------------------------------------------------------------------------------------------- //
	PosYtop : function(){
		return (typeof(window.pageYOffset) == 'number') ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : (document.body.scrollTop) ? document.body.scrollTop : 0;
	},
	
	// --- setHideScroll Body ----------------------------------------------------------------------------------------------- //
	setHideScroll : function(hide){
		if(hide == "block"){   
			document.body.scroll = "no"; 
			document.body.style.overflow = "hidden";
			document.getElementsByTagName("body")[0].style.overflow = "hidden";
		}else{  
			document.body.scroll = "yes"; 
			document.body.style.overflow = "";   
			document.getElementsByTagName("body")[0].style.overflow = "";
		}
	},
	// --- onSelectText Body ----------------------------------------------------------------------------------------------- //
	onSelectText : function(obj, boolean){ // document.body
		obj.onselectstart = function() {return boolean;} // ie
		obj.onmousedown = function() {return boolean;} // mozilla , opera	
	},	
	
	
	// --- show Dialog bgDisable ------------------------------------------------------------------------------------------- //
	setDivbgDisable : function(){
		if( ! j.g('bgDisable') ){ 
			var obj = document.createElement('div');
			obj.setAttribute('id', 'bgDisable');						
			obj.style.display = 'none';		
			obj.style.position = 'absolute';
			obj.style.top = '0px';  obj.style.left = '0px'; 	
			obj.style.backgroundColor = '#666666';	
			obj.onblur = function(){ return false; }		
			obj.onclick = function(){ return false; }
			obj.ondblclick = function(){ return false; }
			obj.onchange = function(){ return false; }		
			obj.onmousedown = function(){ return false; }
			obj.onmousemove = function(){ return false; }
			obj.onmouseover = function(){ return false; }		
			obj.onmouseup = function(){ return false; }
			obj.onscroll = function(){ return false; }
			obj.style.zIndex = 99990;
			document.body.appendChild(obj);			
		}
	},
	showDialog : function(hide,obj_dialog,topx){
		j.setDivbgDisable();
		j.setHideScroll(hide);
		var left = j.PosXleft();  
		var top = j.PosYtop();
		var cw = j.pageWidth();  
		var ch = j.pageHeight() - topx;
		var bg = j.g("bgDisable");
		bg.style.width = "1000%";
		bg.style.height = "1000%";	
		document.body.onmousemove = function(){ 
			if(bg){
				var xMax = j.pageWidth()+j.PosXleft(); bg.style.height = ""+xMax+"%"; 
				var yMax = j.pageHeight()+j.PosYtop(); bg.style.width = ""+yMax+"%";
			}
		}
		bg.style.opacity = 0.5;
		bg.style.filter = "alpha(opacity=50)";	
		obj_dialog.style.top = ""+(top+(ch - obj_dialog.style.height.replace(/px/,""))/2)+"px";
		obj_dialog.style.left = ""+(left+(cw - obj_dialog.style.width.replace(/px/,""))/2)+"px";		
		obj_dialog.style.display = hide;
	},
	cornerDivshow : function(i,zIndex){
		var bxA = DOM.create("div","bxA"+i+"");	
		bxA.style.position = 'absolute';
		bxA.style.width = '150px';	
		bxA.style.height = '100px';		
		bxA.style.backgroundColor = 'white';		
		bxA.style.display = 'none';	
		bxA.style.padding = '0px';	
		bxA.style.zIndex = zIndex;		
		
		var bxB = DOM.create("div","bxB"+i+"");	
		var bxC = DOM.create("div","bxC"+i+"");	
		var bxD = DOM.create("div","bxD"+i+"");	
		bxC.appendChild(bxD);
		bxB.appendChild(bxC);	
		bxA.appendChild(bxB);		
		
		var bxE = DOM.create("div","bxE"+i+"");	
		var bxF = DOM.create("div","bxF"+i+"");	
		var bxG = DOM.create("div","bxG"+i+"");	
		bxF.appendChild(bxG);
		bxE.appendChild(bxF);	
		bxA.appendChild(bxE);	
		
		var bxH = DOM.create("div","bxH"+i+"");		
		var bxI = DOM.create("div","bxI"+i+"");	
		var bxJ = DOM.create("div","bxJ"+i+"");	
		bxI.appendChild(bxJ);
		bxH.appendChild(bxI);	
		bxA.appendChild(bxH);		
		
		var corn = DOM.create("div","corn"+i+"");	
		corn.align = 'left';
		corn.appendChild( DOM.img('../img/loading.gif','mainLoadingIMG','block') );
		bxG.appendChild(corn);
		document.body.appendChild(bxA);
	},		
	
	// --- Disable TabIndex ------------------------------------------------------------------------------------------------- //
	DisableTabIndex : function( nameArr , zIndex){ // Array : var objArr = Array('a','button','input','select','textarea','div','span');
		var objMain = new Array();
		for( var i = 0; i < nameArr.length; i++ ) {		
			objMain[i] = document.getElementsByTagName( ''+nameArr[i]+'' );
		}
		for( var i = 0 ; i < objMain.length; i++ ) {
			for( var j = 0 ; j < objMain[i].length; j++ ) {
				objMain[i][j].tabIndex = zIndex;
			}		
		}
	},	

	// --- Select DropDown ------------------------------------------------------------------------------------------------- //
	setSelectList : function(formName, selectListName, inValue){
		var e = self;
		if (inValue != null){
			for (var i = (e.document[formName][selectListName].length - 1); i >= 0 ; i--){
				if(inValue.toString() == (e.document[formName][selectListName].options[i].value.toString())){
					e.document[formName][selectListName].options[i].selected = true;
				}
			}
		}
	},
	
	// --- getObjInnerText --------------------------------------------------------------------------------------------------- //
	getObjInnerText : function(obj) { 
		return (obj.innerText) ? obj.innerText : (obj.textContent) ? obj.textContent : ''; 
	}, 
	
	// --- loadScript Javascript -------------------------------------------------------------------------------------------- //
	loadScript : function (src,name_id){ 
		var scriptTag = j.g(''+name_id+''); 
		if(scriptTag){ document.body.removeChild(scriptTag); }
		var script = document.createElement('script'); 
		script.setAttribute('src', ''+src+'');			
		script.setAttribute('id', ''+name_id+'');			
		script.setAttribute('type', 'text/javascript');
		document.body.appendChild(script);
	},
	
	// --- Random Number -------------------------------------------------------------------------------------------------- //	
	rand : function(){
		return Math.round(Math.random()*1000000);
	},
	
	// --- Cookie Read and Write ------------------------------------------------------------------------------------------ //	
	CookieWrite : function(name, value, hours){
	  var expire = "";
	  if(hours != null){
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	  } document.cookie = name + "=" + escape(value) + expire;
	},
	CookieRead : function(name){
	  var cookieValue = "";
	  var search = name + "=";
	  if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search);
		if (offset != -1){ 
		  offset += search.length;
		  end = document.cookie.indexOf(";", offset);
		  if (end == -1) end = document.cookie.length;
		  cookieValue = unescape(document.cookie.substring(offset, end))
		}
	  }  return cookieValue;
	},
	
	// --- Replace String ---------------------------------------------------------------------------------------------------- //	
	replaceStr : function(strString,strFind,strReplace) {
		while (strString.indexOf(strFind)>-1) {
			intPosition= strString.indexOf(strFind);
			strString = '' + (strString.substring(0, intPosition) + strReplace +
			strString.substring((intPosition + strFind.length), strString.length));
		}
		return strString;
	},	
	// --- onEmpty input -------------------------------------------------------------------------------------------------------- //	
	onEmpty : function(id_, time_){  
		var time_s= typeof time_ != 'undefined' ? time_ : 100;
		j.g(''+id_+'').focus(); window.setTimeout(" j.g('"+id_+"').className = 'bg_input_red'; ",  time_s );
	},		
	
	// --- submit Form -------------------------------------------------------------------------------------------------------- //	
	submitForm : function(objF,f_target,f_action){  
		objF.setAttribute('target',f_target);
		objF.setAttribute('action',f_action);
		objF.submit(); 	
	},
	
	// --- Get Url QueryString ---------------------------------------------------------------------------------------------- //			
	getUrl : function(){
	  var url = new String( window.location );
	  var len_url = url.length; 
	  var start_data = url.indexOf( "?" );
	  if ( parseInt(start_data,10) < 0 ){ return false; }else{ 
	  	  var alldata = url.substring( start_data+1, len_url );
	  }
		  return alldata;
	},
	
	// --- Hidden Select for Disable Bg IE6 ------------------------------------------------------------------------------ //	
   setHideSelect : function(none){   
	  var obN =document.getElementsByTagName('select');
      if(obN!=null){ for (i=0; i<obN.length; i++){ obN[i].style.display = ''+none+''; } }   
   },  
	
   // --- Get current page url --------------------------------------------------------------------------------------------- //	
	URL_Current : function (){ 
		return ( window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, "") );
	}
	
}; // end j

