// Trim whitespace from left and right sides of s.
function trim(s)
{
    return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

function errorMessageHandler (isValid, fieldName, msg)
{
	if (!isValid)
	{
		if (document.getElementById(fieldName + 'Error') != null)
		{
			if (msg.length > 0)
			{
				document.getElementById(fieldName + 'Error').innerHTML = msg + '<br>';
			}
			else
			{
				document.getElementById(fieldName + 'Error').innerHTML = '';
			}

		}

		if (document.getElementById(fieldName + 'ErrorSymbol') != null)
		{
			document.getElementById(fieldName + 'ErrorSymbol').innerHTML = '*';
		}

		if (document.getElementById(fieldName + 'Label') != null)
		{
			document.getElementById(fieldName + 'Label').className = 'error';
		}

		hideOverlay();
	}
	else
	{
		if (document.getElementById(fieldName + 'Error') != null)
		{
			document.getElementById(fieldName + 'Error').innerHTML = '';
		}

		if (document.getElementById(fieldName + 'ErrorSymbol') != null)
		{
			document.getElementById(fieldName + 'ErrorSymbol').innerHTML = '';
		}

		if (document.getElementById(fieldName + 'Label') != null)
		{
			document.getElementById(fieldName + 'Label').className = '';
		}
	}
}

function clearErrorMessage ()
{
	var all = document.all ? document.all : document.getElementsByTagName('*');
	var elements = new Array();
	var index = 0;

	for (var i = 0; i < all.length; i++)
	{
		var idValue = all[i].id;
		var re1 = /Error$/;
		var re2 = /ErrorSymbol$/;
		var re3 = /Label$/;

		if(re1.test(idValue) || re2.test(idValue))
		{
			all[i].innerHTML = '';
		}
		else if (re3.test(idValue))
		{
			all[i].className = '';
		}
	}
}

// Fixed clearing error messages.
function clearAll(theform)
{
	for (i = 0; i < theform.elements.length; i++)
	{
		var fieldName = theform.elements[i].name;

		if (theform.elements[i].type=="text" ||
			theform.elements[i].type=="textarea" ||
			theform.elements[i].type=="password")
		{
			theform.elements[i].value = '';
		}
		else if (theform.elements[i].type == "checkbox" || theform.elements[i].type=="radio")
		{
			theform.elements[i].checked = false;
		}
		else if (theform.elements[i].type=="select-one")
		{
			theform.elements[i].value = theform.elements[i].options[0].value;
		}

		if (document.getElementById(fieldName + 'Error') != null)
		{
			document.getElementById(fieldName + 'Error').innerHTML = '';
		}

		if (document.getElementById(fieldName + 'ErrorSymbol') != null)
		{
			document.getElementById(fieldName + 'ErrorSymbol').innerHTML = '';;
		}

		if (document.getElementById(fieldName + 'Label') != null)
		{
			document.getElementById(fieldName + 'Label').className = '';
		}
	}

	if (document.getElementById('serverSideError') != null)
	{
		document.getElementById('serverSideError').innerHTML = '';
	}
}


function visitReceiptPage (link)
{
	window.open (link,'receipt','location=0,scrollbars=1,status=1,width=550,height=458,top=50,left=30');
}


function printPage()
{
//	if(window.print)
//	{
		window.print();
//	}
//	else
//	{
//		alert("Your browser doesn't support printing. Please use File->print option.");
//	}
}

function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function overlay()
{
	clearServerReturnMessages();
	pageSize = getPageSize();
	divTD = document.getElementById("div_td");
	divTable = document.getElementById("div_table");
	divElement = document.getElementById("overlay");
	var tableWidth = 0;
	tableWidth = pageSize[0]-103;
	if (tableWidth < 900)
	{
		tableWidth = 900;
	}
	divTable.width = tableWidth;
	divTD.width = tableWidth;
	divTD.height = pageSize[1]-10;
	divElement.style.visibility="visible";
}

function overlayCancel()
{
	overlay();
	bCancel = true; //to bypass struts validation for cancel button
}

function overlayPopUp()
{
	pageSize = getPageSize();
	divTD = document.getElementById("div_td");
	divTable = document.getElementById("div_table");
	divElement = document.getElementById("overlay");
	var tableWidth = 0;
	tableWidth = pageSize[0];
	if (tableWidth < 900)
	{
		tableWidth = 900;
	}
	divTable.width = tableWidth;
	divTD.width = tableWidth;
	divTD.height = pageSize[1];
	divElement.style.visibility="visible";
}

function overlayPopUpCancel()
{
	overlayPopUp();
	bCancel = true; //to bypass struts validation for cancel button
}

function hideOverlay()
{
	//clearAllErrors();
	divElement = document.getElementById("overlay");
	divElement.style.visibility="hidden";
}

function clearServerReturnMessages()
{
	if (document.getElementById('serverSideError') != null)
	{
		document.getElementById('serverSideError').innerHTML = '';
	}

	if (document.getElementById('statusMessage') != null)
	{
		document.getElementById('statusMessage').innerHTML = '';
	}
}

function popUp(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="status,scrollbars,resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,scrollbars,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin', strOptions);
}
