Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-12-2012, 04:30 PM   PM User | #1
chrisboots
New Coder

 
Join Date: Jan 2012
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
chrisboots is an unknown quantity at this point
Price calculator code help

I have been adding a price calculator script to my works website, it all works ok currently but, as it stands, it works on daily price, weekly and monthly price.I need it to work on 1 , 2 ,3 ,4, 5 ,6 day, weekly, summer monthly and winter monthly. I am rather new to javascript and most of my script is not my own, can anyone help me ? also ive had to cut some of the code out so it will fit in the char limit

thanks in advanced

Code:
<
<head>
	
    </script>
<SCRIPT LANGUAGE="JavaScript">

function neil(num)

{

document.date.option.value=num

document.date.group.selectedIndex=num

}



image1 = new Image();

image1.src = "image/blanktext.gif";



function getCookieVal (offset) { 

  var endstr = document.cookie.indexOf (";",offset);

  if (endstr == -1) endstr=document.cookie.length; 

  return unescape(document.cookie.substring(offset,endstr));

}



function GetCookie(name) { 

  var arg=name+"="; 

  var alen=arg.length; 

  var clen=document.cookie.length; 

  var i=0; 

  while (i < clen) { 

    var j=i+alen; 

    if (document.cookie.substring(i,j)==arg) return getCookieVal(j);

    i=document.cookie.indexOf(" ",i)+1; 

    if (i==0) break;

  } 

  return null;

}



function SetCookie(name,value) { 

  var argv=SetCookie.arguments; 

  var argc=SetCookie.arguments.length; 

  var expires=(argc>2) ? argv[2] : null; 

  var path=(argc>3) ? argv[3] : null; 

  var domain=(argc>4) ? argv[4] : null; 

  var secure=(argc>5) ? argv[5] : null; 

  document.cookie = name + "=" + escape (value) + ((expires ==null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure ==true) ? ";secure" : ""); 

} 



function DeleteCookie(name) { 

  var exp=new Date(); 

  exp.setTime (exp.GetTime() -1); 

  var cval = GetCookie (name); 

  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); 

}



// new bit init fields for re-quote

function initialise() { 



  document.date.difference.value = "";



  document.date.amount.value = "£0.00";



}

//



function doitems() { 

  time = document.date.difference.value;

  cost = document.date.amount.value;

  option = document.date.type.value;

  rdate = document.date.rdate.value;

  rtime = document.date.rtime.value;

  cdate = document.date.cdate.value;

  ctime = document.date.ctime.value;

  cfrom = document.date.cfrom.value;	

  expiryDate=new Date(); 

  expiryDate.setTime(expiryDate.getTime() + (365*24*60*60*1000)); 



//Added By Ben for Passthrough to payment screen





		date.SDate.value = cdate;

		date.EDate.value = rdate;

		date.STime.value = ctime;

		date.ETime.value = rtime;

		date.TotTime.value = time;

		date.CarOption.value = option;

		date.TotCost.value = cost;

		date.ColFrom.value = cfrom;



//End of add

  SetCookie ("Item1",""+option ,expiryDate); 

  SetCookie ("Item2",""+cost ,expiryDate); 

  SetCookie ("Item3",""+time ,expiryDate);

  SetCookie ("Item5",""+rdate ,expiryDate);

  SetCookie ("Item4",""+cdate ,expiryDate);

  SetCookie ("Item6",""+rtime ,expiryDate);

  SetCookie ("Item7",""+ctime ,expiryDate);

  SetCookie ("Item8",""+cfrom ,expiryDate);

}



