var isIE = false;
if (navigator.appName.toLowerCase().indexOf("microsoft")>-1 )
	isIE = true;

//********** from jquery.js *****************
var userAgent = navigator.userAgent.toLowerCase();

//Figure out what browser is being used
var browser = {
	version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
	safari: /webkit/.test( userAgent ),
	opera: /opera/.test( userAgent ),
	msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
	mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
//*******************************************

function addEvent(component, eventName, _function) {
	if( component.addEventListener ) { //non microsoft
		component.addEventListener( eventName, _function, false); 
		return _function;
	}
	else if(component.attachEvent){
		component.attachEvent( "on"+ eventName, _function );
		return _function;
	}
	return null;
}

function trimAll(str) {
	if(!str)
		return "";
	if( str!=null ) {
		while ( str.length>0 && "\t\n\r ".indexOf( str.charAt(str.length-1) ) != -1 )
			str=str.substring(0, str.length-1);
		while( str.length>0 && "\t\n\r ".indexOf( str.charAt(0) ) != -1 )
			str=str.substring(1,str.length);
	}
	return str;
}

function showMessage(id, message) {
	document.getElementById(id).style.visibility = 'visible';
	document.getElementById(id).innerHTML = message;
}

function hideMessage(id) {
	document.getElementById(id).style.visibility = 'hidden';
}

function Request(div, infoMessage) {
	this.ajax = false;
	this.run = function() { };
	this.error = function() { };
	if(div)
		hideMessage(div);
	var message = 	"XMLHttpRequest object could not be created <br>"+
   					"Your browser may be too old, or its javascript engine is disabled!";
	
	if( window.XMLHttpRequest ) {
       	this.ajax = new XMLHttpRequest();
   	} 
	else if ( window.ActiveXObject ) { //headache: IE6
   		try {
   			this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
   		}
   		catch(e) {
   			try {
   				this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
   			}
   			catch(e) { this.ajax =false; }
   		}
   	}
   	
   	if(!this.ajax) {
   		if( document.getElementById(div) ) 
   			showMessage(div,message);
   		else
   			alert( message );
   	}
   	
   	this.process = function() {
   		if(infoMessage)
   			showInfo(infoMessage);
   		var method = arguments[0].toUpperCase();
   		var url = arguments[1];
   		var urlTrail = '';
   		var me = this;
   		for( var i=2; i < arguments.length-1; ) {
   			urlTrail += encodeURIComponent(arguments[i++]);
   			urlTrail +='=';	
   			urlTrail += encodeURIComponent(arguments[i++]);	
   			urlTrail += "&";
   		}
   		if( method == 'GET') {
   			url += '?' + urlTrail;
   			urlTrail = null;
   		}

   		me.ajax.open( method, url, true );
   		me.ajax.setRequestHeader("ajax", "true");
   		me.ajax.setRequestHeader("cache-control", "no");
   		me.ajax.setRequestHeader("pragma", "no-cache");
   		me.ajax.onreadystatechange = function() {
 			if( 4 == me.ajax.readyState ) {
 				if( 200 == me.ajax.status ) {
 					me.run();
 				}
 				else {
 					me.error();
 					if( document.getElementById(div) ) {
 						showMessage(div, "İşlem gerçekleştirilimedi. Sunucu mesajı : "+me.ajax.statusText);
 					}
 					else {
 						alert("İşlem gerçekleştirilimedi. Sunucu mesajı : "+me.ajax.statusText );
 					}
 				}
 				if( infoMessage )
	 				hideInfo();
 			}
   		};	
   		this.ajax.send(urlTrail);
   	};  	
}

var msgDiv;

function getMsgSpan() {
	if( msgDiv )
		return msgDiv;
	msgDiv = document.createElement("span");
	msgDiv.style.position = "fixed";
	msgDiv.style.top = "0px";
	msgDiv.style.right = "0px";
	msgDiv.style.padding = "4px";
	msgDiv.style.backgroundColor = "#FF0000";
	msgDiv.style.color = "#FFFFFF";
	msgDiv.style.font = "bold 8pt Verdana";
	
	if( browser.msie && parseInt(browser.version)<7 ) {
		msgDiv.style.position = "absolute";
		addEvent(window, "scroll", 
					function() {
						msgDiv.style.top = window.document.documentElement.scrollTop;
					});
	}
	document.getElementsByTagName("body")[0].appendChild(msgDiv);
	return msgDiv;
}

var fadeInTimeOut, fadeOutTimeOut;
function fadeIn(elm, callback) {
	var speed = 50;
	var increase = 5;
	var opacity;
	var display = elm.style.display;
	if( display && display.toLowerCase()=='none') {
		setOpacity(elm, increase);
		elm.style.display = elm.getAttribute('fade_display') || '';
		opacity = 0;
	} 
	else {
		opacity = elm.getAttribute("fade_opacity");
		opacity = opacity ? parseInt( opacity ) : 5;
	}
	if( opacity<100 ) {
		opacity = opacity>(100-increase) ? 100 : (opacity + increase);
		setOpacity(elm, opacity);
		elm.setAttribute("fade_opacity", opacity);
		if( opacity<100 ){
			fadeInTimeOut = window.setTimeout(function(){ fadeIn(elm, callback);}, speed);
		}
		else {
			fadeInTimeOut = null;
			elm.removeAttribute("fade_opacity");
			if( callback ) 
				callback();
		}	 	
	}
}	

function fadeOut(elm, callback) {
	var speed = 50;
	var increase = 5;
	var opacity = elm.getAttribute("fade_opacity") || 100;
	if( opacity==0 ) {
		elm.setAttribute("fade_display", elm.style.display);
	} 
	if( opacity>0 ) {
		opacity = opacity<increase ? 0 : (opacity - increase);
		setOpacity(elm, opacity);
		elm.setAttribute("fade_opacity", opacity);
		if( opacity>0 ) {
			fadeOutTimeOut = window.setTimeout(function() { fadeOut(elm, callback); },speed);
		}
		else {
			fadeOutTimeOut = null;
			elm.style.display = 'none';
			setOpacity(elm, 100);
			elm.removeAttribute("fade_opacity");
			if( callback ) 
				callback();
		}
	}
}

function stopFading() {
	if( fadeOutTimeOut ) {
		window.clearTimeout(fadeOutTimeOut);
		fadeOutTimeOut = null;
	}
	
	if( fadeInTimeOut ) {
		window.clearTimeout(fadeInTimeOut);
		fadeInTimeOut = null;
	}
}

function showInfo(msg) {
	stopFading();
	getMsgSpan().innerHTML = msg;
	fadeIn( getMsgSpan() );
}	

function hideInfo() {
	stopFading();
	fadeOut( getMsgSpan() ); 
}

function setOpacity(obj, opacity) { 
	opacity = (opacity>100) ? 100 : (opacity<0 ? 0:opacity);
	// IE/Win 
	obj.style.filter = "alpha(opacity:"+opacity+")"; 
	// Safari<1.2, Konqueror 
	// Older Mozilla and Firefox 
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.KHTMLOpacity = 
	obj.style.MozOpacity =	
	obj.style.opacity = opacity/100; 
}

var modelDialog =null;
function Dialog() {
	//below control is to prevent opening another dialog
	//for same window
	if( modelDialog && modelDialog.active && modelDialog.active=="yes") {
		this.show = function() {};
		return;
	}	
	//opened dialog window
	var modelDialogWindow;
	var modelDialogInterval;
	var eventHandler;
	modelDialog = new Object();
	//parentFocused variable to know whether focus is at dialog opened window
	modelDialog.parentFocused = false;
	var tempFocusFunction = window.onfocus;
	window.onfocus = function() {
		if(modelDialog)
			modelDialog.parentFocused = true;
	};
		
	mantainFocus = function() {
		if(modelDialogWindow.closed) {
			modelDialog.active = "no";
			window.onfocus = tempFocusFunction;
			window.clearInterval(modelDialogInterval);
			eval( eventHandler );
			return;
		}
		if( modelDialog.parentFocused==true ) {
			modelDialogWindow.focus();
			modelDialog.parentFocused=false;
		}
	};
	
	this.getValue = function() {
		return modelDialog.action;
	};
	
	this.show = function() {
		modelDialog.active = "yes";
		var args = arguments[0];
		var title = args["title"] ? args["title"] : "Diyalog Kutusu";
		var body = args["body"] ? args["body"] : "your body text is missing!";
		var buttons = args["buttons"] ? args["buttons"] : "<input type=button value='Tamam' onclick='closeForm(0)' />";
		var handler = args["handler"]? args["handler"] : "" ;
		var width = args["width"] ? args["width"] : 300;
		var height = args["height"] ? args["height"] : 200;
		var left = args["left"] ? args["left"] : 20;
		var top = args["top"] ? args["top"] : 20;
		var url = args["url"] ? args["url"] : "";
		var args= 'width='+width+',height='+height+',left='+left+',top='+top+',toolbar=0,' + 
       			  'location=0,status=0,menubar=0,scrollbars=1,resizable=0'; 		
		eventHandler = handler;
		modelDialogWindow = window.open(url,"",args);
		modelDialogWindow.focus();
		modelDialogInterval = window.setInterval("mantainFocus()",10);
	};
}
	
function postForm() {
	var form = document.createElement("form");
	form.method = "post";
	document.body.appendChild(form);
	if( arguments.length>0 ) 
		form.action = arguments[0];
		
	for(var i=1; i<arguments.length; ) {
		var c = document.createElement("input");
		c.type = "hidden";
		c.name = arguments[i];
		c.value = encodeURI( arguments[i+1] );
		i+=2;
		form.appendChild( c );
	}
	form.submit();
	return false;
}

function removeSession() {
	var req = new Request();
}

