/*
$Revision: 10177 $
$Workfile: calendar_ajax.js $
$Author: jcarlson $
$Modtime: 11/01/05 11:26p $ 
*/


	function displayMonth(calendarDivID,direction) {
	
		var cal = document.getElementById(calendarDivID);
		var daMonths = getElementsByClass("availabilityCalendar", "div", cal);
		var curDisplayStartIndex = null; //objUnitCalendars[calendarDivID].curMonthIndex;
		var curDisplayEndIndex = null;					
		var newCalendarDiv = null;
		var displayedCount = 0; 		
		
		for (var ii=0; ii < daMonths.length; ii++) {
			// loop over the months to find which is displayed
			
			if ( daMonths[ii].className == 'availabilityCalendar' ) {
				// ok, this one is displayed, so let's increment the displayed counter
				displayedCount++;
				
				if (curDisplayStartIndex == null) {
					// we just found the starting displayed Month index
					curDisplayStartIndex = ii;
				}
			} 
		}									
		
		if (direction == 'next') {
			// we're moving forward in the calendar			
			
			// disable next buttons			
			setButtonAction(calendarDivID,'next','disabled');
			
			if ( (curDisplayStartIndex + displayedCount) != daMonths.length) {
				// the next one is not the last one, so hide it
				daMonths[curDisplayStartIndex].className = 'availabilityCalendarHidden'; 
			}
			// display the next one after the last displayed one
			daMonths[curDisplayStartIndex + displayedCount].className = 'availabilityCalendar';		
			
			// this will be the one we need to load next
			tmpArray = daMonths[curDisplayStartIndex + displayedCount].id.split('_');	
			curYear = parseInt(tmpArray[1]);
			curMonth = parseInt(tmpArray[2]);				
			
			if (curMonth == 12) {
				//alert('@ end');
				// we're at the end of the year, so let's start at january and increment year
				newMonth = 1;
				newYear = curYear + 1;
			} else {
				//alert('in middle');													
				// we're in the middle of the year, so just increment month
				newMonth = curMonth + 1;			
				newYear = curYear;								
			}
			
			// since we went next, we KNOW the previous button should be enabled
			setButtonAction(calendarDivID,'prev','enable');			
			
		} else {
			// we're moving backwards in the calendar	
			
			// disable previous buttons
			setButtonAction(calendarDivID,'prev','disabled');			
							
			// display the previous month	
			daMonths[curDisplayStartIndex-1].className = 'availabilityCalendar'; 
			
			// hide the last month
			daMonths[curDisplayStartIndex + displayedCount-1].className = 'availabilityCalendarHidden';															
			
			// this will be the one we need to load next
			tmpArray = daMonths[curDisplayStartIndex-1].id.split('_');	
			curYear = parseInt(tmpArray[1]);
			curMonth = parseInt(tmpArray[2]);	
			
			if (curMonth == 1) {
				//alert('@ beginning');														
				// we're at the beginning of the year, so let's start at december and decrement year
				newMonth = 12;
				newYear = curYear - 1;
			} else {
				//alert('in middle');								
				// we're in the middle of the year, so just decrement month
				newMonth = curMonth - 1;			
				newYear = curYear;								
			}	
			
			// since we went previous, we KNOW the next button should be enabled
			setButtonAction(calendarDivID,'next','enable');
		} // end else
		
		
		// this is the new id, based on the unitID, new year and new month we need to download
		var newID = objUnitCalendars[calendarDivID].unitID  + '_' + newYear + '_' + newMonth;
		
		// handle to the month div we're going to display if user clicks this btn again
		newCalendarDiv = document.getElementById(newID); //			

		if (newCalendarDiv == null) {
			// the reference to the calendar we need is null, which means we gotta get it (so call ajax code)
			var calendarFileURL = objUnitCalendars.urlRoot + '/' + objUnitCalendars[calendarDivID].unitID + '/' + objUnitCalendars.proNo + '_' + newID + '.htm';
			getCalendarHTML(calendarDivID,calendarFileURL,direction);
		} else {							
			//alert(newCalendarDiv.id + ' already loaded!');
			// this month has already been loaded into the DOM cache, so re-enable the button
			setButtonAction(calendarDivID,direction,'enabled');	
		} 

			
	}		


		function getCalendarHTML(targetCalendarDivID,calendarFileURL,direction) {

			req = false;			
			
			//prompt('loading....',calendarFileURL);
		    
		    if(window.XMLHttpRequest) {
				// branch for native XMLHttpRequest object
		    	try {
					req = new XMLHttpRequest();
		        } catch(e) {
					req = false;
		        }
				
		    
		    } else if(window.ActiveXObject) {
				// branch for IE/Windows ActiveX version
		       	try {
		        	req = new ActiveXObject("Msxml2.XMLHTTP");
		      	} catch(e) {
		        	try {
		          		req = new ActiveXObject("Microsoft.XMLHTTP");
		        	} catch(e) {
		          		req = false;
		        	}
				}
		    }
			
			if(req) {
			
				req.onreadystatechange = function(){
					// default values to null, so we can test for 'em
					var err = null;
		
				    if (req.readyState == 4) {
						// readyState is "loaded"
				        if (req.status == 200) {
							// return status is "ok"
							
							// make a call to update THIS calendar div with the returned HTML 
							updateCalendar(targetCalendarDivID,direction,req.responseText);
							setButtonAction(targetCalendarDivID,direction,'enabled');	
							if ( err != null) {
								alert(err);
							}
				        } else {
							// the status is something other than 'valid' .. perhaps 404 or something . .just don't do anything...
				            //alert("There was a problem retrieving the XML data:\n" + req.statusText);
				        }
				    }
				};
				
				try 
				{
					// try to open the file .. if it can't .. . catch it and disable that button
					req.open("GET", calendarFileURL, true);				
					req.send("");
				}
				catch (e)
				{
					//alert(e);
				}
			}

		}	
		
		function updateCalendar(targetCalendarDivID,direction,newMonthHTML) {
		
			// this function updates a calendar div with passed HTML, depending on if we're going next or previous
			var cal = document.getElementById(targetCalendarDivID);
			var daMonths = cal.getElementsByTagName('div');
			var newMonthDiv = document.createElement('div');
		
			// 'next' = append to end | 'prev' = prepend to beginning
			cal.innerHTML = (direction == 'next')? cal.innerHTML + newMonthHTML : newMonthHTML + cal.innerHTML;	
		}
		
		
		function setButtonAction(calendarID,direction,btnAction) {
			// grab references to BOTH of the buttons we want to touch
			var btn1 = document.getElementById(direction + '1_' + calendarID);
			var btn2 = document.getElementById(direction + '2_' + calendarID);
			
			// our image filenames
			var btnImages = new Object();
			btnImages.next = ['buttonNext.png', 'buttonNextDisabled.png'];
			btnImages.prev = ['buttonPrevious.png', 'buttonPreviousDisabled.png'];			
			
			if (btnAction == 'disabled') {
				// disabled, so set to disabled image and take away href
				btn1.firstChild.setAttribute("src", objUnitCalendars.iconRoot + '/' + btnImages[direction][1]);
				btn2.firstChild.setAttribute("src", objUnitCalendars.iconRoot + '/' + btnImages[direction][1]);
				btn1.setAttribute("href", "javascript:na();");								
				btn2.setAttribute("href", "javascript:na();");								
			} else {
				// enabled, so set to regular image and add back in href			
				btn1.firstChild.setAttribute("src", objUnitCalendars.iconRoot + '/' + btnImages[direction][0]);
				btn2.firstChild.setAttribute("src", objUnitCalendars.iconRoot + '/' + btnImages[direction][0]);				
				btn1.setAttribute("href", "javascript:displayMonth('" + calendarID + "','" + direction + "');");								
				btn2.setAttribute("href", "javascript:displayMonth('" + calendarID + "','" + direction + "');");												
			}
			
			
		}
		
		function na()
		{
			// do nothing
		}