/*
 *	Function List
 *		BrowserAPI()		Greg Miley		05 Apr 02
 *		CookieParse()		Greg Miley		21 Feb 02
 *		EmptyNull()			Greg Miley		04 Mar 02
 *		FieldCheck()		Greg Miley		04 Mar 02
 *
 *
 */

var dom = (document.getElementById && !document.all)? true: false;

/*************************************************************************************\
 * Name:	BrowserAPI()
 * Author:	Greg Miley
 * Date:	03April02
 * Description:	
 *		Provide clean access to browser objects.
 *		Right now you must provide a StringID for the element you wish
 *		to access. This API wrapper provides access to DOM as well as
 *		older version 4's of IE and NN. Read the internal documentation
 *		comments for further explanation of each method/property.
 * Usage:
 *		1) Create a new instance of the object.
 *			var Browser = new BrowserAPI();
 *		2) Access objects via Browser.ObjRef('ObjIDString')
 *			Browser.ObjRef('mySpan').id = "mySpan";
 *			2.1) Move Items to specified postions.
 *				Browser.ObjRef('mySpan').MoveTo(nX,nY);
 *			2.2) Show and Hide items in document.
 *				Browser.ObjRef('mySpan').Show();
 *				Browser.ObjRef('mySpan').Hide();
 *			2.3) Resize Items to specified size.
 *				Browser.ObjRef('mySpan').Resize(nX,nY);
 *		3) Access styles via Browser.StyleRef('ObjIDString')
 *			Browser.StyleRef('mySpan').border = "1px #000000 solid";
 * Notes:
 *		More to come on this development. I will be adding more 
 *		methods as I complete them. Comments or suggestions to 
 *		greg_s_miley@hotmail.com
 *
 *		As a rule you MUST have styles set for alot of stuff
 *		to be accessed correctly. This is true whether you use
 *		this API or not. This API does NOT set up initial style
 *		for you. You have to do that yourself.
 *
 * Modification History:
 *	Name		When		Why
 **========================================================
 *	g miley		04.05.02	Added .Show() .Hide() and .MoveTo(X,Y)
 *	g miley		04.05.02	Added .Resize()
 *
\*************************************************************************************/
function BrowserAPI() {
	var Root = this;
	this.BrowserID = function BrowserID() {
	// This funtion acts as a dynamic property of the API.
	// It returns the browser capabilities. (DOM | IE | NN).
	// Returns blank if nothing is supported.
		if(parseInt(navigator.appVersion) >= 4) {
			if(document.getElementById) {
				return "DOM";
			} else if(document.layers) {
				return "NN";
			} else {
				return "IE";
			}
		}
		return "";
	}
	this.ObjExists = function TestObjectExists(loObject) {
	// Test for an object to exist... returns true or false.
		var ObjID = loObject;
		return eval(document.getElementById(ObjID) !== null);
	}
	this.ObjRef = function GetObject(loObject) {
	// Reflows object path to access object by a string
	// object id.
		var ObjID = loObject;
		var myObj;
		var lcAllAccess = "";
		var BrowType = Root.BrowserID();
		if(BrowType == "IE") {
			lcAllAccess = "all.";
		}
		if(typeof loObject == "string") {
			if( BrowType == "DOM") {
				myObj = eval(document.getElementById(loObject));				
			} else {
				myObj = eval("document." + lcAllAccess + loObject);
			}
		} else {		
			myObj = loObject;
		}
		// Move item method
		myObj.MoveTo = function MoveTo(Xpos, Ypos) {
			var movObj = Root.StyleRef(ObjID);
			movObj.top = Ypos;
			movObj.left = Xpos;
		}
		// Show Item method
		myObj.Show = function Show() {
			var showObj = Root.StyleRef(ObjID);
			showObj.visibility = "visible";
		}
		// Hide item method
		myObj.Hide = function Hide() {
			var showObj = Root.StyleRef(ObjID);
			showObj.visibility = "hidden";
		}
		// Resize item method
		myObj.Resize = function Resize(Xsize, Ysize) {
			var sizeObj = Root.StyleRef(ObjID);
			sizeObj.height = Ysize;
			sizeObj.width = Xsize;
		}
		return myObj
	}
	this.StyleRef = function GetStyleObject(loStyleObject) {
	// Accesses the style attributes of an element using .ObjRef()
		var myObj;
		var lcStyleAccess = "";
		myObj = Root.ObjRef(loStyleObject);
		if(Root.BrowserID() !== "NN") {
			myObj = myObj.style;
		}
		return myObj;
	}
}

// initialize the API object.
var Browser = new BrowserAPI();

/**********************************************\
* Function: CookieParse()
* Author: Greg Miley
* Description: 
*	Read in cookies and extract designated
*	name/value pair.
\**********************************************/
function CookieParse(tcCookieName) {
	var lckCookie = document.cookie;
	var lckMyCookies = lckCookie.split(String.fromCharCode(59) + String.fromCharCode(32));
	for (i = 0; i < lckMyCookies.length; i++) {
		re = new RegExp(tcCookieName + "=");
		if (lckMyCookies[i].search(re) >= 0) {
			return lckMyCookies[i];
		}
	}
	//return a blank value if it didnt exist before.
	return tcCookieName + "=";
}


