//validation handler

function validateField(chrFieldId) {
	//determine correct field Id (accounting for multi-part date fields which are treated)
	var chrValidationFieldId = getValidationFieldId(chrFieldId);
	var chrMetaFieldId = chrValidationFieldId + '_meta'; 
	var chrMetaFieldSel = '#' + chrMetaFieldId;
	
	if ($.FindNoCase('sortCode',chrValidationFieldId)){
		var chrValidationFieldSel = '#' + chrFieldId;
	}
	else{
		var chrValidationFieldSel = '#' + chrValidationFieldId;
	}
	var chrValidationRequiredSel = chrMetaFieldSel + ' .validationRequired';
	var blnValidationRequired = $(chrValidationRequiredSel).text() == 'true' ? 1 : 0; 
	var blnValidated = true;
	var chrValue = $(chrValidationFieldSel).val();
	
	if (blnValidationRequired) {
		//determine validation type
		var chrValidationTypeSel = chrMetaFieldSel + ' .validationType';
		var blnValidationType = $(chrValidationTypeSel).text();
		switch(blnValidationType) {
			case 'minLength':
				var chrValidationRuleSel = chrMetaFieldSel + ' .validationRule';
				var blnValidationRule = $(chrValidationRuleSel).text();
				blnValidated = isValidLength(chrValue,'min',blnValidationRule);	  		
		  		break;  
		  	case 'email':
				blnValidated = isValidEmail(chrValue);	  		
		  		break;   
		  	case 'postcode':
				blnValidated = isValidPostcode(chrValue);	  		
		  		break;  
		  	case 'phone':
				blnValidated = isValidPhoneNumber(chrValue);	  		
		  		break; 
		  	case 'date':
		  		blnValidated = isValidDateFromForm(chrValidationFieldId);
		  		break; 
		  	case 'regNumber':
		  		blnValidated = isValidRegNumber(chrValue);
		  		break;  
		  	case 'dateAtLeastDependentField':
		  		var chrValidationRuleSel = chrMetaFieldSel + ' .validationRule';
		  		var blnValidationRule = $(chrValidationRuleSel).text();
		  		blnValidated = isDateAtLeastDependentField(chrValidationFieldId,blnValidationRule);
		  		break; 
		  	case 'ageRange':
		  		var chrValidationRuleSel = chrMetaFieldSel + ' .validationRule';
		  		var blnValidationRule = $(chrValidationRuleSel).text();
		  		blnValidated = isValidAgeRange(chrValidationFieldId,blnValidationRule);
		  		break;  
		  	case 'futuredate':
		  		blnValidated = isValidFutureDate(chrValidationFieldId);
		  		break;  
		  	case 'dateWithin30Days':
		  		blnValidated = isDateWithinDays(chrValidationFieldId,30);	
		  		break;
		  	case 'dateInPast':
		  		blnValidated = isValidPastDate(chrValidationFieldId);	
		  		break;
		  	case 'dateTodayOrLesser':
		  		blnValidated = isDateTodayOrLesser(chrValidationFieldId);	
		  		break;
		 	case 'number':
		  		blnValidated = isValidNumber(chrValue);
		  		break; 
		  	case 'suggest':
		  		blnValidated = isValidSuggestion(chrValidationFieldId);
		  		break;
		  	case 'checkbox':
		  		blnValidated = isValidCheckbox(chrValidationFieldId);
		  		break;
		    case 'points':
		  		blnValidated = isValidPoints(chrValidationFieldId);
		  		break;
		  	case 'convictionDate':
		  		blnValidated = isValidConvictionDate(chrValidationFieldId);
		  		break;  
		  	case 'renewalref':
		  		blnValidated = isValidRenewalRef(chrValidationFieldId);
		  		break;
		  	case 'bankAccount':
		  		blnValidated = isValidBankAccount(chrValidationFieldId);
		  		break;        
			default:
				alert('validation not recognised');
		} 
		setValidationClass(chrValidationFieldId,blnValidated);
	} else {
		return true;
	}
}

function getValidationFieldId(chrFieldId) {
	var blnDateField = $.FindNoCase('date',chrFieldId);
	if (blnDateField) {
		chrDatePart = '_' + $.ListLast(chrFieldId,'_');
		return $.ReplaceNoCase(chrFieldId,chrDatePart,'');
	} else {
		var blnSortCodeField = $.FindNoCase('sortCode',chrFieldId);
		if (blnSortCodeField) {
		  chrSortCodePart = '_' + $.ListLast(chrFieldId,'_');
		  return $.ReplaceNoCase(chrFieldId,chrSortCodePart,'');
		}
		return chrFieldId;
	}
}

