var arrMenu = new Array(), idx=-1, section=0, isParent = 1, isSub = 0, hasChildren = 1, hasNoChildren = 0, i = 0;
var docURL = document.location.pathname, pageName = docURL.substr(docURL.lastIndexOf("/") + 1), currSection = 0;
var itemIsParent = false, parentHasChildren = false, subIsOpen = false, subIdx = 1, currSubIdx = 0;
var isMyCarl = document.URL.toLowerCase().indexOf("my.carl") != -1;

// home
arrMenu[++idx] = [++section, isParent, hasNoChildren, "index.aspx", "Home", "index"];
// about us
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "About Us", ""];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_background.aspx", "Company Background", "about_us_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "mission_statement.aspx", "Mission Statement", "about_us_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "employee_profiles.aspx", "Employee Profiles", "about_us_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "testimonials.aspx", "Testimonials", "about_us_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "preferred_partners.aspx", "Preferred Partners", "about_us_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "premier_clients.aspx", "Premier Clients", "about_us_"];
// the system
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "The System", ""];
arrMenu[++idx] = [section, isSub, hasNoChildren, "system_overview.aspx", "System Overview", "sys_overview_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_management.aspx", "C.A.R.L. Management", "sys_overview_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_administration.aspx", "C.A.R.L. Administration", "sys_overview_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_marketing.aspx", "C.A.R.L. Marketing", "sys_overview_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_accounting.aspx", "C.A.R.L. Accounting", "sys_overview_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_technical_spec.aspx", "C.A.R.L. Technical Spec", "sys_overview_"];
// training and support
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "Training &amp; Support", "training_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_support.aspx", "C.A.R.L. Support", "carl_s"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_installation.aspx", "C.A.R.L. Installation", "carl_i"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "training_solutions.aspx", "Training Solutions", "training_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "telephone_training.aspx", "Telephone Training", "telephone_"];
//arrMenu[++idx] = [section, isSub, hasNoChildren, "regional_training.aspx", "Regional Training", "training_"];
//arrMenu[++idx] = [section, isSub, hasNoChildren, "lettings_update_course.aspx", "Lettings Update Course", "lettings_u"];
// additional services
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "Additional Services", ""];
arrMenu[++idx] = [section, isSub, hasNoChildren, "website_services.aspx", "Website Services", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "secure_landlord_website.aspx", "Secure Landlords' Website", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "sms_text_messaging.aspx", "SMS Text Messaging", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "sage_link.aspx", "Sage Link", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "address_finder.aspx", "Address Finder", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "training_for_professionals.aspx", "Training for Professionals", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "online_backup.aspx", "Remote Backup Service", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_connect.aspx", "Connect Premium", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "las.aspx", "<u>L</u>ettings <u>A</u>dvice <u>S</u>ervice (LAS)", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "advanced_account_training.aspx", "Advanced Recon. Service", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "account_cas.aspx", "C.A.R.L. Accounting Services", "additional_"];
//arrMenu[++idx] = [section, isSub, hasNoChildren, "tenant_trace.aspx", "C.A.R.L. Tenant Trace", "additional_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "http://www.epc-graph.com/", "HIPS Packs / EPC Graphs", ""];
// purchasing and payments
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "Purchasing &amp; Payments", "purchasing_"];
//arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_purchase_agreement.aspx", "C.A.R.L. Purchase Agreement", "purchasing_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "payment_options.aspx", "Payment Options", "purchasing_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_purchase_agreement.aspx", "C.A.R.L. Purchase Agreement", "carl_purchase_agreement.aspx"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "carl_rental_agreement.aspx", "C.A.R.L. Rental Agreement", "carl_rental_agreement.aspx"];
// news
arrMenu[++idx] = [++section, isParent, hasChildren, "javascript:;", "News", "news_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "tdschanges.aspx", "Press Releases", "press_releases_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "promotions_intro.aspx", "Promotions", "promotions_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "newsletter_adduser.aspx", "Newsletters", "newsletter_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "vacancies.aspx", "Vacancies", "vacancies_"];
arrMenu[++idx] = [section, isSub, hasNoChildren, "reallifestories.aspx", "Real-life Stories", "reallife_"];
// client login
arrMenu[++idx] = [++section, isParent, hasNoChildren, "http://www.my.carlcomms.co.uk", "Client Login", "mycarl"];
// contact us
arrMenu[++idx] = [++section, isParent, hasNoChildren, "contact_us.aspx", "Contact Us", "contact_"];
// sitemap
arrMenu[++idx] = [++section, isParent, hasNoChildren, "sitemap.aspx", "Site Map", "sitemap"];

// 1st for loop
for (; i<= idx; i++) {

    if (isMyCarl && arrMenu[i][5] == "mycarl") {

        // we're at my.carlcomms, so the current section is "client login", sort of...
        currSubIdx = i;
        // and the parent section that it belongs to is:
        currSection = arrMenu[i][0];

    } if ((pageName.indexOf(arrMenu[i][5])!= -1 || pageName == arrMenu[i][3]) && 
        pageName != "" && 
        arrMenu[i][5] != "") {

        // we've found a page belonging to a subsection; mark which subsection is current and...
        currSubIdx = i;

        // ... the parent section it belongs to
        currSection = arrMenu[i][0];
    }
} // end of 1st for loop

