var http = createRequestObject();
var objectId = "";
var ajaxurlarray = new Array();
var timerstatus = 0; //check whether the timer function is in active.
var loglinenumber = 1; //Used for only Log wrinting numbering.



function createRequestObject(htmlObjectId){
    var obj;
    var browser = navigator.appName;
    
    objectId = htmlObjectId;
    
    if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
        obj = new XMLHttpRequest();
    }
    return obj;
//		//------------------------------
//		$(document).ready(function(){
//        	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
//		});
//		//------------------------------
}

function logg(iname,ivalue) {
	//document.getElementById('div_log').innerHTML += "<br />"+ loglinenumber +". "+iname+" : "+ ivalue + "-----";
	loglinenumber = loglinenumber+1;
}


function sendReq_test(serverFileName, starting, variableNames, variableValues, htmlObjectId, firstlast, nextprev, numbers, stylearray, styletype) {
	var mystra = "";
	mystra += "serverFileName : " + serverFileName + "\n";
	mystra += "starting : " + starting + "\n";
	mystra += "variableNames : " + variableNames + "\n";
	mystra += "variableValues : " + variableValues + "\n";
	mystra += "htmlObjectId : " + htmlObjectId + "\n";
	mystra += "firstlast : " + firstlast + "\n";
	mystra += "nextprev : " + nextprev + "\n";
	mystra += "numbers : " + numbers + "\n";
	mystra += "stylearray : " + stylearray + "\n";
	mystra += "styletype : " + styletype + "\n";
	alert(mystra);
	//alert(createRequestObject(htmlObjectId));
}

function sendReq(serverFileName, starting, variableNames, variableValues, htmlObjectId, firstlast, nextprev, numbers, stylearray, styletype) { //requesttype = login | favourites | cart |
	//logg('objectId - Initial',objectId); //log
	//createRequestObject(htmlObjectId);
	//logg('objectId - Current',objectId); //log
	//Loading Bar---------------------------------
	document.getElementById(htmlObjectId).innerHTML = "<div style=\"font-family: Arial, Helvetica; font-size:11px; color:#666666; width: auto; height: auto; line-height: 30px; padding: 30px 0 30px 0; text-align:center; line-height: inherit;\" ><img src=\"includes_supporters/images/pagination/ajax-loader.gif\" /><br />Loading data... please wait...</div>";
	//--------------------------------------------
	
	var paramString = '';
	
	variableNames = variableNames.split(',');
	variableValues = variableValues.split(',');
	
	for(i=0; i<variableNames.length; i++) {
		paramString += variableNames[i]+'='+variableValues[i]+'&';
	}
	paramString = paramString.substring(0, (paramString.length-1));
			
	//if (paramString.length == 0) {
	//   	http.open('get', serverFileName);
	//}
	//else {
		if(!firstlast) { firstlast = "true"; }
		if(!nextprev) { nextprev = "true"; }
		if(!numbers) { numbers = "true"; }
		if(!stylearray) { stylearray = "0"; }
		if(!styletype) { styletype = "css"; }
		var ajaxurl = "response/ajax_"+serverFileName+"?"+paramString+"&serverpage="+serverFileName+"&htmlObjectId="+htmlObjectId+"&starting="+starting+"&firstlast="+firstlast+"&nextprev="+nextprev+"&numbers="+numbers+"&stylearray="+stylearray+"&styletype="+styletype;
		ajaxurlarray.push([htmlObjectId,ajaxurl]);
		logg('URL',ajaxurl); //log
		logg('Array Length', ajaxurlarray.length); //log
		
		if(timerstatus == "0") {
			//logg('ajaxexecute_pre', 'starting to initiate'); //log
			ajaxexecute_pre();
		} else {
			//logg('timerstatus', 'myself'); //log
		}
		//document.getElementById('div_log').innerHTML += "<br />"+ loglinenumber +". URL : "+ ajaxurl + "-----";
		//logg('URL',ajaxurl); //log
		//logg('Array Length', ajaxurlarray.length); //log
		//http.open('get', ajaxurl);
		
		//http.open('get', 'ajax_response/'+serverFileName+'?'+paramString+'&serverpage='+serverFileName+'&htmlObjectId='+htmlObjectId+'&starting='+starting+'&firstlast='+firstlast+'&nextprev='+nextprev+'&numbers='+numbers+'&stylearray='+stylearray+'&styletype='+styletype);
	//}
    //http.onreadystatechange = handleResponse;
    //http.send(null);
}



function ajaxexecute_pre() {
	//logg('ajaxexecute_pre','initiated'); //log
	if(objectId == "") {
		//logg('objectId - Status','empty'); //log
		if(ajaxurlarray.length > 0) {
			timerstatus = 1;
			ajaxexecute();
		} else {
			timerstatus = 0;
			//logg('Ajax Caller - Status','complete'); //log
			//logg('=====','================================'); //log
		}
	} else {
		//logg('objectId - Status','filled - ' + objectId); //log
	}
}

function ajaxexecute() {
	//logg('ajaxexecute', 'initiated'); //log alert(objectId);
	createRequestObject(ajaxurlarray[0][0]);
	http.open('get', ajaxurlarray[0][1]);
	http.onreadystatechange = handleResponse;
	http.send(null);
}

function handleResponse() {
	//logg('objectId : handleResponse',objectId); //log alert(objectId);
	if(http.readyState == 4){
		responseText = http.responseText;
		document.getElementById(objectId).innerHTML = responseText;
		myajax_response();	
		
		//------------------------------
//		$(document).ready(function(){
//        	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
//		});
		//------------------------------
		ajaxurlarray.shift() //-aja Removing the first element of the ajaxurlarray
		objectId = ""; //-aja Clearing the Object ID
		ajaxexecute_pre(); //-aja Re-calling
    }
}

