var getOnlineUsers = 111;
var getOnlineUsersID = 1;

var DELIMITER_TOKEN	  =String.fromCharCode(6);
var DELIMITER_OBJECT  =String.fromCharCode(7);
var DELIMITER_FIELD	  =String.fromCharCode(8);


function CmdWriter(method){
	this.ID= new Date().getTime();
	this.Method = method;
    this.setID = setID;
    this.getID = getID;
    this.setMethod = setMethod;
    this.getMethod = getMethod;
    this.appendStr = appendStr;
    this.endOfObject = endOfObject;

	this.Buffer = this.ID + DELIMITER_TOKEN + (method) + DELIMITER_TOKEN;

	function appendStr(value){
		this.Buffer += value + DELIMITER_FIELD;
	}

	function endOfObject(){
		this.Buffer += DELIMITER_OBJECT;
	}

    function setID(id){
        this.ID = id;
    }

    function getID(){
        return this.ID;
    }

    function setMethod(method){
        this.Method = method;
    }

    function getMethod(){
        return this.Method;
    }
}

function CmdReader(text){
	this.ID=null;
	this.Method = null;
    this.setID = setID;
    this.getID = getID;
    this.setMethod = setMethod;
    this.getMethod = getMethod;
    this.parse = parse;
    this.hasObject = hasObject;
    this.advanceNextObject = advanceNextObject;
    this.getStr = getStr;

	var	elements = new Array();
	var cursor = 0;
	
	parse(text);

	function parse(text){
		// TODO: convert this thing to char array one time iteration form
		// TODO: add exception throwing then...^
		var tokens = text.split("" + DELIMITER_TOKEN);
		if(tokens==null || tokens.length != 3){
			dbg("error parsing the text => " + text);
			return;
		}
		this.ID=tokens[0];
		this.Method=tokens[1];

		var objects=tokens[2].split("" + DELIMITER_OBJECT);
		
		for(var i=0 ; i < objects.length-1 ; i++){
			var fields = objects[i].split("" + DELIMITER_FIELD);
			for(var j=0 ; j < fields.length-1 ; j++){
				elements.push(fields[j]);
			}
			elements.push(null);
			//objectCount++;
		}
		
	}

	function hasObject(){
		if(cursor==0)
			return true;
		return advanceNextObject();
	}

	function advanceNextObject(){
		for(var i = cursor ; i<elements.length; i++){
			if(elements[i]==null & i<elements.length-1){
				cursor = i+1;
				return true;
			}
		}
		return false;
	}

	function getStr(){
		return elements[cursor++];
	}

    function setID(id){
        this.ID = id;
    }

    function getID(){
        return ID;
    }

    function setMethod(method){
        this.Method = method;
    }

    function getMethod(){
        return Method;
    }
}




function CmdReaderOldVersion(strCommand){
	var arrReturn = new Array(2);
	
	if(strCommand!=null && strCommand.length>0){
		var arrCommands = strCommand.split(DELIMITER_OBJECT);
		if(arrCommands!=null && arrCommands[0]!=null){
			var arrTokens = arrCommands[0].split(DELIMITER_TOKEN);
			if(arrTokens!=null && arrTokens[0]!=null){
				arrReturn[0] = arrTokens[0];
				arrReturn[1] = arrTokens[1];
				var arrObjects = arrTokens[2].split(DELIMITER_OBJECT);
				var arrComplete = new Array();

				if(arrObjects!=null && arrObjects[0]!=null){
					for(var i=0 ; i < arrObjects.length ; i++){
						var arrFields = arrObjects[i].split(DELIMITER_FIELD);

						for(var j=0 ; j < arrFields.length ; j++){
							arrComplete.push(arrFields[j]);
						}
						arrComplete.push(null);
					}
				}

				if(arrComplete!=null && arrComplete[0]!=null){
					arrReturn[2] = arrComplete;
				}
			}
		}
	}

	return arrReturn;
}

