var sCookie = OnCheck();
var uid = escape(sCookie);
var orgid = "1270";
var Lng = "tr";
var omsg_tr = "%c5%9eu+anda+size+yard%c4%b1mc%c4%b1+olam%c4%b1yoruz.%24%24%24kbr%24%24%24bSorular%c4%b1n%c4%b1z+i%c3%a7in+%24%24%24kb%24%24%24b%24%24%24ka+href%3d%22mailto%3agm%40tr.com.tr%22%24%24%24binfo%40yoneticipaneli.com%24%24%24k%2fa%24%24%24b%24%24%24k%2fb%24%24%24b+adresine+e-posta+g%c3%b6nderebilirsiniz.";
var omsg_en = "We+cannot+help+you+at+this+time.%24%24%24kBR%24%24%24bFor+your+questions+please+send+an+e-mail+to+%24%24%24kb%24%24%24b%24%24%24ka+href%3d%22mailto%3agm%40tr.com.tr%22%24%24%24bgm%40tr.com.tr%24%24%24k%2fa%24%24%24b%24%24%24k%2fb%24%24%24b";
var rtype = "";
var rid = "";
CT_DPT = 1;
CT_OP = 2;

var HOST = "www.supportalive.com/SupportAliveClient/";
var HOST1 = "www.supportalive.com/SAT/";
var zIndex = 30000;
var iLeft = 40;
var iTop = 40;
var CampainPage = "About:Blank";
var PID = CreateID();
var isLoadedImage = true;
var HostImg;
var SupportDivName = "SupDiv1";
var bDivVisible = false;

var m_bMoveDiv = false;
var m_bClosed = false;
var LeftBorder = 20;
var RightBorder = 0;

var Step = 1;
var Left = 0;
var Top = 0;
var Width = 0;
var Direction = "right";
var hcControlImage;
var ishcControlImageAdded = false;
var brsn = navigator.appName;
var brsp = navigator.platform;
var brsc = navigator.appCodeName;
var brsv = navigator.appVersion;
var p = document.URL;

function AcceptInvite() {
    SupportOp('', Lng, document.referrer, true, '');
    HideDiv(SupportDivName);
}

function CheckImages() {
    var w = HostImg.width;
    var h = HostImg.height;
    if (w == 0) {
        return;
    }
    HandleWidthHeight(w, h);
}

function CheckTrackingLoop() {
    if (isLoadedImage) {
        if (document.body) {
            if (!ishcControlImageAdded) {
                hcControlImage = new Image();
                hcControlImage.style.visibility = "hidden";
                ishcControlImageAdded = true;
            }
            SendTrackingRequest();
            setTimeout('CheckTrackingLoop()', 10000);
        }
        else {
            setTimeout('CheckTrackingLoop()', 1000);
        }
    }
}

