//pricing.js -- do the pricing work for Surfprint
//
// written by and copyright Tom Cranstoun, working through DDT Technology LTD, All rights reserved June 2001 
//
//	first released version
//
var discounttaken = 0
var offerstate = 0
var doc
var productvar 

var gCost
var gInks

var gPricecode = "bc"

var quantityvar
var papervar
var turnroundvar

var inksvar
var costvar2
var totalvar2
var quantityvar2
var extravar
var extraquantityvar
var pricecodevardecoded

var vatrate = 1.2



var products = new Array
var links = new Array
var product = 1
var productstyle = new Array
var optionlist = new Array
var optionstyle = new Array
var inks = new Array
var prices = new Array
var inkmultiplier = new Array
var quantitymultiplier = new Array
var optionmultiplier = new Array
var quantity = new Array

var optionbc = new Array
var optionlh = new Array
var optionfax = new Array
var turnround = new Array
var turnroundrate = new Array

var postage = new Array
var postagerate = new Array






//base prices

prices[1] = 15
prices[2] = 15
prices[3] = 15 //22
prices[4] = 15 //22
prices[5] = 15 //22
prices[6] = 22
prices[7] = 22
prices[8] = 15
prices[9] = 20

inkmultiplier[1] = 1
inkmultiplier[2] = 1.5
inkmultiplier[3] = 2
inkmultiplier[4] = 2.5
inkmultiplier[5] = 3
inkmultiplier[6] = 3.5
inkmultiplier[7] = 4

quantitymultiplier[1] = 0.6667		//100
quantitymultiplier[2] = 0.6667		//300
quantitymultiplier[3] = 1.5557		//500
quantitymultiplier[4] = 2.6667		//1000
quantitymultiplier[5] =	3.7777		//2000
quantitymultiplier[6] =	7.111		//5000

optionmultiplier[1] = 1
optionmultiplier[2] = 2


quantity[1] = 100
quantity[2] = 300
quantity[3] = 500
quantity[4] = 1000
quantity[5] = 2000
quantity[6] = 5000

inks[1] = 1
inks[2] = 2
inks[3] = 3
inks[4] = 4
inks[5] = 5
inks[6] = 6
inks[7] = 7


optionbc[1] = "Quality 350 gsm"

optionlh[1] = "Quality 100 gsm Wove"
optionlh[2] = "Quality 100 gsm Laid"

optionfax[1] = "Plain 80 gsm Paper"

postage[1] = "Courier"
postage[2] = "Special Offer"

postagerate[1] = 5.00
postagerate[2] = 0.00

turnround[2] = "Normal (despatched within 4 working days)"

turnroundrate[1] = 2
turnroundrate[2] = 1
turnroundrate[2] = 1


products[1] = "Business Card"
products[2] = "Personal Card"
products[3] = "Change of Address"
products[4] = "Invitation"
products[5] = "Ticket"
products[6] = "A4 Letterhead"
products[7] = "A4 continuation"
products[8] = "Compliment Slip"


links[1]="product.aspx?type=pc-bc"
links[2]="product.aspx?type=pc-pc"
links[3]="product.aspx?type=pc-coa"
links[4]="product.aspx?type=pc-i"
links[5]="product.aspx?type=pc-t"
links[6]="product.aspx?type=op-lh"
links[7]="product.aspx?type=op-cs"
links[8]="product.aspx?type=op-wcs"

optionlist[1] = "bc"
optionstyle[1]= 1
optionlist[2] = "bc"
optionstyle[2]= 1
optionlist[3] = "bc"
optionstyle[3]= 1
optionlist[4] = "bc"
optionstyle[4]= 1
optionlist[5] = "bc"
optionstyle[5]= 1

optionlist[6] = "lh"
optionstyle[6]= 2
optionlist[7] = "lh"
optionstyle[7]= 2
optionlist[8] = "lh"
optionstyle[8]= 2


//this is code -- only tom can change this from here




////////////////////////////////////////////////////////////////////////////////////////

function verPricing(wanted)
{
	return(1)
}
///////////////////////////////////////////////////////////////////////////////////////


function quantity_changed() {

pricejob()

}

