// Madgex Limited
// Copyright (c) 2004 Madgex Limited. All Rights Reserved.
// Validation - Core Components Project
// 12 August 2004
// Version 1.0.0

function CheckDateFormat( sFieldName )
{
	//alert ( sFieldName );
	// PJM - 18-05-2005: need to amend this so it checks more than the first form in a 
	// documents form collection - the method employed will mean that each element in
	// a document will be required to have a different name
	// eval( 'var elt = document.forms[0].' + sFieldName )
	var elt = null;	
	if (document.forms.length == 1)
	{
		eval('elt = document.forms[0].' + sFieldName)
	}
	else
	{
		for (var i=0; i<document.forms.length; i++)
		{
			eval('elt = document.forms[' + i + '].' + sFieldName);
			if (elt != null)
			{
				break;
			}
		}
	}
	if (elt != null)
	{
		CheckDateFormatElt( elt )
	}
}

function CheckDateFormatElt( elt )
{
	var sText = elt.value;
	var sMsg = ''
	var sRes = ''
	
	if ( sText && 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 CheckInteger( sFieldName )
{
	// PJM - 18-05-2005: need to amend this so it checks more than the first form in a 
	// documents form collection - the method employed will mean that each element in
	// a document will be required to have a different name
	// eval( 'var elt = document.forms[0].' + sFieldName )
	var elt = null;	
	if (document.forms.length == 1)
	{
		eval('elt = document.forms[0].' + sFieldName)
	}
	else
	{
		for (var i=0; i<document.forms.length; i++)
		{
			eval('elt = document.forms[' + i + '].' + sFieldName + ';');
			if (elt != null)
			{
				break;
			}
		}
	}
	if (elt != null)
	{
		var sText = elt.value;

		var sMsg = ''
		var sRes = ''
		
		if ( sText != '' )
		{	sRes = parseInt( sText,10);
		
			if ( isNaN(sRes) )
				sMsg = 'Invalid number'
		}
		
		if ( sMsg != '' )
		{
			alert( sMsg )
			elt.focus()
			elt.select()
		}
		else
		{
			elt.value = sRes
		}
	}
}

function CheckMoney( sFieldName )
{
	var sText = elt.value
	var sMsg = ''
	var sRes = sText
	
	if ( sText != '' )
	{
		if (sText.substr(0,1) == '.')
			sText = '0' + sText
	
		var d = sText.split('.')

		if ( d.length == 1 )
		{
			d[1] = '00'
		}
	
		if (d.length != 2)
		{
			sMsg = 'Invalid amount'
		}
		else
		{
			if (d[1].length == 1)
			{
				d[1] += '0'; // Pad the fractional amount with a trailing 0 if it's only one character - eg. "1.5" > "1.50"
			}
			
			if ( isNaN(parseInt(d[0],10)) )
			{
				sMsg = 'Invalid amount'
			}
			else if ( isNaN(parseInt(d[1],10)) || parseInt(d[1], 10) < 0 || parseInt(d[1], 10) > 99)
			{
				sMsg = 'Invalid fractional amount'
			}
			else
			{
				sRes = d[0] + '.' + d[1]
			}

		}
	}
		
	if ( sMsg != '' )
	{
		alert( sMsg )
		elt.focus()
		elt.select()
	}
	else
	{
		elt.value = sRes
	}
}

function CheckEmailFormat( sFieldName )
{
	// PJM - 18-05-2005: need to amend this so it checks more than the first form in a 
	// documents form collection - the method employed will mean that each element in
	// a document will be required to have a different name
	// eval( 'var elt = document.forms[0].' + sFieldName )
	var elt = null;	
	if (document.forms.length == 1)
	{
		eval('elt = document.forms[0].' + sFieldName)
	}
	else
	{
		for (var i=0; i<document.forms.length; i++)
		{
			eval('elt = document.forms[' + i + '].' + sFieldName);
			if (elt != null)
			{
				break;
			}
		}
	}
	if (elt != null)
	{
		var sText = elt.value;

		var sMsg = ''
		var sRes = ''
		
		if ( sText != '' )
		{	
			sRes = sText;

			var d = sText.split('@')

			if ( d.length != 2 )
			{
				sMsg = 'Invalid E-mail Address'

			}
			else
			{
				var dom = d[1].split('.') ;
				
				if ( dom.length < 2 )
				{
					sMsg = 'Invalid E-mail Address'

				}
			}
		}
		
		if ( sMsg != '' )
		{
			alert( sMsg )
			elt.focus()
			elt.select()
		}
		else
		{
			elt.value = sRes
		}
	}
}

function CheckURLFormat( sFieldName ) {
	var regEx = /^(file|http|https):\/\/\S+\.\S+$/i
	
	// PJM - 18-05-2005: need to amend this so it checks more than the first form in a 
	// documents form collection - the method employed will mean that each element in
	// a document will be required to have a different name
	// eval( 'var elt = document.forms[0].' + sFieldName )
	var elt = null;	
	if (document.forms.length == 1)
	{
		eval('elt = document.forms[0].' + sFieldName)
	}
	else
	{
		for (var i=0; i<document.forms.length; i++)
		{
			eval('elt = document.forms[' + i + '].' + sFieldName);
			if (elt != null)
			{
				break;
			}
		}
	}
	if (elt != null)
	{
		var sText = elt.value;
		
		if ( sText != '' && !regEx.test( sText ) )
		{	
			alert( 'Invalid URL' )
			elt.focus()
			elt.select()
		}
	}
 }

function CheckTimeFormat( sFieldName )
{
	// PJM - 18-05-2005: need to amend this so it checks more than the first form in a 
	// documents form collection - the method employed will mean that each element in
	// a document will be required to have a different name
	// eval( 'var elt = document.forms[0].' + sFieldName )
	var elt = null;	
	if (document.forms.length == 1)
	{
		eval('elt = document.forms[0].' + sFieldName)
	}
	else
	{
		for (var i=0; i<document.forms.length; i++)
		{
			eval('elt = document.forms[' + i + '].' + sFieldName);
			if (elt != null)
			{
				break;
			}
		}
	}
	if (elt != null)
	{
		var sText = elt.value;

		var sMsg = ''
		var sRes = ''
		
		if ( sText != '' )
		{
			var d = sText.split(':')
			
			if ( d.length != 2 )
			{
				sMsg = 'Invalid time format, colon needed - ' + sText
			}
			else
			{
				if ( isNaN(parseInt(d[0],10)) || d[0] > 24 || d[0] < 0 )
				{
					sMsg = 'Invalid hour - ' + d[0]
				}
				else if ( isNaN(parseInt(d[1],10)) || d[1] > 59 || d[1] < 0 )
				{
					sMsg = 'Invalid minute - ' + d[1]			
				}
				
				var mn, hr
				
				mn = parseInt(d[0],10)
				if (mn<10)
					mn = '0' + mn
				
				hr = parseInt(d[1],10)
				if ( hr<10 )
					hr = '0' + hr
				
				sRes = mn + ':' + hr
			}
		}
		
		if ( sMsg != '' )
		{
			alert( sMsg )
			elt.focus()
			elt.select()
		}
		else
		{
			elt.value = sRes
		}
	}
}

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 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;
}

function validEmail(item)
{
	if(item.indexOf('@') > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
String.prototype.GetDate = function()
	{
		var sText = this;
		var sMsg = ''
		var sRes = ''
		
		if ( sText != '' )
		{
			var d = sText.split('/')

			if ( d.length == 2 )
			{
				var d = new Date()
				d[2] = d.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)
				sRes =  month + '/' +  day + '/' + parseInt(d[2],10)  
			}
		}
		
		if ( sMsg != '' )
		{
			return null;
		}
		else
		{
			var dRes = new Date( Date.parse( sRes ) );
			return dRes;
		}
	}
