var	m_InsuranceCookieUserId	= -1; // to be set in miAddUser if webservice client saves the user
var	mi_autofill				= '*.*autofill';

/******************************************************
	InsuranceQuoteDetails Object and Methods
******************************************************/

function InsuranceQuoteDetails()
{
}

Object.extend(String.prototype, {
  mixin: function(obj) {
    return new Template(this).evaluate(obj);
  }
});

var InsuranceQuoteDetails = Class.create();
InsuranceQuoteDetails.prototype = {

	initialize : function()
	{
		this.InsuranceGroupsArray					= new Array();
		this.PromoCode								= '';
		this.ListQuoteString						= 'Your best monthly <strong>CAR INSURANCE QUOTATION</strong>, fixed for 3 years, on this vehicle from #{company} is ';
		this.FullQuoteEnterDetailsMsg				= '<div id="padding">Please enter just a few details to receive <a href="javascript:(void(0));" onclick="miShowNewUserWindow(\'\',\'\');">Instant Insurance Quotes</a> for each vehicle.</div>';
		this.ListingEnterDetailsMessage				= '<div id="padding">Please enter just a few details to receive <a href="javascript:(void(0));" onclick="miShowNewUserWindow(\'\',\'\');">Instant Insurance Quotes</a> for each vehicle.</div>';
		this.PopupInstructionsText					= 'Enter a few details to get instant insurance quotes on each vehicle.';
		this.CalculatingQuotationsListingMessage	= 'Fetching your <strong>CAR INSURANCE QUOTATION</strong> from our panel of underwriters';
	}
}

//internal use
InsuranceQuoteDetails.prototype.InsuranceGroupsArray						= new Array();
InsuranceQuoteDetails.prototype.CapIdArray									= new Array();
InsuranceQuoteDetails.prototype.VehicleYearsArray							= new Array();
InsuranceQuoteDetails.prototype.IsPopup										= false; // indicates if the form is displayed in popup or inline in a page (currently (02 June 2007) determines if title is added to table for the popup)
InsuranceQuoteDetails.prototype.EditDetails									= false; // indicates if the popup form should be pre-populated with existing details
InsuranceQuoteDetails.prototype.ExistingUser								= false; // determines if Edit details instructions are shown in popup as they are different to Registering user instructions
InsuranceQuoteDetails.prototype.CookieValues								= $H();
InsuranceQuoteDetails.prototype.ExistingUserId								= ''; // adds Existing User Id to hidden field if known
InsuranceQuoteDetails.prototype.PersonalDetailsForm							= ''; // used in RenderFullForm() to added Users' name and other personal details to form

// required externally set
InsuranceQuoteDetails.prototype.ListingQuoteUsingInsuranceGroupAjaxUrl		= ''; // the URL that the Ajax.Updater needs to call
InsuranceQuoteDetails.prototype.ListingQuoteUsingCapIdAjaxUrl				= ''; // the URL that the Ajax.Updater needs to call
InsuranceQuoteDetails.prototype.FullQuotationsAjaxUrl						= ''; // the URL that the Ajax.Updater needs to call
InsuranceQuoteDetails.prototype.ListingUsingCapIdsAjaxMethod				= ''; // the name of the method for the Ajax.Updater to call to perform webservice call
InsuranceQuoteDetails.prototype.FullQuotationsAjaxMethod					= ''; // the name of the method for the Ajax.Updater to call to perform webservice call

// optional externally set
InsuranceQuoteDetails.prototype.PromoCode									= ''; // promotional code
InsuranceQuoteDetails.prototype.ListQuoteString								= ''; // text that contains the quote price
InsuranceQuoteDetails.prototype.FullQuoteEnterDetailsMsg					= ''; // message that appears if cookie does not exist on full details section
InsuranceQuoteDetails.prototype.ListingEnterDetailsMessage					= ''; // message that appears if cookie does not exist on listing section
InsuranceQuoteDetails.prototype.PopupInstructionsText						= ''; // message at top of popup window
InsuranceQuoteDetails.prototype.CalculatingQuotationsListingMessage			= ''; // the message displayed on the listing page when the quotations are being calculated

