/** 
 *  DHTML Calendar
 */

function stopEventPropagation (e) {
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
}

function findPosX(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;
}

function findPosY(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;
}

/** 
 * IE Compatibility functions
 */
function createEventListener ( obj, handler, func_ref ) {
	if (obj.attachEvent) {
		obj.attachEvent('on'+handler, func_ref);
	} else {
		obj.addEventListener( handler, func_ref, false );
	}
}

/**
 * DIV POPUP
 */
var t_popup_div = document.createElement('div');
t_popup_div.setAttribute('style','display:none; z-index:500');
t_popup_div.setAttribute('onmousedown', 'stopEventPropagation(event)');

function DivPopupClass () {
	if (t_popup_div.attachEvent) {
		t_popup_div.attachEvent('onmousedown', stopEventPropagation);
	} else {
		t_popup_div.addEventListener('mousedown', stopEventPropagation, false);
	}

	// append a child as a placeholder
	t_popup_div.appendChild(document.createElement('div'));
	if (document.all) {
		t_popup_div.appendChild(document.createElement('iframe'));
	}


	this.setContent = function ( content ) {
		// content div
		var c = t_popup_div.firstChild;
		
		// content 
		c.innerHTML = ''; //clear it first
		c.style.position = 'absolute';
		c.style.left = '0px';
		c.style.top = '0px';
		c.style.zIndex = '1000';
	
		c.style.backgroundColor = '#ffffff';


		/** mozilla hacks
		// add it to the document to figure out the size
		var test = document.createElement('table');
		test.innerHTML = '<tr></td>content</td></tr>';
		document.body.appendChild( test );
		alert(test.offsetWidth);
		*/
	
		c.innerHTML = "<div>" + content + "</div>";

		/*
		alert(c.firstChild.offsetWidth);	
		*/

		// iframe hack for 'select over everything' ie bug
		if (document.all) {
			var iframe_hack = c.nextSibling;
			iframe_hack.src = '/blank.htm';
			iframe_hack.style.position = 'absolute';
			iframe_hack.style.top = '0px';
			iframe_hack.style.left = '0px';
			iframe_hack.style.zIndex = '900';
		}
	}

	this.show = function (obj) {
		// get the current width of the body
		var body_width = document.body.offsetWidth;

		// ad the div to the body
		document.body.appendChild( t_popup_div );
	
		t_popup_div.style.width = t_popup_div.firstChild.offsetWidth;

		// position it to the right of the object
		t_popup_div.style.position = 'absolute';
		t_popup_div.style.left = findPosX(obj) + obj.offsetWidth + 20 + 'px';
		t_popup_div.style.top = findPosY(obj) + 'px';

		// display the div
		t_popup_div.style.display = 'block';
		
		// size the iframe correctly
		if (document.all) {
			var iframe_hack = t_popup_div.firstChild.nextSibling;
			iframe_hack.style.width = t_popup_div.firstChild.offsetWidth;
			iframe_hack.style.height = t_popup_div.firstChild.offsetHeight;
		}
	
		// if off the page, fix it
		if ((findPosX(obj) + 220) > body_width) {
			t_popup_div.style.left = parseInt( t_popup_div.style.left ) - 220 + 'px';
		}

		if (document.addEventListener) {
			document.addEventListener('mousedown', this.hide, false);
		} else if ( document.attachEvent ) {
			document.attachEvent('onmousedown', this.hide);
		}
	}

	this.hide = function () {
		t_popup_div.style.display = 'none';
	}

	//Used in /shared/examview/p_rating.tpl!
	this.delete_p = function () {
		t_popup_div = document.createElement('div');
		t_popup_div.setAttribute('style','display:none; z-index:500');
		t_popup_div.setAttribute('onmousedown', 'stopEventPropagation(event)');

	}

}
var DivPopup = new DivPopupClass();


/**
 * DATE PICKER
 */
