var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
function Get_Cookie() {
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        if (cookie_name == "ext") {
            // window.alert("we found this cookie");
            b_cookie_found = true;
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            return cookie_value;
            break;
        }

        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

function checkDocument(divid) {
    var outputdiv;
    if (divid && document.getElementById(divid)) {
        outputdiv = document.getElementById(divid);
    }
    var url = document.URL;
    var url1 = url.split("/");
    var appUrl = url1[2];
    var instanceName = appUrl.split(".");
    var inStance = instanceName[0];
    var mydomain = "my.chatelaine.com";
    var targetDomain = "";
    if (inStance == "www" || inStance == "my") {
        mydomain = "my.chatelaine.com";
        targetDomain = "www.chatelaine.com";
    }
    if (inStance == "qa") {
        mydomain = mydomain.replace(/qa/, "");
        mydomain = 'qa' + mydomain;
        targetDomain = "qa.chatelaine.com";
    }
    if (inStance == "dev") {
        targetDomain = "dev.chatelaine.com";
    }

    if (inStance == "communities") {
        targetDomain = "www.chatelaine.com/en/community";

    }

    document.write("<!--" + mydomain + ":" + inStance + ":" + targetDomain + "-->");


    var or = "or";
    var val = Get_Cookie();
    var userId = Get_UserID();

    if (val == null) {

        document.write("<a href=http://my.chatelaine.com/uumsweb/login.do?property=100051&target="+ escape(window.location)+">sign in</a>" + " " + or + " <a href='http://" + mydomain + "/uumsweb/displayLiteSignup.do?property=100051&tier=1' title='Register with chatelaine.com to participate in community activities, e.g. forums, comments, upload recipes and meal plans.'>register</a>");

    } else {
        if (val == "") {
            document.write("Hi! " + val + "<br /><a href='http://" + mydomain + "/uumsweb/displayProfile.do?property=100051&tier=2'>" + " " + "profile" + "  |" + "</a><a href='http://" + mydomain + "/uumsweb/logout.jsp?property=100051&target=http://" + targetDomain + "/index.jsp'> sign out</a>");
        } else {
            var x = val;
            var y = x.split("|");
            screenName = y[5];

            if (screenName == "") {
                screenName = y[2] + " " + y[3];
            }

            document.write("Hi, " + screenName + "<br /><a href='http://" + mydomain + "/uumsweb/displayProfile.do?property=100051&tier=2'>profile</a> | <a href='http://" + mydomain + "/uumsweb/logout.jsp?property=100051&target=" + escape(document.location.toString()) + "'> sign out</a>");
        }
    }
}

function removeCookie() {
    if (cookie_name == "NEW_COOKIE1") {
        cookie_name == "";
        cookie_value == "";

    }
}

function Get_UserID() {

    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');

        //window.alert("we found this cookie"+ a_temp_cookie);
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        if (cookie_name == "uid") {
            //window.alert("we found the UID cookie");
            b_cookie_found = true;
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            //window.alert("Returning +"+cookie_value);
            return cookie_value;
            break;
        }

        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

