function modelesswin(url,mwidth,mheight){
    if (document.all&&window.print) //if ie5
        eval('window.showModelessDialog(url,"","help:0;resizable:1;dialogWidth:'+mwidth+'px;dialogHeight:'+mheight+'px")')
    else
        eval('window.open(url,"","width='+mwidth+'px,height='+mheight+'px,resizable=1,scrollbars=1")')
}

function displayYear() 
{
    var currentDate = new Date();
    year = currentDate.getYear();
    if ( year < 1900 ) {
        year = year + 1900;
    }
    document.write(year);
}

function getGMTOffset() 
{
    currentDate = new Date();
    offset = currentDate.getTimezoneOffset();
    if ( offset != 0 ) {
        offset = offset / 60;
    }
    return offset;
}


function printPreview() {
    try {    
        window.print();
    } catch (e) {
    
    }
}

//----------
// Toggle enable/disable an input object based on a checkbox.
//----------
function toggleDisable(objCheckbox, objInput) {
    if ( objCheckbox.checked == true ) {
        objInput.disabled = false;
    } else {
        objInput.disabled = true;
    }
}

//
// Set display timestamp using the given date parameters.
//
function displayLocalizedTime( gmt ) {
    document.write(getLocalizedTime(gmt));
}

function getLocalizedTime( gmt, showGMT ) {
    var dt = new Date(gmt);
    var localTZ = getLocalTZ(dt);    
    var hours = dt.getHours(); 
    if (localTZ != "GMT" && hours > 12) hours = hours - 12; 
    if (localTZ != "GMT" && hours == 0) hours = 12; 
    // If adding text before the time, need to pad for 
    // the extra digit in 10, 11 and 12
    if (hours < 10)
        hours = "<span style='visibility:hidden;'>0</span>" + hours;
    var ampm = (dt.getHours() < 12) ? "AM" : "PM";
    var minutes = ((dt.getMinutes() < 10) ? "0" : "") + dt.getMinutes();
    var month = dt.getMonth() + 1;
    //if (month < 10)
    //    month = "<span style='visibility:hidden;'>0</span>" + month;
    var day = ((dt.getDate() < 10) ? "0" : "") + dt.getDate();
    var year = ""+dt.getFullYear();
    year = year.substring(2,4);
    
    var fmtString = "";
    fmtString += "<span style='font-weight:bolder;'>" + hours + ":" + minutes + "</span><span style='font-size:smaller;'><span style='font-size:5px;'>&nbsp;</span>"  + ((localTZ != "GMT")?ampm:"") + "</span>&nbsp;<span style='font-size:smaller;'>" + localTZ + "</span>&nbsp;";
    fmtString += "<span style='font-size:smaller;'>" + month + "/" + day + "/" + year + "</span>";

    // if local tz is not GMT, add the GMT info
    if (showGMT && localTZ != "GMT") {
        dt = getGMTDate(dt);
        fmtString += "&nbsp;(" + dt.getHours() + ":" + ((dt.getMinutes() < 10) ? "0" : "") + dt.getMinutes() + "GMT)";
    }
    
    return fmtString;
}

function getGMTDate(date) {
    var localTZ = getLocalTZ(date);    
    // if local tz is not GMT, convert it
    if (localTZ != "GMT") {
        date = new Date(date.getTime()+(date.getTimezoneOffset()*60*1000)); // adjust the date by the tz offset from gmt
    }
    return date;
} 

//----------
// Get the local timezone string given the date passed in. 
//----------
function getLocalTZ(dt) {
    var tz = dt.toString();
    if ( tz.indexOf("EST") >= 0 || tz.indexOf("Eastern Standard Time") >= 0) {
        return "EST";
    } else if ( tz.indexOf("EDT") >= 0 || tz.indexOf("Eastern Daylight Time") >= 0) {
        return "EDT";
    } else if ( tz.indexOf("CST") >= 0 || tz.indexOf("Central Standard Time") >= 0) {
        return "CST";
    } else if ( tz.indexOf("CDT") >= 0 || tz.indexOf("Central Daylight Time") >= 0) {
        return "CDT";
    } else if ( tz.indexOf("MST") >= 0 || tz.indexOf("Mountain Standard Time") >= 0) {
        return "MST";
    } else if ( tz.indexOf("MDT") >= 0 || tz.indexOf("Mountain Daylight Time") >= 0) {
        return "MDT";
    } else if ( tz.indexOf("PST") >= 0 || tz.indexOf("Pacific Standard Time") >= 0) {
        return "PST";
    } else if ( tz.indexOf("PDT") >= 0  || tz.indexOf("Pacific Daylight Time") >= 0) {
        return "PDT";
    } else {
        return "GMT";
    }
}


