/**************************/

function setFocus() {
	var checkForm = document.getElementById("check");
    if (checkForm) {
        checkForm["PostCode"].focus();
    }
}
    
String.prototype.HTMLencode = function() {
	if ( this.length > 0 ) {
		var div = document.createElement('div');
		var text = document.createTextNode(this);
		div.appendChild(text);
		return div.innerHTML;
	} 
	else {
		return this;
	}
};

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
};
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
};

String.prototype.left = function(n) {
	if (n <= 0)
	    return "";
	else if (n > this.length)
	    return this;
	else
	    return this.substring(0,n);
};

String.prototype.right = function(n) {
    if (n <= 0)
       return "";
    else if (n > this.length)
       return this;
    else {
       return String(this).substring(this.length, this.length - n);
    }
};

/**************************/

function WinEvObj(e) {
	this.key = window.event ? e.keyCode : e.which;
	this.c = String.fromCharCode(this.key);
	
	if (!e)
		var e = window.event;
		
	if (e.target)
		this.target = e.target;
	else if (e.srcElement)
		this.target = e.srcElement;

	if (this.target.nodeType == 3) // defeat Safari bug
		this.target = this.target.parentNode;
}

/**************************/

function ndGetElementByID( id ) {	
	return document.all( id ); 
}

/**************************/

function chkSpecialKey( e, iSpace, iAlpha, iNum, iApos, strExtraChars ) {
	var w = new WinEvObj(e);
	
	if ( w.key == 13 || w.key == 8 || w.key == 0 || ( iSpace == 1 && w.key == 32 ) ) {
		return true;
	} else {
		if ( w.c == "'" && iApos == 1 ) 
			return true;
		if ( strExtraChars.indexOf( w.c ) >= 0 )
			return true;
		if ( w.c.match(/^[0-9]$/) != null && iNum == 1 )
			return true;
		if ( w.c.match(/^[A-Za-z]$/) != null && iAlpha == 1 )
			return true;
	}
	return false;
}

function chkTelKey(e) {
	var w = new WinEvObj(e);
	var strVal = w.target.value;
	var blnRet = true;
	var strTmp;
	
	if ( w.key == 13 || w.key == 8 || w.key == 0 ) {
		return true;
	} else {
		if ( strVal.length == 0 && w.c == "+" ) {
			return true;
		} else if ( w.c.match(/^[0-9]$/) != null ) {
			return true;
		} else 	{
			blnRet = false;
		}
	}
	return blnRet;
}

function chkNum(e, nInt, nDec, iNeg ) {
	var w = new WinEvObj(e);
	var strVal = w.target.value;
	var blnRet = true;
	var strTmp;
	
	if ( w.key == 13 || w.key == 8 || w.key == 0 ) {
		return true;
	} else {
		if ( ( iNeg == 1 && w.c == "-" ) && strVal.length == 0 ) {
			return true;
		}
		if 	( nDec > 0 && ( w.c == "." && ( strVal.indexOf( "." ) < 0 ) && strVal.length > 0 ) )  
		{
				return true;
		} else if ( w.c.match(/^[0-9]$/) != null ) {

			if ( strVal.indexOf( "." ) > 0 ) {
				if ( ( strVal.length - strVal.indexOf( "." ) ) <= nDec ) {
					return true;
				} else  {
					return false;
				}
			} else 	if ( strVal.length - strVal.indexOf( "-" ) > nInt ) {
				return false
			}
		} else if ( document.selection == strVal ) {
			return true;
		} else 	{
			blnRet = false;
		}
	}
	return blnRet;
}


function fmtPostCode(Param,v) {
	var re = / /i;
	var strTmp = Param.replace(re,"").toUpperCase();
	var iLen = strTmp.length;
	var strSuffix;
	var strPrefix;
	var strNum;
	
	v.ErrorText = "";
	
	if ( iLen < 9 && iLen > 4 ) {
		strSuffix = strTmp.right(2);
		if ( strTmp.substr(2,1).match(/^[0-9]$/) ) {
			strNum = strTmp.substr( 2, iLen - 3);
			strPrefix = strTmp.left(1);
		} else {
			strNum = strTmp.substr( 3, iLen - 4);
			strPrefix = strTmp.left(1);
		}
		strTmp = strTmp.left(iLen - 3) + " " + strTmp.right(3);
		if ( ! strTmp.match(/^[A-Z]{1,2}([1-9]{1}[0-9]{0,1}|[1-9][A-Z]) [0-9][A-Z]{2}$/) ) {
			v.ErrorText = "'" + Param + "' does not look like a valid Postcode. Please try again.";
			strTmp = "";
		}
	} else {
		v.ErrorText = "'" + Param + "' does not look like a valid Postcode. Please try again.";
		strTmp = "";
	}
	return strTmp;
}

