/*
 * Copyright © 2005-2007 Tim Vasil and Intui Design & Development.
 * All rights reserved.
 */

var g_oCalendar;
 
function getById(id)
{
    return document.all[id];
}

function isValidEmailAddress(str)
{
    return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
}

function trim(str)
{
    return str.replace(/^\s*|\s*$/g, "");
}

function selectText(oField)
{
    if (oField.createTextRange)
    {
        var oRange = oField.createTextRange();
        oRange.moveStart("character", oField.value,length);
        oRange.moveEnd("character", oField.value.length);
        oRange.select();
    }
}

function go(url)
{
    document.location.href = url;
    return false;
}

function focus(oControl)
{
    oControl.focus();
    oControl.scrollIntoView(false);
}

function goLocation(list, baseUrl)
{
    if (list && list.selectedIndex >= 0)
    {
        document.location.href = appendParameter(baseUrl, list.name, list.options[list.selectedIndex].value);
    }
}

function appendParameter(sQs, sParamName, sParamValue)
{
	if (sQs == null)
	{
		sQs = "?";
	}
	else if (sQs.indexOf("?") >= 0)
	{
		sQs += "&";
	}
	else
	{
		sQs += "?";
	}
	sQs += sParamName;
	if (sParamValue != null && !this.isUndefined(sParamValue))
	{
		sQs += "=" + urlEncode(sParamValue);
	}
	return sQs;
}

function urlEncode(sValue)
{
	return encodeURIComponent(sValue);
}

function isUndefined(vItem)
{
	var undef;
	return vItem == undef;
}

function findAncestor(oSrc, tagName)
{
    tagName = tagName.toLowerCase();
    for (; oSrc != null; oSrc = oSrc.parentElement)
    {
        if (oSrc.tagName.toLowerCase() == tagName)
        {
            return oSrc;
        }
    }
    return null;
}

function getFieldSelectionCount(oForm, elementName)
{
    var iCount = 0;
    if (oForm)
    {
        var oElement = oForm.elements[elementName];
        if (oElement)
        {
            if (!oElement.length) { oElement = [ oElement]; }
            for (var i=0; i < oElement.length; i++)
            {
                if (oElement[i].checked) { iCount++; }
            }
        }
    }
    return iCount;
}

function addShift(oButton, bSuppressButtonUpdate)
{
    var oRow = findAncestor(oButton, "tr");
    var oNewRow = oRow.parentElement.insertRow(oRow.sectionRowIndex + 1);
    var oTable = findAncestor(oRow, "table");
    
    if (isUndefined(oTable.nextIdx))
    {
        oTable.nextIdx = oRow.parentElement.rows.length;
    }
    else
    {
        oTable.nextIdx++;
    }
    
    for (var i=0; i < oRow.cells.length; i++)
    {
        var oCell = oNewRow.insertCell();
        oCell.innerHTML = oRow.cells[i].innerHTML;
        oCell.style.paddingLeft = oRow.cells[i].style.paddingLeft;
        oCell.style.paddingRight = oRow.cells[i].style.paddingRight;
        oCell.className = oRow.cells[i].className;
        var oChild = oCell.firstChild;
        var oChild2 = oCell.lastChild;
        if (oChild.tagName && oChild.name)
        {
            var iIdxStart = oChild.name.indexOf('_');
            oChild.name = oChild.name.substr(0, iIdxStart + 1) + oTable.nextIdx;
            if (oChild.tagName.toLowerCase() == "select")
            {
                oChild.selectedIndex = 0;
            }
            else if (oChild2.tagName && oChild2.tagName.toLowerCase() == "input" && oChild2.type == "checkbox")
            {
                oChild2.checked = true;
            }
        }
    }
    
    var oFirstButton = oRow.cells[oRow.cells.length - 1].firstChild;
    if (!bSuppressButtonUpdate)
    {
        oFirstButton.style.display = "none";
        oFirstButton.nextSibling.style.display = "block";
    }
    
    var oIdxTracker = findAncestor(oFirstButton, "td").lastChild;
    oIdxTracker.value = oTable.nextIdx;
}