function calculate() {



    var days = parseInt(document.date.days.value)


if (document.date.cfrom.value == "A") {var grpstring="1,2500,13000,42000"}  //this bit is price 

if (document.date.cfrom.value == "B") {var grpstring="1,2650,14500,46000"}  

if (document.date.cfrom.value == "CD") {var grpstring="1,2800,15500,48000"} 

if (document.date.cfrom.value == "E") {var grpstring="1,3000,16500,51000"} 

if (document.date.cfrom.value == "F") {var grpstring="1,3200,17500,54000"} 

if (document.date.cfrom.value == "G") {var grpstring="1,3500,20000,59000"} 

if (document.date.cfrom.value == "H") {var grpstring="1,3800,22500,62000"} 

if (document.date.cfrom.value == "I") {var grpstring="1,4500,25000,65000"} 

if (document.date.cfrom.value == "J") {var grpstring="1,5000,27000,80000"} 

if (document.date.cfrom.value == "K") {var grpstring="1,6500,31000,82500"} 

if (document.date.cfrom.value == "L") {var grpstring="1,7000,36000,120000"} 

if (document.date.cfrom.value == "M") {var grpstring="1,2650,15000,48000"} 

if (document.date.cfrom.value == "O") {var grpstring="1,4100,21000,72500"} 
   
	for (var a=1; a <= 4; a++) {

      first = (a ==1) ? 0 : last+1;

      last = (a == 4) ? grpstring.length : grpstring.indexOf(",",first+1);

      if (a == 2) var daily=grpstring.substring(first,last);

      if (a == 3) var weekly=grpstring.substring(first,last);

      if (a == 4) var monthly=grpstring.substring(first,last);

    }

    var months=parseInt(days/28);

    var weeks=parseInt((days-(months*28))/7);

    if (weeks < 0) weeks = 0;



// new bit adds £20 for collect at LGW

    var collect = 0;	

    if (document.date.cfrom.value == "LGW") {  	

	collect = 2500;

		 

    }	

	

    var days=days-(months*28)-(weeks*7);

    if (days < 1) days = 0;

    if (days > 5 && weeks < 1) days = 5;

    if (months > 0) daily = parseFloat(monthly/28);

    else if (weeks > 0) daily = parseFloat(weekly/6);

    var cash="£"+((months*monthly)+(weeks*weekly)+(days*daily)+collect)/100;

	

    document.date.amount.value = cash;

    doitems();

}



function getCookieVal (offset) { 

  var endstr = document.cookie.indexOf (";",offset);

  if (endstr == -1) endstr=document.cookie.length; 

  return unescape(document.cookie.substring(offset,endstr));

}



function GetCookie(name) { 

  var arg=name+"="; 

  var alen=arg.length; 

  var clen=document.cookie.length; 

  var i=0; 

  while (i < clen) { 

    var j=i+alen; 

    if (document.cookie.substring(i,j)==arg) return getCookieVal(j);

    i=document.cookie.indexOf(" ",i)+1; 

    if (i==0) break;

  } 

  return null;

}



function SetCookie(name,value) { 

  var argv=SetCookie.arguments; 

  var argc=SetCookie.arguments.length; 

  var expires=(argc>2) ? argv[2] : null; 

  var path=(argc>3) ? argv[3] : null; 

  var domain=(argc>4) ? argv[4] : null; 

  var secure=(argc>5) ? argv[5] : null; 

  document.cookie = name + "=" + escape (value) + ((expires ==null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure ==true) ? ";secure" : ""); 

} 



function DeleteCookie(name) { 

  var exp=new Date(); 

  exp.setTime(exp.getTime() -1); 

  var cval = GetCookie (name); 

  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); 

}





function isValidDate(dateStr) {



var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year



var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {

alert(dateStr + " Date is not in a valid format.")

return false;

}

month = matchArray[1]; // parse date into variables

day = matchArray[3];

year = matchArray[4];

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;

}



function isValidTime(timeStr) {



var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;



var matchArray = timeStr.match(timePat);

if (matchArray == null) {

alert("Time is not in a valid format.");

return false;

}

hour = matchArray[1];

minute = matchArray[2];

second = matchArray[4];

ampm = matchArray[6];



if (second=="") { second = null; }

if (ampm=="") { ampm = null }



if (hour < 0  || hour > 23) {

alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");

return false;

}

if (hour <= 12 && ampm == null) {

if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {

alert("You must specify AM or PM.");

return false;

   }

}

if  (hour > 12 && ampm != null) {

alert("You can't specify AM or PM for military time.");

return false;

}

if (minute < 0 || minute > 59) {

alert ("Minute must be between 0 and 59.");

return false;

}

if (second != null && (second < 0 || second > 59)) {

alert ("Second must be between 0 and 59.");

return false;

}

return true;

}



