var	gtEnablePrintWindow = true;

function fnPromptToSaveChanges()
{
	return confirm('Click "OK" to save your changes or click "Cancel" to continue without saving.');
}

// -------------------------------

function fnPrintWindow()
{
	if	(gtEnablePrintWindow &&
		(navigator.appVersion.indexOf("Mac") < 0))
	{
		window.print();
	}
}

// -------------------------------

function fnIsValidFileName(sFileName)
{
	var re = new RegExp("[^A-Za-z0-9_\-]");
	return !re.test(sFileName);
}

// -------------------------------

function fnIsValidPrice(sPrice)
{
    if (sPrice.length == 0)
    {
        return false;
    }

	var t_isDecimalFound = false;
	for (i = 0; i < sPrice.length; i++)
	{
		if ((i == 0) && (sPrice.charAt(i) == '-'))
		{
			continue;
		}
		if (isNaN(parseInt(sPrice.charAt(i), 10)) &&
			(sPrice.charAt(i) != '.'))
		{
			return false;
		}
		if (sPrice.charAt(i) == '.')
		{
			if (t_isDecimalFound)
			{
				return false;
			}
			else
			{
				t_isDecimalFound = true;
			}
		}
	}
	return true;
}

// -------------------------------

function fnIsValidInteger(sInteger)
{
	if (sInteger.length == 0)
	{
		return false;
	}
	
	for (i = 0; i < sInteger.length; i++)
	{
		if (isNaN(parseInt(sInteger.charAt(i), 10)))
		{
			return false;
		}
	}
	return true;
}

// -------------------------------

function fnIsValidDate(sDate)
{
	var s_splitChar = /\//;
	var an_numbers = sDate.split(s_splitChar);
	var n_month = an_numbers[0] - 1;
	var n_day = an_numbers[1];
	var n_year = an_numbers[2];
	if (n_year < 1000)
	{
		return false;
	}
	var d = new Date(n_year, n_month, n_day);
	if (	(d.getMonth() != n_month) ||
			(d.getDate() != n_day) ||
			(d.getFullYear() != n_year)	)
	{
		return false;
	}
	return true;
}

// -------------------------------

function fnToggleCheckboxes(xForm, tCheckAll, sCheckBoxName)
{
	// sCheckBoxName is optional
	for (var n_index = 0; n_index < xForm.elements.length; n_index++)
	{
		if (xForm.elements[n_index].type == 'checkbox')
		{
			if (!sCheckBoxName)
			{
				xForm.elements[n_index].checked = tCheckAll;
			}
			else if (xForm.elements[n_index].name == sCheckBoxName)
			{
				xForm.elements[n_index].checked = tCheckAll;
			}
		}
	}
}
		
// -------------------------------

function ftAreAnyChecked(xForm, sCheckBoxName)
{
	// sCheckBoxName is optional
	for (var n_index = 0; n_index < xForm.elements.length; n_index++)
	{
		if ((xForm.elements[n_index].type == 'checkbox') &&
			xForm.elements[n_index].checked)
		{
			if (!sCheckBoxName || (xForm.elements[n_index].name == sCheckBoxName))
			{
				return true;
			}
		}
	}
	
	return false;
}
		
// -------------------------------

function fnOpenSaveWindow(sWindowName, sMessage, tIsResizable)
{
	if (!sMessage)
	{
		sMessage = 'Contacting server... Please Wait...';
	}
	
	var x_winDetails = 'left=' + (top.screen.width-300)/2 + ',top=' + (top.screen.height-150)/2 + ',width=300,height=150'
	
	if (tIsResizable)
	{
		x_winDetails += 'resizable=yes,scrollbars=no;status=no;location=no;dialog=no';
	}
	
	x_win = window.open('', sWindowName, x_winDetails);
	x_win.document.write('<HTML>');
	x_win.document.write('<HEAD><TITLE>Online Selection &amp; Acquisitions</TITLE></HEAD>');
	x_win.document.write('<BODY><B>');
	x_win.document.write(sMessage);
	x_win.document.write('</B></BODY>');
	x_win.document.write('</HTML>');
	x_win.focus();
}

// -------------------------------

function fnOpenWindowWithSpecs(	sWindowName,
								lWidthAsPercentage,
								lHeightAsPercentage,
								sWindowText,
								sURL)
{
	if (!sWindowText)
	{
		sWindowText = 'Contacting server... Please Wait...';
	}
	
	var n_width=top.screen.width * lWidthAsPercentage / 100;
	var n_height=top.screen.height * lHeightAsPercentage / 100;
	var n_top=(top.screen.height - n_height) / 2;
	var n_left=(top.screen.width - n_width) / 2;
	var x_details = 'left=' + n_left + ',top=' + n_top + ',width=' + n_width + ',height=' + n_height;
	x_details += 'toolbar=no,menubar=no,scrollbars=yes,resizable=yes';
	igwin = window.open('', sWindowName, x_details);
	igwin.document.write('<HTML>');
	igwin.document.write('<HEAD><TITLE>Online Selection &amp; Acquisitions</TITLE></HEAD>');
	igwin.document.write('<BODY><H1>');
	igwin.document.write(sWindowText);
	igwin.document.write('</H1></BODY>');
	igwin.document.write('</HTML>');
	igwin.focus();
	
	if (sURL)
	{
		igwin.location.href = sURL;
	}
}