function offered(){
pricejob()
offerstate = offerstate + 1
if (offerstate == 2) offerstate=0

	document.order.turnround.setAttribute("disabled",false)
	document.order.quantity.setAttribute("disabled",false)
	document.order.paper.setAttribute("disabled",false)
if (offerstate == 1)  {
	document.order.quantity.value="1"
	document.order.turnround.value="2"
	document.order.turnround.setAttribute("disabled",true)
	document.order.quantity.setAttribute("disabled",true)
	document.order.paper.setAttribute("disabled",true)
	
//	document.order.vatamt.value="2.00"
//	document.order.costing.value="10.00"
//	document.order.total.value="11.50"
//	document.order.ftotal.value="11.50"
//	document.order.extra.value = "thank you for trialling us"
}
}

function discount(){
discountcode = document.order.code.value
if (discounttaken == 0) {

if (discountcode=="surf30-7st8") {
document.order.total.value=document.order.total.value*0.7
document.order.ftotal.value=document.order.ftotal.value*0.7
discounttaken = 1
 }
if (discountcode=="surf50-7fg8") {
document.order.total.value=document.order.total.value*0.5
document.order.ftotal.value=document.order.ftotal.value*0.5
discounttaken = 1
 }
 if (discountcode=="surf10") {
document.order.total.value=document.order.total.value*0.9
document.order.ftotal.value=document.order.ftotal.value*0.9
discounttaken = 1
 }

}
}


function turnround_changed() {


pricejob()

}

function paperbc_changed() {

pricejob()

}
function paperlh_changed() {

pricejob()

}

function paperbc_changed() {

pricejob()

}


function decodequantity(value) {
	return quantity[value]
}

function decodeturnround(howmuch) {

// scott turnround decoder;

turnround[1] = "[4 days]"
turnround[2] = "[2 days]"
turnround[3] = "[next day]"


	return turnround[howmuch]
}
function savepricecode(which) {
	gPricecode = which
}
function saveinks(which) {
	gInks = which
}


function decodepostage(which) {
var ret = 0

		ret = postagerate(which)
		
		return ret
}

function decodepricecode(what) {
var ret = ""

if (what=="bc") {
	ret = "cards"
}
if (what=="lh") {
	ret = "sheets"
}
if (what=="fax") {
	ret = "sheets"
}
if (ret =="") {
	ret = "items"
}
return ret
}

function decodevat() {
	return vatrate
}
function decodetotal() {

	return moneyfmt(gVat)
}
function moneyfmt(money) {
var ret = 0

	money = Math.round(money * 100)
	ret = money /100

return ret
}


function decodepaper(which) {
var ret = ""
if (gPricecode == "bc") {
	ret = optionbc[which]
}

if (gPricecode == "lh") {
	ret = optionlh[which]
}
if (gPricecode == "fax") {
	ret = optionfax[which]
}

if (gPricecode == "poster") {
	ret = "poster"
}
return ret
}
function iwork2(){
 calculateme()
}

function iwork() {
// have initialised work -- save values
productvar =top.frames[4].product.value
work()
}

function work() {

//work is only called from standalone page	

	doc = top.frames[4].document
	doc.clear
	doc.writeln('<table border=0>')

	ff = '<font face="verdana,arial,helvetica" size = 1>'
			
	doc.write('<p>\n<table>')
	doc.write(itemcontrol())
	doc.write(materialcontrol())
	doc.write(inkscontrol())
	doc.write(quantitycontrol())
	doc.write(turnroundcontrol())
	doc.write(postagecontrol())
	doc.write(pricecontrol())
	doc.write('</table><br>\n')

	//doc.write(calculatecontrol())
	doc.write(linkcontrol())
	
	doc.write('<P><FONT face="Verdana" size="1">Refunds will be given at the discretion of the Management, see <a href="Terms.aspx" target = _top>Terms and Conditions</a>')
	doc.write('<P>The price quoted above only applies to products ordered through the online design process')
    doc.write('<P>If you intend to supply your own design as a file click <a href=requestaquote.aspx target = _top>here</a>')
    doc.write('<P>If you would like us to print a product you previously ordered through another supplier click <a href=sendadesign.aspx target = _top>here</a>')
    doc.write('<P>If you intend to supply us with a hard copy of your design click <a href=sendadesign.aspx target = _top> here</a>')
    
	doc.close()
	calculateme()
}

