/*
	info common.js - common javascript functions for most programs.
	info Version 7.1 Rel. (01/21/05)
	info Copyright (c) 2005 by HBS, Inc. 
	info All rights reserved.
*/

var libpgm = "common.js";
var libver = "7.1 01/21/05";

var custno;
var sessid;
var ie = true;
var ErrMsg = "OK";
var Fatal = "0";

function ProcessError()
{
	if ( ErrMsg != "OK" )
	{
		alert( ErrMsg );
		if ( Fatal == "1" )
			window.location.href = "/login.htm";
		return(1);
	}
	return(0);
}

function ShowVersions()
{
	var vers = document.title;
	vers += "\r\n"
	vers += "Library: " + libver;
	vers += "\r\n"
	vers += "Script: " + jsver;
	vers += "\r\n"
	vers += "HTML: " + document.getElementById('htmver').value;
	alert( vers );
}

function GetXMLHTTP()
{
	var xmlhttp  = false;

	ie = true;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}

	if ( !xmlhttp && typeof XMLHttpRequest != 'undefined' )
	{
		ie = false;
		xmlhttp = new XMLHttpRequest();
	}

	if ( !xmlhttp && typeof XMLHttpRequest != 'undefined' )
	{
		ErrMsg = 'This site cannot be accessed with this browser or an error occurred in your browser.';
		// This may seem strange, but FATAL returns them to login which is
		// where they probably got the error message to start with
		Fatal = "0";
		ProcessError();
		return 0;
	}
	return xmlhttp;
}

function getVars()
{
	var is_input = document.URL.indexOf('?');
	if ( is_input != -1 )
	{
		var addr_str = document.URL.substring( is_input+1,document.URL.length);
		if ( addr_str.substring( 0, 4 ) == "cust" )
		{
			custno = addr_str.substring(5, 10 );
		}
		else
		{
			return 0;
		}
		var sess_str = addr_str.substring( 11,addr_str.length);
		if ( sess_str.substring( 0, 2 ) == "id" )
		{
			sessid = sess_str.substring(3, sess_str.length ) - 0;
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
	return 1;
}

function doURL( theUrl )
{
	window.location.href=theUrl+"?cust="+custno + "&id=" + sessid;
}

function startUp( ref, cst, ses )
{
	window.location.href = ref + "?cust=" + cst + "&id=" + ses;
}

function escapeStr( val )
{
	var chkval = checkStr( val );
	var res;
	var tmp = "";
	var len = chkval.length;

	for ( var i = 0; i < len; i++ )
	{
		res = chkval.charCodeAt(i);

		if ( res < 100 )
		{
			tmp += "0";
		}

		tmp += res.toString();
	}

	return tmp;
}

function checkStr( val )
{
	var res;
	var tmp = "";
	var len = val.length;

	for ( var i = 0; i < len; i++ )
	{
		res = val.charAt(i);

		if ( res == '<' )
		{
			tmp += "&#60;";
		}
		else if ( res == '>' )
		{
			tmp += "&#62;";
		}
		else
		{
			tmp += res.toString();
		}
	}

	return tmp;
}

function mapEnterToTab( theEvent )
{
	if ( ie )
	{
		if ( theEvent.keyCode == 10 || theEvent.keyCode == 13 )
		{
			theEvent.keyCode = 9;
		}
	}
}

function digitsOnly( theEvent, theField )
{
	key = theEvent.keyCode;

	// no decimal point or minus signed allowed

	if ( key == 46 || key == 45 )
	{
		theEvent.returnValue = false;

		return;
	}
	else
	{
		hbsAllowOnlyNums( theEvent, theField );
	}
}

function allowOnlyNums(theEvent,theTextBox)
{
	var key = theEvent.keyCode;

	if ( (key != 46) && (key != 45) && (key < 48 || key > 57) )
	{
		theEvent.returnValue = false;
	}
	else
	if ( key == 45 )
	{
		if ( theTextBox.value.indexOf('-') != -1 )
		{
			theEvent.returnValue = false;
		}
	}
	else
	if ( key == 46 )
	{
		if ( theTextBox.value.indexOf('.') != -1 )
		{
			theEvent.returnValue = false;
		}
	}
}

function hbsAllowOnlyNums(theEvent,theTextBox)
{
	var key = theEvent.keyCode;
	var theString = theTextBox.value;
	var foundDecimal = false;
	var foundMinus = false;

	for(var i=0;i<theString.length;i++)  //check for previous decimal
	{
		if(theString.charAt(i) == '.')
		{
			foundDecimal = true;
		}
		if(theString.charAt(i) == '-')
		{
			foundMinus = true;
		}
	}
	if(foundDecimal == false && foundMinus == false)
	{
		if(key == 45)
		{
			theString = "-" + theString;
			theTextBox.value = theString;
			theEvent.returnValue = false;
			return;
		}
		if((key != 46) && (key != 45) && (key < 48 || key > 57) )
		{
			theEvent.returnValue = false;
		}
		return;
	}
	if(foundDecimal == false)
	{
		if(key != 46 && (key < 48 || key > 57) )
		{
			theEvent.returnValue = false;
		}
		return;
	}
	if(foundMinus == false)
	{
		if(key == 45)
		{
			theString = "-" + theString;
			theTextBox.value = theString;
			theEvent.returnValue = false;
			return;
		}
		if(key != 45 && (key < 48 || key > 57) )
		{
			theEvent.returnValue = false;
		}
		return;
	}
	else
	{
		if(key < 48 || key > 57)
		{
			theEvent.returnValue = false;
		}
		return;
	}
}

function hbsMakeUpper(e,textBox)
{
	if ( ie )
	{
		var kc = e.keyCode;

		if(kc > 96 && kc < 123)
		{
			e.keyCode = kc - 32;
		}
	}
}

