// global javascript variables
open_menu = -1;
open_menu_item = -1;
var xmldoc1;
var xmldoc2;
var menus = new Array(0);
var menu_separation = 112;
var menu_separation_1 = 111;
var menu_width = 145;
var menu_left = 0;
var button_top = 115;
var moz = false;
var cursor_text = "cursor:default;";
var logged_in = false;
var pending_close = false;
var pending_open = -1;
var selected_row = -1;
var pane_width = 0;
var pane_height = 0;
var current_page = 0;
var num_pages = 0;
var clicked_param = "";
var menu_bg_color = "ffffff";
var menu_border_color = "#999999";
var font_style = "tahoma";
var font_size = "9pt";
var font_weight = "bold";
var menu_text_colour = "ffffff";
var menu_highlighted_text_colour = "000000";
var menu_pause = 0;

window.onresize = globalResize;
document.body.scroll = "no";

var page_height = document.body.clientHeight;
var page_width = document.body.clientWidth;
var content_height = page_height - 200;
var mid_page = page_width / 2;

var safari = false;
            
if (navigator.appName == "Netscape")
{
    moz = true;
    //cursor_text = "";
}

if (navigator.vendor && navigator.vendor == "Apple Computer, Inc.")
{
    safari = true;
}

// functions
function globalResize()
{
    page_height = document.body.clientHeight;
    page_width = document.body.clientWidth;
    content_height = page_height - 200;
    mid_page = page_width / 2;
            
    var content_div = document.getElementById("content_div");
    
    if (content_div)
    {
        content_div.style.height=content_height;
    }
    
    var this_top = content_height + 160;
    var search_top = this_top;
    
    if (moz)
    {
        search_top += 2;
    }

    var this_div = document.getElementById("quicklinks_image");
    
    if (this_div)
    {
        this_div.style.top = this_top -30 + "px";
        this_div.style.left = mid_page - 400 + "px";
    }
    
    this_div = document.getElementById("quicklinks_search");
    
    if (this_div)
    {
        this_div.style.top = search_top - 26 + "px";
        this_div.style.left = mid_page - 400 + "px";
    }
    
    var x = -120;
    var y = -25;

    for (var i = 0; i < 4; i++)
    {
        var the_div = document.getElementById("quicklinks_" + i);
        if (the_div)
        {
            the_div.style.left = mid_page + x;
            the_div.style.top = search_top + y;
        }
        
        x += 115;
    }
    
    this_div = document.getElementById("copyright_div");
    if (this_div)
    {
        this_div.style.top = this_top + 10;
        this_div.style.left = mid_page - 400;
    }
}

function addMenu(image, colour, highlighted_colour)
{
    var the_menu = new menu(image, colour, highlighted_colour);
    menus.push(the_menu);
}

function addMenuItem(title, url)
{
    var current_menu = menus[menus.length - 1];
    var menu_item = new menuItem(title, url);
    current_menu.items.push(menu_item);
}

function menu(image, colour, highlighted_colour)
{
    this.image = image;
    this.colour = colour;
    this.highlighted_colour = highlighted_colour;
    this.items = new Array(0);
    this.x = 0;
    this.y = 0;
    this.label = null;
}

function menuItem(title, url)
{
    this.title = title;
    this.url = url;
    this.x = 0;
    this.y = 0;
    this.label = null;
}

function displayMenu()
{
    var width = document.body.clientWidth;
    var x = menu_left;
    var y = 0;

    //var menu_div = document.getElementById("menu_div");
    //var menu_x = ((width - 800) / 2) + menu_left
    //menu_div.style.left = menu_x + "px";
    
    for (i = 0; i < menus.length; i++)
    {
        menu = menus[i];
        //var the_image = document.createElement("img");
        //the_image.id = "menu_button_" + i;
        //the_image.src = "/images/" + menu.image + ".gif";
        //the_image.style.position = "absolute";
        //the_image.style.left = x + "px";
        //the_image.style.top = y + "px";
        menu.x = x;
        menu.y = y;
        //var col = "" + i;
        //the_image.onmouseover = function() {scheduleOpenMenu(this.id)};
        //menu_div.appendChild(the_image);
        if (i == 0)
        {
            x += menu_separation_1;
        }
        else
        {
            x += menu_separation;
        }
    }
}

