var URL_WEBCHAT = "https://www.webcontactcenter4.orange-business.com/webchat";
var URL_PROBE = "https://www.w2c-chat4.orange-business.com/akio-acc-sonde";

function AkioTracking(url, instance) {
   this.loaded = false;
   this.url=url;
   this.script;
   this.firstparam=true;
   this.serverResponse;
   this.instance=instance;
   that=this;

   this.addParameter = function(p, v) {
      if (that.firstparam) {
         that.url+='?'+p+'='+v;
         that.firstparam=false;
      } else {
         that.url+='&'+p+'='+v;
      }
   }
   this.request = function(force) {
      if (force) {
         that.addParameter('akio-nocache', Math.random());
      }
      that.script = document.createElement('script');
      that.script.setAttribute('charset','UTF-8');
      that.script.setAttribute('type','text/javascript');
      that.script.setAttribute('src', that.url);
      that.script.onload = that.onLoad;
      that.script.onreadystatechange = that.onLoad;
      document.getElementsByTagName('head')[0].appendChild(that.script);
   }
   this.onComplete = function(serverResponse) { };
   this.onLoad = function() {
      if (that.loaded) { return; }
      that.loaded=true;
   };
   return this;
}
function hideConfirmWindow() {
	getTracker().displayConfirmWindow(false);
}
function akio_openChatPopup(isTrackingMode) {
	if (isTrackingMode != null && isTrackingMode) {
		getTracker().displayConfirmWindow(false);
	}
	var sessionParams = getTracker().getSessionParameters();
	var profileParams = getTracker().getProfileParameters();
	var wScreen = window.screen.availWidth; 
	var hScreen = window.screen.availHeight;
	var wPopup = 582;
	var hPopup = 368;
	var formulaire = document.createElement('form');
	formulaire.setAttribute('name','akioChatPopup');
	formulaire.setAttribute('method','post');
	formulaire.setAttribute('target','int_chat_window');
    formulaire.appendChild(addParam("vdom", sessionParams.vdom));
    formulaire.appendChild(addParam("knowgroupId", sessionParams.knowgroup));
    formulaire.appendChild(addParam("mailboxId",sessionParams.mailbox));
    formulaire.appendChild(addParam("knowdom",sessionParams.knowdom));
    formulaire.appendChild(addParam("knowdomId",sessionParams.knowdom));
    formulaire.appendChild(addParam("locale",sessionParams.locale));
    if (isTrackingMode != null && isTrackingMode) {
        formulaire.appendChild(addParam("trackingMode",isTrackingMode));
        formulaire.setAttribute('action', URL_WEBCHAT+"/pub/tracking_page_attente.jelly");
    } else {
        formulaire.setAttribute('action', URL_WEBCHAT+"/pub/page_attente.jelly");
    }
    try {
    	formulaire.appendChild(addParam("siteParameters", escape(getContextParams())));
    } catch (err) {
    }
    try {
    	formulaire.appendChild(addParam("profileParameters", escape(profileParams)));
    } catch (err) {
    }
    document.getElementsByTagName('body')[0].appendChild(formulaire);
	window.open("","int_chat_window","width="+wPopup+",height="+hPopup+",top=0, left="+(wScreen-wPopup));
    formulaire.submit();
    document.getElementsByTagName('body')[0].removeChild(formulaire);
}

function addParam(name,value) {
	var p1 = document.createElement('input');
    p1.setAttribute("type","hidden");
    p1.setAttribute("name",name);
    p1.setAttribute("value",value);
    return p1;
}

var akioTracker = null;
function getTracker() {
	if (akioTracker == null) {
		akioTracker = new AkioTracker();
	}
	return akioTracker;
}