function CreateDiv() {
    if (!document.getElementById(SupportDivName)) {
        var innerHTML = "";
        innerHTML += "<DIV  ID='" + SupportDivName + "' STYLE='Cursor:Hand;DISPLAY:Block; POSITION:Absolute; LEFT:" + iLeft + "px; TOP:" + iTop + "px; HEIGHT:103px; WIDTH:253px;z-index:" + zIndex + "'>";
        innerHTML += "<TABLE ID='" + SupportDivName + "_tbl' WIDTH='250' HEIGHT='100' BORDER='0' CELLPADDING='0' CELLSPACING='0' BACKGROUND='" + HOST1 + "InviteImg.aspx?orgid=" + orgid + "&";
        if (Lng == "tr") innerHTML += "lng=tr'>";
        else innerHTML += "lng=en'>";

        innerHTML += "<TR>";
        innerHTML += "<TD"
        innerHTML += " onclick='AcceptInvite()'";
        innerHTML += " onmouseover='StopDiv()'"
        innerHTML += " onmouseout='ShowDiv(true)'"
        innerHTML += ">&nbsp;"
        innerHTML += "</TD>"
        innerHTML += "<TD WIDTH='20' HEIGHT='18' ALIGN='CENTER' VALIGN='BOTTOM'"
        innerHTML += " onmouseover='StopDiv()'"
        innerHTML += " onmouseout='ShowDiv(true)'"
        innerHTML += " onClick='RejectInvite()'>"
        innerHTML += "<IMG STYLE='CURSOR:Hand;' SRC='" + HOST1 + "images/Close.jpg' WIDTH='15' HEIGHT='15'"
        innerHTML += " onload='OnInviteDivLoad(\"" + SupportDivName + "\")'>"
        innerHTML += "</TD>";
        innerHTML += "</TR>";

        innerHTML += "<TR>";
        innerHTML += "<TD"
        innerHTML += " onclick='AcceptInvite()'";
        innerHTML += " onmouseover='StopDiv()'"
        innerHTML += " onmouseout='ShowDiv(true)'"
        innerHTML += ">&nbsp;"
        innerHTML += "</TD>"
        innerHTML += "<TD"
        innerHTML += " onclick='AcceptInvite()'";
        innerHTML += " onmouseover='StopDiv()'"
        innerHTML += " onmouseout='ShowDiv(true)'"
        innerHTML += ">&nbsp;"
        innerHTML += "</TD>"
        innerHTML += "</TR>";

        innerHTML += "</TABLE>";
        innerHTML += "</DIV>";
        //alert( innerHTML );
        var inviteDiv = document.createElement('DIV');
        inviteDiv.innerHTML = innerHTML;
        document.body.appendChild(inviteDiv);
        if (!m_bClosed) {
            ShowDiv(true);
        }
    }
    else {
        if (!m_bClosed && !bDivVisible) {
            ShowDiv(true);
        }
    }
}

function CreateID() {
    var now = new Date();

    var strYear = now.getFullYear() + "";
    if (strYear.length == 3) {
        strYear = "0" + strYear;
    }
    var strMonth = now.getMonth() + 1;
    var strDay = now.getDate();
    var strHours = now.getHours();
    var strMinutes = now.getMinutes();
    var strSeconds = now.getSeconds();

    if (parseInt(strMonth) < 10) {
        strMonth = "0" + strMonth
    }
    if (parseInt(strDay) < 10) {
        strDay = "0" + strDay
    }
    if (parseInt(strHours) < 10) {
        strHours = "0" + strHours
    }
    if (parseInt(strMinutes) < 10) {
        strMinutes = "0" + strMinutes
    }
    if (parseInt(strSeconds) < 10) {
        strSeconds = "0" + strSeconds
    }

    var s = "";
    for (i = 0; i < 16; i++) {
        s += String.fromCharCode(65 + Math.round(Math.random() * 25));
    }

    s += "" + strYear + strMonth + strDay + strHours + strMinutes + strSeconds;
    return s;
}

function Delay(type) {
    var innerHTML = "";
    innerHTML += "<DIV  ID='Delay' STYLE='DISPLAY:None; "
    innerHTML += "POSITION:Absolute; LEFT:0px; TOP:0px; "
    innerHTML += "HEIGHT:0px; WIDTH:0px; z-index:0'>";
    innerHTML += "<table>";
    var ctr = 1000;
    if (type == "1") {
        ctr = 3000;
    }
    for (var i = 0; i < ctr; i++) {
        innerHTML += "<tr><td>&nbsp;</td></tr>";
    }
    innerHTML += "</table></DIV>";
    document.body.innerHTML = innerHTML + document.body.innerHTML;
}

var tempW = -1;
var tempH = -1;
function HandleWidthHeight(w, h) {
    if ((tempH == h) && (tempW == w)) {
        return;
    }
    hclastH = h;
    hclastW = w;

    if (w == 10) {
        CreateDiv();
    }
    else {
        m_bClosed = false;
    }

    if (w == 2 && h == 1) {
        var simg = document.getElementById("ost");
        if (simg) {
            simg.src = HOST1 + "images/Organizations/" + orgid + "/" + Lng + "/Online.gif";
        }
    }
    else if (w == 3 && h == 1) {
        var simg = document.getElementById("ost");
        if (simg) {
            simg.src = HOST1 + "images/Organizations/" + orgid + "/" + Lng + "/Offline.gif";
        }
    }
    else if (w == 1) {
        window.open(CampainPage + "?id=" + h);
    }
    isLoadedImage = true;
}

