
// Copyright e-Business Systems Limited.    www.e-businesssystems.co.uk


// version 3 - 06/01/09


function Valid_textbox(fname,fdescription){
	var test;
	var result;
	
	test = /[a-zA-Z]/;
	result = test.exec(fname);
	if ((result == null)){
		window.alert("Please fill in the '" + fdescription + "' field.");
		 
		return false;
	}
	return true;
}

function Valid_field(fname,fdescription,required,fmin,fmax,ftype){
	if (required == null) required = "";
	if ((required!="required")&&(fname.length==0)) return true;
	if ((fmin == 0) && (fname.length == 0)) return true;
	if ((required=="required")&&(fname.length==0)){
		window.alert("Please fill in the '" + fdescription + "' field."); 
		return false;
	}	
	if ((fmin!=null)&&(fmin!="")){
		if (fname.length < fmin) {
			window.alert(fdescription + " must be at least " + fmin + " characters long."); 
			return false;
		}
	}
	if ((fmax!=null)&&(fmax!="")){
		if (fname.length > fmax){
			window.alert(fdescription + " must not be more than " + fmax + " characters long.");
			return false;
		}
	}
	if ((ftype!=null)&&(ftype!="")){
		if (ftype == "num"){
			var numeric = /\d+/;
			var result = numeric.exec(fname);
			if (result == null){
				window.alert(fdescription + " must be a number.");
				return false;
			}
			if (result[0]!= fname){
				window.alert(fdescription + " must be a number.");
				return false;
			}	
		}
	}
	
	if ((ftype!=null)&&(ftype!="")){
		if (ftype.indexOf("curr")>=0){
			var curr1,result;
			if (ftype == "currnp") curr1 = /\d{1,6}\.\d{2}/;
			if (ftype == "currvp") curr1 = /\d{1,6}(\.{0,1}\d{0,2})/;
			if (ftype == "currp") curr1 = /£\d{1,6}\.\d{2}/;
			if (ftype == "currpo") curr1 = /£?\d{1,6}\.\d{2}/;
			result = curr1.exec(fname);
			if ((result != null)&&(result[0]== fname))return true;
			if (ftype == "currnp") curr1 = /\d{1,3}\,\d{3}\.\d{2}/;
			if (ftype == "currp") curr1 = /£\d{1,3}\,\d{3}\.\d{2}/;
			if (ftype == "currpo") curr1 = /£?\d{1,3}\,\d{3}\.\d{2}/;
			result = curr1.exec(fname);
			if ((result != null)&&(result[0]== fname))return true;
			return false;	
		}
	}
	return true;
}

function Valid_range(fname,fdescription,fmin_range,fmax_range){
	if (fname=="") return true;
	fname = parseFloat(fname);
	if (fmin_range==null) fmin_range="";
	if ((fmin_range!="")&&(fname < fmin_range)){
		window.alert(fdescription + " must not be less than " + fmin_range);
		return false;
	}
	if (fmax_range==null) fmax_range="";
	if ((fmax_range!="")&&(fname > fmax_range)){
		window.alert(fdescription + " must not be greater than " + fmax_range);
		return false;
	}
	return true;	
}





//------------------------------------
var postcode_is_valid = true;

function Validate_postcode(postcode)
{
	postcode_is_valid = true;
	var i,j,k,matches;
	var regex;
	
	postcode = postcode.toUpperCase();
	
	regex = /\s/g;
	j = postcode.replace(regex,"");
	
	
	i = 0;
	
	
	regex = /[A-Z]{1,2}\d{2,3}[A-Z]{2}|[A-Z]{2}\d{2}[A-Z]{2}|[A-Z]{1,2}\d[A-Z]\d[A-Z]{2}/;
	
	matches = regex.exec(j);
	
	
	
	
	
	if (matches == null)
	{
		postcode_is_valid = false;
	}
	else if (matches[0] == "")
	{
		
		postcode_is_valid = false;
	}
	
	else 
	{
		postcode_is_valid = true;
		j = matches[0];
	}		

	
	i = j.length;
	
	if (postcode_is_valid && (i <= 8)) 
	{		
		var spaces = "          ";
		
		return (j.substring(0, i-3) + spaces.substring(0,8 - i) + j.substring(i - 3,i));
	}
	else
	{
		return postcode;
	}
	
	
}





//------------------------------------
var email_is_valid = true;

function Validate_email_address(email)
{
	email_is_valid = true;
	var i,j,k,matches;
	var regex;
	
	
	
	regex = /\s/g;
	j = email.replace(regex,"");
	
	
	i = 0;
	
	
	//regex = /[\w\d]+[\.\-\w\d]*\@[\w\d\-\.]+/;
	regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;	
	matches = regex.exec(j);
	
	
	
	
	
	if (matches == null)
	{
		email_is_valid = false;
	}
	else if (matches[0] == "")
	{
		
		email_is_valid = false;
	}
	
	else 
	{
		email_is_valid = true;
		j = matches[0];
	}		

	

	return j;
		
	
}









//------------------------ Validate date ----------------------------------




//This function is used to validate date in the format dd/mm/yyyy 
//returns "ok" if date is valid 

function Validate_date(field_ref) 
{
    var validformat=/^\d{1,2}\/\d{1,2}\/\d{4}$/; //Basic check for format validity
    var success = "ok";
    if (!validformat.test(field_ref.value))
    {
    	success = "The date should be in the format dd/mm/yyyy";
    }
    
    else
    { 
	    //Detailed check for valid date ranges
	    var dayfield=field_ref.value.split("/")[0];
	    var monthfield=field_ref.value.split("/")[1];
	    var yearfield=field_ref.value.split("/")[2];
	    
	    var dayobj = new Date(yearfield, monthfield-1, dayfield);
	    if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
	    {
	    	success = "The date appears to be invalid";
	    }
	    else
	    {
	        success = "ok";
	    }
    }
    return success;
} 