//----------
// Get the local daylight savings & timezone string
//----------
function getLocalDSTZ() {

    // get current date
    var dt = new Date();

    // set the current month & day to 7/01 so it is in daylight savings time period 
    dt.setMonth(6); // months are 0-11
    dt.setDate(1);  // days are 1-31
    
    // now return the daylight savings timezone string
    return getLocalTZ(dt);
}


//
// Open new window.
//
function openWindow(url, windowName, options) {
    newWindow = window.open(url,windowName,options);
    newWindow.focus();
    return newWindow;
}

//
// load Loading popup message.
//
function closeLoadingMessage() {

    
    if (document.getElementById) {
        imgStyle = document.getElementById("loading").style
    }
    if (document.all) {
        imgStyle = eval("document.all.loading.style");
    }
    if (document.layers) {
        imgStyle = document.layers["loading"]; 
    }
    imgStyle.visibility = "hidden";
}

function clickTab(itemid) {
    if (isIE) {
        var tabobj = document.getElementById(itemid);
        tabobj.click();
    }
}


//----------
// This function removes the selected items from the list.
//----------
function removeItems(selectedObj) {
    if(selectedObj.selectedIndex != -1) {
    
        var replaceTextArray = new Array();
        var replaceValueArray = new Array();
        remainingIndex = 0;
        for (var i = 0; i < selectedObj.length; i++) {
           // Put everything except the selected into the array
           if ( !selectedObj.options[i].selected ) {
               replaceTextArray[remainingIndex] = selectedObj.options[i].text;
               replaceValueArray[remainingIndex] = selectedObj.options[i].value;
               remainingIndex++;
           }
        }
        
        // Shorten the input list.
        selectedObj.length = replaceValueArray.length;
        // Put the array back into the list
        for (i = 0; i < replaceValueArray.length; i++) {
           selectedObj.options[i].value = replaceValueArray[i];
           selectedObj.options[i].text = replaceTextArray[i];
           selectedObj.options[i].selected = false;
        }
    }
}


//----------
// This function moves an item in the list up or down.
//----------
function moveItem(selectedObj, direction) {
    if(selectedObj.selectedIndex != -1) {
        if(direction < 0) {
            for(i = 0; i < selectedObj.options.length; i++) {
                swapValue = (i == 0 || selectedObj.options[i + direction].selected) ? null : selectedObj.options[i + direction].value;
                swapText = (i == 0 || selectedObj.options[i + direction].selected) ? null : selectedObj.options[i + direction].text;
                if(selectedObj.options[i].selected && swapValue != null && swapText != null) {
                    thisValue = selectedObj.options[i].value;
                    thisText = selectedObj.options[i].text;
                    selectedObj.options[i].value = swapValue;
                    selectedObj.options[i].text = swapText;
                    selectedObj.options[i + direction].value = thisValue;
                    selectedObj.options[i + direction].text = thisText;
                    selectedObj.options[i].selected = false;
                    selectedObj.options[i + direction].selected = true;
                }
            }
        } else {
            for(i = selectedObj.options.length - 1; i >= 0; i--) {
                swapValue = (i == selectedObj.options.length - 1 || selectedObj.options[i + direction].selected) ? null : selectedObj.options[i + direction].value;
                swapText = (i == selectedObj.options.length - 1 || selectedObj.options[i + direction].selected) ? null : selectedObj.options[i + direction].text;
                if(selectedObj.options[i].selected && swapValue != null && swapText != null) {
                    thisValue = selectedObj.options[i].value;
                    thisText = selectedObj.options[i].text;
                    selectedObj.options[i].value = swapValue;
                    selectedObj.options[i].text = swapText;
                    selectedObj.options[i + direction].value = thisValue;
                    selectedObj.options[i + direction].text = thisText;
                    selectedObj.options[i].selected = false;
                    selectedObj.options[i + direction].selected = true;
                }
            }
        }
    }
}


//----------
// This function selects all items in the list.
//----------
function selectAllItems(obj) {
  if (obj.options.length < 0) { 
    return;
  }
  for (var i=0; i<obj.options.length; i++) {
    obj.options[i].selected = true;
  }
}


function findObjectPosition(oTmp){
    var oPosition = { x : 0, y : 0 };
    while (oTmp.offsetParent){
        oPosition.x += oTmp.offsetLeft
        oPosition.y += oTmp.offsetTop
        oTmp = oTmp.offsetParent;
    }
    return oPosition;
}

function switchToImageMap(name){
  
    var imagemap = document.getElementById("imagemap");
    
    imagemap.usemap = name;
}

function displayMaintenanceMsg() {
    alert("This area of the site is down for maintenance.  We apologize for the inconvenience.");
}