// -------------------------------

function fnOpenFramedWindow(sURL,
							lWidthAsPercentage,
							lHeightAsPercentage)
{
	if (!lWidthAsPercentage)
	{
		lWidthAsPercentage = 90;
	}
	
	if (!lHeightAsPercentage)
	{
		lHeightAsPercentage = 80;
	}

	var n_width=top.screen.width * lWidthAsPercentage / 100;
	var n_height=top.screen.height * lHeightAsPercentage / 100;
	var n_top=(top.screen.height - n_height) / 2;
	var n_left=(top.screen.width - n_width) / 2;
	var x_details = 'left=' + n_left + ',top=' + n_top + ',width=' + n_width + ',height=' + n_height;
	x_details += 'toolbar=no,menubar=no,scrollbars=yes,resizable=yes';
	
	var igwin = window.open('', '_blank', x_details);
	igwin.document.write('<HTML>');
	igwin.document.write('<HEAD><TITLE>Online Selection &amp; Acquisitions</TITLE></HEAD>');
	igwin.document.write('<frameset frameborder="0" framespacing="1" border="0" rows="42,*">');
	igwin.document.write('<frame src="/closepage.htm" scrolling="no" noresize>');
	igwin.document.write('<frame name="PopUpChild" src="');
	igwin.document.write(sURL);
	igwin.document.write('" scrolling="auto" noresize>');
	igwin.document.write('<noframes><body><p>')
	igwin.document.write('This page uses frames, but your browser doesn\'t support them.</p></body></noframes>');
	igwin.document.write('</frameset></HTML>');
	igwin.focus();
}

// -------------------------------

function fnOpenReportWindow(sWindowName, sURL)
{
	var n_width=top.screen.width * .85;
	var n_height=top.screen.height * .75;
	var n_top=(top.screen.height - n_height)/2 - 30;
	var n_left=(top.screen.width - n_width)/2;
	var x_details = 'left=' + n_left + ',top=' + n_top + ',width=' + n_width + ',height=' + n_height;
	x_details += ',toolbar=no,menubar=yes,scrollbars=yes,resizable=yes';
	igwin = window.open('',sWindowName,x_details);
	igwin.document.write('<HTML>');
	igwin.document.write('<HEAD><TITLE>Online Selection &amp; Acquisitions - Generating Report</TITLE></HEAD>');
	igwin.document.write('<BODY><H1>Generating Report. One moment please...</H1></BODY>');
	igwin.document.write('</HTML>');
	igwin.focus();
	
	if (sURL)
	{
		igwin.location.href = sURL;
	}
}

// -------------------------------

function fnUpdateParentWindow(sNewURL)
{
	window.parent.location.replace(sNewURL);
}

// -------------------------------

function fnWriteDateAndTime()
{
	var now = new Date();
	document.write(now.toLocaleString());
}

// -------------------------------

function displayFrame(me, w, h){
	me.style.display="block";

	var l = -w/2;
	var t = -h/2;
	
	me.style.width = w + "px";
	me.style.height = h + "px";
	me.style.marginLeft = l + "px";
	me.style.marginTop = t + "px";
	me.style.top="50%";
	me.style.left="50%";
}

function closeMe(me){
	me.style.display="none";
	me.style.top="-10000px";
	me.style.left="-10000px";
	me.style.width="auto";
	me.style.height="auto";
}

function displayRefreshingPageMsgInParent()
{
    parent.document.getElementById('FrameMsg').firstChild.data = 'Refreshing Page... Please Wait...';
    parent.document.getElementById('FrameMsg').style.display = '';
    parent.document.getElementById('IframeDiv').style.display = 'none';
	displayFrame(parent.document.getElementById('UpdateSettingsDiv'), 300, 150);
}

function displayPleaseWaitMsg(sMessage)
{
	if (!sMessage)
	{
		sMessage = 'Contacting server... Please Wait...';
	}
	else
	{
	    document.getElementById('FrameMsg').firstChild.data = sMessage;
	}

    document.getElementById('FrameMsg').style.display = '';
    document.getElementById('IframeDiv').style.display = 'none';
	displayFrame(document.getElementById('UpdateSettingsDiv'), 300, 150);
}