﻿var msdivOutput = "#divContents_Center";
var gsdivOutput = "";
//*********************************************************************************************************************************
function getScrollX(){
	if (typeof window.pageXOffset == "number") 
		return window.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft)
		return document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft) 
		return document.body.scrollLeft; 
	else if (window.scrollX) 
		return window.scrollX;
	else
		return 0;
}

//*********************************************************************************************************************************
function getScrollY(){
	if (typeof window.pageYOffset == "number") 
		return window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		this.scrollY = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		return document.body.scrollTop; 
	else if (window.scrollY) 
		return window.scrollY;
	else 
		return 0;
}

//*********************************************************************************************************************************
function showDivProcess(tagdivName){
	//showOverlay();
	var y = getScrollY();
	var divProcess = document.getElementById(tagdivName);
	if (divProcess){
		var divW = divProcess.style.width;
		var divH = divProcess.style.height;
		divW = divW.replace('px', ''); divW = parseInt(divW);
		divH = divH.replace('px', ''); divH = parseInt(divH);		
		divProcess.style.left = (screen.width - divW)/2;
		divProcess.style.top = y + (screen.height-400)/2;	
		divProcess.style.display = 'block';
	}
}

//*********************************************************************************************************************************
function hiddenDivProcess(tagdivName){
	//hideOverlay();
	var divProcess = document.getElementById(tagdivName);
	if (divProcess) divProcess.style.display = 'none';
}

//*********************************************************************************************************************************
function showErrorDivProcess(tagdivName){
	var divProcess = document.getElementById(tagdivName);
	divProcess.innerHTML = "Không thể tải dữ liệu. Vui lòng click F5 để refresh";
}

//*********************************************************************************************************************************
function loadAjax(sURL, sdivOutput){
	if (!sdivOutput || sdivOutput == null){
		sdivOutput = msdivOutput;
	}else if (sdivOutput != ""){
		sdivOutput = "#" +  sdivOutput;
	}
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			timeout: 60000,
			url: sURL,
			success: function(data){$(sdivOutput).html(data)},
			error: function(){$(sdivOutput).html("Không thể tải dữ liệu. Vui lòng click F5 để refresh");}
		});//ajax	
	});	
}

//*********************************************************************************************************************************
function getAjax(sURL, sdivOutput, bIsShowLoading){
	if (!sdivOutput || sdivOutput == null){
		sdivOutput = msdivOutput;
	}else if (sdivOutput != ""){
		sdivOutput = "#" +  sdivOutput;
		msdivOutput = sdivOutput;
	}
	gsdivOutput = sdivOutput;
	
	if (bIsShowLoading == null) mbIsShowLoading = true;
	else mbIsShowLoading = bIsShowLoading;
	if (mbIsShowLoading) showDivProcess('divProcess');	
	$.ajax({
		type: "GET",
		timeout: 60000,
		url: sURL,
		success: showResponse,
		error: showError
	});//ajax	
}

//*********************************************************************************************************************************
function getFormData(oForm){
	var sRtn = "";
	for (i=0; i<oForm.elements.length; i++){
		if (!oForm.elements[i].disabled){
			if (sRtn == '') sRtn = oForm.elements[i].name + "=" + replaceAll(oForm.elements[i].value, ' ', '%20');
			else sRtn += "&" + oForm.elements[i].name + "=" + replaceAll(oForm.elements[i].value, ' ', '%20');
		}
	}
	return sRtn;
}

//*********************************************************************************************************************************
mbIsShowLoading = false;
function postAjax(oForm, sURL, sdivOutput, bIsShowLoading){
	if (sdivOutput != null && sdivOutput != ""){
		sdivOutput = "#" +  sdivOutput;
		msdivOutput = sdivOutput;
	}
	gsdivOutput = sdivOutput;
	
	if (bIsShowLoading == null) mbIsShowLoading = true;
	else mbIsShowLoading = bIsShowLoading;
	if (mbIsShowLoading) showDivProcess('divProcess');
	
	var sDataInput = getFormData(oForm);
	$.ajax({
		type: "POST",
		timeout: 60000,
		url: sURL,
		data: sDataInput,
		success: showResponse,
		error: showError
	});//ajax
}

//*********************************************************************************************************************************
function showResponse(sDataOutput, sStatus){ 
	$(msdivOutput).html(sDataOutput);
	if (mbIsShowLoading) hiddenDivProcess('divProcess');	
}

//*********************************************************************************************************************************
function showError(){ 
	$(msdivOutput).html("Không thể tải dữ liệu. Vui lòng click F5 để refresh");
	if (mbIsShowLoading) hiddenDivProcess('divProcess');
}

//*********************************************************************************************************************************
function loadExecute(surl_js){
	$.ajax({
		type: "GET",
		url: surl_js,
		dataType: "script"
	 })	
}

//*********************************************************************************************************************************
//Show Light Box
//*********************************************************************************************************************************
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset){
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){// Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//*********************************************************************************************************************************
function getPageSize(){	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//*********************************************************************************************************************************
function initOverlay(sdivProcess){	
	if (!document.getElementsByTagName) return;
	//create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	if (sdivProcess){
		objOverlay.onclick = function(){
			hiddenDivProcess(sdivProcess);
			return false;
		}
	}//if
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}

//*********************************************************************************************************************************
function showOverlay(){
	var objOverlay = document.getElementById('overlay');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
}

//*********************************************************************************************************************************
function hideOverlay(){
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.display = 'none';
}

//*********************************************************************************************************************************