/*
 * Cross-browser attempt at supporting the regular function,
 * document.getElementById().  That function is a DOM-2 level function,
 * which is something that IE -- even at version 6 -- does not yet fully
 * support.
 */

function get_id(id) {
    if (document.getElementById)
        return document.getElementById(id);
    else
        return document.all[id];        /* probably IE */
}

var ac_b64_enc = {
     0: 'A',  1: 'B',  2: 'C',  3: 'D',  4: 'E',  5: 'F',  6: 'G',  7: 'H',
     8: 'I',  9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P',
    16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X',
    24: 'Y', 25: 'Z', 26: 'a', 27: 'b', 28: 'c', 29: 'd', 30: 'e', 31: 'f',
    32: 'g', 33: 'h', 34: 'i', 35: 'j', 36: 'k', 37: 'l', 38: 'm', 39: 'n',
    40: 'o', 41: 'p', 42: 'q', 43: 'r', 44: 's', 45: 't', 46: 'u', 47: 'v',
    48: 'w', 49: 'x', 50: 'y', 51: 'z', 52: '0', 53: '1', 54: '2', 55: '3',
    56: '4', 57: '5', 58: '6', 59: '7', 60: '8', 61: '9', 62: '-', 63: '!'
};

function ac_b64_elshift(m, i, sh) {
    return (m.charCodeAt(i) << sh) & 63;
}

function ac_b64_ershift(m, i, sh) {
    return (m.charCodeAt(i) >> sh) & 63;
}

// Base-64 encode a string, essentially by taking a 3-character block
// and turning it into a 4-character block using the base-64 alphabet.
// If less than 3 characters exist in the last block, the equal sign is
// used as padding (2 equal signs if only 1 character, 1 equal sign if 2
// characters).

function ac_b64_encode(message) {
    var out = "";
    var buf0;
    var buf1;
    var buf2;
    var buf3;
    var i;

    for (i = 0; i < message.length; i += 3) {
        buf0 = ac_b64_enc[ac_b64_ershift(message, i+0, 2)];
        buf2 = "_";
        buf3 = "_";

        if ((i+1) < message.length)
            buf1 = ac_b64_enc[ac_b64_elshift(message, i+0, 4) | ac_b64_ershift(message, i+1, 4)];
        else
            buf1 = ac_b64_enc[ac_b64_elshift(message, i+0, 4)];

        if ((i+2) < message.length) {
            buf2 = ac_b64_enc[ac_b64_elshift(message, i+1, 2) | ac_b64_ershift(message, i+2, 6)];
            buf3 = ac_b64_enc[ac_b64_elshift(message, i+2, 0)];
        } else if ((i+1) < message.length)
            buf2 = ac_b64_enc[ac_b64_elshift(message, i+1, 2)];

        out += buf0 + buf1 + buf2 + buf3;
    }

    return out;
}

// variables loaded from integration:
//var type = 'text'; // 'image','text' or 'tracking'
var displayPop = 0;
var winXPos = 160;
var winYPos = 90;

if (parseInt(navigator.appVersion) > 3){
	if ((navigator.appName=="Microsoft Internet Explorer")&&document.body) {
		winXPos = (document.body.offsetWidth / 2) - 166;
		winYPos = (document.body.offsetHeight / 2) - 100;
	} else if (navigator.appName=="Netscape") {
		winXPos = (window.innerWidth / 2) - 166;
		winYPos = (window.innerHeight / 2) - 100;
	}
}

var online_text = '<div class=\"frameContainer\" id=\"frameContainer\" tabindex=\"-1\" style=\"z-index:500;border:#989898 2px solid;font-size:1.1em;background-color:#DAD6CB;position:absolute;top:'+winYPos+'px;left:'+winXPos+'px\">   <img title=\"Information\" alt=\"Information\" src=\"/vgnmedia/images/Personal_Finance/cardsCTBanner.jpg\" border=\"0\" />  <div id=\"innerContainer\"><form name=\"chatForm\">  <h2>Need Help?</h2><p id=\"textCopy\">A NAB Cards specialist is online now to assist you with any questions you may have on this page or our range of Credit Card solutions.</p> <a title=\"Click to chat\" href=\"javascript:globalWindowOpen(\'https://www.clicktolivechat.com/index.php?action=chat_select&code=952f0f&dept=5&nopost=1\',\'chat\',530,580,1,0,0,1,1,0,0,0,0)\" onclick=\"javascript:hideChatPop();dcsMultiTrack(\'DCS.dcsuri\',\'Credit cards proactive - chat clicked\',\'DCS.dcssip\',\'nab.com.au\',\'WT.ti\',\'Credit cards proactive - chat clicked\');\"><img title=\"Chat Online\" alt=\"Chat online\" src=\"/vgnmedia/images/Personal_Finance/chat.gif\" border=\"0\" /></a>     <a title=\"No thanks\" href=\"javascript:void(0)\" onclick=\"javascript:setChatCookie();dcsMultiTrack(\'DCS.dcsuri\',\'Credit cards proactive - no thank you\',\'DCS.dcssip\',\'nab.com.au\',\'WT.ti\',\'Click to chat - no thankyou\');\"><img title=\"No thanks\" alt=\"No thanks\" src=\"/vgnmedia/downld/noThankYou.gif\" border=\"0\" /></a><br />   <input id=\"optOutBox\" type=\"checkbox\" value=\"true\" name=\"optOutBox\" />   <label for=\"optOutBox\">Don\'t ask me again</label>  </form> </div> '; 
var offline_text = ''; 