function dateDiff(dateform) {



date1 = new Date();

date2 = new Date();

diff  = new Date();

sysDate = new Date(); 



firstday =  document.date.input1date.value;

firstmonth = document.date.input1date1.value;

firstyear = document.date.input1date2.value;

firsttime = document.date.ctime.value;

secondday1 = document.date.inputdate.value;

secondmonth1 = document.date.inputdate1.value;

secondyear1 = document.date.inputdate2.value;

secondtime1 = document.date.rtime.value;

document.date.firstdate.value = firstmonth + "/" + firstday + "/" + firstyear; 

document.date.seconddate.value = secondmonth1 + "/" + secondday1 + "/" + secondyear1;

document.date.cdate.value = firstday + "/" + firstmonth + "/" + firstyear; 

document.date.rdate.value = secondday1 + "/" + secondmonth1 + "/" + secondyear1;



if (isValidDate(date.firstdate.value)) { // Validates first date 

date1temp = new Date(date.firstdate.value);

date1.setTime(date1temp.getTime());

}

else return false; // otherwise exits



if (isValidDate(date.seconddate.value)) { // Validates second date 

date2temp = new Date(date.seconddate.value);

date2.setTime(date2temp.getTime());

}

else return false; // otherwise exits



//new bit

if (date1temp <= sysDate) { // check for start date after today

alert("Collect date must be after today");

return false;

}

//new bit

if (date1temp >= date2temp) { // check for start date before end date

alert("Return date must be after collect date");

return false;

}

//

// sets difference date to difference of first date and second date



diff.setTime(Math.abs(date1.getTime() - date2.getTime()));



timediff = diff.getTime();

timediff = Math.floor(timediff /(1000*60*60));

if (firsttime < secondtime1 - 1)

{

  timediff = timediff + 24;

}



weeks = Math.floor(timediff / (24 * 7));

timediff -= weeks * ( 24 * 7);



days = Math.floor(timediff / 24); 

timediff -= days * 24;





// new bit validate meet from airport > 6 days hire

    if (weeks == "0") {

	if ((document.date.cfrom.value == "LGW") || (document.date.cfrom.value == "LHR")) {  	

	alert("Airport collections apply to weekly, or longer rentals only");

	return false;

	}

    }	



display()



}



function display(){

weekdays = weeks*7

if (weeks > 0) {  

var line = weeks + " weeks, " + days + " days, "

document.date.difference.value = line

calculate()

}

else document.date.difference.value = days + " days" 

document.date.days.value = days + weekdays

calculate()

}



function check()

{

var letter = document.date.input1date.value.length+1;

if (letter <=2)

{document.date.input1date.focus()}

else

{document.date.input1date1.focus()}

}



function checkone()

{

var letter2 = document.date.input1date1.value.length+1;

if (letter2 <=2)

{document.date.input1date1.focus()}

else

{document.date.input1date2.focus()}

}



function checktwo()

{

var letter3 = document.date.input1date2.value.length+1;

if (letter3 <=4)

{document.date.input1date2.focus()}

else

{document.date.inputdate.focus()}

}



function checkthre()

{

var letter4 = document.date.inputdate.value.length+1;

if (letter4 <=2)

{document.date.inputdate.focus()}

else

{document.date.inputdate1.focus()}

}



function checkfour()

{

var letter5 = document.date.inputdate1.value.length+1;

if (letter5 <=2)

{document.date.inputdate1.focus()}

else

{document.date.inputdate2.focus()}

}



function checkfive()

{

var letter6 = document.date.inputdate2.value.length+1;

if (letter6 <=4)

{document.date.inputdate2.focus()}



}

</script></head>
<body>


<div id="container">

<!-- header ................................. -->
<div id="header">
	<h1><a href="/index.html"><img src="images/pchsmall.gif" /><img src="images/pennylogo.jpg" /></a></h1>
</div> <!-- /header -->

<!-- navigation ................................. -->
<div id="navigation">

	<ul>

		<li><a href="index.html"> ALL VEHICLES </a></li>

		<li><a href="Cars.html"> Car Rental's </a></li>

		<li><a href="MPV.html"> Mpv & People Carrier's</a></li>

		<li><a href="Vans.html"> Van Rental's </a></li>
		
		<li><a href="deals.html"> Monthly Deals </a></li>

			</ul>

</div><!-- /navigation -->

<hr class="low" />


<!-- content ................................. -->
<div id="content">

	<div class="entry single">



   </div>
<!--form-->

	

	<table align="center" bgcolor="#A3C159" border="2" width="470" cellspacing="0" bordercolor="#000000">

	<form method="get" name="date" onsubmit="return dateDiff()" ACTION="https://cwcs16.cwcs.co.uk/~fulcrum/newbookingform.php" target="_blank">



	<tr><td><table align="center" border="0" width="470" cellspacing="0" cellpadding="5">

		<tr><td align="center" height="25" colspan="4" width="470"> 

		<font size="5" face="arial" color="red"><a name="Calculate Cost">Please type in the dates for a quote.</a></font></td>

	</tr>



	<tr><td colspan="4" height="7" width="220">

<input type='hidden' name='type' size='34' value='Toyota Aygo GO &nbsp; Group A'></td>	</tr>



	<!-----new drop down box---------->



	<tr>

		

		      <TD align="right"><b><font size="2" face="arial" color="black">Select Car :</b></font></TD>

                      <td colspan="3" height="25" width="220">

                          <select name="cfrom" style="font: bolder; color: #000000" onChange="initialise()" tabindex="13">

			    <option value="A">Manual Panda / Auto Smart</option>

                <option value="B">Manual Clio 3 3Dr or 5 Dr / Manual Picanto</option>

			    <option value="CD">Manual Rio / Clio 1.2T / Auto Picanto</option>

			    <option value="E">Manual Venga / Auto Rio</option>

			    <option value="F">Auto Focus / Cee'd / Manual Megane</option>	

				<option value="G">Auto Cee'd Estate / Manual Megane Estate</option>	

				<option value="H">Auto Mondeo</option>		

				<option value="I">Manual Sportage 2WD</option>		

				<option value="J">Auto Sportage 4X4</option>		

				<option value="K">Auto Galaxy / Manual RCZ</option>		
				
				<option value="L">Auto Sorento 4X4</option>	

				<option value="M">Manual Kangoo Van</option>

				<option value="O">Manual LWB LL29 Trafic</option>		
                          </select>

                      </TD>

	</tr>



<!---------end drop down box------>



	<tr><td height="25" align="right"><b><font size="2" face="arial" color="black">Collection Date :</b></font></td>

	    <td height="24" width="220">

		<select name="input1date" style="font: bolder; color: #000000" onChange="initialise()"  onkeyup="check()" tabindex="2"><option value="00">DD</option>

			<option value="01">01</option>

			<option value="02">02</option>

			<option value="03">03</option>

			<option value="04">04</option>

			<option value="05">05</option>

			<option value="06">06</option>

			<option value="07">07</option>

			<option value="08">08</option>

			<option value="09">09</option>

			<option value="10">10</option>

			<option value="11">11</option>

			<option value="12">12</option>

			<option value="13">13</option>

			<option value="14">14</option>

			<option value="15">15</option>

			<option value="16">16</option>

			<option value="17">17</option>

			<option value="18">18</option>

			<option value="19">19</option>

			<option value="20">20</option>

			<option value="21">21</option>

			<option value="22">22</option>

			<option value="23">23</option>
		
     		<option value="24">24</option>

			<option value="25">25</option>

			<option value="26">26</option>

			<option value="27">27</option>

			<option value="28">28</option>

			<option value="29">29</option>

			<option value="30">30</option>

			<option value="31">31</option>

              		</select>

		/ <select name="input1date1" style="font: bolder; color: #000000" onChange="initialise()"  onkeyup="check()" maxlength="2" tabindex="3"><option value="00">MM</option>

			<option value="01">01</option>

			<option value="02">02</option>

			<option value="03">03</option>

			<option value="04">04</option>

			<option value="05">05</option>

			<option value="06">06</option>

			<option value="07">07</option>

			<option value="08">08</option>

			<option value="09">09</option>

			<option value="10">10</option>

			<option value="11">11</option>

			<option value="12">12</option>

			</select>

		/ <select name="input1date2" style="font: bolder; color: #000000" onChange="initialise()"  onkeyup="check()" maxlength="2" tabindex="4" > 

		<option value="00">YYYY</option>
		
		<option value="2012">2012</option>

			</select>
		<font size="2" face="arial" color="black">(dd/mm/yyyy)</font></td>



<!--------new time box---------------------->

		<td height="25" align="right"><b><font size="2" face="arial" color="black">Time:</b></font></td>

		<td align="right" width="97"><font size="2"> 

              		<select name="ctime" style="font: bolder; color: #000000" onChange="initialise()" tabindex="10">

                	<option value="00">00:00</option>

			<option value="01">01:00</option>

			<option value="02">02:00</option>

			<option value="03">03:00</option>

			<option value="04">04:00</option>

			<option value="05">05:00</option>

			<option value="06">06:00</option>

			<option value="07">07:00</option>

			<option value="08">08:00</option>

			<option value="09">09:00</option>

			<option value="10">10:00</option>

			<option value="11">11:00</option>

			<option value="12">12:00</option>

			<option value="13">13:00</option>

			<option value="14">14:00</option>

			<option value="15">15:00</option>

			<option value="16">16:00</option>

			<option value="17">17:00</option>

			<option value="18">18:00</option>

			<option value="19">19:00</option>

			<option value="20">20:00</option>

			<option value="21">21:00</option>

			<option value="22">22:00</option>

			<option value="23">23:00</option>

              		</select></font>

		</td>



<!--------new time box---------------------->

 

</body>

</html>
chrisboots is offline   Reply With Quote
Old 01-13-2012, 08:00 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,362
Thanks: 18
Thanked 347 Times in 346 Posts
sunfighter is on a distinguished road
You need to declare a doc type first. Then run your code through http://validator.w3.org/ to find your errors and then correct them. My editor says you have 64 errors many depreciated tags. many tags missing.

It would be a big help to remove the white spaces (blank lines).
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:16 AM.


Advertisement
Log in to turn off these ads.