//Prepayplans.js javascript
//J. Wagner  / i-Create Web 2009

//Description of item, for fulfillment (passed to gateways)
var description;
//tax rates (percentages)
	var commercialtaxrate=6;
	var residentialtaxrate=4;
//UPDATE RATES BELOW
//autofill rates
	var autofill_prebuy=1.899;
	var autofill_pricecap=2.049;
	var autofill_budget=2.049;
//willcall rates
	var willcall_prebuy=1.949;
	var willcall_pricecap=2.099
	var willcall_budget=2.049;
//fixed or one-time tax-included payments
	var pricecap_onetime=59.95;
//number of budget plan payments, of which this is the first
	var budget_payments=10;

function tackon(varin,newvar){
	if (varin){
		return varin+", "+newvar;
	} else {
		return newvar;
	}
}
function calcprice(){
	//totprice is the end-result calculated total
	var totprice=0;
	//the ultimate per gallon rate
	var pergallonrate=0;
	//ultimate number of payments
	var total_payments=1;
	//reset the item description
	description="";
	var autofill=0;
	var willcall=0;
	var pricecap=0;
	var budget=0;
	var prebuy=0;

	//reference the items by their document IDs
	var baseval=1;
	if (document.getElementById('auto-fill').checked=='1'){
		autofill=baseval;
	}
	baseval=baseval<<2;
	if (document.getElementById('willcall').checked=='1'){
		willcall=baseval;
	}
	baseval=baseval<<2;
	if (document.getElementById('pricecap').checked=='1'){
		pricecap=baseval;
	}
	baseval=baseval<<2;
	if (document.getElementById('budget').checked=='1'){
		budget=baseval;
	}
	baseval=baseval<<2;
	if (document.getElementById('pre-buy').checked=='1'){
		prebuy=baseval;
	}

	switch (willcall|autofill|pricecap|budget|prebuy){
		case (autofill|prebuy):
			pergallonrate=autofill_prebuy;
			description=tackon(description,'Autofill/prebuy ($'+pergallonrate+'/gal.)');
		break;
		case (autofill|pricecap):
			pergallonrate=autofill_pricecap;
			description=tackon(description,'Autofill/pricecap ($'+pergallonrate+'/gal.)');
		break;
		case (autofill|budget):
			pergallonrate=autofill_budget;
			description=tackon(description,'Autofill/budget ($'+pergallonrate+'/gal.)');
		break;
		case (willcall|pricecap):
			pergallonrate=willcall_pricecap;
			description=tackon(description,'Willcall/pricecap ($'+pergallonrate+'/gal.)');
		break;
		case (willcall|budget):
			pergallonrate=willcall_budget;
			description=tackon(description,'Willcall/budget ($'+pergallonrate+'/gal.)');
		break;
		case (willcall|prebuy):
			pergallonrate=willcall_prebuy;
			description=tackon(description,'Willcall/prebuy ($'+pergallonrate+'/gal.)');
		break;
		default:
			pergallonrate=0;
	}
	if (pricecap|budget){
		//no per gallon rate at this time for pricecap or budget
		pergallonrate=0;
	}
	var numberofgallons=parseInt(document.getElementById('pbamount').value);
	description=tackon(description,'gallons:'+numberofgallons);
	//In case of fractional gallons or invalid input
	if (isNaN(numberofgallons)){
		numberofgallons=0;
	}
	document.getElementById('pbamount').value=numberofgallons;
	document.getElementById('pbrate').value=pergallonrate;
	
	var subtotal=numberofgallons*pergallonrate;

	//determine taxrate
	var taxrate=0;
	switch (document.getElementById('residential').checked+(2*document.getElementById('commercial').checked)){
		case 2:
			taxrate=commercialtaxrate;
			description=tackon(description,'tax rate(commerc.):'+taxrate+'%');
		break;
		case 1:
			taxrate=residentialtaxrate;
			description=tackon(description,'tax rate(resi.):'+taxrate+'%');
		break;
		default:
		taxrate=0;
	}
	document.getElementById('taxrate').value=taxrate;
	//Apply taxes
	var totprice=subtotal*((100+taxrate)/100);

	//For budget and payment plans- here we would maybe divide the price up
	if (budget){
		total_payments=budget_payments;
		description=tackon(description,total_payments+" payments");
	}
	//Divide by number of total_payments
	totprice=totprice/total_payments;

	//now add one-time tax included factors
	var onetimefee=0;
	if (pricecap){
		onetimefee=pricecap_onetime;
		description=tackon(description,'One-time fee:$'+onetimefee);
	}
	totprice+=onetimefee;	

	//return rounded (to pennies) price- only at last step to minimize inaccuracy.
	totprice= Math.round(totprice*100)/100;
	description=tackon(description,"Total:$"+totprice);
	if (totprice>0){
		return totprice;
	} else {
		//something is invalid or there is no price-
		return '';
	}
}
function updateprice(){
	var totprice=calcprice();
	
	document.getElementById('totalprice').value=totprice;
	document.getElementById('displayprice').innerHTML=totprice;
	/* in case of hiding blocks by amount
	if (totprice>0){
		document.getElementById('submitsection').style.display="block";
	} else {
		document.getElementById('submitsection').style.display="none";
	}
	*/
	document.getElementById('itemdescription').value=description;
}
function activatepayzone(){
	if (document.getElementById('ccpaymeth').checked){
		//activateCC
		document.getElementById('ccinfo').style.display="block";
		document.getElementById('paypalinfo').style.display="none";
	} else {
		//activatePaypal
		if (document.getElementById('paypalpaymeth').checked){	
			document.getElementById('paypalinfo').style.display="block";
			document.getElementById('ccinfo').style.display="none";
		} else {
			document.getElementById('paypalinfo').style.display="none";
			document.getElementById('ccinfo').style.display="none";
		}
	}
}
