//Starts the AJAX request. function doCalculation(calculationParameters) { var allsuspects=document.getElementsByTagName("script"); for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove if (allsuspects[i] && allsuspects[i].getAttribute("id")=="dynamicID") allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild() } var fileref=document.createElement('script'); fileref.setAttribute("type","text/javascript"); fileref.setAttribute("src", 'http://www.money.co.uk/php/calculator_do_calculation.php?calculatorID=1¶meters='+ calculationParameters); fileref.setAttribute("id","dynamicID"); if (typeof fileref!="undefined") document.getElementsByTagName("head")[0].appendChild(fileref) } warningtext = ""; function validateParams() { invalid_title=0; invalid_title_size=0; invalid_width=0; invalid_image_url=0; invalid_currency=0; invalid_intro_font_size=0; invalid_font=0; invalid_text_size=0; invalid_title_colour=0; invalid_text_colour=0; invalid_background_colour=0; invalid_border_fill_colour=0; invalid_outer_border_colour=0; invalid_inner_border_colour=0; invalid_border_width=0; if (typeof title=="undefined") { invalid_title=1; warningtext = warningtext + "There is a missing definition for title
"; } else { } if (invalid_title==1) {title = "HOW MUCH DOES YOUR PET COST YOU?"; } if (typeof title_size=="undefined") { invalid_title_size=1; warningtext = warningtext + "There is a missing definition for title_size
"; } else { title_size = parseInt(title_size); if( isNaN(title_size) ) { invalid_title_size=1; if(title_size != "") { warningtext = warningtext + "Invalid value given for title_size
"; } } } if (invalid_title_size==1) {title_size = "13"; } if (typeof width=="undefined") { invalid_width=1; warningtext = warningtext + "There is a missing definition for width
"; } else { width = parseInt(width); if( isNaN(width) ) { invalid_width=1; if(width != "") { warningtext = warningtext + "Invalid value given for width
"; } } width = Math.max(width,220); width = Math.min(width,500); } if (invalid_width==1) {width = "300"; } if (typeof image_url=="undefined") { invalid_image_url=1; warningtext = warningtext + "There is a missing definition for image_url
"; } else { var regexp=/^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/ if (regexp.test(image_url) == false) { invalid_image_url=1; if(image_url != "") { warningtext = warningtext + "Invalid URL supplied for image_url
"; } }} if (invalid_image_url==1) { if(typeof image_url!="undefined" && image_url == "") { invalid_image_url=0; } else { image_url = ""; } } if (typeof currency=="undefined") { invalid_currency=1; warningtext = warningtext + "There is a missing definition for currency
"; } else { } if (invalid_currency==1) {currency = "£"; } if (typeof intro_font_size=="undefined") { invalid_intro_font_size=1; warningtext = warningtext + "There is a missing definition for intro_font_size
"; } else { intro_font_size = parseInt(intro_font_size); if( isNaN(intro_font_size) ) { invalid_intro_font_size=1; if(intro_font_size != "") { warningtext = warningtext + "Invalid value given for intro_font_size
"; } } } if (invalid_intro_font_size==1) {intro_font_size = "13"; } if (typeof font=="undefined") { invalid_font=1; warningtext = warningtext + "There is a missing definition for font
"; } else { } if (invalid_font==1) { if(typeof font!="undefined" && font == "") { invalid_font=0; } else { font = ""; } } if (typeof text_size=="undefined") { invalid_text_size=1; warningtext = warningtext + "There is a missing definition for text_size
"; } else { text_size = parseInt(text_size); if( isNaN(text_size) ) { invalid_text_size=1; if(text_size != "") { warningtext = warningtext + "Invalid value given for text_size
"; } } } if (invalid_text_size==1) {text_size = "12"; } if (typeof title_colour=="undefined") { invalid_title_colour=1; warningtext = warningtext + "There is a missing definition for title_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(title_colour) == false) { invalid_title_colour=1; if(title_colour != "") { warningtext = warningtext + "Invalid value supplied for title_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_title_colour==1) {title_colour = "#000000"; } if (typeof text_colour=="undefined") { invalid_text_colour=1; warningtext = warningtext + "There is a missing definition for text_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(text_colour) == false) { invalid_text_colour=1; if(text_colour != "") { warningtext = warningtext + "Invalid value supplied for text_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_text_colour==1) {text_colour = "#000000"; } if (typeof background_colour=="undefined") { invalid_background_colour=1; warningtext = warningtext + "There is a missing definition for background_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(background_colour) == false) { invalid_background_colour=1; if(background_colour != "") { warningtext = warningtext + "Invalid value supplied for background_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_background_colour==1) {background_colour = "#E895D3"; } if (typeof border_fill_colour=="undefined") { invalid_border_fill_colour=1; warningtext = warningtext + "There is a missing definition for border_fill_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(border_fill_colour) == false) { invalid_border_fill_colour=1; if(border_fill_colour != "") { warningtext = warningtext + "Invalid value supplied for border_fill_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_border_fill_colour==1) {border_fill_colour = "#D94FB7"; } if (typeof outer_border_colour=="undefined") { invalid_outer_border_colour=1; warningtext = warningtext + "There is a missing definition for outer_border_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(outer_border_colour) == false) { invalid_outer_border_colour=1; if(outer_border_colour != "") { warningtext = warningtext + "Invalid value supplied for outer_border_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_outer_border_colour==1) {outer_border_colour = "#000000"; } if (typeof inner_border_colour=="undefined") { invalid_inner_border_colour=1; warningtext = warningtext + "There is a missing definition for inner_border_colour
"; } else { var regexp=/^#([0-9A-Fa-f]{6})$/ if (regexp.test(inner_border_colour) == false) { invalid_inner_border_colour=1; if(inner_border_colour != "") { warningtext = warningtext + "Invalid value supplied for inner_border_colour
Format should be \"#XXXXXX\"
"; } }} if (invalid_inner_border_colour==1) {inner_border_colour = "#000000"; } if (typeof border_width=="undefined") { invalid_border_width=1; warningtext = warningtext + "There is a missing definition for border_width
"; } else { if(border_width=="") { border_width=0; }border_width = parseInt(border_width); if( isNaN(border_width) ) { invalid_border_width=1; if(border_width != "") { warningtext = warningtext + "Invalid value given for border_width
"; } } border_width = Math.max(border_width,0); border_width = Math.min(border_width,20); } if (invalid_border_width==1) { if(typeof border_width!="undefined" && border_width == "") { invalid_border_width=0; } else { border_width = "5"; } } }function calculateCost() { //get the form values and convert to ints // Validate params // Set up parameters variable and serialise it. var price = parseInt(document.getElementById('petForm').price.value); var lifeSpanYears = parseInt(document.getElementById('petForm').lifeSpanYears.value); var foodWeek = parseInt(document.getElementById('petForm').foodWeek.value); var treatsYear = parseInt(document.getElementById('petForm').treatsYear.value); var stableChargesYear = parseInt(document.getElementById('petForm').stableChargesYear.value); var kennelYear = parseInt(document.getElementById('petForm').kennelYear.value); var vetsYear = parseInt(document.getElementById('petForm').vetsYear.value); var othersMonth = parseInt(document.getElementById('petForm').othersMonth.value); //Ensure all values default to 0, even if nothing entered if (isNaN(price)) { price = 0; } if (isNaN(lifeSpanYears)||lifeSpanYears==0) { lifeSpanYears = 1;//set to 1 as division by 0 is a no no. See calculation below } if (isNaN(foodWeek)) { foodWeek = 0; } if (isNaN(treatsYear)) { treatsYear = 0; } if (isNaN(stableChargesYear)) { stableChargesYear = 0; } if (isNaN(kennelYear)) { kennelYear = 0; } if (isNaN(vetsYear)) { vetsYear = 0; } if (isNaN(othersMonth)) { othersMonth = 0; } calculationParameters = '&price='+price+'&lifeSpanYears='+lifeSpanYears+'&foodWeek='+foodWeek+'&treatsYear='+treatsYear+'&stableChargesYear='+stableChargesYear+'&kennelYear='+kennelYear+'&vetsYear='+vetsYear+'&othersMonth='+othersMonth+'&lifeSpanYears='+lifeSpanYears; doCalculation(calculationParameters); } function customiseStyles() { // Take the variables and use them to override the CSS using javascript to make the alterations. // Ensure all (style) variables have something in.... // Manually entered by calculator designer. var oldBaseCssFile = document.getElementById("baseCssFile"); var oldBaseCssFileParent = oldBaseCssFile.parentNode // oldBaseCssFileParent.removeChild(oldBaseCssFile); oldBaseCssFileParent.appendChild(oldBaseCssFile); var oldStyle=document.getElementById("styleCustomisations"); if((oldStyle != null)) { oldStyle.parentNode.removeChild(oldStyle); } var stylecontent=document.createElement("style"); stylecontent.setAttribute("type", "text/css"); // Generate the CSS from the variables for the bits needed cssTextContents = "#calculatorContainer { color: "+text_colour+"; font-size: "+text_size+"px; }\n"; cssTextContents += "#calculatorContainer td { color: "+text_colour+"; font-size: "+text_size+"px; }\n"; if ( font != "" ) { cssTextContents += "#petCont p, #petCont h2, #petCont table .label { font-family: "+font+"; }\n"; } if(image_url != "") cssTextContents += "#petCont { background-image: url('"+image_url+"'); }\n"; else cssTextContents += "#petCont { background-image: none; }\n"; cssTextContents += "#petCont { background-color: "+background_colour+"; }\n"; cssTextContents += "#petCont INPUT.field { border: 1px solid "+text_colour+"; color: "+text_colour+"; }\n"; cssTextContents += "#petCont .legend { color: "+title_colour+"; font-size: "+title_size+"px; }\n"; border_width = parseInt(border_width); border_trim = border_width*2+2; if(border_width != '0') { border_inner_width = '2px'; modifier = 4; } else { border_inner_width = '0'; modifier = 0; } cssTextContents += "#petCont { border: "+border_inner_width+" solid "+inner_border_colour+"; }\n"; cssTextContents += "#petForm #introText { font-size: "+intro_font_size+"px; }\n"; cssTextContents += "#calculatorContainer { background-color: "+border_fill_colour+"; }\n"; cssTextContents += "#calculatorContainer { border:1px solid "+outer_border_colour+"; }\n"; cssTextContents += "#petCont { width: "+(width-border_trim-modifier)+"px; }\n"; cssTextContents += "#calculatorContainer { width: "+(width-border_trim)+"px; padding: "+border_width+"px; }\n"; // Finish generating the CSS if(stylecontent.styleSheet) { // IE bit stylecontent.styleSheet.cssText = cssTextContents; } else { stylecontent.innerHTML = cssTextContents; } stylecontent.setAttribute("id","styleCustomisations"); document.getElementsByTagName("head")[0].appendChild(stylecontent); } function displayCalculator() { // Displays the calculator complete with content. // Ensure all (content) variables have something in.... // This is where all validation of input for actual use in the calculator should happen // This is also where defaults should be set if the field contents are either invalid or empty. validateParams(); customiseStyles(); // Call the function to establish the CSS base. var calcStr = '
'; calcStr += '

'+title+'

'; calcStr += '

This calculator shows you the annual cost of caring for your four-legged friends.

'; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += ''; calcStr += '
Purchase price:'+currency+'
Life span: years
Food:'+currency+'a week
Equipment / treats:'+currency+'a year
Stable charges:'+currency+'a year
Vets\' bills / insurance premium:'+currency+'a year
Kennel/cattery fees:'+currency+'a year
Other services:
(eg Dog walking)
'+currency+'a month

Result

Annual bill:'+currency+'
Lifetime bill:'+currency+'
'; calcStr += '
'; document.getElementById('calc').innerHTML = calcStr; } function loadBaseStylesheet() { var fileref=document.createElement("link") fileref.setAttribute("rel", "stylesheet") fileref.setAttribute("type", "text/css") // fileref.setAttribute("href", "http://81.179.12.238/petCalc/main.css") Temporarily moving so I can track access fileref.setAttribute("href", "http://www.money.co.uk/css/main.css") fileref.setAttribute("id", "baseCssFile") document.getElementsByTagName("head")[0].appendChild(fileref) } loadBaseStylesheet(); displayCalculator();