var WriteOnce = true;
// Constants
var milliInDay = 60 * 60 * 24 * 1000;
var OldEventListener;
var InstancedCals = Array();
var CalCount=0;

// This function is used mainly for Front end calendars like www.bookit.com, keystone.bookit.com, etc.
// Its purpose is to validate the calendar dates and submit if valid.
function ValidateDates(ArrDateVal, DepDateVal, StartCal, EndCal) {
	var Adt = new Date(TwoDateYear_to_FourDateYear(ArrDateVal));
	var Ddt = new Date(TwoDateYear_to_FourDateYear(DepDateVal));
	var ErrMsg = document.getElementById("ErrMsg");
	var ErrDiv = document.getElementById('error');
	var ErrMsgTable = document.getElementById("ErrMsgTable");

	if(ArrDateVal.length != 8 || DepDateVal.length != 8) {
		ErrMsg.innerHTML = "<b>Arrival date and Departure date must be in the form mm/dd/yy</b>";
		ErrDiv.style.display='block';
		if (typeof ErrMsgTable != 'undefined')
			ErrMsgTable.style.display='';
		return;
	}
	else if(Adt == null || Adt == NaN || Ddt == null || Ddt == NaN) {
		ErrMsg.innerHTML = "<b>Invalid date.</b>";
		ErrDiv.style.display='block';
		if (typeof ErrMsgTable != 'undefined')
			ErrMsgTable.style.display='';
		return;
	}
	else if(Adt.getTime() > Ddt.getTime()) {
		ErrMsg.innerHTML = "<b>Arrival date can not be after the departure date.</b>";
		ErrDiv.style.display='block';
		if (typeof ErrMsgTable != 'undefined')
			ErrMsgTable.style.display='';
		return;	
	}
	
	if(Adt.getTime() < StartCal.initialDate.getTime() || Ddt.getTime() < EndCal.initialDate.getTime()) {
		if(Adt.getTime() < StartCal.initialDate.getTime()) {
			ErrMsg.innerHTML = "<b>Arrival date can not be in the past.</b>";
			ErrDiv.style.display='block';
			if (typeof ErrMsgTable != 'undefined')
				ErrMsgTable.style.display='';
			document.search.ARR_DATE_HM.value = '';
		}
		if(Ddt.getTime() < EndCal.initialDate.getTime()) {
			ErrMsg.innerHTML = "<b>Departure date can not be in the past.</b>";
			ErrDiv.style.display='block';
			if (typeof ErrMsgTable != 'undefined')
				ErrMsgTable.style.display='';
			document.search.DEP_DATE_HM.value = '';
		}
		return;
	}
	
	document.search.submit();
}

function getAnchorPosition(anchorname) {
        // This function will return an Object with x and y properties
        var useWindow=false;
        var coordinates=new Object();
        var x=0,y=0;
        // Browser capability sniffing
        var use_gebi=false, use_css=false, use_layers=false;
        if (document.getElementById) { use_gebi=true; }
        else if (document.all) { use_css=true; }
        else if (document.layers) { use_layers=true; }
        // Logic to find position
         if (use_gebi && document.all) {
                x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
                y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
        }
        else if (use_gebi) {
                var o=document.getElementById(anchorname);
                x=AnchorPosition_getPageOffsetLeft(o);
                y=AnchorPosition_getPageOffsetTop(o);
        }
         else if (use_css) {
                x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
                y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
        }
        else if (use_layers) {
                var found=0;
                for (var i=0; i<document.anchors.length; i++) {
                        if (document.anchors[i].name==anchorname) { found=1; break; }
                }
                if (found==0) {
                        coordinates.x=0; coordinates.y=0; return coordinates;
                }
                x=document.anchors[i].x;
                y=document.anchors[i].y;
        }
        else {
                coordinates.x=0; coordinates.y=0; return coordinates;
        }
        coordinates.x=x;
        coordinates.y=y;
        return coordinates;
}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
        var coordinates=getAnchorPosition(anchorname);
        var x=0;
        var y=0;
        if (document.getElementById) {
                if (isNaN(window.screenX)) {
                        x=coordinates.x-document.body.scrollLeft+window.screenLeft;
                        y=coordinates.y-document.body.scrollTop+window.screenTop;
                }
                else {
                        x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
                        y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
                }
        }
        else if (document.all) {
                x=coordinates.x-document.body.scrollLeft+window.screenLeft;
                y=coordinates.y-document.body.scrollTop+window.screenTop;
        }
        else if (document.layers) {
                x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
                y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
        }
        coordinates.x=x;
        coordinates.y=y;
        return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
        var ol=el.offsetLeft;
        while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
        return ol;
}
function AnchorPosition_getWindowOffsetLeft (el) {
        return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}
