/////////////////////////////////////////
	// PHPLiveX Library                    //	
	// (C) Copyright 2006 Arda Beyazoðlu   //
	// Version: 2.2                        //
	// Home Page: phplivex.sourceforge.net //
	/////////////////////////////////////////
	
function PHPLiveX(type, target, mode, preload, method, loadingDesc, subFunc, extSubFunct ){
	this.TYPE = type;
	this.MODE = mode;
	this.TARGET = target;
	this.PRELOAD = preload;
	this.METHOD = method;
	this.LOADING_DESC = loadingDesc;
	this.SUB_FUNC = subFunc;
	this.EXT_SUB_FUNC = extSubFunct;
}

PHPLiveX.prototype.GetSpecialChars = function(str){
	str = str.replace(/%E7/g,"ç");
	str = str.replace(/%C7/g,"Ç");
	str = str.replace(/%F0/g,"ð");
	str = str.replace(/%D0/g,"Ð");
	str = str.replace(/%FD/g,"ý");
	str = str.replace(/%DD/g,"Ý");
	str = str.replace(/%F6/g,"ö");
	str = str.replace(/%D6/g,"Ö");
	str = str.replace(/%FE/g,"þ");
	str = str.replace(/%DE/g,"Þ");
	str = str.replace(/%FC/g,"ü");
	str = str.replace(/%DC/g,"Ü");
	return unescape(str);
}

PHPLiveX.prototype.EscapeSpecialChars = function(str){
	str = str.replace(/ç/g,escape("ç"));
	str = str.replace(/Ç/g,escape("Ç"));
	str = str.replace(/ð/g,escape("ð"));
	str = str.replace(/Ð/g,escape("Ð"));
	str = str.replace(/ý/g,escape("ý"));
	str = str.replace(/Ý/g,escape("Ý"));
	str = str.replace(/ö/g,escape("ö"));
	str = str.replace(/Ö/g,escape("Ö"));
	str = str.replace(/þ/g,escape("þ"));
	str = str.replace(/Þ/g,escape("Þ"));
	str = str.replace(/ü/g,escape("ü"));
	str = str.replace(/Ü/g,escape("Ü"));
	return str;
}

PHPLiveX.prototype.ShowError = function(errorMsg){
	if(errorMsg != ""){
		alert(errorMsg);
		return false;
	}else{
		return true;
	}
}

