<!--
var xmlHttp;
var toUpdate;
var rootpath;
var messages = new Array();
var secondAdultShown = false;
 
rootpath = "/services/quote/";

function updateIPT(str) {
	var url=rootpath + "remote.asp?query=products&ipt=" + str;
    getResponse(url, "product");
}

function getProducts() {
    var url=rootpath + "remote.asp?query=products"
    getResponse(url, "product");
}

function getCoverType(str) {
    var url=rootpath + "remote.asp?query=covertype&product_id=" + str;
    getResponse(url, "covertype");
}

function updateCoverType(str) {
    var url=rootpath + "remote.asp?query=update&covertype_id=" + str
    getResponse(url, null);
}

function updateQuoteDetails(field) {
    var url=rootpath + "remote.asp?query=update&"+ field +"="+ getValue(getElem(field));
    getResponse(url, null);
}

function getAges(str) {
    // Check that the user has selected the max trip duration.
    if (checkDuration()) {
        //var url=rootpath + "remote.asp?query=ageranges&duration=" + str
        //getResponse(url, "ageranges");
    }
}

function getQuote() {
    var destination = getValue(getElem('S2Destination'));
    var pricing_base_id = getValue(getElem('pricing_base_id'));
    var pricing_base_id_second_adult = getValue(getElem('pricing_base_id_second_adult'));
    var numberofchildren = getValue(getElem('numberofchildren'));

    if (getElem('pricing_base_id_second_adult') != null && getValue(getElem('pricing_base_id_second_adult')) == "") { return false; }

    var url=rootpath + "remote.asp?query=quote&S2Destination=" + destination
                     + "&pricing_base_id="+ (pricing_base_id ? pricing_base_id : '')
                     + "&numberofchildren="+ numberofchildren;
                     
    if (pricing_base_id_second_adult) url += "&pricing_base_id_second_adult="+ (pricing_base_id_second_adult ? pricing_base_id_second_adult : '');
                     
    // Now find the option that the user has selected.
    for (var i = 0; i < document.mainform.elements.length; i++ ){
        var ele = document.mainform.elements[i];

        if (ele.name && ele.name.indexOf('factor') == 0) {
            // Get add the whole element name and value onto the url as it gets parsed on the server.
            url += "&"+ ele.name +"="+ getValue(ele);
        }
    }
    getResponse(url, "quotes");
}

function updatePriceOnly() {
    var destination = getValue(getElem('S2Destination'));
    var pricing_base_id = getValue(getElem('pricing_base_id'));
    var pricing_base_id_second_adult = getValue(getElem('pricing_base_id_second_adult'));
    var numberofchildren = getValue(getElem('numberofchildren'));
    var covertype = getValue(getElem('covertype_ID'));
    
    var url=rootpath + "remote.asp?query=updatequote&S2Destination=" + destination
                     + "&pricing_base_id="+ (pricing_base_id ? pricing_base_id : '')
                     + "&numberofchildren="+ numberofchildren +"&covertype_ID="+ covertype;
    
    url += "&pricing_base_id_second_adult="+ (pricing_base_id_second_adult ? pricing_base_id_second_adult : '');
                     
    // Now find the option that the user has selected.
    for (var i = 0; i < document.mainform.elements.length; i++ ){
        var ele = document.mainform.elements[i];

        if (ele.name && ele.name.indexOf('factor') == 0) {
            // Get add the whole element name and value onto the url as it gets parsed on the server.
            url += "&"+ ele.name +"="+ getValue(ele);
        }
    }
    
    // Find amount owing.
    if (getElem('owing')) {
    	getResponse(rootpath + "remote.asp?query=owing", "owing");
    }
    
    getResponse(url, "newquotevalue");
}