function AkioTracker() {
	this.buttonID = "button";
	this.confirmID = "confirmWindow";
	this.RULES_TRACKING = "RULES";
	this.SESSION = "SESSION";
	this.ONLINE = "online";
	this.OFFLINE = "offline";

	this.sessionParams;
	this.profileParams;
	this.activity;
	this.delayBeforeStartProbe = 0;
	this.delayBetween2Probe = 5000;
	this.ruleOK;
	this.mode = this.OFFLINE;
	this.debug = false;
	this.type = this.SESSION;
    var self = this;
	
	this.startRulesTracking = function(dBeforeFirstProbe) {
		this.newSession(dBeforeFirstProbe, null, this.RULES_TRACKING);
	}
	this.startSession = function(dBeforeFirstProbe, dBetween2Probe) {
		this.newSession(dBeforeFirstProbe, dBetween2Probe, this.SESSION);
	}
	this.newSession = function(dBeforeFirstProbe, dBetween2Probe, type) {
		this.type = type;
		if (dBeforeFirstProbe != null) {
			this.delayBeforeStartProbe = dBeforeFirstProbe;
		}
		if (dBetween2Probe != null) {
			this.delayBetween2Probe = dBetween2Probe;
		}
		if (this.mode == this.ONLINE) {
			this.initRules();
		} else {
			this.evalRules();
		}
	}
	this.initRules = function() {
		var myRequest = new AkioTracking(URL_PROBE + "/ruleProbe");
		myRequest.addParameter("vdom", this.sessionParams.vdom);
		myRequest.addParameter("pageid", this.sessionParams.page);
		myRequest.addParameter("callback", "getTracker().setRules");
		myRequest.request(true);
	}
	this.setRules = function(rules) {
		try {
			eval(rules);
			this.evalRules();
		} catch (err) {
			this.trace("Exception lors de l'evaluation des regles de gestion", err);
		}
	}
	this.evalRules = function() {
		try {
			var fctName = this.mode+"_businessRule();";
			if (eval(fctName)) {
				var self = this;
				setTimeout(function() {self.startProbe()},self.delayBeforeStartProbe);
			}
		} catch (err) {
			this.trace("Exception lors de l'exécution de la méthode "+this.mode+"_businessRule();", err);
		}
	}
	this.startProbe = function() {
		var myRequest = new AkioTracking(URL_PROBE + "/sonde");
	    myRequest.addParameter("vdom",this.sessionParams.vdom);
		myRequest.addParameter("mailbox",this.sessionParams.mailbox);
		myRequest.addParameter("knowdom",this.sessionParams.knowdom);
		myRequest.addParameter("knowgroup",this.sessionParams.knowgroup);
		myRequest.addParameter("callback", "getTracker().setProbeRule");
	    myRequest.request(true);
	}
	this.setProbeRule = function(rule) {
		try {
			this.activity = rule;
			this.evalProbeRule();
		} catch (err) {
			this.trace("Exception lors de l'evaluation de l'objet JSON activity", err);
		}
	}
	this.evalProbeRule = function() {
		try {
			var bResult;
			if (this.mode == this.OFFLINE) {
				bResult = offline_chatRule(this.activity);
			} else if (this.mode == this.ONLINE) {
				bResult = online_chatRule(this.activity);
			}
			if (this.type == this.SESSION) {
				this.displayButton(bResult);
				self = this;
				setTimeout(function() {self.startProbe()},self.delayBetween2Probe);
			}
			else if (this.type == this.RULES_TRACKING && bResult) {
				this.displayConfirmWindow(true);
			}
		} catch (err) {
			this.trace("evalProbeRule", err);
		}
	}	
	this.displayButton = function(b) {
		try {
			if (b) {
				document.getElementById(this.buttonID).style.display = "block";
			} else {
				document.getElementById(this.buttonID).style.display = "none";
			}
		} catch (err) {
			this.trace("img button not found", err);
		}
	}
	this.displayConfirmWindow = function(b) {
		try {
			if (b) {
				document.getElementById(this.confirmID).style.display = "block";
			} else {
				document.getElementById(this.confirmID).style.display = "none";
			}
		} catch (err) {
			this.trace("confirm Window not found", err);
		}
	}
	this.setDebug = function(b) {
		this.debug = b;
	}
	this.setButtonId = function(b) {
		try {
			this.buttonID = b;
		} catch(err) {
			this.trace("setButtonId", err);
		}
	}
	this.setConfirmWindowId = function(b) {
		try {
			this.confirmID = b;
		} catch(err) {
			this.trace("setConfirmWindowId", err);
		}
	}
	this.getConfirmWindowId = function() {
		return this.confirmID;
	}
	this.setSessionParameters = function(p) {
		try {
			this.sessionParams = eval('('+p+')');
		} catch (err) {
			this.trace("setSessionParameters", err);
		}
	}
	this.setProfileParameters = function(p) {
		try {
			this.profileParams = eval('('+p+')');
		} catch (err) {
			this.trace("setProfileParameters", err);
		}
	}
	this.setMode = function(m) {
		try {
			this.mode = m;
		} catch (err) {
			this.trace("setMode", err);
		}
	}
	this.trace = function(fctName, err) {
		if (this.debug) {
			alert(fctName+ " : "+ err);
		}
	}
	this.getSessionParameters = function() {
		return this.sessionParams;
	}
	this.getProfileParameters = function() {
		return this.profileParams;
	}
	return this;
}

