﻿var header = function() {

    this.renderHtml = function(idDiv) {
        var isSuperUser = 'false';
        var locationId = 1;
        var affiliatelogo = "";
        var homesite = "";
        var objJson = eval('(' + this.readCookie("pencilauth") + ')');
        var strHtml = [];
        if (objJson !== null) {
            isSuperUser = objJson.login.isSuperUser;
            locationId = objJson.login.locationId;
        }

        if ($.query.get('locationId')) {
            locationId = $.query.get('locationId');

        }
        if ($.query.get('homesite')) {
            homesite = $.query.get('homesite');

        }
        if ($.query.get('affiliatelogo')) {
            affiliatelogo = $.query.get('affiliatelogo');

        }





        if (locationId == 1 || isSuperUser == 'true') {
            strHtml[strHtml.length] = '            <div class="site-header">';
        }
        else {
            strHtml[strHtml.length] = '            <div class="site-headerAffiliafte">';
        }
        strHtml[strHtml.length] = '                <div>';
        if (objJson == null) {
            if (locationId == 1) {
                strHtml[strHtml.length] = '                    <div class="site-top-links-guest">';
                strHtml[strHtml.length] = '                        <a href="default.htm">Home</a>&nbsp;|&nbsp;<a href="page.htm?id=51">Contact Us</a>';
                strHtml[strHtml.length] = '                    </div>';
            }
            else {
                strHtml[strHtml.length] = '                    <img src="assets/pencil' + affiliatelogo + '"/> <a href="javascript:" onclick="window.open(\'http://' + homesite + '\');"><img src="assets/' + affiliatelogo + '"/></a>';

            }
        }
        else {
            if (isSuperUser == 'true' || locationId == 1) {
                strHtml[strHtml.length] = '                    <div class="site-top-links-session">';
                strHtml[strHtml.length] = '                        Welcome, <b>' + objJson.login.firstname + ' ' + objJson.login.lastname + '</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                strHtml[strHtml.length] = '                        <a href="javascript:" onclick="logout('+locationId+');" >Log Out</a>&nbsp;|&nbsp;';
            }
            else {
                strHtml[strHtml.length] = '                    <img src="assets/pencil' +objJson.login.Logo + '"/><a href="javascript:" onclick="window.open(\'http://' + objJson.login.homeSite + '\');"><img src="assets/' + objJson.login.Logo + '"/></a>';
                strHtml[strHtml.length] = '                    <span class="site-top-links-sessionAffiliate">';
                strHtml[strHtml.length] = '                        Welcome, <b>' + objJson.login.firstname + ' ' + objJson.login.lastname + '</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                strHtml[strHtml.length] = '                        <a href="javascript:" onclick="logout('+locationId+');" >Log Out</a>&nbsp;|&nbsp;';
            }
            var location = '';
            switch (objJson.login.rcode) {
                case "BP":
                case "G":
                    location = "bpprofile.htm";
                    break;
                case "SP":
                    location = "spprofile.htm";
                    break;
                case "PC":
                    location = "pcprofile.htm";
                    //location = "cprofile.htm?id=" + objJson.login.userid;
                    break;
                case "PA":
                    location = "cprofile.htm?id=" + objJson.login.userid;
                    break;

                case "A":
                case "CM":
                case "S":
                    location = "aprofile.htm";
                    break;


            }

            strHtml[strHtml.length] = '                        <a href=' + location + '>My Profile</a>&nbsp;';
            if (objJson == null || isSuperUser == 'true' || locationId == 1) {
                strHtml[strHtml.length] = '                         |&nbsp;<a href="default.htm">Home</a>&nbsp;|';
                strHtml[strHtml.length] = '                         &nbsp;<a href="page.htm?id=51">Contact Us</a>';
                strHtml[strHtml.length] = '                    </div>';
            }
            else {
                strHtml[strHtml.length] = '                    </span>';
            }
        }
        if (isSuperUser == 'true' || locationId == 1) {
            strHtml[strHtml.length] = '                    <div class="site-top-search">';
            strHtml[strHtml.length] = '                        <b>SEARCH</b>&nbsp;&nbsp;<input type="text" id="topsearchkeyword"  />&nbsp;&nbsp;<img src="assets/go.gif" alt="Go" border="0" style="POSITION: relative; TOP: 2px" onclick="myheader.topKeywordSearch(event);" />';
            strHtml[strHtml.length] = '                    </div>';
        }
        strHtml[strHtml.length] = '                </div>';
        strHtml[strHtml.length] = '          </div>';
        strHtml[strHtml.length] = '<div id="headerDiv" style="position: absolute; top:10px; left:10px; width:250; height: 80; cursor:hand;" onclick=document.location="default.htm"></div>';
        document.getElementById(idDiv).innerHTML = strHtml.join('');

        $("input[id='topsearchkeyword']").keypress(function(e) { myheader.topKeywordSearch(e); });
    };

    this.topKeywordSearch = function(ev) {
        if (ev.which == 13 || ev.type == "click") {
            document.location = "search.htm?keyword=" + document.getElementById("topsearchkeyword").value;
        }
    };

    this.readCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    };

    logout = function(loc) {

        createCookie("pencilauth", "", -1);
        if (loc == 1) {
            document.location = "default.htm";
        }
        else {
            document.location = "LandingPage.htm";

        }
        return null;
    };

    createCookie = function(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    };
};




var myheader = new header();
myheader.renderHtml("header1");