var myAlohaLinkButton_Clicked = false;

function countCharacters(label,obj,maxlength)
{	
   try
   {
	if (document.all){
	   
		var message = 'Limit up to '+maxlength+' characters ('+obj.value.length+' used).';
		
		if (obj.value.length > maxlength)
			document.getElementById(label).style.color='red';
		else
			document.getElementById(label).style.color='black';
		
		document.getElementById(label).firstChild.nodeValue=message;
	}
   } catch(err)
   {
	//do nothing
   }
}

//if (window.parent.frames.length == 0)
//	window.location='home.aspx';

// openNonSizableWindow
function openNonSizableWindow(url)
{
	popupWin = window.open(url,'calendarPopup','resizable=no,menubar=no,status=no,scrollbars=no,toolbar=no,directories=no,location=no,width=250,height=190');
	popupWin.focus();
}


// at the top of every error alert.
var stdErrorPrefix = "There were one or more problems with the form values you entered.\nPlease check the following and try submitting the form again:\n\n"


// returns the name of the month
Date.prototype.getMonthName = function() {
	return ["January","February","March","April","May","June","July","August","September","October","November","December"][this.getMonth()]
}


// returns a nicely formatted date string
Date.prototype.getHumanDateString = function() {
	return this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear()
}


// returns a nicely formatted time string
Date.prototype.getHumanTimeString = function() {
	var h = this.getHours()
	var m = this.getMinutes()
	var t = h >= 12 ? "pm" : "am"

	if (h == 0) h = 24
	if (h > 12) h -= 12
	h = String(h)
	m = String(m)
	if (m.length == 1) m = "0" + m

	return h + ":" + m + " " + t
}


// Time is a wrapper class for the Date object
// it accepts a wide variety of strings representing the time
// and returns a date object representing the current date with the
// specified time.
//
// accepted formats include 4p, 4:p, 4:1p, 4:10p, 4:10pm, 16:00, and a bunch more...
/*
function Time(sTimeStr) {
	var a = sTimeStr.match(/(\d{1,2})\s*\x3A?\s*(\d{0,2})\s*(am|pm|a|p)?/i)
	if (a) {
		var d = new Date()
		var h = a[1]
		var m = a[2]
		var t = a[3]
		if (t && t.charAt(0).toLowerCase() == "p") h = h % 12 + 12
		d.setHours(h)
		d.setMinutes(m)
		return d
	}
	return null
}
*/

function Time(sTimeStr) {
	return new Date(Date.parse("01/01/01 " + sTimeStr));
}


// push is a quite useful method of arrays in newer javascript implementation, but not in ie5-
Array.prototype.push = function(v) {
	this[this.length] = v
	return v
}


// like Trim( ) in vbscript. removes trailing and leading whitespace from a string
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "")
}


// determines whether or not a filename has one of the specified extensions
String.prototype.hasFileExtension = function(a) {
	var bOk = false
	for (var i = 0; i < a.length; i++) {
		if (this.indexOf(a[i]) == this.length - a[i].length) {
			bOk = true
			break
		}
	}
	return bOk
}


// attaches textFieldBlurHandler to the onblur event of every text or textarea element in f
function attachAllTextHandlers(f) {
	var el
	for (var i = 0; (el = f.elements[i]); i++) {
		if (el.type == "text" || el.type == "textarea") el.onblur = textFieldBlurHandler
	}
}


// functions that attempt to parse and reformat element's values as a specific datatype
function textFieldBlurHandler() {
	this.value = this.value.trim()
}

function dateFieldBlurHandler() {
	var d = new Date(this.value)
	// if input year is not 4 digits, use the current century.
	if (this.value.search(/\d{4}/) == -1) {
		var now = new Date()
		var curr_century = Math.floor(now.getFullYear() / 100) * 100
		var this_year = d.getFullYear() % 100
		d.setYear(curr_century + this_year)
	}
	if (!isNaN(d)) this.value = d.getHumanDateString()
}

function ssnFieldBlurHandler() {
	var temp = this.value.replace(/\D/g, "")
	if (temp.length >= 9) this.value = [temp.substring(0, 3), temp.substring(3,5), temp.substring(5,9)].join("-")
}

