function Trim(text)
{
	ndx = 0;
    lgth = text.length;

    while ((ndx < lgth) && (text.charAt(ndx) == ' ')) ++ndx;
    while ((lgth > ndx) && (text.charAt(lgth - 1) == ' ')) --lgth;
    return text.substring(ndx, lgth);
}

function IsNumeric(num)
{
	ndx = 0;
    lgth = num.length;

	while (ndx < lgth) {
        if ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9'))
			return false;
		++ndx;
	}

	return true;
}

function IsAmount(num)
{
	ndx = 0;
	dec = 0;
    lgth = num.length;

	while (ndx < lgth) {
        if ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9')) {
			if ((num.charAt(ndx) == '.') && (dec == 0)) {
				dec++;
				ndx++;
				continue;
			}

			return false;
		}
		++ndx;
	}

	return true;
}

function FormError(obj, message)
{
	alert(message);
	obj.focus();
	return false;
}

function EditRequired(obj, msg)
{
	obj.value = Trim(obj.value);
	if (obj.value.length == 0) {
		return FormError(obj, msg);
	}

	return true;
}

function EditSelected(obj, msg)
{
	if (obj.selectedIndex >= 0) {
		if (obj.options[selectedIndex].text.charAt(0) != '-')
			return true;
	}

	return FormError(obj, msg);
}

function EditRadio(obj, msg)
{
    for (ndx = 0; ndx < obj.length; ndx++) {
        if (obj[ndx].checked)
			return true;
	}

	return FormError(obj[0], msg);
}

function FormatText(obj)
{
	obj.value = Trim(obj.value);
}

function FormatUpper(obj)
{
	obj.value = Trim(obj.value.toUpperCase());
}

function FormatPostal(obj)
{
	FormatUpper(obj);
	if ((obj.value.length == 6) && (obj.value.charAt(0) >= 'A') && (obj.value.charAt(0) <="Z"))
		obj.value = obj.value.substring(0, 3) + ' ' + obj.value.substring(3, 6);
}

function FormatPhone(obj)
{
	obj.value = Trim(obj.value);

	if (obj.value.length == 0)
		return true;

	ndx = 0;
	num = obj.value;

	if ((num.charAt(0) == '(') && (num.charAt(4) == ')')) {
		area = num.substring(1, 4);
		num = Trim(num.substring(5));
	} else {
		area = num.substring(0, 3);
		if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
			num = Trim(num.substring(4));
		else
			num = Trim(num.substring(3));
	}

	xchg = num.substring(0,3);
	if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
		num = Trim(num.substring(4));
	else
		num = Trim(num.substring(3));

	if (num.length > 4) {
		if ((num.charAt(4) == '-') || (num.charAt(4) == '.'))
			ext = Trim(num.substring(5));
		else
			ext = Trim(num.substring(4));

		num = Trim(num.substring(0, 4));
	}
	else
		ext = '';

	for (ndx = 0; ndx < ext.length; ndx++) {
		if ((ext.charAt(ndx) >= '0') && (ext.charAt(ndx) <= '9'))
			break;
	}

	ext = Trim(ext.substring(ndx));

	if (ext.length > 0)
		ext = 'Ext. ' + ext;

	obj.value = Trim(area + ' ' + xchg + '-' + num + ' ' + ext);
	return true;
}

function PhoneRequired(obj, msg)
{
	if (!EditRequired(obj, msg))
		return false;

	return FormatPhone(obj);
}

function SelectBox(checkbox)
{
	box = eval(checkbox);
	box.checked = !box.checked;
}

function doClear(theText)
{
    if (theText.value == theText.defaultValue)
        theText.value = ""
}

PositionX = 10;
PositionY = 10;
defaultWidth  = 480;
defaultHeight = 319;
var AutoClose = true;

function popImage(imageURL,imageTitle)
{
	var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
	if( !imgWin ) { return true; }
	imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
		'function resizeWinTo() {\n'+
		'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
		'var oH = document.images[0].height, oW = document.images[0].width;\n'+
		'if( !oH || window.doneAlready ) { return; }\n'+
		'window.doneAlready = true;\n'+
		'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
		'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
		'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
		'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
		'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
		'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
		'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
		'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
		'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
		'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
		'}\n'+
		'<\/script>'+
		'<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
		(document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;">'))+
		'<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
		(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
	imgWin.document.close();
	if( imgWin.focus ) { imgWin.focus(); }
	return false;
}
