function formatDate(oTextBox)
{
	// Dates entered with periods as the delimiter (mm.dd.yy) pass the .Net date validator but aren't valid in the JScript Date object
	var dateEntered = oTextBox.value.replace(/\./g, "-"); 
	dateEntered = dateEntered.replace(/\-/g, "/"); // Convert dashes to slashes
	var yearEntered = dateEntered.split("/")[2];
	dateEntered = new Date(dateEntered)
	if (dateEntered.toDateString() != "NaN")
	{
		// If the year is entered as two digits and the calculated date is more than 80 years old, add 100 years to it
		 var calculatedDate = new Date((dateEntered.getMonth() + 1) + "/" + dateEntered.getDate() + "/" + dateEntered.getFullYear());
		 if (yearEntered.length == 2 && (new Date().getFullYear() - calculatedDate.getFullYear()) > 80)
			calculatedDate = new Date((dateEntered.getMonth() + 1) + "/" + dateEntered.getDate() + "/" + (dateEntered.getFullYear() + 100));
		 oTextBox.value = (calculatedDate.getMonth() + 1) + "/" + calculatedDate.getDate() + "/" + calculatedDate.getFullYear();
	}
}

function formatNumber(oTextBox, DecimalPlaces)
{
	DecimalPlaces = parseInt(DecimalPlaces);
	var amount = oTextBox.value;
	amount -= 0;
	var scale= Math.pow(10,DecimalPlaces);
	if (! isNaN(amount))
	{
		amount = (Math.round(amount*scale))/scale + (1/scale/100) ;
		var sAmount = amount.toString();
		if (DecimalPlaces == 0)
			oTextBox.value = sAmount.substr(0,sAmount.indexOf('.') + DecimalPlaces);
		else
			oTextBox.value = sAmount.substr(0,sAmount.indexOf('.') + DecimalPlaces + 1);
	}
}
function toggleNextRowDisplay(oImage,CourseCode)
{
	if (event.srcElement.tagName.toLowerCase() == 'td')
		oImage = event.srcElement.firstChild;
	var sContractImageName = "contract.gif";
	var sExpandImageName = "expand.gif";
	var sImagePath = oImage.src.substring(0, oImage.src.lastIndexOf("/"));
	var sImageName = oImage.src.substr(oImage.src.lastIndexOf("/")+1);
	if (oImage.parentElement.parentElement.parentElement.parentElement.nextSibling.nextSibling.style.display=="none")
	{
		oImage.parentElement.parentElement.parentElement.parentElement.nextSibling.nextSibling.style.display = "";
		oImage.src = sImagePath + "/contract.gif";
		oImage.title = "Hide Details";
		oImage.parentElement.parentElement.parentElement.parentElement.nextSibling.firstChild.style.display = "none";
	}
	else
	{
		oImage.parentElement.parentElement.parentElement.parentElement.nextSibling.nextSibling.style.display = "none";
		oImage.src = sImagePath + "/expand.gif";
		oImage.title = "Show Details";
		oImage.parentElement.parentElement.parentElement.parentElement.nextSibling.firstChild.style.display = "";
	
	}
}

function getCookieData(name) 
{
	name = name.replace(/\=/g, "_");
//		alert("Getting cookie: " + name);
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;
	var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
//		alert("Cookie: " + unescape(document.cookie.substring(len,end)));
	return unescape(document.cookie.substring(len,end));
}

function setCookieData(name, value, expires, path, domain, secure) 
{
	name = name.replace(/\=/g, "_");
//		alert("Setting cookie " + name + " to " + value);
	document.cookie = name + "=" +escape(value) +
		( (expires) ? ";expires=" + expires.toGMTString() : "") +
		( (path) ? ";path=" + path : "") + 
		( (domain) ? ";domain=" + domain : "") +
		( (secure) ? ";secure" : "");
}

String.prototype.trim = function () { 
	return this.replace(/\s*/, '').replace(/\s*$/, '');
}

String.prototype.escape = function () { return window.escape(this); }

String.prototype.unescape = function() { return window.unescape(this); }

