﻿
function AjaxRequest(url,data,template,contentID,messageID,keepEvents) {
	return glbXmlRequest.loadURL(url,data,template,contentID,messageID,keepEvents)
}

function AkimanXmlHTTP(loading, objDebug){ 
	this.xmlHttp=null;  
	this.isBusy=false;
	this.busyMessage="Loading... Please wait";
	this.errorMessage="Connection Error";
	this.intGarbageCount=0;
	this.isLogging = false;
	
	if (window.ActiveXObject) {
		this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
    else {
		this.xmlHttp=new XMLHttpRequest();
	}

	this.setBusy = function(blnBusy){		
		this.isBusy=blnBusy;
		
		if(loading) {
			if (this.isBusy) {        
				loading.style.display="block";
			}
			else {
				loading.style.display="none";
			}
		}
	}

	this.loadURL=function(url,data,template,contentID,messageID,keepEvents) {

		if (!this.isBusy){
			var objContent = null;
			if(contentID != null && contentID != "") {
				objContent = document.getElementById(contentID);
			}
			if(objContent == null) {
				objContent = document.getElementById("Content");
			}
			
			var message = null;
			if(messageID != null && messageID != "") {
				message = document.getElementById(messageID);
			}
			if(message == null) {
				message = document.getElementById("message");
			}

			this.setBusy(true);
			this.intGarbageCount++;
			this.xmlHttp.open("POST", url, false);
			this.xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");        
			this.xmlHttp.setRequestHeader("Content-Length",data.length); 
			this.xmlHttp.send(data);
			if(objDebug) {
				objDebug.innerHTML="Veri Boyutu:"+this.xmlHttp.responseText.length+" byte <br /><br />";        
				objDebug.innerHTML+= this.xmlHttp.responseText.replace(/</g,"&lt;").replace(/>/g,"&gt;<br>")+"<br />";
			}
			
			if (this.xmlHttp.status==200) {	
				var innerHTML = "";
				var responseText = this.xmlHttp.responseText;
				var akimanXmlDocument = new AkimanXmlDocument(this.xmlHttp.responseText);
				
				if(template != null) {
					var akimanXslDocument = null;
					if(DEBUG) {
						akimanXslDocument = new AkimanXslDocument("layout/" + template);
					}
					else {
						akimanXslDocument = GetXSLT(template);
						if(akimanXslDocument == null) {
							akimanXslDocument = new AkimanXslDocument("layout/" + template);
							glbXslCacheArray.push(new Array(template, akimanXslDocument));
						}
					}
					
					if(akimanXslDocument == null) {
						akimanXslDocument = new AkimanXslDocument("layout/" + template);
						glbXslCacheArray.push(new Array(template, akimanXslDocument));
					}

					innerHTML = akimanXmlDocument.transform(akimanXslDocument);
					akimanXslDocument = null;
				}
				
				this.resultXML = akimanXmlDocument.objXml;
				akimanXmlDocument = null;

				if(template!=null && !keepEvents) {
					glbContentEvents.clear();			// clear events
					suggestCollection.removeAll();	// clear suggest objects and events			
				}

				this.setBusy(false);
				
				if(template != null) {
					if(this.GotMessageOnly()) {
						if(message) message.innerHTML = innerHTML;
					}
					else {
						if(objContent) objContent.innerHTML = innerHTML;
					}
				}
				
				triggeredEventHandler();
				return responseText;
			}
			else {
				if(!this.isLogging) {
					this.isLogging = true;
					this.setBusy(false);
					var params = CustomParamBuilder(
								"url", location.href,
								"service", url,
								"sent",data.replace(/]]>/g, "]]]]><![CDATA[>"),"template",template,
								"received", this.xmlHttp.responseText.replace(/]]>/g, "]]]]><![CDATA[>")
								);
					var soapData = SoapDataBuilder("Log", params);

					var asyncXmlHTTP = new AkimanXmlAsyncHTTP();
					asyncXmlHTTP.loadURL("ErrorLog.asmx", soapData)

					this.isLogging = false;
					alert(this.errorMessage);
				}
				
				return "";
			}

			this.setBusy(false);
		}
		else {
			if(!this.isLogging) {
				alert(this.busyMessage);
			}
		}
		
		if(window.CollectGarbage) {
			if (this.intGarbageCount > 30) {
				window.CollectGarbage();
				this.intGarbageCount=0;
			}
		}
	}
	
	this.ResponseText = function() {
		return this.xmlHttp.responseText;
	}

	this.MessageCode = function() {
		var code = "";
		if (this.resultXML) {
			try {
				var nodes = this.resultXML.getElementsByTagName("MessageCode");
				if(nodes && nodes.length>0 && nodes[0].attributes && nodes[0].attributes.length>0) {
					code = nodes[0].attributes[0].nodeValue;
				}
			}
			catch(e) {}
		}
		return code;
	}
	
	this.GotMessageOnly = function() {
		return (this.xmlHttp.responseText.indexOf("<MessageOnly") > -1);
	}
}


