

//check site status by online form.
//once site is ok, will call methods

var check_site_method;
function call_after_check_site(myMethod){

	try{
		check_site_method = myMethod;
		run_check_site();
	}catch(e){
		alert(e.message);
	}
}


//****************************************
//     A timer call by other pages
//****************************************	
var check_site_timer = null
function run_check_site()
{
	if(check_site_timer!=null){
	   	check_site_timer =null;
	}
	
	//do count now:
	ajax_call("online_form.html?action=ajaxTestAvailable&r="+getRandom());
	
}
	
function start_check_site_timer()
{
	show_check_site_message();
    check_site_timer = self.setTimeout('run_check_site()', 2000)  
}	
	
//***************************************************
//     Ajax call back - call function
//***************************************************	
	
	

function ajax_callback(str){

	
    if (str.indexOf("<div>OK</div>")!=0){
    	start_check_site_timer();
    }else{
    	try{
    		if (check_site_method){
    			eval(check_site_method);
    		}
    	}catch(e){
    	}
    }
}


//***************************************************
//     Ajax call - check if site is available
//***************************************************	

	
function ajax_call(strURL) {
	try{
	
	
	    var xmlHttpReq = false;
	    var self = this;
	    // Mozilla/Safari
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    self.xmlHttpReq.open('POST', strURL, true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {
	            ajax_callback(self.xmlHttpReq.responseText);
	        }
	    }
	    self.xmlHttpReq.send("");
	    
	}catch(e){
		alert("Ajax Error: "+e.message);
		start_check_site_timer();
	}
}



function getRandom(){
	var max = 10000;
	var min = 1;
    var range = max - min;
    var rand = Math.random();
    return(min + Math.round(rand * range));
}



//***************************************************
//     Show DIV status - Checking Sites Status
//***************************************************	

function show_check_site_message(){
		
	var myWidth = getClientWidth();
	var myHeight = getClientHeight();
	
	var x = myWidth/2 - 300/2;
	var y = myHeight/2 - 100;
	
	showDivObj("div_checksite_text",x,y);
}

function get_check_site_message(){

var s ="\n<div id='div_checksite_text' name='div_checksite_text'";
	s+="\n	style='z-index: 9999999; position: absolute; float:inherit; display: none; border:3px orange solid; background: #FFFFFF; padding: 10px 10px 10px 10px; top=0; left=0; '>";
	s+="\n <table width='300' border='0' cellpadding='2'> ";
	s+="\n 	<tr> ";
	s+="\n 		<td colspan='2' class='normalText' align='center'> ";
	s+="\n 		<br>";
	s+="\n 		 <font color='red'><b>Notice</b></font>";

	s+="\n 		<br><br>";
	s+="\n 		System is now busy. Please do not close this page.<br>";
	s+="\n 		Will submit your data as soon as system is back.<br><br>";
	
	s+="\n 		</td> ";
	s+="\n 	</tr> ";
	s+="\n </table> ";
	s+="\n </div> ";

return s;

}




//***************************************************
//     Show DIV object
//***************************************************	


function showDivObj(divName,x,y){

	var divObj = document.getElementById(divName);
	if (!divObj){
		return;
	}


	divObj.style.display ="";	
	divObj.style.left   =x + getScrollX();
	divObj.style.top    =y + getScrollY();
}


function getElementX(e){
		var x=e.offsetLeft;
		while(e=e.offsetParent){
			x+=e.offsetLeft;
		}
		return x;
}
	
function getElementY(e){
		var y=e.offsetTop;
		while(e=e.offsetParent){
			y+=e.offsetTop;
		}
		return y;
}


//***************************************************
//     get screen size, get client window size
//***************************************************	


function getClientWidth() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight() {
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}


function getScrollX() {
	return getScrollXY()[0];
}

function getScrollY() {
	return getScrollXY()[1];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



//***************************************************
//     write a div message conent to doucment
//***************************************************	
document.write(get_check_site_message());
	