function ShowHelp(appRoot, helpFileName)
{
	window.open(appRoot + "/Help/" + helpFileName, 'Connect', 'menubar=no, toolbar=no, scrollbars=yes, resizable=yes');
}

function DisplayCalendar(controlName)
{
	var arSpans = document.getElementsByTagName("span");

	for (i=0;i<arSpans.length;i++)
	{	// Find any validators associated with controlName and hide them
		if (arSpans[i].controltovalidate == controlName && arSpans[i].id.match(/.*Validator/gi))
		{
			arSpans[i].style.display="none";
		}
	}

	var windowOptions = 'height=180,width=235,left=200,top=150'
	var textBox = document.forms[0].elements[controlName]
	var controlDate = new Date(textBox.value).toDateString();
	var TestControlDate = new Date(controlDate)
	if (TestControlDate.toDateString() == "NaN" || controlDate == "") {
		controlDate = "";
		textBox.value = "";
	} 
	var url = getPathRoot() + '/Common/ivDataEntryCalendar.aspx?ControlDate=' + controlDate + '&FormName=' + document.forms[0].name + '&ControlName=' + controlName;
	var DateWindow=window.open(url,'ivDataEntryDate',windowOptions);
	DateWindow.focus();
}

function getPathRoot()
{
	var s = location.pathname;
	if (s.substr(0, 1) != "/") s = "/" + s;
	var nPos = s.indexOf("/", 1);
	return (s.substr(0, nPos));
}

function displayMessage(header,msg,code)
{
	var windowOptions = 'height=450,width=450,left=200,top=150,resizable=yes,scrollbars=yes'
	var url = getPathRoot() + '/Common/ivDataEntryMessage.aspx?Header=' + header + '&Message=' + msg + '&Code=' + code;
	var MessageWindow=window.open(url,'Message',windowOptions);
	MessageWindow.focus();
}

function formatAjaxDate(oTextBox)
{
	// Calls AJAX process to format date
	var dDateVal
	var arSpans = document.getElementsByTagName("span");

	for (i=0;i<arSpans.length;i++)
	{	// Find the DateExpressionValidator associated with oTextBox so we can shut off its display
		if (arSpans[i].controltovalidate == oTextBox.id && arSpans[i].id.match(/DateExpressionValidator/gi))
		{
			var oValidator = arSpans[i];
			i = arSpans.length;
		}
	}
	dDateVal = runAJAXProcess("/Common/AjaxFunctions.aspx?Func=FormatDate&sDate=" + oTextBox.value)
	if (dDateVal != "INVALID")
	{
		oTextBox.value = dDateVal;
		oValidator.style.display = "none";  // Hide regular expression validator error message
	}
	else
	{
		oValidator.innerHTML = "Invalid date format";
		oValidator.style.display = "";
	}
}

function runAJAXProcess(url)
{
	g_req = createXMLHTTPConduit();
	var url = getPathRoot() + url;
	if(g_req != null)
	{
		g_req.open("GET", url, false); // 'false' means call it synchronously -- wait for the ASP page to finish
		g_req.send(null);
		if (g_req.status == 200)
		{
			return g_req.responseText;
		}
		else if (g_req.status == 500)
		{
			// Internal Server Error -- show entire contents of error in a pop-up
			var errorText = "<span style='font-family: arial; font-size: x-small; font-weight: bold;'>Error encountered by runAJAXProcess<br><span style='font-size: small'>" + url + "</span></span><br><br>"
			errorText += g_req.responseText;
			var errorWin;
			// Create a new window and display the error
			errorWin = window.open('', 'errorWin');
			errorWin.document.body.innerHTML = errorText;
			errorWin.focus();
		}
	}
}

function createXMLHTTPConduit()
{
	//This function creates an XMLHTTP object to be used in AJAX-type operations
	var oXMLHTTP;
	try
	{
		oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			oXMLHTTP = null;
		}
	}

	if(!oXMLHTTP && typeof XMLHttpRequest != "undefined")
	{
		oXMLHTTP = new XMLHttpRequest();
	}
	return oXMLHTTP;
}