function isFieldValidated(chrFieldId) {
	var chrValidationFieldId = getValidationFieldId(chrFieldId);
	var chrFieldSel = '#' + chrValidationFieldId;
	var chrValiditySel = chrFieldSel + '_validity';
	var chrMetaSel = chrFieldSel + '_meta';
	var chrValidationStatusSel = chrMetaSel + ' .validationStatus';
	if ($.LCase($.Trim($(chrValidationStatusSel).text())) == 'valid') {
		return true;
	} else {
		return false;
	}
}

//validate step 

function validateStep(numStepId) {
	var numErrors = 0;
	//revalidate individual entries first
	validateFieldsOnLoad();
	var chrErrorMessage = "<b>Please correct the following:</b><br>";
	
	//job 3488 confimation mobile number
	if (numStepId == 8){
		var proposer_daytimeTelephone = $('#proposer_daytimeTelephone').val();
		var proposer_daytimeTelephone_conf = $('#proposer_daytimeTelephone_conf').val();
		// If the user has not typed in the same email address
		if(proposer_daytimeTelephone != proposer_daytimeTelephone_conf){
			showModal("The confirmation mobile number does not match.","Please ensure that you have typed your mobile number correctly.");
			return false;
		}
	}
	
	
	if (numStepId == 1 && ($('input[name=proposer_nonRtaConviction]:checked').val() == 'Y' || $('input[name=proposer_insuranceRefused]:checked').val() == 'Y')) {
		showNoQuoteMessage();
		return false;
	}
	
	//job:3683
	if (numStepId == 1 ) {
		//if policy holder is under 16 when they got their licence ( impossible!)
		chrDOBDateString = getDateString(getYearFromForm('proposer_dateOfBirth'),getMonthFromForm('proposer_dateOfBirth'),getDayFromForm('proposer_dateOfBirth'));
		var numAgeOfPolicyHolder = getAge(chrDOBDateString);
		chrDOLicenceDateString = getDateString(getYearFromForm('proposer_bikeLicence_date'),getMonthFromForm('proposer_bikeLicence_date'),getDayFromForm('proposer_bikeLicence_date'));
		var numAgeWhenObtainedLicence = getAge(chrDOLicenceDateString);
        
		if(numAgeOfPolicyHolder - numAgeWhenObtainedLicence <= 16){
		   
			if(numAgeOfPolicyHolder - numAgeWhenObtainedLicence == 16){
				errorPresent="No";
			/*	if(getMonthFromForm('proposer_bikeLicence_date') < getMonthFromForm('proposer_dateOfBirth'))
					errorPresent = "Yes";
				else{
					if( getDayFromForm('proposer_bikeLicence_date') < getDayFromForm('proposer_dateOfBirth'))
						errorPresent = "Yes";
				} */
			}
			else{
				errorPresent="Yes";
			}

			//we have an error so highlight it and add red cross
			if(errorPresent == "Yes"){
				numErrors++;
				chrErrorMessage = $.ListAppend(chrErrorMessage,'Proposer must be at least 16 years old when they receive a licence.','<br />');
				$('#proposer_bikeLicence_date_dd').addClass('fieldInvalid');
				$('#proposer_bikeLicence_date_mm').addClass('fieldInvalid');
				$('#proposer_bikeLicence_date_yyyy').addClass('fieldInvalid');
				$('#proposer_bikeLicence_date_validity').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
			}
		}
	}
	
	if (numStepId == 1){
		var old_proposer_address_postCode = getProposerAddressPostCode();
		var proposer_address_postCode = $('#proposer_address_postCode').val()
		// If the user has changed the postcode they must click get my address
		if(proposer_address_postCode != old_proposer_address_postCode){
			showPostCodeError();
			return false;
		}
	}
	
	if (numStepId == 1){
		var proposer_email = $('#proposer_email').val();
		var proposer_confirmemail = $('#proposer_confirmemail').val();
		// If the user has not typed in the same email address
		if(proposer_email!= proposer_confirmemail){
			showModal("The confirmation e-mail address does not match.","Please ensure that you have typed your e-mail address correctly.");
			return false;
		}
	}
	
	
	if (numStepId == 3 && $('#vehicle_owner').val() == 'Other') {
		showNoQuoteMessage();
		return false;
	}
	
	
	
	//open address lines to ensure they are included in the validation
	$('#addressLines').show();
	$('.validationStatus').each(function() {
		//alert(this.id);
		//only perform validation if the form element, or its immediate parent 
		// also  (parent, parent, parent required for occupations on step 2 - && $(this).parent().parent().parent().is(':visible'))
		if ($(this).parent().next().is(':visible') && $(this).parent().parent().is(':visible') && $(this).parent().parent().parent().is(':visible')) {
			/*if ($.FindNoCase('occCode',$(this).parent().attr('id')) && numStepId == 2) {
				alert($(this).parent().attr('id'));
				alert($(this).parent().parent().parent().attr('id'));
			}*/
			if ($(this).text() == 'invalid') {
				if ($(this).parent().children().filter('.validationMessage').size()) {
					numErrors++;
					var chrMessage = $(this).parent().children().filter('.validationMessage').text();
					chrErrorMessage = $.ListAppend(chrErrorMessage,chrMessage,'<br />');
					var chrFieldId = $.ReplaceNoCase($(this).parent().attr('id'),'_meta','');
					var chrFieldSel = '#' + chrFieldId;
					var chrValiditySel = chrFieldSel + '_validity';
					if ($.FindNoCase('date',chrFieldSel)) { 
						$(chrFieldSel + '_dd').addClass('fieldInvalid');
						$(chrFieldSel + '_mm').addClass('fieldInvalid');
						$(chrFieldSel + '_yyyy').addClass('fieldInvalid');
					} else {
						
						if ($.FindNoCase('sortCode',chrFieldSel)) {
							$(chrFieldSel + '_1').addClass('fieldInvalid');
							$(chrFieldSel + '_2').addClass('fieldInvalid');
							$(chrFieldSel + '_3').addClass('fieldInvalid');
						}
						
						$(chrFieldSel).addClass('fieldInvalid');
					}
					$(chrValiditySel).attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
				}
			}
	}
	});
	
	if (numStepId == 3 && !$('#vehicleSelector').is(':visible')) {
		numErrors++;
		chrErrorMessage = $.ListAppend(chrErrorMessage,'Please click the "Get my bike" button to retrieve results','<br />');
	}
	
	if (numStepId == 3){
		// make sure they have selected a valid bike
		if(!isValidNumber($('#vehicle_selector').val())){
			numErrors++;
			chrErrorMessage = $.ListAppend(chrErrorMessage,'Please select a bike','<br />');
		}
		
		
/*		if($("input[@name='vehicle_regNumberKnown']:checked").val() == "Y"){
			var yearOfManf = $('#vehicle_yearOfManufacture1').val();
			var year="1";
		}
		else{
			var yearOfManf = $('#vehicle_yearOfManufacture2').val();
			var year="2";
		}
			
		var yearOfPurch = $('#vehicle_purchased_date_yyyy').val();
			
		if(yearOfManf > yearOfPurch){
			if(year == "1"){
				$('#vehicle_yearOfManufacture1').addClass('fieldInvalid');
				$('#vehicle_yearOfManufacture1').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
			}
			else{
				$('#vehicle_yearOfManufacture2').addClass('fieldInvalid');
				$('#vehicle_yearOfManufacture2').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
			}
			$('#vehicle_purchased_date_yyyy').addClass('fieldInvalid');
			$('#vehicle_purchased_date_yyyy').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
	    	
	    	numErrors++;
			chrErrorMessage = $.ListAppend(chrErrorMessage,'Year of manufacture cannot be greater than purchase date','<br />');
		} */
			
	}
	
	if (numStepId == 3){
		mystring = $('#vehicle_selector').find(':selected').text();
		endOfString = (mystring.search('cc'));
		startOfString = (endOfString - 4);
		ccOfBike = (mystring.substring(startOfString,endOfString));
		licenceType = ($('#proposer_bikeLicence_type').attr('type'));
		
		if(ccOfBike > 125 && licenceType == "P"){
			numErrors++;
			chrErrorMessage = $.ListAppend(chrErrorMessage,'A provisional licence does not entitle you to ride a motorcycle over 125cc.','<br />');
			$('#vehicle_selector').addClass('fieldInvalid');
			$('#vehicle_selector_validity').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
		}
		else if(ccOfBike > 50 && licenceType == "M"){
			numErrors++;
			chrErrorMessage = $.ListAppend(chrErrorMessage,'A moped only licence does not entitle you to ride a motorcycle over 50cc.','<br />');
			$('#vehicle_selector').addClass('fieldInvalid');
			$('#vehicle_selector_validity').attr('src','images/' + $('#sAssetsSubFolder').text() + '/invalid.gif');
		}
	}
	var chrItems = numErrors > 1 ? 'items have' : 'item has'; 
	var chrErrorMessage = chrErrorMessage + "<br><br><img src='images/" + $('#sAssetsSubFolder').text() + "/invalid-white.gif' style='float:left; margin-bottom:10px; margin-right:5px; '><i> The " + chrItems + " been highlighted in <span style='background-color: #FFE0E0;'>&nbsp;this colour&nbsp;</span> and with the icon shown left. </i>";
	var chrErrorMessage = chrErrorMessage + "";
	if (numErrors) {
		//errors on form so diplay before proceeding
		displayStepValidationErrors(chrErrorMessage);
		return false;
	} else {
		return true;
	}
}