function oyXMLRPCProvider (showLoading) {	
	var url = null;	
	var msg = null;
	var req = null;
	var oThis = this;
	this.createPacket = null;

	//get the caller function details
	myCallerName = oyXMLRPCProvider.caller.toString().match(/function (\w*)/)[1];
	myCallerArgCount = oyXMLRPCProvider.caller.arguments.callee.arguments.length;
	if(myCallerName == "retrieveShortProfile" || myCallerName == "retrieveShortProfileNew" ||  myCallerName == "retrieveUserDetails"){
		setTimeout("reloadProfileBanner();",1000);
	}
	/*if(myCallerName != "getIM" && myCallerName != "relationSearch" && myCallerName != "display20PercentDiscountMsj" && myCallerName != "displayUniversityUpdateWarning" && myCallerName != "displayTagsInfoLayer" && myCallerName != "displayMobileInfoLayer" && myCallerName != "getQuickContacts" && myCallerName != "retrieveQuickContacts"){
		if(myCallerName == "displayProfile"){
			reloadBanner('profilelayer');
			dbg("s1");
		} else {
			reloadBanner();
			dbg("s2");
		}
	}*/
	
	var strArguments = "";
	if(myCallerArgCount>0){
		for(var argCount = 0; argCount < myCallerArgCount; argCount++){
			strArguments += "#SA#" + oyXMLRPCProvider.caller.arguments.callee.arguments[argCount] + "#SA#";
			if(argCount+1<myCallerArgCount){strArguments += ",";}
		}
	}
	var strFunction = myCallerName + "(" + strArguments + ")";

	// internal callback function for the browser; it is called when a state of a request object changes
	var internalRequestComplete = function() {
		var STATE_COMPLETED = 4;
		var STATUS_200 = 200;
		if (req.readyState == STATE_COMPLETED) {
			if (req.status == STATUS_200) {
				if(showLoading==null || showLoading){loading("none");}
				if(oThis.onComplete){
					respText = req.responseText;
					if(respText!=null && respText.length>0){
						try{
							reader = new CmdReader(respText);
							id = reader.getID();
							method = reader.getMethod();
							if(id!=null && method!=null){
								if(id=="0"){
									dbg("XMLRPCProvider error : Server returned an error!!!");
								}else if(id=="-1"){
									dbg(strFunction);
									if(strFunction && strFunction.indexOf("loadSelectedGame")>=0){
										displayLogin();
									}else {
										displayLogin(strFunction);  //?
									}
									if(myCallerName == "retrieveTest" || myCallerName == "getIM"){
										document.getElementById('sMsgLayer').style.display="none";
									}
									if(myCallerName == "retrieveTestResultsForUser"){
										if(document.getElementById('generalTable')){
											document.getElementById('generalTable').style.display = "";
										}
									}
									dbg("XMLRPCProvider error : Session died!!!");
								}else{
									oThis.onComplete(req.responseText);
								}
							}
						}catch(e){
							if(e.description == null){
								dbg("XMLRPCProvider error<br>CallerName : " + myCallerName + "<br>Err : " + e.message + "<br>And the responseText iz : " + respText);
							}else{
								dbg("XMLRPCProvider error<br>CallerName : " + myCallerName + "<br>Err : " + e.description + "<br>And the responseText iz : " + respText);
							}
						}
					}else{
						dbg("XMLRPCProvider error : responseText is null!!!");
					}
				}				 
			} 
		}
	}	
	
		// internal callback function for the browser; it is called when a state of a request object changes
	var internalRequestCompleteForGet = function() {
		var STATE_COMPLETED = 4;
		var STATUS_200 = 200;
		if (req.readyState == STATE_COMPLETED) {
			if (req.status == STATUS_200) {
				if(showLoading==null || showLoading){loading("none");}
				if(oThis.onComplete){
					respText = req.responseText;
					oThis.onComplete(req.responseText);

				}				 
			} 
		}
	}

	//  call this function to submit new request
	this.submit = function(_url){
		if(showLoading==null || showLoading){loading("");}
		url = _url;	
		
		msg = "";
		if(oThis.createPacket) {
			msg = "m=" + escape(oThis.createPacket());
			
		}
		
	    if (window.XMLHttpRequest) {
	    	// branch for native XMLHttpRequest object
	    		
	        req = new XMLHttpRequest();
	        req.onreadystatechange = internalRequestComplete;
	        req.open("POST", url, true);
	        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		   	req.setRequestHeader("Content-length", msg.length);
        	req.send(msg);	
	    } else { 
	    	// branch for IE/Windows ActiveX version
	    	if (window.ActiveXObject) {	    		
		        req = new ActiveXObject("Microsoft.XMLHTTP");
		        if (req) {
		            req.onreadystatechange = internalRequestComplete;
		            req.open("POST", url, true);
			    	req.setrequestheader("Pragma","no-cache");
		   	    	req.setrequestheader("Cache-control","no-cache");
		   	    	req.setrequestheader("Content-type", "application/x-www-form-urlencoded");
		   	    	req.setrequestheader("Content-length", msg.length);
		        	req.send(msg);	
		        } 
		    } 
	    }
	}
	
	//  call this function to submit new request
	this.get = function(_url){
		if(showLoading==null || showLoading){loading("");}
		url = _url;	
		
		msg = "";
		if(oThis.createPacket) {
			msg = "m=" + escape(oThis.createPacket());
			
		}
		
	    if (window.XMLHttpRequest) {
	    	// branch for native XMLHttpRequest object
	    		
	        req = new XMLHttpRequest();
	        req.onreadystatechange = internalRequestCompleteForGet;
	        req.open("GET", url, true);
        	req.send(null);	
	    } else { 
	    	// branch for IE/Windows ActiveX version
	    	if (window.ActiveXObject) {	    		
		        req = new ActiveXObject("Microsoft.XMLHTTP");
		        if (req) {
		            req.onreadystatechange = internalRequestCompleteForGet;
		            req.open("GET", url, true);
			    	req.setrequestheader("Pragma","no-cache");
		   	    	req.setrequestheader("Cache-control","no-cache");
		        	req.send(null);	
		        } 
		    } 
	    }
	}	
	
	
	// user can override this;  this function is called when response is received without errors
	this.onComplete = function(responseText){
	}
}

