// ----------------------------------------------------------------------------
//       File Name: enduserWidgets_2007.js
//       Subsystem: enduser
//   Document Type: Javascript file
//    Dependencies: enduser.js
//         Purpose: currently sets up the date pickers for wild boar
//                  eventually it will hold all the logic for enduser widgets
// ----------------------------------------------------------------------------

var jsCalObjects = new Array();
var overCal = false;

//keep track of the mouse so that we know when to hide and show calendar
function setupListeners() 
{
    YAHOO.util.Event.addListener(this.oDomContainer.id, 'mouseover', mouseoverCal);
    YAHOO.util.Event.addListener(this.oDomContainer.id, 'mouseout', mouseoutCalendar);
}

//this event handler should basically just call fire the select event 
//and filter things through the calendar, but we do need to 
//do some validation since we allow people to type directly into the textbox
function calTextInputChanged(e, calObject)
{
    var re = /^\s*$/;
    var elem = YAHOO.util.Event.getTarget(e);
    var dateStr = elem.value;
    //test to see if they just blanked out the field... currently the only way to choose no value
    if(re.test(elem.value))
    {
        //blank out all of the hidden dropdowns
        //fudge the post vars to minimize impact of adding this widget in 8-3-fixes
        //perhaps something more elegant can be done in trunk
        YAHOO.util.Dom.get(calObject.calParmRoot + "_mon").value = "";
        YAHOO.util.Dom.get(calObject.calParmRoot + "_day").value = "";
        YAHOO.util.Dom.get(calObject.calParmRoot + "_yr").value = "";
        calObject.clear();
    }
    else 
    {
        var passedTest = isTextPartValidDate(elem)
        if(!passedTest)
        {
            //do nothing, it should be caught onSubmit
        }
        else
        {
            //inserted valid date by hand this will fire the select event
            calObject.select(passedTest);
        }
    }
}
//shows the calendar...
//also does some validation to decide which month/year to show, and which date to select
//only show a selected date if the text in the box can be parsed to a valid date.
function showCal(e, calObject) 
{
    var xy = YAHOO.util.Dom.getXY(calObject.correspondingTextInput);
    var elem = YAHOO.util.Event.getTarget(e);
    var dateStr = elem.value;
    
    if (dateStr)
    {
        //passedTest is either false or a Date object
        var passedTest = isTextPartValidDate(elem);     
        if(!passedTest)                 {
            //if it doesn't, default to now
            passedTest = new Date();
            dateStr = "";
        }
        //we assume that the dateStr they entered is valid
        calObject.cfg.setProperty('selected', dateStr);
        calObject.cfg.setProperty('pagedate', passedTest, true);
        calObject.render();
    }
    calObject.show();
    xy[1] = xy[1] + 20;
    YAHOO.util.Dom.setXY(calObject.oDomContainer.id, xy);
}

//the onSelect handler handles event when user clicks on date in calendar
//or when they change the textbox by hand
function getSelectedDate() 
{
    //get the date selected form the calendar, this
    var thisDate = this.getSelectedDates()[0];
    if(typeof(thisDate) != "undefined" && !isNaN(thisDate))
    {
        //parse the date to the string to put in the textbox
        var monthPos = this.cfg.getProperty("MDY_MONTH_POSITION");
        var datePos = this.cfg.getProperty("MDY_DAY_POSITION");
        var yearPos = this.cfg.getProperty("MDY_YEAR_POSITION");
        var tempArray = new Array();
        tempArray[monthPos] = thisDate.getMonth() + 1;
        tempArray[datePos] = thisDate.getDate();
        tempArray[yearPos] = thisDate.getFullYear();
        thisDateString = tempArray[1] + "/" + tempArray[2] + "/" + tempArray[3];
        //set the input text box to the date
        YAHOO.util.Dom.get(this.correspondingTextInput).value = thisDateString;
        //fudge the post vars to minimize impact of adding this widget in 8-3-fixes
        //perhaps something cleaner can  be done in trunk 
        YAHOO.util.Dom.get(this.calParmRoot + "_mon").value = thisDate.getMonth() + 1;
        YAHOO.util.Dom.get(this.calParmRoot + "_day").value = thisDate.getDate();
        YAHOO.util.Dom.get(this.calParmRoot + "_yr").value = thisDate.getFullYear();
    }
    overCal = false;
    hideCal(null, this);
}

function hideCal(e, calObject) 
{
    if (!overCal)
    {
        calObject.hide();
    }
}

function mouseoverCal() 
{
    overCal = true;
}

function mouseoutCalendar() 
{
    overCal = false;
}
