// IMT namespace PHP GENERATE VERSION
var InsureMyTrip = InsureMyTrip || {};
// QuoteForm object
InsureMyTrip.QuoteForm = function(settings)
{
// Properties
var self = this;
var server_now;
var renderContainer = settings['containerId'];
var fields = settings['fields'];
var styles = settings['styles'];
var isFormValid = true;
var is_not_empty_regex = /\S/;
var is_currency_regex = /^(?:\$\s*)?(?:(?:\d{0,3}(?:[, ]\d{0,3})*[, ])+\d{3}|\d+)(?:\.\d*)?$/;
var is_valid_age_regex = /^(<1|\d\d|\d)$/;
// Setup fields and features
this.init = function(){
this.initToolTips();
// Bind validation triggers
self.initializeValidation();
$("#imt-quoteform").bind("submit", function() {
$('.compact-quoteform__tooltip').tooltipster('hide');
return self.validate();
});
// Bind trigger to display more traveler age fields
$('#add-more-travelers').click(function() { $('#extra-age-boxes').show('fast'); $('#add-more-travelers').hide(); });
// Bind field title popups
$('a.planinfo-form-link').click(function() { return self.displayPopup(this.href); });
var curDate = new Date();
server_now = new Date(curDate.getFullYear(), curDate.getMonth(), curDate.getDate());
// Apply jquery datepicker to departure date field
$(".date-input").datepicker({
changeMonth: true,
changeYear: true,
showOtherMonths:true,
selectOtherMonths:true,
minDate: server_now,
onSelect: function(dateText, inst) { $(this).blur(); }
});
// Remove empty anchor target
$(".ui-datepicker a").removeAttr("href");
// If style overrides have been specified, apply them
if (styles)
{
self.setStyles();
}
//Accept filters to be passed to the quote form from settings on init.
//presetFilters is a hidden GET input in quoteform PHP file.
if(settings['filters']){
$("#quote\\[presetFilters\\]").val(settings['filters']);
}else{
$("#quote\\[presetFilters\\]").remove();
}
};
//Tooltipster's Tooltips
this.initToolTips = function(){
var tooltipPosition = 'left';
var tooltipTrigger = 'click';
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
if(settings['tooltipPosition'])
{
tooltipPosition = settings['tooltipPosition'];
}
if(settings['tooltipTrigger'])
{
tooltipTrigger = settings['tooltipPosition'];
}
if(tooltipPosition == 'right'){
$('#form-field-date-departure').removeClass('compact-quoteform__tooltip')
.addClass('compact-quoteform__tooltip--offset');
$('#form-field-date-return').removeClass('compact-quoteform__tooltip--offset')
.addClass('compact-quoteform__tooltip');
}
var defaultToolTipSettings = {
maxWidth:470,
theme:'.tooltipster-dark',
position: tooltipPosition,
animation: 'grow',
delay: 150,
touchDevices: false,
trigger: tooltipTrigger,
hideOnClick: true
};
// items in right hand column: tool tips are cover the other fields of forms
var offsetToolTipSettings = JSON.parse(JSON.stringify( defaultToolTipSettings ) );
offsetToolTipSettings['position'] = 'top';
// firefox looses focus drop downs, when tool tips are active, use hover state for Firefox indead.
if(is_firefox)
{
defaultToolTipSettings['trigger'] = 'hover';
defaultToolTipSettings['delay'] = '0';
defaultToolTipSettings['animation'] = 'fade';
offsetToolTipSettings['trigger'] = 'hover';
offsetToolTipSettings['delay'] = '0';
offsetToolTipSettings['animation'] = 'fade';
}
//Close tooltips when you click outside tooltip area.
$('body').click(function(){
$('.compact-quoteform__tooltip').tooltipster('hide');
// $('.compact-quoteform__tooltip--offset').tooltipster('hide');
});
$('div.compact-quoteform__tooltip').click(function(e){
e.stopPropagation();
});
// Initialize tooltips
$(document).ready(function() {
$('.compact-quoteform__tooltip').tooltipster(defaultToolTipSettings);
$('.compact-quoteform__tooltip--offset').tooltipster(offsetToolTipSettings);
});
};
// Display field name popups
this.displayPopup = function(href)
{
var WIDTH = 600;
var HEIGHT = 250;
window.open( href, 'popup', 'toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,width='+WIDTH+',height='+HEIGHT+',left=20,top=20');
return false;
}
// Display inline error messages
this.displayError = function(errorDiv, errorText)
{
$(errorDiv).fadeIn("normal");
$(errorDiv).html(errorText);
isFormValid = false;
}
// Parses dates to four digits
this.parseDate = function(dateString)
{
var date = new Date(dateString);
// Adjust dates in the 1900s into the 2000s, as JS assumes 2 digit dates to be 1900s
if (date.getFullYear() < 2000)
{
date.setFullYear(date.getFullYear() + 100);
}
return date;
}
// Use AJAX to retrieve and render form structure
this.render = function()
{
jQuery.ajax(
{
type: "POST",
url: "https://www.insuremytrip.com/api/quoteform/quoteform.jsonp",
async: true,
dataType: "jsonp",
data: settings,
success: function(data)
{
// Populate container with returned content
$('#' + renderContainer).html(data);
// Initialize various fields and settings
self.init();
},
error: function(requestObj, statusText, errorThrown)
{
$('#' + renderContainer).html("The InsureMyTrip quote form was unable to load.");
}
});
};
// Add css files
this.getCSS = function()
{
var cssFile;
cssFile = "https://www.insuremytrip.com/global/css/quoteform/quoteform.css";
(document.createStyleSheet) ? document.createStyleSheet(cssFile) : $('').appendTo('head');
cssFile = "https://www.insuremytrip.com/global/css/smoothness/jquery-ui-1.8.2.custom.css";
(document.createStyleSheet) ? document.createStyleSheet(cssFile) : $('').appendTo('head');
cssFile = "https://www.insuremytrip.com/global/css/tooltipster/tooltipster.css";
(document.createStyleSheet) ? document.createStyleSheet(cssFile) : $('').appendTo('head');
cssFile = "https://www.insuremytrip.com/global/css/tooltipster/tooltipster-light.css";
(document.createStyleSheet) ? document.createStyleSheet(cssFile) : $('').appendTo('head');
cssFile = "https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css";
(document.createStyleSheet) ? document.createStyleSheet(cssFile) : $('').appendTo('head');
};
// Override default styles
this.setStyles = function()
{
// All allowed selectors and properties.
// Users will set styles by specifying selectors and property-value overrides.
// We abstract everything for the customer.
// They don't need to know the actual HTML structure, nor real CSS properties.
var refSelectors = {
"form" : {
"selector" : "#imt-quoteform",
"properties" : {
"font-color" : "color",
"border-color" : "border-color",
"font" : "font-family"
}
},
"header" : {
"selector" : "#imt-quoteform h3",
"properties" : {
"font-color" : "color",
"font" : "font-family"
}
},
"border" : {
"selector" : "#imt-quoteform",
"properties" : {
"color" : "border-color",
"width" : "border-width"
}
},
"fields" : {
"selector" : ".planinfo-form-link",
"properties" : {
"font-color" : "color",
"font" : "font-family"
}
},
"errors" : {
"selector" : ".imt-error",
"properties" : {
"font-color" : "color",
"font" : "font-family"
}
},
"button" : {
"selector" : "#imt-submit",
"properties" : {
"font-color" : "color",
"background" : "background"
}
}
};
// Loop through the list of provided style overrides,
// Apply those that match our allowed selectors/properties
$.each(styles, function(inSelector, inStyle) {
// Check if provided selector is in list of allowed selectors
if (refSelectors.hasOwnProperty(inSelector)) {
// Save REAL selector for specified FAKE selector
var refSelector = refSelectors[inSelector]['selector']
// Save REAL properties for specified FAKE selector
var refProperties = refSelectors[inSelector]['properties'];
// Loop through each provided property-value combo for provided selector
$.each(inStyle, function(inProperty, inValue) {
// Check if provided property is in list of allowed properties
if (refProperties.hasOwnProperty(inProperty)) {
// Ovveride property with new value
$(refSelector).css(refProperties[inProperty], inValue);
}
});
}
});
};
// Checks validity by forces each field to (if invalid) display an error
this.validate = function()
{
isFormValid = true;
$("input").blur();
$("select").blur();
//very hacky way to show a loading screen but because safari doesn't execute javascript in order and it stops all rendering and execution on submit this is the only way to get the screen to show on safari
if(isFormValid && !$('#loadingBox').is(":visible")){
$('#loadingBox').fadeIn( function(){$('#imt-quoteform').submit()});
return false;
}else{
if(isFormValid){
var json = jQuery.parseJSON('{"25031":{"button":"https:\/\/www.insuremytrip.com?linkId=25031&utm_campaign=Marketing&utm_source=Cruise%20Radio%20Widget&utm_medium=Sponsorship&utm_content=See%20Plans%20and%20Prices%20Button","logo":"https:\/\/www.insuremytrip.com?linkId=25031&utm_campaign=Marketing&utm_source=Cruise%20Radio%20Widget&utm_medium=Sponsorship&utm_content=InsureMyTrip%20Logo"}}');
if(json[settings['linkId']]['button'] != 'undefined'){
jQuery.get(json[settings['linkId']]['button'],function() {
}).done(function(){ return isFormValid;});
}
}else{
return isFormValid;
}
}
}
this.addCommas = function (nStr)
{
nStr += '';
nStr = nStr.replace(',','');
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
// Binds validation and error displaying to fields
this.initializeValidation = function()
{
// Validate TRIP COST if displayed
if ($("div#tripCost").length > 0)
{
//Format number like this: 2,343 or 234,324
$("#trip-cost").keyup(function() {
$("#trip-cost").val(self.addCommas($("#trip-cost").val()));
});
$("#trip-cost").on( "blur", function() {
if ((String(this.value).search(is_not_empty_regex) != -1) && (String(this.value).search(is_currency_regex) == -1)) {
self.displayError("#error-trip-cost", "Enter a valid trip cost.");
return;
}
$("#error-trip-cost").fadeOut("fast");
$(".quoteRequestTripCost").blur();
});
var numTravelers = 0;
$(".quoteRequestTripCost").on( "blur", function() {
numTravelers = 0;
$("input[name^='quote[TravelerAges]']").each( function() {
if ( (String(this.value).search(is_valid_age_regex) != -1) ) numTravelers++;
});
if ( $("#trip-cost").val() != 0 && numTravelers > 1 &&
(($("input[name='quote[TripCost][radio]']:checked").val() != 'combined' ) &&
($("input[name='quote[TripCost][radio]']:checked").val() != 'perPerson' )) ) {
self.displayError("#error-quoteRequestTripCost", "Select whether the trip cost is for all travelers or for each traveler.");
return;
}
$("#error-quoteRequestTripCost").fadeOut("fast");
});
}
// Validate DESTINATION if displayed
if ($("div#destination").length > 0)
{
$("#planinfo-destination").on( "blur", function() {
if (String(this.value).search(is_not_empty_regex) == -1) {
self.displayError("#error-destination", "Select a Primary Destination.");
return;
}
if (this.value == 192) {
self.displayError("#error-destination", "We are not allowed to provide insurance for travelers whose primary destination is Cuba. Select another destination.");
return;
}
$("#error-destination").fadeOut("fast");
});
}
// Validate TRIP DATES if displayed
var returnDateField = $('.js-compact-quoteform__return-date');
var dapartureDateField = $('.js-compact-quoteform__departure-date');
if ($("div#tripDates").length > 0)
{
$(dapartureDateField).on("blur", function() {
var input_date = self.parseDate(this.value);
if (isNaN(input_date.valueOf()) || (String(this.value).search(is_not_empty_regex) == -1)) {
self.displayError("#error-depart-date", "Enter the Departure Date in mm/dd/yyyy or m/d/yyyy format.");
return;
}
if (input_date.valueOf() < server_now.valueOf()) {
self.displayError("#error-depart-date", "The Departure Date cannot be earlier than today's date.");
return;
}
$("#error-depart-date").fadeOut("fast");
if ($(returnDateField).val() != "" && $(returnDateField ).val() != "mm/dd/yyyy")
$(returnDateField).blur();
});
$(returnDateField).on( "blur", function() {
var input_date = self.parseDate(this.value);
if (isNaN(input_date.valueOf()) || (String(this.value).search(is_not_empty_regex) == -1)) {
self.displayError("#error-return-date", "Enter the Return Date in mm/dd/yyyy or m/d/yyyy format.");
return;
}
var depart_date = self.parseDate($(dapartureDateField).val());
if (!isNaN(depart_date.valueOf())) {
if (input_date.valueOf() < depart_date.valueOf()) {
self.displayError("#error-return-date", "The Return Date cannot be earlier than the Departure Date.");
return;
}
}
if (input_date.valueOf() < server_now.valueOf()) {
self.displayError("#error-return-date", "The Return Date cannot be earlier than today's date.");
return;
}
$("#error-return-date").fadeOut("fast");
});
}
// Validate TRAVELER AGES if displayed
if ($("div#travelerAges").length > 0)
{
$(".age").on( "blur", function() {
var found = false;
var invalid = false;
$("input[name^='quote[travelerAges]']").each( function() {
if (String(this.value).search(is_not_empty_regex) != -1)
{
found = true;
if (String(this.value).search(is_valid_age_regex) == -1)
{
invalid = true;
}
}
});
if (!found)
{
self.displayError("#error-traveler-ages", "Enter at least one Traveler Age.");
return;
}
if (invalid)
{
self.displayError("#error-traveler-ages", "Enter Traveler Ages as numbers. For children less than one year, enter \"0\".");
return;
}
$("#error-traveler-ages").fadeOut("fast");
$(".quoteRequestTripCost").blur();
});
}
// Validate RESIDENCE if displayed
if ($("div#residence").length > 0)
{
$("#planinfo-residence").on( "blur", function() {
if (String(this.value).search(is_not_empty_regex) == -1) {
self.displayError("#error-residence", "Select a Residence.");
return;
}
else if($("#planinfo-residence").val()=="QC")
{
self.displayError("#error-residence", "IMT is not licensed to sell to residents of Quebec.");
return;
}
$("#error-residence").fadeOut("fast");
});
}
$("#planinfo-citizenship").on( "blur", function() {
if (String(this.value).search(is_not_empty_regex) == -1) {
self.displayError("#error-citizenship", "Select a Citizenship.");
return;
}
$("#error-citizenship").fadeOut("fast");
});
};
// Run!
self.getCSS();
self.render();
};