/*dependencies
    <dependencies>
        <item note = "this script utilizes the MoveToCursor functions in shared.js" domain = "sabreindustriesinc.com" name = "Shared.js">2</item>
        <item note = "this script utilizes calendar class calendarClass.js" domain = "sabreindustriesinc.com" name = "CalendarClass.js">2</item>
    </dependencies>
dependencies*/
/*
function DatePickerHelper() {}
DatePickerHelper.DatePicked = function () {}
DatePickerHelper.SetSelectedDate = function () {}
DatePickerHelper.SelectedDate = null;*/

var arCals = new Array();
var divIDs = new Array();

// this class will put a calendar gif wherever the script lies in the HTML, and seamlessly integrate it with the calendar class
function DatePicker(TxtID, CanPickPastYears, ResetToCurrentMonthWhenOpened, InstanceName, alignment, valign, bSkipAutoPosition, numYears, imgPath) {

    var self = this;
    this.Disabled = false;
    var tableDayClass = null;
    var dayComponentClass = null;
    var dayOfWeekHeaderClass = null;
    var calButtonClass = null;
    var calDropClass = null;
    var leftRightArrows = null;
    var skipAllRePositioning = false;

    if (imgPath == null) { imgPath = "http://www.SabreIndustriesInc.com/img/SHAREDIMG/icons/cal.gif"; }
    this.SkipAllRePositioning = function (value) { if (value == null) { return skipAllRePositioning; } else { skipAllRePositioning = value; } }
    
    // set the class names to be used when calendar is built.
	this.TableDayClass = function (value) { if(value == null) { return tableDayClass; } else { tableDayClass = value; } }
	this.DayComponentClass = function (value) { if(value == null) { return dayComponentClass; } else { dayComponentClass = value; } }
	this.DayOfWeekHeaderClass = function (value) { if(value == null) { return dayOfWeekHeaderClass; } else { dayOfWeekHeaderClass = value; } }
	this.CalButtonClass = function (value) { if(value == null) { return calButtonClass; } else { calButtonClass = value; } }
	this.CalDropClass = function (value) { if(value == null) { return calDropClass; } else { calDropClass = value; } }
	this.LeftRightArrowsClass = function (value) { if(value == null) { return leftRightArrows; } else { leftRightArrows = value; } }
	this.ImagePath = function (value) { if(value == null) { return imgPath; } else { imgPath = value; } }
	
	this.SetClassNames = function (tblDayClass, dayComponent, dayOfWeekHeader, buttonClass, dropClass, arrowsClass) {
	    
	    self.TableDayClass(tblDayClass);
	    self.DayComponentClass(dayComponent);
	    self.DayOfWeekHeaderClass(dayOfWeekHeader);
	    self.CalButtonClass(buttonClass);
	    self.CalDropClass(dropClass);
	    self.LeftRightArrowsClass(arrowsClass);
	
	}

    this.Write = function () {

        try {
        
            var index = arCals.length;
            var divID = "divCal" + index.toString() + TxtID;
            divIDs.push(divID);
            
            
            document.write("<span id = \"" + divID + "\" style = \"background-color:blue;position:absolute;display:none;\"></span>");
            
            arCals.push(new Calendar(divID, CanPickPastYears, ResetToCurrentMonthWhenOpened, "#688dc1", "silver", "#eeeeee", "red", "lightBlue", "arCals[" + index.toString() + "]", numYears));
            arCals[index].SetClassNames(self.TableDayClass(), self.DayComponentClass(), self.DayOfWeekHeaderClass(), self.CalButtonClass(), self.CalDropClass(), self.LeftRightArrowsClass());
            arCals[index].makeCalendar(); 
            
            
            document.getElementById(TxtID).readOnly = "readonly"; 
            document.write("<img src = \"" + self.ImagePath() + "\" id = \"img" + InstanceName + "\" alt = \"Click to select a date, right click to clear date.\" style = \"cursor:pointer;\" onclick = \"" + InstanceName + ".ShowCalendar(this, " + index.toString() + ");\" oncontextmenu = \"" + InstanceName + ".ClearDate(" + index.toString() + "); return false;\" />");
            
        } catch(x) {}

    }
    
    this.DatePicked = function () {}        // this is modified on a per instance basis. additional functionality to happen after the date is selected.
    this.DateCleared = function () {}       // same as above, but for when the field is cleared.

    this.ShowCalendar = function(obj, index) {

        if (this.Disabled) { return; }
        arCals[index].ShowHide();
        if (arCals[index].Hidden()) { return; }
        if (!skipAllRePositioning) { Shared.MoveToCursor(obj, el(divIDs[index]), alignment, valign, bSkipAutoPosition); } // using it's pos relative to the icon, now move it where it needs to go.
        arCals[index].DatePicked = function(sDate) { document.getElementById(TxtID).value = sDate; self.DatePicked(); }
        
    }

    this.ClearDate = function() { document.getElementById(TxtID).value = ""; self.DateCleared(); }

    this.Disable = function(bool) {

        self.Disabled = bool;
        var img = document.getElementById("img" + InstanceName);
        var txt = document.getElementById(TxtID);
        if (bool) {

            img.style.cursor = "default";
            Shared.AddClass(txt, "Disabled");
            self.ClearDate();

        } else {

            img.style.cursor = "pointer";
            Shared.RemoveClass(txt, "Disabled");

        }

    }
    
}