var href = "";
var traffic_id = 0;

var do_not_disturb = 0;
var operators_are_online = 0;
var browser_info_img = new Image();


var do_auto_invite = '0';
var dept  = '5';
var code  = '952f0f';
var type = 'text';
var plink = 'https://www.clicktolivechat.com';
var details = '19';
var initial_dept = '5';
var traffic_id = '3';
var invite_href = plink + "/index.php?action=chat_select&code=952f0f&dept=5&nopost=1";
var default_name = '';
var default_question = '';
var online_offline_interval = '30';
var invite_interval = '10000';
var opt_nopost = '1';

//http://clicktolivechat.com/index.php?action=chat_select&code=952f0f&dept=5&nopost=1

var styles = new Array();

styles[1] = new Array();
styles[1]['accept_isimage'] = 1;
styles[1]['reject_isimage'] = 1;
styles[1]['hpixels'] = '150';
styles[1]['vpixels'] = '150';
styles[1]['hposition'] = 'left';
styles[1]['vposition'] = 'top';

var online_offline_img = new Image();
online_offline_img.onload = function() {
	var cookieString = document.cookie;
	if ((cookieString.indexOf("hlChat") == -1) && (displayPop == 0)) {
		if (online_offline_img.width == '1') { // 1 = online, 2 = offline
			operators_are_online = 1;
			dcsMultiTrack('DCS.dcsuri','Click to chat Credit cards proactive - displayed','DCS.dcssip','nab.com.au','WT.ti','Credit cards proactive - displayed');
			get_id('online_offline').innerHTML = online_text;        
		} else {
			operators_are_online = 0;
			get_id('online_offline').innerHTML = offline_text;
		}
	}
};

var invitation_img = new Image();
invitation_img.onload = function() { 
    if (invitation_img.width == '1') { // 1 = yes, 2 = no, 3 = do not disturb (visitor is in chat)
        do_not_disturb = 0; 
        do_invitation();
    } else if (invitation_img.width == '2') {
        do_not_disturb = 0; 
    } else if (invitation_img.width == '3') {
        do_not_disturb = 1;
    }
};

browser_info_img.src = plink + '/image_utility.php?mode=browser_info&details_id=' + details + '&width='+screen.width +'&height='+screen.height+'&page='+escape(document.location.href)+'&referer='+escape(document.referrer)+'&title='+escape(document.title)+'&dept='+dept;

document.write('<span id="online_offline"></span>');

// This portion below is not our code...

