﻿
var	BaseURL		= "http://www.bahcelievler.bel.tr";
var	Online1		= "/Shared/SupportAlive/online_1.png";
var	Online2		= "/Shared/SupportAlive/online_2.png";
var	Online3		= "/Shared/SupportAlive/online_3.png";
var	Offline1	= "/Shared/SupportAlive/offline_1.png";
var	Offline2	= "/Shared/SupportAlive/offline_2.png";
var	Offline3	= "/Shared/SupportAlive/offline_3.png";

function SA(){

	this.URI			= parseUri(document.location.href.toString());
	this.UID			= this.CheckCookie();
	this.PID			= this.CreateID();
		
	var	_this	= this;
	
	if(this.URI.protocol == "file"){
	
		this.URI.protocol	= "http";
	}
			
	if(document.attachEvent){
	
		window.attachEvent("onbeforeunload", function(){_this.EndVisit();});
		window.attachEvent("onscroll", function(){_this.PinInviteWindow();});
		window.attachEvent("onresize", function(){_this.PinInviteWindow();});
	}
	else{
	
		window.addEventListener("beforeunload", function(){_this.EndVisit();}, false);
		window.addEventListener("scroll", function(){_this.PinInviteWindow();}, false);
		window.addEventListener("resize", function(){_this.PinInviteWindow();}, false);
	}
}

function CustomParam(Name, Value){

	this.Name	= Name;
	this.Value	= Value;
}

SA.prototype.URI				= null;
SA.prototype.UID				= null;
SA.prototype.PID				= null;
SA.prototype.Interval			= 15000;
SA.prototype.SATRServerURL		= "track.visilabs.com";
SA.prototype.ChatURL			= "chat.supportalive.com";
SA.prototype.SATRImg			= "vt.gif";
SA.prototype.VisitorName		= null;
SA.prototype.VisitorMail		= null;
SA.prototype.CustomerCode		= null;
SA.prototype.Username			= null;
SA.prototype.ContentGroup		= null;
SA.prototype.CustomParams		= new Array();
SA.prototype.OrgID				= "Imv1lBE%2fSfM%3d";
SA.prototype.InviteWindow		= null;
SA.prototype.InvitePad			= 10;
SA.prototype.OfflineMessage		= "Operatörlerimiz online değildir, şu anda size yardımcı olamıyoruz.<BR>Sorularınız için <B><a href=\"mailto:beyazmasa@bahcelievler.bel.tr\">beyazmasa@bahcelievler.bel.tr</a></B> adresine e-posta gönderebilirsiniz.";
SA.prototype.Lang				= "tr";
SA.prototype.Tracking			= true;
SA.prototype.ScriptContainer	= "_SAD";
SA.prototype.OperatorImg		= null;
SA.prototype.OperatorImg2		= null;
SA.prototype.OperatorImg3		= null;
SA.prototype.OpenSurvey			= "false";
SA.prototype.VisitorParams		= "";
SA.prototype.InfoParams			= null;

SA.prototype.CreateID = function(){

    var a = new Date();
    var b = a.getUTCFullYear();
    var c = a.getMonth() + 1;
    var d = a.getDate();
    var e = a.getHours();
    var f = a.getMinutes();
    var g = a.getSeconds();
    var s = "";
    
    if (parseInt(c) < 10) { c = "0" + c }
    if (parseInt(d) < 10) { d = "0" + d }
    if (parseInt(e) < 10) { e = "0" + e }
    if (parseInt(f) < 10) { f = "0" + f }
    if (parseInt(g) < 10) { g = "0" + g }
    
    for (i = 0; i < 16; i++) {
        s += String.fromCharCode(65 + Math.round(Math.random() * 25))
    }
    
    s += "" + b + c + d + e + f + g;
    
    return s;
};

SA.prototype.CheckCookie = function(){

	var CookieName	= 'SupportAlive';
	var cookies		= document.cookie;
	var	MyCookie	= "";

	if(cookies.indexOf(CookieName) != -1){
	
		var startpos	= cookies.indexOf(CookieName)+CookieName.length+1;
		var endpos		= cookies.indexOf(';',startpos);
		
		if(endpos == -1){
		 
			endpos = cookies.length;
		}
		
		MyCookie	= unescape(cookies.substring(startpos,endpos));
	}
	
	if(MyCookie.length == 30){
	
		var bCheck	= false;
		
		for( i=0;i<30;i++)
		{
			iCh = MyCookie.charCodeAt(i);

			if(((iCh >=48) && (iCh <=57)) || ((iCh >=65) && (iCh <=90)))
			{
				bCheck = true;
			}
			else
			{
				bCheck	= false;
				break;
			}
		}

		if(bCheck)
		{
			return MyCookie.substring(0,30);
		}
	}

	MyCookie = this.CreateID();
	this.SetCookie('SupportAlive=' + MyCookie);
		
	return MyCookie;
};