function reloadBanner(reqStr){
		/*
		if(document.getElementById("ifAd")){
			if(reqStr){
				document.getElementById("ifAd").src="/adver.jsp?reqURI="+reqStr;
			} else {
				document.getElementById("ifAd").src="/adver.jsp?reqURI="+requestURI;
			}
			
		}*/
		if(document.getElementById("ifAdTop")){
			if(reqStr){
				document.getElementById("ifAdTop").src="/adverTop.jsp?reqURI="+reqStr;
			}else{
				document.getElementById("ifAdTop").src="/adverTop.jsp?reqURI="+requestURI;
			}
		}
		
		
	}
	
function reloadProfileBanner(reqStr){
	dbg("in reload profile banner");
	/*
	if(document.getElementById("ifAd")){
		if(reqStr){
			document.getElementById("ifAd").src="/adver.jsp?reqURI="+reqStr;
		} else {
			document.getElementById("ifAd").src="/adver.jsp?reqURI="+requestURI;
		}
	}
	*/
	if(document.getElementById("ifAdProfile300")){
		document.getElementById("ifAdProfile300").src="/adver.jsp?reqURI=profile300";
	}
	
	if(document.getElementById("ifAdTop")){
		if(reqStr){
			document.getElementById("ifAdTop").src="/adverTop.jsp?reqURI="+reqStr;
		}else{
			document.getElementById("ifAdTop").src="/adverTop.jsp?reqURI="+requestURI;
		}
	}
	
}
	
function loading(strDisplayValue){
	try{document.getElementById("loading").style.display = strDisplayValue;}
	catch(e){}
}

function dbg(debugStr){
	if(document.getElementById("debug")){
		document.getElementById("debug").innerHTML = "<pre>" + debugStr + "</pre>" + document.getElementById("debug").innerHTML;
	}
}