/***********************************************
* Floating Top Bar script- © Dynamic Drive (www.dynamicdrive.com)
* Sliding routine by Roy Whittle (http://www.javascript-fx.com/)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var persistclose=0 //set to 0 or 1. 1 means once the bar is manually closed, it will remain closed for browser session
var startX = 30 //set x offset of bar in pixels
var startY = 5 //set y offset of bar in pixels
var verticalpos="fromtop" //enter "fromtop" or "frombottom"

function iecompattest(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function get_cookie(Name) {
    var search = Name + "=";
    var returnvalue = "";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1)
                end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end));
        }
    }
    return returnvalue;
}

function closebar(){
    if (persistclose)
        document.cookie="remainclosed=1";
    document.getElementById("invite_popup").style.visibility="hidden";
}

function staticbar(){
    barheight=document.getElementById("invite_popup").offsetHeight;
    var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
    var d = document;
    function ml(id){
        var el=d.getElementById(id);
        if (!persistclose || persistclose && ("remainclosed")=="")
            el.style.visibility="visible";
        if(d.layers)el.style=el;
        el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
        el.x = startX;
        if (verticalpos=="fromtop")
            el.y = startY;
        else{
            el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
            el.y -= startY;
        }
        return el;
    }
    window.stayTopLeft=function(){
        if (verticalpos=="fromtop"){
            var pY = ns ? pageYOffset : iecompattest().scrollTop;
            ftlObj.y += (pY + startY - ftlObj.y)/8;
        }
        else{
            var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
            ftlObj.y += (pY - startY - ftlObj.y)/8;
        }
        ftlObj.sP(ftlObj.x, ftlObj.y);
        setTimeout("stayTopLeft()", 10);
    }
    ftlObj = ml("invite_popup");
    stayTopLeft();
}

function get_defaults(use_message, invited) {
    var extra = "";

    if (default_name != '')
        extra = extra + "&name=" + default_name;
    else if (invited)
        extra = extra + "&name=" + ac_b64_encode("Auto-Invite");

    if (default_question != '') {
        if (use_message)
            extra = extra + "&message=" + default_question;
        else
            extra = extra + "&question_box=" + default_question;
    }

    return extra;
}

// The rest of our code continues from here.

function do_invitation() {
    var invite_src = plink + '/image_utility.php?mode=invitation_style_id&anti_cache='+no_cache_integer();
    img_1.src = invite_src + '&digit=1';
    img_2.src = invite_src + '&digit=2';
    img_3.src = invite_src + '&digit=3';
    img_4.src = invite_src + '&digit=4';
    img_5.src = invite_src + '&digit=5';
    img_6.src = invite_src + '&digit=6';
    img_7.src = invite_src + '&digit=7';
    img_8.src = invite_src + '&digit=8';
    img_9.src = invite_src + '&digit=9';
    img_10.src = invite_src + '&digit=10';
    // what actually happens when these images are loaded takes place in the image.onload handler
}
function get_online_offline() {
    online_offline_img.src = plink + '/image_utility.php?mode=online_offline&code=' + code + '&dept=' + dept + '&anti_cache='+no_cache_integer();

    try {
        if (check_back == 1)
            window.setTimeout('get_online_offline()', online_offline_interval * 1000);
    } catch (e) {
        window.setTimeout('get_online_offline()', online_offline_interval * 1000);
    }

}

function check_for_invitation() {
	var cookieString = document.cookie;
	if ((cookieString.indexOf("hlChat") == -1) && (displayPop == 0)) {
		invitation_img.src = plink + '/image_utility.php?mode=invitation_check&traffic_id=' + traffic_id + '&anti_cache='+no_cache_integer();
		window.setTimeout('check_for_invitation()', invite_interval * 1000);
	}
}

function no_cache_integer() {
    var d = new Date();
    return d.getTime();
}
function popup_info(style_id) {
    var acp = get_id('accept_div');
    var rjt = get_id('reject_div');
    var box = get_id('invite_popup');
    
    if (!style_id) {
        try {
            style_id = invite_id;
        } catch (e) {
            style_id = 1;
        }
    } 
    if (styles[style_id] && !do_not_disturb) {
        acp.innerHTML = '';
        if (styles[style_id].accept_isimage == 1) {
            acp.innerHTML = "<img src='" + plink + "/image.php?mood=accept&image=" + style_id + "' alt='Accept' style='cursor:pointer;'>";
        } else {
            acp.innerHTML = styles[style_id].accept_html;
        }
        rjt.innerHTML = '';
        if (styles[style_id].reject_isimage == 1) {
            rjt.innerHTML = "<img src='" + plink + "/image.php?mood=reject&image=" + style_id + "' alt='Reject' style='cursor:pointer;'>";
        } else {
            rjt.innerHTML = styles[style_id].reject_html;
        }
    
        box.style.position = 'absolute';
        
        switch (styles[style_id].hposition) {
            default:        // FALLTHRU
            case 'left':
                startX = parseInt(styles[style_id].hpixels, 10);
                //box.style.left = styles[style_id].hpixels + "px";
                break;
            case 'right':
                startX = parseInt(styles[style_id].hpixels, 10);
                //box.style.right = styles[style_id].hpixels + "px";
                break;
        }
    
        switch (styles[style_id].vposition) {
            default:        // FALLTHRU
            case 'top':
                startY = parseInt(styles[style_id].vpixels, 10);
                //box.style.top = styles[style_id].vpixels;
                break;
            case 'bottom':
                startY = parseInt(styles[style_id].vpixels, 10);
                //box.style.bottom = styles[style_id].vpixels;
                break;
        }
    
        staticbar();
        box.style.display = 'block';
    
        try {
            if (invite_hide > 0)
                window.setTimeout('hide_invite_popup()', invite_hide * 1000);
        } catch (e) {}
    }
}

function hide_invite_popup() {
    get_id("invite_popup").style.display = "none"; 
    get_id("accept_div").innerHTML = ' '; 
    get_id("reject_div").innerHTML = ' ';
}

function auto_invite() {
    if (do_auto_invite == 1) {
        try {
            popup_info(invite_id);
        } catch (e) {
            popup_info(1);
        }
    }
}

// We have to create our (potential) popup-invite div.
var div = document.createElement("div");

div.style.display = 'none';
div.style.margin = '5px';
div.id = 'invite_popup';

var accept_div = document.createElement("div");
var reject_div = document.createElement("div");
var accept_div_img = new Image();

accept_div.id = "accept_div";
reject_div.id = "reject_div";

accept_div.onclick = function() {
    if (invite_href != "")
        window.open(invite_href + get_defaults(true, true), "Chat", "height=625,width=500,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");

    div.style.display = 'none';
    accept_div_img.src = plink + '/image_utility.php?mode=accept_reject_div&traffic_id=' + traffic_id + '&result=1';
}

reject_div.onclick = function() {
    div.style.display = 'none';
    accept_div_img.src = plink + '/image_utility.php?mode=accept_reject_div&traffic_id=' + traffic_id + '&result=0';
    closebar();
}

div.appendChild(accept_div);
div.appendChild(reject_div);

document.write('<div id="acp_int_div"></div>');
get_id("acp_int_div").appendChild(div);

var is_loaded_1 = 0;
var is_loaded_2 = 0;
var is_loaded_3 = 0;
var is_loaded_4 = 0;
var is_loaded_5 = 0;
var is_loaded_6 = 0;
var is_loaded_7 = 0;
var is_loaded_8 = 0;
var is_loaded_9 = 0;
var is_loaded_10 = 0;

var value_1 = 0;
var value_2 = 0;
var value_3 = 0;
var value_4 = 0;
var value_5 = 0;
var value_6 = 0;
var value_7 = 0;
var value_8 = 0;
var value_9 = 0;
var value_10 = 0;

var img_1 = new Image();
var img_2 = new Image();
var img_3 = new Image();
var img_4 = new Image();
var img_5 = new Image();
var img_6 = new Image();
var img_7 = new Image();
var img_8 = new Image();
var img_9 = new Image();
var img_10 = new Image();

img_1.onload = function() { 
    is_loaded_1 = 1;
    if (img_1.width == 10) { value_1 = 0; } else { value_1 = img_1.width; }
    do_event ();
};  
img_2.onload = function() { 
    is_loaded_2 = 1;
    if (img_2.width == 10) { value_2 = 0; } else { value_2 = img_2.width; }
    do_event ();
};  
img_3.onload = function() { 
    is_loaded_3 = 1;
    if (img_3.width == 10) { value_3 = 0; } else { value_3 = img_3.width; }
    do_event ();
};  
img_4.onload = function() { 
    is_loaded_4 = 1;
    if (img_4.width == 10) { value_4 = 0; } else { value_4 = img_4.width; }
    do_event ();
};  
img_5.onload = function() { 
    is_loaded_5 = 1;
    if (img_5.width == 10) { value_5 = 0; } else { value_5 = img_5.width; }
    do_event ();
};  
img_6.onload = function() { 
    is_loaded_6 = 1;
    if (img_6.width == 10) { value_6 = 0; } else { value_6 = img_6.width; }
    do_event ();
};  
img_7.onload = function() { 
    is_loaded_7 = 1;
    if (img_7.width == 10) { value_7 = 0; } else { value_7 = img_7.width; }
    do_event ();
};  
img_8.onload = function() { 
    is_loaded_8 = 1;
    if (img_8.width == 10) { value_8 = 0; } else { value_8 = img_8.width; }
    do_event ();
};  
img_9.onload = function() { 
    is_loaded_9 = 1;
    if (img_9.width == 10) { value_9 = 0; } else { value_9 = img_9.width; }
    do_event ();
};  
img_10.onload = function() { 
    is_loaded_10 = 1;
    if (img_10.width == 10) { value_10 = 0; } else { value_10 = img_10.width; }
    do_event ();
};  

function do_event() {
    if (is_loaded_1 && is_loaded_2 && is_loaded_3 && is_loaded_4 && is_loaded_5 && is_loaded_6 && is_loaded_7 && is_loaded_8 && is_loaded_9 && is_loaded_10) {
        var this_style_id = parseInt(''+value_1+value_2+value_3+value_4+value_5+value_6+value_7+value_8+value_9+value_10, 10);
        popup_info(this_style_id);  
    } 
} 

try {
    if (invite_initiate > 0) {
        window.setTimeout('auto_invite()', invite_initiate * 1000);
    }
} catch (e) { }      // do nothing

function setChatCookie() {
	if (document.chatForm.optOutBox.checked) {
		var expireDate = new Date;
		expireDate.setMonth(expireDate.getMonth()+6);
		document.cookie = "hlChat=optOut; expires=" + expireDate.toGMTString();
	}
	hideChatPop();
}

function hideChatPop() {
	displayPop = 1;
	$(".frameContainer").hide();
}

window.setTimeout('get_online_offline()',online_offline_interval * 1000);
check_for_invitation();