//Renders form without Name fields
InsuranceQuoteDetails.prototype.RenderForm	= function()
{
	sSrc			= '<form name="frmMotorInsurance" id="frmMotorInsurance"><table cellspacing="0">\n';
	if(this.IsPopup)
	{
		sSrc			+= '	<tr>\n';
		sSrc			+= '		<td colspan="3" class="registrationTitle">Insurance Quote Details</td>\n';
		sSrc			+= '	</tr>\n';
	}
	sSrc			+= this.PersonalDetailsForm;
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Postcode</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input name="postcode" type="text" id="postcode" class="textShort"';
	if(this.EditDetails && this.CookieValues['postcode'] != null)
		sSrc	+=	' value="' + unescape(this.CookieValues['postcode']) + '"';
	sSrc			+=	' onblur="miAutofill(this, false);" />';
	sSrc			+= '		</td>\n';
	sSrc			+= '		<td class="registrationErrorMessage" rowspan="6">';
	sSrc			+= '			<div id="instructions">Simply complete this <strong>1 page form</strong> to obtain highly competitive motor insurance quotes from the UK\'s <strong>leading insurance companies</strong>, all in less than 40 seconds.</div>';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">House Number</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+=	'			<input name="building" type="text" id="building" class="textShort"';
	if(this.EditDetails && this.CookieValues['building'] != null)
		sSrc	+=	' value="' + unescape(this.CookieValues['building']) + '"';
	sSrc			+= ' />&nbsp;or House Name: <input name="buildingName" type="text" id="buildingName" class="textShort"'
	if(this.EditDetails && this.CookieValues['buildingname'] != null)
		sSrc	+=	' value="' + unescape(this.CookieValues['buildingname']) + '"';
	sSrc			+= ' /><strong>  *</strong>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Email</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input name="email" type="text" id="email" class="text"';
	if(this.EditDetails && this.CookieValues['email'] != null)
		sSrc	+=	' value="' + unescape(this.CookieValues['email']) + '"';
	sSrc	+=	' />\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Telephone</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input name="telephone" type="text" id="telephone" class="text"';
	if(this.EditDetails && this.CookieValues['telephone'] != null)
		sSrc	+=	' value="' + this.CookieValues['telephone'] + '"';
	sSrc	+=	' /><strong>  *</strong>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Gender</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input type="radio" id="gender" name="gender" value="false"';
	if(this.EditDetails && this.CookieValues['gender'] != null && this.CookieValues['gender'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> Male\n';
	
	sSrc			+= '			<input type="radio" id="gender" name="gender" value="true"';
	if(this.EditDetails && this.CookieValues['gender'] != null && this.CookieValues['gender'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> Female\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Date Of Birth</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<select id="dobDate" name="dobDate" class="selectShort">\n';
	
	var		dob		= new Date();
	var		date;
	if(this.EditDetails && this.CookieValues['dob'] != null)
	{
		date		= this.CookieValues['dob'].split('/');
		dob.setFullYear(date[2], (date[1] - 1), date[0]);
	}
	
	for(var i = 1; i < 32; i++)
	{
		sSrc		+= '				<option value="' + i + '"';
		if(this.EditDetails && dob.getDate() == i)
			sSrc	+=	' selected';
		sSrc		+= '>' + i + '</option>';
	}
	sSrc	+=	'</select>\n';
	
	sSrc			+= '			<select id="dobMonth" name="dobMonth" class="selectShort">\n';
	for(var i = 1; i < 13; i++)
	{
		sSrc		+= '				<option value="' + i + '"';
		if(this.EditDetails && (dob.getMonth() + 1) == i)
			sSrc	+=	' selected';
		sSrc		+= '>' + i + '</option>';
	}
	sSrc	+=	'</select>\n';
	
	sSrc			+= '			<select id="dobYear" name="dobYear" class="selectShort">\n';
	var		now		= new Date();
	for(var i = (now.getFullYear() - 21); i > 1899; i--)
	{
		sSrc		+= '				<option value="' + i + '"';
		if(this.EditDetails && dob.getFullYear() == i)
			sSrc	+=	' selected';
		sSrc		+= '>' + i + '</option>';
	}
	
	sSrc	+=	'</select>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Annual Mileage</td>\n';
	sSrc			+= '		<td class="registrationInput" nowrap="nowrap">\n';
	sSrc			+= '			<select id="insuranceMileage" name="insuranceMileage" class="selectText">\n';
	for(var i = 1000; i < 26000; i = i + 1000)
	{
		sSrc		+= '				<option value="' + i + '"';
		if(!this.EditDetails && i == 10000)
			sSrc	+=	' selected';
		else if(this.EditDetails && i == this.CookieValues['insurancemileage'])
			sSrc	+=	' selected';
		
		sSrc		+= '>' + i + '</option>\n';
	}
	sSrc	+=	'</select>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '		<td class="registrationErrorMessage" rowspan="8">';
	sSrc			+= '			<div style="position: relative;">';
	sSrc			+= '				<img src="https://webservice.yourcarmotorinsurance.co.uk/images/rsa_small.jpg" /><br />';
	sSrc			+= '				<img src="https://webservice.yourcarmotorinsurance.co.uk/images/norwichunion_small.gif" /><br />';
	sSrc			+= '				<img src="https://webservice.yourcarmotorinsurance.co.uk/images/cornhill_small.gif" /><br />';
	sSrc			+= '				<img src="https://webservice.yourcarmotorinsurance.co.uk/images/zurich_small.gif" /><br />';
	sSrc			+= '			</div>';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Tracker Fitted</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input type="radio" id="tracker" name="tracker" value="true"';
	if(this.EditDetails && this.CookieValues['tracker'] != null && this.CookieValues['tracker'].toLowerCase() == 'true')
	{
		sSrc	+=	' checked=checked"';
	}
	sSrc	+=	' /> Yes\n';
	sSrc			+= '			<input type="radio" id="tracker" name="tracker" value="false"';
	if(this.EditDetails && this.CookieValues['tracker'] != null && this.CookieValues['tracker'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> No\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Is The Vehicle Garaged</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input type="radio" id="garaged" name="garaged" value="true"';
	if(this.EditDetails && this.CookieValues['garaged'] != null && this.CookieValues['garaged'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> Yes\n';
	sSrc			+= '			<input type="radio" id="garaged" name="garaged" value="false"';
	if(this.EditDetails && this.CookieValues['garaged'] != null && this.CookieValues['garaged'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> No\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">Cover Type</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<select id="coverType" name="coverType" class="selectText">\n';
	sSrc			+= '				<option value="1"';
	if(this.EditDetails && this.CookieValues['covertype'] == '1')
		sSrc	+=	' selected"';
	else if(!this.EditDetails)
		sSrc	+=	' selected"';
	sSrc	+=	' >Insured Only To Drive</option>\n';
	sSrc			+= '				<option value="3"';
	if(this.EditDetails && this.CookieValues['covertype'] == '3')
		sSrc	+=	' selected';
	sSrc	+=	'>Insured Plus 1 Named Driver Over 25</option>\n';
	sSrc			+= '				<option value="2"';
	if(this.EditDetails && this.CookieValues['covertype'] == '2')
		sSrc	+=	' selected';
	sSrc	+=	'>Insured Plus 2 Named Driver Over 25</option>\n';
	sSrc			+= '			</select>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel">No Claims Bonus</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<select id="noClaimsDiscount" name="noClaimsDiscount" class="selectText">\n';
	sSrc			+= '				<option value="0"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '0')
		sSrc	+=	' selected"';
	else if(!this.EditDetails)
		sSrc	+=	' selected"';
	sSrc	+=	' >0</option>\n';
	sSrc			+= '				<option value="1"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '1')
		sSrc	+=	' selected';
	sSrc	+=	'>1</option>\n';
	sSrc			+= '				<option value="2"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '2')
		sSrc	+=	' selected';
	sSrc	+=	'>2</option>\n';
	sSrc			+= '				<option value="3"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '3')
		sSrc	+=	' selected';
	sSrc	+=	'>3</option>\n';
	sSrc			+= '				<option value="4"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '4')
		sSrc	+=	' selected';
	sSrc	+=	'>4</option>\n';
	sSrc			+= '				<option value="5"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '5')
		sSrc	+=	' selected';
	sSrc	+=	'>5</option>\n';
	sSrc			+= '				<option value="6"';
	if(this.EditDetails && this.CookieValues['noClaimsDiscount'] == '6')
		sSrc	+=	' selected';
	sSrc	+=	'>6+</option>\n';
	sSrc			+= '			</select>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel"><div id="claimContainer">Have You Or Any Additional Driver Had Any "Fault" Accidents In The Last 3 Years <a href="javascript:void(0);" id="btnFaultHover" onmouseover="miShowhide(\'claimDescriptionContainer\', true);" onmouseout="miShowhide(\'claimDescriptionContainer\', false);">?</a>';
	sSrc			+= '			<div id="claimDescriptionContainer" style="display: none;">This includes any claim made where your insurer was unable to recover all of their and your costs from another party.</div></div>';
	sSrc			+= ' </td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input type="radio" id="accidents" name="accidents" value="true"';
	if(this.EditDetails && this.CookieValues['accidents'] != null && this.CookieValues['accidents'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> Yes\n';
	sSrc			+= '			<input type="radio" id="accidents" name="accidents" value="false"';
	if(this.EditDetails && this.CookieValues['accidents'] != null && this.CookieValues['accidents'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> No\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel"><br />Have You Or Any Additional Driver Had Any Motoring Convictions In The Last 3 Years?</td>\n';
	sSrc			+= '		<td class="registrationInput">\n';
	sSrc			+= '			<input type="radio" id="convictions" name="convictions" value="true"';
	if(this.EditDetails && this.CookieValues['convictions'] != null && this.CookieValues['convictions'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> Yes\n';
	sSrc			+= '			<input type="radio" id="convictions" name="convictions" value="false"';
	if(this.EditDetails && this.CookieValues['convictions'] != null && this.CookieValues['convictions'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> No\n';
	if(this.EditDetails && this.CookieValues['userid'] != null)
		sSrc		+= '			<input type="hidden" id="hdnUserId" name="hdnUserId" value="' + this.CookieValues['userid'] + '" />\n';
	else if(this.ExistingUserId != '')
		sSrc		+= '			<input type="hidden" id="hdnUserId" name="hdnUserId" value="' + this.ExistingUserId + '" />\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel" style="height: 30px;">&nbsp;</td>\n';
	sSrc			+= '		<td class="registrationInput">&nbsp;\n</div>';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel"><strong>*</strong> - optional</td>\n';
	sSrc			+= '		<td class="registrationInput">&nbsp;\n</div>';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td colspan="3" style="border-top:1px solid #cccccc;"></td>\n';
	sSrc			+= '	</tr>\n';
	
	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td colspan="3"><div id="insuranceQuoteResponseMessage"></div><div id="insuranceQuoteBlank" style="display: none;"></div></td>\n';
	sSrc			+= '	</tr>\n';
	sSrc			+=	'</table>\n</form>\n';
	
	if(!this.IsPopup)
	{
		$('insuranceRegistrationDetails').innerHTML	= sSrc;
	}
	else
	{
		return sSrc;
	}
}

InsuranceQuoteDetails.prototype.RenderFullForm = function()
{
	try
	{
		if(!this.EditDetails && !this.ExistingUser)
		{
			this.PersonalDetailsForm			= '	<tr>\n';
			this.PersonalDetailsForm			+= '		<td colspan="3" style="border-bottom:1px solid #cccccc;">' + this.PopupInstructionsText + '</td>\n';
			this.PersonalDetailsForm			+= '	</tr>\n';
		}
		else if(this.EditDetails && this.CookieValues['title'] == null)
		{
			this.PersonalDetailsForm			= '	<tr>\n';
			this.PersonalDetailsForm			+= '		<td colspan="3" style="border-bottom:1px solid #cccccc;">Unfortunately your cookie was not found, your details have been lost. Please re-enter your details.</td>\n';
			this.PersonalDetailsForm			+= '	</tr>\n';
		}
		
		this.PersonalDetailsForm	+=	'		<tr>\n' +
						'			<td class="registrationLabel">Title</td>\n' +
						'			<td class="registrationInput">\n' +
						'				<select id="title" name="title" class="selectText" onchange="miSetGender(this);">\n' +
						'					<option value="Mr"';
		if(this.EditDetails && this.CookieValues['title'] != null && this.CookieValues['title'] == 'Mr')
			this.PersonalDetailsForm	+= ' selected';
		else if(this.CookieValues['title'] == null)
			this.PersonalDetailsForm	+= ' selected';
		
		this.PersonalDetailsForm		+= '>Mr</option>\n' +
						'					<option value="Mrs"';
		if(this.EditDetails && this.CookieValues['title'] != null && this.CookieValues['title'] == 'Mrs')
			this.PersonalDetailsForm	+= ' selected';
		
		this.PersonalDetailsForm		+= '>Mrs</option>\n' +
						'					<option value="Ms"';
		if(this.EditDetails && this.CookieValues['title'] != null && this.CookieValues['title'] == 'Ms')
			this.PersonalDetailsForm	+= ' selected';
		
		this.PersonalDetailsForm		+= '>Ms</option>\n' +
						'					<option value="Miss"';
		if(this.EditDetails && this.CookieValues['title'] != null && this.CookieValues['title'] == 'Miss')
			this.PersonalDetailsForm	+= ' selected';
		
		this.PersonalDetailsForm		+= '>Miss</option>\n' +
						'					<option value="Other"';
		if(this.EditDetails && this.CookieValues['title'] != null && this.CookieValues['title'] == 'Other')
			this.PersonalDetailsForm	+= ' selected';
		
		this.PersonalDetailsForm		+= '>Other</option>\n' +
						'				</select>\n' + 
						'			</td>\n' +
						'		<td class="registrationErrorMessage" rowspan="3"><div id="errorMessage" class="errorMessage">&nbsp;</div></td>\n' +
						'		</tr>\n' + 
						'		<tr>\n' +
						'			<td class="registrationLabel">Forename</td>\n' +
						'			<td class="registrationInput">\n' +
						'				<input name="forename" type="text" id="forename" class="text"';
		if(this.EditDetails && this.CookieValues['forename'] != null)
			this.PersonalDetailsForm	+=	' value="' + unescape(this.CookieValues['forename']) + '"';
		this.PersonalDetailsForm	+=	' onblur="miAutofill(this, true);" />\n' + 
						'			</td>\n' +
						'		</tr>\n' + 
						'		<tr>\n' +
						'			<td class="registrationLabel">Surname</td>\n' +
						'			<td class="registrationInput">\n' +
						'				<input name="surname" type="text" id="surname" class="text"';
		if(this.EditDetails && this.CookieValues['surname'] != null)
			this.PersonalDetailsForm	+=	' value="' + unescape(this.CookieValues['surname']) + '"';
		this.PersonalDetailsForm	+=	' />\n' + 
						'			</td>\n' +
						'		</tr>\n';
	
		return this.RenderForm();
	}
	catch(err)
	{
		//if(console != null) console.error('RenderFullForm Error: ' + err);
	}
}

InsuranceQuoteDetails.prototype.GroupResponseComplete	= function(group, company, premium)
{
	var	arrayMessage = $$('.insuranceGroup' + group + 'Container');
	for(var i = 0; i < arrayMessage.length; i++) {
		arrayMessage[i].innerHTML	= '<span class="miPoundSign">&pound;</span><span class="miPremium">' + Number(premium).toFixed(0) + '</span> <span class="miPerMonthText">per month</span>';
	}
	
	var	arrayPremium = $$('.getInsuranceQuoteLink' + group);
	for(var i = 0; i < arrayPremium.length; i++) {
		arrayPremium[i].innerHTML	= this.ListQuoteString.mixin({company: company})
	}
}

InsuranceQuoteDetails.prototype.CapIdsResponseComplete	= function(capid, regyear, company, premium)
{
	try
	{
		// loop through each vehicle with the same CapId and RegYear to set the premium for all
		// $$('#theID') does not work in IE7 so have to specify the element type where the id attribute id equal to 'theID'
		$$('div[id="insuranceVehicle' + capid + '_' + regyear + 'Premium"]').each(function(obj)
		{
			obj.innerHTML	= '&pound;' + Number(premium).toFixed(0);
		});
		
		// loop through each vehicle with the same CapId and RegYear to set the message for all
		var	sContent	= this.ListQuoteString.mixin({company: company});
		$$('div[id="getInsuranceQuoteLink' + capid + '_' + regyear + '"]').each(function(obj)
		{
			obj.innerHTML	= sContent;
		});
	}
	catch(err)
	{
		//console.error(err);
	}
}

InsuranceQuoteDetails.prototype.GetInsuranceGroups	= function()
{
	var tempArr = new Array();
	$$('.insurancegroup').each(function(item) {
			tempArr.push(item.value);
	});
	
	this.InsuranceGroupsArray = tempArr.uniq();
}

InsuranceQuoteDetails.prototype.GetCapIds	= function()
{
	var tempArr = new Array();
	$$('.capid').each(function(item) {
			tempArr.push(item.value);
	});
	
	this.CapIdArray = tempArr.uniq();
}

InsuranceQuoteDetails.prototype.GetCapIdsForUsedVehicles	= function()
{
	var tempArr = new Array();
	$$('.capid').each(function(item) {
		if(item.value != '')
			tempArr.push(item.value);
	});
	
	this.CapIdArray = tempArr;
}

InsuranceQuoteDetails.prototype.GetVehicleYearsForUsedVehicles	= function()
{
	var tempArr = new Array();
	$$('.vehicle_year').each(function(item) {
			tempArr.push(item.value);
	});
	
	this.VehicleYearsArray = tempArr;
}

InsuranceQuoteDetails.prototype.KillLoadingBoxes	= function()
{
	for(var i=1; i <= 20; i++) {
		$$('.insuranceGroup' + i + 'Container').each(
			function(item) {
				new Effect.Fade(item);
			}
		)
	}	
}

InsuranceQuoteDetails.prototype.DisplayListingEnterDetailsMessage	= function(id)
{
	var sListingEnterDetailsMessage	= this.ListingEnterDetailsMessage;
	$$('.insuranceResponse').each(
		function(item) {
			item.innerHTML		= sListingEnterDetailsMessage;
	});
}

InsuranceQuoteDetails.prototype.ListingQuotationsUsingInsuranceGroup	= function()
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			// displays ajax loaders
			var	arrayAjaxLoaders	= $$('.ajaxLoaderInsuranceQuote');
			
			for(var i = 0; i < arrayAjaxLoaders.length; i++)
			{
				arrayAjaxLoaders[i].style.display	= 'block';
			}
			
			// sets message to inform user the quotes are being calculated
			this.GetInsuranceGroups();
			
			for(var i = 0; i < this.InsuranceGroupsArray.length; i++)
			{
				var arrayQuoteLinks	= $$('.getInsuranceQuoteLink' + this.InsuranceGroupsArray[i]);

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}
			}
				
			// set message for all vehicles without a capid
			arrayQuoteLinks	= $$('.getInsuranceQuoteLink-1');

			for(var j = 0; j < arrayQuoteLinks.length; j++)
			{
				arrayQuoteLinks[j].innerHTML	= 'Quotation unavailable for this vehicle.';
			}
			
			// generate post data from cookie values to retreive quotes
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			detailsArray	= details.split(',');
			
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + 'method=' + this.ListingQuoteUsingInsuranceGroupAjaxMethod
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= sOperator + 'insurancegroups=' + escape(this.InsuranceGroupsArray);
			
			new Ajax.Updater('insuranceResponse', this.ListingQuoteUsingInsuranceGroupAjaxUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miListingQuotationCompletion,
				onFailure: function(){ miResponseReport(true) }, 
				onSuccess: miListingQuotationSuccess
			});
		}
	}
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(true);
	}
}

InsuranceQuoteDetails.prototype.ListingQuotationsUsingCapIdAndDealerIdForUsedVehicles	= function(dealerid)
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			// displays ajax loaders
			var	arrayAjaxLoaders	= $$('.ajaxLoaderInsuranceQuote');
			
			for(var i = 0; i < arrayAjaxLoaders.length; i++)
			{
				arrayAjaxLoaders[i].style.display	= 'block';
			}
			
			// sets message to inform user the quotes are being calculated
			this.GetCapIdsForUsedVehicles();
			this.GetVehicleYearsForUsedVehicles();
			
			for(var i = 0; i < this.CapIdArray.length; i++)
			{
				var arrayQuoteLinks	= $$('.getInsuranceQuoteLink');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}
				
				// set message for all vehicles without a capid
				arrayQuoteLinks	= $$('.getInsuranceQuoteLinkNoQuote');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= 'Quotation unavailable for this vehicle.';
				}
			}
			
			// generate post data from cookie values to retreive quotes
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			detailsArray	= details.split(',');
			
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + this.ListingUsingCapIdsAjaxMethod;
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= sOperator + 'capids=' + escape(this.CapIdArray);
			sPost			+= sOperator + 'vehicleyears=' + escape(this.VehicleYearsArray);
			sPost			+= sOperator + 'dealerid=' + dealerid;
			
			new Ajax.Updater('insuranceResponse', this.ListingQuoteUsingCapIdUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miListingQuotationCompletion,
				onFailure: function(){ miResponseReport(true) }, 
				onSuccess: miListingQuotationSuccess
			});
		}
	}
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(true);
	}
}

InsuranceQuoteDetails.prototype.ListingQuotationsUsingCapIdForUsedVehicles	= function()
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			// displays ajax loaders
			var	arrayAjaxLoaders	= $$('.ajaxLoaderInsuranceQuote');
			
			for(var i = 0; i < arrayAjaxLoaders.length; i++)
			{
				arrayAjaxLoaders[i].style.display	= 'block';
			}
			
			// sets message to inform user the quotes are being calculated
			this.GetCapIdsForUsedVehicles();
			this.GetVehicleYearsForUsedVehicles();
			
			for(var i = 0; i < this.CapIdArray.length; i++)
			{
				var arrayQuoteLinks	= $$('.getInsuranceQuoteLink');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}
				
				// set message for all vehicles without a capid
				arrayQuoteLinks	= $$('.getInsuranceQuoteLinkNoQuote');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= 'Quotation unavailable for this vehicle.';
				}
			}
			
			// generate post data from cookie values to retreive quotes
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			detailsArray	= details.split(',');
			
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + this.ListingUsingCapIdsAjaxMethod;
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= sOperator + 'capids=' + escape(this.CapIdArray);
			sPost			+= sOperator + 'vehicleyears=' + escape(this.VehicleYearsArray);
			
			new Ajax.Updater('insuranceResponse', this.ListingQuoteUsingCapIdUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miListingQuotationCompletion,
				onFailure: function(){ miResponseReport(true) }, 
				onSuccess: miListingQuotationSuccess
			});
		}
	}
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(true);
	}
}