function updateUpgradePrices() {
    var destination = getValue(getElem('S2Destination'));
    var pricing_base_id = getValue(getElem('pricing_base_id'));
    var pricing_base_id_second_adult = getValue(getElem('pricing_base_id_second_adult'));
    var numberofchildren = getValue(getElem('numberofchildren'));
    var covertype = getValue(getElem('covertype_ID'));
    
    var url=rootpath + "remote.asp?query=upgrade&S2Destination=" + destination
                     + "&pricing_base_id="+ (pricing_base_id ? pricing_base_id : '')
                     + "&numberofchildren="+ numberofchildren +"&covertype_ID="+ covertype;
    
    url += "&pricing_base_id_second_adult="+ (pricing_base_id_second_adult ? pricing_base_id_second_adult : '');
                   
    // Now find the option that the user has selected.
    for (var i = 0; i < document.mainform.elements.length; i++ ){
        var ele = document.mainform.elements[i];

        if (ele.name && ele.name.indexOf('factor') == 0) {
            // Get add the whole element name and value onto the url as it gets parsed on the server.
            url += "&"+ ele.name +"="+ getValue(ele);
        }
    }
    getResponse(url, "upgradedetails");
}

function getAdditionalParties() {
	var destination = getValue(getElem('S2Destination'));
    var pricing_base_id = getValue(getElem('pricing_base_id'));
    var pricing_base_id_second_adult = getValue(getElem('pricing_base_id_second_adult'));
    var numberofchildren = getValue(getElem('numberofchildren'));
    var covertype = getValue(getElem('covertype_ID'));
	// Try and update the price.
	updateUpgradePrices();
    showSecondAdult(covertype);
}

function showSecondAdult(covertype) {
    getResponse(rootpath + "remote.asp?query=secondadult&covertype_ID="+ covertype, "secondadult");
    alert('Please enter the name(s) and date of birth(s) of the parties that you would like to include in this policy.\n\nOnce you have added all of the details click on the \'Save your Upgrade\' button to save your changes AND to display the new quote value.')
	
	showElem(getElem('addParties'));
}

function updateQuote(obj, quoteID, productID, covertypeID, pricing_base_id, pricing_base_id_second_adult, tripLength, canSave) {
    var destination = getValue(getElem('S2Destination'));
    var numberofchildren = getValue(getElem('numberofchildren'));
    var discount = getValue(getElem('S5Discount'))
	
    var url=rootpath + "remote.asp?"+ (!canSave ? "dontsave=true&" : "") +"query=updatequote&S2Destination=" + destination
                     + "&product_id="+ productID
                     + "&covertype_id="+ covertypeID
                     + "&pricing_base_id="+ (pricing_base_id ? pricing_base_id : '')
                     + "&numberofchildren="+ numberofchildren;
                     
    if (pricing_base_id_second_adult) url += "&pricing_base_id_second_adult="+ (pricing_base_id_second_adult ? pricing_base_id_second_adult : '');
    
	url += "&"+ obj.name +"="+ escape(getValue(obj));
	
	for (x = 0; x < obj.form.elements.length; x++) {
    	e = obj.form.elements[x];
    	if (e.name && e.name.substring(0, 7) == "factor_") { url += "&"+ e.name +"="+ getValue(e); }
    }

    getResponse(url, "price_"+ productID);
}

function updateQuoteWithOptions(obj, quoteID, productID, covertypeID, pricing_base_id, pricing_base_id_second_adult, tripLength, canSave) {
    var destination = getValue(getElem('S2Destination'));
    var numberofchildren = getValue(getElem('numberofchildren'));
	
    var url=rootpath + "remote.asp?"+ (!canSave ? "dontsave=true&" : "") +"query=updatequote&S2Destination=" + destination
                     + "&product_id="+ productID
                     + "&covertype_id="+ covertypeID
                     + "&pricing_base_id="+ (pricing_base_id ? pricing_base_id : '')
                     + "&numberofchildren="+ numberofchildren;
                     
    if (pricing_base_id_second_adult) url += "&pricing_base_id_second_adult="+ (pricing_base_id_second_adult ? pricing_base_id_second_adult : '');
    
    // loop through all of the factors in this form and add to URL.
    for (x = 0; x < obj.form.elements.length; x++) {
    	e = obj.form.elements[x];
    	if (e.name && e.name.substring(0, 7) == "factor_") { url += "&"+ e.name +"="+ getValue(e); }
    }
    
    getResponse(url, "price_"+ productID);
}