function HideDiv() {
    document.getElementById(SupportDivName).style.display = "None";
    StopDiv();
    bDivVisible = false;
    m_bClosed = true;
}

function LoadImage() {
    var CookieName = 'SupportAlive';
    var cookies = document.cookie;
    //alert(cookies.indexOf(CookieName));
    if (cookies.indexOf(CookieName) != -1) {
        var startpos = cookies.indexOf(CookieName) + CookieName.length + 1;
        var endpos = cookies.indexOf(';', startpos);

        if (endpos == -1) {
            endpos = cookies.length;
        }
        return unescape(cookies.substring(startpos, endpos));
    }
    else {
        return false;
    }
}

function MoveDivToLeft() {
    Direction = "left";
    if (m_bMoveDiv) {
        if (Left > LeftBorder) {
            document.getElementById(SupportDivName).style.left = Left - Step + "px";
            Left = Left - Step;
            setTimeout("MoveDivToLeft()", 50);
        }
        else {
            MoveDivToRight();
        }
    }
}
function MoveDivToRight() {
    Direction = "right";
    if (m_bMoveDiv) {
        if (Left < (document.body.offsetWidth - Width - 40)) {
            document.getElementById(SupportDivName).style.left = Left + Step + "px";
            Left = Left + Step;
            setTimeout("MoveDivToRight()", 50);
        }
        else {
            MoveDivToLeft();
        }
    }
}
function OnCheck() {
    var MyCookie = LoadImage();

    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) {
            isDefinedCookie = true;
            return MyCookie.substring(0, 30);
        }
    }

    MyCookie = CreateID();
    setCookie('SupportAlive=' + MyCookie);
    isDefinedCookie = true;
    return MyCookie;
}

function OnInviteDivLoad() {}

function RejectInvite() {
    var now = new Date();
    var img = new Image();
    var link = HOST1 + 'RejectInvite.aspx?&oid=' + orgid + '&uid=' + uid + '&nowtime=' + escape(now.getTime());
    img.src = link;
    HideDiv(SupportDivName);
}

function ReplaceText(t) {
    t = t.split("<").join("$$$k");
    t = t.split(">").join("$$$b");
    t = t.split("+").join("$$$p");
    t = encodeURIComponentNew(t);
    return t;
}

function SendChatRequest(lng, r, bNewWindow, subject, type, sExtraParams) {
    var p = document.location + "";
    if (r.length <= 0) {
        r = document.referrer;
    }

    var omsg;
    if (lng == "tr") {
        omsg = omsg_tr;
    }
    else {
        omsg = omsg_en;
    }

    var sLocation = HOST + "Default.aspx?uid=" + uid + "&orgid=" + orgid + "&t=" + type + "&brsn=" + navigator.appName + "&brsp=" + navigator.platform + "&p=" + p.split("?")[0].split("#")[0] + "&r=" + r.split("?")[0].split("#")[0] + "&s=" + ReplaceText(subject) + "&omsg=" + omsg + "&l=" + lng;

    if (sExtraParams.length > 0) {
        sLocation += sExtraParams;
    }

    if (bNewWindow) {
        window.open(
        sLocation, "SupportAliveChat", "height=480, width=465, left=80, top=80, location=no, menubar=no, resizable=yes, scrollbars=no, titlebar=yes, status=yes", true);
    }
    else {
        window.location = sLocation;
    }
}

function SendEndVisit(type) {
    var xImg = document.createElement('IMG');
    xImg.style.visibility = "hidden";
    document.body.appendChild(xImg);

    var now = new Date();
    var link = HOST1 + 'VisitEnd.aspx?oid=' + orgid + '&uid=' + uid + '&e=' + type + '&pid=' + PID + '&nowtime=' + escape(now.getTime());

    xImg.src = link;
    Delay();
}

