

	var calDAY 		= null;
	var calMONTH 	= null;
	var calYEAR 	= null;
	
	var calACTIVE	= false;

	function runcal(cal_ref,cal_img,cal_wrap, cal_type){
		
		//runcal('calendar_housing_start','cal_start','arrival')
		
		//console.log("Running cal");
	
		var calendarReference 	= document.getElementById(cal_ref) ;		
		

	
		if ( calendarReference.style.display == 'none' )
		{
			

			var calendarWrapper		= document.getElementById(cal_wrap);
		
			var cal = new Calendar();
			
			cal.setCal(cal_ref);
			cal.setType(cal_type);
			
		
			
			
			calendarReference.style.display = 'block';
			calendarReference.style.position = 'absolute';
			calendarReference.style.left = "25px";
			calendarReference.style.top = "2px";
			

		
			calDAY = document.getElementById((cal_type+"Day")).options[document.getElementById((cal_type+"Day")).selectedIndex].value;
			
			
			var segments = document.getElementById((cal_type+"Month")).options[document.getElementById((cal_type+"Month")).selectedIndex].value;
			segments = segments.split("-");
			
			calMONTH =segments[0];
			calYEAR	=segments[1];
			

			cal.getCalInformation(calDAY,calMONTH,calYEAR);
			cal.generateHTMLCore(cal.CalInformation);
			cal.addHeaderMonthName();
			cal.addNavSymbols();
			
			
			
		}
		else
		{
			calendarReference.style.display = 'none';
		}
		
		
		
	}
	
	function Calendar()
	{
		this.CalInformation = {};
		this.calRef = null;
		
		this.calType;
		
		this.setCal = function(cal_ref){
			this.calRef=cal_ref;
		}
		
		this.setType=function(type){ this.calType=type; }
		
		
		/*
		 * getCalInformation
		 * @param day, month, year
		 * Get all the calendar information we might need for us
		 * to generate a calendar object display for the user
		 */
		this.getCalInformation = function( day, month, year )
		{
			var j=month-1;
			
			
			// console.log("Calendar information: " + day + "/" + month + "/"+year);
			
			var primaryDate = new Date(year,parseInt(j),1,0,0,0,0);			
			
			var monthName = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
			var monthNameLong = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
			var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
			var dayNamesShort = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
			
			
			this.CalInformation['start_day'] 	= 1;
			this.CalInformation['month'] 		= month;
			this.CalInformation['year'] 		= year;
			this.CalInformation['today']		= day;
			this.CalInformation['monthShort']	= monthName[j];
			this.CalInformation['monthLong']	= monthNameLong[j];
			this.CalInformation['available']	= [];
			this.CalInformation['next']	= {};
			this.CalInformation['prev'] = {};
			
			
			
			/* PRevious and next elements */
			
			var isNextDateOptionAvailable = true ;
			
			prevYear=year; var prevMonth = parseInt(month,10)-1; if ( prevMonth<1 ) { prevMonth = 12; prevYear--; }
			nextYear=year; var nextMonth = parseInt(month,10)+1; if ( nextMonth>12 ) { nextMonth = 1; nextYear++; }
			
			var currentDateUsePoint = new Date();
//			/console.log("Y:"+(parseInt(currentDateUsePoint.getFullYear())+1)+" M:"+currentDateUsePoint.getMonth()+" D:1");
			
			var currentDateVerification = new Date((parseInt(currentDateUsePoint.getFullYear())+1),currentDateUsePoint.getMonth(), 1,3,0,0,0);
			var destinationDateVerification = new Date(nextYear,(nextMonth-1),1,3,0,0,0);		
			
		if ( destinationDateVerification > currentDateVerification ) 
		{
			isNextDateOptionAvailable=false;
		}
		else
		{
			isNextDateOptionAvailable=true;
		}
			
			
			//var isNextDateOptionAvailable = (parseInt(nextOptionalDateSelection) <= parseInt(limitationDateNode)) ? true : false ; 
			
			
			
			//console.log("Calendar date check: nextLimit:" + nextOptionalDateSelection + " internalLimit:"+limitationDateNode);
			
			this.CalInformation['nextday']=1;
			this.CalInformation['nextmonth']=nextMonth;
			this.CalInformation['nextyear']=nextYear;
			this.CalInformation['isNextDateOptionAvailable']=isNextDateOptionAvailable;
			
			this.CalInformation['prevday']=1;
			this.CalInformation['prevmonth']=prevMonth;
			this.CalInformation['prevyear']=prevYear;
			
			
			
			
			var increment = 0;
			
			for ( var i = 1 ; i <= 31 ; i++ )
			{
				var myDate = new Date(year,parseInt(j,10),parseInt(i,10),4,0,0,0);
				
				if ( j<28 || this.isValid(myDate,primaryDate) )
				{					
					if ( myDate.getDate() > increment )
					{
				
						this.CalInformation['available'][increment] = {};
						
						this.CalInformation['available'][increment]['day'] = myDate.getDate();
						this.CalInformation['available'][increment]['dayName'] = myDate.getDay();
						this.CalInformation['available'][increment]['isSelected'] = (parseInt(day,10)==parseInt(myDate.getDate(),10)) ? 1 : 0;
						this.CalInformation['available'][increment]['dayLong'] = dayNames[myDate.getDay()];
						this.CalInformation['available'][increment]['dayShort'] = dayNamesShort[myDate.getDay()];
						this.CalInformation['available'][increment]['year'] = myDate.getFullYear();
						this.CalInformation['available'][increment]['month'] = myDate.getMonth();

						increment++;
					}
				}
			}
			
			if ( this.CalInformation['available'][0]['dayName'] == "0" ) 
			{
				this.CalInformation['offset'] = 7;
			}
			else
			{			
				this.CalInformation['offset']	= this.CalInformation['available'][0]['dayName'];
			}
			
			this.CalInformation['limit']	= increment;	
			
			// console.dir(this.CalInformation ) ; 
			// console.dir(this.CalInformation);			
		}
		
		
		/*
		 * isValid
		 * @param date,primaryDate - Date Objects
		 * Tests to ensure its a valid date in that months, stops
		 * over running into next month
		 */
		this.isValid = function( myDate,primaryDate )
		{
			return ( myDate.getMonth()==primaryDate.getMonth() && myDate.getYear()==primaryDate.getYear() ) ? true : false;
		}
		
		this.applyHover = function(obj)
		{
			obj.onmouseover=function(){
				this.style.color = '#FFFFFF';
				this.style.backgroundColor = '#444444';
			}
			obj.onmouseout=function(){
				this.style.color = '#444444';
				this.style.backgroundColor = '';
			}
			obj.onmousedown=function()
			{
				var d=document.getElementById((this.calType+"Day"));
				var monthyear = document.getElementById((this.calType+"Month"));
			
				var otherCal = (this.calType=="arrival") ? "departure":"arrival";
				
				//console.log("Day: " + this.day + " caltype:  " + this.calType);
				
				d.selectedIndex=(this.day-1);
				
				var mySeg = (this.month+1) + "-" + this.year;
				
			
				for ( var k = 0 ; k < monthyear.options.length ; k++ ) 
				{
					if ( monthyear.options[k].value == mySeg ) 
					{
						
						monthyear.selectedIndex = k;
					}
				}
				
			
					
				
				this.objref.style.display = 'none';
				
				HSSearch.checkDate();
				
			}
		}
		
		
		
		this.generateHTMLCore = function( cal )
		{
			var offset = cal['offset'];
			var limit = cal['limit'];
			var majorDiv = document.getElementById(this.calRef);
			
			
			
			majorDiv.innerHTML = '';
			
			var incrementalDay = 0;
			
			var left = 2;
			var top = 47;
			
			for ( var i = 1 ; i < 7 ; i++ )
			{
				for ( var j = 1 ; j < 8 ; j++ )
				{
					var overall_offset = (i*j)+1 ;
					
					
					if ( j < offset && i==1  )						
					{
						var el = this.makeElement("div",{'position':'absolute','left':left+"px",'top':top+"px",'backgroundColor':'#dedede'},{'className':'cal_left','innerHTML':''});
						majorDiv.appendChild(el);
					}
					
					else 
					{
						try 
						{
							var dayPoint = cal['available'][incrementalDay];
							
							if ( dayPoint.day ) 
							{					
								
								var isSelected = (calYEAR==dayPoint.year&&calMONTH==(dayPoint.month+1)&&calDAY==dayPoint.day) ? true : false;
								
								
								//console.log("Test:" + calYEAR+"=="+dayPoint.year+"&&"+calMONTH+"=="+(dayPoint.month+1)+"&&"+calDAY+"=="+dayPoint.day);
								
								
								
								var el = this.makeElement("div",{'position':'absolute','left':left+"px",'top':top+"px"},{'className':'cal_left'});
								majorDiv.appendChild(el);
								
								if ( isSelected )
								{
									el.style.backgroundColor = '#489F1C';
									el.style.color = '#FFFFFF';
								}
								
								el.day=dayPoint.day;
								el.month=dayPoint.month;
								el.year=dayPoint.year;
								el.objref = document.getElementById(this.calRef);
								el.calType = this.calType;
								
								var current_date_check = new Date();
								var node_destination_date = new Date(el.year,el.month,el.day+1);
								if ( node_destination_date >= current_date_check )
								{
								
									this.applyHover(el);
									 
									
									
									var numberPoint = this.makeElement("div",{'textAlign':'right','paddingRight':'3px','paddingTop':'2px'},{'innerHTML':dayPoint.day});
									el.appendChild(numberPoint);
								}
								else
								{
									var numberPoint = this.makeElement("div",{'textAlign':'right','paddingRight':'3px','paddingTop':'2px','textDecoration':'line-through'},{'innerHTML':dayPoint.day});
									el.appendChild(numberPoint);	
								}
								
								incrementalDay++;								
							}						
						} 
						catch ( invalidDateException )
						{
							//console.log(invalidDateException.message);
							
							majorDiv.appendChild(this.makeElement("div",{'position':'absolute','left':left+"px",'top':top+"px",'backgroundColor':'#dedede'},{'className':'cal_left','innerHTML':''}));
						}
					}
					
					left = left + 26;
						
				}
				
				
				
					
				left = 2;
				top = top + 19;
				
			}
			
		}
		
		this.makeElement = function( type, style, directs )
		{
			var d = document.createElement(type);
			if (style != null ) for ( var p in style ) { try { d.style[p]=style[p];  } catch(e){} }
			if ( directs != null ) for ( var j in directs ) { try {  d[j]=directs[j]  } catch(e1){} };
			return d;
		}
		
		this.findPosX = function(obj){
		var curleft = 0;
		if (obj.offsetParent){while (obj.offsetParent){curleft += obj.offsetLeft;obj = obj.offsetParent;}} else if (obj.x) curleft += obj.x;
		return curleft;
		}
	
		this.findPosY = function (obj){
			var curtop = 0;
			if (obj.offsetParent){ while (obj.offsetParent) { curtop += obj.offsetTop; obj = obj.offsetParent; } } else if (obj.y) curtop += obj.y;
			return curtop;
		}
		
		this.addHeaderMonthName = function()
		{	
			var header_element = this.makeElement("div",{'textAlign':'center','position':'absolute','left':'20px','width':'140px','height':'20px','top':'6px'},{'className':'cal_header','innerHTML':this.CalInformation['monthLong'] + " " + this.CalInformation['year']});
			
			
			document.getElementById(this.calRef).appendChild(header_element);
		}	
		
		
		this.addNavSymbols = function()
		{
			var prev = this.makeElement("img",{'cursor':'pointer','position':'absolute','left':'10px','top':'6px'},{'src':'/hotels/resource/images/calendar/prev.jpg'});
			
			prev.onmouseout=function(){ calACTIVE=false; }
			prev.onmouseover=function(){ calACTIVE=true; }
			
			prev.cal = new Array(this.CalInformation['prevdsay'],this.CalInformation['prevmonth'],this.CalInformation['prevyear']);
			prev.calRef = this.calRef;
			prev.calType = this.calType;
			prev.onmousedown=function()
			{
				var cal = new Calendar();
				cal.setCal(this.calRef);
				cal.setType(this.calType);
				cal.getCalInformation(this.cal[0],this.cal[1],this.cal[2]);
				cal.generateHTMLCore(cal.CalInformation);
				cal.addHeaderMonthName();
				cal.addNavSymbols();	
			}
			
			
			document.getElementById(this.calRef).appendChild(prev);
			var next = this.makeElement("img",{'cursor':'pointer','position':'absolute','left':'160px','top':'6px'},{'src':'/hotels/resource/images/calendar/next.jpg'});
			
			next.onmouseout=function(){ calACTIVE=false; }
			next.onmouseover=function(){ calACTIVE=true; }
			
			next.cal = new Array(this.CalInformation['nextday'],this.CalInformation['nextmonth'],this.CalInformation['nextyear']);
			next.isNextDateOptionAvailable = this.CalInformation['isNextDateOptionAvailable'] ; 
			next.calRef = this.calRef;
			next.calType = this.calType;
			next.onmousedown=function()
			{
				if ( this.isNextDateOptionAvailable ) 
				{
				
					var cal = new Calendar();
					cal.setCal(this.calRef);
					cal.setType(this.calType);
					cal.getCalInformation(this.cal[0],this.cal[1],this.cal[2]);
					cal.generateHTMLCore(cal.CalInformation);
					cal.addHeaderMonthName();
					cal.addNavSymbols();	
				}
				else
				{
					notice.create("You cannot book that far in the future","Date Error","Close|null");
				}
			}
			
			document.getElementById(this.calRef).appendChild(next);
		}
		
		
	}
