var IsIE = getBrowser(); //Hold this flag for the duration of the page
var IsInvalid = false;   //Used to prevent IE from generating several errors at once if the
						             //user presses the enter key after inputting a value that is invalid.

function Required(Value, Message){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		IsInvalid = true;
		if(Message == ''){ var Message = 'Please enter a value for the selected field.'; }
		alert(Message);
		return false;
	}
	return true;
}

function IsDate(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	var d = new Date(s);
	if(isNaN(d)){
		IsInvalid = true;
		alert('Please enter a valid date.'); 
		return false;
	}
	return true;
}

function IsNumeric(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	if(isNaN(s)){
		IsInvalid = true;
		alert('Please enter a valid number.');
		return false;
	}
	return true;
}

function IsCurrency(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	var c = '£1234567890.';
	var m = 'Please enter a valid currency.';

	//check for valid chars...
	for(var x = 0; x <= s.length; x++){
		if(c.indexOf(s.charAt(x)) == -1){
			IsInvalid = true;
			alert(m);
			return false;
		}
	}

	//ensure only single dot...
	if((s.length > 0) && (s.indexOf('.') != -1)){
		if(CharCount(s, '.') > 1){
			IsInvalid = true;
			alert(m);
			return false;
		}		
	}
	
	//if a dot is present, force two decimal places...
	if((s.length > 0) && (s.indexOf('.') != -1)){
		if(s.indexOf('.') != s.length - 3){
			IsInvalid = true;
			alert(m);
			return false;
		}		
	}
	
	//ensure only single pound sign...
	if((s.length > 0) && (s.indexOf('£') != -1)){
		if(CharCount(s, '£') > 1){
			IsInvalid = true;
			alert(m);
			return false;
		}		
	}
	
	//ensure pound sign is first char...
	if((s.length > 0) && (s.indexOf('£') != -1)){
		if(s.indexOf('£') > 0){
			IsInvalid = true;
			alert(m);
			return false;
		}		
	}
	
	//two decimal places at most...
	var t = s.substring(s.indexOf('.') + 1);
	if((s.indexOf('.') != -1) && (t.length > 2)){
		IsInvalid = true;
		alert(m);
		return false;
	}

	return true;
}

function IsTelephoneOrFax(Value, IsTelephone){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	var c = '1234567890-+(). ';
	var m = 'Please enter a valid ';
	m += IsTelephone == true ? 'telephone' : 'fax';
	m += ' number.';
	for(var x = 0; x <= s.length; x++){
		if(c.indexOf(s.charAt(x)) == -1){
			IsInvalid = true;
			alert(m);
			return false;
		}
	}
	if(s.length < 6){
		IsInvalid = true;
		alert(m);
		return false;
	}
	return true;
}

function IsValidImage(Value){
	var IsValid = true;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	var c = '.gif';
	var d = '.jpg';
	var m = 'Only GIF and JPG files are supported.';
	if(s.indexOf(c) == -1){
		IsValid = false;
	}
	if(IsValid){
		return true;
	}
	IsValid = true;
	if(s.indexOf(d) == -1){
		IsValid = false;
	}
	if(!IsValid){
		alert(m);
		return false;
	}
	else{
		return true;
	}
}

function IsInRange(Value, Min, Max){
	IsInvalid = false;
	var ErrMsg = 'Incorrect parameter passed to function \'IsInRange\'.'
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	if(isNaN(s)){
		IsInvalid = true;
		alert('Please enter a valid number.');
		return false;
	}
	else{
		var x = Number(s);
		if(isNaN(Min) || (isNaN(Max))){
			IsInvalid = true;
			alert(ErrMsg);
			return false;
		}
		else{
			if((x < Min) || (x > Max)){
				IsInvalid = true;
				alert('Please enter a number between ' + Min + ' and ' + Max + '.');
				return false;
			}
		}
	}
	return true;
}

function IsPositive(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	if(isNaN(s)){
		IsInvalid = true;
		alert('Please enter a valid number.');
		return false;
	}
	if(s <= 0){
		IsInvalid = true;
		alert('Please enter a positive number.');
		return false;
	}
	return true;
}

function IsNegative(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	if(isNaN(s)){
		IsInvalid = true;
		alert('Please enter a valid number.');
		return false;
	}
	if(s >= 0){
		IsInvalid = true;
		alert('Please enter a negative number.');
		return false;
	}
	return true;
}