function fmtBCSRef(Param, v) {
	var strTmp = Param.toUpperCase();
	
	v.ErrorText = "";

	if ( strTmp.length > 0 ) {
		if ( ! strTmp.match(/^(BCS\/1\/)[0-9]{1,14}$/) ) {
			v.ErrorText = "'" + Param + "' does not look like a valid BCS Reference. Please try again.";
			strTmp = "";
		}
	}

	return strTmp;
}

function fmtClaimNo(Param, v) {
	var strTmp = Param.toUpperCase();
	
	v.ErrorText = "";

	if ( strTmp.length > 0 ) {
		if ( ! strTmp.match(/^[0-9A-Za-z\/]{1,30}$/) ) {
			v.ErrorText = "'" + Param + "' does not look like a valid Claim Number. Please try again.";
			strTmp = "";
		}
	}

	return strTmp;
}

/*************************************************************/

function ValidationRule(v) {
	this.ErrorText = "";
	this.SetErrorText = function SetErrorText() {
		if ( this.ErrorText.length > 0 ) {
			//document.getElementById( v.id ).style.backgroundColor = "#F88";
			// document.getElementById( v.id + "_lb").title = this.ErrorText.HTMLencode();
		}
		else {
			//document.getElementById( v.id ).style.backgroundColor = "#8F8";
			// document.getElementById( v.id ).style.backgroundColor = "";
		}
		// ndGetElementByID( v.id + "_em").innerHTML = this.ErrorText.HTMLencode();
	};
}

function vldRegExp( t, strName, re ) {
	if( !( strName.right(2) == "RQ" && t.value.length == 0 ) ) {
		v = new ValidationRule( t );

		if ( ("" + t.value).match(re) == null ) {
			v.ErrorText = "'" + t.value + "' is not a valid " + strName;
			t.value = "";
		}
		else {
			v.ErrorText = "";
		}
	}
	else {
		v.ErrorText = "";
	}

	v.SetErrorText();
	return t.value;
}

