
//window.onerror = errorHandler;

window.onload = body_onload_global;

function body_onload_global()
{
	if ( bPlaceFocus ) 
		placeFocus();
	
	try
	{
		body_onload();
	}
	catch(e)
	{
	}
}

function fixcode(unclean) 
{ 
	var clean = unclean.replace(/%27%/g,"'"); 
	return clean; 
} 

function javascriptString( str )
{
  var sTemp = str.replace( /\\/g, '\\\\');
  return sTemp.replace( /\'/g, '\\\'');
}

var bPlaceFocus = true;
function placeFocus() 
{
	var j, i, field;

	if (document.forms.length > 0)
		for ( j = 0; j < document.forms.length; j++ )
			for (i = 0, field = document.forms[j]; i < field.length; i++) 
				if ( !field.elements[i].disabled && field.elements[i].style.display != "none" )
					switch ( field.elements[i].type )
					{
						case 'text':		;
						case 'textarea':	;
						case 'password':	field.elements[i].focus();
											return;
					}
}

function createWindow(URL, name, options)
{
	var j = window.open(URL, name, options); 
	j.focus();
}

function closeWindow(bRefreshParent)
{
	if (bRefreshParent)
		window.opener.location.reload();
	
	window.close();
}

// Returns true if the given string contains no printable characters.
function isStringEmpty(str)
{
	if (!str) return true;
	return !str.match(/[A-Za-z0-9_]/,'i');
}

// Focus's the browser on the specified object.
function objFocus(obj)
{
	//if ()// && obj.style.display != "none", obj.style.visibility == "visible"
	//{
		if ( obj.length > 1)
			for( var i = 0; i < obj.length; i++ )
			{
				if ( obj[i].type != "hidden" && !obj[i].disabled && obj[i].style.display != "none" )
				{
					objFocus( obj[i] );
					break;
				}
			}
		else ( obj.type != "hidden" && !obj.disabled && obj.style.display != "none" )
		{
			if ( obj.focus )
				obj.focus();
			if (obj.type == "text" || obj.type == "textarea" || obj.type == "password")
				obj.select();
		}
	//}
}

// Returns true if the given object contains only alpha-numeric chars.
function validateNumeric(obj, sMsg)
{
	return validateRegExp(obj, /^-*(\d+,)*\d+$/, sMsg);
}

