var xmlHTTP;
var checkflag = "true";
var timeout=null;
/*................create XMLHttp..........*/
function createXMLHttpRequest()
{
	try
	{
		xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
    }
	catch(e)
	{
		try
		{
			xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e)
		{
			xmlHTTP = false;
		}
	}
	if(!xmlHTTP && document.createElement)
	{
			xmlHTTP = new XMLHttpRequest();
	}
	return xmlHTTP;
}
/*............end create XMLHttp..........*/
function ajaxTimeout()
{
	if(xmlHTTP.readyState==1)
	{
		xmlHTTP.abort();
		alert("ไม่มีการตอบสนองจาก server");
		window.location = window.location.href;
	}
	clearTimeout(timeout);
}
function ajaxLoad1(method,url,data,displayId,divLoad,actionDis,divBlock,divFade,FuncName)
{
	timeout=setTimeout("ajaxTimeout()",20000);
	var today = new Date();
	today.setTime( today.getTime() );
	url += "&td="+today;
	
	/*
		action=1 คือ ทำการเปิด div loading และปิดการแสดงของ content แล้วจึงนำค่าที่ได้จาก server มาแสดง
		action=2 คือ ทำการเปิด div loading(อาจจะอยู่กลางหน้าจอแล้วแต่กำหนด) และ content ก็ยังแสดงอยู่และนำค่าที่ได้จาก server มาแสดง
	*/
	if(actionDis==1)  
	{
		document.getElementById(divLoad).style.display = "block";
		document.getElementById(displayId).style.display = "none";
		//$('#div-content-show').fadeOut(1500);
		//$('#div-content-show').hide(1000);
	}
	else if(actionDis==2)
	{
		document.getElementById(divLoad).style.visibility = "visible";
	}
	else if(actionDis==3)
	{
		document.getElementById(divLoad).style.visibility = "visible";
		setSizeDivfade(divFade);
	}
	xmlHTTP = createXMLHttpRequest();
	xmlHTTP.open(method, url, true);
	if(method=="POST")
	{
		xmlHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	xmlHTTP.onreadystatechange=function()
	{
		if(xmlHTTP.readyState == 4 && xmlHTTP.status ==200)
		{
			if(var_msg == "redirect")
			{
				window.location = HOST_PATH;
			}
			else
			{
				var var_msg = xmlHTTP.responseText;
				if(actionDis==1)
				{	
					document.getElementById(divLoad).style.display = "none";
					//document.getElementById(displayId).style.display = "block";
					$('#'+displayId).fadeIn(1000);
					//$("#div-content-show").slideUp(5000);
					//$('#div-content-show').show(1000);
				}
				else if(actionDis==2)
				{
					document.getElementById(divLoad).style.visibility = "hidden";
				}
				else if(actionDis==3)
				{
					document.getElementById(divLoad).style.visibility = "hidden";
					setDisableDivfade(divFade);
				}
				if(divBlock!=null)
				{
					document.getElementById(divBlock).style.display = "block";
					//$("#"+divBlock).show(500);
				}
				document.getElementById(displayId).innerHTML = var_msg;
				//var targetOffset = $target.offset().top;
				if(FuncName!=null)
				{
				    // setTimeout("showDetail(1)",100);
					var func = FuncName+"()";
					setTimeout(func,100);
				}
				
				clearTimeout(timeout);
			}
		}
	};
	if(method=="POST")
	{
		xmlHTTP.send(data);
	}
	else
	{
		xmlHTTP.send('');
	}
}


function getBaseURL() 
{
    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
	else if(baseURL.match(/[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/))
	{
	    // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    
	}
    else {
        // Root Url for domain name
        return baseURL + "/";
    }
}
function showDialog(dialogName,fadeName,coorX,coorY)
{
	/*.............หาความกว้างและความสูง Screen ....................*/
	var documentWidth;
	var documentHeight;
	documentWidth = document.documentElement.clientWidth < document.documentElement.scrollWidth ? 
			      document.documentElement.scrollWidth : document.documentElement.clientWidth;
	documentHeight = document.documentElement.scrollHeight < document.documentElement.clientHeight ?
				   document.documentElement.clientHeight : document.documentElement.scrollHeight;
	/*.........END หาความกว้างและความสูง Screen ....................*/
		
	//.............assing ค่าให้ Dialog ................//
	var dialogWidth = $("#"+dialogName).outerWidth(true);
	var dialogHeight = $("#"+dialogName).outerHeight(true);
	$("#"+dialogName).css({
		 "position": "absolute",
		 "top": ($(window).height()/2)-(dialogHeight/2)+getUsedScrollbar('height')+(coorY),
		 "left": ($(window).width()/2)-(dialogWidth/2)+(coorX)
	});
	if(fadeName!='')
	{
		$("#"+fadeName).css({
			"height": documentHeight,
			"width" : documentWidth
		});
		$("#"+fadeName).show();
	}
	$("#"+dialogName).fadeIn("normal");
	//..........END assing ค่าให้ Dialog ................//
}
function closeDialog(dialogName,fadeName)
{
	$("#"+fadeName).hide();
	$("#"+dialogName).fadeOut("normal");
}
function getUsedScrollbar(type)
{
	if(type=='height')
	{
		/*...........หา Scrollbar Height ที่ถูกใช้ไป.................*/
		var usedScrollbar;
		if(typeof(window.innerHeight)=='undefined')  //is IE6+..............................
		{
			usedScrollbar = document.documentElement.scrollTop;
		}
		else if(navigator.appName=="Netscape" || navigator.appName=="Opera")   //is firefox,safari,google chrome..............................
		{
			usedScrollbar = window.pageYOffset;
		}
		/*.........END หาความกว้างและความสูง Screen ....................*/
	}
	return usedScrollbar;
}
function setElementCenter(elementName,coorX,coorY)
{
	$("#"+elementName).css({
		 "position": "absolute",
		 "top": ($(window).height()/2)-($("#"+elementName).outerHeight(true)/2)+getUsedScrollbar('height')+(coorY),
		 "left": ($(window).width()/2)-($("#"+elementName).outerWidth(true)/2)+(coorX)
	});
	$('#'+elementName).show();
}
function trim(str)
{  
     return str.replace(/^\s*|\s*$/g,"");  
} 

// ฟังก์ชั่นแสดงกล่องข้อความ Tooltip   
function startTooltip(event,obj,indexX,indexY,delay) 
{ 
	 var locateX=event.clientX;     
	 
	 var locateY=(event.clientY)+getUsedScrollbar('height');   
	 locateX+=indexX;  
	 locateY-=indexY;
  
	 var elOffsetWidth=$("#"+obj).outerWidth(true);;
	 var elOffsetHeight=$("#"+obj).outerWidth(true);;
	 if((locateX + elOffsetWidth) > document.body.clientWidth)
	 {
		 locateX -= (elOffsetWidth);
	 }
	 /*if((locateY + elOffsetHeight) > document.body.clientHeight)
	 {
		 locateY -= (elOffsetHeight);
	 }*/
	 $("#"+obj).fadeIn(delay).css({
		 left:locateX,  
		 top:locateY
	 });    
}
var checkflag = "true";
function checkboxAll(objName) 
{	
	var objCheckbox=document.getElementsByName(objName);
	for(i=0;i<objCheckbox.length;i++)
	{
		objCheckbox[i].checked = checkflag;
	}
	checkflag = !checkflag;
}  
function getQuerystring(key, default_)
{
	if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(window.location.href);
	if(qs == null)
		return default_;
	else
		return qs[1];
}
function getAbsolutePath() {
    var loc = window.location;
    var pathName = loc.pathname.substring(0, loc.pathname.lastIndexOf('/') + 1);
    return loc.href.substring(0, loc.href.length - ((loc.pathname + loc.search + loc.hash).length - pathName.length));
}
function get_param(param)
{
	var param2=param.split("@");
	var qstr="";
	for(i=0;i<param2.length;i++)
	{
		qstr += "&para[]="+param2[i];
	}
	return qstr;
}
function get_ArrayParam(obj)
{
	var qstr="";
	var temp="";
	for(var i=0;i<obj.length;i++)
	{
		if(obj[i].checked)
		{
			qstr += "&"+obj[i].name+"="+obj[i].value;
		}
	}
	return qstr;
}