function getSubSourceCodes(quoteID) {
	var sourceID = getValue(getElem('SourceCodeID'));

	if (sourceID.length > 0) {
		var url=rootpath + "remote.asp?query=subsourcecodes&SourceCodeID="+ sourceID;
		getResponse(url, "subsourcecodes");
	} else {
		getElem('subsourcecodes').innerHTML = '';
	}
}

function checkDate(obj) {
	var dob = new Date();
	dob = Date.parse(getValue(obj));
}

function updateNumberOfDependants() {
    var numberofchildren = getValue(getElem('numberofchildren'));
    var url=rootpath + "remote.asp?numberofchildren="+ numberofchildren;
                     
    getResponse(url, "");
}


function getElem(objName) {
    if (document.mainform[objName] != undefined) {
        return document.mainform[objName];
    } else if (document.getElementById(objName)) {
        return document.getElementById(objName);
    }
    return null;
}

function getElement(objName, formName) {
	if (document['mainform'][objName] != undefined) {
        return document['mainform'][objName];
    } else if (document['mainform'].getElementById(objName)) {
        return document['mainform'].getElementById(objName);
    }
    return null;
}

function getResponse(url, div) {
    var origClass = "";

    if (getElem(div) && getElem(div).className) { origClass = getElem(div).className; }
    
    // Append the quote ID to the url.
    url += '&quoteid='+ getElem('quoteid').value +'&affiliateid='+ getElem('affiliateid').value; 
	
    toUpdate = div;
    if (div != null && getElem(div)) { $(div).update("&nbsp;"); /*getElem(div).className = "loading";*/ }
    xmlHttp=GetXmlHttpObject(div, url, origClass);
}

function showElem(elem) {
    elem.className += (elem.tagName == "SPAN" ? ' show_inline' : ' show_block');
	elem = null;
}

function GetXmlHttpObject(elem, url, origClass)
{
    var objXmlHttp=null
    var handler = function() { 
        if (toUpdate != null && (objXmlHttp.readyState==4 || objXmlHttp.readyState=="complete")) {
            var response = objXmlHttp.responseText;
            try {
                var script = (response.indexOf("<script>")) ? response.substring(response.indexOf("<script>") + 8, response.indexOf("</script>")) : response;
                eval(script);
				
            } catch (err) {  }
            finally {
				if (elem != null) {
					getElem(elem).className = origClass;
    				$(elem).update(response);
					$(elem).show();
				}
            }
        }
    };
 
    try {
		if (navigator.userAgent.indexOf("MSIE")>=0)
        {
            var strName="Msxml2.XMLHTTP";
            if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
            {
                strName="Microsoft.XMLHTTP";
            }
            
            objXmlHttp = new ActiveXObject(strName);
            try
            {
                objXmlHttp.onreadystatechange=handler;
            }
            catch(e)
            {
                alert("Error. Scripting for ActiveX might be disabled");
                return;
            }
        } else {
        	objXmlHttp = new XMLHttpRequest();
			objXmlHttp.onload=handler;
        	objXmlHttp.onerror=handler;
		}
    }
    catch (e) { }
	
    objXmlHttp.open("GET", url, true);
    objXmlHttp.send(null);
} 

function checkDuration() {
    if (getElem('S2TripLength')[getElem('S2TripLength').selectedIndex].value == '') {
        addMessage("S2TripLength", "Please select a maximum duration.");
        return false;
    } else {
        if ( checkError(getElem('S2TripLength'))) {
            return true;
        }
    }
}

function checkDOB(obj) {
    return (isDate(obj.value));
}

function getBasePriceForAge(obj) {
    var url=rootpath + "remote.asp?query=getbaseprice&dob=" + obj.value
    getResponse(url, "pricing_base_id_second_adult");
}

function getType(obj) {
    var type;
    if (obj && obj.length > 0) type = obj[0].type;
    if (!type) type = obj ? obj.type : null;
    
    return type; 
}