// Returns true if the given object contains a guid.
function validateGUID(obj, sMsg)
{
	return validateRegExp(obj, /^\{*[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}*$/, sMsg);
}

// Returns true if the given object contains a valid floating point number.
function validateFloat(obj, sMsg)
{
	return validateRegExp(obj, /^-*(\d+,)*\d+(\.\d+)?$/, sMsg);
}

// Returns true if the given object contains valid text.
function validateText( obj, sMsg )
{
	return validateRegExp( obj, /[\w]/, sMsg );
}

// Returns true if the given object contains a valid floating point number.
function validateEmail(obj, sMsg)
{
	return validateRegExp(obj, /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/, sMsg);
}

function validateURL( obj, sMsg )
{
	return validateRegExp( obj, /^https?:\/\/([a-zA-Z0-9\-]*\.)*[a-zA-Z0-9\-]+(:[0-9]+)?(\/|(\/([a-zA-Z0-9\-_\.]+\/)*[a-zA-Z0-9\-_\.]*((\?[a-zA-Z0-9\-%]+(=[a-zA-Z0-9\-%]*)?){1}(&[a-zA-Z0-9\-%]+(=[a-zA-Z0-9\-%]*)?)*)?)?)?$/, sMsg )
}

function validateRegExp(obj, regExp, sMsg)
{
	if ( obj ) 
	{
		// Check that it isn't the null value;
		var sValue = new String( obj.value );
		
		if ( !(sValue.match( regExp, 'i' ) ) )
		{
			if ( sMsg && sMsg.length > 0 ) 
			{
				//objFocus( obj );				
				alert( sMsg );
			}
			return false;
		}
	}
	return true;
}

function ValidateControl( obj, bIsRequired, sDatType, sObjNarrative )
{
	var bErr = false,
		sValue = null,
		i, strarray, sPickNar, objType;
		
	objType = ( obj.type ? obj.type : ( obj.length > 1 ? obj[0].type : null ) )

	// Get the value from the object.
	switch ( objType )
	{

		case 'select-one':			sValue = obj.value;
          									sPickNar = 'Please select ' + selectSingular( sObjNarrative ) + ' ';
          									break;
									
		case 'text':				;
		case 'password':			;
		case 'hidden':				;
		case 'textarea':			sValue = obj.value;
									sPickNar = 'Please enter a valid ';
									break;

		case 'radio':				if (obj.length > 1)
									{
										for (i = 0; i < obj.length; i++)
											if (obj[i].checked)
											{
												sValue = obj[i].value;
												break;
											}
									}
									else if (obj.checked)
										sValue = obj.value;
									
									sPickNar = 'Please select ' + selectSingular( sObjNarrative ) + ' ';
									break;
									
		case 'checkbox':			if (obj.length > 1)
									{
										for (i = 0; i < obj.length; i++)
											if (obj[i].checked)
												sValue = sValue + (i > 0 ? ', ' : '' ) + obj[i].value;
									}
									else if (obj.checked)
										sValue = obj.value;
									//alert(sValue);
									sPickNar = 'Please select ' + selectSingular( sObjNarrative ) + ' ';
									break;

		case 'select-multiple':		for (i = 0; i < obj.options.length; i++)
										if ( obj.options(i).selected )
											sValue = sValue + (sValue.length > 0 ? ', ' : '' ) + obj.options(i).value;
									
									sPickNar = 'Please select ' + selectSingular( sObjNarrative ) + ' ';
									break;
								
		default:					alert( "Unknown Control Type: " + obj.name );
									//alert( "Unknown Control Type: '" + obj.type + "' (document." + obj.form.name + "." + obj.name + ")" );
									return false;
	}
	
	// Abort as successful if object is empty but optional anyway.
	if ( !bIsRequired && isStringEmpty( sValue ) )
	{
		return true;
	}

	// Evaluate the data against 
	switch ( sDatType )
	{
		case 'text':		bErr = isStringEmpty( sValue );
							break;
							
		case 'date':		bErr = !getDate( sValue );
							break;

		case 'time':		bErr = !getTime( sValue, new Date )
							break;
															
		case 'currency':	bErr = !sValue.match( /^-?(\d+,)*\d+\.\d{2,2}$/, 'i' );
							break;
		
		// Accept currency and number values
		case 'money':		bErr = !sValue.match( /^-?((\d+,)*(\d+\.\d{1,2})|(\d+))$/, 'i' );
							break;

		case 'decimal':		bErr = !sValue.match( /^-?(\d+,)*\d+(\.\d+)?$/, 'i' );
							break;
							
		case 'number':		bErr = !sValue.match( /^-?(\d+,)*\d+$/, 'i');
							break;
							
		case 'percentage':	bErr = !sValue.match( /^-?((\d{1,3}\.\d{1,2})|(\d{1,3}))$/, 'i' );
							break;
							
		case 'email':		bErr = !sValue.match( /^([^\x00-\x1f\x80-\xff\x7f<>\(\)\\,;:""@]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/, 'i' );
							break;

		case 'url':			bErr = !sValue.match( /^https?:\/\/([a-zA-Z0-9\-]*\.)*[a-zA-Z0-9\-]+(:[0-9]+)?(\/|(\/([a-zA-Z0-9\-_\.]+\/)*[a-zA-Z0-9\-_\.]*((\?[a-zA-Z0-9\-%]+(=[a-zA-Z0-9\-%]*)?){1}(&[a-zA-Z0-9\-%]+(=[a-zA-Z0-9\-%]*)?)*)?)?)?$/, 'i' );
							break;
							
		case 'phone':		bErr = !sValue.match( /^(\+\d{1,3}\s*)?(\(\d{2,3}\))?([-| ]*\d+)+$/, 'i' );
							break;
							
		default:			alert( "Unknown Data Type: '" + sDatType + "' (document." + obj.form.name + "." + obj.name + ")");
							return false;
	}
	
	// Handle the error situation.
	if ( bErr )
	{
		objFocus( obj );
		alert( sPickNar + sObjNarrative );
		return false;
	}
	return true;
}

function selectSingular( noun )
{
	switch ( noun.charAt(0) )
	{
		case 'a':	;
		case 'A':	;
		case 'e':	;
		case 'E':	;
		case 'i':	;
		case 'I':	;
		case 'h':	;
		case 'H':	;
		case 'u':	;
		case 'U':	return 'an';
	}
	return 'a';
}