InsuranceQuoteDetails.prototype.FullQuotations	= function(capid)
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			if($('insuranceAjaxLoader') != null) 
				$('insuranceAjaxLoader').style.display	= 'block';
			
			$('insuranceResponse').innerHTML			= 'Calculating Insurance Quotes';
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			
			detailsArray	= details.split(",");
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + 'method=' + this.FullQuotationsAjaxMethod;
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= '&capid=' + capid;
						
			new Ajax.Updater('insuranceResponse', this.FullQuotationsAjaxUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miFullQuotationsCompletion, 
				onFailure: function(){ miResponseReport(false) }, 
				onSuccess: miFullQuotationsSuccess
			});
		}
		else
		{
			$('insuranceResponse').innerHTML	=	this.FullQuoteEnterDetailsMsg;
		}
	} 
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(false);
	}
}

InsuranceQuoteDetails.prototype.FullQuotationsRenderedUsingDealerIdLogin	= function(capid, vehicleprice, dealerid)
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			if($('insuranceAjaxLoader') != null) 
				$('insuranceAjaxLoader').style.display	= 'block';
			
			$('insuranceResponse').innerHTML			= 'Calculating Insurance Quotes';
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			
			detailsArray	= details.split(",");
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + 'method=' + this.FullQuotationsAjaxMethod;
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= sOperator + 'capid=' + capid;
			sPost			+= sOperator + 'vehicleprice=' + vehicleprice;
			sPost			+= sOperator + 'dealerid=' + dealerid;

			new Ajax.Updater('insuranceResponse', this.FullQuotationsAjaxUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miFullQuotationsCompletion, 
				onFailure: function(){ miResponseReport(false) }, 
				onSuccess: miFullQuotationsSuccess
			});
		}
		else
		{
			$('insuranceResponse').innerHTML	=	this.FullQuoteEnterDetailsMsg;
		}
	} 
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(false);
	}
}