function AnchorPosition_getPageOffsetTop (el) {
        var ot=el.offsetTop;
        while((el=el.offsetParent) != null) { ot += el.offsetTop; }
        return ot;
}
function AnchorPosition_getWindowOffsetTop (el) {
        return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

function DateRange(StartDate, InclusiveStart, EndDate, InclusiveEnd) {

        function ValidStartDate(TestDate) {
                if(this.inclusiveStart == true) {
                        if(this.startDate.getTime() <= TestDate.getTime())
                                return true;
                }
                else {
                        //alert(this.startDate.getTime());
                        if(this.startDate.getTime() < TestDate.getTime())
                                return true;
                }

                return false;
        }

        function ValidEndDate(TestDate) {
                if(this.inclusiveEnd == true) {
                        if(this.endDate.getTime() >= TestDate.getTime())
                                return true;
                }
                else {
                        if(this.endDate.getTime() > TestDate.getTime())
                                return true;
                }

                return false;
        }

        function DateInRange(TestDate) {

                if(this.startDate == null && this.endDate != null) {
                        return this.ValidEndDate(TestDate);
                }
                else if(this.startDate != null && this.endDate == null) {
                        return this.ValidStartDate(TestDate);
                }
                else if(this.startDate != null && this.endDate != null) {
                        return this.ValidStartDate(TestDate) && this.ValidEndDate(TestDate);
                }

                return false;
        }

        // METHODS
        this.DateInRange = DateInRange;
        this.ValidEndDate = ValidEndDate;
        this.ValidStartDate = ValidStartDate;

        // PROPERTIES
        this.startDate = StartDate;
        this.endDate = EndDate;
        this.inclusiveStart = InclusiveStart;
        this.inclusiveEnd = InclusiveEnd;
}



function IsClicked(e) {
        var Div = document.getElementById("NewestCalendarDiv");
        var use_gebi=false;
        var use_css=false;
        var use_layers=false;

        if (Div != null) {
                if (document.getElementById)        { use_gebi = true; }
                else if (document.all)                        { use_css = true; }
                else if (document.layers)                { use_layers = true; }

                if (use_layers && e) {
                        //alert("Layered toad goes KaBoOm!!!");
                        var clickX = e.pageX;
                        var clickY = e.pageY;
                        var t = document.layers["NewestCalendarDiv"];
                        if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) {
                                return true;
                        }
                        else { return false; }
                }
                else if (document.all) { // Need to hard-code this to trap IE for error-handling
                        //alert("Document.All toad goes KaBoOm!!!");
                        var t = window.event.srcElement;
                        while (t.parentElement != null) {
                                //alert(t.id);
                                if (t.id=='PrevMonth' || t.id=='NextMonth') {
                                        return false;
                                }
                                if (t.id=="NewestCalendarDiv") {
                                        return true;
                                }
                                t = t.parentElement;
                        }
                        return false;
                }
                else if (use_gebi && e) {
                        var t = e.originalTarget;
                        while (t.parentNode != null) {
                                if (t.id=="NewestCalendarDiv") {
                                        return true;
                                }
                                t = t.parentNode;
                        }
                        return false;
                }
                return false;
        }
        return false;
}

function IsNodeId(str) {
        var t = window.event.srcElement;
        //alert(t.id);
        while (t.parentNode != null) {
                if(t.id == str)
                        return true;
                t = t.parentNode;
        }
        return false;
}

