//===============================
//	Instructions
//	1. 	Install the script at the top of the Body:
//		response.write "<script  src="/resource/js/calendar.js"></script>"
// 	2.	Include the calendar script file at the top of the page
//		<!-- #include virtual="/resource/inc/calendar.inc" --> 
//	3.	Call from VBScript
//			set obj = new Calendar
//				obj.CreateCalendar strDate, true, false
//				strOutput = strOutput & obj.Html
//			Set obj = nothing

//===============================
// Procedure	: MOVE MONTH	
// Purpose		: Move one month forward/back on the calendar
// In			: calendarID = ID for calendar clicked
//				: direction = positive for foward; negative for backward
// Out			: Nothing - sub to change calendar
// Note			: Calls insertNewMonth for xmlResponse request
//				: Name attibute has current DateCode
//===============================
function moveMonth(calendarID, direction)
{	var el, dc, dte
	var attr = "name"							//seems to need to be a predefined attribute for IE, anything in FF 
	var id = calendarID;
	el = document.getElementById('dc'+id);		//no attribute in initial html - blank initially	
	dc = el.getAttribute(attr);
		dc = nextDC(dc,direction);
		el.setAttribute(attr, dc);
		dte = DCMonthYear(dc)
		el.innerHTML = dte
	insertNewMonth(id, dc);
}
//===============================
// Procedure	: EXPOSE OUTPUT	
// Purpose		: Provide visual and code access for a clicked date
// In			: dateCode - valid dateCode in format 20090101
//				: calendarID = ID for calendar clicked 
// Out			: dateCode = newly clicked datecode value
// Note			: 
//===============================
function exposeOutput(calendarID, dateCode)
{	var dc = dateCode, id = calendarID;
	var elOld, elNew;
	var attr = "dc";
	var strDate = dcDecode(dc);
	var el = document.getElementById("txt"+id);		// get stored value from output txt field
	var dcOld = el.getAttribute(attr);
	if (dcOld!=null)
	{	try
		{	elOld = document.getElementById("cal"+id+dcOld);
			elOld.style.border = "2px solid #ffffff";
		}	
		catch (e) {}
	}
	el.setAttribute(attr, dc);
	if (String(strDate).substring(0,3)=="NaN")
		{	el.value = "";
		}
		else
		{	el.value = strDate;
			elNew = document.getElementById("cal"+id+dc);
			elNew.style.border = "2px solid #000000";
		}
}







//===============================
// Procedure	: GET REQUEST STRING
// Purpose		: Walk through fields on form to build up a request string
// In			: currentForm = reference to form node
// Out			: URI encoded query string
//===============================
function getRequestString(currentForm)
{	var arr = new Array();
	var frm = currentForm;
	for (var i=0; i<frm.elements.length; i++)
	{	var fld = frm.elements[i];
		switch (fld.type)
		{	case "button":
			case "submit":
			case "reset":
				break;
			case "checkbox":
			case "radio":
				if (!fld.checked)
				{	break;
				}
			case "text":
			case "hidden":
			case "password":
				arr.push(encodeValues(fld.name, fld.value));
				break;
			default:
				switch(fld.tagName.toLowerCase())
				{	case "select":
						arr.push(encodeValues(fld.name, fld.options[fld.selectedIndex].value));
						break;
					default:
						arr.push(encodeValues(fld.name, fld.value));
				}
		}
	}	
	return arr.join("&");
}
//===============================
// Procedure	: ENCODE VALUES
// Purpose		: URI encode a name value pair for query string
// In			: Name = name of value pair
//				: Value = value for value pair
// Out			: encoded value, joined with equals sign
//===============================
function encodeValues(Name, Value)
{	var str = encodeURIComponent(Name);
	str += "=";
	str += encodeURIComponent(Value);
	return str;
}
//===============================
function insertNewMonth(currentID, dateCode)
{	var xmlResponse = "Initial value";
	var xmlhttp=createXMLHTTP();
	var url = '/resource/ajax/calendar.asp?id='+currentID+'&dc='+dateCode;
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null); 
	if (xmlhttp.status == 200) 
	{	replaceContent('c'+currentID, xmlhttp.responseText);
	}
}
//===============================
function dcDecode(dateCode)
{	months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	x = dateCode;
	y = String(x).substring(0,4);
	m = parseFloat(String(x).substring(4,6));
	d = parseFloat(String(x).substring(6,8));
	x = d + ' ' + months[m-1] + ' ' + y;
	return x;
}
//===============================
function dcEncode(dateString)
{	var x = dateString;
	var dte = new Date(dateString);
	y = dte.getFullYear;
	m = dte.getMonth;
	d = dte.getDate;
	x = y + zeroPad(m,2) + zeroPad(d,2);
alert(x);
	return x;
}



//===============================
function DCMonthYear(dateCode)
{	months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	x = dateCode;
	y = String(x).substring(0,4);
	m = parseFloat(String(x).substring(4,6));
	x = months[m-1] + ' ' + y;
	return x;
}
//===============================
function nextDC(dc,direction)
{	x = dc;
	y = String(x).substring(0,4);
	m = String(x).substring(4,6);
	d = String(x).substring(6,8);
	m = parseFloat(m) + (1*direction);
	y = parseInt(y);
	if (m==13){m=1;y=y+1;}
	if(m==0){m=12;y=y-1;}
	m = zeroPad(m, 2);
	y+='';
	x = y + m + d;
	return x;
}


//===============================
function zeroPad(n, digits) {
	n = n.toString();
	while (n.length < digits) {
		n = '0' + n;
	}
	return n;
}
//===============================
function replaceContent(tag,content)	{
	if (document.getElementById && !document.all)
	{	rng = document.createRange();
		el = document.getElementById(tag);
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes())
			el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}
	else
	{	document.getElementById(tag).innerHTML = content;
	}
}


//===============================
function createXMLHTTP()	{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript Conditional compilation: old IE versions and security blocked creation of the objects.
	try 
	{	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{	try 
		{	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  		} 
		catch (E) 
		{	xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{	try 
		{	xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{	xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) 
	{	try 
		{	xmlhttp = window.createRequest();
		} 
		catch (e) 
		{	xmlhttp=false;
		}
	}
	return xmlhttp
}






