﻿// Phantom Base Functions
// -------------------------------------------------------------------------------------------------
function getXMLHTTP()
{
     var retval=null;
     try
     {
          retval=new ActiveXObject("Msxml2.XMLHTTP");
     }
     catch(e)
     {
          try
          {
               retval=new ActiveXObject("Microsoft.XMLHTTP");               
          }
          catch(oc)
          {
               retval=null;
          }
     }

     if(!retval && typeof XMLHttpRequest != "undefined")
     {
          retval=new XMLHttpRequest();
     }

     return retval;
}

function phantom_setContent(element, address, postvars, _method, async, loader_img_src)
{
    document.getElementById(element).innerHTML = loader_img_src != null ? loader_img_src : "<img src='images/progress.gif' border='0'>";

    // XmlHTTP nesnesi olustur.
    var xmlhttp = getXMLHTTP();
    
    // Method'u belirle.
    var method = _method == 'GET' ? 'GET' : 'POST';
    
    if (xmlhttp)
    {
        // xmlhttp.setTimeouts(resolveTimeout, connectTimeout,sendTimeout, receiveTimeout);        
    
        xmlhttp.open(method, address, async);
        
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml; charset=iso-8859-9');
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		// xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Connection", "close");
		
        xmlhttp.onreadystatechange=function()
        {
            /*
            0 -> uninitialized ( henüz open metodu kullanılmadı)
            1 -> loading (sunucuya istek gönderildi, yükleniyor)
            2 -> loaded (sunucuya istek gönderildi.)
            3 -> interactive (sunucudan cevap geldi ama başka bir veri daha bekleniyor)
            4 -> complete (cevap tamamlandı ve geri geldi.)
            */
            if(xmlhttp.readyState == 4)
            {					
                if (xmlhttp.status == 200)
				{
					document.getElementById(element).innerHTML = xmlhttp.responseText;
				}
				else
				{
					document.getElementById(element).innerHTML = "Hata Oluştu Lütfen Tekrar Deneyin";
				}
            }
        }
		
        xmlhttp.send(postvars);
    }
}

function phantom_getContent(id, address, postvars, _method, async, loader_img_src)
{
	document.write("<div id='" + id + "'></div>");	
	var div = document.getElementById(id);	
	div.innerHTML = loader_img_src != null ? loader_img_src : "<img src='images/progress.gif' border='0'>";		
	
    var xmlhttp = getXMLHTTP();
    
    var method = _method == 'GET' ? 'GET' : 'POST';
    
    if (xmlhttp)
    {
        xmlhttp.open(method, address, async);
        
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml; charset=iso-8859-9');
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		
		xmlhttp.setRequestHeader("Connection", "close");
		
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState == 4)
            {					
                if (xmlhttp.status == 200)
				{					
					div.innerHTML = xmlhttp.responseText;
				}
				else
				{
					div.innerHTML = "Hata Oluştu Lütfen Tekrar Deneyin";					
				}
            }
        }
		
        xmlhttp.send(postvars);
    }
}

function phantom_setContent_afterFunc(element, address, postvars, _method, async, afterFunction, params, loader_img_src)
{
    document.getElementById(element).innerHTML = loader_img_src != null ? loader_img_src : "<img src='images/progress.gif' border='0'>";
	
	// XmlHTTP nesnesi olustur.
    var xmlhttp = getXMLHTTP();
    
    // Method'u belirle.
    var method = _method == 'GET' ? 'GET' : 'POST';
    
    if (xmlhttp)
    {
        // xmlhttp.setTimeouts(resolveTimeout, connectTimeout,sendTimeout, receiveTimeout);        
    
        xmlhttp.open(method, address, async);
        
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml; charset=iso-8859-9');
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		// xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.setRequestHeader("Connection", "close");
		
        xmlhttp.onreadystatechange=function()
        {
            /*
            0 -> uninitialized ( henüz open metodu kullanılmadı)
            1 -> loading (sunucuya istek gönderildi, yükleniyor)
            2 -> loaded (sunucuya istek gönderildi.)
            3 -> interactive (sunucudan cevap geldi ama başka bir veri daha bekleniyor)
            4 -> complete (cevap tamamlandı ve geri geldi.)
            */
            if(xmlhttp.readyState == 4)
            {					
                if (xmlhttp.status == 200)
				{
					document.getElementById(element).innerHTML = xmlhttp.responseText;
					afterFunction(params);
				}
				else
				{
					document.getElementById(element).innerHTML = "Hata Oluştu Lütfen Tekrar Deneyin";
				}
            }
        }
		
        xmlhttp.send(postvars);
    }
}