var toggle = false;
var MoveHandler = {

        mouseUpHandler:                function (e) {
                var CalDiv = document.getElementById("NewestCalendarDiv");
                if(e == null) {
                        if(toggle) {
                                //if (t.id=='Prev' || t.id=='Next') {
                                //        return;
                                //}
                                //alert("TOAD1 is hidding now");
                                if(IsNodeId('Prev')) {
                                        //alert('Prev node hit');
                                }
                                else if(IsNodeId('Next')) {
                                        //alert('Next node hit');
                                }
                                else {
                                        CalDiv.style.visibility = 'hidden';
                                        showElement("SELECT");
                                        toggle = !toggle;
                                }
                        }
                }
                else {
                        if(!IsClicked(e)) {
                                //alert("TOAD1 is hiding now");
                                CalDiv.style.visibility = 'hidden';
                        }
                }
        },

        start:                function () {

                // test DOM support
                // notice that CSS requires Views so there is no need to test that
                if ( typeof document.implementation != "undefined" &&
                        document.implementation.hasFeature("HTML", "1.0") &&
                        document.implementation.hasFeature("Events", "2.0") &&
                        document.implementation.hasFeature("CSS", "2.0") ) {

                        var oThis = this;
                        document.addEventListener("mouseup",   function (e) { oThis.mouseUpHandler(e); },   false);
                }
                else {
                        //alert("EXPLODING TOADS!");
                        /*if (document.layers) {
                                //alert("Layerd Toads!");
                                document.captureEvents(Event.MOUSEUP);
                        }
                        var OldEventListener = document.onmouseup;
                        if (OldEventListener != null) {
                                //alert("Old Toads!");
                                document.onmouseup = new Function("OldEventListener(); MoveHandler.mouseUpHandler();");
                        }
                        else {
                                //alert("Plain old toads!");
                                //var CalDiv = document.getElementById("NewestCalendarDiv");
                                //CalDiv.onmouseup = MoveHandler.mouseUpHandler;

                        }*/
                        document.onmouseup = MoveHandler.mouseUpHandler;
                }
        }
};

/* hides <select> and <applet> objects (for IE only) */
function hideElement(elmID) {
        var ie  = document.all;
        if(ie) {
                for(i = 0; i < document.all.tags( elmID ).length; i++) {
                        obj = document.all.tags(elmID)[i];
                        if(!obj || !obj.offsetParent) continue;
                        obj.style.visibility = 'hidden';
                }
        }
}

function BlankInputs(start, end, WhoValidating) {
        if(start.selectedDate.getTime() <= end.selectedDate.getTime()) {
		//alert("Not Blanked!");
                return;
        }
        else if(end.selectedDate.getTime() < start.selectedDate.getTime() && WhoValidating == end) {
                //alert("Blanked1 " + end.selectedDate.getTime() + " < " + start.selectedDate.getTime());
		var Input = document.getElementById(end.inputId);
                Input.value = '';
        }
        else if(end.selectedDate.getTime() < start.selectedDate.getTime() && WhoValidating == start) {
		//alert("Blanked2 " + end.selectedDate.getTime() + " < " + start.selectedDate.getTime());
                var Input = document.getElementById(end.inputId);
                Input.value = '';
        }
}

function showElement(elmID) {
        var ie  = document.all;
        if(ie) {
                for(i = 0; i < document.all.tags( elmID ).length; i++) {
                        obj = document.all.tags(elmID)[i];
                        if(!obj || !obj.offsetParent) continue;
                        obj.style.visibility = '';
                }
        }
}

function GetDayOfWeek(DayN) {
        //alert("GetDayOfWeek");
        //document.write("Day of week = " + DayN);
        switch(DayN) {
                case(0):
                return 'Sun';
                case(1):
                return 'Mon';
                case(2):
                return 'Tue';
                case(3):
                return 'Wed';
                case(4):
                return 'Thr';
                case(5):
                return 'Fri';
                case(6):
                return 'Sat';
        }
        return "";
}

function TwoDateYear_to_FourDateYear(str) {
	var StrArr = str.split('/');
	
	return(StrArr[0] + "/" + StrArr[1] + "/20" + StrArr[2]);
}