InsuranceQuoteDetails.prototype.FullQuotationsRendered	= function(capid, vehicleprice)
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			if($('insuranceAjaxLoader') != null) 
				$('insuranceAjaxLoader').style.display	= 'block';
			
			$('insuranceResponse').innerHTML			= 'Calculating Insurance Quotes';
			var	sPost		= '';
			var sOperator	= '';
			var	details		= new Array();
			details			= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
			
			detailsArray	= details.split(",");
			for(var i = 0; i < detailsArray.length; i++)
			{
				sPost		+= sOperator + detailsArray[i];
				sOperator	= '&';
			}
			
			sPost			+= sOperator + 'method=' + this.FullQuotationsAjaxMethod;
			sPost			+= sOperator + 'promocode=' + this.PromoCode;
			sPost			+= sOperator + 'capid=' + capid;
			sPost			+= sOperator + 'vehicleprice=' + vehicleprice;
			sPost			+= sOperator + 'vehicleReg=' + vehicleReg;
			sPost			+= sOperator + 'vehicleMileage=' + vehicleMileage;

			new Ajax.Updater('insuranceResponse', this.FullQuotationsAjaxUrl, { postBody: sPost, method: 'post', evalScripts: true, 
				onComplete: miFullQuotationsCompletion, 
				onFailure: function(){ miResponseReport(false) }, 
				onSuccess: miFullQuotationsSuccess
			});
		}
		else
		{
			$('insuranceResponse').innerHTML	=	this.FullQuoteEnterDetailsMsg;
		}
	} 
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(false);
	}
}