SA.prototype.SetCookie = function(txt){

	var	today = new Date();
	
	today.setDate(today.getDate() + 365 * 5);
	document.cookie = txt + ';path=/;expires=' + today.toGMTString();
};

SA.prototype.SendTrackingRequest = function(Type){

	var	_this	= this;
	var	_Type	= null;
	var	Dt		= new Date();
	var	Img		= new Image();
	
	if(Type && Type != null){
	
		_Type	= Type;
	}
	else{
	
		_Type	= "v";
	}
	
	var	Src		= this.URI.protocol + "://" + this.SATRServerURL +"/"+ this.SATRImg +"?type="+ _Type +"&oid="+ this.OrgID +"&uid="+ this.UID +"&pid=" + this.PID +"&r="+ PrepEncoded(document.referrer.toString()) + "&t=&rid=&dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();
	
	if(this.VisitorName != null){
	
		Src	+= "&n=" + PrepEncoded(this.VisitorName);
	}
	
	if(this.VisitorMail != null){
	
		Src	+= "&m=" + PrepEncoded(this.VisitorMail);
	}
	
	if(this.CustomerCode != null){
	
		Src	+= "&cc=" + PrepEncoded(this.CustomerCode);
	}

	if(this.Username != null){
	
		Src	+= "&un=" + PrepEncoded(this.Username);
	}
	
	if(this.ContentGroup != null){
	
		Src	+= "&cg=" + PrepEncoded(this.ContentGroup);
	}
	
	if(this.InfoParams != null){
	
		for(var i=0; i<this.InfoParams.length; i++){
		
			Src	+= "&pnid_"+ i.toString() +"="+ this.InfoParams[i][0] +"&pv_"+ i.toString() +"="+ PrepEncoded(this.InfoParams[i][1]);
		}
	}
	
	Img.onload	= function(){ _this.Switch(Img);};
	Img.src		= Src;
			
	if(this.Tracking){
	
		setTimeout(function(){_this.SendTrackingRequest("v");}, _this.Interval);
	}
};

SA.prototype.CreateInvite = function(){
	
	var	Tag	= null;
	var	Dt	= new Date();
		
	if(this.InviteWindow == null){
				
		Tag			= document.createElement("script"); 
		Tag.type	= "text/javascript"; 
		Tag.src		= this.URI.protocol + "://"+ this.SATRServerURL +"/SupportAliveInvite.js?oid=" + this.OrgID +"&lc="+ this.Lang +"&uid=" + this.UID +"&dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();
		
		if(document.getElementById(this.ScriptContainer)){
		
			document.getElementById(this.ScriptContainer).appendChild(Tag);
		}
		else{
		
			document.getElementsByTagName("head")[0].appendChild(Tag);
		}
	}
};

SA.prototype.CloseInvite = function(){

	document.body.removeChild(this.InviteWindow);
	this.InviteWindow	= null;	
};

SA.prototype.RejectInvite = function(){

	var	Dt		= new Date();
	var	Img		= new Image();
	Img.src		= this.URI.protocol + "://" + this.SATRServerURL +"/"+ this.SATRImg +"?type=rj&oid="+ this.OrgID +"&uid="+ this.UID +"&dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();

	this.CloseInvite();
};

SA.prototype.AcceptInvite = function(OperatorMail){

	var	Dt		= new Date();
	var	Img		= new Image();
	Img.src		= this.URI.protocol + "://" + this.SATRServerURL +"/"+ this.SATRImg +"?type=ac&oid="+ this.OrgID +"&uid="+ this.UID +"&dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();
	
	this.StartChat(true, "", null, OperatorMail);
	this.CloseInvite();
};

SA.prototype.ShowInvitation = function(){
	
	this.InviteWindow.style.display		= "";
	this.InviteWindow.style.top			= ((-1 * this.InviteWindow.clientHeight) + document.documentElement.scrollTop).toString() + "px";
	this.InviteWindow.style.left		= (document.documentElement.clientWidth - this.InviteWindow.clientWidth - this.InvitePad).toString() + "px";
		
	SlideElement(this.InviteWindow, "S", this.InviteWindow.clientHeight + this.InvitePad, 10, null);
};

