var XMLPopup = false;

function showPopup(obj, address, width, height) {
	var r;
	var divMain;
	var divClose;
	var divContainer;
	var divContent;
	var divScrollArea;
	var divScroller;
	r = MS.Position.getLocation(obj);
	
	if (document.getElementById("popupDiv_Main") == null) {
		divMain = document.createElement("div");
		divMain.id = "popupDiv_Main";
		divMain.style.backgroundColor = "White";
		divMain.style.width = (width+20) + "px";
		divMain.style.height = height + "px";
		divMain.style.zIndex = 1;
		document.body.appendChild(divMain);
		
		divClose = document.createElement("div");
		divClose.id = "popupDiv_Close";
		divClose.style.position = "absolute";
		divClose.style.overflow = "hidden";
		divClose.align="right";
		divClose.innerHTML = "<span style='cursor:pointer' onclick=\"document.getElementById('popupDiv_Main').style.display='none';\"><B><font color=\"#990000\">Kapat</font></B></span>"
		divMain.appendChild(divClose);

		divContainer = document.createElement("div");
		divContainer.id = "popupDiv_Container";
		divContainer.style.backgroundColor = "White";
		divContainer.style.position = "absolute";
		divContainer.style.overflow = "hidden";
		divContainer.style.border = "1px";
		divContainer.style.borderColor = "#666";
		divContainer.style.borderStyle = "solid";
		divContainer.style.zIndex = 1;
		
		divContent = document.createElement("div"); 
		divContent.id = "popupDiv_Content";
		divContent.style.position = "relative";

		divContainer.appendChild(divContent);

		divScrollArea = document.createElement("div"); 
		divScrollArea.id = "popupDiv_ScrollArea";
		divScrollArea.style.position = "absolute";
		divScrollArea.style.width = "15px";		
		divScrollArea.style.overflow = "hidden";
		divScrollArea.style.border = "1px";
		divScrollArea.style.borderColor = "#666";
		divScrollArea.style.borderStyle = "solid";
		
		divScroller = document.createElement("div"); 
		divScroller.id = "popupDiv_Scroller";
		divScroller.style.cursor = "pointer";
		divScroller.style.position = "relative";
		divScroller.style.width = "15px";		
		divScroller.style.backgroundColor = "#AAA";
		divScroller.style.borderTop = "1px";
		divScroller.style.borderTopColor = "#666";
		divScroller.style.borderTopStyle = "solid";
		divScroller.style.borderBottom = "1px";
		divScroller.style.borderBottomColor = "#666";
		divScroller.style.borderBottomStyle = "solid";
		divScrollArea.appendChild(divScroller);

		divMain.appendChild(divContainer);
		divMain.appendChild(divScrollArea);
		
	} else {
		divMain = document.getElementById("popupDiv_Main");
		divClose = document.getElementById("popupDiv_Close");
		divContainer = document.getElementById("popupDiv_Container");
		divContent = document.getElementById("popupDiv_Content");
		divScrollArea = document.getElementById("popupDiv_ScrollArea");
		divScroller = document.getElementById("popupDiv_Scroller");		
	}

	MS.Position.setLocation(divMain, r);
	divClose.style.width = (width+20) + "px";
	divContainer.style.top = "15px";
	divContainer.style.height = height + "px";
	divContainer.style.width = width + "px";
	divContainer.style.height = height + "px";
	//divContent.style.height = height + "px";
	divContent.style.width = (width-10) + "px";
	divContent.style.left = (1) + "px";
	divScrollArea.style.top = "15px";
	divScrollArea.style.left = (width + 5) + "px";
	divScrollArea.style.height = height + "px";
	divScroller.style.top = "-1";
	
	XMLPopup = null;
	if (window.XMLHttpRequest) {
		XMLPopup = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try 
		{ 
			XMLPopup = new ActiveXObject("Microsoft.XMLHTTP"); 
		}
		catch (err) 
		{ 
			return; 
		}
	}

	if (XMLPopup) {
		XMLPopup.onreadystatechange = function() {
			if(XMLPopup.readyState == 4)
			{
				if(XMLPopup.status != 200) { alert("Lütfen daha sonra tekrar deneyiniz."); return; }
			}
			else 
			{
				return;
			}

			document.getElementById("popupDiv_Content").innerHTML = XMLPopup.responseText;
			
			var rBound = MS.Position.getBounds( document.getElementById("popupDiv_Content") );
			var docH = rBound.height+100;
			rBound = MS.Position.getBounds( document.getElementById("popupDiv_Container") );
			var contH = rBound.height;
			rBound = MS.Position.getBounds( document.getElementById("popupDiv_ScrollArea") );
			var scrollAreaH = rBound.height;
			//var docH = document.getElementById("popupDiv_Content").offsetHeight;
			//var contH = document.getElementById("popupDiv_Container").offsetHeight;
			//var scrollAreaH = document.getElementById("popupDiv_ScrollArea").offsetHeight;

			var scrollH = scrollAreaH * (contH / docH); //(contH * scrollAreaH) / docH;
			document.getElementById("popupDiv_Scroller").style.height = Math.floor(scrollH) + "px";
			var scrollDist = Math.round(scrollAreaH-scrollH);
			
			Drag.init(document.getElementById("popupDiv_Scroller"),null,0,0,-1,scrollDist);
			document.getElementById("popupDiv_Scroller").onDrag = function (x,y) {
				var scrollY = parseInt(document.getElementById("popupDiv_Scroller").style.top);
				var docY = 0 - (scrollY * (docH - contH) / scrollDist);
				document.getElementById("popupDiv_Content").style.top = docY + "px";
			}
		};
	}
	
	divContent.innerHTML = "<table style=\"background-color:White\" width=\"" + (width-8) + "px\" height=\"" + (height) + "px\">" +
								"<tr>" +
									"<td valign=\"middle\" align=\"center\">" +
										"<img alt=\"Y&#252kleniyor\" src=\"/resim/ajax-loader.gif\" border=\"0\"><p style=\"color:Blue;\">Y&#252kleniyor</p>"
									"</td>" +
								"</tr>" +
							"</table>";
	divMain.style.display = "inline";

	XMLPopup.open("GET", window.location.protocol + "//" + window.location.hostname + "/" +  address, true);
	XMLPopup.setRequestHeader("Accept-Charset", "iso-8859-9");
	XMLPopup.setRequestHeader("Accept-Language", "tr");
	XMLPopup.send(null);	

}

