function bindEvent(n,o,f)//nesne,olay,fonksiyon
{
	if(n.attachEvent)return n.attachEvent("on"+o,f);
	else if(n.addEventListener)return n.addEventListener(o,f,false);	
}
function unbindEvent(n,o,f)
{
	if(n.detachEvent)return n.detachEvent("on"+o,f);
	if(n.removeEventListener)return n.removeEventListener(o,f,false);
}

function nTop(n)
{
	try
	{
		if(n.offsetParent.tagName=='BODY')return n.offsetTop;
		else if(n.offsetParent.tagName=='TBODY')return 0;
		else return n.offsetTop+nTop(n.offsetParent);
	}
	catch(e)
	{
		return 0;
	}
}

function nLeft(n)
{
	try
	{
		if(n.offsetParent.tagName=='BODY')return n.offsetLeft;
		else if(n.offsetParent.tagName=='TBODY')return 0;
		else return n.offsetLeft+nLeft(n.offsetParent);
	}
	catch(e)
	{
		return 0;
	}
}

function getWindowWidth()
{
	if(window.innerHeight){
		var w=window.innerWidth;
	} else if(document.documentElement.clientHeight){
		var w=document.documentElement.clientWidth;
	} else if(document.body.offsetHeight){
		var w=document.body.offsetWidth;
	} else {
		var w=944;
	}
	return w;
}

function getWindowHeight()
{
	if(window.innerHeight){
		var h=window.innerHeight;
	} else if(document.documentElement.clientHeight){
		var h=document.documentElement.clientHeight;
	} else if(document.body.offsetHeight){
		var h=document.body.offsetHeight;
	} else {
		var h=1000;
	}
	
	return h;
}

function $id(n)
{
	return document.getElementById(n);
}

function elementHTML( element )
{
	var h = '<' + element.tagName;
	
	for( var i = 0 ; i < element.attributes.length ; ++i ){
		h += ' ' + element.attributes[i].nodeName + '="' + element.attributes[i].nodeValue + '"';
	}
	
	if( element.tagName != 'INPUT' ){
		h += '>' + element.innerHTML + '</' + element.tagName + '>';
	} else {
		h += ' />';
	}
	
	return h;
}

function setOpacity(element,opacity)
{
	if(navigator.appName=="Microsoft Internet Explorer"){
		var f="alpha(opacity="+(parseFloat(opacity)*100)+")";
		element.style.filter=f;
		
	} else {
		element.style.opacity=opacity;
	}
}

function xOverlay(bgColor,opacity,hideOnClick)
{
	this.bgColor=bgColor;
	this.opacity=opacity;
	this.hideOnClick=hideOnClick;
	var xo=this;
	var $w=$(window);
	var $d=$(document);
	var o=document.createElement('div');
	this.o=o;
	o.style.position='absolute';
	o.style.top="0px";
	o.style.left="0px";
	this.setSize = function (){
		try{
			
			if(navigator.appVersion.indexOf("MSIE 7")!=-1){
				o.style.width=($d.width())+"px";
				o.style.height=($d.height())+"px";
			} else {
				var hsbar=false;
				var vsbar=false;
				if( $d.width() > ($w.width()+21) )hsbar=true;
				if( $d.height() > ($w.height()+21) )vsbar=true;
				if(vsbar&&hsbar){
					o.style.width=($d.width())+"px";
					o.style.height=($d.height())+"px";
				} else if(vsbar){
					o.style.width=($d.width()-21)+"px";
					o.style.height=($d.height())+"px";
				} else if(hsbar){
					o.style.width=($d.width())+"px";
					o.style.height=($d.height()-21)+"px";
				} else {
					o.style.width=($d.width()-4)+"px";
					o.style.height=($d.height()-4)+"px";
				}
			}
		} catch(exc) {
			
		}
	};
	
	this.setSize();
	o.style.backgroundColor=this.bgColor;
	o.style.display='none';
	setOpacity(o,opacity);
	o.style.padding="0px";
	o.style.margin="0px";
	o.style.zIndex=9999999;
	
	$(o).click(function (){
		if(xo.hideOnClick){
			xo.hide();
		}
	});
	document.body.appendChild(o);
	$w.resize(function (){
		xo.setSize();
	});
	$w.scroll(function(){
		return;
		//alert($w.scrollTop()+":"+$w.scrollLeft());
		o.style.top=$w.scrollTop()+"px";
		o.style.left=$w.scrollLeft()+"px";
		if(xo.activeElement){
			xo.relocateElement(xo.activeElement);
		}
	});
	
	this.relocateElement = function (element){
		element.style.top=(($w.height()-element.clientHeight)/2+$w.scrollTop())+"px";
		element.style.left=(($w.width()-element.clientWidth)/2+$w.scrollLeft())+"px";
	};
	
	this.show=function (element,opacity){
		xo.setSize();
		o.style.display='block';
		if(element){
			if(typeof(element)=="string"){
				element=document.getElementById(element);
			}
			if(element.parentNode){
				element.parentNode.removeChild(element);
			}
			xo.activeElement=element;
			element.style.position="absolute";
			element.style.visibility="hidden";
			element.style.display="block";
			setOpacity(element,opacity);
			document.body.appendChild(element);
			this.relocateElement(element);
			element.style.zIndex=9999999+1;
			element.style.visibility="visible";
		}
		if(this.onShow){
			this.onShow();
		}
	};
	
	this.hide=function (){
		o.style.display='none';
		if(xo.activeElement){
			xo.activeElement.style.visibility="hidden";
			xo.activeElement.style.display="none";
		}
		if(this.onHide){
			this.onHide();
		}
		delete xo.activeElement;
	};
	
	this.hide2=function (){
		o.style.display='none';
		if(xo.activeElement){
			xo.activeElement.style.visibility="hidden";
			xo.activeElement.style.display="none";
		}
		delete xo.activeElement;
	};
}