function miSetGender(obj)
{
	switch(obj.value)
	{
		case 'Mr':
			document.frmMotorInsurance.gender[0].checked	= true;
			break;
		case 'Mrs':
			document.frmMotorInsurance.gender[1].checked	= true;
			break;
		case 'Ms':
			document.frmMotorInsurance.gender[1].checked	= true;
			break;
		case 'Miss':
			document.frmMotorInsurance.gender[1].checked	= true;
			break;
		case 'Other':
			document.frmMotorInsurance.gender[0].checked	= true;
			break;
	}
}

function miListingQuotationCompletion(response)
{
	// hide ajax loaders
	var	arrayAjaxLoaders	= $$('.ajaxLoaderInsuranceQuote');
	for(var i = 0; i < arrayAjaxLoaders.length; i++)
	{
		arrayAjaxLoaders[i].style.display	= 'none';
	}
}

function miFullQuotationsCompletion()
{
	// hide ajax loaders
	if($('insuranceAjaxLoader') != null) 
		$('insuranceAjaxLoader').style.display = 'none'; 
	
	if($('insuranceResponse').innerHtml == '')
		$('insuranceResponse').innerHtml = '<div id=\"insuranceUnavailableMessage\">Unfortunately the car insurance quotation calculation failed.</div>';
}