function displayStepValidationErrors(chrErrorMessage) {
	var chrTitle = 'Sorry, we could not process your entries.';
	showModal(chrTitle,chrErrorMessage);
}

function setValidationClass(chrFieldId,blnValidated) {
	//add some typeof validation here to ensure correct classes are added?
	var chrFieldSel = '#' + chrFieldId;
	var chrValiditySel = chrFieldSel + '_validity';
	var chrMetaSel = chrFieldSel + '_meta';
	var chrValidationStatusSel = chrMetaSel + ' .validationStatus';
	$(chrValidationStatusSel).text('');
	//inbox box underline removed as only used on step validation
	if ($.FindNoCase('date',chrFieldSel)) { 
		$(chrFieldSel + '_dd').removeClass('fieldInvalid');
		$(chrFieldSel + '_mm').removeClass('fieldInvalid');
		$(chrFieldSel + '_yyyy').removeClass('fieldInvalid');
	} else {
		if ($.FindNoCase('sortCode',chrFieldSel)) {
			$(chrFieldSel + '_1').removeClass('fieldInvalid');
			$(chrFieldSel + '_2').removeClass('fieldInvalid');
			$(chrFieldSel + '_3').removeClass('fieldInvalid');
		}
		$(chrFieldSel).removeClass('fieldInvalid');
	}
	if (blnValidated) {
		$(chrValiditySel).attr('src','images/' + $('#sAssetsSubFolder').text() + '/valid.gif');
		$(chrValidationStatusSel).text('valid');
	} else {
		$(chrValiditySel).attr('src','images/' + $('#sAssetsSubFolder').text() + '/validityoff.gif');
		$(chrValidationStatusSel).text('invalid');
	}
}