function SendInfo(s) {
    var now = new Date();
    var img = new Image();
    var sLocation = HOST + "Info.aspx?uid=" + ReplaceText(uid) + "&orgid=" + orgid + s;
    img.src = sLocation + "&nt=" + now.getTime();
}

function SendInfo2(n, m, cc, un, sPIDs, sPValues) {
    var now = new Date();
    var img = new Image();
    var sLocation = HOST + "Info.aspx?uid=" + ReplaceText(uid) + "&orgid=" + orgid + "&n=" + ReplaceText(n) + "&m=" + ReplaceText(m) + "&cc=" + ReplaceText(cc) + "&un=" + ReplaceText(un)

    if (sPIDs.length > 0 && sPValues.length > 0) {
        var arrIDs = sPIDs.split('###');
        var arrValues = sPValues.split('###');

        if (arrIDs.length == arrValues.length) {
            for (i = 0; i < arrIDs.length; i++) {
                sLocation += "&pid_" + i + "=";
                sLocation += arrIDs[i];
                sLocation += "&pv_" + i + "=";
                sLocation += ReplaceText(arrValues[i]);
            }
        }
    }
    img.src = sLocation + "&nt=" + now.getTime();
}

function SendTrackingRequest() {
    var now = new Date();
    isLoadedImage = false;
    HostImg = new Image();
    HostImg.onload = CheckImages;
    var link = HOST1 + 'WebForm1.aspx?';
    link += 'uid=' + uid
    link += '&brsn=' + escape(navigator.appName)
    link += '&brsp=' + escape(navigator.platform)
    link += '&p=' + escape(document.location)
    link += '&pid=' + PID
    link += '&r=' + escape(document.referrer)
    link += '&org_id=' + orgid
    if (rtype.length > 0 && rid.length > 0) {
        link += '&t=' + rtype;
        link += '&rid=' + rid;
    }
    link += '&nowtime=' + now.getTime();
    HostImg.src = link;
}

function setCookie(txt) {
    today = new Date;
    today.setDate(today.getDate() + 365);
    document.cookie = txt + ';path=/;expires=' + today.toGMTString();
}

function SA_SetInvLeftTop(iL, iT) {
    iLeft = iL;
    iTop = iT;
    alert(iLeft + " - " + iTop);
}

function ShowDiv(bMoveDiv) {
    if (!m_bClosed) {
        m_bMoveDiv = bMoveDiv;
        document.getElementById(SupportDivName).style.display = "Block";
        if (bMoveDiv) {
            arrLeft = document.getElementById(SupportDivName).style.left.split("px");
            arrWidth = document.getElementById(SupportDivName).style.width.split("px");
            Left = Number(arrLeft[0]);
            Width = Number(arrWidth[0]);
            RightBorder = (document.body.offsetWidth - Width - 40);
            if (Direction == "right") MoveDivToRight();
            else MoveDivToLeft();
        }
        bDivVisible = true;
    }
}

function StopDiv() {
    m_bMoveDiv = false;
}

function SupportDpt(DptID, lng, r, bNewWindow, subject) {
    var sExtraParams = "&dptid=" + ReplaceText(String(DptID));
    SendChatRequest(lng, r, bNewWindow, subject, '1', sExtraParams);
}

function SupportDptWithInfo(DptID, lng, r, bNewWindow, n, m, s, sPIDs, sPValues) {
    var now = new Date();
    var img = new Image();
    var sLocation = HOST + "Info.aspx?uid=" + ReplaceText(uid) + "&orgid=" + orgid + "&n=" + ReplaceText(n) + "&m=" + ReplaceText(m) + "&s=" + ReplaceText(s);

    if (sPIDs.length > 0 && sPValues.length > 0) {
        var arrIDs = sPIDs.split('###');
        var arrValues = sPValues.split('###');

        if (arrIDs.length == arrValues.length) {
            for (i = 0; i < arrIDs.length; i++) {
                sLocation += "&pid_" + i + "=";
                sLocation += arrIDs[i];
                sLocation += "&pv_" + i + "=";
                sLocation += ReplaceText(arrValues[i]);
            }
        }
    }
    img.src = sLocation + "&nt=" + now.getTime();
    img.onLoad = SendInfoCompleted_Dpt(DptID, s, bNewWindow);
}