function AkimanXmlAsyncHTTP(){ 
	this.xmlHttp=null;  
	this.isBusy=false;
	this.errorMessage="Connection error";
	this.busyMessage="Loading... Please wait";
	this.xsl=null;

    if (window.ActiveXObject)
        this.xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    else
        this.xmlHttp=new XMLHttpRequest();

	this.setBusy = function(blnBusy){		
		this.isBusy = blnBusy;
		
//		if(loading) {
//			if (this.isBusy) {        
//				loading.style.display="block";
//			}
//			else {
//				loading.style.display="none";
//			}
//		}
	}
	
	this.loadURL=function(url, data, func, template){
		if (!this.isBusy){
			this.xsl = template;
			this.xmlHttp.open("POST",url,true);
			if(func != null) {
				this.xmlHttp.onreadystatechange = func;
			}		
			this.xmlHttp.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");        
			this.xmlHttp.setRequestHeader("Content-Length",data.length); 
			this.xmlHttp.send(data);
		}
		else{
			//alert(this.busyMessage);
		}
	}
	
	this.TransformResponse = function() {
		return TransformXML(this.xmlHttp.responseText, this.xsl);
	}

	this.ReadyState = function() {
		return this.xmlHttp.readyState;
	}

	this.Status = function() {
		return this.xmlHttp.status;
	}

	this.ResponseText = function() {
		return this.xmlHttp.responseText;
	}
}

function Paging() {
	this.pageNumber = null;
	this.recordIndex = null;
	this.fromCache = null;
	this.pageSize = null;
	this.sortColumn = null;
	
	this.OutputXML = function() {
		var result = "";
		
		if(this.pageNumber != null) {
			result += "<pageNumber>"+this.pageNumber+"</pageNumber>";
		}
		if(this.recordIndex != null) {
			result += "<recordIndex>"+this.recordIndex+"</recordIndex>";
		}
		if(this.fromCache != null) {
			result += "<fromCache>"+this.fromCache+"</fromCache>";
		}							
		if(this.pageSize != null) {
			result += "<pageSize>"+this.pageSize+"</pageSize>";
		}
		if(this.sortColumn != null) {
			result += "<sortColumn>"+this.sortColumn+"</sortColumn>";
		}

		return result;
	}
}

function SoapDataBuilder(functionName,params,paging,strTask){
    if (strTask == null) strTask="";
    if (params == null) params="";

    var soapData= 
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
            "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"+
            "<soap12:Body>"+
                "<"+functionName+" xmlns=\"http://tempuri.org/\">"+
                "<serviceRequest>"+
                    "<requestData>"+
						"<strTask>"+strTask+"</strTask>"+
						"<postdata>"+params+"</postdata>"+
						"<paging>"
						
						if(paging && paging.OutputXML) {
							soapData += paging.OutputXML();
						}

	soapData +=			"</paging>"+
					"</requestData>"+
                "</serviceRequest>"+
            "</"+functionName+">"+
        "</soap12:Body>"+
        "</soap12:Envelope>";       
        return soapData;
}

function CustomParamBuilder(){    
	result="";
	for (i = 0; i< arguments.length;){
		result+="<item name=\"" + arguments[i] + "\"><![CDATA[" + arguments[i+1]+ "]]></item>";  
		i=i+2;
	}
	return result;
}

function triggeredEventHandler(){
	if (glbXmlRequest.resultXML){
		var eventList = glbXmlRequest.resultXML.getElementsByTagName("triggeredEvent");
		for (var i=0; i<eventList.length; i++) {
			try {
//				alert(GetXmlNodeValue(eventList, i))
				eval(GetXmlNodeValue(eventList, i));
			}
			catch(e) {
				if(DEBUG) {
					alert("triggeredEventHandler Error:\n" + GetXmlNodeValue(eventList, i) + "\n" + e.message)
				}
			}
		}
	}
}