// Shows a modal window
function showModal(element)
{
    // Scroll to the top
    scroll(0, 0);

    var d = document; 
    var rootElm = (d.documentelement && d.compatMode == 'CSS1Compat') ? d.documentelement : d.body; 
    var vpw = self.innerWidth ? self.innerWidth : rootElm.clientWidth; // viewport width 
    var vph = self.innerHeight ? self.innerHeight : rootElm.clientHeight; // viewport height 
    element.style.position = 'absolute';
    var width = element.style.width.replace('px', '').replace(' ', '').replace('%', '');
    var height = element.style.height.replace('px', '').replace(' ', '').replace('%', '');
    element.style.left = ((vpw -width) / 2) + 'px';  
    // element.style.top = ((vph - height) / 2) + 'px';  
    element.style.top = '40px';
    // element.style.top = (rootElm.scrollTop + (vph - element.height)/2 ) + 'px';
 
    // element.style.top = '40px';
 
 
    // Set the display to block
    element.style.display='block';    
    // Create the black overlay div
    var blackOverlay = document.createElement('div');
    // Set the ID
    blackOverlay.id = '_blackOverlay';
    // Set the class
    blackOverlay.className = 'blackOverlay';
    // Set the display to block
    blackOverlay.style.display='block';
    // Append the black overlay to the body
    document.getElementsByTagName('body')[0].appendChild(blackOverlay);    
}

function hideModal(element)
{
    // Hide the element
    element.style.display = 'none';
    // Delete the black overlay
    document.getElementById('_blackOverlay').parentNode.removeChild(document.getElementById('_blackOverlay'));
}


// Checks to see if a form has been modified
function isFormModified(oForm, validateHidden)
{
	var el, opt, hasDefault, i = 0, j;
	while (el = oForm.elements[i++]) {
		switch (el.type) {
			case 'text' :
            case 'textarea' :
            case 'hidden' :
                if (validateHidden == true)
                {
                    if (!/^\s*$/.test(el.value) && el.value != el.defaultValue)
                    {
                        return true;
                    }
                }
                break;
            case 'checkbox' :
            case 'radio' :
                if (el.checked != el.defaultChecked)
                {
                    return true;
                }
                break;
            case 'select-one' :
            case 'select-multiple' :
                j = 0, hasDefault = false;
                while (opt = el.options[j++])
                if (opt.defaultSelected) hasDefault = true;
                j = hasDefault ? 0 : 1;
                while (opt = el.options[j++]) 
                    if (opt.selected != opt.defaultSelected)
                    {
                        return true;
                    }
                break;
		}
	}
	return false;
}

// Confirms a modified form continue
function confirmModifiedFormContinue(oForm, validateHidden, message)
{
    if (isFormModified(oForm, validateHidden))
    {
        return confirm(message);            
    }
}

// If the form has been modofied- show the message- return continue boolean indicator
function confirmModifiedCurrentFormContinue(validateHidden, message)
{
    return confirmModifiedFormContinue(document.forms[0], validateHidden, message);
}


/* cross browser method to set opacity */
function setOpacity(elementID, level) {
    var element = document.getElementById(elementID);
    element.style.opacity = level;
    element.style.MozOpacity = level;
    element.style.KhtmlOpacity = level;
    element.style.filter = "alpha(opacity=" + (level * 100) + ");";
}

function fadeRotateElementsInContainer(container, fadeDuration, displayDuration) {
    var elements = new Array();

    for (var x = 0; x < container.childNodes.length; x++) {
        if (container.childNodes[x].style != null) {
            elements.push(container.childNodes[x]);
        }
    }

    fadeRotateElements(elements, fadeDuration, displayDuration);
}


// Fade rotates elements
function fadeRotateElements(elements, fadeDuration, displayDuration) {
    // Set the item interval - TOTAL TIME to fade in, display, and fade out
    var itemInterval = (fadeDuration * 2) + displayDuration;

    // Set the common interval - TOTAL TIME for ALL ITEMS to fade in, display, and fade out
    var commonInterval = itemInterval * elements.length;

    // Iterate thru the elements
    for (var x = 0; x < elements.length; x++) {
        // Get a ref to the current element
        var element = elements[x];

        // Hide the element
        element.style.display = 'none';

        // Set the opacity to zero
        setOpacity(element.id, 0);

        // Create the fade in interval command
        var fadeInIntervalCommand = "setInterval(\"beginFadeIn(\'" + element.id + "\', " + fadeDuration + ")\", " + commonInterval + ");";

        // Create the fade out interval command
        var fadeOutIntervalCommand = "setInterval(\"beginFadeOut(\'" + element.id + "\', " + fadeDuration + ")\", " + commonInterval + ");";

        // Set a timeout to begin an interval for fading in
        setTimeout(fadeInIntervalCommand, itemInterval * x);

        // Set a timeout to begin an interval for fading out
        setTimeout(fadeOutIntervalCommand, (itemInterval * x) + (fadeDuration + displayDuration));

        setTimeout('beginFadeIn (\'' + element.id + '\', ' + fadeDuration + ');', itemInterval * x);
        setTimeout('beginFadeOut(\'' + element.id + '\', ' + fadeDuration + ');', itemInterval * x + (fadeDuration + displayDuration));

    }
}

// Begins an element fade in
function beginFadeIn(elementID, fadeDuration) {
    // Get the element
    var element = document.getElementById(elementID);

    // Show the element
    element.style.display = '';

    // Set the stepSize
    var stepSize = 0.05;

    // Fade Interval
    var fadeInterval = stepSize * fadeDuration;

    // Get the step count
    var stepCount = fadeDuration / fadeInterval;

    // var test = 'will need to bump ' + stepSize + ' up ' + stepCount + ' times to get to 1.' ;

    // Set a timeout to fully show the item
    // setTimeout("setOpacity(\"" + elementID + "\", 1)", fadeDuration);

    // Get the fade interval
    var fadeInterval = fadeDuration * stepSize;

    for (x = 1; x <= stepCount; x++) {
        // Set a timeout
        setTimeout("setOpacity(\"" + elementID + "\", " + (x * stepSize) + ")", fadeInterval * x);
    }

}

function beginFadeOut(elementID, fadeDuration) {
    // Set a timeout to hide the item
    setTimeout("document.getElementById(\'" + elementID + "\').style.display=\'none\';", fadeDuration);

    // Get the element
    var element = document.getElementById(elementID);

    // Set the stepSize
    var stepSize = 0.05;

    // Fade Interval
    var fadeInterval = stepSize * fadeDuration;

    // Get the step count
    var stepCount = fadeDuration / fadeInterval;

    // var test = 'will need to bump ' + stepSize + ' up ' + stepCount + ' times to get to 1.' ;

    // Set a timeout to fully show the item
    // setTimeout("setOpacity(\"" + elementID + "\", 1)", fadeDuration);

    // Get the fade interval
    var fadeInterval = fadeDuration * stepSize;
    var counter = 0;
    for (x = stepCount - 1; x >= 0; x--) {
        // Set a timeout
        setTimeout("setOpacity(\"" + elementID + "\", " + (x * stepSize) + ")", fadeInterval * counter);
        counter++;
    }

}

