// Madgex Limited
// Copyright (c) 2004 Madgex Limited. All Rights Reserved.
// Validation - Core Components Project
// 12 August 2004
// Version 1.0.0

/*
Mofication
----------
Date:		20th April 2005
Author:		Paul Bilton & Oliver Southgate (Written by PB, added by OS)
Purpose:	With the existing date validation, it only checks for numeric 
			structure and does not account for number of days within the 
			month & Feb leap year day’s count.
Example:	<input type="text" id="txt1" value="" onblur="CheckDateFormatNew(this)">
*/

/*
Mofication
----------
Date:		6th May 2005
Author:		Oliver Southgate
Purpose:	Allows the use of multiple forms, create a variable (frmNumber) which holds a 
			number by default, this can then be set on a perpage level if you need a different form
Example:	frmNumber = 1;
*/

var frmNumber = 0;

function CheckDateFormat( sFieldName )
{
	//alert ( sFieldName );
	eval( 'var elt = document.forms[frmNumber].' + sFieldName )
	CheckDateFormatElt( elt )
}

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 CheckInteger( sFieldName )
{
	eval( 'var elt = document.forms[frmNumber].' + sFieldName )
	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 )
{
	eval( 'var elt = document.forms[frmNumber].' + 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 ( isNaN(parseInt(d[0],10)) )
			{
				sMsg = 'Invalid amount'
			}
			else if ( isNaN(parseInt(d[1],10)) || d[1]<0)
			{
				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 )
{
	eval( 'var elt = document.forms[frmNumber].' + sFieldName )
	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
	
	eval( 'var elt = document.forms[frmNumber].' + sFieldName )
	var sText = elt.value;
	
	if ( sText != '' && !regEx.test( sText ) )
	{	
		alert( 'Invalid URL' )
		elt.focus()
		elt.select()
	}
 }

function CheckTimeFormat( sFieldName )
{
	eval( 'var elt = document.forms[frmNumber].' + sFieldName )
	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;
		}
	}
 //////////////////////////////////////////////////////////////////////
 
 var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function CheckDateFormatNew( txtField ){
  
  var dt=txtField
  if ( dt.value != '' ) {
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
  }

 }