PHPLiveX.prototype.GetXmlHttp = function(){
	objXmlHttp = false;
    if (window.XMLHttpRequest) {
        objXmlHttp = new XMLHttpRequest();
        if (objXmlHttp.overrideMimeType) {
            objXmlHttp.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) {
        try {
            objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
	
    if (!objXmlHttp) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

	return objXmlHttp;
}

PHPLiveX.prototype.CreatePreloading = function(preloadId){
	createTransparentBg();
	pLdr = document.getElementById(preloadId);
	pLdr.innerHTML = "<img src=\"images/yukleniyor.gif\" border=0>&nbsp;"+ this.LOADING_DESC;
	pLdr.style.top = ( document.body.clientHeight  / 2);
	pLdr.style.left = ( (document.body.clientWidth - 130 ) / 2);
	pLdr.style.visibility = "visible";
}

PHPLiveX.prototype.CreateOutput = function(funcName, funcArgs, funcUrl){
	var data = "";
	var args = new Array();
	if(funcUrl == null){ alert( "Gönderilecek adres bilgisi alýnamadý." ); return; }
	if(funcArgs != ""){
		/*if(funcArgs.indexOf(",") != -1){
			args = funcArgs.split(",");
			for (i=0;i<args.length;i++) data += "&funcArgs[]=" + escape(args[i]);
		}else{
			data += "&funcArgs[]=" + escape(funcArgs);
		}*/
		
		data = this.GetSpecialChars(funcArgs);
	}
	var parentObject = this;
	
	var XmlHttp = this.GetXmlHttp();
	var ajaxType = false;
	if(this.TYPE != "r") ajaxType = true;
	
	if(this.METHOD == "get"){
		if(funcUrl.indexOf("?") != -1){
			XmlHttp.open("GET", funcUrl + "&" + this.EscapeSpecialChars(data), ajaxType);
		}else{
			XmlHttp.open("GET", funcUrl + "?" + this.EscapeSpecialChars(data), ajaxType);
		}
	}else{ XmlHttp.open("post", funcUrl, ajaxType); }
	
	XmlHttp.setRequestHeader("Method", this.METHOD + " " + funcUrl + " HTTP/1.1");
	XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1");
	XmlHttp.setRequestHeader("Pragma", "no-cache");
	XmlHttp.setRequestHeader("Cache-Control", "must-revalidate");
	XmlHttp.setRequestHeader("Cache-Control", "no-cache");
	XmlHttp.setRequestHeader("Cache-Control", "no-store");
	
	if(this.TYPE != "r"){
		if(this.PRELOAD != null){ this.CreatePreloading(this.PRELOAD); }
		
		XmlHttp.onreadystatechange = function(){
			if(XmlHttp.readyState == 0){
				XmlHttp.abort();
				if(parentObject.PRELOAD != null){
					document.getElementById(parentObejct.PRELOAD).style.visibility = "hidden";
				}
			}else if(XmlHttp.readyState == 4){
				//Remove transparent background.
				removeTransparentBg();
				//var output = parentObject.GetSpecialChars(XmlHttp.responseText);
				var output = XmlHttp.responseText;
				var outparts = output.split("<phplivex>");
				output = outparts[outparts.length-1].split("</phplivex>")[0];
				
				//If JS Code Exists
				var jscode = "";
				var parts = output.match(/<script[^>]*>(.|\n|\t|\r)*?<\/script>/gi);
				
				if(parts){
					for(i=0;i<parts.length;i++){
						jsSrc = parts[i].match(/src=\"[^\"]*/gi);
						
						jscode = parts[i].replace(/<script[^>]*>|<\/script>/gi, "");
						
						output = output.replace(parts[i], "");
						
						var script = document.createElement("script");
						script.type = 'text/javascript'; 
						script.lang = 'javascript';
						if( jsSrc != null ) script.src = jsSrc.toString().replace("src=\"", "") ;
						script.text = jscode;
						document.getElementsByTagName('head')[0].appendChild(script);
					}
				}
				
				//alert(document.getElementsByTagName('head')[0].innerHTML);
				
				if(parentObject.PRELOAD != null){ document.getElementById(parentObject.PRELOAD).style.visibility = "hidden"; }
				
				if(parentObject.TYPE == "e"){ return; }
				else if(parentObject.TARGET == "alert"){ shnCreateErrorWindow(output); }
				else if(parentObject.MODE == "aw"){ document.getElementById(parentObject.TARGET).innerHTML += output; }
				else if(parentObject.MODE == "rw"){ document.getElementById(parentObject.TARGET).innerHTML = output; }
				
				if( typeof parentObject.EXT_SUB_FUNC != "undefined" && parentObject.EXT_SUB_FUNC != null && parentObject.EXT_SUB_FUNC.length > 0 )
				{
					self[ parentObject.EXT_SUB_FUNC ]();
				} else {
					if( parentObject.SUB_FUNC != true){
						if( typeof initSub != "undefined" && initSub != null ) initSub();
					} else if( typeof initSub2 != "undefined" && initSub2 != null ) initSub2();
				}
			}
		}
		
		if(this.METHOD == "get"){ XmlHttp.send(null); }
		else{ XmlHttp.send(this.EscapeSpecialChars(data)); }
	}else{
		if(this.METHOD == "get"){ XmlHttp.send(null); }
		else{ XmlHttp.send(this.EscapeSpecialChars(data)); }
		var output = this.GetSpecialChars(XmlHttp.responseText);
		var outparts = output.split("<phplivex>");
		output = outparts[outparts.length-1].split("</phplivex>")[0];
		
		//If JS Code Exists
		var jscode = "";
		var parts = output.match(/<script[^>]*>(.|\n|\t|\r)*?<\/script>/gi);
		if(parts){
			for(i=0;i<parts.length;i++){
				jscode += parts[i].replace(/<script[^>]*>|<\/script>/gi, "");
				output = output.replace(parts[i], "");
			}
		}
		if(jscode != ""){
			var script = document.createElement("script");
			script.type = 'text/javascript'; 
			script.lang = 'javascript';
			script.text = jscode;
			document.getElementsByTagName('head')[0].appendChild(script);
		}
		//
		
		return output;
	}
}




/**
 * @param obj -> current object.
 * @return array -> current positions of current object.
 **/
function findPosition(obj) 
{
	var curleft = curtop = 0;
	
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function createTransparentBg()
{
	try{
		if( document.getElementById("spnTBg") == null )
		{//Create transparent background.
			var tBgg = window.document.createElement("SPAN");
			tBgg.setAttribute("id", "spnTBg");
			tBgg.setAttribute("className", "transBg");
			document.body.appendChild(tBgg);
		}
	} catch(e){alert(e.description);}
}

function removeTransparentBg(){
	try{
		if( document.getElementById("spnTBg") != null )
		{//Remove transparent background.
			document.getElementById("spnTBg").innerHTML = "";
			document.getElementById("spnTBg").removeNode(false);
		}
	} catch(e){alert(e.description);}
}