function mouseLeftMenu()
{
    //alert("left menu!");
    // cancel open event
    pending_open = -1;

    // wait a second then close the menu down
    pending_close = true;
    setTimeout("closeMenus()", 250); 
}

function mouseEnterMenu()
{
    // cancel close event
    pending_close = false;
}

function scheduleOpenMenu(the_id)
{
    pending_open = the_id;
    setTimeout("openMenu()", menu_pause);
}

function openMenu()
{
    the_id = pending_open;
    
    if (the_id > -1)
    {
        //var column = the_id.substring(12);
        var column = the_id;
        var menu_div = document.getElementById("menu_div");

        pending_close = false;

        if (column != open_menu)
        {
            if (open_menu > -1)
            {
                var menu1 = menus[open_menu];
                //var the_button1 = document.getElementById("menu_button_" + open_menu);
                var the_span1 = document.getElementById("menu_span_" + open_menu);

                //the_button1.style.backgroundColor = menu1.colour;
                //the_button1.style.color = "ffffff";

                //the_button1.src = "/images/" + menu1.image + ".png";
                //the_span1.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/" + menu1.image + ".png" + "')";

                the_span1.className = "chch_menu";
                
                for (i = 0; i < menu1.items.length; i++)
                {
                    var menu_item = menu1.items[i];
                    menu_div.removeChild(menu_item.label);
                }

                var menu_border = document.getElementById("menu_border");

                if (menu_border)
                {
                    menu_div.removeChild(menu_border);
                }
            }

            var menu2 = menus[column];
            //var the_button2 = document.getElementById("menu_button_" + column);
            var the_span2 = document.getElementById("menu_span_" + column);

            //the_button2.style.backgroundColor = menu2.highlighted_colour;
            //the_button2.style.color = "000000";

            //the_button2.src = "/images/" + menu2.image + "_highlighted.png";
            //the_span2.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/" + menu2.image + "_highlighted.png" + "')";

            the_span2.className = "chch_menu_highlighted";
            
            //var x = menu2.x - 15;
            //var x = menu2.x;
            var x = parseInt(the_span2.style.left + 0);
            var y = menu2.y + button_top;

            // work out height of border
            var h = 20 * menu2.items.length;

            //var table = document.createElement("table");
            //table.style.position = "absolute";
            //table.id = "menu_border";
            //table.style.left = (x - 1) + "px";
            //table.style.top = (y - 1) + "px";
            //table.style.width = (menu_width + 2) + "px";
            //table.style.height = (h + 2) + "px";
            //table.style.border = "1px solid " + menu_border_color;
            //table.style.backgroundColor = menu_bg_color;
            //var tbody = document.createElement("tbody");
            //var row = document.createElement("tr");
            //var cell = document.createElement("td");
            //row.align = "left";
            //row.appendChild(cell);
            //table.appendChild(tbody);
            //tbody.appendChild(row);        
            //menu_div.appendChild(table);

            for (i = 0; i < menu2.items.length; i++)
            {
                var menu_item = menu2.items[i];

                var the_label = document.createElement("label");

                if (!moz)
                {
                    the_label.cursor = "hand";
                }

                the_label.id = "menu_item_" + i;
                //the_label.style.backgroundColor = menu2.colour;
                the_label.style.backgroundColor = "#07aeff";
                the_label.style.height = 20;
                the_label.style.fontFamily = font_style;
                the_label.style.fontSize = font_size;
                the_label.style.fontWeight = font_weight;
                the_label.style.color = menu_text_colour;
                the_label.style.position = "absolute";
                the_label.style.left = x + "px";
                the_label.style.top = y + "px";
                the_label.onmouseover = function() {openMenuItem(this.id, true)};
                the_label.onmouseout = function() {mouseLeftMenu()};
                the_label.onclick = function() {clickMenuItem(this.id)};
                the_label.style.width = the_span2.style.width;
                //the_label.style.width = menu_width + "px";
                the_label.style.textAlign = "left";
                the_label.style.textOverflow = "ellipsis";
                the_label.style.overflow = "hidden";
                the_label.style.whiteSpace = "nowrap";

                the_label.innerHTML = "&nbsp;" + menu_item.title;

                menu_item.label = the_label;
                menu_item.x = x;
                menu_item.y = y;

                menu_div.appendChild(the_label);

                y += 20;
            }

            open_menu = column;
            open_menu_item = -1;
        }
    }
}

