// 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 != '' )
	{
		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 ( d[2] > -1 && d[2] < 100 )
			{
				d[2] = parseInt(d[2],10);	
				var year = (new Date()).getFullYear();			
				var cutOffYear = ((year % 100)+50)%100;
				d[2] += ((year - year % 100) - (d[2] < cutOffYear ? 0 : 100)); 	
			}
			
			if ( isNaN(parseInt(d[0],10)) || d[0] > 31 || d[0] < 1 )
			{
				sMsg = 'Invalid day of month - ' + d[0];
			}
			else if ( (d[1] == 2) && (d[0] > 29))
			{
				sMsg = 'Invalid day of month - ' + d[0];
			}
			else if ( ((parseInt(d[2] / 4) != d[2] / 4)) && (d[0] > 28) && (d[1] == 2) )
			{
				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 year - ' + d[2];		
			}
			
			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 ( 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 )
{
	// 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;
		}
	}

function validateHomePageSearchScreen(){
	var str=document.ICISsearchHome.sKeywords.value.trim();
	if(str.length==0){
		alert('Please enter a search keyword.');
		return false;
	}else{
		return true;
	}
}

function TrimFreeListing(){
	document.frmFreeListing.txtContactName.value=document.frmFreeListing.txtContactName.value.trim();
	document.frmFreeListing.txtCompanyName.value=document.frmFreeListing.txtCompanyName.value.trim();
	document.frmFreeListing.txtTownOrCity.value=document.frmFreeListing.txtTownOrCity.value.trim();
	document.frmFreeListing.txtTelephone.value=document.frmFreeListing.txtTelephone.value.trim();
}

String.prototype.trim = function() {
 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

function LoadURLFromListBox(){
	
	var destination;
	var box = document.RefineSearchList.country;
	destination = box.options[box.selectedIndex].value;
		
	if (destination) location.href = destination;
	//var num = box.selectedIndex;
	//num=num + '';
	//window.status= 'test';
	//alert(destination);
}