function vldInput( t, sType ) {
	var strResult;
	var blnFound = true;	
	//
	// WARNING - Regular expressions are very fragile
	// A mistake here will break the page
	//
	switch ( sType ) {
	case "INT":
		strResult = vldRegExp( t, sType, /^\+{0,1}[0-9]*$/ );
		break;
	case "MONEY":
		strResult = vldRegExp( t, sType, /^[0-9]{1,8}(\.[0-9]{1,2})?$/ );
		break;
	case "LASTNAME":
		strResult = vldRegExp( t, sType, /^[A-Za-z\x20'-]+$/ );
		break;
	case "INITIALS":
		strResult =  vldRegExp( t, sType, /^[A-Z](\x20[A-Z])+$/ );
		break;
	case "CAPS":
		strResult = vldRegExp( t, sType, /^[A-Z](\x20[A-Z])+$/ );
		break;
	case "FIRSTNAME":
		strResult = vldRegExp( t, sType, /^^[A-Za-z\x20'-]+$/ );
		break;
	case "TELNUM":
		strResult = vldRegExp( t, sType, /^\+?[0-9\x20]+$/ );
		break;
	case "EMAIL":
		strResult = vldRegExp( t, sType, /^\+{0,1}[0-9]+$/ );
		break;
	case "ADDRESS":
		strResult = vldRegExp( t, sType, /^[A-Za-z\x20'-]+$/ );
		break;
	case "ADDRESS1":
	case "REF":
		strResult = vldRegExp( t, sType, /^[0-9A-Za-z\x20'-]+$/ );
		break;
	case "TOWN":
		strResult = vldRegExp( t, sType, /^[A-Z]?$/ );
		break;
	case "COUNTY":
		strResult = vldRegExp( t, sType, /^[A-Za-z]?$/ );
		break;
	default:
		blnFound = false;
		break;
	}
	if ( blnFound ) {	
		t.value = strResult;
	}
	return blnFound;
}

/*************************************************************/

var vldChain = null; 

Object.prototype.Inherits = function( parent )	{
	// Apply parent's constructor to this object
	if( arguments.length > 1 ) {	
		// Note: 'arguments' is an Object, not an Array
		parent.apply( this, Array.prototype.slice.call( arguments, 1 ) );
	} else {
		parent.call( this );
	}
}

function Validator( name ) {
	this.name = name
	this.Next = null;
	this.ErrorText = "";
	this.blnKeyHandlerFound = false;
	this.SetErrorText = function SetErrorText() {
		if ( this.ErrorText.length > 0 ) {
			//document.getElementById( this.ctl.id ).style.backgroundColor = "#F88";
			document.getElementById( "eMsg").innerHTML = this.ErrorText.HTMLencode();
		}
		else {
			//document.getElementById( this.ctl.id ).style.backgroundColor = "#8F8";
			document.getElementById( "eMsg").innerHTML = "";
		}
	};
}

Validator.prototype.Validate = function( t, sType ) {
	var blnValid;
	blnValid = this.Test( t, sType );
	if ( blnValid ) {
		this.ctl = t;
		this.SetErrorText();
		// alert("Found " + sType);
	} else {
		if ( this.Next != null ) {
			blnValid = this.Next.Validate( t, sType );
		} else {
			if (!blnValid) {
				alert("Missing Validation handler for " + sType );
			}
		}
	}
	return blnValid;	
}

Validator.prototype.KeyCheck = function( e, sType ) {
	var blnValid;
	
	blnValid = this.CheckKeys( e, sType );
	if ( this.blnKeyHandlerFound ) {
		// alert("Found " + sType);
	} else {
		if ( this.Next != null ) {
			blnValid = this.Next.KeyCheck( e, sType );
		} else {
			if (!this.blnKeyHandlerFound) {
				alert("Missing Key handler for " + sType );
			}
		}
	}
	return blnValid;	
}

Validator.prototype.Test = function( t, sType ) {
	return false;
}

Validator.prototype.CheckKeys = function( e, sType ) {
	this.blnKeyHandlerFound = false;
	return false;
}

Validator.prototype.Add = function( fn ) {
	if ( fn ) {
		fn.Next = vldChain;
		vldChain = fn;
	}
}

vldChain = new Validator( "base" );

function vv(t, sName) {
	vldChain.Validate(t, sName);
}

function ck(e, sName) {
	return vldChain.KeyCheck(e, sName);
}

function sd(t) {
	// t.style.backgroundColor = "#fF8";
}

/*************************************/

function vldMain( name ) {
	this.Inherits( Validator, name );
}

vldMain.prototype = new Validator();
vldMain.prototype.constructor = vldMain;

vldMain.prototype.CheckKeys = function( e, sType ) {
	var blnResult = false;
	var blnFound = true;
	
	switch ( sType ) {
	case "POSTCODE":
		blnResult = chkSpecialKey( e, 1, 1, 1, 0, '' );
		break;
	case "INT":
		blnResult = chkNum( e, 10, 0, 0 );
		break;
	case "LASTNAME":
		blnResult = chkSpecialKey( e, 1, 1, 0, 1, '-' );
		break;
	case "INITIALS":
		blnResult = chkSpecialKey( e, 1, 1, 0, 0, '' );
		break;
	case "CAPS":
		blnResult = chkSpecialKey( e, 0, 1, 0, 0, '' );
		break;
	case "FIRSTNAME":
		blnResult = chkSpecialKey( e, 1, 1, 0, 0, '-' );
		break;
	case "DATE":
		blnResult = chkSpecialKey( e, 0, 0, 1, 0, '/' );
		break;
	case "TELNUM":
		blnResult = chkTelKey(e);
		break;
	case "EMAIL":
		blnResult = chkSpecialKey( e, 0, 1, 1, 0, '-_@.' );
		break;
	case "ADDRESS":
		blnResult = chkSpecialKey( e, 1, 1, 1, 1, '-/' );
		break;
	case "ADDRESS1":
	case "REF":
		blnResult = chkSpecialKey( e, 1, 1, 1, 1, '-/' );
		break;
	case "TOWN":
		blnResult = chkSpecialKey( e, 1, 1, 0, 1, '-/!' );
		break;
	case "BCSREF":
		blnResult = chkSpecialKey( e, 0, 1, 1, 0, '/' );
		break;
	case "COUNTY":
		blnResult = chkSpecialKey( e, 1, 1, 0, 1, '' );
		break;
	case "MONEY":
		blnResult = chkNum( e, 4, 2, 0 );
		break;
	case "HDATE":
		blnResult = chkSpecialKey( e, 0, 0, 1, 0, '/' );
		break;
	case "FDATE":
		blnResult = chkSpecialKey( e, 0, 0, 1, 0, '/' );
		break;
	case "CLAIMNO":
		blnResult = chkSpecialKey( e, 0, 1, 1, 0, '/' );
		break;
	default:
		blnFound = false;
		break;
	}
	this.blnKeyHandlerFound = blnFound;
	return blnResult;
}

vldMain.prototype.Test = function( t, sType ) {
/*
	var blnValid = false;
	if ( sType == this.name ) {
		blnValid = true;
	}
	return blnValid;	
*/
	var strResult;
	var blnFound = true;	

	//
	// WARNING - Regular expressions are very fragile
	// A mistake here will break the page
	//
	switch ( sType ) {
	case "INT":
		strResult = vldRegExp( t, sType, /^\+{0,1}[0-9]*$/ );
		break;
	case "MONEY":
		strResult = vldRegExp( t, sType, /^[0-9]{1,8}(\.[0-9]{1,2})?$/ );
		break;
	case "LASTNAME":
		strResult = vldRegExp( t, sType, /^[A-Za-z\x20'-]+$/ );
		break;
	case "INITIALS":
		t.value = t.value.toUpperCase();
		strResult =  vldRegExp( t, sType, /^[A-Z](\x20[A-Z])+$/ );
		break;
	case "CAPS":
		t.value = t.value.toUpperCase();
		strResult = vldRegExp( t, sType, /^[A-Z](\x20[A-Z])+$/ );
		break;
	case "FIRSTNAME":
		strResult = vldRegExp( t, sType, /^^[A-Za-z\x20'-]+$/ );
		break;
	case "TELNUM":
		strResult = vldRegExp( t, sType, /^\+?[0-9\x20]+$/ );
		break;
	case "EMAIL":
		strResult = vldRegExp( t, sType, /^\+{0,1}[0-9]+$/ );
		break;
	case "ADDRESS":
		strResult = vldRegExp( t, sType, /^[A-Za-z\x20'-]+$/ );
		break;
	case "ADDRESS1":
	case "REF":
		strResult = vldRegExp( t, sType, /^[0-9A-Za-z\x20'-]+$/ );
		break;
	case "TOWN":
		strResult = vldRegExp( t, sType, /^[A-Z]?$/ );
		break;
	case "COUNTY":
		strResult = vldRegExp( t, sType, /^[A-Za-z]?$/ );
		break;
	default:
		blnFound = false;
		break;
	}
	if ( blnFound ) {
		t.value = strResult;
	}
	return blnFound;
}

vldChain.Add( new vldMain( "_main" ));

/************************************/

vldPC.prototype = new Validator();
vldPC.prototype.constructor = vldPC;

function vldPC( name ) {
	this.Inherits( Validator, name );
}

vldPC.prototype.Test = function( t, sType ) {
	var blnValid = false;
	if ( sType == this.name ) {
		blnValid = true;
		t.value = fmtPostCode( t.value, this );
	}
	return blnValid;	
}

vldChain.Add( new vldPC( "POSTCODE" ));

/************************************/

vldBCS.prototype = new Validator();
vldBCS.prototype.constructor = vldBCS;

function vldBCS( name ) {
	this.Inherits( Validator, name );
}

vldBCS.prototype.Test = function( t, sType ) {
	var blnValid = false;
	if ( sType == this.name ) {
		blnValid = true;
		t.value = fmtBCSRef( t.value, this );
	}
	return blnValid;	
}

vldChain.Add( new vldBCS( "BCSREF" ));

/************************************/

vldCN.prototype = new Validator();
vldCN.prototype.constructor = vldCN;

function vldCN( name ) {
	this.Inherits( Validator, name );
}

vldCN.prototype.Test = function( t, sType ) {
	var blnValid = false;
	if ( sType == this.name ) {
		blnValid = true;
		t.value = fmtClaimNo( t.value, this );
	}
	return blnValid;	
}

vldChain.Add( new vldCN( "CLAIMNO" ));

/************************************/