function closeMenus()
{
    if (pending_close)
    {
        pending_close = false;
        var menu_div = document.getElementById("menu_div");

        if (open_menu > -1)
        {
            var menu1 = menus[open_menu];
            //var the_button1 = document.getElementById("menu_button_" + open_menu);
            var the_span1 = document.getElementById("menu_span_" + open_menu);

            //the_button1.style.backgroundColor = menu1.colour;
            //the_button1.style.color = "ffffff";

            //the_button1.src = "/images/" + menu1.image + ".png";
            //the_span1.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/images/" + menu1.image + ".png" + "')";

            the_span1.className = "chch_menu";
            
            if (menu_div)
            {
                for (i = 0; i < menu1.items.length; i++)
                {
                    var menu_item = menu1.items[i];
                    menu_div.removeChild(menu_item.label);
                }
            }
        }

        var the_obj1 = document.getElementById("menu_border");

        if (the_obj1)
        {
            menu_div.removeChild(the_obj1);
        }

        open_menu = -1;
    }
}

function openMenuItem(the_id, val)
{
    var column = the_id.substring(10);

    pending_close = false;
    var menu = menus[open_menu];
    var menu_div = document.getElementById("menu_div")

    if (val)
    {
        if (column != open_menu_item)
        {
            if (open_menu_item > -1)
            {            
                var the_item1 = document.getElementById("menu_item_" + open_menu_item);
                //the_item1.style.backgroundColor = menu_bg_color;
                the_item1.style.color = menu_text_colour;
                var menu_item = menu.items[open_menu_item];
            }

            var the_item2 = document.getElementById(the_id);
            //the_item2.style.backgroundColor = menu.colour;
            the_item2.style.color = menu_highlighted_text_colour;
            open_menu_item = column;

            var menu_item = menu.items[column];
        }
    }
    else
    {
        if (column > -1)
        {
            var the_item1 = document.getElementById(the_id);
            the_item1.style.backgroundColor = "eeeeee";
            the_item1.style.color = menu_text_colour;
            var menu_item = menu.items[column];

            if (open_menu_item == column)
            {
                open_menu_item = -1;
            }
        }
    }
}

function clickMenuItem(the_id)
{
    var column = the_id.substring(10);

    pending_close = false;
    var menu = menus[open_menu];
    var menu_item = menu.items[column];
    goto2(menu_item.url);
}

function goto2(url)
{
    document.location = url;
}

function highlightItem(id, the_val)
{
    var the_element = document.getElementById(id);
    var the_element2 = document.getElementById(id + "_div");

    if (the_val)
    {
        the_element.style.backgroundColor = "#6666aa";
        the_element2.style.color = "#ffffff";
    }
    else
    {
        the_element.style.backgroundColor = "#ffffff";
        the_element2.style.color = "#000000";
    }
}

function popCalendar(fieldname)
{
    val = eval("document.form." + fieldname);
    initdate = "";
    
    if (val)
    {
        initdate = val.value;
    }

    newwindow = window.open(
        "/calendar_popup.jsp?field=" + fieldname + "&initdate=" + initdate,
        "ptcalendar",
        "width=310, height=240, scrolling=0, resize=0");

    if (!newwindow.opener)
    {
        newwindow.opener = window;
    }

    newwindow.focus();
}

function popCalendar2(fieldname, init_fieldname)
{
    val = eval("document.form." + init_fieldname);
    initdate = "";
    
    if (val)
    {
        initdate = val.value;
    }

    newwindow = window.open(
        "/calendar_popup.jsp?inc=true&field=" + fieldname + "&initdate=" + initdate,
        "ptcalendar",
        "width=310, height=240, scrolling=0, resize=0");

    if (!newwindow.opener)
    {
        newwindow.opener = window;
    }

    newwindow.focus();
}

function checkSignup()
{
    // check user has entered at least their name and a means of contacting
    var the_form = document.signup_form;
    
    var name = (the_form.name.value.length > 0);
    var email = (the_form.email.value.length > 0);
    var address = (the_form.address1.value.length > 0);
    var mobile = (the_form.mobile.value.length > 0);
    var phone = (the_form.phone.value.length > 0);
    var details = (the_form.details.value.length > 0);
    
    var form_ok = name && (email || address || mobile || phone || details);
    
    if (form_ok)
    {
        return true;
    }
    else
    {
        alert("please enter your name and at least one way in which we can get in touch with you");
        return false;
    }
}