function itemcontrol(){

var ret = ''
	ret = ret + "<tr><td>"+ ff+ "Product</td><td>\n"
	ret = ret + "<SELECT size=1 ID=product onchange = top.frames[3].iwork() style='Font: 8pt Arial'>\n"
	for (i=1; i< (products.length)-1; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			if (i==productvar) {
			ret = ret + " SELECTED "
			}
			ret = ret + ">" + products[i]
			ret = ret + "</OPTION>\n" 
	}	
ret = ret + "</SELECT></tr>"

return ret
}
function materialcontrol(){

var ret = ''
	ret = ret +"<tr><td>"+ff+"Material</td><td><SELECT size=1  onchange = top.frames[3].iwork2() ID=material style='Font: 8pt Arial'>\n"

lproductvar = optionstyle[top.frames[4].product.value]



if (lproductvar == 0) {
	lproductvar = 1
}
if (lproductvar == 1) {

	//business card style (personal, coa etc too)

	for (i=1; i< optionbc.length; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			ret = ret + ">" + optionbc[i]+ "</OPTION>\n" 
	}	
}

if (lproductvar == 2) {	
	//lh style (A4 personal, continuation etc etc too)

	for (i=1; i< optionlh.length; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			ret = ret + ">" + optionlh[i]+ "</OPTION>\n" 
	}	
}

if (lproductvar == 3) {	
	//fax style (future proofing)

	for (i=1; i< optionfax.length; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			ret = ret + ">" + optionfax[i]+ "</OPTION>\n" 
	}	
}

ret = ret + "</SELECT></tr>"
return ret
}



function inkscontrol(){
var i
var ret = ''


	ret = ret + "<tr><td>"+ ff+ "No of Inks</td><td><SELECT size=1 ID=inks onchange = top.frames[3].iwork2()  style='Font: 8pt Arial'>\n"
	
	if (optionlist[top.frames[4].product.value]== "bc" ) {
	
			ret = ret + "<OPTION value=1>Single Ink</OPTION>\n" 
			ret = ret + "<OPTION value=2>Multiple Inks</OPTION>\n" 
	
	} else { 
		for (i=1; i< inks.length; i++) {
				ret = ret + "<OPTION value='" + i  + "'"
				ret = ret + ">" + inks[i]+ "</OPTION>\n" 
		}	
	}
ret = ret + "</SELECT></tr>"

return ret
}
function quantitycontrol(){
var ret = ''
	ret = ret + "<tr><td>"+ ff+ "Quantity</td><td><SELECT size=1  onchange = top.frames[3].iwork2() ID=quantity style='Font: 8pt Arial'>\n"
	for (i=1; i< quantity.length; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			if (quantity[i]==500) {
				ret = ret + " SELECTED "
			}
			ret = ret + ">" + quantity[i]+ "</OPTION>\n" 
	}	
ret = ret + "</SELECT></tr>"
return ret
}

function turnroundcontrol(){
var ret = ''
	ret = ret + "<tr><td>"+ ff+ "Turnaround</td><td><SELECT size=1  onchange = top.frames[3].iwork2() ID=turnround style='Font: 8pt Arial'>\n"
	for (i=1; i< (turnround.length)-1; i++) {
			ret = ret + "<OPTION value='" + i  + "'" 
			if (i==2) {
				ret = ret + " SELECTED "
			}
			ret = ret + ">" + turnround[i]+ "</OPTION>\n" 
	}	
ret = ret + "</SELECT></tr>"
return ret
}
function postagecontrol(){
var ret = ''
	ret = ret + "<tr><td>"+ ff+ "Postage and Packing</td><td><SELECT size=1 ID=postage  onchange = top.frames[3].iwork2() style='Font: 8pt Arial'>\n"
	for (i=1; i< (postage.length)-1; i++) {
			ret = ret + "<OPTION value='" + i  + "'"
			ret = ret + ">" + postage[i]+ "</OPTION>\n" 
	}	
ret = ret + "</SELECT></tr>"

return ret
}

