// ************************************************
// Document form validator
//
function gFRM_validate(theForm)
{
	var reason = "";
	
	reason += gFRM_isname(theForm.first_name, "Invalid First Name!\r\n");
	reason += gFRM_isname(theForm.last_name, "Invalid Last Name!\r\n");
	reason += gFRM_isemail(theForm.email, "Invalid Email Address!\r\n");
	reason += gFRM_empty(theForm.country, "Please select a Country!\r\n");
	
	if (reason != "")
	{
    	alert("Some fields need correction:\r\n" + reason);
    	return false;
	}
	return true;
}

function gFRM_empty( field, errMSG )
{
	with ( field )
	{
		if ( value==null || trim(value)=="" )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
	}
}

function gFRM_isname( field, errMSG )
{
    var illegalChars = /[^a-zA-Z\s']/; // allow only letters space and '
 
	if ( gFRM_empty( field, errMSG ) != "" )
	{
		return errMSG;
	}
 	with ( field )
	{
		if ( illegalChars.test(value) )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
    }
}

function gFRM_isalpha( field, errMSG )
{
    var illegalChars = /[^a-zA-Z\s]/; // allow only letters and space 
 
 	with ( field )
	{
		if ( illegalChars.test(value) )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
    }
}

function gFRM_isalnum( field, errMSG )
{
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
 	with ( field )
	{
		if ( illegalChars.test(value) )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
    }
}

function gFRM_isemail( field, errMSG )
{
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
	if ( gFRM_empty( field, errMSG ) != "" )
	{
		return errMSG;
	}
	with ( field )
	{
		if ( !emailFilter.test(value) )
		{
			return errMSG;
		}
		if ( illegalChars.test(value) )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
	}
}

function gFRM_isphone( field, errMSG )
{
    var allowedChars = /[\(\)\.\-\ ]/g ;
	
	with ( field )
	{
		if ( isNaN(parseInt(value.replace(allowedChars,''))) )
		{
			return errMSG;
		}
		else
		{
			return "";
		}
	}
}
// ************************************************
// GLOBAL FUNCTIONS
//
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

//
// Get object information
//
function getInfo( obj ){
	out = "";
	for ( r in obj ){
		out += r + " = " + obj[r] + "\r\n";
	}
	document.getElementById('infoWindow').innerText = out;
}// end function

//
// Do nothing
//
function _void(){
	return;
}