function getValue(obj) {
    if (obj != null) {
        switch (getType(obj)) {
            case 'text': { return obj.value; } break;
            case 'radio': {
                var radioLength = obj.length;
                if(radioLength == undefined)
                    return (obj.checked) ? obj.value : '';
                for(var i = 0; i < radioLength; i++) {
                    if(obj[i].checked) {
                        return obj[i].value;
                    }
                }
            } break;
            case 'checkbox': {
                var radioLength = obj.length;
                if(radioLength == undefined)
                    return (obj.checked) ? obj.value : '';
                for(var i = 0; i < radioLength; i++) {
                    if(obj[i].checked) {
                        return obj[i].value;
                    }
                }
            } break;
            default : {
                if (obj && obj.selectedIndex) {
                    return obj[obj.selectedIndex].value;
                } else if (obj) {
                    return obj.value;
                }
            } break;
        }
    } else {
        return null;
    }
}

function checkError(obj) {
    if (!getValue(obj)) return false;
    var identifier = (getType(obj) == 'radio') ? obj.name : obj.id;
    //alert("Looking for '"+ identifier +"' in '"+ messages.join(',') +"'");
    
    if(messages.inArray(identifier) > -1) {
        messages.splice(messages.inArray(obj.id), 1);
        document.getElementById('messages').removeChild(document.getElementById('msg_'+ identifier));
        obj.className = '';
        
        if (messages.length == 0) { getElem('errors').className = 'hide'; }
    }
    
    return true;
}

function addMessage(elem, msg) {
    if (messages.inArray(elem) == -1) {
        messages[messages.length] = elem;
        getElem('messages').innerHTML += "<li id=\"msg_"+ elem +"\">"+ msg +"</li>";
		// - <a href=\"#\" onclick=\"giveFocus('"+ elem +"'); return false;\">show me</a>
        if (getElem(elem)) { getElem(elem).className = 'errHighlight'; }
        getElem('errors').className = 'show';
    }
}

function giveFocus(obj) {
    if (obj) { 
        var elem = getElem(obj);
        elem.focus();
        elem.select();
        scrollToElement(elem);
    }
}

function scrollToElement(theElement){
  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                                      
 window.scrollTo(selectedPosX,selectedPosY);
}

function getHelp(sHelpKey, sTitle, sBody, isAdmin, affiliateID, iQuoteID) {
	try {
		try {
			if (sBody.length == 0) { throw "empty" }
			$('helpContent').innerHTML = "<h4>"+ sTitle +"</h4><p>"+ unescape(sBody) +"</p>";
			if (!$('OverlayContainer').visible())Effect.Grow('OverlayContainer', {duration:0.9, direction:'bottom-right'}); 
			
		} catch (e) {
			$('helpContent').innerHTML = "<p>There is currently no help associated with this item</p>";
			if (!$('OverlayContainer').visible())Effect.Grow('OverlayContainer', {duration:0.8, direction:'bottom-right'}); 
		}
		
		if (isAdmin) {
			$('helpContent').innerHTML += "<p><a href=\"/services/callcentre/help/addupdate.asp?key="+ sHelpKey +"&affiliateid="+ affiliateID +"&quoteid="+ iQuoteID +"\">Click here to edit</a></p>";
			if (!$('OverlayContainer').visible())Effect.Grow('OverlayContainer', {duration:0.8, direction:'bottom-right'}); 
		}
		
		new Draggable($('helpArea'));
	}
	catch(e) {  }
	finally { return false; }
}

function closeModal() {
Effect.Shrink('OverlayContainer', {duration:1, direction:'top-left'}); 
}

function hideHelp() {
	//var helpArea = getElem('helparea');
	//helpArea.className = "hide";
	Effect.Shrink('helpContent');
}

function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
        return false;
    }

    month = matchArray[3]; // p@rse date into variables
    day = matchArray[1];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }
    
    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }
    
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn`t have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn`t have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

Array.prototype.inArray = function (value) {
    var i;
    for (i=0; i < this.length; i++) {
        if (this[i] === value) {
            return i;
        }
    }
    return -1;
};
-->