Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2009
    Location
    UK
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Array Problem..... will not calculate quotation

    i have a problem with the arrays...... i have entered all the relevant arrays to calculate a list of cheap quotations but when clicking on the submit button it doesnt do nothing..... when clicking on the submit button i want it to bring up the list of quotations based on the data entered in the form...... if i havent explained well maybe the coding below will clarify things i hope so


    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
         <head>
         <script type="text/javascript">
        
    	 function testResult(form) {     // calling function to check fields //
    
         //************starting arrays*****************//
    	 
    	 var companyName = new Array ("Kestrel Moon", "BB", "Whatever You Drive", "Discount Drivers", "Indirect Curve", "Swindel",
    	 			      "Captain", "Saintly Drivers", "Ipswich Union", "Young Drivers", "Atlee", "Good Deal", "Nice Price",
    	 			      "Insure For Less", "Instant Insure");
    	 
    	 var basicPrice = new Array (1165, 1231, 1052, 1667, 804, 1010, 510, 890, 490, 803, 923, 1690, 775, 1435, 1223);
    	 
    	 var olderdriversDisc = new Array (475, 380, 210, 310, 599, 210, 100, 0, 0, 0, 200, 300, 0, 0, 424);
    	 
    	 var femdriversDisc = new Array (0, 89, 34, 291, 0, 187, 0, 0, 0, 203, 107, 434, 0, 998, 43);
    	 
    	 var lowmileDisc = new Array (97, 43, 23, 302, 45, 35, 19, 0, 0, 78, 81, 112, 487, 49, 113);
    	 
    	 var extraDiscount = new Array (0, 180, 450, 189, 0, 32, 0, 0, 0, 0, 92, 493, 0, 0, 68);
    	 
    	 var noclaimsDisc = new Array (98, 85, 43, 93, 0, 103, 40, 155, 0, 82, 70, 47, 30, 59, 96);
    	 
    	 var quotes = new Array();
    	 
    	 //**************end arrays****************//
     
    	 var testField = form.txtfirst.value;
    	 var testField = form.txtsurname.value;
    	 var testField = form.txtage.value;
    	 var age = parseInt(form.txtage.value);
    	 var sex = form.sex.value;
    	 var annual = form.annual.value;
    	 var NCD = form.NCD.value;
    	 
    	 validateForm();
    	 
    	 //*********calculating quotes***********//
    
    	 for (var i=0;i<=14;i++){
    	
    	 quotes[i] = basicPrice[i];
    	 if (age >= 25){
         quotes[i] =  quotes[i] - olderdriversDisc[i];
    	}
    	if (sex == "op3"){                                               
    	 	quotes[i] = quotes[i] - femdriversDisc[i];                    
    	 }  
    	if (annual == "op4"){                                               
    	 	quotes[i] = quotes[i] - lowmileDisc[i];   
    	  } 
    	  
    	   if (age >= 25 && Sex == "op3" && lowmileDisc == "op4"){      
    	 	quotes[i] = quotes[i] - extraDiscount[i];                     
    	 }
    	 
    	 if (noclaimsDisc > 0){                                               
    	 	quotes[i] = quotes[i] - (noclaimsDisc[i]* NCD);
     	}
    
     	};                                            
    
    
    	for(var i=0;i<=14;i++){
    		if (i==0){
    			results ="The quote for"+companyName[i]+" is "+quotes[i]+"<br/>";
    		}else{
    		results = results+"The quote for"+companyName[i]+" is "+quotes[i]+"<br/>";
      }
    };
       
    	//calling function to display result
    	DisplayResult(results);
    	
    	for (var i=0; i<=14; i++){
    		document.write(companyName[i]+quotes[i]);
    	};
     //*********end calculations*********//
    	 
     
     	 function validateForm() {
     
    	 if (testField == "") {
    		 window.alert("Please fill in the required fields *");
    		 form.txtfirst.focus();
    		 form.txtsurname.focus();
    		 form.txtage.focus();
    		 return false;
     	 }
    }
     /*
     //*********function to display results*********
     
     function DisplayResult(content) {
    	 
    var win = window.open("", "win", "width=500,height=500"); // a window object
    var doc = win.document;
    doc.open("text/html", "replace");
    //****************************************************
    doc.write(
    	'<html><head><title>Quote</title><SCRIPT LANGUAGE=JavaScript>'
    	+'function butClose_onclick(){window.close()}'
      	+'<' + '/SCRIPT>'	
    	+'</head>'
       	+'<body bgcolor=white onLoad="self.focus()">'
       	+'<p><h1 color: #660000 >Your Car Insurance Quote</h1></p>'
       +content
    	+'<br><br><form><input type="button" value="Close" name=butClose onclick=butClose_onclick()>		</form>'
       +'</body></html>'
    );
    //********************************************
    doc.close();
    }
    //***************************************
    return true;*/
    };
    
    	 </script>
    	 </head>
    	 <body>
    	 <table> 
    	 <form name="myform" action="" method="get">
    	 
         <title> Car Insurance Quotation </title>
              
        <h1> Iqbal's Car Insurance Quotes </h1>
        
             
        <h2>Please enter your details and click on the 'Calculate' button to see your quotations</h2>
    <br/>          
       
    <tr>
    	<td>First Name:*</td>
    	<td><input type="text" name="txtfirst"/></td>
    </tr>
    
    <tr>
    	<td>Surname:*</td>
    	<td><input type="text" name="txtsurname"/></td>
    </tr>
    
    <tr>
    	<td>Age:*</td>
    	
    	<td><input type="text" name="txtage"/></td>
    </tr>
    
    <tr>
    <td>Sex:</td>
    
    	<td><select name="sex">
    	<option value='op1'>Male</option>
    	<option value='op3'>Female</option>
    </select></td>
    </tr>
    
    <tr>
    	<td>Annual Mileage:*</td>
    <td><select name="annual">
    	<option value='op4'>--Please Select--</option>
    	<option value='op5'>Less than 10,000</option>
    	<option value='op6'>More than 10,000</option>
    </select></td>
    </tr>
    
    <tr>
    <td>Number of Years of NCD:*</td>
    
    <td><select name="NCD">
    <option value='0'>0</option>
    	<option value='op7'>1 Year</option>
    	<option value='op8'>2 Years</option>
    	<option value='op9'>3 Years</option>
    	<option value='op10'>4 Years</option>
    	<option value='op11'>5 Years</option>
    </select></td>
    </tr>
    
    
    <tr>
    <td><input type="submit"  value="Calculate" onclick="testResult(this.form)">
    <input type="reset" value="Reset"></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    i hope this helps you outin trying to give me advice or help its driving me insane lol

  • #2
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    I typed this up for you. Style/tweak it however you like, but it's a bit more correct of coding:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/" />
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // <![CDATA[
    
    // Company class
    function Company(cname,basic_price,old_drivers_disc,fem_drivers_disc,lowmile_disc,extra_disc,noclaims_disc)
    	{
    	this.cname = cname;
    	this.basic_price = basic_price;
    	this.old_drivers_disc = old_drivers_disc;
    	this.fem_drivers_disc = fem_drivers_disc;
    	this.lowmile_disc = lowmile_disc;
    	this.extra_disc = extra_disc;
    	this.noclaims_disc = noclaims_disc;
    	}
    
    function Person(first_name,sur_name,age,sex,annual,NCD)
    	{
    	this.first_name = first_name || "";
    	this.sur_name = sur_name || "";
    	this.sex = sex || "";
    	this.age = isNaN(age) ? 0 : age;
    	this.annual = isNaN(annual) ? 0 : annual;
    	this.NCD = isNaN(NCD) ? 0 : NCD;
    	}
    
    function calculateQuotes(companies_list,for_person)
    	{
    	for_person.quotes = new Array();
    	var p = for_person;
    	for (var i=0;i<companies_list.length;i++)
    		{
    		var c = companies_list[i];
    		var quote = c.basic_price;
    		if (p.age >= 25) quote -= c.old_drivers_disc;
    		if (p.sex == "f") quote -= c.fem_drivers_disc;
    		if (p.annual <= 20000) quote -= c.lowmile_disc;
    		if (p.NCD) quote -= c.noclaims_disc*p.NCD;
    		for_person.quotes.push({company:c,quote:quote});
    		}
    	displayQuotes(p);
    	}
    
    function displayQuotes(for_person)
    	{
    	var html_to_add = "";
    	for (var i=0;i<for_person.quotes.length;i++)
    		{
    		html_to_add += "The quote for "+for_person.quotes[i].company.cname+" is "+for_person.quotes[i].quote+"<br />";
    		}
    	document.getElementById("output").innerHTML = html_to_add;
    	}
    
    function getQuote(form_element)
    	{
    	var ok = true;
    	var fn = form_element.first_name.value;
    	var ln = form_element.sur_name.value;
    	var age = form_element.age.value || "";
    	var annual = form_element.annual.value || "";
    	var NCD = form_element.NCD.value || "";
    	var sex = (form_element.sex.value || "").toLowerCase();
    	sex = (sex.indexOf("m") != -1 && sex.indexOf("female") == -1) ? "m" : "f";
    	form_element.sex.value = sex;
    
    	if (!fn || !ln || !sex) ok = false;
    	if (isNaN(age) || isNaN(annual) || isNaN(NCD)) ok = false;
    
    	if (!ok) alert("Please correctly fill in the required fields.");
    	else calculateQuotes(companies,new Person(fn,ln,age,sex,annual,NCD));
    
    	return false;
    	}
    
    window.onload = function()
    	{
    	companies = new Array();
    	companies.push(new Company("Kestrel Moon",1165,475,0,97,0,98));
    	companies.push(new Company("BB",1231,380,89,43,180,85));
    	companies.push(new Company("Whatever You Drive",1052,210,34,23,450,43));
    	companies.push(new Company("Discount Drivers",1667,310,291,302,189,93));
    	companies.push(new Company("Indirect Curve",804,599,0,45,0,0));
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <form onsubmit="return getQuote(this)">
    	<input type="text" name="first_name" value="bob" /><br />
    	<input type="text" name="sur_name" value="dylan" /><br />
    	<input type="text" name="age" maxlength="3" value = "27" /><br />
    	<input type="text" name="sex" value="m" /><br />
    	<input type="text" name="annual" value="4000" /><br />
    	<input type="text" name="NCD" value="5" /><br />
    	<input type="submit" value="get quote" />
    </form>
    
    <br />
    <div id="output"></div>
    
    </body>
    </html>
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #3
    New Coder
    Join Date
    Mar 2009
    Location
    UK
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by itsallkizza View Post
    I typed this up for you. Style/tweak it however you like, but it's a bit more correct of coding:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title></title>
    <link rel="stylesheet" type="text/css" href="css/" />
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // <![CDATA[
    
    // Company class
    function Company(cname,basic_price,old_drivers_disc,fem_drivers_disc,lowmile_disc,extra_disc,noclaims_disc)
    	{
    	this.cname = cname;
    	this.basic_price = basic_price;
    	this.old_drivers_disc = old_drivers_disc;
    	this.fem_drivers_disc = fem_drivers_disc;
    	this.lowmile_disc = lowmile_disc;
    	this.extra_disc = extra_disc;
    	this.noclaims_disc = noclaims_disc;
    	}
    
    function Person(first_name,sur_name,age,sex,annual,NCD)
    	{
    	this.first_name = first_name || "";
    	this.sur_name = sur_name || "";
    	this.sex = sex || "";
    	this.age = isNaN(age) ? 0 : age;
    	this.annual = isNaN(annual) ? 0 : annual;
    	this.NCD = isNaN(NCD) ? 0 : NCD;
    	}
    
    function calculateQuotes(companies_list,for_person)
    	{
    	for_person.quotes = new Array();
    	var p = for_person;
    	for (var i=0;i<companies_list.length;i++)
    		{
    		var c = companies_list[i];
    		var quote = c.basic_price;
    		if (p.age >= 25) quote -= c.old_drivers_disc;
    		if (p.sex == "f") quote -= c.fem_drivers_disc;
    		if (p.annual <= 20000) quote -= c.lowmile_disc;
    		if (p.NCD) quote -= c.noclaims_disc*p.NCD;
    		for_person.quotes.push({company:c,quote:quote});
    		}
    	displayQuotes(p);
    	}
    
    function displayQuotes(for_person)
    	{
    	var html_to_add = "";
    	for (var i=0;i<for_person.quotes.length;i++)
    		{
    		html_to_add += "The quote for "+for_person.quotes[i].company.cname+" is "+for_person.quotes[i].quote+"<br />";
    		}
    	document.getElementById("output").innerHTML = html_to_add;
    	}
    
    function getQuote(form_element)
    	{
    	var ok = true;
    	var fn = form_element.first_name.value;
    	var ln = form_element.sur_name.value;
    	var age = form_element.age.value || "";
    	var annual = form_element.annual.value || "";
    	var NCD = form_element.NCD.value || "";
    	var sex = (form_element.sex.value || "").toLowerCase();
    	sex = (sex.indexOf("m") != -1 && sex.indexOf("female") == -1) ? "m" : "f";
    	form_element.sex.value = sex;
    
    	if (!fn || !ln || !sex) ok = false;
    	if (isNaN(age) || isNaN(annual) || isNaN(NCD)) ok = false;
    
    	if (!ok) alert("Please correctly fill in the required fields.");
    	else calculateQuotes(companies,new Person(fn,ln,age,sex,annual,NCD));
    
    	return false;
    	}
    
    window.onload = function()
    	{
    	companies = new Array();
    	companies.push(new Company("Kestrel Moon",1165,475,0,97,0,98));
    	companies.push(new Company("BB",1231,380,89,43,180,85));
    	companies.push(new Company("Whatever You Drive",1052,210,34,23,450,43));
    	companies.push(new Company("Discount Drivers",1667,310,291,302,189,93));
    	companies.push(new Company("Indirect Curve",804,599,0,45,0,0));
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <form onsubmit="return getQuote(this)">
    	<input type="text" name="first_name" value="bob" /><br />
    	<input type="text" name="sur_name" value="dylan" /><br />
    	<input type="text" name="age" maxlength="3" value = "27" /><br />
    	<input type="text" name="sex" value="m" /><br />
    	<input type="text" name="annual" value="4000" /><br />
    	<input type="text" name="NCD" value="5" /><br />
    	<input type="submit" value="get quote" />
    </form>
    
    <br />
    <div id="output"></div>
    
    </body>
    </html>
    how would i tweak the code above and add it in this html coding dont want nothing changed (coding) but want it tweaked so it works within the existing coding below

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    		  <title> Car Insurance Form</title>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
              <script type="text/javascript">
          
    	  var companyName = ["Kestrel Moon","BB", "Whatever You Drive", "Discount Drivers", 
    	  "Indirect Curve", "Swindel", "Captain", "Saintly Drivers","Ipswich Union", "Young Drivers", 
    	  "Atlee", "Good Deal", "Nice Price", "Insure For Lees", "Instant Insure"]
    	  var basicPrice = [1165,1231,1052,1667,804,1010,510,890,490,803,923,1690,775,1435,1223];
    	  var olderdriversDisc = [475,380,210,310,599,100,0,0,0,200,300,0,0,424];
    	  var experiencedDisc = [475,380,210];
    	  var femaleDisc = [0,89, 34,291,0,187,0,0,203,107,434,0,998,43];
    	  var lowMilesDisc = [97,43,23,302,45,35,19,0,0,78,81,112,487,49,113];
    	  var extraDisc = [0,180,450,189,0,32,0,0,0,92,493,0,0,68];
    	  var noClaimsDisc = [98,85,43,93,0,103,40,155,0,82,70,47,30,59,96];	
    
    	  function toAscending(a,b){
    
    		 return a-b;
    	  }
    
              function validate(nForm){
    
    		var nTitle = nForm['title'];
    		var nFirstName = nForm['firstname'];
    		var nLastName = nForm['lastname'];
    		var nSex = nForm['sex'];
    		var nCol = nForm['col'];
    		var nTitle3 = nForm['title3'];
    		var nTitle4 = nForm['title4'];
    
    		var femaleFactor = 0;
    		var experiencedFactor = 0;
    		var lowMilesFactor = 0;
    		var extraFactor = 0;
    		var noClaimsFactor = 0;
    
    		if (nTitle.selectedIndex == 0)
    			{
    			 alert('Choose your title');
    			 return false;
    			}
    		if (nFirstName.value.replace(/\s/g, "").length < 1)
    			{
    			 alert('Enter your first name');	
    			 return false;
    			}
    		if (nLastName.value.replace(/\s/g, "").length < 1)
    			{
    			 alert('Enter your last name');	
    			 return false;
    			}
    		if (!nSex[0].checked && !nSex[1].checked)
    			{
    			 alert('Male or Female?');
    			 return false;
    			}
    		if (!nCol[0].checked && !nCol[1].checked)
    			{
    			 alert('Choose your age group');
    			 return false;
    			}
    		if (nTitle3.selectedIndex == 0)
    			{
    			 alert('Select the number of previous claims');
    			 return false;
    			}
    		if (nTitle4.selectedIndex == 0)
    			{
    			 alert('Estimate your annual mileage');
    			 return false;
    			}
    	
    		if (nSex[1].checked)
    			{
    			 femaleFactor = 1;
    			}
    		if (nCol[1].checked)
    			{
    			 experiencedFactor = 1;
    			}
    		if (nTitle4.selectedIndex == 1)
    			{
    			 lowMilesFactor = 1;
    			}
    		noClaimsFactor = (nTitle3.selectedIndex * 1) - 1;
    
    		var resultsSorted = [];
    		var results = [];
    		var computedPrice = "";
    		
    		for (i=0; i<companyName.length; i++)
    			{
    			 computedPrice = basicPrice[i]
    			 - (experiencedDisc[i] * experiencedFactor)
    			 - (femaleDisc[i] * femaleFactor)
    			 - (lowMilesDisc[i] * lowMilesFactor)
    			 - extraDisc[i]	 
    			 - noClaimsDisc[i] * noClaimsFactor;
    
    			 resultsSorted[resultsSorted.length] = computedPrice;
    			 results[results.length] = computedPrice;
    			}
    
    		resultsSorted.sort(toAscending);
    
    		var company = ""		
    
    		for (i=0; i<results.length; i++)
    			{
    			 if (resultsSorted[0] == results[i])
    				{
    				 company = companyName[i];
    				}
    			}
    	
    		alert("Best rate:\n" + company + ": " + resultsSorted[0]) 
    		return false;
    		
              }         
    
              </script> 
    </script>
    
         </head>
    
         <body>
         <form action="" method="post" onsubmit="return validate(this)">
         <fieldset>
         <legend>Form</legend>
         <table>
         <tr>
         <td>Title</td>
         <td>
         <select class="select1col" name="title">
             <option>
             Please select
             </option>
             <option value="MR">
                    Mr
             </option>
             <option value="MRS">
                    Mrs
             </option>
             <option value="MISS">
                    Miss
             </option>
             <option value="MS">
                    Ms
             </option>
             <option value="DR">
                    Dr
             </option>
             <option value="REV">
                    Reverend
             </option>
             <option value="SIR">
                    Sir
             </option>
             </select>
             </td>
             </tr>
             <tr>
             <td>First Name</td>
             <td>
             <input type="text" name="firstname">
             </td>
             </tr>
             <tr>
             <td>Last Name</td>
             <td>
             <input type="text" name="lastname">
             </td>
             </tr>
             <tr>
             <td></td>
             </tr>
             <tr>
             <td>Sex</td>
             <td colspan="2" class="center">
             <table border="0" cellspacing="0" cellpadding="0" align="left">
             <tr>
             <td>Male</td>
             <td>
             <input type="radio" name="sex" value="M">&nbsp;
             </td>
             <td>Female</td>
             <td>
             <input type="radio" name="sex" value="F">
             </td>
             </tr>
             </table>
             </td>
             </tr>
             <tr>
             <td>Age</td>
             </tr>
             <tr>
             <td>18-24</td>
             <td>
             <input type="radio" name="col" value="18-24">
             </td>
             </tr>
             <tr>
             <td>25 &amp; Over</td>
             <td>
             <input type="radio" name="col" value="25-34">
             </td>
             </tr>
             </table>
             <hr>
             Car Details
             <hr>
             <table>
             <tr>
             <td>No of No Claims</td>
             <td>
             <select class="select1col" name="title3">
             <option value="">
             Please select
             </option>
             <option>
                1
             </option>
             <option>
                2
             </option>
             <option>
                3
             </option>
             <option>
                4
             </option>
             <option>
                5+
             </option>
             </select>
             </td>
             </tr>
             <tr>
             <td>Estimated Annual Mileage</td>
             <td colspan="2" class="center">
             <select class="select1col" name="title4">
             <option value="">
             Estimated Annual Mileage
             </option>
             <option>
             3000 or less
             </option>
             <option>
             5000 or more
             </option>
             </select>
             </td>
             </tr>
             </table>
    <input type="submit" name="submit" value="Submit" class="submitBtn">
    </fieldset>
    </form>
    
    </body>
    
    </html>
    it would be grateful if you could do this for me and another thing i want the form validated so all details have to be entered thank you

  • #4
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    I went ahead and inserted the rest of the companies for you because I'm nice, but the rest is up to you. Anyone with basic javascript knowledge and a little interest in the project would be able to style/tweak the shell I created to do what they want. Seriously, spend 20 minutes on this and you'll be done:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Car Insurance Form</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
    // <![CDATA[
    
    // Company class
    function Company(cname,basic_price,old_drivers_disc,fem_drivers_disc,lowmile_disc,extra_disc,noclaims_disc)
    	{
    	this.cname = cname;
    	this.basic_price = basic_price;
    	this.old_drivers_disc = old_drivers_disc;
    	this.fem_drivers_disc = fem_drivers_disc;
    	this.lowmile_disc = lowmile_disc;
    	this.extra_disc = extra_disc;
    	this.noclaims_disc = noclaims_disc;
    	}
    
    function Person(first_name,sur_name,age,sex,annual,NCD)
    	{
    	this.first_name = first_name || "";
    	this.sur_name = sur_name || "";
    	this.sex = sex || "";
    	this.age = isNaN(age) ? 0 : age;
    	this.annual = isNaN(annual) ? 0 : annual;
    	this.NCD = isNaN(NCD) ? 0 : NCD;
    	}
    
    function calculateQuotes(companies_list,for_person)
    	{
    	for_person.quotes = new Array();
    	var p = for_person;
    	for (var i=0;i<companies_list.length;i++)
    		{
    		var c = companies_list[i];
    		var quote = c.basic_price;
    		if (p.age >= 25) quote -= c.old_drivers_disc;
    		if (p.sex == "f") quote -= c.fem_drivers_disc;
    		if (p.annual <= 20000) quote -= c.lowmile_disc;
    		if (p.NCD) quote -= c.noclaims_disc*p.NCD;
    		for_person.quotes.push({company:c,quote:quote});
    		}
    	displayQuotes(p);
    	}
    
    function displayQuotes(for_person)
    	{
    	var html_to_add = "";
    	var best_rate_index = 0;
    	for (var i=0;i<for_person.quotes.length;i++)
    		{
    		if (for_person.quotes[i].quote < for_person.quotes[best_rate_index].quote) best_rate_index = i;
    		html_to_add += "The quote for "+for_person.quotes[i].company.cname+" is $"+for_person.quotes[i].quote+"<br />";
    		}
    	html_to_add += "<br />Best Rate: "+for_person.quotes[best_rate_index].company.cname+" at $"+for_person.quotes[best_rate_index].quote;
    	document.getElementById("output").innerHTML = html_to_add;
    	}
    
    function getQuote(form_element)
    	{
    	var ok = true;
    	var fn = form_element.first_name.value;
    	var ln = form_element.sur_name.value;
    	var age = form_element.age.value || "";
    	var annual = form_element.annual.value || "";
    	var NCD = form_element.NCD.value || "";
    	var sex = (form_element.sex.value || "").toLowerCase();
    	sex = (sex.indexOf("m") != -1 && sex.indexOf("female") == -1) ? "m" : "f";
    	form_element.sex.value = sex;
    
    	if (!fn || !ln || !sex) ok = false;
    	if (isNaN(age) || isNaN(annual) || isNaN(NCD)) ok = false;
    
    	if (!ok) alert("Please correctly fill in the required fields.");
    	else calculateQuotes(companies,new Person(fn,ln,age,sex,annual,NCD));
    
    	return false;
    	}
    
    window.onload = function()
    	{
    	// set up each company here, easier to organize
    	// format: new Company(cname,basic_price,old_drivers_disc,fem_drivers_disc,lowmile_disc,extra_disc,noclaims_disc)
    	companies = new Array();
    	companies.push(new Company("Kestrel Moon",1165,475,0,97,0,98));
    	companies.push(new Company("BB",1231,380,89,43,180,85));
    	companies.push(new Company("Whatever You Drive",1052,210,34,23,450,43));
    	companies.push(new Company("Discount Drivers",1667,310,291,302,189,93));
    	companies.push(new Company("Indirect Curve",804,599,0,45,0,0));
    	companies.push(new Company("Swindel",1010,100,187,35,32,103));
    	companies.push(new Company("Captain",510,0,0,19,0,40));
    	companies.push(new Company("Saintly Drivers",890,0,0,0,0,155));
    	companies.push(new Company("Ipswich Union",490,0,203,0,0,0));
    	companies.push(new Company("Young Drivers",803,200,107,78,92,82));
    	companies.push(new Company("Atlee",923,300,434,81,493,70));
    	companies.push(new Company("Good Deal",1690,0,0,112,0,47));
    	companies.push(new Company("Nice Price",775,0,998,487,0,30));
    	companies.push(new Company("Insure For Lees",1435,424,43,49,68,59));
    	companies.push(new Company("Instant Insure",1223,0,0,113,0,96));
    	}
    
    // ]]>
    </script>
    </head>
    <body>
    
    <form onsubmit="return getQuote(this)">
    	<input type="text" name="first_name" value="bob" /><br />
    	<input type="text" name="sur_name" value="dylan" /><br />
    	<input type="text" name="age" maxlength="3" value = "27" /><br />
    	<input type="text" name="sex" value="m" /><br />
    	<input type="text" name="annual" value="4000" /><br />
    	<input type="text" name="NCD" value="5" /><br />
    	<input type="submit" value="get quote" />
    </form>
    
    <br />
    <div id="output"></div>
    
    </body>
    </html>
    At second glance I'm not sure exactly what you're missing. I gave you more than a framework, I actually built out most of the pieces for you. Other than perhaps tweaking the form, what else do you expect it to do?
    Last edited by itsallkizza; 03-24-2009 at 08:14 PM.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com

  • #5
    New Coder
    Join Date
    Mar 2009
    Location
    UK
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts
    the coding below is a completed form and the coding that you kindly did for me i want the outcome to look like the form below..... the layout and css used i want you to make it look like that on the coding you have typed/tweaked up for me if you could kindly help me out???? hope you understand basically i want it to look professional the form lol


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    		  <title> Car Insurance Form</title>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
              <script type="text/javascript">
    
              function validate(nForm){
    
    		var nTitle = nForm['title'];
    		var nFirstName = nForm['firstname'];
    		var nLastName = nForm['lastname'];
    		var nSex = nForm['sex'];
    		var nCol = nForm['col'];
    		var nTitle3 = nForm['title3'];
    		var nTitle4 = nForm['title4'];
    		if (nTitle.selectedIndex == 0)
    			{
    			 alert('Choose your title');
    			 return false;
    			}
    		if (nFirstName.value.replace(/\s/g, "").length < 1)
    			{
    			 alert('Enter your first name');	
    			 return false;
    			}
    		if (nLastName.value.replace(/\s/g, "").length < 1)
    			{
    			 alert('Enter your last name');	
    			 return false;
    			}
    		if (nSex[0].checked == false && nSex[1].checked == false)
    			{
    			 alert('Male or Female?');
    			 return false;
    			}
    		if (nCol[0].checked == false && nCol[1].checked == false)
    			{
    			 alert('Choose your age group');
    			 return false;
    			}
    		if (nTitle3.selectedIndex == 0)
    			{
    			 alert('Select the number of previous claims');
    			 return false;
    			}
    		if (nTitle4.selectedIndex == 0)
    			{
    			 alert('Estimate your annual mileage');
    			 return false;
    			}
    		alert('Thank you for your submission')
    		return true;
              }         
    
    </script>
    <style type="text/css">
    
              body {background-color: #fffacd; margin-top: 60px;}
              form {width: 620px; margin: auto; font-family: 'times new roman'; font-size: 12pt;}
              fieldset {width: 620px; padding-left: 10px; background-color: #eee8aa; border: 1px solid #e6b280;}
              legend {font-family: arial; font-size: 14pt; color: #000000; background-color: transparent; padding-top: 3px;
              padding-left: 3px; padding-right: 3px; margin-bottom: 5px;}
              .submitBtn {background-color: #fff8dc; border: 1px solid #000000; font-family: arial; font-size: 10pt;
              font-weight: bold; cursor: pointer; display: block; margin-left: auto; margin-right: auto; 
              margin-top: 5px; margin-bottom: 5px;}
    
              </style>
         </head>
    
         <body>
    
              <form action="" method="post" onsubmit="return validate(this)">
                   <fieldset>
                        <legend>Form</legend>
                        <table>
                             <tr>
                                  <td>
                                       Title
                                  </td>
                                  <td>
    
                                       <select class="select1col" name="title">
                                            <option>
                                                 Please select
                                            </option>
                                            <option value="MR">
                                                 Mr
                                            </option>
                                            <option value="MRS">
                                                 Mrs
                                            </option>
    
                                            <option value="MIS">
                                                 Miss
                                            </option>
                                            <option value="MS">
                                                 Ms
                                            </option>
                                            <option value="DR">
                                                 Dr
                                            </option>
                                            <option value="REV">
    
                                                 Reverend
                                            </option>
                                            <option value="SIR">
                                                 Sir
                                            </option>
                                       </select>
                                  </td>
                             </tr>
                             <tr>
                                  <td>
    
                                       First Name
                                  </td>
                                  <td>
                                       <input type="text" name="firstname">
                                  </td>
                             </tr>
                             <tr>
                                  <td>
                                       Last Name
                                  </td>
    
                                  <td>
                                       <input type="text" name="lastname">
                                  </td>
                             </tr>
                             <tr>
                                  <td></td>
                             </tr>
                             <tr>
                                  <td>
    
                                       Sex
                                  </td>
                                  <td colspan="2" class="center">
                                       <table border="0" cellspacing="0" cellpadding="0" align="left">
                                            <tr>
                                                 <td>
                                                      Male
                                                 </td>
                                                 <td>
                                                      <input type="radio" name="sex" value="M">&nbsp;
    
                                                 </td>
                                                 <td>
                                                      Female
                                                 </td>
                                                 <td>
                                                      <input type="radio" name="sex" value="F">
                                                 </td>
                                            </tr>
                                       </table>
    
                                  </td>
                             </tr>
                             <tr>
                                  <td>
                                       Age
                                  </td>
                             </tr>
                             <tr>
                                  <td>
    
                                       18-24
                                  </td>
                                  <td>
                                       <input type="radio" name="col" value="18-24">
                                  </td>
                             </tr>
                             <tr>
                                  <td>
                                       25 &amp; Over
                                  </td>
    
                                  <td>
                                       <input type="radio" name="col" value="25-34">
                                  </td>
                             </tr>
                        </table>
                        <hr>
                        Car Details
                        <hr>
                        <table>
    
                             <tr>
                                  <td>
                                       No of No Claims
                                  </td>
                                  <td>
                                       <select class="select1col" name="title3">
                                            <option value="">
                                                 Please select
                                            </option>
                                            <option>
    
                                                 1
                                            </option>
                                            <option>
                                                 2
                                            </option>
                                            <option>
                                                 3
                                            </option>
                                            <option>
                                                 4
                                            </option>
    
                                            <option>
                                                 5+
                                            </option>
                                       </select>
                                  </td>
                             </tr>
                             <tr>
                                  <td>
                                       Estimated Annual Mileage
                                  </td>
    
                                  <td colspan="2" class="center">
                                       <select class="select1col" name="title4">
                                            <option value="">
                                                 Please select
                                            </option>
                                            <option>
                                                 3000 or less
                                            </option>
                                            <option>
                                                 5000 or more
                                            </option>
    
                                       </select>
                                  </td>
                             </tr>
                             </table>
    			 <input type="submit" name="submit" value="Submit" class="submitBtn">
                   </fieldset>
              </form>
         </body>
    </html>

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    See this other thread:
    help with javascript insurance calculator

    itsallkizza really shouldn't have written as much code for you as he did. The fact that now you are asking somebody to make student homework look professional strikes me as the worst kind of cheating. You better hope your professor doesn't catch you!

  • Users who have thanked Old Pedant for this post:

    itsallkizza (03-25-2009)

  • #7
    New Coder
    Join Date
    Mar 2009
    Location
    Barnsley, UK
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Hmmm... two guys in the same class as me?

    I think so... I also have no idea what I'm doing on this but what I do know, nothing in the examples given by these professionals looks anything like what we're being taught. Theres all new techniques in there. If you use that, he's gonna ask you how you got to that stage, I wouldn't advise it.

    Really I know the assignment will be possible, just go through all the work and lectures etc. (Which is what I'm doing with no luck).


    Now,

    Could any of you guys re-work the code so that it only contains simple arrays, variables and loops? Since this is all we have been taught, surely we can't achieve what we don't yet know? I'm personally only looking to view a working example so that I can get an idea of how the damn thing works.

  • #8
    Senior Coder
    Join Date
    Oct 2008
    Location
    Long Beach
    Posts
    1,196
    Thanks
    36
    Thanked 164 Times in 164 Posts
    You can do anything with strings, numbers and the browser's native functions. That said there are thousands of ways of approaching each project.

    If I hadn't spent so much time trying to give you the most efficient method to begin with I might be more inclined to helping you search for your error(s) now . As it is, I'm not really in the mood to dumb down the code I gave you simply for the purpose of deceiving your instructor.

    I'm going to guess you're not returning false in form handler or there's some simple error in one of your loops (I did notice that not all your arrays are the same length) throwing an error in your JS (which halts the code and allows the form to submit without running everything first).

    Just run through your code with Firebug or IE and find out which lines are causing issues, and fix them line by line. You'll learn more that way than us handing the solution over to you.
    Feel free to e-mail me if I forget to respond ;)
    ohsosexybrit@gmail.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •