// JavaScript Document


function checklevels(n){
 form = document.calcform;	
	var col = form.collection.selectedIndex;
	if(col==1){	
		if(n==2 && form.returns.selectedIndex != 1) { 
			alert("Deliveries to your home must be followed by collection from your home. Unless you wish to carry the large and heavy carriage case around with you (see FAQ!)"); 
		}
	form.returns.selectedIndex=1;
	}
}



function dosums(xtype){
	
	checklevels(2);
  
  form = document.calcform;
	cont=true;


	if(form.collection.selectedIndex==0){ 
		alert ("Please select the method of collection!");
		form.collection.focus();
		cont = false;
	}

	if(form.returns.selectedIndex==0 && cont==true){ 
		alert ("Please select the method of return!");
		form.returns.focus();
		cont = false;
	}


	if(form.days.selectedIndex==0 && cont==true){ 
		alert ("Please select the number of days!");
		form.days.focus();
		cont = false;
	}

	if(form.boards.selectedIndex==0 && cont==true){ 
		alert ("Please select the number of boards!");
		form.boards.focus();
		cont = false;
	}
	
	if(cont==true){ 

		var days=form.days.value;
		var boards=form.boards.value;
		var collfee=eval(form.collection.value);
		var retfee=eval(form.returns.value);
		
		if(xtype==2 && form.collection.selectedIndex != form.returns.selectedIndex ) {
			// for the form showing the actual agent locations, if the collection != return we need to add the $20 surcharge
			retfee = retfee + 25;
		}
		


//		var days20=0; days25=0; days30 = 0;
//		if(days>14){ days20 = days-14; days25 = 7; days30 = 3; days40 = 4; }
//		else if(days>7){ days25 = days-7; days30 = 3; days40 = 4; }
//		else if(days>4){ days30 = days-4;  days40=4; }
//		else {days40 = days}
//		
////		info = "days@20=" + days20 + " days@25=" + days25 + " days@30=" + days30 + " days@40=" + days40;
//        price = collfee + retfee + days20*20 + days25*25 + days30*30 + days40*40 + 30;
price = (collfee + retfee + days*20)*boards;
//		if(boards==2) { price = price*1.88; }
//		if(boards==3) { price = price*2.68; }		
//		if(boards==4) { price = price*3.38; }	
	 
	 	// calculate per day price
		perday = parseInt( (price / (days * boards)),10);
		if(boards==1) { perdaytext = "<br>($" + perday + " per day)"; }
		else { perdaytext = "<br>($" + perday + " per day per board)"; }		
		
	 	if(xtype==1) {finishedhtml = "<b>$" + parseInt(price,10) + "</b>" + perdaytext; } // rentals.php page
	 	if(xtype==2) {finishedhtml = "<b>Total price: NZ$" + parseInt(price,10) + "</b>" + perdaytext; } // index.php page		 
		 document.getElementById('total').innerHTML=finishedhtml;
  	}
}
  

function makeprice(xval) {
	if(xval==0){xval6="0.00";}
	else{
		xval2 = parseInt(xval, 10);
		xval3 = (xval * 100);
		xval3A = parseInt(xval3, 10);
		xval4 = xval3A + "X";
		if (xval < 10){xval5 = xval4.substring(1,3);}
		if (xval >= 10 && xval < 100) {xval5 = xval4.substring(2,4);}
		if (xval >= 100 && xval < 1000) {xval5 = xval4.substring(3,5);}
		if (xval >= 1000 && xval < 10000) {xval5 = xval4.substring(4,6);}
		xval6 = xval2 + "." + xval5;
	}
	return(xval6);
}





function changecss(theClass,element,value) {  
	//documentation for this script at
	//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	 var cssRules;

	 var added = false;
	 for (var S = 0; S < document.styleSheets.length; S++){

    if (document.styleSheets[S]['rules']) {
	  cssRules = 'rules';
	 } else if (document.styleSheets[S]['cssRules']) {
	  cssRules = 'cssRules';
	 } else {
	  //no rules found... browser unknown
	 }

	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    if(document.styleSheets[S][cssRules][R].style[element]){
	    document.styleSheets[S][cssRules][R].style[element] = value;
	    added=true;
		break;
	    }
	   }
	  }
	  if(!added){
	  if(document.styleSheets[S].insertRule){
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
	  }
	 }
}




