// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// api_Common.js
//
// These are CLIENTSIDE JavaScript functions that help us on a "common" basis. For example
// opening new windows, setting focus to windows, cookie stuff...
//
// sections : 
//   SECTION I   = Window Functions
//   SECTION II  = Date / Time Functions
//   SECTION III = Cookie
//   SECTION IV  = Misc Functions
//
// created  : 02.14.00
// updated  : 02.14.00
//
// notes    : 2/14/00 JMB - Today i started the commenting standard...
//            2/14/00 JMB - Split the file up into III sections...
//            7/12/00 JMB - Added Misc Functions
//
//
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


function  SearchValidate(form)
{
	if(form.keyword.value.replace(/\s/g,"")!="")
	{ return true; }
	else
	{
		alert("\nPlease enter a keyword to begin your search.\n\n");
		form.keyword.value="";
		form.keyword.focus();
		return false;
	}
}

function RequireNumber(frmFld,sMsg)
{
	if(isNaN(frmFld.value))
	{
		alert(sMsg);
		frmFld.value="";
		frmFld.focus();
	}

	return;
}

function SpawnUtilityChild(url)
{ SpawnChild(url,450,500); }

function SpawnChild(url,w,h)
{
	win=open(url,'win'+w+h,'width='+w+',height='+h+',scrollbars=yes,resizable=yes,menubar=no,titlebar=no,toolbars=no,status=no');
	win.focus();
}


//
//
// SECTION I 
//
//
// //////////////////////////////////////////////////////////////////////////////////////////////
//
// Window Functions
//
// These functions operate, and manipulate popup windows or any browser window you specify..
//
//
//
// //////////////////////////////////////////////////////////////////////////////////////////////


// JS PROPERTY ***********************************************************************************
//
// window.focus()
//
// This IF-BLOCK takes care of focusing the Popup windows. if there is an opener (parent), then
// set the focus()....
//
// input  : none
//
// output : none
//
// **********************************************************************************************
if(window.opener)
{
 window.focus();
}


// JS PROPERTY ***********************************************************************************
//
// onunload=??
//
// This will set the onunload to call refresh list. common on popups that maintain lists below..
//
// input  : none
//
// output : none
//
// **********************************************************************************************
//if(opener){ if(opener.RefreshList){ window.onunload = window.opener.RefreshList; }}

//
// Written from MIBOR, used in new site
//
function InfopopUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300,left = 650,top = 450');");
}



// FUNCTION *************************************************************************************
//
// PopupWindow
//
// This function pops-up a window of size w and h...
//
// input  : url [string], width [number], height [number], windowName [string], 
//          allowScrolling [boolean]
//
// output : funcWindow [object]
//
// **********************************************************************************************
function PopupWindow(page, width, height, windowName, allowScrolling, useModal) 
{ 
 scrollFlag     = (allowScrolling) ? "yes" : "no";
 openParameters = "width="+width+",height="+height+",resizable=yes,scrollbars="+scrollFlag+",menubars=no,toolbar=yes,directories=no";

 funcWindow = window.open(page, windowName, openParameters);

 return funcWindow;
} 



// FUNCTION *************************************************************************************
//
// Redirect
//
// This function redirects to a url specified using the location.href object...
//
// input  : URL [string]
//
// output : none
//
// **********************************************************************************************
function Redirect(URL)
{ 
 if(URL != "")
 {
   window.location.href=URL;
 }
}



//
//
// SECTION III
//
//
// //////////////////////////////////////////////////////////////////////////////////////////////
//
// Cookie Functions
//
// These functions get and/or set cookies
//
//
//
// //////////////////////////////////////////////////////////////////////////////////////////////


// FUNCTION *************************************************************************************
//
// GetCookie
//
// This function gets the cookie value of cookieName
//
// input  : cookieName [string]
//
// output : cookieValue [string]
//
// **********************************************************************************************
function GetCookie(check_name) 
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			cookie_value = cookie_value.replace(/\+/g," ");
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}



// FUNCTION *************************************************************************************
//
// SetCookie
//
// This function sets a cooke of cookieName with cookieValue for cookieLife
//
// input  : cookieName [string], cookieValue [string], cookieLife [date obj]
//
// output : none
//
// **********************************************************************************************
function setCookie(cookieName, cookieValue, cookieLife)
{
   expirationDate = new Date();

   if(cookieLife == null || cookieLife != -1)
   {
    cookieLife = 365*86400000;
    expirationDate.setTime(expirationDate.getTime() + cookieLife);
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + ";expires=" + expirationDate.toGMTString() + ";path=/";
   }
   else
   {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + ";path=/";
   }
} 





//
//
// SECTION IV
// ----------
//
//
// //////////////////////////////////////////////////////////////////////////////////////////////
//
// Misc Functions
// 
// This section holds all general things...
// 
// //////////////////////////////////////////////////////////////////////////////////////////////

// FUNCTION ************************************************************************************
//
// FormatMoney
//
// This function take a FLOAT and format it into money with commas and optional "$"
//
// Input  : number [integer], showDollarSign [string, optional]
//
// Output : money [string]
//
// *********************************************************************************************
 function FormatMoney(num,wantDollarSign)
 {
   tensCount = 1;
   money     = "";
   num       = (num+"").replace(/\$|\,/g,"");
   numArr    = num.split(".");

   // dollars
   if(numArr[0].length > 3){
    for(x=numArr[0].length-1;x>=0;x--)
    {
      if(tensCount == 3 && x != 0){ comma = ","; tensCount = 0;} else { comma = ""; }
      money = comma + numArr[0].substr(x,1) + money;
  	  tensCount++;
    }
   }
   else { money=numArr[0]; }

   // cents
   if(numArr[1]){money += "." + numArr[1].substr(0,2); }
  
   // dollar sign?
   if(wantDollarSign) { money = "$"+money; }
  
   return money;
 }

// -- IE ONLY
// onkeypress, returns the item if it is a digit! :)
//
function IsDigit()
{ 
  return ( (event.keyCode >= 48) && (event.keyCode <= 57) || (event.keyCode ==46)); 
}