function GetDateStr2DY(dt)
{
        if(dt == null)
                return "null";

        if(dt.getMonth()+1 < 10)
                result = "0" + (dt.getMonth() + 1);
        else
                result = (dt.getMonth() + 1);

        if(dt.getDate() < 10)
                result += "/0" + dt.getDate();
        else
                result += "/" + dt.getDate();

        var str = dt.getFullYear().toString();
        result += "/" + str.charAt(2) + str.charAt(3);
        //if((dt.getFullYear() - 2000) < 10)
        //        result += "/0" + (dt.getFullYear() - 2000);
        //else
        //        result += "/" + (dt.getFullYear() - 2000);

        return result;
}

function GetDateStr(dt)
{
        if(dt == null)
                return "null";

        if(dt.getMonth()+1 < 10)
                result = "0" + (dt.getMonth() + 1);
        else
                result = (dt.getMonth() + 1);

        if(dt.getDate() < 10)
                result += "/0" + dt.getDate();
        else
                result += "/" + dt.getDate();

        if(dt.getYear() < 10)
                result += "/" + dt.getFullYear();
        else
                result += "/" + dt.getFullYear();

        return result;
}

// The Calendar Object
function Calendar(aId, iId, dtStr, CalendarName, dtRange) {

        function LooseFocus() {
                var CalDiv = document.getElementById(this.divId);
                CalDiv.style.visibility = 'hidden';
                showElement("SELECT");
        }

        function Clear() {
                //alert("ClearCalendar");
                for(var i=1; i<=6; i++) {
                        for(var j=0; j<7; j++) {
                                var id = GetDayOfWeek(j) + i;
                                var el = document.getElementById(id);
                                el.innerHTML = '&nbsp';
                                el.className = 'NewestCalendarDisabledDay';
                                //document.write("Changing " + id + "<br>");
                        }
                }
        }

        function AddDay() {
                //alert("AddDay");
                this.currentDate.setDate(this.currentDate.getDate() + 1);
        }

        function SubMonth() {
                //alert("SubMonth");
                if(this.currentDate.getMonth() == 1) {
                        this.currentDate.setMonth(12);
                        this.currentDate.setFullYear(this.currentDate.getFullYear() - 1);
                        this.currentDate.setDate(1);
                }
                else {
                        this.currentDate.setMonth(this.currentDate.getMonth() - 1);
                        this.currentDate.setDate(1);
                }
                //alert(this.currentDate);
        }

        function AddMonth() {
                //alert("AddMonth");
                if(this.currentDate.getMonth() == 12) {
                        this.currentDate.setMonth(1);
                        this.currentDate.setFullYear(this.currentDate.getFullYear() + 1);
                        this.currentDate.setDate(1);
                }
                else {
                        this.currentDate.setMonth(this.currentDate.getMonth() + 1);
                        this.currentDate.setDate(1);
                }
        }

        function GetMonthName() {
                //alert("GetMonthName");
                var dtYear = this.currentDate.getFullYear();
                switch(this.currentDate.getMonth())
                {
                        case(0):
                                return("January " + dtYear);
                        case(1):
                                return("Feburary " + dtYear);
                        case(2):
                                return("March " + dtYear);
                        case(3):
                                return("April " + dtYear);
                        case(4):
                                return("May " + dtYear);
                        case(5):
                                return("June " + dtYear);
                        case(6):
                                return("July " + dtYear);
                        case(7):
                                return("August " + dtYear);
                        case(8):
                                return("September " + dtYear);
                        case(9):
                                return("October " + dtYear);
                        case(10):
                                return("November " + dtYear);
                        case(11):
                                return("December " + dtYear);
                }
        }

        function DaysInMonth() {
                //alert("DaysInMonth");
                switch(this.currentDate.getMonth()) {
                        case(0):
                                return(31);
                        case(1):
                                if (this.currentDate.getFullYear()/4 == parseInt(this.currentDate.getFullYear()/4))
                                        return(29);

                                return(28);// 28 usually or 29 for leap year which is above
                        case(2):
                                return(31);
                        case(3):
                                return(30);
                        case(4):
                                return(31);
                        case(5):
                                return(30);
                        case(6):
                                return(31);
                        case(7):
                                return(31);
                        case(8):
                                return(30);
                        case(9):
                                return(31);
                        case(10):
                                return(30);
                        case(11):
                                return(31);
                }
                return 0;
        }

        function WriteCellData(tblCell, day) {
                var ClassName='';
                var inner='';
				
		var Enabled = this.dateRange.DateInRange(this.currentDate);
		var Selected = this.currentDate.getTime() == this.selectedDate.getTime();
		var HighLight1 = this.currentDate.getTime() == this.HighLightDate1.getTime();
		var HighLight2 = this.currentDate.getTime() == this.HighLightDate2.getTime();
		
		if(Enabled)
			inner = "<span class='NewestCalendarEnabledDay' onclick='" + this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")'>"+day+"<span>";
		else
			inner = "<span class='NewestCalendarDisabledDay'>"+day+"</span>";
		
		if((HighLight1 || HighLight2) && Enabled) {
			ClassName = 'NewestCalendarHighLight';
		}
		else if((HighLight1 || HighLight2) && !Enabled) {
			ClassName = 'NewestCalendarHighLightDisabled';
		}
		/*else if(Selected && Enabled) {
			ClassName = 'NewestCalendarSelectedDay';
		}
		else if(Selected && !Enabled) {
			ClassName = 'NewestCalendarDisabledDay';
		}*/
		else {
			ClassName = 'NewestCalendarDisabledDay';
		}
		
		tblCell.className = ClassName;
                tblCell.innerHTML = inner;
                //alert(this.dateRange.startDate + "    " + this.dateRange.endDate);
                /*if(this.dateRange.DateInRange(this.currentDate) == true) {
                        ClassName = 'NewestCalendarEnabledDay';
                        inner = "<span class='NewestCalendarEnabledDay' onclick='" + this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")'";
                }
                else {
                        ClassName = 'NewestCalendarDisabledDay';
                        inner = "<span class='NewestCalendarDisabledDay' ";
                }
            	
                if(this.currentDate.getMonth() == this.selectedDate.getMonth() &&
                   this.currentDate.getDate() == this.selectedDate.getDate() &&
                   this.currentDate.getFullYear() == this.selectedDate.getFullYear()) {
                        inner += " class='NewestCalendarSelectedDay'>" + day + "</span>";
                }
                else {
                        inner += ">" + day + "</span>";
                }

                if(this.HighLightDate1 != null) {
	                if(this.currentDate.getMonth() == this.HighLightDate1.getMonth() &&
	                	this.currentDate.getDate() == this.HighLightDate1.getDate() &&
	                	this.currentDate.getFullYear() == this.HighLightDate1.getFullYear()) {
		                //alert("DATE 1 HIGHLIGHTING");
		                ClassName = 'NewestCalendarHightlight';
		            	inner = "<span class='NewestCalendarHightlight' onclick='" + this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")'>" + day + "</span>";    	
	            	}
        		}
        		
        		//alert(this.HighLightDate2);
        		if(this.HighLightDate2 != null) {
	        		if(this.currentDate.getMonth() == this.HighLightDate2.getMonth() &&
	                	this.currentDate.getDate() == this.HighLightDate2.getDate() &&
	                	this.currentDate.getFullYear() == this.HighLightDate2.getFullYear()) {
		                //alert("DATE 2 HIGHLIGHTING");
		                ClassName = 'NewestCalendarHightlight';
		            	inner = "<span class='NewestCalendarHightlight' onclick='" + this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")'>" + day + "</span>";    	
	            	}
				}            	                

				if(this.HighLightDate1 != null && this.HighLightDate2 != null) {
					if(this.HighLightDate1.getTime() < this.currentDate.getTime() && this.currentDate.getTime() < this.HighLightDate2.getTime()) {
						ClassName = 'NewestCalendarInBetweenHighlight';
						inner = "<span class='NewestCalendarInBetweenHighlight' onclick='" + this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")'>" + day + "</span>";
					}
				}
				
                tblCell.className = ClassName;
                tblCell.innerHTML = inner;
                //tblCell.onClick = this.calName + ".Hide(\"" + GetDateStr(this.currentDate) + "\")";*/
        }

        function RecalLayout() {
                //alert("LayoutCalendar");
                var DiM = this.DaysInMonth();
                var MonthNameDiv = document.getElementById('MonthName');
                MonthNameDiv.innerHTML = this.GetMonthName(this.currentDate);
                //alert("Start Date Range: " + GetDateStr(this.dateRange.startDate) + "\nEnd Date Range: " + GetDateStr(this.dateRange.endDate));

                this.Clear();
                var WeekN = 1;
                this.currentDate.setDate(1);
                //alert("Starting current date: " + this.currentDate);
                for(var i=1; i<=DiM; i++) {
                        var id = GetDayOfWeek(this.currentDate.getDay()) + WeekN;
                        //document.write(id + " " + tempDate + "Day = " + tempDate.getDay() + "<br>");
                        var tblCell = document.getElementById(id);
                        this.WriteCellData(tblCell, i);
                        if(this.currentDate.getDate() != DiM)
                                this.AddDay();

                        if(this.currentDate.getDay() == 0)
                                WeekN++;
                }
                this.currentDate.setDate(1);
                //alert("Ending current date: " + this.currentDate);
        }

        // Methods defined
        function Hide(day) {
                //alert(day);
                var CalDiv = document.getElementById(this.divId);
                var Input = document.getElementById(this.inputId);
                CalDiv.style.visibility = 'hidden';
		if(day != null || day == "") {
                	this.selectedDate = new Date(day);
                	this.currentDate = new Date(day);
                }
		this.SelectCallBack(this);
                showElement("SELECT");
        }
	
	function Blur() {
		var CalDiv = document.getElementById(this.divId);
		CalDiv.style.visibility = 'hidden';
		showElement("SELECT");
	}
	
        function PrevMonth() {
                //alert(this.calName + ".Prev() Called!");
                var MonthNameDiv = document.getElementById('MonthName');
                this.SubMonth();
                MonthNameDiv.innerHTML = this.GetMonthName(this.currentDate);
                this.RecalLayout();
        }

        function NextMonth() {
                //alert(this.calName + ".Next() Called!");
                var MonthNameDiv = document.getElementById('MonthName');
                this.AddMonth();
                MonthNameDiv.innerHTML = this.GetMonthName(this.currentDate);
                this.RecalLayout();
        }

        function ShowCalendar() {
		// Preload all images
		var Left = new Image();
		Left.src = "http://images.bookit.com/system/leftArrow_a.gif";
		var Right = new Image();
		Right.src = "http://images.bookit.com/system/rightArrow_a.gif";
				
                //alert(this.divId);
                var CalDiv = document.getElementById(this.divId);

		// Set the PREV and NEXT table innerHTML's
                var Prev = document.getElementById('Prev');
                var Next = document.getElementById('Next');
                var Today = document.getElementById('Today');

                Prev.innerHTML  = "<img src='http://images.bookit.com/system/leftArrow_a.gif' align='left' onclick='" + this.calName + ".PrevMonth()'>                <div id='MonthName' class='NewestCalendarMonthBar' align='center'>Test</div>";
                Next.innerHTML  = "<img src='http://images.bookit.com/system/rightArrow_a.gif' onclick='" + this.calName + ".NextMonth()'>";
                Today.innerHTML = "<a class=NewestCalendarToday href=\"javascript:void(0)\" onclick=\"" + this.calName + ".Hide('"+GetDateStr(this.initialDate)+"')\">Today</a>";
                
		hideElement("SELECT");
                this.startFocus        = false;
                if(this.PreShowCallBack != null)
                	this.PreShowCallBack(this);
                this.RecalLayout();
                this.SetPosition();
                CalDiv.style.visibility = 'visible';
		//document.getElementById('tblId').focus();
                toggle = true;
                //alert("Current: " + GetDateStr(this.currentDate) + "   Selected: " + GetDateStr(this.selectedDate) + "   Initial: ");
        }

        function SetPosition() {
                var Anchor = document.getElementById(this.anchorId);
                var CalDiv = document.getElementById(this.divId);
                var curleft = 0;
                var curtop = 0;

                point = getAnchorPosition(this.anchorId);
                CalDiv.style.left = point.x;
                CalDiv.style.top = point.y;
        }

        function SetLinked(obj) {
                this.LinkedCal = obj;
        }

        // Properties
        this.LinkedCal = null;
        this.anchorId = aId;
        this.inputId = iId;
        this.currentDate = null;                // This is an internal date used by the calendar not to be USED!!!!
        this.selectedDate = null;        // Contains the selectedDate the user clicked on.
        this.initialDate = null;
        this.dateRange = dtRange;
        this.calName = CalendarName;
		this.HighLightDate1 = null;
		this.HighLightDate2 = null;
		
        // Methods declared as an interface!
        this.SetLinked = SetLinked;
        this.LooseFocus = LooseFocus;
        this.SetPosition = SetPosition;
        this.Show = ShowCalendar;
        this.Hide = Hide;
	this.Blur = Blur;
        this.PrevMonth = PrevMonth;
        this.NextMonth = NextMonth;
        this.RecalLayout = RecalLayout;
        this.DaysInMonth = DaysInMonth;
        this.GetMonthName = GetMonthName;
        this.SubMonth = SubMonth;
        this.AddMonth = AddMonth;
        this.AddDay = AddDay;
        this.Clear = Clear;
        this.WriteCellData = WriteCellData;
        this.PreShowCallBack = null;
        this.SelectCallBack = null;                                // To be overridden by driver.
        this.startFocus = false;
        this.divId = "NewestCalendarDiv";
        InstancedCals[CalCount] = this;
        CalCount++;

        if(WriteOnce == true) {
                // Write the table when Calendar is called!
                document.write("<span class='NewestCalendarBody' id='" + this.divId + "'>");
                document.write("<table class='NewestCalendarTable' id='tblId' cellSpacing='0' cellPadding='0'>");
                document.write("        <tr>");
                document.write("                <td id='Prev' colSpan='7' class='NewestCalendarBarHeader'><img src='http://images.bookit.com/system/leftArrow_a.gif' align='left' onclick='" + this.calName + ".PrevMonth()'>");
                document.write("                        <div id='MonthName' class='NewestCalendarMonthBar' align='center'>Test</div>");
                document.write("                </td>");
                document.write("                <td height='2'></td>");
                document.write("                <td height='2'></td>");
                document.write("                <td height='2'></td>");
                document.write("                <td height='2'></td>");
                document.write("                <td height='2'></td>");
                document.write("                <td id='Next' height='2' class='NewestCalendarBarHeader' align='right'><img src='http://images.bookit.com/system/rightArrow_a.gif' onclick='" + this.calName + ".NextMonth()'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='ncSun' class='NewestCalendarWeekLetter'>S</td>");
                document.write("                <td id='ncMon' class='NewestCalendarWeekLetter'>M</td>");
                document.write("                <td id='ncTue' class='NewestCalendarWeekLetter'>T</td>");
                document.write("                <td id='ncWed' class='NewestCalendarWeekLetter'>W</td>");
                document.write("                <td id='ncThr' class='NewestCalendarWeekLetter'>T</td>");
                document.write("                <td id='ncFri' class='NewestCalendarWeekLetter'>F</td>");
                document.write("                <td id='ncSat' class='NewestCalendarWeekLetter'>S</td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri1' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat1' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri2' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat2' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri3' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat3' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri4' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat4' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri5' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat5' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Sun6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Mon6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Tue6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Wed6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Thr6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Fri6' class='NewestCalendarDisabledDay'></td>");
                document.write("                <td id='Sat6' class='NewestCalendarDisabledDay'></td>");
                document.write("        </tr>");
                document.write("        <tr>");
                document.write("                <td id='Today' colspan=7 class=NewestCalendarToday></td>");
                document.write("                <td></td>");
                document.write("                <td></td>");
                document.write("                <td></td>");
                document.write("                <td></td>");
                document.write("                <td></td>");
                document.write("                <td></td>");
                document.write("        </tr>");
                document.write("</table>");
                document.write("</span>");

                WriteOnce = false;
        }

}


MoveHandler.start();

