function ValidateEmailToAFriend()
{
	var empty_fields = "";
	var sRecipientEmail = document.EmailToAFriend.sRecipientEmail.value.Trim();
	var sMessage = document.EmailToAFriend.sMessage.value.Trim();
	
	if ( sRecipientEmail == '' )
	{
		empty_fields += "\n        - Recipient Email Address";
		if (! empty_fields)
		{
			document.EmailToAFriend.sRecipientEmail.focus();
			document.EmailToAFriend.sRecipientEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sRecipientEmail) )
		{
			empty_fields += "\n        - Invalid Recipient Email Address";
			if (! empty_fields)
			{
				document.EmailToAFriend.sRecipientEmail.focus();
				document.EmailToAFriend.sRecipientEmail.select();
			}
		}
	}
	
	if ( sMessage == '' )
	{
		empty_fields += "\n        - Message";
		if (! empty_fields)
		{
			document.EmailToAFriend.sMessage.focus();
			document.EmailToAFriend.sMessage.select();
		}
	}

	return alertUser(empty_fields);
}

function ValidateFeedback()
{
	var empty_fields = "";
	var sSenderEmail = document.Feedback.sSenderEmail.value.Trim();
	var sMessage = document.Feedback.sMessage.value.Trim();
	
	if ( sSenderEmail == '' )
	{
		empty_fields += "\n        - Your Email Address";
		if (! empty_fields)
		{
			document.EmailToAFriend.sSenderEmail.focus();
			document.EmailToAFriend.sSenderEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sSenderEmail) )
		{
			empty_fields += "\n        - Invalid Your Email Address";
			if (! empty_fields)
			{
				document.EmailToAFriend.sSenderEmail.focus();
				document.EmailToAFriend.sSenderEmail.select();
			}
		}
	}
	
	if ( sMessage == '' )
	{
		empty_fields += "\n        - Message";
		if (! empty_fields)
		{
			document.EmailToAFriend.sMessage.focus();
			document.EmailToAFriend.sMessage.select();
		}
	}

	return alertUser(empty_fields);
}

/* Generic JavaScript Validation Functions */
	function alertUser(empty_fields)
	{
		var msg="";
		
		if (empty_fields){
			msg += "Please enter the following required information\n in order to submit your application:"
					+ empty_fields + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}

	function alertUserDates(invalid_Dates)
	{
		var msg="";
		
		if (invalid_Dates){
			msg += "Invalid date range:"
					+ invalid_Dates + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}


function CheckValidEmail( sText )
{
		
	// Assumes sText is not empty
	var d = sText.split('@')

	if ( d.length != 2 )
	{
		return false;
	}
	else
	{
		var dom = d[1].split('.') ;
		
		if ( dom.length < 2 )
			return false;
	}
	
	return true;
}

String.prototype.IsWhiteSpace = function()
{
	return this == ' ' || this == '\t';
}

String.prototype.TrimLeft = function()
{
	var i=0;
	
	while (this.charAt(i).IsWhiteSpace())
		i++;
		
	return this.substr(i)
}

String.prototype.TrimRight = function()
{
	var i=this.length;
	
	while (this.charAt(i).IsWhiteSpace())
		i--;
		
	return this.substr(0,i+1)
}

String.prototype.Trim = function()
{
	return this.TrimLeft().TrimRight();
}

function CheckDateFormatElt( elt )
{
	var sText = elt.value;
	var sMsg = ''
	var sRes = ''
	
	if ( sText != '' )
	{
		var d = sText.split('/')

		if ( d.length == 2 )
		{
			var dt = new Date()
			d[2] = dt.getFullYear()
		}
		
		if ( d.length != 3 )
		{
			sMsg = 'Invalid date format - ' + sText
		}
		else
		{
			if ( isNaN(parseInt(d[0],10)) || d[0] > 31 || d[0] < 1 )
			{
				sMsg = 'Invalid day of month - ' + d[0]
			}
			else if ( isNaN(parseInt(d[1],10)) || d[1] > 12 || d[1] < 1 )
			{
				sMsg = 'Invalid month - ' + d[1]			
			}
			else if ( isNaN(parseInt(d[2],10)) )
			{
				sMsg = 'Invalid day of year - ' + d[2]			
			}
			else if ( d[2] > -1 && d[2] < 100 )
			{
				d[2] = parseInt(d[2],10) + 2000
			}
			
			if ( d[2] < 1900 || d[2] > 3000 )
			{
				sMsg = 'Invalid year - ' + d[2]
			}
			
			var day, month
			
			day = parseInt(d[0],10)
			if (day<10)
				day = '0' + day
			
			month = parseInt(d[1],10)
			if ( month<10 )
				month = '0' + month
			
			sRes = day + '/' + month  + '/' + parseInt(d[2],10) 
		}
	}
	
	if ( sMsg != '' )
	{
		alert( sMsg )
		elt.focus()
		elt.select()
	}
	else
	{
		elt.value = sRes
	}
}
function CheckDateFormat ( sText )
{
	var sMsg = ''
	var sRes = ''
	
	if ( sText != '' )
	{
		var d = sText.split('/')

		if ( d.length == 2 )
		{
			var dt = new Date()
			d[2] = dt.getFullYear()
		}
		
		if ( d.length != 3 )
		{
			return false;
		}
		else
		{
			if ( isNaN(parseInt(d[0],10)) || d[0] > 31 || d[0] < 1 )
			{
				return false;
			}
			else if ( isNaN(parseInt(d[1],10)) || d[1] > 12 || d[1] < 1 )
			{
				return false;			
			}
			else if ( isNaN(parseInt(d[2],10)) )
			{
				return false;		
			}
			else if ( d[2] > -1 && d[2] < 100 )
			{
				return false;
			}
			
			if ( d[2] < 1900 || d[2] > 3000 )
			{
				return false;
			}
			
			var day, month
			
			day = parseInt(d[0],10)
			if (day<10)
				day = '0' + day
			
			month = parseInt(d[1],10)
			if ( month<10 )
				month = '0' + month
			
			sRes = day + '/' + month  + '/' + parseInt(d[2],10) 
		}
	}
	
	return true ;
}
// used to prevent against cross site scripting
function StripHTMLTags( elt )
{
	var sText = elt.value;
	sText1 = sText.replace(/<(\S+).*>/, ""); 
	// We can use this from other functions or just on its own. 
	// If on its own, just replace the element value.
	if ( arguments.length > 1  && arguments[1] == true )
	{
		return sText1;
	}
	else
	{
		elt.value = sText1;
	}
		
}