SA.prototype.PinInviteWindow = function(){
	
	if(this.InviteWindow != null && this.InviteWindow.style.display == ""){
	
		this.InviteWindow.style.top		= (document.documentElement.scrollTop + this.InvitePad).toString() + "px";
		this.InviteWindow.style.left	= (document.documentElement.clientWidth - this.InviteWindow.clientWidth - this.InvitePad).toString() + "px";
	}
};

SA.prototype.Switch = function(Img){

	switch(Img.height)
	{
		case 1: // Online
			{
			
				if(document.getElementById("_SAD1")){
			
					this.AddChatButton();
					this.OperatorImg.src	= BaseURL + Online1;
				}
				
				if(document.getElementById("_SAD2")){
			
					this.AddChatButton();
					this.OperatorImg2.src	= BaseURL + Online2;
				}
				
				if(document.getElementById("_SAD3")){
			
					this.AddChatButton();
					this.OperatorImg3.src	= BaseURL + Online3;
				}
				
				break;
			}
		case 2: // Invite
			{			
				this.CreateInvite();
				break;
			}
		case 3: // Offline
			{
				if(document.getElementById("_SAD1")){
			
					this.AddChatButton();
					this.OperatorImg.src	= BaseURL + Offline1;
				}
				
				if(document.getElementById("_SAD2")){
			
					this.AddChatButton();
					this.OperatorImg2.src	= BaseURL + Offline2;
				}
				
				if(document.getElementById("_SAD3")){
			
					this.AddChatButton();
					this.OperatorImg3.src	= BaseURL + Offline3;
				}
				
				break;
			}
	}
};

SA.prototype.AddChatButton = function(){

	if(this.OperatorImg == null){
	
		var	_DptID						= null;
		var	_OpMail						= null;
		var	_this						= this;
		this.OperatorImg				= new Image();
		this.OperatorImg2				= new Image();
		this.OperatorImg3				= new Image();
		this.OperatorImg.style.cursor	= "pointer";
		this.OperatorImg2.style.cursor	= "pointer";
		this.OperatorImg3.style.cursor	= "pointer";
		
		if(document.getElementById("__sa").attributes["DptID"] && document.getElementById("__sa").attributes["DptID"].value != ""){
		
			_DptID	= document.getElementById("__sa").attributes["DptID"].value;
		}
		
		if(document.getElementById("__sa").attributes["OpMail"] && document.getElementById("__sa").attributes["OpMail"].value != ""){
		
			_OpMail	= document.getElementById("__sa").attributes["OpMail"].value;
		}
		
		if(window.attachEvent){
		
			this.OperatorImg.attachEvent("onclick", function(){_this.StartChat(true, "", _DptID, _OpMail);});
			this.OperatorImg2.attachEvent("onclick", function(){_this.StartChat(true, "", _DptID, _OpMail);});
			this.OperatorImg3.attachEvent("onclick", function(){_this.StartChat(true, "", _DptID, _OpMail);});
		}
		else{
		
			this.OperatorImg.addEventListener("click", function(){_this.StartChat(true, "", _DptID, _OpMail);}, false);
			this.OperatorImg2.addEventListener("click", function(){_this.StartChat(true, "", _DptID, _OpMail);}, false);
			this.OperatorImg3.addEventListener("click", function(){_this.StartChat(true, "", _DptID, _OpMail);}, false);
		}
		
		if(document.getElementById("_SAD1")){
		
			document.getElementById("_SAD1").appendChild(this.OperatorImg);
		}
		
		if(document.getElementById("_SAD2")){
		
			document.getElementById("_SAD2").appendChild(this.OperatorImg2);
		}
		
		if(document.getElementById("_SAD3")){
		
			document.getElementById("_SAD3").appendChild(this.OperatorImg3);
		}
	}
};