var refreshTimer = null;
function startRefreshTimer(numSeconds) {
    // if a refresh timer hasn't already been created
    if (refreshTimer == null) {
        refreshTimer = window.setTimeout('doAutoRefresh();', numSeconds*1000); 
    } 
}


function doAutoRefresh() {
	// if there is an isAutoRefreshOK function defined
	if (window.isAutoRefreshOK) {
		// call it and see if it is ok to autorefresh now
		if (isAutoRefreshOK()) {
		    window.location.reload(true);
		} else {
			refreshTimer = null;
		}
	// else no function defined, assume it is ok
	} else {
	    window.location.reload(true);
	}
}


function appendHTML(target, html)
{
    if ( target.insertAdjacentHTML ) {
        target.insertAdjacentHTML("BeforeEnd", html);
    } else {
        var range = document.createRange();
        range.setStartAfter(target.lastChild);
        var docFrag = range.createContextualFragment(html);
        target.appendChild(docFrag);
    }
}

function executeServerLogic(iframeId, url) {
	// put the name in it's own namespace so it is less likely to conflict with other dom objects
	iframeId = iframeId + "_iframe";
	
    // see if this iframe already exists
    var iframe = document.getElementById(iframeId);
    if (iframe) {
        // use the existing iframe, add a unique query parm to ensure the reload
        var delimiter = (url.indexOf("?")==-1)?"?":"&"; 
        iframe.src = url + delimiter + "ts=" + new Date().getTime();   
    } else {
		// if the document isn't finished loading yet, call back in 1/10th ssecond and check again
		if (document.readyState && document.readyState != "complete") {
			window.setTimeout("executeServerLogic('"+iframeId+"','"+url+"');", 100);
		} else {
	        // create & insert a new iframe
	        var newiframe = '<iframe id="'+iframeId+'" src="'+url+'" frameborder=0 height=0 width=1 marginheight=0 marginwidth=0 scrolling=no></iframe>';
	
			// add the new iframe to the dom
	        appendHTML(document.body, newiframe);
        }
    }     
}

//----------
// This function get's the x/y location for a mouse click event.
//----------
function getXYLocation(event) {
    var location = new Object();
    // if offsetX/Y are defined - just use those
    if (event.offsetX && event.offsetY) {
        location.x = event.offsetX;
        location.y = event.offsetY;
    // else calculate the harder way    
    } else {
        location.x = event.pageX;
        location.y = event.pageY;
        var element = event.target;
        while (element.offsetParent) {
            location.x -= element.offsetLeft;
            location.y -= element.offsetTop;
            element = element.offsetParent;
        };
        if (location.x < 0) location.x = 0;
        if (location.y < 0) location.y = 0;
    }
    
    return location;
}

//---------
// select/deselect all checkboxes
//---------
var isChecked = false;
function toggleChecked(field) 
{
    var i;
    if(!isChecked) {
        for(i=0; i<field.length; i++) {
            field[i].checked = true;
            isChecked = true;
        }
    } else {
        for(i=0; i<field.length; i++) {
            field[i].checked = false;
            isChecked = false;
        }   
    }
}

//---------
// Toggle Visibility of this element
//---------
function toggleVisibility(element) 
{
    if (element) {
        if(element.style.visibility=='visible' || element.style.display!='none') {
            element.style.visibility='hidden'; 
            element.style.display='none';
        } else {
            element.style.visibility='visible';
            element.style.display='block';
        }
    }
}

var messageHiderTimer = null;
function showStatusMessage(messageTxt) {
    var statusMessage = document.getElementById("statusMessage");

    if (statusMessage) {
        if (messageHiderTimer) {
           window.clearTimeout(messageHiderTimer); 
           messageHiderTimer = null;
        }
        
        statusMessage.style.visibility = "hidden";
        statusMessage.innerHTML = messageTxt;
        setOpacity("statusMessageHolder", 75);
        statusMessage.style.visibility = "visible";
        
        // hide the message after a wait
        messageHiderTimer = window.setTimeout('fadeStatusMessage()', 2000);
    } else {
        alert(messageTxt);
    }
}

function fadeStatusMessage() {
	fadeElement("statusMessageHolder", 0, 2000);
}

function hideStatusMessage() {
    var statusMessage = document.getElementById("statusMessage");

    if (statusMessage) {
        if (messageHiderTimer) {
           window.clearTimeout(messageHiderTimer); 
           messageHiderTimer = null;
        }
        
        statusMessage.style.visibility = "hidden";
        statusMessage.innerHTML = "";
        setOpacity("statusMessageHolder", 0);
   }
}


