/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var receiveReq = null;
var doClose = null;
var doRefresh = null;
/*-----------------------------------------------------------------------------------------------*/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS = "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------
window.onload = initialize;
window.onload = getBrowserInfo

-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
//

function createAjaxObj(){
  //window.alert("createAjaxObj");
  var httprequest=false
  if (window.XMLHttpRequest)
  { // for Mozilla, Safari
    httprequest=new XMLHttpRequest()
    if (httprequest.overrideMimeType)
      httprequest.overrideMimeType('text/xml')
   }
   else if (window.ActiveXObject)
   { // for IE
     try {
       httprequest=new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch (e){
       try{
          httprequest=new ActiveXObject("Microsoft.XMLHTTP");
       }
       catch (e){}
     }
   }
   return httprequest
}

function executeWithHandle(sendUrl,handleFunction){

	receiveReq = createAjaxObj();
	
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//
		receiveReq.open("POST", sendUrl, true);
		receiveReq.setRequestHeader('Cache-Control','no-cache');
		receiveReq.setRequestHeader('Content-Type','text/html;charset=gbk');
		if(handleFunction){
			receiveReq.onreadystatechange = handleFunction;
			receiveReq.send(null);
		}else{
			alert(handleFunction+"\n no such method define");
		}
		//window.alert(submitURL);
	}

}

function initialize(){
    //window.alert("function initialize()");
	addLightboxMarkup();
	getBrowserInfo();
	//
	//lbox = document.getElementsByClassName('lbOn');
	//for(i = 0; i < lbox.length; i++) {
	//	valid = new lightbox(lbox[i]);
	//}
}

// 
function addLightboxMarkup() {
	//window.alert("function addLightboxMarkup()");
	bod 			= document.getElementsByTagName('body')[0];
	overlay 		= document.createElement('div');
	overlay.id		= 'overlay';
	overlay.className	= 'overlay';
	lb				= document.createElement('div');
	lb.id			= 'lightbox';
	lb.className 	= 'lightbox';
	lb.innerHTML	= '<div id="close" class="lightboxclose">'
		+ '<img src="images/btcloseblue.gif" onclick="javascript:hiddenLightBox()"></div>'

		+ '<div id="lb_content" class="lightboxcontent">' 
		+ '<p><img src="images/indicator.gif">weitting ... </p></div>'; 

	bod.appendChild(overlay);
	bod.appendChild(lb);
	//window.alert("addLightboxMarkup");
}

function showLightBox() {
	//window.alert("lbLoadMessage");
	//window.alert(document.getElementById("lbLoadMessage"));
	document.getElementById("overlay").style.display='block';
	document.getElementById("lightbox").style.display='block';
	if(browser == 'Internet Explorer'){
		//window.alert("aaa");
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.display = 'none';
		}		
	}

}

function lightBoxResize(width, height){
	try{
		if(width){
			document.getElementById("lightbox").style.width=width;
		}
		if(height){
			document.getElementById("lightbox").style.height=height;
		}
	}catch(e){
	}
}

function hiddenLightBox() {
	//window.alert("lbLoadMessage");
	//window.alert(document.getElementById("lbLoadMessage"));
	document.getElementById("overlay").style.display='none';
	document.getElementById("lightbox").style.display='none';
	
	if(browser == 'Internet Explorer'){
		//window.alert("aaa");
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.display = '';
		}		
	}

}
/*
function executeInLightBox ( executeUrl ){
	alert("ddd");
	initialize();
	showLightBox();
	
	receiveReq = createAjaxObj();
	doClose=false;
	doRefresh = false;
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//
		submitURL = executeUrl;
		//window.alert(submitURL);
		if(submitURL.indexOf("AddToIndex")!=-1 || submitURL.indexOf("RemoveFromArea")!=-1){
			doClose=true;
			doRefresh=true;
		}
		receiveReq.open("POST", submitURL, true);
		receiveReq.setRequestHeader('Cache-Control','no-cache');
		receiveReq.setRequestHeader('Content-Type','text/html;charset=gbk');
		receiveReq.onreadystatechange = handleReceive;
		receiveReq.send(null);
		//window.alert(submitURL);
	}
}
*/
function executeInLightBox ( executeUrl, width, height){
	//alert("ddd");
	initialize();
	showLightBox();
	if(width&&height){
		lightBoxResize(width, height);
	}
	receiveReq = createAjaxObj();
	doClose=false;
	doRefresh = false;
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//
		submitURL = executeUrl;
		//window.alert(submitURL);
		if(submitURL.indexOf("AddToIndex")!=-1 || submitURL.indexOf("RemoveFromArea")!=-1){
			doClose=true;
			doRefresh=true;
		}
		receiveReq.open("POST", submitURL, true);
		receiveReq.setRequestHeader('Cache-Control','no-cache');
		receiveReq.setRequestHeader('Content-Type','text/html;charset=gbk');
		receiveReq.onreadystatechange = handleReceive;
		receiveReq.send(null);
		//window.alert(submitURL);
	}
}

function handleReceive(){
	if (receiveReq.readyState == 4) {//
		//window.alert(receiveReq.responseText);
		try{
			document.getElementById("lb_content").innerHTML = receiveReq.responseText;
		
			if(doClose){
				hiddenLightBox();
			}
			if(doRefresh){
				window.location.reload();
			}
		}catch(e){}
	}
}

function removeBlockFromArea (executeUrl){
	receiveReq = createAjaxObj();
	doClose=false;
	doRefresh = false;
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//
		submitURL = executeUrl;
		//window.alert(submitURL);
		
		doClose=true;
		doRefresh=true;
		
		receiveReq.open("POST", submitURL, true);
		receiveReq.setRequestHeader('Cache-Control','no-cache');
		receiveReq.setRequestHeader('Content-Type','text/html;charset=gbk');
		receiveReq.onreadystatechange = handleReceive1;
		receiveReq.send(null);
		//window.alert(submitURL);
	}
}

function handleReceive1(){
	if(receiveReq.readyState == 4) {//
		window.location.reload();
	}
}