SA.prototype.StartChat = function(IsNewWindow, Subject, DepartmentID, OperatorMail){
		
    var	sLocation	 = "http://" + this.ChatURL + "/Default.aspx?uid=";
		sLocation	+= this.UID;
		sLocation	+= "&orgid=" + this.OrgID;
		sLocation	+= "&os=" + this.OpenSurvey;
			
	if(DepartmentID != null && DepartmentID != ""){
	
		sLocation	+= "&t=1";
	}
	else if(OperatorMail != null && OperatorMail != ""){
	
		sLocation	+= "&t=2";
	}
	else{
	
		sLocation	+= "&t=0";
	}
		
	sLocation	+= "&brsn="	+ navigator.appName;
	sLocation	+= "&brsp="	+ navigator.platform;
	sLocation	+= "&p=" + encodeURIComponent(document.location);
	sLocation	+= "&r=" + encodeURIComponent(document.referrer);
	sLocation	+= "&s=" + PrepEncoded(Subject);
	sLocation	+= "&omsg="	+ encodeURIComponent(this.OfflineMessage);
	sLocation	+= "&l=" + this.Lang;
	
	if(DepartmentID != null && DepartmentID != ""){
	
		sLocation	+= "&dptid="+ DepartmentID;
	}
		
	if(OperatorMail != null && OperatorMail != ""){
	
		sLocation	+= "&opm="+ OperatorMail;
}

    if (this.VisitorParams != null && this.VisitorParams != "") {

        sLocation += "&vp=" + encodeURIComponent(this.VisitorParams);
    }
		
	if(IsNewWindow){
	
		window.open(sLocation, "SupportAliveChat", "height=480, width=640, left=80, top=80, location=no, menubar=no, resizable=yes, scrollbars=no, titlebar=yes, status=yes", true);
	}
	else{
	
		window.location	= sLocation;
	}
};

SA.prototype.EndVisit = function(){
	
	var	Dt		= new Date();
	var	Img		= new Image();
	var	Src		= this.URI.protocol + "://" + this.SATRServerURL +"/"+ this.SATRImg +"?type=ve&oid="+ this.OrgID +"&uid="+ this.UID +"&pid=" + this.PID +"&dat=" + Dt.toUTCString() + "-" + Dt.getMilliseconds();
	
	if(this.ContentGroup != null){
	
		Src	+= "&cg=" + PrepEncoded(this.ContentGroup);
	}
	
	Img.src		= Src;
};

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function PrepEncoded(S){

	return encodeURIComponent(unescape(S));
}

// ===========================================================================================================================================================================================================
// Eski Methodlar
// ===========================================================================================================================================================================================================

function SupportOrg(lng, r, bNewWindow, subject){

	_SA.StartChat(bNewWindow, subject, null, null);
}

function SendInfo2(n, m, cc, un, sPIDs, sPValues){

	_SA.VisitorName		= n;
	_SA.VisitorMail		= m;
	_SA.CustomerCode	= cc;
	_SA.Username		= un;
	
	if(sPIDs && sPIDs != null){
	
		var	IDArr		= sPIDs.split("###");
		var	ValueArr	= sPValues.split("###");
		var	InfoArr		= new Array();
		
		for(var i=0; i<IDArr.length; i++){
			
			InfoArr[InfoArr.length]	= new Array(IDArr[i], ValueArr[i]);
		}
		
		_SA.InfoParams	= InfoArr;
	}
	
	if(!_SA.Tracking){
	
		_SA.SendTrackingRequest("v");
	}
}

function SendInfo3(n, m, cc, un, InfoParams){

	_SA.VisitorName		= n;
	_SA.VisitorMail		= m;
	_SA.CustomerCode	= cc;
	_SA.Username		= un;
	_SA.InfoParams		= InfoParams;
	
	if(!_SA.Tracking){
	
		_SA.SendTrackingRequest("v");
	}
}

function SupportDptWithInfo(DptID, lng, r, bNewWindow, n, m, s, sPIDs, sPValues){

	SendInfo2(n, m, "", "", sPIDs, sPValues);
	SupportDpt(DptID, lng, r, bNewWindow, s);
}

function SupportOrgWithInfo(lng, r, bNewWindow, n, m,cc, un, s, sPIDs, sPValues) {

    SendInfo2(n, m, cc, un, sPIDs, sPValues);
    SupportOrg(lng, r, bNewWindow, s);
}

function SupportDpt(DptID, lng, r, bNewWindow, subject){
	
	_SA.StartChat(bNewWindow, subject, ReplaceText(String(DptID)), null);
}

function SupportOp( sOpMail, lng, r, bNewWindow, subject ){

	_SA.StartChat(bNewWindow, subject, null, ReplaceText(sOpMail));
}

function ReplaceText(t){

    t	= t.split("<").join("$$$k");
    t	= t.split(">").join("$$$b");
    t	= t.split("+").join("$$$p");
  	t	= encodeURIComponent(t);
  	
    return t;
}

var	_SA	= new SA();

if(document.getElementById("__sa")){
	
	if(document.getElementById("__sa").attributes["sr"] && document.getElementById("__sa").attributes["sr"].value == "notrack"){
			
		_SA.Tracking	= false;
	}	
}

if(_SA.Tracking){

	_SA.SendTrackingRequest("v");
}