/**********************************************\
* Function:	CRSubmit()
* Author: 	Evie Platt
* Created:	30 Mar 2004
* Description: 
*		Submit the page if enter is pressed
\**********************************************/
function CRSubmit(e, formname)
{
	var key;
	var keyval = 13;
	var lReturn = false;
	
	// If this is Firefox, you don't need to do the submit.
	if (navigator.userAgent.indexOf('Firefox') == -1)
	{
		key = e.keyCode
		if (key == keyval)
		{
			var cLayoutPage = formname.elements['CriteriaLayoutType'].value;	
			lReturn = (ClickStartSearch(formname, cLayoutPage));
		}
	}
	return lReturn;

}

/**********************************************\
* Function: EmptyNull()
* Author: Greg Miley
* Description: 
*	Returns true if string is only whitespace 
*	or null.
\**********************************************/
function EmptyNull(tcString) 
{
	var re = / /g;
	tcString = tcString.replace(re, "");

	if (((tcString != ' ') && (tcString != '\n') && (tcString != '\t') && (tcString != '')) && (tcString != null)) return false;
	return true;
}

/**********************************************\
* Function: RTrim()
* Author: Evie Platt
* Description: 
*	Remove trailing white space.
\**********************************************/
function RTrim(tcString) 
{
	var re = /\s$/g;
	var lcString = tcString.replace(re, "");
	return lcString
}

/**********************************************\
* Function: FieldCheck()
* Author: Greg Miley
* Description: 
*	Checks fields of forms for blank on 
*	required items.
\**********************************************/
function FieldCheck(tcFormName) {
	var lcMsg = "";
	var llError = 0;
	var lcEmptyFields = "";
	
	for (i=0; i< tcFormName.length; i++)
	{
		var lcElem = tcFormName.elements[i];
		//Check for empty or null text fields.

		if ((((lcElem.type == "text") || (lcElem.type == "textarea")) && (EmptyNull(lcElem.value)&& lcElem.name.substring(0,7) != 'noquery')) && 
			(lcElem.attributes["required"] && lcElem.attributes["required"].value == 1))
		{
			llError++;
			lcEmptyFields += "\n	" + lcElem.id;
			continue;
		}
	}

	if (llError > 0) {
		lcMsg = "_____________________________________________________________\n\n";
		lcMsg += "The form was not submitted because the following fields contained \n";
		lcMsg += "invalid data. Please check each required field and try again.\n";
		lcMsg += "_____________________________________________________________\n\n";
		lcMsg += lcEmptyFields;
		alert(lcMsg);
		return false;
	}
	else
	{
		return true;
	}
}

function popUp(url)
{
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
self.name = "mainWin";
}

/**********************************************\
* Function: Format Currency
* Author: javascript.internet.com
* Description: 
*	Formats a number into currency
\**********************************************/
function FormatCurrency(num) 
{
	var sign, num, cents;
	
	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));

	return (((sign)?'':'-') + '$' + num + '.' + cents);

}

/**********************************************\
* The following functions are used to
* verify a date or a number.
\**********************************************/
/**********************************************\
* Function:	ValidateDate()
* Author: 	Evie Platt
* Created:	?? ??? ??
* Description: 
*	Determine if date is valid.
*
* Modified:
*	Who		When		Why
*	ep		27 Jul 04	Set focus back to field
\**********************************************/
function ValidateDate(tcDate, tlCheckToday, tcID)
{

	var obj = (dom)? document.getElementById(tcID): document.all[tcID];
	var dToday = new Date();
	var dChkDate = new Date(tcDate);
	
	if (tlCheckToday == true)
	{
		if(dChkDate < dToday)
		{
			alert ("The date cannot be before today.");
			obj.focus()
			return false
		}
	}

	if (!isDate(tcDate, dToday.getFullYear() - 1, dToday.getFullYear() + 2))
	{
		obj.focus()
		return false
	}
    return true
 }
 
 /**********************************************\
* Function:	ValidateInteger()
* Author: 	Evie Platt
* Created:	?? ??? ??
* Description: 
*	Determine if date is valid.
*
* Modified:
*	Who		When		Why
*	ep		27 Jul 04	Set focus back to field
\**********************************************/
function ValidateInteger(tcNumber, tcID)
{

	var obj = (dom)? document.getElementById(tcID): document.all[tcID];
	if (!isInteger(tcNumber))
	{
		alert("Please enter a valid number");
		obj.focus();
		return false;
	}
	return true;  
 }
 
function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
	{   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true; 
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf("/")
	var pos2=dtStr.indexOf("/",pos1+1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strMonth=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) 
	{
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1)
	{
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 )
	{
		alert("Please enter a valid 4 digit year")
		return false
	}
	if (dtStr.indexOf("/",pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, "/"))==false)
	{
		alert("Please enter a valid date")
		return false
	}
return true
}

/**********************************************\
* Function:	ClickConfirm()
* Author: 	Evie Platt
* Created:	12 Nov 2003
* Description: 
*		Passes back the value of the button
*		that was selected
* Note:
*		To you this function, your form needs		
*		to be called confirmupdate and
*		the buttons named confirm
\**********************************************/
function ClickConfirm(tcAction)
{
	document.confirmupdate.elements['confirm'].value = tcAction;
	confirmupdate.submit();
}


function PrintScreen() 
{
	window.open('', 'PrintScreen', 'menubar=0,status=0,width=600,height=500,scrollbars,left=10000,top=100,screenX=10000,screenY=100');
	Browser.ObjRef('printForm').submit();
}

