var bn_window_param_start = "unb~intuit~[";
var bn_window_param_end = "]~";
var bn_location_href= window.location.href;
var window_name = window.name;

var tag_uri = "http://intuit-ca_support.baynote.net";
var ccode = "ca_support";
var metas = document.getElementsByTagName("meta");
for (var i = 0; i < metas.length; i++) {
    if (metas[i] && (metas[i].name =="language")) {
        if( metas[i].content == "fr" ) {
            tag_uri="http://intuit-ca_support_fr.baynote.net";
            ccode="ca_support_fr";
        }
    }
}

function bn_isNotEmpty(name) {
    return (name != null) && (name != "");
}
function bn_getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}
function bn_setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+ ";path=/" +
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function bn_checkU(action) {
    if(action == "setWU" ) { bn_setWU(); }
    else if (action == "getWU" ) { bn_getWU(); }
}

function bn_setWU(bn_iter) {
    var bn_iter_limit = 5;
    if(typeof(bn_iter) != 'undefined' && bn_iter > bn_iter_limit) { return; }
    var bn_u_val = bn_getCookie('bn_u');
    if(bn_isNotEmpty(bn_u_val)) {
        window.name = bn_window_param_start + bn_u_val + bn_window_param_end;
    } else {
        if(typeof(bn_iter) != 'undefined' && bn_iter > 0)
            setTimeout("bn_setWU("+(bn_iter+1)+");",500);
        else
            setTimeout("bn_setWU("+1+");",500);
    }
}

function bn_getWU() {
    var bn_u_cookieval = bn_getCookie('bn_u');
    var start_idx = bn_window_param_start.length;
    var end_idx = window_name.indexOf(bn_window_param_end);

    if(end_idx > start_idx && end_idx < window_name.length) {
        var bn_u_val = window_name.substring(start_idx, end_idx);
        if(bn_isNotEmpty(bn_u_val)) {
            bn_setCookie('bn_u',bn_u_val,365*3);
        } else {
            bn_setCookie('bn_u',4,365*3);
        }
    }
}

function bn_showObserver() {
    var article = document.getElementById('article');
    if (article != null) {
        ptag = article.getElementsByTagName("p");
        if (ptag!=null && ptag[0] && ptag[0].textContent)
            baynote_tag.summary = ptag[0].textContent.substring(0,120);
    }

    baynote_tag.server=tag_uri;
    baynote_tag.customerId="intuit";
    baynote_tag.code=ccode;
    baynote_tag.type="baynoteObserver";
    baynote_tag.metaKeys="language";
    baynote_tag.show();
}

/*
If window.name has been initialized with the baynote pattern, then call getWU to set the bn_u cookie.
Otherwise, call bn_showObserver(), then bn_setWU()
*/

if (bn_isNotEmpty(window_name) && window_name.indexOf(bn_window_param_start) == 0) {
    //alert("window.name had been set");
    bn_getWU();
    bn_showObserver();
} else{
    bn_showObserver();
    bn_setWU();
}