function miResponseReport(bListing)
{
	if(bListing)
	{
		var insuranceQuotes		= new InsuranceQuoteDetails();
		insuranceQuotes.GetInsuranceGroups();
		for(var i = 0; i < insuranceQuotes.InsuranceGroupsArray.length; i++)
		{
			var arrayQuoteLinks	= $$('.getInsuranceQuoteLink' + insuranceQuotes.InsuranceGroupsArray[i]);

			for(var j = 0; j < arrayQuoteLinks.length; j++)
			{
				arrayQuoteLinks[j].innerHTML	= '<div id=\"insuranceUnavailableMessage\">Unfortunately the car insurance quotation calculation failed.</div>';
			}
		}
	}
	else
	{
		$('insuranceResponse').innerHTML	= '<div id=\"insuranceUnavailableMessage\">Unfortunately the car insurance quotation calculation failed.</div>';
	}
}

function miShowhide(id, show)
{
	if(show)
		$(id).style.display		= 'block';
	else
		$(id).style.display		= 'none';
}

function miListingQuotationSuccess()
{
}

function miFullQuotationsSuccess()
{
}

var	bMiReturn	= true;

// displays window with all required fields to add a new user and return insurance quotes
function miShowNewUserWindow(insuranceObject, functionName, capId) 
{
	var QuoteDetails		= new InsuranceQuoteDetails();
	QuoteDetails.IsPopup	= true;
	
	Dialog.confirm('<div id="insuranceRegistrationPopupDetails">\n' + QuoteDetails.RenderFullForm() + '</div>\n', {windowParameters: {recenterAuto: false, className: "dialog", width:600, height: 560}, okLabel: "Get Quotes", cancelLabel: "Close", 
		ok: function(win) {
			if(miValidateFullInsuranceDetails())
			{
				miAddUser();
				if(bMiReturn)
				{
					miAddInsuranceCookie('');
					//window.location	= window.location;
					switch(functionName)
					{
						case 'ListingQuotationsUsingInsuranceGroup':
							insuranceObject.ListingQuotationsUsingInsuranceGroup();
							break;
						case 'FullQuotations':
							insuranceObject.FullQuotations(capId);
							break;
						default:
							window.location	= window.location;
							break;
					}
				}
				return bMiReturn;
			}
			else 
				return false;
		}
	});
}

//displays window with all fields required to return insurance quotes and save credentials for existing user
function miShowExistingUserWindow(iUserId) 
{
	var QuoteDetails				= new InsuranceQuoteDetails();
	QuoteDetails.IsPopup			= true;
	QuoteDetails.ExistingUser		= true;
	QuoteDetails.ExistingUserId		= iUserId;
	
	m_InsuranceCookieUserId		= -1;
	Dialog.confirm('<div id="insuranceRegistrationPopupDetails">\n' + QuoteDetails.RenderFullForm() + '</div>\n', {windowParameters: {recenterAuto: false, className: "dialog", width:600, height: 560}, okLabel: "Get Quotes", cancelLabel: "Close", 
		ok: function(win) {
			if(miValidateFullInsuranceDetails())
			{
				miAddUserInsuranceDetails();
				if(bMiReturn)
				{
					var userId		= (Number(m_InsuranceCookieUserId) != -1) ? m_InsuranceCookieUserId : iUserId;
					miAddInsuranceCookie(userId);
					window.location	= window.location;
				}
				return bMiReturn;
			}
			else
				return false;
		}
	});
}

// displays window with all fields required to edit existing user details
function miShowEditWindow(iUserId)
{
	var QuoteDetails			= new InsuranceQuoteDetails();
	if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
	{
		var details					= miGetInsuranceCookieValue('insuranceQuoteFormDetails');
		var detailsArray			= details.split(",");
		
		for(var i = 0; i < detailsArray.length; i++)
		{
			var splitCookie			= detailsArray[i].split('=');
			QuoteDetails.CookieValues[splitCookie[0]]	= splitCookie[1];
		}
	}
	
	QuoteDetails.IsPopup		= true;
	QuoteDetails.EditDetails	= true;
	QuoteDetails.ExistingUserId	= iUserId;
	
	m_InsuranceCookieUserId		= -1;
	Dialog.confirm('<div id="insuranceRegistrationPopupDetails">\n' + QuoteDetails.RenderFullForm() + '</div>\n', {windowParameters: {recenterAuto: false, className: "dialog", width:600, height: 560}, okLabel: "Update Details", cancelLabel: "Close", 
		ok: function(win) {
			if(miValidateFullInsuranceDetails())
			{
				miUpdateUserInsuranceDetails(); 
				if(bMiReturn) 
				{
					var iUserId		= (Number(m_InsuranceCookieUserId) != -1) ? m_InsuranceCookieUserId : QuoteDetails.CookieValues['userid'];
					miAddInsuranceCookie(iUserId);
					window.location	= window.location;
				}
				return bMiReturn;
			}
			else
				return false;
		}
	});
	/*}
	else
	{
		Dialog.alert('Your cookie has been removed, Unfortuately your credentials are unavailable.', { windowParameters: { width:300, height:100 }, okLabel: "close" });
	}*/
}