function phantom_doJob(address, postvars, _method, async)
{
	var xmlhttp = getXMLHTTP();
    
    var method = _method == 'GET' ? 'GET' : 'POST';
    
    if (xmlhttp)
    {
        xmlhttp.open(method, address, async);
        
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml; charset=iso-8859-9');
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		
		xmlhttp.setRequestHeader("Connection", "close");
		
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState == 4)
            {					
                if (xmlhttp.status == 200)
				{	
					// islem tamam
				}
				else
				{
					//element.innerHTML = "Hata Oluştu Lütfen Tekrar Deneyin";					
				}
            }
        }
		
        xmlhttp.send(postvars);
    }
}

function phantom_doJob_afterFunc(address, postvars, _method, async, afterFunction, params)
{
	var xmlhttp = getXMLHTTP();
    
    var method = _method == 'GET' ? 'GET' : 'POST';
    
    if (xmlhttp)
    {
        xmlhttp.open(method, address, async);
        
        if (xmlhttp.overrideMimeType) xmlhttp.overrideMimeType('text/xml; charset=iso-8859-9');
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");		
		xmlhttp.setRequestHeader("Connection", "close");
		
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState == 4)
            {					
                if (xmlhttp.status == 200)
				{	
					afterFunction(params);
				}
				else
				{
					//element.innerHTML = "Hata Oluştu Lütfen Tekrar Deneyin";					
				}
            }
        }
		
        xmlhttp.send(postvars);
    }
}
// -------------------------------------------------------------------------------------------------

// JSON
// -------------------------------------------------------------------------------------------------
function getJSON()
{
	return document.createElement('script');	
}

function phantom_JSON_setContent(element, address)
{
	var script = getJSON();
	
	var char = '?';
	if (address.indexOf('?') != -1)
	{
		char = '&';
	}
	
	script.setAttribute('src', address + char + 'e=' + element);

	document.getElementById(element).appendChild(script);
}
// -------------------------------------------------------------------------------------------------

/*
Sample
<input type="button" name="button" value="FCK İçeriğini Gönder" onClick="fckform.submit(); getContent('content', 'loaded_content.aspx?deneme=1', 'test=' + fckform.fck.value, 'POST', true );" />

<input type="button" name="button" value="Content Değeri" onClick="alert(document.getElementById('content').innerHTML);" />
*/


// Documentation 
/*

METHODS
-----------
abort()  	Cancels the current request

getAllResponseHeaders() 	Returns the complete set of http headers as a string

getResponseHeader("headername") 	Returns the value of the specified http header

open("method","URL",async,"uname","pswd") 	Specifies the method, URL, and other optional attributes of a request
The method parameter can have a value of "GET", "POST", or "PUT" (use "GET" when requesting data and use "POST" when sending data (especially if the length of the data is greater than 512 bytes.
The URL parameter may be either a relative or complete URL.
The async parameter specifies whether the request should be handled asynchronously or not. true means that script processing carries on after the send() method, without waiting for a response. false means that the script waits for a response before continuing script processing

send(content) 	Sends the request

setRequestHeader("label","value") 	Adds a label/value pair to the http header to be sent

PROPERTIES
-------------
onreadystatechange 	An event handler for an event that fires at every state change

readyState 	Returns the state of the object:

0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete

responseText 	Returns the response as a string

responseXML 	Returns the response as XML. This property returns an XML document object, which can be examined and parsed using W3C DOM node tree methods and properties

status 	Returns the status as a number (e.g. 404 for "Not Found" or 200 for "OK")

statusText 	Returns the status as a string (e.g. "Not Found" or "OK")

// Using JSON in Ajax
// -------------------------------------------------------------------------------------------------------------------
var the_object;
var http_request = new XMLHttpRequest();
http_request.open( "GET", url, true );
http_request.onreadystatechange = function () {
    if ( http_request.readyState == 4 ) {
        if ( http_request.status == 200 ) {
            the_object = eval( "(" + http_request.responseText + ")" );
        } else {
            alert( "There was a problem with the URL." );
        }
        http_request = null;
    }
};

*/