// Utility objects and functions
//
// 08/03 cbh brainworks
//

// Utility Protos
//



// Utility functions
//
function isEmailValid( str)
{
if(str == null || str.length == 0)
	return false; 

pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

return( pattern.test( str));

}

function emitScriptHeader( lang) {
	return "<" + "script language='" + lang + "'>";
}

function endScript() {
	return "<" + "/script>";
}

function emitJsVar( val,name)
{
	return name + "=" + "'" + val + "';";
}

function emitVbVar( val,name)
{
	return name + "=" + "'" + val + "'";
}




//
// return node from dom tree with specified name
//
function getNode( name) {
	return document.getElementById( name);
}

// Trim whitespace using regexp
//
function xtrim( str) {
	s = new String(str);
	trimpat = /^\s*|\s*$/gi
	return s.replace( trimpat, "");
}		


// check if string is null or empty
//
function isNullOrEmpty( str) {
	return ( str == null  ||
	    	xtrim(str) == null);
	    
}

// Convert a date of the form YYYYMMDD
// into a Date object. Returns null if
// handed a parameter that is nonconformant.


function toDate( d) {


	if( !isNullOrEmpty( d) &&
	    d.length == 8) {

		// Specify radix else a leading zero
		// causes parseInt to assume base 8.
		//
		ye = parseInt( d.slice( 0, 4), 10);	
		mo = parseInt( d.slice( 4, 6), 10);
		da = parseInt( d.slice( 6, 8), 10);

		if( !isNaN(ye) && !isNaN(mo) && !isNaN(da)) 
		{
		   ndate = new Date( ye, mo-1, da);
//		   ndate.setFullYear( ye);
//		   ndate.setMonth( mo);
//		   ndate.setDate( da);
		   return ndate;
	  	}
	} else
	return null;
}


//
// This is server-side stuff, doesn't belong here.
//
// redir to start page if session has expired
//
/*

function sessionExpired() {
	id = session( "memid");
	email = session( "email");
	debug = request ("nocookie");
	defaultStart = "default.asp";
	if( !isNullOrEmpty( Application( "Start_page")) )
		defaultStart = Application( "Start_page");

	if( !isEmailValid( email) ||
	     isNullOrEmpty( id))
		Response.redirect( defaultStart + "?error=HA103");

}

function debug( s) {
	Response.write( s + "<br>");
}




*/
