var KeyMilestone = 10;
var OutputType = 'date';
/*
	Name: conditionalClear
	Notes: If the value of the object given by f is the same as that given by the argument v, clear the
			value of f, and change its format to that of the format class given by fmt. 
			This function is useful for changing the appearance of a text entry field when the user clicks on it,
			by calling it with the text field, the initial value, and a new format, if any.
*/
function conditionalClear(f, v, fmt)
{
	if(f.value == v)
	{
		f.value = "";
		f.setAttribute("class", fmt);
	}
}

/*
	Name: contactValidator
	Notes: Validate the contact information from a form to make sure it's safe
*/
function contactValidator(f)
{
	var n = f.name.value;
	var e = f.email.value;
	var s = f.subject.value;
	var m = f.feedback.value;
	
	var Params = "?n=" + n + "&e=" + e + "&s=" + s + "&m=" + m;
	getRequest("send.php", Params, writeHTML, 'bgform');
}

/*
	Name: IsDigit
	Notes: Characterize an input character as a digit (return true) or not (return false)
*/
function isDigit(c)
{	
	if((c >= '0') && c <= '9')
		return true;
	return false;
}

/*
	Name: findMonth
	Notes: Given an input string, see if we can parse a month out of it. If not, return -1. If so, return 
			the month: 1=January, 12=December
*/
function findMonth(bdl)
{
	mSearch = new Array("jan", "fe", "mar", "ap", "may", "jun", 
					   	"jul", "au", "se", "oc", "no", "de");
	var i = 0;
	for(i=0; i<12; i++)
	{
		if(bdl.search(mSearch[i]) > -1)
			return i + 1;
	}
	return 0;
}

function showUpcoming(o, r)
{
	var currentTime = new Date();
	var curMonth = currentTime.getMonth() + 1;
	var Params = "?cd=" + currentTime.getDate() + "&cy=" + currentTime.getFullYear() + "&cm=" + curMonth + "&cr=" + r;
	getRequest("upcoming.php", Params, writeHTML, o);
}

/*
	Name: findDate
	Notes: f is the input field, o is the output div, for which we're planning to get the inner html
*/
function findDate(n, o)
{
	var f = document.getElementById(n);
	var bd = f.value;
	if(bd.length >= 6 && bd != 'enter your birthdate')		// can't think of any date format less than 6 characters
	{
		// First, see how many different numbers we have, separated by any non-numeric character
		var i = 0;
		var n = 0;					// The count of numbers in the string
		var bdl = bd.toLowerCase();
		Nums = new Array(0, 0, 0);	// The numbers we'll parse out of the string
		Digits = new Array(0, 0, 0);
	
		for(i=0; i<=bdl.length; i++)
		{
			if(n >= 3)				// we're done if we've snagged 3 numbers out
				break;
			var c = bdl.charAt(i);
			if(isDigit(c))
			{
				Digits[n] += 1;
				Nums[n] *= 10;
				Nums[n] += c - '0';
			}
			else
			{
				if(Nums[n] != 0)
					n++;
			}
		}
	
		if(Nums[2] > 0)
			n = 3;
		var M, D, Y;
		var YDig = 0;
		switch(n)
		{
			default:
			case 0:
			case 1:		// Not enough digits to make a date
				return;
			break;
			
			case 2:		// Numeric date, year... is there an alpha month too?
				M = findMonth(bdl);
				if(0 == M)
				{
					return;				// Couldn't find a month, so we're done
				}
	
				D = Nums[0];
				Y = Nums[1];
				YDig = Digits[1];
			break;
			
			case 3:		// Numeric date, month, year
				if(Nums[0] > 31)		// Assume 4-digit year at the front
				{
					Y = Nums[0];
					YDig = Digits[0];
					M = Nums[1];
					D = Nums[2];
				}
				else if(Nums[0] > 12)	// Must be a date up front, month next
				{
					D = Nums[0];
					M = Nums[1];
					Y = Nums[2];
					YDig = Digits[2];
				}
				else					// Assume it's Month/Date/Year... which is somewhat American thinking
				{
					M = Nums[0];
					D = Nums[1];
					Y = Nums[2];
					YDig = Digits[2];
				}
			break;
		}
		if(YDig < 2 || YDig == 3)	// No year digits, 1 year digit or 3 year digits are all invalid
			return;
			
		if(Y <= 20 && Y >= 19)		// We're not accommodating anyone born in 1919, 1920, 2019 or 2020 with a 2-digit
			return;					// year. Assume this is the start of a 4 digit year - 19xx or 20xx.
	
		if(Y <= 19)
			Y += 2000;				// It's a young person... we think.
		else if(Y <= 100)			// Must be a last-century person
			Y += 1900;
			
		MonthDays = new Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		if(M > 12 || M < 1)
			return;
			
		if(D > MonthDays[M] || D < 1)
			return;
	
		Months = new Array("January", "February", "March", "April", "May", "June", 
							"July", "August", "September", "October", "November", "December");
		var date = D + '';
		var year = Y + '';
		var month = M + '';
	
		var O = Months[M-1] + ' ' + date + ', ' + year;
		f.value = O;			// Output what we think the date is to the original field
		var currentTime = new Date();
		var curMonth = currentTime.getMonth() + 1;
		var Params = "?d=" + date + "&y=" + year + "&m=" + month + "&b=" + KeyMilestone + "&t=" + OutputType + "&cd=" + currentTime.getDate() + "&cy=" + currentTime.getFullYear() + "&cm=" + curMonth;
		getRequest("find_date.php", Params, updateHeader, o);
	}
	else
		updateHeader(' ', o);
}