function SendInfoCompleted_Dpt(DptID, s, bNewWindow) {
    SupportDpt(DptID, 'tr', document.referrer, bNewWindow, s);
}

function SupportOrg(lng, r, bNewWindow, subject) {
    SendChatRequest(lng, r, bNewWindow, subject, '0', '');
}

function SupportOrgWithInfo(lng, r, bNewWindow, n, m, s, sPIDs, sPValues) {
    var now = new Date();
    var img = new Image();
    var sLocation = HOST + "Info.aspx?uid=" + ReplaceText(uid) + "&orgid=" + orgid + "&n=" + ReplaceText(n) + "&m=" + ReplaceText(m) + "&s=" + ReplaceText(s);

    if (sPIDs.length > 0 && sPValues.length > 0) {
        var arrIDs = sPIDs.split('###');
        var arrValues = sPValues.split('###');

        if (arrIDs.length == arrValues.length) {
            for (i = 0; i < arrIDs.length; i++) {
                sLocation += "&pid_" + i + "=";
                sLocation += arrIDs[i];
                sLocation += "&pv_" + i + "=";
                sLocation += ReplaceText(arrValues[i]);
            }
        }
    }
    img.src = sLocation + "&nt=" + now.getTime();
    img.onLoad = SendInfoCompleted_Org(s, bNewWindow);
}

function SendInfoCompleted_Org(s, bNewWindow) {
    SupportOrg('tr', document.referrer, bNewWindow, s);
}

function SupportOp(sOpMail, lng, r, bNewWindow, subject) {
    var sExtraParams = "&opm=" + ReplaceText(sOpMail);
    SendChatRequest(lng, r, bNewWindow, subject, '2', sExtraParams);
}

function Unload() {
    SendEndVisit("0");
}

function utf8(wide) {
    var c, s;
    var enc = "";
    var i = 0;
    while (i < wide.length) {
        c = wide.charCodeAt(i++);
        // handle UTF-16 surrogates
        if (c >= 0xDC00 && c < 0xE000) continue;
        if (c >= 0xD800 && c < 0xDC00) {
            if (i >= wide.length) continue;
            s = wide.charCodeAt(i++);
            if (s < 0xDC00 || c >= 0xDE00) continue;
            c = ((c - 0xD800) << 10) + (s - 0xDC00) + 0x10000;
        }
        // output value
        if (c < 0x80) enc += String.fromCharCode(c);
        else if (c < 0x800) enc += String.fromCharCode(0xC0 + (c >> 6), 0x80 + (c & 0x3F));
        else if (c < 0x10000) enc += String.fromCharCode(0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
        else enc += String.fromCharCode(0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
    }
    return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
    return hexchars.charAt(n >> 4) + hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
    var s = utf8(s);
    var c;
    var enc = "";
    for (var i = 0; i < s.length; i++) {
        if (okURIchars.indexOf(s.charAt(i)) == -1) {
            enc += "%" + toHex(s.charCodeAt(i));
        }
        else {
            enc += s.charAt(i);
        }
    }
    return enc;
}

function DecodeUTF(s) {
    return decodeURIComponent(s);
}

if (window.location.href.indexOf("https") >= 0) {
    HOST = "https://" + HOST;
    HOST1 = "https://" + HOST1;
}
else {
    HOST = "http://" + HOST;
    HOST1 = "http://" + HOST1;
}
//window.addEventListener("onunload", Unload, false);
window.onunload = Unload;
//window.addEventListener("onload", CheckTrackingLoop(), false);
window.onload = CheckTrackingLoop;