// 2nd for loop
for (i = 0; i <= idx; i++) {

    itemIsParent = arrMenu[i][1] == isParent;
    parentHasChildren = arrMenu[i][2] == hasChildren;
    subIdx = arrMenu[i][0];
    var preAddr = "";

    if (arrMenu[i][3].indexOf("http:") == -1 && isMyCarl)
        preAddr = "http://www.carlcomms.co.uk/";

    if (itemIsParent) {

        // this is a parent item...

        if (subIsOpen) {

            //...so close a submenu, if one is open, and flag as closed...
            document.write("</div>");
            subIsOpen = false;
        }

        if (i > 0) {

            //...and the last parent item, if one is open (which is true if we're not on the first item!)
            document.write("</div>");
        }

        //now open a new parent div... (which may be selected! -- currSubIdx == i)
        document.write("<div id=\"parent_" + arrMenu[i][0] + "\" class=\"" + 
            (parentHasChildren ? "r_e" : "r") + (currSubIdx == i ? " current" : "") + "\">");
        //...and insert the link to a major(parent) section
        //e.g. <a href="#" id="open_1" class="current"  onclick="onSub(1); return false;">About Us</a>
        document.write("<a href=\"" + preAddr + arrMenu[i][3] + "\" id=\"open_" + subIdx + "\"" +
            (parentHasChildren ? " onclick=\"onSub(" + subIdx + "); return false;\"" : "") +
            " class=\"" + (currSection == subIdx ? "current" : "none") + "\">" + 
            arrMenu[i][4] + "</a>");

    } else { // else is Submenu rathen than itemIsParent

        if (!subIsOpen && i > 0) {

            // open a new submenu, and make sure we set the flag to true!
            document.write("<div class=\"rsub\" id=\"sub_" + subIdx + "\"" +
                (subIdx == currSection ? "" : " style=\"display:none;\"") + 
                ">");
            subIsOpen = true;
        }

        // write the link (if we're in the current subsection, assign it the ID attrib (id="current")...)
        document.write("<a href=\"" + preAddr + arrMenu[i][3] + "\"" + (i == currSubIdx ? "id=\"current\"" : "") + 
            ">" + arrMenu[i][4] + "</a>");

    } // end of if (itemIsParent) section
} // end of 2nd for loop

// close any open sub menus...
if (subIsOpen) document.write("</div>");

// ...and then the last parent menu
document.write("</div>");

// now, for the client's area... but you need to wait for the doc to finish loading...
var grandparent, grandtext, isCurr = false, cls = "";

window.onload = function() {
    grandparent = getElem("cl_nav");
    var parent, pID, child, nURL, arrCousins = new Array();
    idx = 1;
    if (grandparent) {
        arrCousins = loadCousins();
        var cntCous = arrCousins.length - 1;
        //createUncle("installation_service.aspx", "Installation Service");
        grandtext = grandparent.innerHTML;
        parent = getElem("parent_8");
        parent.className = "r_e";
        child = getElem("open_8");

        if (child) {
            child.className = "current";
            child.href = "javascript:onSub(8);";
        }

        pID = "sub_8";

        if (parent) {
             child = createElem(pID, "rsub", "", "");
             parent.appendChild(child);
        }

        parent = getElem(pID);
        for (var i = 0; i <= cntCous; i++)
            checkLink(arrCousins[i][0], arrCousins[i][1], pID, parent, child);
    }
}

function checkLink(nURL, lnkName, pID, parent, child) {
    // checks to see if nURL exists under the existing right panel
    // and if it's the currently loaded document 
    var ok = false;
    isCurr = false;

    if (docURL.indexOf(nURL) != -1) {
        isCurr = true;
        ok = true;
    }
    cls = isCurr ? "current" : pID + "_" + idx;

    if (!ok) {
        // get innerHTML of cl_nav object and search for nURL
        // ok if object contains the URL!
        ok = grandtext.indexOf(nURL) != -1;
    }

    if (ok) {
        child = createElem(cls, "", nURL, lnkName);
        if (child) parent.appendChild(child);
        idx++;
        return true;
    }
}

function createUncle(href, hname) {
    var newListItem = document.createElement("li");
    // each uncle is an <li> with an <a> stemming from newParent (which is the UL!)
    var newParent = grandparent.childNodes[1];
    // uncle should be added to top of list!
    var nID = docURL.indexOf(href) != -1 ? "current" : "";
    var newLink = createElem(nID, "", href, hname);
    newListItem.appendChild(newLink);
    var topListItem = newParent.childNodes[1];
    topListItem.parentNode.insertBefore(newListItem, topListItem);
}

function loadCousins() {
    var newarr = new Array();
    // read list items' a href attrib and innerHTML, insert into array (elems 0,1 respectively) and return array
    //.getAttribute("name");
    var ul = grandparent.childNodes[1], liIdx = 1, li = ul.childNodes[liIdx], c = 0;

    while (li) {
        if (li) {
            var a = li.firstChild, pgName = a.toString(), lpos = pgName.lastIndexOf("/");
            if (lpos > -1) pgName = pgName.substr(lpos + 1);
            newarr[c] = [pgName, a.innerHTML];
            liIdx += 2;
            c++;
        }
        li = ul.childNodes[liIdx];
    }
    return newarr;
}

function createElem(id, cls, href, inTxt) {
    var newElem;
    if (href == "") {
        newElem = document.createElement("div");
    } else {
        newElem = document.createElement("a");
        newElem.setAttribute("href", href);
        newElem.innerHTML = inTxt;
    }
    if (id != "") newElem.setAttribute("id", id);
    if (cls != "") newElem.setAttribute("class", cls);
    return newElem;
}

function getElem(id) {
	var obj;
	if(document.getElementById) {
		obj = document.getElementById(id);
		if (obj)
			return obj;
		else
			return false;
	}
	else {
		obj = document.all[id];
	}
}