var Drag = {
	obj : null,
	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
		o.onmousedown	= Drag.start;
		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;
		o.root = oRoot && oRoot != null ? oRoot : o ;
		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";
		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;
		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;
		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},
	start : function(e) {
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);
		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;
		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}
		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}
		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		return false;
	},
	drag : function(e) {
		e = Drag.fixE(e);
		var o = Drag.obj;
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;
		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);
		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));
		if (o.xMapper) { nx = o.xMapper(y); }		
		else if (o.yMapper)	{ ny = o.yMapper(x); }
		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;
		Drag.obj.root.onDrag(nx, ny);
		return false;
	},
	end : function() {
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd( parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
			parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
			Drag.obj = null;
	},
	fixE : function(e) {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return document.body.scrollTop + document.documentElement.clientHeight;
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function HideDiv(objDiv) {
	if (objDiv)
	{
		objDiv.style.visibility = 'hidden';
	}
	var divx=document.getElementById('popupBackgroundDIV');
	if(divx)
	{	
		document.body.removeChild(divx);
	}
}
function ShowDiv(objDiv) {
	var newDiv;
	var r = MS.Position.getPageSize();
	newDiv = document.createElement('DIV');
	document.body.appendChild(newDiv);
	newDiv.id = 'popupBackgroundDIV';
	newDiv.style.position = 'absolute';
	newDiv.style.width = (r.pageWidth - 17) +'px';
	newDiv.style.height = r.pageHeight +'px';
	newDiv.style.background = '#C0C0C0';

	if (newDiv.filters) {
		newDiv.style.filter = 'alpha(opacity=70)';
	}
	else {
		newDiv.style.MozOpacity = 0.7;
	}

	newDiv.style.left = '0px';
	newDiv.style.top = '0px';
	//newDiv.style.opacity='.5';
	var mxz = MaxZIndex();
	newDiv.style.zIndex = mxz + 1;

	var r1 = {left:10,top:10,width:10,height:10};
	r1.height=objDiv.offsetHeight;
	r1.width=objDiv.offsetWidth;
	r1.left = (eval((r.windowWidth - objDiv.offsetWidth)/2) < 0 ? 0 : eval((r.windowWidth - objDiv.offsetWidth)/2));
	r1.top = MS.Position.getPageScroll() + (eval(r.windowHeight-objDiv.offsetHeight)/2);
	MS.Position.setBounds(objDiv, r1);
	objDiv.style.visibility = 'visible';
	objDiv.style.zIndex = mxz + 2;
	//objDiv.style.position = "fixed";
}
function MaxZIndex() {
	var divlist, zindex;
	divlist = document.getElementsByTagName('div');
	zindex = 0;
	for(i=0;i<divlist.length;i++)
	{
		if(divlist[i].style.zIndex>zindex)
		{
			zindex = divlist[i].style.zIndex;
		}
	}
	return zindex;
}
function ShowPicture(url) {
	var divImageCont = document.createElement("div");
	divImageCont.id = "pictureDIV";
	divImageCont.style.zIndex = MaxZIndex() + 1;
	divImageCont.style.display = "none";
	//divImageCont.style.backgroundColor = "white";
	divImageCont.style.width = "100px";
	divImageCont.style.height = "50px";
	divImageCont.style.visibility = 'visible';
	divImageCont.className = 'ShowPopup';
	divImageCont.innerHTML = "<table width=\"100px\" height=\"50px\">" +
							"<tr>" +
								"<td valign=\"middle\" align=\"center\">" +
									"<img alt=\"Y&#252kleniyor\" src=\"/resim/ajax-loader.gif\" border=\"0\"><p style=\"color:#000000;\"><B>Y&#252kleniyor</B></p>"
								"</td>" +
							"</tr>" +
						"</table>";


	document.body.appendChild(divImageCont);
	divImageCont.style.display = "";
	ShowDiv(divImageCont);
	var divImage = document.createElement("img");
	divImage.onclick=function() {
		document.body.removeChild(document.getElementById("pictureDIV"));

		HideDiv(document.getElementById("pictureDIV"));
		divImage.onclick=function(){};
	}
	divImage.onload=function(){
		var sImgName="tdPictureBox_" + Math.floor(Math.random()*1000000);
		var r = MS.Position.getPageSize();
		var r1 = {left:10,top:10,width:10,height:10};

		divImageCont.innerHTML = "";
		divImageCont.style.width = eval(divImage.width + 40) + "px";
		divImageCont.style.height = eval(divImage.height + 40) + "px";
		divImageCont.style.visibility = 'visible';
		divImageCont.innerHTML = "<table width='90%' style='border: 1px solid #000080;background-color:#FFFFFF' height='100%'><tr><td id=\"" + sImgName + "\" align=\"center\" valign=\"middle\"></td></tr><tr><td align=\"right\"><span style=\"cursor:pointer;color:#000080\" onclick=\"document.body.removeChild(document.getElementById('pictureDIV'));HideDiv(document.getElementById('pictureDIV'));\">Kapat</span></tr></table>"

		document.getElementById(sImgName).appendChild(divImage);

		r1.height=divImageCont.offsetHeight;
		r1.width=divImageCont.offsetWidth;
		r1.left = eval((r.windowWidth - divImageCont.offsetWidth)/2);
		r1.top = MS.Position.getPageScroll() + (eval(r.windowHeight-divImageCont.offsetHeight)/2);
		MS.Position.setBounds(divImageCont, r1);
		
		divImage.onload=function(){};
	}
	

	divImage.src=url;
}

function ShowComboBox(id, url, count) {
	if (objLast10PageCombo) {
		objLast10PageCombo.show();
	}
	else {
		objLast10PageCombo = new KY.Web.DivComboBox(id, count, url);
	}
}

function ExtendElement(obj, url) {
	var r = MS.Position.getBounds($(obj));
	var div = null;
	if ($("div" + $(obj).id + "Extend")) {
		div = $("div" + $(obj).id + "Extend");
	}
	else {
		div = document.createElement("div");
		div.id = "div" + $(obj).id + "Extend";
		div.style.bgColor = "#FFFFFF";
        div.style.borderColor = "black";
        div.style.borderStyle = "solid";
        div.style.borderWidth = "thin";
		div.style.visibility = "visible";
		//div.style.display = "inline";
		var p = MS.Position.getParent($(obj));
		p.innerHTML+="<div style='heigth:5px'>&nbsp;</div>";
		p.appendChild(div);
	}

	div.innerHTML = "";
	if (url.indexOf("?") < 0) {
		url += "?" + "refererEle=" + div.id;
	}
	else {
		if (url.substring(url.legnth-1) != "&") { url += "&"; }
		url += "refererEle=" + div.id;
	}
	GetNavigateUrl(div, url, true);
}

function DebugWindow(key, msg){
	if (document.location.hostname.indexOf('dene')<0) return;
	var outerdiv, containerDiv;
	outerdiv = document.getElementById('popupDebugWindow');
	containerDiv = document.getElementById('popupContainerDebugWindow');
	if(!outerdiv){
		outerdiv = document.createElement('DIV');
		outerdiv.id = 'popupDebugWindow';
		outerdiv.className='debugWindow';
		outerdiv.style.zIndex = MaxZIndex() + 1;
		document.body.appendChild(outerdiv);

		containerDiv = document.createElement('DIV');
		containerDiv.id = 'popupContainerDebugWindow';
		containerDiv.style.width = '100%';
		containerDiv.style.height = '100%';
		containerDiv.style.left = '0px';
		containerDiv.style.top = '0px';
		containerDiv.style.overflow = 'auto';
		containerDiv.style.position = 'relative';
		containerDiv.index = 0;
		outerdiv.appendChild(containerDiv);
	}
	outerdiv.style.visibility = 'visible';
	containerDiv.index = containerDiv.index + 1;
	var an = new Date();
	containerDiv.innerHTML = '<font color=green size=2 >['+an.getMinutes()+':'+an.getSeconds()+':'+an.getMilliseconds()+']</font><font color=red size=2>' + key + '</font><b> : </b>' + msg + '<br/>' + containerDiv.innerHTML; 
}

function showBusy(obj){
	var div=document.createElement('DIV');
	document.body.appendChild(div);
	div.id='div_busy_'+obj.id;
	div.style.visibility = 'visible';
	div.style.borderWidth = '0px';
	div.style.borderStyle = 'solid';
	div.style.backgroundImage = 'url(\'/resim/ajax-loader.gif\')';
	div.style.backgroundColor = 'silver';
	div.style.backgroundRepeat = 'no-repeat';
	div.style.backgroundPosition = 'center';
	div.style.zIndex = 10;
	var bounds = MS.Position.getBounds(obj);
	MS.Position.setBounds(div, bounds);
}

function cancelBusy(obj){
	var div = document.getElementById('div_busy_'+obj.id);
	if(div) document.body.removeChild(div);
}