/******************************************************
	Validation
******************************************************/

function miValidateFullInsuranceDetails()
{
	var objs				= new Array();
	var msgs  				= new Array();
	var valid				= true;
	var error				= $('insuranceQuoteResponseMessage');
	
	var forename  			= $('forename');
	var surname  			= $('surname');
	var postcode			= $('postcode');
	var building			= $('building');
	var email	  			= $('email');
	var telephone  			= $('telephone');
	var dobDate	  			= $('dobDate');
	var dobMonth  			= $('dobMonth');
	var dobYear			 	= $('dobYear');
	
	var dob					= dobDate.value + '/' + dobMonth.value + '/' + dobYear.value;
	var exclude				= /[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check				= /@[\w\-]+\./;
	var checkend			= /\.[a-zA-Z]{2,4}$/;

	error.value				= '';
	forename.style.border	= '1px solid #999';
	surname.style.border	= '1px solid #999';
	postcode.style.border	= '1px solid #999';
	building.style.border	= '1px solid #999';
	dobDate.style.border	= '1px solid #999';
	dobMonth.style.border	= '1px solid #999';
	dobYear.style.border	= '1px solid #999';
	email.style.border		= '1px solid #999';
	telephone.style.border	= '1px solid #999';
	
	if(forename.value == mi_autofill)
	{
		miAutofill(true);
		autofilled			= true;
	}

	if(forename.value == '') 
	{
		msgs.push('Please enter a Forename');
		objs.push(forename);
		valid = false;					
	} 
	
	if(surname.value == "") 
	{
		msgs.push('Please enter a Surname');
		objs.push(surname);
		valid = false;					
	} 
	
	if(postcode.value == '' || !miValiDatePostcode(postcode.value)) 
	{
		msgs.push('Please enter a valid Postcode');
		objs.push(postcode);
		valid = false;					
	} 
	
	if(building.value != '' && !miCheckNumber(building)) 
	{
		msgs.push('Please enter a valid Building Number');
		objs.push(building);
		valid = false;					
	} 
		
	if((((email.value.search(exclude) != -1) || (email.value.search(check)) == -1) || (email.value.search(checkend) == -1)))
	{
		msgs.push('Please enter a valid Email');
		objs.push(email);
		valid = false;					
	}
	
	if(telephone.value != '' && !miCheckTelNumber(telephone)) 
	{
		msgs.push('Please enter a valid Telephone Number');
		objs.push(telephone);
		valid = false;					
	}
	
	if(!miValiDate(dob, 1) || !miCheckAge(dob)) 
	{
		msgs.push('Please enter a valid Date Of Birth<br />(Must be 21 or over)');
		objs.push(dobDate);
		objs.push(dobMonth);
		objs.push(dobYear);
		valid = false;
	}
	
	if(!valid) 
	{
		var sErrorMessage		= '<ul>';
		for(var i = 0; i < objs.length; i++ ) 
		{
			objs[i].style.border = 'red 2px solid';
			if(msgs[i] != null) 
			{
				//var id	= objs[i].name + 'ErrorMessage';
				//if(objs[i].name.indexOf('dob') > -1)
					//id	= 'dobErrorMessage';
				//$(id).innerHTML = msgs[i];
				sErrorMessage	+= '<li>' + msgs[i] + '</li>'
			}
		}
		sErrorMessage		+= '</ul>';
		$('errorMessage').innerHTML		= sErrorMessage;
	}
	
	return valid;
}


function miValiDatePostcode(sText)
{
	var sFormat = new RegExp(/^(GIR ?0AA)|([A-PR-UWYZ]((\d(\d|[A-HJKSTUW])?)|([A-HK-Y]\d(\d|[ABEHMNPRV-Y])?)) ?\d[ABD-HJLNP-UW-Z]{2})$/i);
	var bCheck = sFormat.test(sText);
	
	if(bCheck)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function miCheckNumber(obj)
{
	var sValue		= miCleanNumber(obj);
	var sFormat		= new RegExp(/^[0-9 ]+$/i);
	return sFormat.test(sValue);
}

function miCleanNumber(obj)
{
	var sValue		= obj.value.replace(/[\£\$\s\,]/g,'');
	obj.value		= sValue;
	return	sValue;
}

function miReplaceChar(intNumber, charReplace, charReplaceWith)
{
	while(intNumber.indexOf(charReplace) != -1)
	{
		intNumber = intNumber.replace(charReplace, charReplaceWith);
	}
	return intNumber;
}

function miCheckTelNumber(obj)
{
	var iNumber = obj.value;
	if(miCheckNumber(obj))
	{
		iNumber		= miReplaceChar(iNumber, ' ', '');
		var sFormat = new RegExp(/^0[12578]\d{9}$/);		
		var check	= sFormat.test(iNumber);
		
		if(!check)
			return false
		else
			return true;
	}
	else
		return false;
}

function miValiDate(value, iFormatId)
{
	if(iFormatId == 1)
	{
		var sFormat = 'dd/mm/yyyy';
	}
	else if(iFormatId == 2)
	{
		var sFormat = 'mm/dd/yyyy';
	}
	else if(iFormatId == 3)
	{
		var sFormat = 'yyyy/mm/dd';
	}
	else
	{
		var sFormat = 'dd/mm/yyyy';
	}
	
	var sDateSplit = DateComponents(value, sFormat);
	
	if (sDateSplit == null) return false;
	
	var iDay	= sDateSplit[0];
	var iMonth	= sDateSplit[1];
	var iYear	= sDateSplit[2];

	if (iYear.length == 2) 
	{
		var dtNow = new Date();
		if(((parseInt(iYear) + 2000)) > dtNow.getFullYear())
		{
			iYear = '19' + iYear;
		}
		else
		{
			iYear  = '20' + iYear;
		}
	}
	
	if ((iMonth < 1 || iMonth > 12) || (iDay < 1 || iDay > 31)) // check month range 
	{
		return false;
	} 
	if ((iMonth == 4 || iMonth == 6 || iMonth == 9 || iMonth == 11) && iDay == 31) 
	{
		return false;
	}
	if (iMonth == 2) // check for february 29th 
	{	
		var bIsLeap = (iYear % 4 == 0 && (iYear % 100 != 0 || iYear % 400 == 0)); 
		
		if (iDay > 29 || (iDay == 29 && !bIsLeap)) 
		{
			return false;
		}
	}
	
	if(iYear < 1753 || iYear > 9999)
	{
		return false;
	}
	
	return true;
}

function miCheckAge(date)
{
	var dateArray		= date.split("/");
	var minAge			= 17;
	var dteTheirDate	= new Date(((parseInt(dateArray[2])) + minAge), dateArray[1], dateArray[0]);
	var dteToday		= new Date();
	
	if((dteToday.getTime() - dteTheirDate.getTime()) < 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function DateComponents(dateStr, format) 
{
	var results		= new Array();
	var datePat		= /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	var matchArray	= dateStr.match(datePat);
	
	if (matchArray == null) return null; 
	// parse date into variables
	if (format.charAt(0) == "d") //format=dd/mm/yyyy
	{
		results[0] = matchArray[1];
		results[1] = matchArray[3];
		results[2] = matchArray[4];
	} 
	else if (format.charAt(0) == "m") //format=mm/dd/yyyy
	{
		results[0] = matchArray[3];
		results[1] = matchArray[1];
		results[2] = matchArray[4];
	} 
	else //format=yyyy/mm/dd
	{
		results[0] = matchArray[4];
		results[1] = matchArray[3];		
		results[2] = matchArray[1];
	}

	return results;
}

/******************************************************
	Autofill
******************************************************/

function miAutofill(obj, fullForm)
{
	if(obj.value == mi_autofill)
	{
		if(fullForm)
		{
			$('forename').value					= 'Codeweavers';
			$('surname').value					= 'TestInsurance';
		}
		
		$('postcode').value						= 'ST46EX';
		$('building').value						= '18';
		$('email').value						= 'christopher.knight@codeweavers.net';
		$('telephone').value					= '08704430888';
		$('insuranceMileage').selectedIndex		= 6;
		$('dobDate').selectedIndex				= 0;
		$('dobMonth').selectedIndex				= 0;
		$('dobYear').selectedIndex				= 20;
		$('noClaimsDiscount').selectedIndex		= 6;
	}
}

/******************************************************
	Cookies
******************************************************/

function miAddInsuranceCookie(iUserId)
{
	var cookieValue			= new Array();
	var dob					= $F('dobDate') + '/' + $F('dobMonth') + '/' + $F('dobYear');
	var isFemale			= ($('gender').checked) ? false : true;
	
	if(m_InsuranceCookieUserId != -1)
		cookieValue.push('userid='				+ m_InsuranceCookieUserId);
	else
		cookieValue.push('userid='				+ iUserId);
	
	cookieValue.push('title='				+ escape($('title').value));
	cookieValue.push('forename='			+ escape($F('forename')));
	cookieValue.push('surname='				+ escape($F('surname')));
	cookieValue.push('postcode='			+ escape($F('postcode').replace(' ', '')));
	cookieValue.push('building='			+ escape($F('building')));
	cookieValue.push('buildingname='		+ escape($F('buildingName')));
	cookieValue.push('gender='				+ escape(isFemale));
	cookieValue.push('dob='					+ escape(dob));
	cookieValue.push('email='				+ escape($F('email')));
	cookieValue.push('telephone='			+ escape($F('telephone')));
	cookieValue.push('insurancemileage='	+ escape($F('insuranceMileage')));
	cookieValue.push('tracker='				+ escape($('tracker').checked));
	cookieValue.push('garaged='				+ escape($('garaged').checked));
	cookieValue.push('accidents='			+ escape($('accidents').checked));
	cookieValue.push('convictions='			+ escape($('convictions').checked));
	cookieValue.push('covertypeid='			+ escape($F('coverType')));
	cookieValue.push('noClaimsDiscount='	+ escape($F('noClaimsDiscount')));
	
	miSetInsuranceCookie('insuranceQuoteFormDetails', cookieValue);
}

function miSetInsuranceCookie(cookieName, cookieValue)
{
	cookieValue			= escape(cookieValue);
	var dateExpiry		= new Date();
	dateExpiry.setDate(dateExpiry.getDate() + 14);
	document.cookie		= cookieName + '=' + cookieValue + ';path=/;expires=' + dateExpiry;
}
			
function miGetInsuranceCookieValue(cookieName)
{
	var cookieValue		= document.cookie;
	var cookieStartsAt	= cookieValue.indexOf(" " + cookieName + '=');
	
	if (cookieStartsAt == -1) {
		cookieStartsAt = cookieValue.indexOf(cookieName + '=');
	}
	
	if (cookieStartsAt == -1) {
		cookieValue = null;
	} else {
		if(cookieValue.indexOf('covertype%3D') > -1)
		{
			var expires			= "";
			document.cookie		= cookieName + "=;expires=-1; path=/";
			cookieValue			= null;
		}
		else if(cookieValue.indexOf('noClaimsDiscount%3D') == -1)
		{
			var expires			= "";
			document.cookie		= cookieName + "=;expires=-1; path=/";
			cookieValue			= null;
		}
		else
		{
			cookieStartsAt		= cookieValue.indexOf('=', cookieStartsAt) + 1;
			var cookieEndsAt	= cookieValue.indexOf(';', cookieStartsAt);
			
			if (cookieEndsAt == -1)	{
				cookieEndsAt		= cookieValue.length;
			}
			cookieValue			= unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
			
			if(cookieValue == '')
				cookieValue		= null;
			else
			{
				cookieValue		= cookieValue.replace('covertypeid', 'covertype');
			}
		}
	}
	return cookieValue;
}


/******************************************************
	Blank functions to be overwritten by client
******************************************************/

function miAddUser()
{
}

function miAddUserInsuranceDetails()
{
}

function miUpdateUserInsuranceDetails()
{
}