function showProgressMessage(messageTxt, finished, total) {
    var statusMessage = document.getElementById("statusMessage");
    if (statusMessage) {
        statusMessage.style.visibility = "hidden";
        statusMessage.innerHTML = messageTxt + " " + buildProgressBar(finished, total);
        statusMessage.style.visibility = "visible";
        setOpacity("statusMessageHolder", 75);
    } 
}

function fadeProgressMessage() {
	fadeStatusMessage();
}

function hideProgressMessage() {
	hideStatusMessage();
}

function buildProgressBar(finished, total) {
    var progressBar = "<div style='vertical-align:middle;border:1px white solid;padding:1px;height:15px;display:inline;font-size:8px;margin-top:3px;margin-bottom:3px;'>";

    for (var i=0; i<total; i++){
        progressBar = progressBar + "<span style='padding:1px;background-color:"+((i<=finished)?"white":"transparent")+";'>&nbsp;</span>";
    }  
    
    progressBar = progressBar + "</div>";

    return progressBar;    
}

// fade 'id' element from current opacity to 'targetOpacity' in 'millisec' milliseconds
function fadeElement(elementId, opacityTarget, totalMillisec) { 
	var element = document.getElementById(elementId);
	if (element) {
		shiftOpacity(elementId, element.style.opacity*100, opacityTarget, totalMillisec);
    }
} 

// shift the opacity of this element using a timer, not to be called directly - this is for timer
function shiftOpacity(elementId, opacityPrev, opacityTarget, totalMillisec) {
	var element = document.getElementById(elementId);
	
	if (element) { 
		// opacity start & stop
		var opacityStart = Math.round(element.style.opacity * 100); // opacity is stored as percentage
		var opacityEnd = opacityTarget;
		
		// if no one else is adjusting opacity, and we haven't finished with the opacity changes
		if (opacityStart == opacityPrev && opacityStart != opacityEnd) {
			var stepMillisec = 100; // assume 1/10 second per step
		    var opacityChange = Math.abs(opacityStart-opacityEnd);
		    var numSteps = Math.floor(totalMillisec / stepMillisec); 
			var opacityStep = Math.ceil(opacityChange / numSteps);
			var newOpacity = opacityStart;
			
		    // calculate the next opacity
		    if(opacityStart > opacityEnd) { 
		    	newOpacity = (opacityStart-opacityStep>=opacityEnd) ? opacityStart-opacityStep : opacityEnd;	
		    } else if(opacityStart < opacityEnd) { 
		    	newOpacity = (opacityStart+opacityStep<=opacityEnd) ? opacityStart+opacityStep : opacityEnd;	
		    } 
		    
		    // now set the opacity
    		setOpacity(elementId, newOpacity);
    		// set timer to callback for the next adjustment (assume 50 ms overhead)
            setTimeout("shiftOpacity('" + elementId + "'," + newOpacity + "," + opacityTarget + "," + (totalMillisec-stepMillisec) + ");",(stepMillisec-50));  
	    } 
    }
}

//change the opacity for different browsers 
function setOpacity(elementId, opacity) { 
	if (document.getElementById(elementId)) {
	    var elementStyle = document.getElementById(elementId).style 
	    elementStyle.opacity = (opacity / 100) 
	    elementStyle.filter = "alpha(opacity=" + opacity + ")" 
    }
}

// Checks to see if a string is a valid zip code
function isValidZip(text) {
  var re = /^\d{5}([\-]\d{4})?$/;
  return (re.test(text));
}

function isValidCanadianPostalCode(text) {
  var re = /^\s*[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d\s*$/i;
  return (re.test(text));
}

// Set the home page for the browser (only works with IE)
function setHomePage(homePageURL, anchor) {
   anchor.style.behavior='url(#default#homepage)';
   anchor.setHomePage(homePageURL);
}


var isIE = ((navigator.userAgent.indexOf("MSIE") != -1)); 
var isIE5 = ((navigator.userAgent.indexOf("MSIE 5.") != -1)); 
var isIE6 = ((navigator.userAgent.indexOf("MSIE 6.") != -1)); 
var isIE7 = ((navigator.userAgent.indexOf("MSIE 7.") != -1)); 
var isIE8 = ((navigator.userAgent.indexOf("MSIE 8.") != -1)); 
var isFirefox = ((navigator.userAgent.indexOf("Firefox") != -1)); 
var isFirefox2 = ((navigator.userAgent.indexOf("Firefox/2.") != -1)); 
var isFirefox3 = ((navigator.userAgent.indexOf("Firefox/3.") != -1)); 
var isSafari = ((navigator.userAgent.indexOf("Safari") != -1)); 
var isIPhone = ((navigator.userAgent.indexOf("iPhone") != -1)); 