function resetDefaultValidationClass(chrFieldId) {
	var chrFieldSel = '#' + chrFieldId;
	var chrValiditySel = chrFieldSel + '_validity';
		//alert(chrFieldSel);
	if ($.FindNoCase('date',chrFieldSel)) { 
		$(chrFieldSel + '_dd').removeClass('fieldInvalid');
		$(chrFieldSel + '_mm').removeClass('fieldInvalid');
		$(chrFieldSel + '_yyyy').removeClass('fieldInvalid');
	} else {
		if ($.FindNoCase('sortCode',chrFieldSel)) {
			$(chrFieldSel + '_1').removeClass('fieldInvalid');
			$(chrFieldSel + '_2').removeClass('fieldInvalid');
			$(chrFieldSel + '_3').removeClass('fieldInvalid');
		}
		$(chrFieldSel).removeClass('fieldInvalid');
	}
	$(chrValiditySel).attr('src','images/' + $('#sAssetsSubFolder').text() + '/validityoff.gif');
}


function isValidRegNumber(chrValue) {
	
	var re = new RegExp(/^([A-HJ-PR-Y]{2,2}[056][0-9]\s?[A-HJ-PR-Y]{3,3})$|^([A-HJ-NP-Y]{1,3}[0-9]{2,3}?\s[A-Z]{3,3})$|^([A-Z]{1,3}\s?[0-9]{1,4}([A-Z]{1,1})?)$|^([0-9]{4,4}[A-Z]{1,3})$|^([A-Z]{1,2}\s?[0-9]{1,4})$|^([A-Z]{2,3}\s?[0-9]{1,4})$|^([0-9]{1,4}\s?[A-Z]{2,3})$/);
    chrValue.toUpper
    var m = re.exec(chrValue);
    
    if (chrValue.length >8 )
		return false;
	if (chrValue.length <8 )
		return false;
	
	if(m==null)
	   return false;
	else
	   return true;
	   
	//return reReg.test(chrValue);
	//if (chrValue.length > 1 && chrValue.length < 9) {
	//	return true;			
	//} else {
	//	return false;
	//}
}

