﻿function $(id) {
    return document.getElementById(id);
}

function setHeaderColor() {
    var browser = navigator.appName;

    if (browser == "Microsoft Internet Explorer") {
        var div = $("cityu_header");
        div.style.backgroundColor = '#660918';
    }
    else {
        var div = $("cityu_header");
        div.style.backgroundColor = '#720918';
    }
}

function swapImage(button, src) {
    var button = $(button);
    button.src = src;
}

function ShowModalPopup(height, width) {
    
    var modalStyle = $('modalWindow').style;
    modalStyle.height = height + 'px';
    modalStyle.width = width + 'px';
        
    $('modalWindow').style.display = $('modalBackground').style.display = 'block';

//    // special < IE7 -only processing for windowed elements, like select	
//    if (window.XMLHttpRequest == null) {
//        var type = $('hideType').value;

//        if (type == 'iframe')
//            $('modalIframe').style.display = 'block';
//        if (type == 'replace')
//            ReplaceSelectsWithSpans();
//    }

    // call once to center everything
    OnWindowResize();

    if (window.attachEvent)
        window.attachEvent('onresize', OnWindowResize);
    else if (window.addEventListener)
        window.addEventListener('resize', OnWindowResize, false);
    else
        window.onresize = OnWindowResize;

    // we won't bother with using javascript in CSS to take care
    //   keeping the window centered
    if (document.all)
        document.documentElement.onscroll = OnWindowResize;
}

function OnWindowResize() {
    // we only need to move the dialog based on scroll position if
    //   we're using a browser that doesn't support position: fixed, like < IE 7
    var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
    var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
    var div = $('modalWindow');

    div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
    div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}

function HideModalPopup() {
    $('modalWindow').style.display = $('modalBackground').style.display = 'none';

//    // special IE-only processing for windowed elements, like select	
//    if (document.all) {
//        var type = $('hideType').value;

//        if (type == 'iframe')
//            $('modalIframe').style.display = 'none';
//        if (type == 'replace')
//            RemoveSelectSpans();
//    }

    if (window.detachEvent)
        window.detachEvent('onresize', OnWindowResize);
    else if (window.removeEventListener)
        window.removeEventListener('resize', OnWindowResize, false);
    else
        window.onresize = null;
}

/* utiltiy functions */

function GetWindowWidth() {
    var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;

    return width;
}

function GetWindowHeight() {
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;

    return height;
}

/* These functions deal with IE's retardedness in not allowing divs to 
* cover select elements by replacing the select elements with spans. */

function RemoveSelectSpans() {
    var selects = document.getElementsByTagName('select');

    for (var i = 0; i < selects.length; i++) {
        var select = selects[i];

        if (select.clientWidth == 0 || select.clientHeight == 0 ||
			select.nextSibling == null || select.nextSibling.className != 'selectReplacement') {
            continue;
        }

        select.parentNode.removeChild(select.nextSibling);
        select.style.display = select.cachedDisplay;
    }
}

function ReplaceSelectsWithSpans() {
    var selects = document.getElementsByTagName('select');

    for (var i = 0; i < selects.length; i++) {
        var select = selects[i];

        if (select.clientWidth == 0 || select.clientHeight == 0 ||
			select.nextSibling == null || select.nextSibling.className == 'selectReplacement') {
            continue;
        }

        var span = document.createElement('span');

        // this would be "- 3", but for that appears to shift the block that contains the span 
        //   one pixel down; instead we tolerate the span being 1px shorter than the select
        span.style.height = (select.clientHeight - 4) + 'px';
        span.style.width = (select.clientWidth - 6) + 'px';
        span.style.display = 'inline-block';
        span.style.border = '1px solid rgb(200, 210, 230)';
        span.style.padding = '1px 0 0 4px';
        span.style.fontFamily = 'Arial';
        span.style.fontSize = 'smaller';
        span.style.position = 'relative';
        span.style.top = '1px';
        span.className = 'selectReplacement';

        span.innerHTML = select.options[select.selectedIndex].innerHTML +
			'<img src="custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';

        select.cachedDisplay = select.style.display;
        select.style.display = 'none';
        select.parentNode.insertBefore(span, select.nextSibling);
    }
}

function preloadImages() {
    var doc = document; if (doc.images) {
    if (!doc.imageArray) doc.imageArray = new Array();
    var i, j = doc.imageArray.length, a = preloadImages.arguments; for (i = 0; i < a.length; i++)
        if (a[i].indexOf("#") != 0) { doc.imageArray[j] = new Image; doc.imageArray[j++].src = a[i]; } 
    }
}

function fixExpandoDiv(id) {
    var browser = navigator.appName
    if (browser == "Microsoft Internet Explorer") {
        var bottomRightDiv = $(id);
        bottomRightDiv.style.marginTop = '0px';
    }
}

function fixIEMargin(id,qty,units) {
    var browser = navigator.appName
    if (browser == "Microsoft Internet Explorer") {
        var list = $(id);
        list.style.marginLeft = qty + units;
    }
}