function DatePicker () {

	this.obj;
	this.months = new Array('January','February','March','April','May','June','July','August','September', 'October','November','December');

	this.getDaysInMonth = function (iMonth, iYear) {
		var dPrevDate = new Date(iYear, (parseInt(iMonth)+1), 0);
		//alert("new Date("+iYear+", "+(iMonth+1)+", 0); getDate("+dPrevDate.getMonth()+")");
		return dPrevDate.getDate();
	}

	this.buildCal = function (iYear, iMonth, iDayStyle) {
		var aMonth = new Array();
		aMonth[0] = new Array(7);
		aMonth[1] = new Array(7);
		aMonth[2] = new Array(7);
		aMonth[3] = new Array(7);
		aMonth[4] = new Array(7);
		aMonth[5] = new Array(7);
		aMonth[6] = new Array(7);
		var dCalDate = new Date(iYear, iMonth, 1);
		var iDayOfFirst = dCalDate.getDay();
		var iDaysInMonth = this.getDaysInMonth(iMonth, iYear);
		var iVarDate = 1;
		var i, d, w;
		if (iDayStyle == 2) {
			aMonth[0][0] = "Sunday";
			aMonth[0][1] = "Monday";
			aMonth[0][2] = "Tuesday";
			aMonth[0][3] = "Wednesday";
			aMonth[0][4] = "Thursday";
			aMonth[0][5] = "Friday";
			aMonth[0][6] = "Saturday";
		} else if (iDayStyle == 1) {
			aMonth[0][0] = "Sun";
			aMonth[0][1] = "Mon";
			aMonth[0][2] = "Tue";
			aMonth[0][3] = "Wed";
			aMonth[0][4] = "Thu";
			aMonth[0][5] = "Fri";
			aMonth[0][6] = "Sat";
		} else {
			aMonth[0][0] = "Su";
			aMonth[0][1] = "Mo";
			aMonth[0][2] = "Tu";
			aMonth[0][3] = "We";
			aMonth[0][4] = "Th";
			aMonth[0][5] = "Fr";
			aMonth[0][6] = "Sa";
		}
		for (d = iDayOfFirst; d < 7; d++) {
			aMonth[1][d] = iVarDate;
			iVarDate++;
		}
		for (w = 2; w < 7; w++) {
			for (d = 0; d < 7; d++) {
				if (iVarDate <= iDaysInMonth) {
					aMonth[w][d] = iVarDate;
					iVarDate++;
			  	}
		   	}
		}
		return aMonth;
	}


	this.drawCal = function (iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight, iDayStyle) {
		var myMonth;
		myMonth = this.buildCal(iYear, iMonth, iDayStyle);

		var cur_date = new Date();

		var c_html = '';


		c_html += "<table class='calendar' border='0' cellspacing='1' style='border: solid 1px gray'>";
		c_html += "  <tr>";
		c_html += "    <th nowrap colspan='7'>";

		c_html += "      <span onClick='date_picker.updateCalendar("+((iMonth==0)?(iYear-1):iYear)+", "+((iMonth==0)?11:(iMonth-1))+");'>&lt;</span>";
		c_html += "      <select name='month' onchange='date_picker.updateCalendar(this.nextSibling.nextSibling.value, this.value)'>";
		for ( var i=0; i<12; i++ ) {
			c_html += "     <option value='" + (i) + "'"; 
			if (iMonth == i) c_html += " selected='selected'";
			c_html += ">" + this.months[i] + "</option>";
		}
		c_html += "      </select>";

		c_html += "      <select name='year' onchange='date_picker.updateCalendar(this.value, this.previousSibling.previousSibling.value)'>";
		c_html += "        <option value='"+(iYear-1)+"'>" + (iYear-1) + "</option>";
		c_html += "        <option value='"+(iYear)+"' selected='selected'>" + iYear+ "</option>";
		c_html += "        <option value='"+(parseInt(iYear)+1)+"'>" + (parseInt(iYear)+1) + "</option>";
		c_html += "      </select>";
		c_html += "      <span onClick='date_picker.updateCalendar("+((iMonth==11)?(iYear+1):iYear)+", "+((iMonth==11)?0:(iMonth+1))+");'>&gt;</span>";

		c_html += "    </th>\n";
		c_html += "  </tr>";
		c_html += "  <tr>";
		c_html += "    <th>" + myMonth[0][0] + "</th>";
		c_html += "    <th>" + myMonth[0][1] + "</th>";
		c_html += "    <th>" + myMonth[0][2] + "</th>";
		c_html += "    <th>" + myMonth[0][3] + "</th>";
		c_html += "    <th>" + myMonth[0][4] + "</th>";
		c_html += "    <th>" + myMonth[0][5] + "</th>";
		c_html += "    <th>" + myMonth[0][6] + "</th>";
		c_html += "  </tr>\n";
		for (w = 1; w < 7; w++) {
			c_html += "  <tr>";
			for (d = 0; d < 7; d++) {
				if (!isNaN(myMonth[w][d])) {
					c_html += "    <td onmouseover='date_picker.toggleColor(this)' \n";
					c_html += "      onmouseout='date_picker.toggleColor(this)'";
					if (iYear == cur_date.getFullYear() && iMonth == cur_date.getMonth() && myMonth[w][d] == cur_date.getDate()) c_html += " style='background: #cdf' ";
					c_html += "      onclick='date_picker.toggleColor(this); date_picker.setSelectedDay("+iYear+","+iMonth+","+myMonth[w][d]+")'>\n";
					c_html += myMonth[w][d];
					c_html += "    </td>";
				} else {
					c_html += "<td></td>";
				}
			}
			c_html += "  </tr>\n";
		}
		c_html += "</table>";

		return c_html;
	}


	this.prompt = function( obj ) {
		this.obj = obj;
		cur_date = new Date();
		DivPopup.setContent( this.drawCal(cur_date.getFullYear(),cur_date.getMonth()) );
		DivPopup.show(obj);
	}


	this.updateCalendar = function ( iYear, iMonth ) {
		DivPopup.setContent( this.drawCal( iYear, iMonth ) );
	}


	this.setSelectedDay = function (iYear,iMonth,iDay) {
		this.obj.value = (iMonth + 1) + '/' + iDay + '/' + iYear;
		
		// if we can run the onchange on it
		if (this.obj.onchange) {
			this.obj.onchange();
		}
		DivPopup.hide();
	}

	this.toggleColor = function (myTd) {
		if (myTd.style.color == "red") {
			myTd.style.color = "black";
		} else {
			myTd.style.color = "red";
		}
	}

	this.addStyle = function () {
		var myStyle = document.createElement('style');
		myStyle.innerHTML = c_html;

		document.appendChild( myStyle );
	}
}

var date_picker = new DatePicker;

/**
 * INPUT MODIFIER
 */
function InputModifier () {
	this.run = function () {

		// get all of the input tags
		var all_inputs = document.getElementsByTagName('input');

		for ( var i=0; i<all_inputs.length; i++ ) {
			if ( all_inputs[i].getAttribute('pick') == 'date' ) {
				this.addDatePicker( all_inputs[i] );
			}

		}
	}
	
	this.addDatePicker = function ( obj ) {
		this.addIcon ( obj, '<img src="/images/calendar.jpg" onclick="date_picker.prompt(this.parentNode.previousSibling);" />' );
	}
	
	this.addIcon = function ( obj, htmlcode ) {
		var icon_image = document.createElement('span');
		icon_image.innerHTML = htmlcode;
		
		obj.parentNode.insertBefore( icon_image, obj );
		obj.parentNode.insertBefore( obj, icon_image );
		
		icon_image.style.verticalAlign = 'center';
	}
	
	this.run();
}

createEventListener( window, 'load', InputModifier );
