/*
 The Organic Supermarket
 Written by Ian Collier, of lidd Consulting on behalf of Atonic Internet Limited
 This work copyright lidd Consulting and Atonic Internet Limited
 Please do not copy or modified this work without seeking the express permission of
 the copyright holder beforehand.
 Thank you
*/

/* pricing javascript supporting file */

// function to calculate the total price
function calctotal(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13) {
	// initialise counters
	var itemcount=0;
	var imagecount=0;
	// initialise a net total
	var nettotal=0;
	// get starting total
	var starttotal;
	starttotal=parseFloat(document.getElementById("txtstarttotal").value);
	// check checkboxes
	// basic name, address, tel (required)
	if(document.getElementById("chklistbasic").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p0));
	}
	// email address
	if(document.getElementById("chklistemail").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p1));
	}
	// website link
	if(document.getElementById("chklistwebsite").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p2));
	}
	// short description
	if(document.getElementById("chklistshortdesc").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p3));
	}
	// long description
	if(document.getElementById("chklistlongdesc").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p4));
	}
	// location map
	if(document.getElementById("chklistmap").checked) {
		nettotal=(parseFloat(nettotal)+parseFloat(p5));
	}
	// product ranges
	// count how  many we have
	for(var x=0;x<parseInt(document.getElementById("txtnumcats").value);x++) {
		if(document.getElementById("chkcategory"+x).checked) {
			itemcount++;
		}
	}
	// determine how much to add to nettotal depending on on the value of count
	if(itemcount>=1 && itemcount<=5) { 
		nettotal=(parseFloat(nettotal)+parseFloat(p6));
	}
	if(itemcount>=6 && itemcount<=10) {
		nettotal=(parseFloat(nettotal)+parseFloat(p7));
	}
	if(itemcount>=11 && itemcount<=15) {
		nettotal=(parseFloat(nettotal)+parseFloat(p8));
	}
	if(itemcount>=16) {
		nettotal=(parseFloat(nettotal)+parseFloat(p9));
	}
	// graphics
	// count how many we have
	for (var y=1;y<=3;y++) {
		if(document.getElementById("chkimage"+y).checked) {
			imagecount++;
		}
	}
	// determine how much to add to nettotal depending on the value of count
	if(imagecount==1) {
		nettotal=(parseFloat(nettotal)+parseFloat(p10));
	}
	if(imagecount==2) {
		nettotal=(parseFloat(nettotal)+parseFloat(p11));
	}
	if(imagecount==3) {
		nettotal=(parseFloat(nettotal)+parseFloat(p12));
	}
	// if nettotal is less than the start total then set nettotal to be the same as the start total
	// otherwise, take the start total away from the net total
	if(nettotal<starttotal) {
		nettotal=0;
	}
	else {
		nettotal=(nettotal-starttotal);
	}
	// set starttotal precision
	starttotal=starttotal.toFixed(2);
	// set total precision
	nettotal=nettotal.toFixed(2);
	// p13 is the VAT rate
	var vat=(parseFloat(nettotal)*(parseFloat(p13)/100));
	vat=vat.toFixed(2);
	// calculate grandtotal
	var grandtotal=(parseFloat(nettotal)+parseFloat(vat));
	grandtotal=grandtotal.toFixed(2);
	// store the values in the appropriate hidden textboxes
	document.getElementById("txtnettotal").value=nettotal;
	document.getElementById("txtvat").value=vat;
	document.getElementById("txtgrandtotal").value=grandtotal;
	// generate the html string for displaying the total
	var html="<span style='font-weight : bold;font-size : 12pt;'>&#163;"+starttotal+"<br />&#163;"+nettotal+"<br />&#163;"+vat+"<br />&#163;"+grandtotal+"</span>";
	// show the net total, vat, total price in the total price placeholder
	document.getElementById("totalprice").innerHTML=html;
	// show and store the number of items selected
	document.getElementById("txtnumitems").innerHTML=itemcount;
	document.getElementById("txtselectedproductranges").value=itemcount;
	// show the number of images selected
	document.getElementById("txtnumimages").innerHTML=imagecount;
}

// function to set start total (for non-new listings)
function setstarttotal() {
	document.getElementById("txtstarttotal").value=document.getElementById("txtnettotal").value;
}

// function to store checkbox value
function changecheckbox(cb, tb) {
	// get the value of cb
	if(document.getElementById(cb).checked) {
		document.getElementById(tb).value="1";
	}
	else {
		document.getElementById(tb).value="0";
	}
}

// function to record changes to the region selection list
function changeregion() {
	// get seleted index
	var selindex=document.frmSend.selregion.selectedIndex;
	// get selected value
	var selitem=document.frmSend.selregion.options[selindex].value;
	// update the txtregion textbox
	document.getElementById("txtregion").value=selitem;
}

// function to record changes to a file input box
function replacefilename(n) {
	// declare fname
	var fname=document.forms["frmSend"]["fileupload"+n].value;
	// declare newfname
	var newfname="";
	// declare a found flag
	var found=false;
	// declare and initialize charachter codes
	var bs=String.fromCharCode(92);
	var fs=String.fromCharCode(47);
	// process fname from the end towards the beginning
	// if we encounter a / for the first time, then ignore the rest of the string
	// this will give us only the filename
	for(var x=(fname.length-1);x>=0;x--) {
		// check that the current character is not a "\" (&#92;) - bs,  or a (&#47;) - fs
		// if the current character is a /  or a \ set found to true and exit loop
		// otherwise add current character to newfname
		if(!found) {
			if(fname.charAt(x)==bs || fname.charAt(x)==fs) {
				found=true;
				break;
			}
			else {
				newfname=fname.charAt(x)+newfname;
			}
		}
	}
	// update the appropriate txtfilename textbox
	document.getElementById("txtfilename"+n).value=newfname;
	// update the appropriate span's inner text
	document.getElementById("txtfilenameonly"+n).innerHTML=newfname;
	// change the image checkbox to be ticked
	document.getElementById("chkimage"+n).checked=true;
	// ensure the appropriate textbox has a value of 1
	document.getElementById("txtimage"+n).value="1";
}

// function to navigate to the payment system
function gotopay() {
	// *********************************************************************
	// assume payment system has aleady been integrated and payment is fine
//	alert("You will now be taken to the payment system.");
	self.location="thankyou.asp";
	// *********************************************************************
}