function xDialog(xo,hasCloseButton,hasBorder)
{
	this.xo=xo;
	this.hasBorder=hasBorder;
	this.hasCloseButton=hasCloseButton;
	this.disableHide=false;
	var xd=this;
	
	this.d=document.createElement('div');
	this.d.id='xDialogContainer';
	this.d.style.position='absolute';
	this.d.style.visibility='hidden';
	document.body.appendChild(this.d);
	
	if(hasBorder){
		this.borderTL=document.createElement('div');
		this.borderTL.id='xDialogTopLeft';
		this.borderTL.style.position='absolute';
		this.borderTL.style.zIndex=1;
		this.d.appendChild(this.borderTL);
		this.borderTC=document.createElement('div');
		this.borderTC.id='xDialogTopCenter';
		this.borderTC.style.position='absolute';
		this.borderTC.style.zIndex=1;
		this.d.appendChild(this.borderTC);
		this.borderTR=document.createElement('div');
		this.borderTR.id='xDialogTopRight';
		this.borderTR.style.position='absolute';
		this.borderTR.style.zIndex=1;
		this.d.appendChild(this.borderTR);
		this.borderML=document.createElement('div');
		this.borderML.id='xDialogMiddleLeft';
		this.borderML.style.position='absolute';
		this.borderML.style.zIndex=1;
		this.d.appendChild(this.borderML);
		this.borderMR=document.createElement('div');
		this.borderMR.id='xDialogMiddleRight';
		this.borderMR.style.position='absolute';
		this.borderMR.style.zIndex=1;
		this.d.appendChild(this.borderMR);
		this.borderBL=document.createElement('div');
		this.borderBL.id='xDialogBottomLeft';
		this.borderBL.style.position='absolute';
		this.borderBL.style.zIndex=1;
		this.d.appendChild(this.borderBL);
		this.borderBC=document.createElement('div');
		this.borderBC.id='xDialogBottomCenter';
		this.borderBC.style.position='absolute';
		this.borderBC.style.zIndex=1;
		this.d.appendChild(this.borderBC);
		this.borderBR=document.createElement('div');
		this.borderBR.id='xDialogBottomRight';
		this.borderBR.style.position='absolute';
		this.borderBR.style.zIndex=1;
		this.d.appendChild(this.borderBR);
	}
	
	if(hasCloseButton){
		this.clsBtn=document.createElement('div');
		this.clsBtn.id='xDialogCloseButton';
		this.clsBtn.style.cursor='pointer';
		this.clsBtn.style.position='absolute';
		this.clsBtn.style.visibility='hidden';
		this.clsBtn.onclick = function (){
			if(!xd.disableHide){
				xd.hide();
			}
		};
		this.clsBtn.style.zIndex=9999999+3;
		document.body.appendChild(this.clsBtn);
	}

	this.relocateElement = function (element,width,height){
		if(!width||!height){
			return;
		}
		var awidth=width;
		var aheight=height;
		if(this.hasBorder){
			
			var leftBorderWidth=this.borderTL.clientWidth;
			leftBorderWidth=Math.max(leftBorderWidth,this.borderML.clientWidth);
			leftBorderWidth=Math.max(leftBorderWidth,this.borderBL.clientWidth);
			
			var rightBorderWidth=this.borderTR.clientWidth;
			rightBorderWidth=Math.max(rightBorderWidth,this.borderMR.clientWidth);
			rightBorderWidth=Math.max(rightBorderWidth,this.borderBR.clientWidth);
			
			width+=leftBorderWidth+rightBorderWidth;
			
			var topBorderHeight=this.borderTL.clientHeight;
			topBorderHeight=Math.max(topBorderHeight,this.borderTC.clientHeight);
			topBorderHeight=Math.max(topBorderHeight,this.borderTR.clientHeight);
			
			var bottomBorderHeight=this.borderBL.clientHeight;
			bottomBorderHeight=Math.max(bottomBorderHeight,this.borderBC.clientHeight);
			bottomBorderHeight=Math.max(bottomBorderHeight,this.borderBR.clientHeight);
			
			height+=topBorderHeight+bottomBorderHeight;
			
			element.style.top=topBorderHeight+'px';
			element.style.left=leftBorderWidth+'px';
			
			this.borderTL.style.left='0px';
			this.borderTL.style.top='0px'
			
			this.borderTC.style.left=leftBorderWidth+'px';
			this.borderTC.style.top='0px';
			this.borderTC.style.width=awidth+"px";
			
			this.borderTR.style.left=(leftBorderWidth+awidth)+'px';
			this.borderTR.style.top='0px';
			
			this.borderML.style.top=topBorderHeight+"px";
			this.borderML.style.height=aheight+"px";
			this.borderMR.style.top=topBorderHeight+"px";
			this.borderMR.style.left=(leftBorderWidth+awidth)+'px';
			this.borderMR.style.height=aheight+"px";
			
			this.borderBL.style.top=(topBorderHeight+aheight)+'px';
			this.borderBC.style.top=(topBorderHeight+aheight)+'px';
			this.borderBC.style.left=leftBorderWidth+'px';
			this.borderBC.style.width=awidth+'px';
			this.borderBR.style.top=(topBorderHeight+aheight)+'px';
			this.borderBR.style.left=(leftBorderWidth+awidth)+'px';
			
		}
		
		this.d.style.width=width+"px";
		this.d.style.height=height+"px";
		this.xo.relocateElement(this.d);
	};
	
	this.show = function(element,width,height,opacity,disableHide){
		if(typeof(element)=="string"){
			element=document.getElementById(element);
		}
		this.activeElement=element;
		
		this.clsBtn.style.display='none';
		this.clsBtn.style.visibility='hidden';
		/*if(disableHide){
			this.disableHide=true;
			this.xo.hideOnClick=false;
		} else {
			this.disableHide=false;
			this.xo.hideOnClick=true;
			this.clsBtn.style.visibility='visible';
		}
		*/

		if(!opacity){
			opacity=1;
		}

		element.parentNode.removeChild(element);
		this.d.appendChild(element);
		element.style.display='block';
		element.style.visibility='visible';
		element.style.position='absolute';
		element.style.top='0px';
		element.style.left='0px';
		element.style.zIndex=2;

		this.relocateElement(element,width,height);

		this.xo.onHide=function (){
			xd.hide();
		};
		this.xo.show(this.d,opacity);
		this.relocateElement(element,width,height);
		
		if(this.hasCloseButton && !disableHide){
			this.disableHide=false;
			this.xo.hideOnClick=true;
			this.clsBtn.style.display='block';
			this.clsBtn.style.visibility='visible';
			this.clsBtn.style.top=(nTop(this.borderTR))+"px";
			this.clsBtn.style.left=(nLeft(this.borderTR)-16)+"px";
		} else {
			this.disableHide=true;
			this.xo.hideOnClick=false;
			this.clsBtn.style.visibility='hidden';
			this.clsBtn.style.display='none';
		}
		
		if(this.onShow){
			this.onShow();
		}
	};

	this.hide = function(){
		this.xo.hide2();
		xd.d.style.display='none';

		xd.d.style.visibility='hidden';
		if(xd.activeElement){
			xd.activeElement.style.display='none';
			xd.activeElement.style.visibility='hidden';
			xd.d.removeChild(xd.activeElement);
			document.body.appendChild(xd.activeElement);
		}
		if(this.clsBtn){
			this.clsBtn.style.display='none';
		}
		
		delete xd.activeElement;
		if(this.onHide){
			this.onHide();
		}
	};
}

function clearOptions( s )
{
	for( var i = s.options.length - 1 ; i > -1 ; --i ){
		s.options[i] = null;
	}
}

function selectAll( form, name )
{
	var f = document.forms[form];

	for( var i = 0 ; i < f.elements.length ; ++i ){
		if( f.elements[i].name == name ){
			f.elements[i].checked = true;
		}
	}
}

function unselectAll( form, name )
{
	var f = document.forms[form];
	
	for( var i = 0 ; i < f.elements.length ; ++i ){
		if( f.elements[i].name == name ){
			f.elements[i].checked = false;
		}
	}
}

function warn(msg, element)
{
	alert(msg);
	if(element && element.focus){
		element.focus();
	}
	return false;
}