function pricecontrol(){
var i
var ret = ''
	ret = ret + "<tr><td>"+ ff+ "Calculated Price</td><td style='Font: 8pt Arial'>\n"
	ret = ret + "<div id =pricediv name=pricediv>&nbsp;</div>"
	ret = ret + "</tr>"

return ret
}


function linkcontrol(){
var ret 
	ret = "<font face='verdana,arial,helvetica' size = 1><p>To start designing your " + products[top.frames[4].product.value] + ", click <a href = " + links[top.frames[4].product.value] + " target =_top>here</a> "

return ret
}

function calculatecontrol() {

ret = ""
ret = '<INPUT id="Button1" type="button" value="Calculate" onclick = top.frames[3].calculateme() name="Button1">'

return ret
}
function finalcalc(product,inks,quantity,turnround,postage) {
var i = 0
var cost = 0

var qprice = 0
var tprice = 0

//bc - business cards
//alert(quantity) -- 1 = 100, 2 = 200, 
//alert(turnround) -- 1 = next day, 2 = 4 days ,3 = 16 days

// scott pricing is simply qty + turnround.

if (quantity==1) qprice = 14.68  //100
if (quantity==2) qprice = 21.34 //200
if (quantity==3) qprice = 27.18 //300
if (quantity==4) qprice = 33.01 //400
if (quantity==5) qprice = 39.68 //500
if (quantity==6) qprice = 52.18 //750
if (quantity==7) qprice = 60.51 //1000

if (turnround ==1) tprice = 1.99 // 21 day despatch
if (turnround ==2) tprice = 4.99 // 7 working days
if (turnround ==3) tprice = 9.99 //2 working days

cost = qprice + tprice



cost = moneyfmt(cost)
gCost = cost
if (cost < 0) cost ="Please Phone"
return(cost)
}

function decodevatamt() {

var ret = 0
var cost = gCost

vat = moneyfmt(cost * vatrate)

gVat = vat

if (vat <0) vat = "Please Phone"

ret = vat - gCost

return (moneyfmt(ret))
}



function calculateme () {
var i = 0
var cost = 0


i = prices[top.frames[4].product.value] 
cost = i


i = inkmultiplier[top.frames[4].inks.value]
if (top.frames[4].product.value < 6){
	if (top.frames[4].inks.value > 2) {
		i=inkmultiplier[2]
	}	
}

cost = cost * i

i = quantitymultiplier[top.frames[4].quantity.value]
cost = cost * i

i = turnroundrate[top.frames[4].turnround.value]
cost = cost * i

i = postagerate[top.frames[4].postage.value]
cost = cost + i
cost = moneyfmt(cost)
vat = moneyfmt(cost * vatrate)


top.frames[4].pricediv.innerHTML = cost + " pounds + VAT = "  + vat

//alert("£" + cost + " with vat = " + "£" + vat)
return vat
}


function keeproduct(what) {
product = what

}

function pricejob() {
var costing

document.order.quantityh.value=decodequantity(document.order.quantity.value)
document.order.paperh.value=decodepaper(document.order.paper.value)
document.order.turnroundh.value=decodeturnround(document.order.turnround.value)



costing =finalcalc(gPricecode,gInks,document.order.quantity.value ,document.order.turnround.value , 1)

document.order.costing.value=costing

document.order.vat.value=decodevat()


document.order.vatamt.value=decodevatamt()
document.order.total.value=decodetotal()
document.order.ftotal.value=decodetotal()
quantityvar2 = parseInt(document.order.quantity.value) + 1
//costvar2 = finalcalc(gPricecode ,gInks , quantityvar2, document.order.turnround.value, 1)

totalvar2 =decodevatamt()
totalvar2 =decodetotal()
extravar = moneyfmt(totalvar2 - document.order.total.value)
extraquantityvar = decodequantity(quantityvar2) - document.order.quantityh.value
pricecodevardecoded = decodepricecode(gPricecode)
//if (quantityvar2<6) {
//document.order.extra.value='An extra ' + extravar + ' gets you another ' + extraquantityvar + ' ' + pricecodevardecoded
//} else {
document.order.extra.value=''
//}
}
