﻿function AjaxClient(){

	this.Http = new ActiveXObject("microsoft.xmlhttp");

	this.Get = function(url){	
		this.Http.open("GET",url,true);
		this.Http.onreadystatechange = AjaxClientCallback;
		this.Http.send();
	}
	this.Get = function(url, callbackFunc){	
		this.Http.open("GET",url,true);
		this.Http.onreadystatechange = callbackFunc;
		this.Http.send();
	}
	this.Set = function(url,data,callback){
		this.Http.open("POST",url,true);
		this.Http.onreadystatechange = callbak;
		this.Http.send(data);	
	}

}
var xmlHttp  = null;
var AjaxHold = false;

function AjaxInit() 
{
	//if (xmlHttp != null) return true;
	
	if (window.XMLHttpRequest) { // Non-IE browsers 
		xmlHttp = new XMLHttpRequest();   
	} else if (window.ActiveXObject){ // IE 
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
	}
	
	if (xmlHttp == null) return false;
	else return true;
}

AjaxCallBack = function()
{
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200) {
			AjaxHold = false;
			if(xmlHttp.responseText=="" || xmlHttp.responseText== "<html><head></head><bobd></body></html>")
				return false;
			else
			{
				//alert(xmlHttp.responseText);
				eval(xmlHttp.responseText);
			}
		}
	}
}
BanAjaxCallBack = function()
{
	if (xmlHttp.readyState == 4){
		if (xmlHttp.status == 200) {
			AjaxHold = false;
			if(xmlHttp.responseText=="" || xmlHttp.responseText== "<html><head></head><bobd></body></html>")
				return false;
			else
			{
				if (xmlHttp.responseText.substr(0,5) == "fun##")
				{
					var data = xmlHttp.responseText;
					data = data.replace("fun##", "");
					$("randomBanner").innerHTML = data;
				}
				else
				{
					//alert(xmlHttp.responseText);
					eval(xmlHttp.responseText);
				}
			}
		}
	}
}

function AjaxSendData (ajaxCommand, callData) {
	if ($("divDebug")) $("divDebug").innerText = callData;
	
	AjaxHold = true;

	var NoCache = new Date().getTime();
	
	if(AjaxInit()) 
	{
		//if (ajaxCommand == "PB") 		alert("ajaxCommand = "+ajaxCommand+" AjaxSendData = "+callData);
		//alert(PageFrom);
		xmlHttp.onreadystatechange = AjaxCallBack;
		
		var curUrl = window.location.href.toLowerCase();
		var endIndex = curUrl.indexOf("templates")
		curUrl = curUrl.substring(0, endIndex);
		var url = curUrl + "Services/BidsEngine.aspx?NC=" + NoCache + "&PF=" + PageFrom + "&AC=" + ajaxCommand + "&" + callData;
		//alert(url);
		
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function AjaxSendBanData (ajaxCommand, callData) {
	if ($("divDebug")) $("divDebug").innerText = callData;
	
	AjaxHold = true;

	var NoCache = new Date().getTime();
	
	if(AjaxInit()) 
	{
		//alert(window.location.href.toLowerCase());
		//alert("ajaxCommand " + ajaxCommand+ " AjaxSendData "+callData);
		xmlHttp.onreadystatechange = BanAjaxCallBack;
		xmlHttp.open("GET", AppPath + "/Services/BannersData.aspx?NC=" + NoCache + "&AC=" + ajaxCommand + "&" + callData, true);
		xmlHttp.send(null);
	}
}