//check for the '@' sign, at least one dot, and chars before and after both the @ and dot.
function IsEmail(Value){
	
	IsInvalid = false;

	var s = JSTrim(Value);

	//exit if field empty
	if(s == ''){
		return true;
	}

	var m = 'Please enter a valid e-mail address.';
	var i = s.indexOf("@");

	//check for '@'.
	if(i < 0){
		IsInvalid = true;
		alert(m);
		return false;
	}
	//ensure only one '@' present.
	else{
		if((s.indexOf("@", i + 1) != -1) || (i == s.length - 1)){
			IsInvalid = true;
			alert(m);
			return false;
		}
	}
	//check for a dot.
	i = s.indexOf(".");
	if(i < 0){
		IsInvalid = true;
		alert(m);
		return false;
	}
	//ensure there are at least two chars after the dot.
	else{
		i = s.lastIndexOf(".");
		if((i == s.length - 1) || (i == s.length - 2)){
			IsInvalid = true;
			alert(m);
			return false;
		}
	}
	return true;
}

function IsURL(Value){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	var m = 'Please enter a valid web site address.';
	var i = s.indexOf(".");

	//check for dot.
	if(i < 0){
		IsInvalid = true;
		alert(m);
		return false;
	}
	//ensure there are at least two chars after the dot.
	else{
		i = s.lastIndexOf(".");
		if((i == s.length - 1) || (i == s.length - 2)){
			IsInvalid = true;
			alert(m);
			return false;
		}
	}
	return true;
}

function IsValidDate(Day, Month, Year){

	if((Day == '') && (Month == '') && (Year == '')){ return true; }
	if((Day != '') && ((Month == '') || (Year == ''))){ return false; }
	if((Month != '') && (Year == '')){ return false; }
	if((Month == '') && (Year != '')){ return false; }

	//Cannot have 31st of April, June, September or November...
	if(((Month == 'April') || (Month == 'June') || (Month == 'September') || (Month == 'November')) && ((Day == 31))){ 
		return false;
	}
	else if(Month == 'February'){
		//Test for a leap year if the month is February...
		if(Day != ''){
			if(((Year % 4 == 0) && (Year % 100 != 0)) || ((Year % 100 == 0) && (Year % 400 == 0))){
				//Yes - it's a leap year...
				if((Day == 30) || (Day == 31)){
					return false;
				}
			}
			else{
				if((Day == 29) || (Day == 30) || (Day == 31)){
					return false;
				}
			}
		}
	}
	return true;
}

function getBrowser()
{
	var n = new String(navigator.appName);
	var v = new String(navigator.appVersion);
	if ((n.indexOf("Internet Explorer") != -1) && ((v.indexOf("MSIE 4") != -1) || (v.indexOf("MSIE 5") != -1))) return true;
}

function LimitWords(Value, Limit, FieldName){
	IsInvalid = false;
	var s = JSTrim(Value);
	if(s == ''){
		return true;
	}
	if(WordCount(s) > Limit){
		IsInvalid = true;
		alert('Please limit your ' + FieldName + ' to ' + Limit + ' words.'); 
		return false;
	}
	return true;
}

function JSTrim(Text){
	if(Text == ''){
		return '';
	}
	var s = new String(Text);
	var l = Text.length;
	var Start = 0;
	var End = l;
	for(var x = 0; x < l; x++){
		if(s.charAt(x) == ' '){
			Start += 1;
		}
		else{
			break;
		}
	}
	for(var x = l - 1; x >= 0; x--){
		if(s.charAt(x) == ' '){
			End -= 1;
		}
		else{
			break;
		}
	}
	if(End == 0){End = l;}
	s = s.substring(Start, End);
	return s;
}

function JSTrim(Text){
	if((Text == '') || (Text == 'undefined')){
		return '';
	}
	var s = new String(Text);
	var l = Text.length;
	var Start = 0;
	var End = l;
	for(var x = 0; x < l; x++){
		if(s.charAt(x) == ' '){
			Start += 1;
		}
		else{
			break;
		}
	}
	for(var x = l - 1; x >= 0; x--){
		if(s.charAt(x) == ' '){
			End -= 1;
		}
		else{
			break;
		}
	}
	if(End == 0){End = l;}
	s = s.substring(Start, End);
	return s;
}

function WordCount(Text){
	var i = 1;
	var IsSpace = false;
	var s = new String(Text);
	if((s == 'undefined') || (s == '')){
		return 0;
	}
	for(var x = 0; x < s.length; x++){
		if(s.charAt(x) == ' '){
			if(IsSpace == false){
				i += 1;
			}
			IsSpace = true;
		}
		else{
			IsSpace = false;
		}
	}
	return i;
}

function CharCount(Text, Char){
	if((Text == '') || (Char == '')){
		return 0;
	}
	var i = 0;
	var s = new String(Text);
	for(var x = 0; x < Text.length; x++){
		if(s.charAt(x) == Char){
			i += 1;
		}
	}
	return i;
}