function createXMLHttpRequest() 
{
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
 }
 
function requestDone(hreq, action, id)
{
	if (hreq.readyState == 4) 
	{
		if (hreq.status == 200)
			action(hreq.responseText, id);
		else 
			alert('There was a problem with the request: ' + hreq.status + hreq.responseText);
	}
}

function postRequest(url, parameters, action, id) 
{
	var http_request = createXMLHttpRequest();
	if(!http_request)
		window.alert("Could not createXMLHttpRequest");
	http_request.onreadystatechange = function() {requestDone(http_request, action, id);}
//	window.alert("Filling Models 2" + url + " " + parameters);
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function getRequest(url, parameters, action, id) 
{
	var http_request = createXMLHttpRequest();
	if (!http_request) 
		window.alert('Cannot create XMLHTTP instance');
	else
	{
		http_request.onreadystatechange = function() {requestDone(http_request, action, id);}
		http_request.open('GET', url + parameters, true);
		http_request.send(null);
	}
}

function writeHTML(response, id)
{
	document.getElementById(id).innerHTML = response;
}

/*
	Name:	loadPage
	Notes:	Make a request to the page loader to fetch the inner page. This allows the active part of the
			application to act independently of the header, sidebar and footer.
*/
function loadPage(id)
{
	var Params = "?pid=" + id;
	getRequest("pages.php", Params, writeHTML, 'page_contents');
}

function changeBase(newBase, n, o)
{
	KeyMilestone = newBase;
	findDate(n, o);
}

function toggleOutput(n, o)
{
	if(OutputType == 'days')
		OutputType = 'date';
	else
		OutputType = 'days';
	findDate(n, o);
}
//
//	Switch the contents of a box by making a database request
//
function switchBoxes(itm, box)
{
	var params = "?item=" + itm + "&box=";

	var sp = box.split("_");
	var t = box;
	if(sp.length > 1)
		t = sp[1];
	params += t;
	getRequest("store.php", params, writeHTML, t);
//	var Params = "?pid=" + id;
//	getRequest(".php", Params, writeHTML, 'page_contents');
//	alert("Switching boxes");
}

function fillFromForm(fo)
{
	var i;
	var getstr = '';
	var E = fo.getElementsByTagName("input");		// Elements
	var S = fo.getElementsByTagName("select");		// Random selects
	var T = fo.getElementsByTagName("textarea");	// Textareas
//	window.alert("Elements " + E.length + " Text " + T.length);
	for (i=0; i < S.length; i++)
	{
		var el = S[i].name + "=" + S[i].options[S[i].selectedIndex].value + "&";
		getstr += el;
	}
	for (i=0; i < E.length; i++) 
	{
		if (E[i].type == "text") 
			getstr += E[i].name + "=" + E[i].value + "&";
			
		else if (E[i].type == "checkbox") 
		{
			if (E[i].checked) 
				getstr += E[i].name + "=" + E[i].value + "&";
			else 
				getstr += E[i].name + "=&";
		}
		else if (E[i].type == "radio") 
		{
			if (E[i].checked) 
				getstr += E.name + "=" + E[i].value + "&";
		}
		else if(E[i].type == "hidden")
			getstr += E[i].name + "=" + E[i].value + "&";
	}
	// Textareas
	for (i=0; i < T.length; i++) 
	{
		getstr += T[i].name + "=" + T[i].value + "&";
	}
	return getstr;
}