function timeFieldBlurHandler() {
	if (this.value.length > 0) {
		var t = new Time(this.value)
		if (!isNaN(t)) this.value = t.getHumanTimeString()
	}
}

function phoneFieldBlurHandler()
{		
	this.value = this.value.replace(/\D/g,"");
	if (this.value.charAt(0) == "1") this.value = this.value.substr(1);
	if (this.value.length > 15) this.value = this.value.substr(0,15);
					
	if (this.value.length >= 10)
	{
		this.value = "(" + this.value.substr(0,3) + ") " + this.value.substr(3,3) + "-" + this.value.substr(6,4) + (this.value.length > 10 ? " x" + this.value.substr(10,5) : "");
	}	
}

function canadaPostalCodeFieldBlurHandler() {
	var temp = this.value.trim();
	if (temp.length == 6) 
		this.value = temp.substring(0,3).toUpperCase() + " " + temp.substring(3,6).toUpperCase();
	else if (temp.length == 7 && temp.substring(3,4) == " ") 
		this.value = temp.toUpperCase();
}

function zipcodeFieldBlurHandler() {
	var temp = this.value.replace(/\D/g, "")
	if (temp.length == 5 || temp.length >= 9) 
		this.value = temp.length == 5 ? temp : temp.substring(0,5) + "-" + temp.substring(5,9)
}

function positiveFloatValueBlurHandler(obj) {
	if (!obj) obj = this;
	if (obj.value.indexOf(".") == 0) obj.value = "0" + obj.value;
	obj.value = obj.value.replace(/[^0-9\.0-9]/g,"");

	if (!isNaN(obj.value) && obj.value!='')
		obj.value = AddCommas(obj.value, true)
}

function floatValueBlurHandler(obj) {
	if (!obj) obj = this;
	if (obj.value.indexOf(".") == 0) obj.value = "0" + obj.value;
	obj.value = obj.value.replace(/[^-?0-9\.0-9]/g,"");

	if (!isNaN(obj.value) && obj.value!='')
		obj.value = AddCommas(obj.value, true)
}

function positiveIntegerValueBlurHandler(obj) {	
	if (!obj) obj = this;
	if (obj.value.indexOf(".") > 0) obj.value = obj.value.replace(obj.value.substr(obj.value.indexOf("."),obj.value.length-1),"");
	obj.value = obj.value.replace(/[^0-9]/g,"");			

	if (!isNaN(obj.value) && obj.value!='')
		obj.value = AddCommas(obj.value, false)
}

function integerValueBlurHandler(obj) {
	if (!obj) obj = this;
	if (obj.value.indexOf(".") > 0) obj.value = obj.value.replace(obj.value.substr(obj.value.indexOf("."),obj.value.length-1),"");
	obj.value = obj.value.replace(/[^-?0-9]/g,"");
	
	if (!isNaN(obj.value) && obj.value!='')
		obj.value = AddCommas(obj.value, false);
	else
		obj.value = "";
}

function yearFieldBlurHandler(obj) {
	if (!obj) obj = this;
	if (obj.value.indexOf(".") > 0) obj.value = obj.value.replace(obj.value.substr(obj.value.indexOf("."),obj.value.length-1),"");
	obj.value = obj.value.replace(/[^0-9]/g,"");
}

function dollarValueBlurHandler(obj) {
	if (!obj) obj = this;
	obj.value = obj.value.replace(/[^0-9\-\.]/g,"");
				
	var value = parseFloat(obj.value);
	if (!isNaN(value)) {
		value = String(Math.round(value * 100) / 100);
		if (value.indexOf(".") == -1) value += ".00";
		value = AddCommas(value, true);
		obj.value = value;
	}
}

function AddCommas(num, includeCentsYN) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	
	num.substring(num.length-(4*i+3));
	
	if (includeCentsYN)
		return (((sign)?'':'-') + num + '.' + cents);
	else
		return (((sign)?'':'-') + num);
}

function dollarNoDecimalValueBlurHandler(obj) {
	if (!obj) obj = this;
	obj.value = obj.value.replace(/[^0-9\-\.]/g,"");
			
	var value = parseInt(obj.value);
	obj.value = value;
	
	if (obj.value == "NaN")
		obj.value = "0";
	else
		value = AddCommas(value, false);
}