function splitShift(oButton)
{
    var oRow = findAncestor(oButton, "tr");
    addShift(oButton, true);
    var oNewRow = oRow.nextSibling;
    oNewRow.cells[0].firstChild.selectedIndex = oRow.cells[0].firstChild.selectedIndex;
    var iStartMin = (oRow.cells[1].firstChild.selectedIndex - 1) * 15;
    var iEndMin = (oRow.cells[2].firstChild.selectedIndex - 1) * 15;
    if (iStartMin >= 0 && iEndMin >= 0)
    {
        var iDiff = Math.round((iEndMin - iStartMin) / 2);
        if (iDiff < 0)
        {
            iDiff = 60 * 12 + iDiff;
        }
        else if (iDiff == 0)
        {
            iDiff = 60 * 12;
        }
        iDiff -= iDiff % 15;
        oRow.cells[2].firstChild.selectedIndex = ((iStartMin + iDiff) / 15) % (24 * 4) + 1;
        oNewRow.cells[1].firstChild.selectedIndex = oRow.cells[2].firstChild.selectedIndex;
        oNewRow.cells[2].firstChild.selectedIndex = iEndMin / 15 + 1;
    }
    else if (iEndMin >= 0)
    {
        oNewRow.cells[2].firstChild.selectedIndex = iEndMin / 15 + 1;
        oRow.cells[2].firstChild.selectedIndex = -1;
    }
}

function removeShift(oButton)
{
    if (confirm("Are you sure you want to remove this shift?\n\nRemoved shifts will not appear on any newly created schedules for this hospital, but they will remain visible on preexisting schedules."))
    {
        var oRow = findAncestor(oButton, "tr");
        oRow.parentElement.deleteRow(oRow.sectionRowIndex);
    }
}

function initCalendar(oBaseTag, bNoDisabledDates)
{
	var oElement = document.createElement();
	oElement.id = document.uniqueID;
	oElement.style.position = 'absolute';
	oElement.style.visibility = 'hidden';
	oElement.style.backgroundColor = 'white';

	oBaseTag.appendChild(oElement);
	var oCalendar = new CalendarPopup(oElement.id);
	if (bNoDisabledDates != true)
	{
	    oCalendar.addDisabledDates(null, formatDate(new Date(new Date() - 1000*60*60*24),"yyyy-MM-dd"));	
    }
	oCalendar.setCssPrefix("calendar");
	oCalendar.setReturnFunction("showCalendar_callback");
	oCalendar.showNavigationDropdowns();
	return oCalendar;
}

function showCalendar(oLink, oInput, bNoDisabledDates)
{
	if (!g_oCalendar)
	{
		g_oCalendar = initCalendar(document.body, bNoDisabledDates);
	}
	if (!oLink.name)
	{
		oLink.id = oLink.name = document.uniqueID;
	}
	
    var dDate = new Date(oInput.value);
    if (!isNaN(dDate))
    {
        g_oCalendar.currentDate = formatDate(dDate, "yyyy-M-d");
    }	
	g_oCalendar._inputField = oInput;
	g_oCalendar.select(oInput, oLink.id, 'M/d/yyyy');
}

function showCalendar_callback(y, m, d)
{
    var sOldValue = g_oCalendar._inputField.value;
    if (y == 0 && m == 0 && d == 0)
    {
        g_oCalendar._inputField.value = "";
    }
    else
    {
	    var dt = new Date(y, m-1, d, 0, 0, 0);
	    g_oCalendar._inputField.value = (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
	}
	if (g_oCalendar._inputField.onchange && sOldValue != g_oCalendar._inputField.value)
	{
	    g_oCalendar._inputField.onchange();
	}
}