var global_windmal_path = "plugins/windmal/";

function windmal_ajax() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   alert("XMLHttpRequest not supported");
   return null;
};

function windmal_loadingwindow(display){
	if(display==1){
		var x=158;
		var y=34;	 
		var nloa = document.createElement('div');
		nloa.setAttribute('id', 'windmal_loading'); 
		document.body.appendChild(nloa);
		nloa.style.width = x+"px";
		nloa.style.height = y+"px";
		nloa.innerHTML="<div class=\"windmal_loading_image\"><!-- --></div>";
		nloa.style.display = "block";
		windmal_staticbar('windmal_loading', x, y);		
	} else {
		document.body.removeChild(document.getElementById('windmal_loading'));
	}
}

function windmal(a, b, c, d, e){
	var objname="windmal_box";
	var contentobj="windmal_contentbox";	
	switch(a){
		case "close":
			windmal_openwindow(objname);
			return false;			
		break;
		case "content":
			windmal_content(b, c, d);
			return false;
		break;
	}
	var x = a;
	var y = b;
	var title = c;
	var sfn = d;
	var content_address = e;
	
	windmal_openground(1);
	windmal_loadingwindow(1);
	y=y+24;
	var windmal_onobj=document.getElementById(objname);
	windmal_onobj.style.width = x+"px";
	windmal_onobj.style.height = y+"px";	
 	var xmlhttp=windmal_ajax();
    xmlhttp.onreadystatechange = function () { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
                    windmal_onobj.innerHTML=xmlhttp.responseText;
					document.getElementById(contentobj).style.height=y-25;               
					windmal_loadingwindow(0);
	                windmal_staticbar(objname, x, y);
					windmal_openwindow(objname);
					windmal_content(content_address, title, sfn);
					return true;
               } 
          }
     };
     xmlhttp.open("GET", global_windmal_path+"html/window_design.html");
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
     xmlhttp.send(null);
}

//function windmalopenwindow(obj) {
function windmal_openwindow(obj) {
	var sDiv = document.getElementById(obj);
	if( sDiv.style.display == "block" ) {
		sDiv.style.display = "none";
		windmal_openground(0);		
		obj.innerHTML = "";
	} else {
		sDiv.style.display = "block";
	}
}

//function windmalopenground(display) {
function windmal_openground(display) {
	var id_ground = "windmal_ground";
 	if(display==1){
		var ndiv = document.createElement('div');
		ndiv.setAttribute('id', id_ground); 
		document.body.appendChild(ndiv);
		var ua = navigator.userAgent.toLowerCase();
		if(ua.indexOf("msie") != -1){
			 var bodix_he = document.body.parentNode.scrollHeight > document.body.parentNode.offsetHeight ? document.body.parentNode.scrollHeight : document.body.parentNode.offsetHeight;
			 ndiv.style.height = bodix_he+"px";
		}		
		ndiv.style.display = "block";
	} else {
		document.body.removeChild(document.getElementById('windmal_ground'));				
	}
	return true;
}

function windmal_content(content_address, title, sfn, var_a, var_b, var_c, var_d, var_e){
	switch(content_address){
		case "finished":
			content_address = global_windmal_path+"html/processfinish.html";
			window.windmal_ajustFinishedPage = function(){
				var path_ico = "public/img/";
				var ico = document.getElementById('windmal_finishedico');
				var title = document.getElementById('windmal_finishedtitle');
				var description = document.getElementById('windmal_finisheddesc');
				var secbutton = document.getElementById('windmal_button_a');
				if(var_a==1) ico.src = path_ico+"oksend.gif";
				if(var_a==2) ico.src = path_ico+"oinfo.gif";
				if(var_a==3) ico.src = path_ico+"nosend.gif";
				title.innerHTML = var_b;
				if(var_c){
					description.style.display = "block";
					description.innerHTML = var_c;
				}				
				if(var_d){
					secbutton.style.display = "inline";
					secbutton.value = var_e;
					secbutton.onclick = function(){
						eval(var_d);
					}
				}				
			}
		break;
	}
	document.getElementById('windmal_title').innerHTML=title;
	var obj_inner = document.getElementById('windmal_contentbox');
	obj_inner.innerHTML="<div class=\"windmal_loading_incontent\"><!-- --></div>";
 	var xmlhttp = windmal_ajax();
    xmlhttp.onreadystatechange = function() { 
          if(xmlhttp.readyState == 4) {
               if(xmlhttp.status == 200) {
                    obj_inner.innerHTML=xmlhttp.responseText;
                    if(var_a) windmal_ajustFinishedPage();
					if(sfn) eval(sfn);
               } 
          }
     };
     xmlhttp.open("GET", content_address);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
     xmlhttp.send(null);	
}

//function iecompattest(){
function windmal_iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

//function staticbar(obj, x, y){
function windmal_staticbar(obj, x, y){
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	var unlef = ns ? innerWidth : windmal_iecompattest().clientWidth;
	var untop = ns ? innerHeight : windmal_iecompattest().clientHeight;
	var startX = (unlef/2)-(x/2);
	var startY = (untop/2)-(y/2);
	function windmal_ml(id){
		var el=d.getElementById(id);
		if(d.layers)
			el.style=el;
		el.sP=function(x,y){
			this.style.left=x+"px";
			this.style.top=y+"px";
		};
		el.x = startX;
		el.y = startY;
		return el;
	}
	window.windmal_stayTopLeft=function(){
		var pY = ns ? pageYOffset : windmal_iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("windmal_stayTopLeft()", 10);
	}
	var ftlObj = windmal_ml(obj);
	windmal_stayTopLeft();
}

function windmail_startServices(){
	includeCSS(global_windmal_path+"css/windmal.css");
	var div_box = document.createElement('div');
	div_box.setAttribute('id', 'windmal_box'); 
	document.body.appendChild(div_box);	
}