function isValidCheckbox(chrValidationFieldId) {
	var chrValidationFieldSel = '#' + chrValidationFieldId;
	return $(chrValidationFieldSel).is(':checked');
}


//validation rules - returns true if OK

function isValidSuggestion(chrValidationFieldId) {
	var blnValidated = false;
	var chrSuggestResultsId = chrValidationFieldId + '_suggestResults';
	var chrValidationFieldSel = '#' + chrValidationFieldId;
	var chrSuggestResultsSel = '#' + chrSuggestResultsId + ' li';
	//alert($(chrSuggestResultsSel).size());
	if ($(chrSuggestResultsSel).size()) {
		$(chrSuggestResultsSel).each(function() {
			if ($.LCase($.Trim($(chrValidationFieldSel).val())) == $.LCase($.Trim($(this).text()))) {
				blnValidated = true;
			}
		});
	} else {
		//if there is suggest results available but there is already text in the field then
		//it must be a refreshed page and therefore pre-validated 
		if (!$(chrSuggestResultsSel).size() && $.Len($.Trim($(chrValidationFieldSel).val())) > 2 ) {
			blnValidated = true;
		}
	}
	return blnValidated;
}

function isValidLength(chrValue,chrMinMax,blnValidationRule) {
	var numLength = parseInt(blnValidationRule); 
	if (chrMinMax == 'min') {
		if ($.Len($.Trim(chrValue)) < numLength) {
			return false;
		} else {
			return true;
		}
		
	} else {
		if ($.Len($.Trim(chrValue)) > numLength) {
			return false;
		} else {
			return true;
		}
		
	}
}

function isValidNumber(sText) {
	return $.Len(sText) && $.IsNumeric(sText);
}

//replaced this function with a better one
//function isValidEmail(sText) {
//	 var reEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
     //var reEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
//     return reEmail.test(sText);
//}

function isValidPostcode(sText) {
	var rePostcode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;
	return rePostcode.test(sText);
}

function isValidPhoneNumber(sText) {
	// use Googles validation for phone number 
	
	if(sText.length > 3){
	var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
	var number = phoneUtil.parseAndKeepRawInput(sText, 'GB');
	return(phoneUtil.isValidNumber(number));
	}
}
	

function isValidEmail(emailStr){

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

//alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("Ths username contains invalid characters.");
		return false;
	}
	//Check for these characters in the username as they are not valid in CDL
	if (user[i] == '}' || user[i] == '{') {
		return false;
	}
}


for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
//alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
//alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
//alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
//alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
//alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

function isValidPoints(chrValidationFieldId) {
	var chrValidationFieldSel = '#' + chrValidationFieldId;
	var numPoints = $(chrValidationFieldSel).val();
	
	if ( numPoints == '' || numPoints >12){
		return false;
	}
	
	return true;
}

function isValidRenewalRef(chrValidationFieldId) {
	var chrValidationFieldSel = '#' + chrValidationFieldId;
	var ref1 = $('#renewal_reference_1').val();
	var ref2 = $('#renewal_reference_2').val();
	if((ref1.length == 0) || (ref2.length == 0)){
		if (ref1.length == 0)
			$('#renewal_reference_1').addClass('fieldInvalid');
		
		return false;
	}
	if (!isValidNumber(ref1)){
		$('#renewal_reference_1').addClass('fieldInvalid');
		return false;
	}
	if (!isValidNumber(ref2)){
		$('#renewal_reference_2').addClass('fieldInvalid');
		return false;
	}
		
	
	return true;
}

function isValidConvictionDate(chrValidationFieldId) {
	 var chrValidationFieldSel = '#' + chrValidationFieldId;
	 
	 var inceptionDateString = chrValidationFieldId.replace('conviction','inception');
	 var inceptionDate = getDateFromForm(inceptionDateString);
	 var covictionDate = getDateFromForm(chrValidationFieldId);
	 //alert(covictionDate);
	 //alert(inceptionDate);
	 
	if(isValidDateFromForm(chrValidationFieldId)){
		if(inceptionDate < covictionDate){
			return false;
		}	
	}
	else{
		return false;
	}

	return true;
}

function isValidBankAccount(chrValidationFieldId) {
	 var chrValidationFieldSel = '#' + chrValidationFieldId;
	 var bankAccount = $(chrValidationFieldSel).val();
	 
	 // account number must be numeric and 6-10 characters in length
	 if ($.Len(bankAccount) > 5 && $.IsNumeric(bankAccount)){
	 	if($.Len(bankAccount) < 11){
	 	  return true;
	 	}
	 	return false;
	 }
	 
	 return false;
}
