// JavaScript Document

function calendarChooser( docObj, fieldname, xpos, ypos )
{
	// object that creates a calendar that allows the user to choose a date
	
	/* explaination of parameters
	
	myname 				- is used to give a callback to the instance name.
	
	docObj [optional] 		- is the DOM element id that the calendar will be inserted into
								can be left as a null string for entry into the body tag
	
	eventfunction			- the name of the external function that is called 
							  (and passed this object's instance name as a parameter)
							  when a date is chosen
	
	*/
	
	
	// define properties for this object

	
	var tempdate = new Date(); // use today's date as default for view
	
	//get position attributes
	this.xpos = xpos;
	this.ypos = ypos;
	// set up variables to store the working date
	this.myyear 		= tempdate.getFullYear();
	this.mymonth 		= tempdate.getMonth();
	this.myday	 	= tempdate.getDay();
	// set up variables to store todays date
	this.todaysyear 	= tempdate.getFullYear();
	this.todaysmonth 	= tempdate.getMonth();
	this.todaysday	 	= tempdate.getDate();
	
	this.displayed 		= false;
	this.chosenObj 		= "";
	this.chosenClass 	= "date";
	this.formfield          = fieldname;
	this.docObj             = docObj;

	
	
	var months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
	var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	
	// define functions for this object
	this.displayme	    = displayme;
	this.nextMonth      = nextMonth;
	this.prevMonth      = prevMonth;
	this.nextYear	    = nextYear;
	this.prevYear	    = prevYear;
	this.chooseDate	    = chooseDate;
	this.update	    = update;
	this.remove	    = remove;


	function displayme()
	{
		//alert("got here");
		// function that presents the object in the web page
		
		this.myDomObj = document.createElement("div");
		this.myDomObj.style.position = "absolute";
		this.myDomObj.style.zIndex = "6000";
		this.myDomObj.style.offsetLeft = this.xpos+" px";
		this.myDomObj.style.offsetTop = this.ypos+" px";
		var myInsertPoint = document.getElementById(this.docObj);
		myInsertPoint.parentNode.style.zIndex = "3000";
		if (myInsertPoint) {
			myInsertPoint.appendChild(this.myDomObj);
		} else {
			document.body.appendChild(this.myDomObj);
		}
		this.myDomObj.className = "calendarbig";
		this.update(); // update populates the existing dom element
		this.displayed = true;

		
	}
	
	function remove()
	{
		if (this.displayed) {
			var myInsertPoint = document.getElementById(this.docObj);
			myInsertPoint.parentNode.style.zIndex = "1";
			this.myDomObj.parentNode.removeChild(this.myDomObj);
			this.displayed = false;
		} else {
			alert("already removed");
		}
	}
	
	function nextMonth()
	{
		this.mymonth += 1;
		if (this.mymonth > 11) {
			this.mymonth = 0;
			this.myyear += 1;
		}
		this.update();
	}
	
	function prevMonth()
	{
		this.mymonth -= 1;
		if (this.mymonth < 0) {
			this.myyear -=1;
			this.mymonth = 11;
		}
		this.update();
	}
	
	function nextYear()
	{
	 	this.myyear +=1;
		this.update();
	}
	
	function prevYear()
	{
	 	this.myyear -=1;
		this.update();
	}
	
	function chooseDate(DocObj,chosendate)
	{
		// reset the last chosen date to display normally (turn off the highlight)
		
		
		// set up the highlight for the new chosen dat
		DocObj.className 	= "dateChosen";
		this.mychosendate 	= chosendate;
		this.mychosenmonth 	= this.mymonth;
		this.mychosenyear	= this.myyear;
	 	//alert("date chosen "+chosendate+" "+months[this.mymonth]+" "+this.myyear);
                document.getElementById(this.formfield).value    = chosendate+"/"+(this.mymonth+1)+"/"+this.myyear;
                this.remove();
                var calIcon = document.createElement("img");
                calIcon.src = "images/calendar_icon.png";
                calIcon.alt ="calendar";
                calIcon.style.border = "none";
                calIcon.id = "calicon";
                calIcon.onclick = function () { popupCal() };

                document.getElementById(this.docObj).appendChild(mylastcalicon);
	}
	

	function update()
	{
                //alert("update");
		// first check to see if there is any existing content in the DOM object ... if so remove it
		if (this.myDomObj.hasChildNodes())
		{
                        
			//this.myDomObj.childNodes[0].removeNode(this.myDomObj.childNodes[0].childNodes[0]);
			//this.myDomObj.childNodes[0].style.backgroundColor = "#FF0000";
			var myremoval = this.myDomObj.childNodes[0];
			//alert("got here nodevalue = "+myremoval.nodeType);
			this.myDomObj.removeChild(myremoval,true);

		}

		//define some local variables
		var tempdate = new Date();
		
		// get full date info for the date provided (includes day of week!)
		tempdate.setFullYear(this.myyear,this.mymonth,1);

		
		var themonth = months[tempdate.getMonth()];
		//alert("themonth: "+themonth);
		var theyear = tempdate.getFullYear();
		
		var startdate = new Date();
		startdate.setFullYear(tempdate.getFullYear(),tempdate.getMonth(),1);
		var startdayofweeknum = startdate.getDay();
		
		if (startdayofweeknum == 0) {
		// make sunday be the last day on the table for aesthetic reasons
		startdayofweeknum = 7;
		}
		
		//build calendar html
		var mycaltable 				= document.createElement("table");
		mycaltable.border 			= 2;
		mycaltable.cellPadding 		        = 0;
		mycaltable.cellSpacing		        = 0;
		mycaltable.borderColor		        = "#CCCCCC";
		
		var mytablebody				= document.createElement("tbody");
		mycaltable.appendChild(mytablebody);

		// build year heading and buttons
		var mytablerow				= document.createElement("tr");
		mytablebody.appendChild(mytablerow);
		
		var mytablecell				= document.createElement("td");
		
		mytablecell.className		= "calbuttons";
		var myobj = this;
		var mytemptext				= document.createTextNode("<");
		var mylink					= document.createElement("a");
		mylink.href					= "#";
		mylink.style.color 			= "#fff";
		mylink.onclick			= function () {
												//myobj = eval(myname);
												myobj.prevYear();
												return false;
											}
		mylink.appendChild(mytemptext);
		mytablecell.appendChild(mylink);
		mytablecell.appendChild(mylink);
		mytablerow.appendChild(mytablecell);
		
		var mytablecell				= document.createElement("td");
		mytablecell.colSpan			= "5";
		mytablecell.align			= "center";
		var mytemptext				= document.createTextNode(this.myyear);
		mytablerow.appendChild(mytablecell);
		mytablecell.appendChild(mytemptext);
		
		var mytablecell				= document.createElement("td");
		mytablecell.className		= "calbuttons";
		
		var mytemptext				= document.createTextNode(">");
		var mylink					= document.createElement("a");
		mylink.href					= "#";
		mylink.style.color 			= "#fff";
		mylink.onclick			= function () {
												//myobj = eval(myname);
												myobj.nextYear();
												return false;
											}
		mylink.appendChild(mytemptext);
		mytablecell.appendChild(mylink);
		//mytablecell.appendChild(mytemptext);
		mytablerow.appendChild(mytablecell);
		
		
		// build month heading and buttons
		var mytablerow				= document.createElement("tr");
		mytablebody.appendChild(mytablerow);
		var mytablecell				= document.createElement("td");
		mytablecell.className		= "calbuttons";
		
		var mytemptext				= document.createTextNode("<");
		var mylink					= document.createElement("a");
		mylink.href					= "#";
		mylink.style.color 			= "#fff";
		mylink.onclick			= function () {
												//myobj = eval(myname);
												myobj.prevMonth();
												return false;
											}
		mylink.appendChild(mytemptext);
		mytablecell.appendChild(mylink);
		mytablerow.appendChild(mytablecell);
		
		var mytablecell				= document.createElement("td");
		mytablecell.colSpan			= "5";
		mytablecell.align			= "center";
		var mytemptext				= document.createTextNode(themonth);
		mytablerow.appendChild(mytablecell);
		mytablecell.appendChild(mytemptext);
		
		var mytablecell				= document.createElement("td");
		mytablecell.className		= "calbuttons";
		/*mytablecell.onclick			= function () {
												//myobj = eval(myname);
												myobj.nextMonth();
											}
		*/
		var mytemptext				= document.createTextNode(">");
		var mylink					= document.createElement("a");
		mylink.href					= "#";
		mylink.style.color 			= "#fff";
		mylink.onclick			= function () {
												//myobj = eval(myname);
												myobj.nextMonth();
												return false;
											}
		mylink.appendChild(mytemptext);
		mytablecell.appendChild(mylink);
		//mytablecell.appendChild(mytemptext);
		mytablerow.appendChild(mytablecell);
		
		// build day headings
		var mytablerow				= document.createElement("tr");
		mytablebody.appendChild(mytablerow);
		
		var dayheadings = new Array("M","T","W","T","F","S","S");
		
		for (i = 0; i < dayheadings.length; i++) {
			var mytablecell				= document.createElement("td");
			mytablecell.className 		= "dayheadings";
			var mytemptext				= document.createTextNode(dayheadings[i]);
			mytablecell.appendChild(mytemptext);
			mytablerow.appendChild(mytablecell);
		}
		
		// now add dates into the table
		var posx = 0;
		var posy = 0;
		var datecount = 1;
		// build blanks up until the start of the numbers
		
		var mytablerow				= document.createElement("tr");
		mytablebody.appendChild(mytablerow);
		for(i = 1; i < startdayofweeknum ; i++) {
			var mytablecell				= document.createElement("td");
			if (posx > 4) { // weekend date
				mytablecell.className 		= "dateweekend";
			} else {
				mytablecell.className 		= "date";
			}
			var mytemptext				= document.createTextNode(" ");
			mytablerow.appendChild(mytablecell);
			mytablecell.appendChild(mytemptext);
			posx = posx + 1;
		}// end for
		
		
		var validdate = true;
		var rowstartedflag = true;
		var calendarnotes = "";
		
		for(i = startdayofweeknum; validdate == true; i++) {
		
			var mytablecell						= document.createElement("td");
			
			
			if (posx > 4) 
			{// weekend date
					mytablecell.className 		= "dateweekend";
			} else {// normal date
					mytablecell.className 		= "date";
			}
			
			if ((datecount == this.todaysday) && (this.mymonth == this.todaysmonth) && (this.myyear == this.todaysyear)) 
			{
				// mark todays date
				mytablecell.className 			= "datetoday";
				//alert("i = "+i+" this.todaysday="+this.todaysday+" datecount="+datecount);
			} 
			if (this.dateischosen) 
			{
				if ((datecount == this.mychosendate) && (this.mymonth == this.mychosenmonth) && (this.myyear == this.mychosenyear)) {
								// mark todays date
								//alert("i = "+i+" this.chosendate="+this.mychosendate+" datecount="+datecount);
					mytablecell.className 		= "dateChosen";
				} 
			}
			// add event to capture pressing on a date
			//mytablecell.datecount				= datecount;
			//mytablecell.onclick			= function () 
					/*						{
												
                                                                                                //myobj = eval(myname);
												myobj.chooseDate(this,this.datecount);
											}
											*/
											
			var mytemptext				= document.createTextNode(datecount);								
			// new code
			var mylink					= document.createElement("a");
			mylink.datecount			= datecount;
			mylink.href 				= "#";
			mylink.style.color 			= "#fff";
			mylink.onclick				= function () 
											{
												myobj.chooseDate(this,this.datecount);
												return false;
											}
			mylink.appendChild(mytemptext);
			// end of new code
			//var mytemptext				= document.createTextNode(datecount);
			mytablerow.appendChild(mytablecell);
			//mytablecell.appendChild(mytemptext);
			
			mytablecell.appendChild(mylink);
			
			datecount += 1;
			posx += 1;
			if (posx > 6)
			{
				var mytablerow				= document.createElement("tr");
				mytablebody.appendChild(mytablerow);
				posx = 0;
				posy += 1;
				rowstartedflag = false;
			}
			
			
			var testdate = new Date();
			
			testdate.setFullYear(tempdate.getFullYear(),tempdate.getMonth(),datecount);
			checkdate = testdate.getDate();
			if (checkdate != datecount)
			{
				validdate = false;
			}
			
			
		}// end for	

		if (posx > 0) { // only create row if it needs it
			for (i = posx; i < 7; i++) {
				
					var mytablecell				= document.createElement("td");
					if (i < 5) {
						// normal date
						mytablecell.className 		= "date";
					} else {
						// weekend date
						mytablecell.className 		= "dateweekend";
					}
					
					var mytemptext				= document.createTextNode(" ");
					mytablerow.appendChild(mytablecell);
					mytablecell.appendChild(mytemptext);
			}
		}
		
		
		// insert the table into the dom object
		this.myDomObj.appendChild(mycaltable);
	} // end of update method
}