function textAreaFieldBlurHandler(obj, maxLength) {
	if (obj.value.length > maxLength)
	{
		obj.outerHTML += "&nbsp;<font size='1' color='red' >(" + obj.value.length + " characters entered)</font>";
		return false;
	}
	return true;
	
}

var loaded=false;
var counter=0;
var timer;

var ajax_xmlhttp = false;

//Seabrooks, Inc. would like to thank Peter-Paul Koch & Alex Tingle for the use of the following functions.
//findPosX by Peter-Paul Koch (http://www.quirksmode.org/js/findpos.html) & Alex Tingle (http://blog.firetree.net/2005/07/04/javascript-find-position/).
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1)
		{
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
			break;
		obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

//findPosY by Peter-Paul Koch (http://www.quirksmode.org/js/findpos.html) & Alex Tingle (http://blog.firetree.net/2005/07/04/javascript-find-position/).
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
			break;
		obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function hideHelp()
{
	hideLoadingMessage();
	hideContent();

	document.getElementById("CSHelpPanel").style.visibility = 'hidden';
}

function showFormPacketAttachmentHelp(obj, formPacketAttachmentId)
{
	try
	{
		clearTimeout(timer);
		positionHelpPanel(obj);
		
		loaded=false;
		showLoadingMessage();
		getContent(formPacketAttachmentId, "packetAttachment");

		clearTimeout(timer);
	}
	catch (e) {}
}

function showHelp(obj, rowId)
{
	try
	{
		clearTimeout(timer);
		positionHelpPanel(obj);
		
		loaded=false;
		showLoadingMessage();
		getContent(rowId, "row");

		clearTimeout(timer);
	}
	catch (e) {}
}

function positionHelpPanel(obj)
{
	var x = findPosX(obj) + 'px';
	var y = (findPosY(obj) + 20) + 'px';

	document.getElementById("CSHelpPanel").style.left = x;
	document.getElementById("CSHelpPanel").style.top = y;
	document.getElementById("CSHelpPanel").style.visibility = 'visible';
}

function hideLoadingMessage()
{
	document.getElementById('LoadingMessageDiv').innerHTML = ''
	document.getElementById("LoadingMessageDiv").style.visibility = 'hidden';
	clearTimeout(timer);
}

function showLoadingMessage()
{
	var msg = 'Retrieving Context-Sensitive Help Information. Please Wait.';

	if (counter<5)
		counter++;
	else
		counter=0;

	if(!loaded)
	{
		timer = window.setTimeout("showLoadingMessage()",250);

		for(i=0;i<counter;i++)
			msg += '.';

		document.getElementById("LoadingMessageDiv").style.visibility = 'visible';
		document.getElementById('LoadingMessageDiv').innerHTML = msg;
	}
	else
		hideLoadingMessage();
}

function hideContent()
{
	document.getElementById("ContentDiv").style.visibility = 'hidden';
	document.getElementById("ContentDiv").innerHTML = '';
}

function getContent(ID, type)
{			
	try
	{
		ajax_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			ajax_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			ajax_xmlhttp = false;
		}
	}

	if (!ajax_xmlhttp && typeof XMLHttpRequest != 'undefined')
		ajax_xmlhttp = new XMLHttpRequest();
	
	if (type=="row")
		ajax_xmlhttp.open("POST", urlBase + "forms/form_data_fetch.aspx?RowID="+ID, false);
	else if (type=="packetAttachment")
		ajax_xmlhttp.open("POST", urlBase + "forms/form_data_fetch.aspx?FormPacketAttachmentID="+ID, false);
	
	ajax_xmlhttp.send(null);

	ajax_xmlhttp.onreadystatechange = ajax_callback();
}

function ajax_callback()
{	
	if(ajax_xmlhttp.readyState == 4)
	{
		loaded=true;
		document.getElementById("ContentDiv").innerHTML = ajax_xmlhttp.responseText;
		document.getElementById("ContentDiv").style.visibility = 'visible';
		hideLoadingMessage();
	}
}
