var checkflag = "true";
var timeout=null;
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 + "/";
		return baseURL + "/";
    }
}
function trim(str)
{  
     return str.replace(/^\s*|\s*$/g,"");  
}  
function check() 
{	
    var myCheckbox = document.form_all['check_all[]'];
	if( myCheckbox )
	{
		for(i=0; i<myCheckbox.length; i++)
		{
			myCheckbox[i].checked = checkflag;
		}
			checkflag = !checkflag;
	}

}
function checkEmail(val)
{
	if(val != "")
	{
   		 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(!filter.test(val) && val.length != 0)
		{
			return false;
		}
		else
	    {
			return true;

		}
	}
		
}
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;
}
function ajaxTimeout()
{
	if(xmlHTTP.readyState==1)
	{
		xmlHTTP.abort();
		alert("ไม่มีการตอบสนองจาก server");
		window.location = window.location.href;
	}
	clearTimeout(timeout);
}
function ajaxLoad2(url,method,data)
{
	var today = new Date();
	today.setTime( today.getTime() );
	url += "&td="+today;
	xmlHTTP = createXMLHttpRequest();
	if(method == "GET")
	{
		xmlHTTP.open("GET", url, false);
		xmlHTTP.send('');
	}
	else if(method == "POST")
	{
		xmlHTTP.open("POST", url, false);
		xmlHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHTTP.send(data);
	}
	var varMsg = xmlHTTP.responseText;
	if(varMsg == "redirect")
	{
		window.location = HOST_PATH;
	}
	return varMsg;
}
function ajaxLoad3(url,data)
{
	var today = new Date();
	today.setTime( today.getTime() );
	url += "&td="+today;
	xmlHTTP = createXMLHttpRequest();
	xmlHTTP.open("POST", url, false);
	xmlHTTP.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	xmlHTTP.send(data);
	var var_msg = xmlHTTP.responseText;
	return var_msg;
}
function ajaxLoad1(method,url,data,displayId,divLoad,actionDis,divFadeName,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";
	}
	else if(actionDis==2)
	{
		document.getElementById(divLoad).style.visibility = "visible";
	}
	else if(actionDis==3)
	{
		document.getElementById(divLoad).style.visibility = "visible";
		set_size_divfade(divFadeName);
	}
	else if(actionDis==4)  
	{
		document.getElementById(divLoad).style.display = "block";
		document.getElementById(displayId).style.display = "none";
	}
	

	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";
				}
				else if(actionDis==2)
				{
					document.getElementById(divLoad).style.visibility = "hidden";
				}
				else if(actionDis==3)
				{
					document.getElementById(divLoad).style.visibility = "hidden";
					set_disable_divfade(divFadeName);
				}
				else if(actionDis==4)
				{	
					document.getElementById(divLoad).style.display = "none";
					document.getElementById(displayId).style.display = "block";
				}
				document.getElementById(displayId).innerHTML = var_msg;
				if(FuncName!=null)
				{
					window[FuncName](var_msg);
					
					//return "true";
				}
				clearTimeout(timeout);
			}
		}
	};
	if(method=="POST")
	{
		xmlHTTP.send(data);
	}
	else
	{
		xmlHTTP.send('');
	}
}
function mouse_position(event,div_id)
{
	var div=document.getElementById(div_id);
	var left = event.clientX+5;
	if((left + div.offsetWidth) > document.body.clientWidth)
	{
		left -= (div.offsetWidth + 10);
	}
	var top = event.clientY;
	if((top + div.offsetHeight) > document.body.clientHeight)
	{
		top -= (div.offsetHeight +10);
	}
	top += document.documentElement.scrollTop;
	div.style.top = top + "px";
	div.style.left = left + "px";
}
function set_position_center(div_id,indexX,indexY)
{
	var div=document.getElementById(div_id);
	/*div.style.left= (document.documentElement.clientWidth/2)-(div.clientWidth/2)+(indexX)+"px";
	div.style.top = (document.documentElement.clientHeight/2)-(div.clientHeight/2)+used_scrollHeight()+(indexY)+"px";*/
	
	var tempLeft=(document.documentElement.clientWidth/2)-(div.clientWidth/2)+(indexX);
	var tempTop = (document.documentElement.clientHeight/2)-(div.clientHeight/2)+used_scrollHeight()+(indexY)
	if(tempTop < 0)
	{
		tempTop = 0;
	}
	if(tempLeft < 0 )
	{
		tempLeft =0;
	}
	div.style.left = tempLeft+"px";
	div.style.top = tempTop+"px";
	
}
function setSizeDivfade(id)
{
	var myWidth;
	var myHeight;
	if(document.documentElement.clientWidth<document.documentElement.scrollWidth)
	{
		myWidth = document.documentElement.scrollWidth;
	}
	else
	{
		myWidth = document.documentElement.clientWidth
	}
	if(document.documentElement.scrollHeight<document.documentElement.clientHeight)
	{
		myHeight = document.documentElement.clientHeight;
	}
	else
	{
		myHeight = document.documentElement.scrollHeight
	}
	document.getElementById(id).style.display = "block";
	document.getElementById(id).style.width = myWidth+"px";
	document.getElementById(id).style.height = myHeight+"px";
	

}
function set_disable_divfade(id)
{
	document.getElementById(id).style.width = 0+"px";
	document.getElementById(id).style.height = 0+"px";
	document.getElementById(id).style.display = "none";
}
function used_scrollHeight()
{
	var myScroll;
	if(typeof(window.innerHeight)=='undefined')  //is IE6+..............................
	{
		myScroll = document.documentElement.scrollTop;
	}
	else if(navigator.appName=="Netscape" || navigator.appName=="Opera")   //is firefox,safari,google chrome..............................
	{
		myScroll = window.pageYOffset;
	}
	return myScroll;
}

//......................without framwore use my project ................//
function getProjectsub(id,type,elementName1,elementDis1,classname)
{
	var url = HOST_PATH_ADMIN+"/ajax-file/get-projectsub.php?projmain_id="+id+"&type="+type+"&elementName1="+elementName1+"&classname="+classname;
	var varMsg = ajaxLoad2(url,"GET",null);
	document.getElementById(elementDis1).innerHTML = varMsg;
	//alert(var_msg);
}
function chgProvince(value)
{
	/*var memProvince=document.getElementById("memProvince");
	var txt=memProvince.options[memProvince.selectedIndex].text;*/
	var memCountry=document.getElementById("memCountry");
	if(value!="")
	{
		for(var i=0;i<memCountry.options.length;i++)
		{
			var tempCountry=memCountry.options[i].text;
			//document.write(tempCountry+"\n");
			if(tempCountry == "Thailand")
			{
				memCountry.selectedIndex = i;
				break;
			}
		}
	}
	else
	{
		memCountry.selectedIndex = 0;
	}
}
function chgCountry()
{
	var memCountry=document.getElementById("memCountry");
	var memProvince=document.getElementById("memProvince");
	var txtCountry=memCountry.options[memCountry.selectedIndex].text;
	if(txtCountry != "Thailand")
	{
		memProvince.selectedIndex = 0;	
		memProvince.disabled = true;
	}
	else
	{
		memProvince.disabled = false;
	}
	//alert(txt);
}
//......................without framwore use my project ................//

//......................refesh image code for form ................//
function refeshImage(idInner)
{
	var url = HOST_PATH+"/source/ajaxfile/verify-image-refesh.php?";
	var varMsg = ajaxLoad2(url,"GET",null);
	document.getElementById(idInner).innerHTML = varMsg;
}
//.................end refesh image code for form ................//


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 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 + "/";
    }
}
