var	m_InsuranceCookieUserId	= -1; // to be set in miAddUser if webservice client saves the user
var	mi_autofill				= '*.*autofill';

/******************************************************
	InsuranceQuoteDetails Object and Methods
******************************************************/
// inserting via DOM fails in Safari 2.0, so brute force approach
//document.write('<script type="text/javascript" src="insurance_window.js"></script>');

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.VehicleIdsArray								= 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" methode="post" id="frmMotorInsurance" action="insurance.aspx"><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)
		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)
		sSrc	+=	' value="' + unescape(this.CookieValues['building']) + '"';
	sSrc			+= ' />&nbsp;<span style="color:Black;">or House Name:</span> <input name="buildingName" type="text" id="buildingName" class="textShort"'
	if(this.EditDetails)
		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)
		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)
		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" class="radio"';
	if(this.EditDetails && this.CookieValues['gender'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">Male</span>\n';

	sSrc			+= '			<input type="radio" id="gender" name="gender" value="true" class="radio"';
	if(this.EditDetails && this.CookieValues['gender'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> <span style="color:Black;">Female</span>\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)
	{
		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://secure.codeweavers.net/miwebservices/images/rsa_small.jpg" /><br />';
	sSrc			+= '				<img src="https://secure.codeweavers.net/miwebservices/images/norwichunion_small.gif" /><br />';
	sSrc			+= '				<img src="https://secure.codeweavers.net/miwebservices/images/cornhill_small.gif" /><br />';
	sSrc			+= '				<img src="https://secure.codeweavers.net/miwebservices/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" class="radio"';
	if(this.EditDetails && this.CookieValues['tracker'].toLowerCase() == 'true')
	{
		sSrc	+=	' checked=checked"';
	}
	sSrc	+=	' /> <span style="color:Black;">Yes</span>\n';
	sSrc			+= '			<input type="radio" id="tracker" name="tracker" value="false" class="radio"';
	if(this.EditDetails && this.CookieValues['tracker'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">No</span>\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" class="radio"';
	if(this.EditDetails && this.CookieValues['garaged'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> <span style="color:Black;">Yes</span>\n';
	sSrc			+= '			<input type="radio" id="garaged" name="garaged" value="false" class="radio"';
	if(this.EditDetails && this.CookieValues['garaged'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">No</span>\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['covertypeid'] == '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['covertypeid'] == '3')
		sSrc	+=	' selected';
	sSrc	+=	'>Insured Plus 1 Named Driver Over 25</option>\n';
	sSrc			+= '				<option value="2"';
	if(this.EditDetails && this.CookieValues['covertypeid'] == '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 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" class="radio"';
	if(this.EditDetails && this.CookieValues['accidents'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked"';
	sSrc	+=	' /> <span style="color:Black;">Yes</span>\n';
	sSrc			+= '			<input type="radio" id="accidents" name="accidents" value="false" class="radio"';
	if(this.EditDetails && this.CookieValues['accidents'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">No</span>\n';
	sSrc			+= '		</td>\n';
	sSrc			+= '	</tr>\n';

	sSrc			+= '	<tr>\n';
	sSrc			+= '		<td class="registrationLabel"><br />Have You 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" class="radio"';
	if(this.EditDetails && this.CookieValues['convictions'].toLowerCase() == 'true')
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">Yes</span>\n';
	sSrc			+= '			<input type="radio" id="convictions" name="convictions" value="false" class="radio"';
	if(this.EditDetails && this.CookieValues['convictions'].toLowerCase() != 'true')
		sSrc	+=	' checked=checked';
	else if(!this.EditDetails)
		sSrc	+=	' checked=checked';
	sSrc	+=	' /> <span style="color:Black;">No</span>\n';
	if(this.EditDetails)
		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';
		}
		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.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.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 = document.getElementsByClassName('insuranceGroup' + group + 'Container');
	for(var i = 0; i < arrayMessage.length; i++) {
		arrayMessage[i].innerHTML	= '&pound;' + Number(premium).toFixed(2);
	}

	var	arrayPremium = document.getElementsByClassName('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.VehicleIdsResponseComplete	= function(vehicleid, company, premium)
{
	try
	{
		//  set the premium
		$('insuranceVehicle' + vehicleid + '_Premium').innerHTML	= '&pound;' + Number(premium).toFixed(0);

		// set the message
		var	sContent	= this.ListQuoteString.mixin({ company: company });
		$('getInsuranceQuoteLink' + vehicleid).innerHTML	= sContent;
	}
	catch(err)
	{
		//console.error(err);
	}
}

InsuranceQuoteDetails.prototype.GetInsuranceGroups	= function()
{
	var tempArr = new Array();
	document.getElementsByClassName('insurancegroup').each(function(item) {
			tempArr.push(item.value);
	});

	this.InsuranceGroupsArray = tempArr.uniq();
}

InsuranceQuoteDetails.prototype.GetCapIds	= function()
{
	var tempArr = new Array();
	document.getElementsByClassName('capid').each(function(item) {
			tempArr.push(item.value);
	});

	this.CapIdArray = tempArr.uniq();
}

InsuranceQuoteDetails.prototype.GetCapIdsForUsedVehicles	= function()
{
	var tempArr = new Array();
	document.getElementsByClassName('capid').each(function(item) {
		if(item.value != '')
			tempArr.push(item.value);
	});

	this.CapIdArray = tempArr;
}

InsuranceQuoteDetails.prototype.GetVehicleYearsForUsedVehicles	= function()
{
	var tempArr = new Array();
	document.getElementsByClassName('vehicle_year').each(function(item) {
			tempArr.push(item.value);
	});

	this.VehicleYearsArray = tempArr;
}

InsuranceQuoteDetails.prototype.GetVehicleIdsForUsedVehicles	= function()
{
	var tempArr = new Array();
	document.getElementsByClassName('vehicle_id').each(function(item) {
			tempArr.push(item.value);
	});

	this.VehicleIdsArray = tempArr;
}

InsuranceQuoteDetails.prototype.KillLoadingBoxes	= function()
{
	for(var i=1; i <= 20; i++) {
		document.getElementsByClassName('insuranceGroup' + i + 'Container').each(
			function(item) {
				new Effect.Fade(item);
			}
		)
	}
}

InsuranceQuoteDetails.prototype.DisplayListingEnterDetailsMessage	= function(id)
{
	var sListingEnterDetailsMessage	= this.ListingEnterDetailsMessage;
	document.getElementsByClassName('insuranceResponse').each(
		function(item) {
			item.innerHTML		= sListingEnterDetailsMessage;
	});
}

InsuranceQuoteDetails.prototype.ListingQuotationsUsingInsuranceGroup	= function()
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			// displays ajax loaders
			var	arrayAjaxLoaders	= document.getElementsByClassName('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	= document.getElementsByClassName('getInsuranceQuoteLink' + this.InsuranceGroupsArray[i]);

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}
			}

			// 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
			});
		}
		else
		{
			this.GetInsuranceGroups();

			for(var i = 0; i < this.InsuranceGroupsArray.length; i++)
			{
				var arrayQuoteLinks	= document.getElementsByClassName('getInsuranceQuoteLink' + this.InsuranceGroupsArray[i]);

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.ListingEnterDetailsMessage;
				}
			}
		}
	}
	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	= document.getElementsByClassName('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();
			this.GetVehicleIdsForUsedVehicles();

			for(var i = 0; i < this.CapIdArray.length; i++)
			{
				var arrayQuoteLinks	= document.getElementsByClassName('getInsuranceQuoteLink');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}

				// set message for all vehicles without a capid
				arrayQuoteLinks	= document.getElementsByClassName('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 + 'vehicleids=' + escape(this.VehicleIdsArray);
			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
			});
		}
		else
		{
			this.GetInsuranceGroups();

			for(var i = 0; i < this.InsuranceGroupsArray.length; i++)
			{
				var arrayQuoteLinks	= document.getElementsByClassName('getInsuranceQuoteLink' + this.InsuranceGroupsArray[i]);

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.ListingEnterDetailsMessage;
				}
			}
		}
	}
	catch(err)
	{
		//if(console != null) console.log(err);
		miResponseReport(true);
	}
}

InsuranceQuoteDetails.prototype.ListingQuotationsUsingCapIdForUsedVehicles	= function()
{
	try
	{
		if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
		{
			// displays ajax loaders
			var	arrayAjaxLoaders	= document.getElementsByClassName('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();
			this.GetVehicleIdsForUsedVehicles();

			for(var i = 0; i < this.CapIdArray.length; i++)
			{
				var arrayQuoteLinks	= document.getElementsByClassName('getInsuranceQuoteLink');

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.CalculatingQuotationsListingMessage;
				}

				// set message for all vehicles without a capid
				arrayQuoteLinks	= document.getElementsByClassName('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 + 'vehicleids=' + escape(this.VehicleIdsArray);
			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
			});
		}
		else
		{
			this.GetInsuranceGroups();

			for(var i = 0; i < this.InsuranceGroupsArray.length; i++)
			{
				var arrayQuoteLinks	= document.getElementsByClassName('getInsuranceQuoteLink' + this.InsuranceGroupsArray[i]);

				for(var j = 0; j < arrayQuoteLinks.length; j++)
				{
					arrayQuoteLinks[j].innerHTML	= this.ListingEnterDetailsMessage;
				}
			}
		}
	}
	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	= document.getElementsByClassName('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	= document.getElementsByClassName('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()
{
	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: 620}, okLabel: "Get Quotes", cancelLabel: "Close",
		ok: function(win) {
			if(miValidateFullInsuranceDetails())
			{
				miAddUser();
				if(bMiReturn)
				{
					miAddInsuranceCookie('');
					window.location	= window.location;
				}
				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: 520}, 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()
{

	if(miGetInsuranceCookieValue('insuranceQuoteFormDetails') != null)
	{
		var QuoteDetails			= new InsuranceQuoteDetails();
		QuoteDetails.IsPopup		= true;
		QuoteDetails.EditDetails	= true;

		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];
		}

		m_InsuranceCookieUserId		= -1;
		Dialog.confirm('<div id="insuranceRegistrationPopupDetails" style="position:0">\n' + QuoteDetails.RenderFullForm() + '</div>\n', {windowParameters: {recenterAuto: false, className: "dialog", width:600, height: 620}, 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()
{
    if(document.getElementById('visDiv')!=null)
	document.getElementById('visDiv').style.visibility='visible';
    if(document.getElementById('grid')!=null)
	document.getElementById('grid').style.visibility='hidden';

	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,3}$/;

	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()
{
}






















// Copyright (c) 2006 SÃ©bastien Gruhier (http://xilinus.com, http://itseb.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// VERSION 0.99

var Window = Class.create();
Window.keepMultiModalWindow = false;
Window.prototype = {
  // Constructor
  // Available parameters : className, title, minWidth, minHeight, maxWidth, maxHeight, width, height, top, left, bottom, right, resizable, zIndex, opacity, recenterAuto, wiredDrag
  //                        hideEffect, showEffect, showEffectOptions, hideEffectOptions, effectOptions, url, draggable, closable, minimizable, maximizable, parent, onload
  initialize: function(id) {
    if ($(id))
      alert("Window " + id + " is already register is the DOM!!, be sure to use setDestroyOnClose()")

    this.hasEffectLib = String.prototype.parseColor != null;
    this.options = Object.extend({
      className:         "dialog",
      minWidth:          100,
      minHeight:         20,
      resizable:         true,
      closable:          true,
      minimizable:       true,
      maximizable:       true,
      draggable:         true,
      userData:          null,
      showEffect:        (this.hasEffectLib ? Effect.Appear : Element.show),
      hideEffect:        (this.hasEffectLib ? Effect.Fade : Element.hide),
      showEffectOptions: {},
      hideEffectOptions: {},
      effectOptions:     null,
      parent:            document.getElementsByTagName("body").item(0),
      title:             "&nbsp;",
      url:               null,
      onload:            Prototype.emptyFunction,
      width:             200,
      height:            300,
      opacity:           1,
      recenterAuto:      true,
      wiredDrag:         false
    }, arguments[1] || {});

    if (this.options.effectOptions) {
      Object.extend(this.options.hideEffectOptions, this.options.effectOptions);
      Object.extend(this.options.showEffectOptions, this.options.effectOptions);
    }
    if (this.options.hideEffect == Element.hide)
      this.options.hideEffect = function(){ Element.hide(this.element); if (this.destroyOnClose) this.destroy(); }.bind(this)

    this.element = this._createWindow(id);

    // Bind event listener
    this.eventMouseDown = this._initDrag.bindAsEventListener(this);
    this.eventMouseUp   = this._endDrag.bindAsEventListener(this);
    this.eventMouseMove = this._updateDrag.bindAsEventListener(this);
    this.eventOnLoad    = this._getWindowBorderSize.bindAsEventListener(this);
    this.eventMouseDownContent = this.toFront.bindAsEventListener(this);
    this.eventResize = this._recenter.bindAsEventListener(this);

    this.topbar = $(this.element.id + "_top");
    this.bottombar = $(this.element.id + "_bottom");
    this.content = $(this.element.id + "_content");

    Event.observe(this.topbar, "mousedown", this.eventMouseDown);
    Event.observe(this.bottombar, "mousedown", this.eventMouseDown);
    Event.observe(this.content, "mousedown", this.eventMouseDownContent);
    Event.observe(window, "load", this.eventOnLoad);
    Event.observe(window, "resize", this.eventResize);
    Event.observe(window, "scroll", this.eventResize);

    if (this.options.draggable)  {
      this.bottombar.addClassName("bottom_draggable");
      this.topbar.addClassName("top_draggable");
    }

    if (this.options.resizable) {
      this.sizer = $(this.element.id + "_sizer");
      Event.observe(this.sizer, "mousedown", this.eventMouseDown);
    }

    this.useLeft = null;
    this.useTop = null;
    if (arguments[1].left != null) {
      this.element.setStyle({left: parseFloat(arguments[1].left) + 'px'});
      this.useLeft = true;
    }
    if (arguments[1].right != null) {
      this.element.setStyle({right: parseFloat(arguments[1].right) + 'px'});
      this.useLeft = false;
    }
    if (this.useLeft == null) {
      this.element.setStyle({left: "0px"});
      this.useLeft = true;
    }

    if (arguments[1].top != null) {
      this.element.setStyle({top: parseFloat(arguments[1].top) + 'px'});
      this.useTop = true;
    }
    if (arguments[1].bottom != null) {
      this.element.setStyle({bottom: parseFloat(arguments[1].bottom) + 'px'});
      this.useTop = false;
    }
    if (this.useTop == null) {
      this.element.setStyle({top: "0px"});
      this.useTop = true;
    }

    this.storedLocation = null;

    this.setOpacity(this.options.opacity);
    if (this.options.zIndex)
      this.setZIndex(this.options.zIndex)

    this.destroyOnClose = false;

    this._getWindowBorderSize();
    this.width = this.options.width;
    this.height = this.options.height;
    this.visible = false;

    if (this.width && this.height)
      this.setSize(this.options.width, this.options.height);
    this.setTitle(this.options.title)
    Windows.register(this);
  },

  // Destructor
   destroy: function() {
    Windows.notify("onDestroy", this);
    Event.stopObserving(this.topbar, "mousedown", this.eventMouseDown);
    Event.stopObserving(this.bottombar, "mousedown", this.eventMouseDown);
    Event.stopObserving(this.content, "mousedown", this.eventMouseDownContent);

    Event.stopObserving(window, "load", this.eventOnLoad);
    Event.stopObserving(window, "resize", this.eventResize);
    Event.stopObserving(window, "scroll", this.eventResize);

    Event.stopObserving(this.content, "load", this.options.onload);

    if (this._oldParent) {
      var content = this.getContent();
      var originalContent = null;
      for(var i = 0; i < content.childNodes.length; i++) {
        originalContent = content.childNodes[i];
        if (originalContent.nodeType == 1)
          break;
        originalContent = null;
      }
      if (originalContent)
        this._oldParent.appendChild(originalContent);
      this._oldParent = null;
    }

    if (this.sizer)
        Event.stopObserving(this.sizer, "mousedown", this.eventMouseDown);

    if (this.options.url)
      this.content.src = null

     if(this.iefix)
      Element.remove(this.iefix);

    Element.remove(this.element);
    Windows.unregister(this);
  },

  // Sets window deleagte, should have functions: "canClose(window)"
  setDelegate: function(delegate) {
    this.delegate = delegate
  },

  // Gets current window delegate
  getDelegate: function() {
    return this.delegate;
  },

  // Gets window content
  getContent: function () {
    return this.content;
  },

  // Sets the content with an element id
  setContent: function(id, autoresize, autoposition) {
    var element = $(id);
    if (null == element) throw "Unable to find element '" + id + "' in DOM";
    this._oldParent = element.parentNode;

    var d = null;
    var p = null;

    if (autoresize)
      d = Element.getDimensions(element);
    if (autoposition)
      p = Position.cumulativeOffset(element);

    var content = this.getContent()
    content.appendChild(element);
    element.show();
    if (autoresize)
      this.setSize(d.width, d.height);
    if (autoposition)
      this.setLocation(p[1] - this.heightN, p[0] - this.widthW);
  },

  setAjaxContent: function(url, options, showCentered, showModal) {
    this.showFunction = showCentered ? "showCenter" : "show";
    this.showModal = showModal || false;

    if (options == null)
      options = {}
    this.onComplete = options.onComplete;
    options.onComplete = this._setAjaxContent.bind(this);

    new Ajax.Request(url, options);
  },

  _setAjaxContent: function(originalRequest) {
    Element.update(this.getContent(), originalRequest.responseText);
    if (this.onComplete)
      this.onComplete(originalRequest);
    this[this.showFunction](this.showModal)
  },

  // Stores position/size in a cookie, by default named with window id
  setCookie: function(name, expires, path, domain, secure) {
    name = name || this.element.id;
    this.cookie = [name, expires, path, domain, secure];

    // Get cookie
    var value = WindowUtilities.getCookie(name)
    // If exists
    if (value) {
      var values = value.split(',');
      var x = values[0].split(':');
      var y = values[1].split(':');

      var w = parseFloat(values[2]), h = parseFloat(values[3]);
      var mini = values[4];
      var maxi = values[5];

      this.setSize(w, h);
      if (mini == "true")
        this.doMinimize = true; // Minimize will be done at onload window event
      else if (maxi == "true")
        this.doMaximize = true; // Maximize will be done at onload window event

      this.useLeft = x[0] == "l";
      this.useTop = y[0] == "t";

      this.element.setStyle(this.useLeft ? {left: x[1]} : {right: x[1]});
      this.element.setStyle(this.useTop ? {top: y[1]} : {bottom: y[1]});
    }
  },

  // Gets window ID
  getId: function() {
    return this.element.id;
  },

  // Detroys itself when closing
  setDestroyOnClose: function() {
    var destroyFunc = this.destroy.bind(this);
    if (this.options.hideEffectOptions.afterFinish) {
      var func = this.options.hideEffectOptions.afterFinish;
      this.options.hideEffectOptions.afterFinish = function() {func();destroyFunc() }
    }
    else
      this.options.hideEffectOptions.afterFinish = function() {destroyFunc() }
    this.destroyOnClose = true;
  },

  // initDrag event
  _initDrag: function(event) {
    // Get pointer X,Y
    this.pointer = [Event.pointerX(event), Event.pointerY(event)];

    if (this.options.wiredDrag)
      this.currentDrag = this._createWiredElement();
    else
      this.currentDrag = this.element;

    // Resize
    if (Event.element(event) == this.sizer) {
      this.doResize = true;
      this.widthOrg = this.width;
      this.heightOrg = this.height;
      this.bottomOrg = parseFloat(this.element.getStyle('bottom'));
      this.rightOrg = parseFloat(this.element.getStyle('right'));
      Windows.notify("onStartResize", this);
    }
    else {
      this.doResize = false;

      // Check if click on close button,
      var closeButton = $(this.getId() + '_close');
      if (closeButton && Position.within(closeButton, this.pointer[0], this.pointer[1])) {
        this.currentDrag = null;
        return;
      }

      this.toFront();

      if (! this.options.draggable)
        return;
      Windows.notify("onStartMove", this);
    }
    // Register global event to capture mouseUp and mouseMove
    Event.observe(document, "mouseup", this.eventMouseUp, false);
    Event.observe(document, "mousemove", this.eventMouseMove, false);

    // Add an invisible div to keep catching mouse event over iframes
    WindowUtilities.disableScreen('__invisible__', '__invisible__');

    // Stop selection while dragging
    document.body.ondrag = function () { return false; };
    document.body.onselectstart = function () { return false; };

    this.currentDrag.show();
    Event.stop(event);
  },

  // updateDrag event
  _updateDrag: function(event) {
     var pointer = [Event.pointerX(event), Event.pointerY(event)];
    var dx = pointer[0] - this.pointer[0];
    var dy = pointer[1] - this.pointer[1];

    // Resize case, update width/height
    if (this.doResize) {
      this.setSize(this.widthOrg + dx , this.heightOrg + dy);

      dx = this.width - this.widthOrg
      dy = this.height - this.heightOrg

      // Check if it's a right position, update it to keep upper-left corner at the same position
      if (! this.useLeft)
        this.currentDrag.setStyle({right: (this.rightOrg -dx) + 'px'});
      // Check if it's a bottom position, update it to keep upper-left corner at the same position
      if (! this.useTop)
        this.currentDrag.setStyle({bottom: (this.bottomOrg -dy) + 'px'});
    }
    // Move case, update top/left
    else {
      this.pointer = pointer;

      if (this.useLeft)
        this.currentDrag.setStyle({left: parseFloat(this.currentDrag.getStyle('left')) + dx + 'px'});
      else
        this.currentDrag.setStyle({right: parseFloat(this.currentDrag.getStyle('right')) - dx + 'px'});

      if (this.useTop)
        this.currentDrag.setStyle({top: parseFloat(this.currentDrag.getStyle('top')) + dy + 'px'});
      else
        this.currentDrag.setStyle({bottom: parseFloat(this.currentDrag.getStyle('bottom')) - dy + 'px'});
    }
    if (this.iefix)
      this._fixIEOverlapping();

    this._removeStoreLocation();
    Event.stop(event);
  },

   // endDrag callback
   _endDrag: function(event) {
    // Remove temporary div over iframes
     WindowUtilities.enableScreen('__invisible__');

    if (this.doResize)
      Windows.notify("onEndResize", this);
    else
      Windows.notify("onEndMove", this);

    // Release event observing
    Event.stopObserving(document, "mouseup", this.eventMouseUp,false);
    Event.stopObserving(document, "mousemove", this.eventMouseMove, false);

    Event.stop(event);

    this._hideWiredElement();

    // Store new location/size if need be
    this._saveCookie()

    // Restore selection
    document.body.ondrag = null;
    document.body.onselectstart = null;
  },

  // Creates HTML window code
  _createWindow: function(id) {
    var className = this.options.className;
    var win = document.createElement("div");
    win.setAttribute('id', id);
    win.className = "dialog";

    var content;
    if (this.options.url)
      content= "<iframe frameborder=\"0\" name=\"" + id + "_content\"  id=\"" + id + "_content\" src=\"" + this.options.url + "\"> </iframe>";
    else
      content ="<div id=\"" + id + "_content\" class=\"" +className + "_content\"> </div>";

    var closeDiv = this.options.closable ? "<div class='"+ className +"_close' id='"+ id +"_close' onclick='Windows.close(\""+ id +"\", event)'> </div>" : "";
    var minDiv = this.options.minimizable ? "<div class='"+ className + "_minimize' id='"+ id +"_minimize' onclick='Windows.minimize(\""+ id +"\", event)'> </div>" : "";
    var maxDiv = this.options.maximizable ? "<div class='"+ className + "_maximize' id='"+ id +"_maximize' onclick='Windows.maximize(\""+ id +"\", event)'> </div>" : "";
    var seAttributes = this.options.resizable ? "class='" + className + "_sizer' id='" + id + "_sizer'" : "class='"  + className + "_se'";

    win.innerHTML = closeDiv + minDiv + maxDiv + "\
      <table id='"+ id +"_row1' class=\"top table_window\">\
        <tr>\
          <td class='"+ className +"_nw'>&nbsp;</td>\
          <td class='"+ className +"_n'><div id='"+ id +"_top' class='"+ className +"_title title_window'>"+ this.options.title +"</div></td>\
          <td class='"+ className +"_ne'>&nbsp;</td>\
        </tr>\
      </table>\
      <table id='"+ id +"_row2' class=\"mid table_window\">\
        <tr>\
          <td class='"+ className +"_w'></td>\
            <td id='"+ id +"_table_content' class='"+ className +"_content' valign='top'>" + content + "</td>\
          <td class='"+ className +"_e'></td>\
        </tr>\
      </table>\
        <table id='"+ id +"_row3' class=\"bot table_window\">\
        <tr>\
          <td class='"+ className +"_sw'>&nbsp;</td>\
            <td class='"+ className +"_s'><div id='"+ id +"_bottom' class='status_bar'>&nbsp;</div></td>\
            <td " + seAttributes + ">&nbsp;</td>\
        </tr>\
      </table>\
    ";
    Element.hide(win);
    this.options.parent.insertBefore(win, this.options.parent.firstChild);
    Event.observe($(id + "_content"), "load", this.options.onload);
    return win;
  },

  // Sets window location
  setLocation: function(top, left) {
    if (top < 0)
      top = 0;
    if (left < 0)
      left= 0
    this.element.setStyle({top: top + 'px'});
    this.element.setStyle({left: left + 'px'});
    this.useLeft = true;
    this.useTop = true;
  },

  getLocation: function() {
    var location = {};
    if (this.useTop)
      location = Object.extend(location, {top: this.element.getStyle("top")});
    else
      location = Object.extend(location, {bottom: this.element.getStyle("bottom")});
    if (this.useLeft)
      location = Object.extend(location, {left: this.element.getStyle("left")});
    else
      location = Object.extend(location, {right: this.element.getStyle("right")});

    return location;
  },

  // Gets window size
  getSize: function() {
    return {width: this.width, height: this.height};
  },

  // Sets window size
  setSize: function(width, height) {
    width = parseFloat(width);
    height = parseFloat(height);

    // Check min and max size
    if (width < this.options.minWidth)
      width = this.options.minWidth;

    if (height < this.options.minHeight)
      height = this.options.minHeight;

    if (this.options. maxHeight && height > this.options. maxHeight)
      height = this.options. maxHeight;

    if (this.options. maxWidth && width > this.options. maxWidth)
      width = this.options. maxWidth;

    this.width = width;
    this.height = height;
    var e = this.currentDrag ? this.currentDrag : this.element;
    e.setStyle({width: width + this.widthW + this.widthE + "px"})
    e.setStyle({height: height  + this.heightN + this.heightS + "px"})

    // Update content height
    if (!this.currentDrag || this.currentDrag == this.element) {
      var content = $(this.element.id + '_content');
      content.setStyle({height: height  + 'px'});
      content.setStyle({width: width  + 'px'});
    }
  },

  updateHeight: function() {
    this.setSize(this.width, this.content.scrollHeight)
  },

  updateWidth: function() {
    this.setSize(this.content.scrollWidth, this.height)
  },

  // Brings window to front
  toFront: function() {
    if (Windows.focusedWindow == this)
      return;
    this.setZIndex(Windows.maxZIndex + 20);
    Windows.notify("onFocus", this);
  },

  // Displays window modal state or not
  show: function(modal) {
    if (modal) {
      Windows.addModalWindow(this);

      this.modal = true;
      this.setZIndex(Windows.maxZIndex + 20);
      Windows.unsetOverflow(this);
    }

    // To restore overflow if need be
    if (this.oldStyle)
      this.getContent().setStyle({overflow: this.oldStyle});

    if (! this.width || !this.height) {
      var size = WindowUtilities._computeSize(this.content.innerHTML, this.content.id, this.width, this.height, 0)
      if (this.height)
        this.width = size + 5
      else
        this.height = size + 5
    }

    this.setSize(this.width, this.height);
    if (this.centered)
      this._center(this.centerTop, this.centerLeft);

    if (this.options.showEffect != Element.show && this.options.showEffectOptions )
      this.options.showEffect(this.element, this.options.showEffectOptions);
    else
      this.options.showEffect(this.element);

    this._checkIEOverlapping();
    this.visible = true;
    WindowUtilities.focusedWindow = this
    Windows.notify("onShow", this);
  },

  // Displays window modal state or not at the center of the page
  showCenter: function(modal, top, left) {
    this.centered = true;
    this.centerTop = top;
    this.centerLeft = left;

    this.show(modal);
  },

  isVisible: function() {
    return this.visible;
  },

  _center: function(top, left) {
    var windowScroll = WindowUtilities.getWindowScroll();
    var pageSize = WindowUtilities.getPageSize();

    if (!top)
      top = (pageSize.windowHeight - (this.height + this.heightN + this.heightS))/2;
    top += windowScroll.top

    if (!left)
      left = (pageSize.windowWidth - (this.width + this.widthW + this.widthE))/2;
    left += windowScroll.left

    this.setLocation(top, left);
    this.toFront();
  },

  _recenter: function(event) {
    if (this.modal && this.centered) {
      var pageSize = WindowUtilities.getPageSize();
      // Check for this stupid IE that sends dumb events
      if (this.pageSize && this.pageSize.pageWidth == pageSize.windowWidth && this.pageSize.pageHeight == pageSize.windowHeight)
        return;

      this.pageSize = pageSize;
      // set height of Overlay to take up whole page and show
      if ($('overlay_modal')) {
        $('overlay_modal').style.height = (pageSize.pageHeight + 'px');
        $('overlay_modal').style.width = (pageSize.pageWidth + 'px');
      }
      if (this.options.recenterAuto)
        this._center(this.centerTop, this.centerLeft);
    }
  },

  // Hides window
  hide: function() {
    this.visible = false;
    if (this.modal) {
      Windows.removeModalWindow(this);
      Windows.resetOverflow();
    }
    // To avoid bug on scrolling bar
    this.oldStyle = this.getContent().getStyle('overflow') || "auto"
    this.getContent().setStyle({overflow: "hidden"});

    this.options.hideEffect(this.element, this.options.hideEffectOptions);

     if(this.iefix)
      this.iefix.hide();
    Windows.notify("onHide", this);
  },

  minimize: function() {
    var r2 = $(this.getId() + "_row2");
    var dh = r2.getDimensions().height;

    if (r2.visible()) {
      var h  = this.element.getHeight() - dh
      r2.hide()
      this.element.setStyle({height: h + "px"})
      if (! this.useTop) {
        var bottom = parseFloat(this.element.getStyle('bottom'));
        this.element.setStyle({bottom: (bottom + dh) + 'px'});
      }
    }
    else {
      var h  = this.element.getHeight() + dh;
      this.element.setStyle({height: h + "px"})
      if (! this.useTop) {
        var bottom = parseFloat(this.element.getStyle('bottom'));
        this.element.setStyle({bottom: (bottom - dh) + 'px'});
      }
      r2.show();

      this.toFront();
    }
    Windows.notify("onMinimize", this);

    // Store new location/size if need be
    this._saveCookie()
  },

  maximize: function() {
    if (this.storedLocation != null) {
      this._restoreLocation();
      if(this.iefix)
        this.iefix.hide();
    }
    else {
      this._storeLocation();
      Windows.unsetOverflow(this);

      var windowScroll = WindowUtilities.getWindowScroll();
      var pageSize = WindowUtilities.getPageSize();

      this.element.setStyle(this.useLeft ? {left: windowScroll.left} : {right: windowScroll.left});
      this.element.setStyle(this.useTop ? {top: windowScroll.top} : {bottom: windowScroll.top});

      this.setSize(pageSize.windowWidth - this.widthW - this.widthE, pageSize.windowHeight - this.heightN - this.heightS)
      this.toFront();
      if (this.iefix)
        this._fixIEOverlapping();
    }
    Windows.notify("onMaximize", this);

    // Store new location/size if need be
    this._saveCookie()
  },

  isMinimized: function() {
    var r2 = $(this.getId() + "_row2");
    return !r2.visible();
  },

  isMaximized: function() {
    return (this.storedLocation != null);
  },

  setOpacity: function(opacity) {
    if (Element.setOpacity)
      Element.setOpacity(this.element, opacity);
  },

  setZIndex: function(zindex) {
    this.element.setStyle({zIndex: zindex});
    Windows.updateZindex(zindex, this);
  },

  setTitle: function(newTitle) {
    if (!newTitle || newTitle == "")
      newTitle = "&nbsp;";

    Element.update(this.element.id + '_top', newTitle);
  },

  setStatusBar: function(element) {
    var statusBar = $(this.getId() + "_bottom");

    if (typeof(element) == "object") {
      if (this.bottombar.firstChild)
        this.bottombar.replaceChild(element, this.bottombar.firstChild);
      else
        this.bottombar.appendChild(element);
    }
    else
      this.bottombar.innerHTML = element;
  },

  _checkIEOverlapping: function() {
    if(!this.iefix && (navigator.appVersion.indexOf('MSIE')>0) && (navigator.userAgent.indexOf('Opera')<0) && (this.element.getStyle('')=='absolute')) {
        new Insertion.After(this.element.id, '<iframe id="' + this.element.id + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
        this.iefix = $(this.element.id+'_iefix');
    }
    if(this.iefix)
      setTimeout(this._fixIEOverlapping.bind(this), 50);
  },

  _fixIEOverlapping: function() {
      Position.clone(this.element, this.iefix);
      this.iefix.style.zIndex = this.element.style.zIndex - 1;
      this.iefix.show();
  },

  _getWindowBorderSize: function(event) {
    // Hack to get real window border size!!
    var div = this._createHiddenDiv(this.options.className + "_n")
    this.heightN = Element.getDimensions(div).height;
    div.parentNode.removeChild(div)

    var div = this._createHiddenDiv(this.options.className + "_s")
    this.heightS = Element.getDimensions(div).height;
    div.parentNode.removeChild(div)

    var div = this._createHiddenDiv(this.options.className + "_e")
    this.widthE = Element.getDimensions(div).width;
    div.parentNode.removeChild(div)

    var div = this._createHiddenDiv(this.options.className + "_w")
    this.widthW = Element.getDimensions(div).width;
    div.parentNode.removeChild(div);

    // Workaround for IE!!
    if (isIE) {
      this.heightS = $(this.getId() +"_row3").getDimensions().height;
      this.heightN = $(this.getId() +"_row1").getDimensions().height;
    }

    // Safari size fix
    if (/Konqueror|Safari|KHTML/.test(navigator.userAgent))
      this.setSize(this.width, this.height);
    if (this.doMaximize)
      this.maximize();
    if (this.doMinimize)
      this.minimize();
  },

  _createHiddenDiv: function(className) {
    var objBody = document.getElementsByTagName("body").item(0);
    var win = document.createElement("div");
    win.setAttribute('id', this.element.id+ "_tmp");
    win.className = className;
    win.style.display = 'none';
    win.innerHTML = '';
    objBody.insertBefore(win, objBody.firstChild);
    return win;
  },

  _storeLocation: function() {
    if (this.storedLocation == null) {
      this.storedLocation = {useTop: this.useTop, useLeft: this.useLeft,
                             top: this.element.getStyle('top'), bottom: this.element.getStyle('bottom'),
                             left: this.element.getStyle('left'), right: this.element.getStyle('right'),
                             width: this.width, height: this.height };
    }
  },

  _restoreLocation: function() {
    if (this.storedLocation != null) {
      this.useLeft = this.storedLocation.useLeft;
      this.useTop = this.storedLocation.useTop;

      this.element.setStyle(this.useLeft ? {left: this.storedLocation.left} : {right: this.storedLocation.right});
      this.element.setStyle(this.useTop ? {top: this.storedLocation.top} : {bottom: this.storedLocation.bottom});
      this.setSize(this.storedLocation.width, this.storedLocation.height);

      Windows.resetOverflow();
      this._removeStoreLocation();
    }
  },

  _removeStoreLocation: function() {
    this.storedLocation = null;
  },

  _saveCookie: function() {
    if (this.cookie) {
      var value = "";
      if (this.useLeft)
        value += "l:" +  (this.storedLocation ? this.storedLocation.left : this.element.getStyle('left'))
      else
        value += "r:" + (this.storedLocation ? this.storedLocation.right : this.element.getStyle('right'))
      if (this.useTop)
        value += ",t:" + (this.storedLocation ? this.storedLocation.top : this.element.getStyle('top'))
      else
        value += ",b:" + (this.storedLocation ? this.storedLocation.bottom :this.element.getStyle('bottom'))

      value += "," + (this.storedLocation ? this.storedLocation.width : this.width);
      value += "," + (this.storedLocation ? this.storedLocation.height : this.height);
      value += "," + this.isMinimized();
      value += "," + this.isMaximized();
      WindowUtilities.setCookie(value, this.cookie)
    }
  },

  _createWiredElement: function() {
    if (! this.wiredElement) {
      if (isIE)
        this._getWindowBorderSize();
      var div = document.createElement("div");
      div.className = "wired_frame " + this.options.className + "_wired_frame";

      //div.style.display = "none";
      div.style.position = 'absolute';
      document.body.insertBefore(div, document.body.firstChild);
      div = $(div);

      var dim = this.element.getDimensions();
      div.setStyle({width: dim.width + "px", height: dim.height +"px"});

      if (this.useLeft)
        div.setStyle({left: this.element.getStyle('left')});
      else
        div.setStyle({right: this.element.getStyle('right')});

      if (this.useTop)
        div.setStyle({top: this.element.getStyle('top')});
      else
        div.setStyle({bottom: this.element.getStyle('bottom')});
      this.wiredElement = div
    }
    this.wiredElement.setStyle({zIndex: Windows.maxZIndex+30});
    return this.wiredElement;
  },

  _hideWiredElement: function() {
    if (this.currentDrag == this.element)
      this.currentDrag = null;
    else {
      if (this.useLeft)
        this.element.setStyle({left: this.currentDrag.getStyle('left')});
      else
        this.element.setStyle({right: this.currentDrag.getStyle('right')});

      if (this.useTop)
        this.element.setStyle({top: this.currentDrag.getStyle('top')});
      else
        this.element.setStyle({bottom: this.currentDrag.getStyle('bottom')});

      this.currentDrag.hide();
      this.currentDrag = null;
      this.setSize(this.width, this.height);
    }
  }
};

// Windows containers, register all page windows
var Windows = {
  windows: [],
  modalWindows: [],
  observers: [],
  focusedWindow: null,
  maxZIndex: 0,

  addObserver: function(observer) {
    this.removeObserver(observer);
    this.observers.push(observer);
  },

  removeObserver: function(observer) {
    this.observers = this.observers.reject( function(o) { return o==observer });
  },

  notify: function(eventName, win) {  //  onStartResize(), onEndResize(), onStartMove(), onEndMove(), onClose(), onDestroy(), onMinimize(), onMaximize(), onHide(), onShow(), onFocus()
    this.observers.each( function(o) {if(o[eventName]) o[eventName](eventName, win);});
  },

  // Gets window from its id
  getWindow: function(id) {
    return this.windows.detect(function(d) { return d.getId() ==id });
  },

  // Gets the last focused window
  getFocusedWindow: function() {
    return this.focusedWindow;
  },

  // Registers a new window (called by Windows constructor)
  register: function(win) {
    this.windows.push(win);
  },

  // Add a modal window in the stack
  addModalWindow: function(win) {
    // Disable screen if first modal window
    if (this.modalWindows.length == 0)
      WindowUtilities.disableScreen(win.options.className, 'overlay_modal', win.getId());
    else {
      // Move overlay over all windows
      if (Window.keepMultiModalWindow) {
        $('overlay_modal').style.zIndex = Windows.maxZIndex + 20;
        Windows.maxZIndex += 20;
        WindowUtilities._hideSelect(this.modalWindows.last().getId());
      }
      // Hide current modal window
      else
        this.modalWindows.last().element.hide();
      // Fucking IE select issue
      WindowUtilities._showSelect(win.getId());
    }
    this.modalWindows.push(win);
  },

  removeModalWindow: function(win) {
    this.modalWindows.pop();

    // No more modal windows
    if (this.modalWindows.length == 0)
      WindowUtilities.enableScreen();
    else {
      if (Window.keepMultiModalWindow) {
        this.modalWindows.last().toFront();
        WindowUtilities._showSelect(this.modalWindows.last().getId());
      }
      else
        this.modalWindows.last().element.show();
    }
  },

  // Registers a new window (called by Windows constructor)
  register: function(win) {
    this.windows.push(win);
  },

  // Unregisters a window (called by Windows destructor)
  unregister: function(win) {
    this.windows = this.windows.reject(function(d) { return d==win });
  },

  // Closes a window with its id
  close: function(id, event) {
    var win = this.getWindow(id);
    // Asks delegate if exists
    if (win && win.visible) {
      if (win.getDelegate() && ! win.getDelegate().canClose(win))
        return;
      this.focusedWindow = this.windows.length >=2 ? this.windows[this.windows.length-2] : null;
      this.notify("onClose", win);
      win.hide();
    }
    if (event)
      Event.stop(event);
  },

  // Closes all windows
  closeAll: function() {
    this.windows.each( function(w) {Windows.close(w.getId())} );
  },

  closeAllModalWindows: function() {
    WindowUtilities.enableScreen();

    this.modalWindows.each( function(win) {win.hide()});
  },
  // Minimizes a window with its id
  minimize: function(id, event) {
    var win = this.getWindow(id)
    if (win && win.visible)
      win.minimize();
    Event.stop(event);
  },

  // Maximizes a window with its id
  maximize: function(id, event) {
    var win = this.getWindow(id)
    if (win && win.visible)
      win.maximize();
    Event.stop(event);
  },

  unsetOverflow: function(except) {
    this.windows.each(function(d) { d.oldOverflow = d.getContent().getStyle("overflow") || "auto" ; d.getContent().setStyle({overflow: "hidden"}) });
    if (except && except.oldOverflow)
      except.getContent().setStyle({overflow: except.oldOverflow});
  },

  resetOverflow: function() {
    this.windows.each(function(d) { if (d.oldOverflow) d.getContent().setStyle({overflow: d.oldOverflow}) });
  },

  updateZindex: function(zindex, win) {
    if (zindex > this.maxZIndex)
      this.maxZIndex = zindex;
    this.focusedWindow = win;
  }
};

var Dialog = {
  dialogId: null,
  onCompleteFunc: null,
  callFunc: null,
  parameters: null,

  confirm: function(content, parameters) {
    // Get Ajax return before
    if (typeof content != "string") {
      Dialog._runAjaxRequest(content, parameters, Dialog.confirm);
      return
    }

    parameters = parameters || {};
    var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";
    var cancelLabel = parameters.cancelLabel ? parameters.cancelLabel : "Cancel";

    var windowParam = parameters.windowParameters || {};
    windowParam.className = windowParam.className || "alert";

    var okButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " ok_button'"
    var cancelButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " cancel_button'"
    var content = "\
      <div class='" + windowParam.className + "_message'>" + content  + "</div>\
        <div class='" + windowParam.className + "_buttons'>\
          <form id='submitins' onsubmit='return miValidateFullInsuranceDetails()' methode='post' action='insurance.aspx'><input type='Submit' value='" + okLabel + "' onclick='Dialog.okCallback()'" + okButtonClass + "/>\
          <input type='button' value='" + cancelLabel + "' onclick='Dialog.cancelCallback()' " + cancelButtonClass + "/></form>\
        </div>\
    ";
    return this._openDialog(content, parameters)
  },

  alert: function(content, parameters) {
    // Get Ajax return before
    if (typeof content != "string") {
      Dialog._runAjaxRequest(content, parameters, Dialog.alert);
      return
    }

    parameters = parameters || {};
    var okLabel = parameters.okLabel ? parameters.okLabel : "Ok";

    var windowParam = parameters.windowParameters || {};
    windowParam.className = windowParam.className || "alert";

    var okButtonClass = "class ='" + (parameters.buttonClass ? parameters.buttonClass + " " : "") + " ok_button'"
    var content = "\
      <div class='" + windowParam.className + "_message'>" + content  + "</div>\
        <div class='" + windowParam.className + "_buttons'>\
          <input type='button' value='" + okLabel + "' onclick='Dialog.okCallback()'" + okButtonClass + "/>\
        </div>";
    return this._openDialog(content, parameters)
  },

  info: function(content, parameters) {
    // Get Ajax return before
    if (typeof content != "string") {
      Dialog._runAjaxRequest(content, parameters, Dialog.info);
      return
    }

    parameters = parameters || {};
    parameters.windowParameters = parameters.windowParameters || {};

    var className = parameters.windowParameters.className || "alert";

    var content = "<div id='modal_dialog_message' class='" + className + "_message'>" + content  + "</div>";
    if (parameters.showProgress)
      content += "<div id='modal_dialog_progress' class='" + className + "_progress'>  </div>";

    parameters.windowParameters.ok = null;
    parameters.windowParameters.cancel = null;
    parameters.windowParameters.className = className;

    return this._openDialog(content, parameters)
  },

  setInfoMessage: function(message) {
    $('modal_dialog_message').update(message);
  },

  closeInfo: function() {
    Windows.close(this.dialogId);
  },

  _openDialog: function(content, parameters) {
    if (! parameters.windowParameters.height && ! parameters.windowParameters.width) {
      parameters.windowParameters.width = WindowUtilities.getPageSize().pageWidth / 2;
    }
    if (parameters.id)
      this.dialogId = parameters.id;
    else {
      var t = new Date();
      this.dialogId = 'modal_dialog_' + t.getTime();
    }

    // compute height or width if need be
    if (! parameters.windowParameters.height || ! parameters.windowParameters.width) {
      var size = WindowUtilities._computeSize(content, this.dialogId, parameters.windowParameters.width, parameters.windowParameters.height)
      if (parameters.windowParameters.height)
        parameters.windowParameters.width = size + 5
      else
        parameters.windowParameters.height = size + 5
    }
    var windowParam = parameters && parameters.windowParameters ? parameters.windowParameters : {};
    windowParam.resizable = windowParam.resizable || false;

    windowParam.effectOptions = windowParam.effectOptions || {duration: 1};
    windowParam.minimizable = false;
    windowParam.maximizable = false;
    windowParam.closable = false;

    var win = new Window(this.dialogId, windowParam);
    win.getContent().innerHTML = content;
    win.showCenter(true, parameters.top, parameters.left);
    win.setDestroyOnClose();

    win.cancelCallback = parameters.cancel;
    win.okCallback = parameters.ok;

    return win;
  },

  _getAjaxContent: function(originalRequest)  {
      Dialog.callFunc(originalRequest.responseText, Dialog.parameters)
  },

  _runAjaxRequest: function(message, parameters, callFunc) {
    if (message.options == null)
      message.options ={}
    Dialog.onCompleteFunc = message.options.onComplete;
    Dialog.parameters = parameters;
    Dialog.callFunc = callFunc;

    message.options.onComplete = Dialog._getAjaxContent;
    new Ajax.Request(message.url, message.options);
  },

  okCallback: function() {
    var win = Windows.focusedWindow;
    if (!win.okCallback || win.okCallback(win)) {
      // Remove onclick on button
      $$("#" + win.getId()+" input").each(function(element) {element.onclick=null;})
      win.hide();
    }
  },

  cancelCallback: function() {
    var win = Windows.focusedWindow;
    // Remove onclick on button
    $$("#" + win.getId()+" input").each(function(element) {element.onclick=null})
    win.hide();
    if (win.cancelCallback)
      win.cancelCallback(win);
  }
}
/*
  Based on Lightbox JS: Fullsize Image Overlays
  by Lokesh Dhakar - http://www.huddletogether.com

  For more information on this script, visit:
  http://huddletogether.com/projects/lightbox/

  Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
  (basically, do anything you want, just leave my name and link)
*/

var isIE = navigator.appVersion.match(/MSIE/) == "MSIE";

var WindowUtilities = {
  // From script.aculo.us
  getWindowScroll: function() {
    var w = window;
      var T, L, W, H;
      with (w.document) {
        if (w.document.documentElement && documentElement.scrollTop) {
          T = documentElement.scrollTop;
          L = documentElement.scrollLeft;
        } else if (w.document.body) {
          T = body.scrollTop;
          L = body.scrollLeft;
        }
        if (w.innerWidth) {
          W = w.innerWidth;
          H = w.innerHeight;
        } else if (w.document.documentElement && documentElement.clientWidth) {
          W = documentElement.clientWidth;
          H = documentElement.clientHeight;
        } else {
          W = body.offsetWidth;
          H = body.offsetHeight
        }
      }
      return { top: T, left: L, width: W, height: H };

  },
  //
  // getPageSize()
  // Returns array with page width, height and window width, height
  // Core code from - quirksmode.org
  // Edit for Firefox by pHaez
  //
  getPageSize: function(){
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;

    if (self.innerHeight) {  // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }
    var pageHeight, pageWidth;

    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
      pageHeight = windowHeight;
    } else {
      pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){
      pageWidth = windowWidth;
    } else {
      pageWidth = xScroll;
    }

    return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
  },

   disableScreen: function(className, overlayId, contentId) {
    WindowUtilities.initLightbox(overlayId, className);
    var objBody = document.getElementsByTagName("body").item(0);

    // prep objects
     var objOverlay = $(overlayId);

    var pageSize = WindowUtilities.getPageSize();

    // Hide select boxes as they will 'peek' through the image in IE, store old value
    if (contentId && isIE) {
      WindowUtilities._hideSelect();
      WindowUtilities._showSelect(contentId);
    }

    // set height of Overlay to take up whole page and show
    objOverlay.style.height = (pageSize.pageHeight + 'px');
    objOverlay.style.width = (pageSize.windowWidth + 'px');
    objOverlay.style.display = 'block';
  },

   enableScreen: function(id) {
     id = id || 'overlay_modal';
     var objOverlay =  $(id);
    if (objOverlay) {
      // hide lightbox and overlay
      objOverlay.style.display = 'none';

      // make select boxes visible using old value
      if (id != "__invisible__")
        WindowUtilities._showSelect();

      objOverlay.parentNode.removeChild(objOverlay);
    }
  },

  _hideSelect: function(id) {
    if (isIE) {
      id = id ==  null ? "" : "#" + id + " ";
      $$(id + 'select').each(function(element) {
        if (! WindowUtilities.isDefined(element.oldVisibility)) {
          element.oldVisibility = element.style.visibility ? element.style.visibility : "visible";
          element.style.visibility = "hidden";
        }
      });
    }
  },

  _showSelect: function(id) {
    if (isIE) {
      id = id ==  null ? "" : "#" + id + " ";
      $$(id + 'select').each(function(element) {
        if (WindowUtilities.isDefined(element.oldVisibility)) {
          // Why?? Ask IE
          try {
            element.style.visibility = element.oldVisibility;
          } catch(e) {
            element.style.visibility = "visible";
          }
          element.oldVisibility = null;
        }
        else {
          if (element.style.visibility)
            element.style.visibility = "visible";
        }
      });
    }
  },

  isDefined: function(object) {
    return typeof(object) != "undefined" && object != null;
  },

  // initLightbox()
  // Function runs on window load, going through link tags looking for rel="lightbox".
  // These links receive onclick events that enable the lightbox display for their targets.
  // The function also inserts html markup at the top of the page which will be used as a
  // container for the overlay pattern and the inline image.
  initLightbox: function(id, className) {
    // Already done, just update zIndex
    if ($(id)) {
      Element.setStyle(id, {zIndex: Windows.maxZIndex + 10});
    }
    // create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
    else {
      var objBody = document.getElementsByTagName("body").item(0);
      var objOverlay = document.createElement("div");
      objOverlay.setAttribute('id', id);
      objOverlay.className = "overlay_" + className
      objOverlay.style.display = 'none';
      objOverlay.style.position = 'absolute';
      objOverlay.style.top = '0';
      objOverlay.style.left = '0';
      objOverlay.style.zIndex = Windows.maxZIndex + 10;
       objOverlay.style.width = '100%';
      objBody.insertBefore(objOverlay, objBody.firstChild);
    }
  },

  setCookie: function(value, parameters) {
    document.cookie= parameters[0] + "=" + escape(value) +
      ((parameters[1]) ? "; expires=" + parameters[1].toGMTString() : "") +
      ((parameters[2]) ? "; path=" + parameters[2] : "") +
      ((parameters[3]) ? "; domain=" + parameters[3] : "") +
      ((parameters[4]) ? "; secure" : "");
  },

  getCookie: function(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
    } else {
      begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
      end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
  },

  _computeSize: function(content, id, width, height, margin) {
    if (margin == null)
      margin = 5;

    var objBody = document.getElementsByTagName("body").item(0);
    var tmpObj = document.createElement("div");
    tmpObj.setAttribute('id', id);

    if (height)
      tmpObj.style.height = height + "px"
    else
      tmpObj.style.width = width + "px"

    tmpObj.style.position = 'absolute';
    tmpObj.style.top = '0';
    tmpObj.style.left = '0';
    tmpObj.style.display = 'none';

    tmpObj.innerHTML = content;
    objBody.insertBefore(tmpObj, objBody.firstChild);

    var size;
    if (height)
      size = $(id).getDimensions().width + margin;
    else
      size = $(id).getDimensions().height + margin;

    objBody.removeChild(tmpObj);
    return size;
  }
}
