function SetUpBasket(toclear) { // OK

	//alert("In SetUpBasket : Before Key IF Statement, toclear : " + toclear)

	if ( document.cookie.indexOf("MLB1") == -1 || toclear == 1 ) {

		//alert("In SetUpBasket : After Key IF Statement, toclear : " + toclear)

		var cookdate = new Date()
		cookdate.setTime(cookdate.getTime()+1000*60*30) // 30 minutes

		for (var i=1;i<=9;i++) {
			document.cookie="MLB" + i + "=." ; expires = cookdate.toGMTString()  
		}

		// MLB1 = Code
		// MLB2 = Quantity
		// MLB3 = BriefDescription
		// MLB4 = ExPrice
		// MLB5 = Tax
		// MLB6 = IncPrice
		// MLB7 = Spare
		// MLB8 = Spare
		// MLB9 = Spare
	}
}

function FindMLB1Item(mlb1code) { // OK

	var count = GetCountItemTypes("MLB1")

	for (var i=1; i<=count ; i=i+1) {

		str = GetMLBxItem("MLB1", i )

		if (str == mlb1code) {
			return i
		}

	}
	return 0
}

function ModifyMLBQuantity(mlb1code,newquantity) { // OK

	pos = FindMLB1Item(mlb1code)
	
	var jcount = GetCountItemTypes("MLB2") // count current list length

	var cookdate = new Date()
	cookdate.setTime(cookdate.getTime()+1000*60*30) // 30 minutes

	var str = "MLB2" + "="

	for (var j=1; j<=jcount ; j=j+1 ) {
		if ( j != pos ) {
			str = str + GetMLBxItem("MLB2",j) + ","
		}
		else {
			str = str + newquantity + ","
		}
	}

	str = str.substring(0,str.length-1)
	
	document.cookie= str ; expires = cookdate.toGMTString()
	
}

function SimpleAddMLB(mlb1,mlb2,mlb3,mlb4,mlb5,mlb6,mlb7,mlb8,mlb9){ // OK

	var str = new Array (9)  // Arrays are referenced from 0 !

	var jcount = GetCountItemTypes("MLB1") // count current list length

	for (var i=0;i<9;i=i+1) {

		str[i] = "MLB" + (i+1) + "="
		if ( jcount != 0 ) {
			for (var j=1; j<=jcount ; j=j+1 ) {
				str[i] = str[i] + GetMLBxItem("MLB"+(i+1),j) + ","
			}
		}
	}
	
	var cookdate = new Date()
	cookdate.setTime(cookdate.getTime()+1000*60*30) // 30 minutes
	
	document.cookie=str[0] + mlb1 ; expires = cookdate.toGMTString()
	document.cookie=str[1] + mlb2 ; expires = cookdate.toGMTString()
	document.cookie=str[2] + mlb3 ; expires = cookdate.toGMTString()
	document.cookie=str[3] + mlb4 ; expires = cookdate.toGMTString()
	document.cookie=str[4] + mlb5 ; expires = cookdate.toGMTString()
	document.cookie=str[5] + mlb6 ; expires = cookdate.toGMTString()
	document.cookie=str[6] + mlb7 ; expires = cookdate.toGMTString()
	document.cookie=str[7] + mlb8 ; expires = cookdate.toGMTString()
	document.cookie=str[8] + mlb9 ; expires = cookdate.toGMTString()

//	document.cookie.write // ****** Change1 commented out
	
}

function ComplexAddMLB(mlb1code,mlb2quantity,mlb3,mlb4,mlb5,mlb6,mlb7,mlb8,mlb9) { // OK
// Checks for existing order, else adds
	
	var qty = parseInt(mlb2quantity,10)

	if (isNaN(qty)) {
		alert ("Sorry, the quantity entered is an invaild number. Please retype the quantity")
		return
	}

	if (qty<0  || qty>99) {
		alert ("Sorry, the quantity entered is outside the range 0 to 99. Please retype the quantity")
		return
	}



	var countitems

	var pos = FindMLB1Item(mlb1code)

	if ( pos != 0 ) {
		ModifyMLBQuantity(mlb1code,qty)
	}
	else {
//		countitems = GetCountItemTypes("MLB1")
//		if (countitems >= 18) {
//			alert("                        SHOPPING BASKET SIZE LIMIT REACHED!\nPlease call Nutpie to place this order on 01462 685984")
//			return
//		}

		SimpleAddMLB(mlb1code,qty,mlb3,mlb4,mlb5,mlb6,mlb7,mlb8,mlb9)
	}

	// Check for Zeros!

	countitems = GetCountItemTypes("MLB1")

	for ( i=countitems ; i>=1 ;  i=i-1 ) {
		if ( GetMLBxItem("MLB2",i) == 0 ) {
			var code = GetMLBxItem("MLB1",i)
			DeleteMLB1(code)				
		}			
	}

	alert ("You have placed " + mlb2quantity + " x " + mlb3 + " in your shopping basket.\n\nTo view your shopping click VIEW BASKET on the left hand menu.")
}

function DeleteMLB1(mlb1code) { // OK

	
	var pos = FindMLB1Item(mlb1code) // find position of item to be deleted

//	alert( "DeleteMLB1() pos=" + pos )

	if ( pos == 0 ) { return }       // if pos = 0 not found

	var jcount = GetCountItemTypes("MLB1") // count current list length

	var cookdate = new Date()
	cookdate.setTime(cookdate.getTime()+1000*60*30) // 30 minutes

	for (var i=1;i<=9;i=i+1) {

		var str = "MLB" + i + "="
		if ( jcount==1 ) { str=str + ".," }  // Only one item, so it must be deleted odd commer deleted later!
		else {
			for (var j=1; j<=jcount ; j=j+1 ) {
				if ( j != pos ) {
					str = str + GetMLBxItem("MLB"+i,j) + ","
				}
			}
		}
		str = str.substring(0,str.length-1)

		document.cookie=str ; expires=cookdate.toGMTString()
	}

}

function GetMLBxItem(mlbx,item) { // OK

	var items = GetCountItemTypes(mlbx)
	
	if ( items == 0 || item > items ) {
		return ""
	}
	else {
		var str = GetMLBxString(mlbx)
		var start = 0
		if ( item > 1 ) {
			for ( var i = 1 ; i < item ; i=i+1 ) {
				start = str.indexOf(",", start )+1   // should never fail since never reaches the end
			}	
		}	
		end = str.indexOf(",", start )
		if ( end == -1 ) { end = str.length  }

		//alert( str.substring(start,end) )
		return str.substring(start,end)
	}
}

function GetCountItemTypes(mlbx) { // OK

	var str = GetMLBxString(mlbx)

	if ( str.length == 1 ) {
		if( str == "." ) {	
			return 0
		}
		else {
			return 1
		}
	}
	else {
		var count = 1;

		start=0
		while  ( str.indexOf(",",start) != -1 ) {
			count=count+1
			start=str.indexOf(",",start)+1
		}

		return count
	
	}
}

function GetMLBxString(mlb) { // OK

	var index = document.cookie.indexOf(mlb)

	if (index > -1) {
	
		countstart = document.cookie.indexOf("=",index)+1
		countend = document.cookie.indexOf(";",index)
		
		if (countend == -1) {
			countend=document.cookie.length
		}
		//alert ( document.cookie.substring(countstart,countend) )
		contents = document.cookie.substring(countstart,countend);
	
		return contents
		
	}
	
	return ""
	
}

function DisplayCookieCrudeAll() { // OK

	for (var i=1;i<=9;i++) {
		DisplayCookieCrude("MLB"+i)
	}

}

function DisplayCookieCrude(cookiename) { // OK


	var index = document.cookie.indexOf(cookiename)

	document.write("COOKIE " + cookiename + ":  ")
	
	if (index > -1) {
		countstart = document.cookie.indexOf('=',index)+1
		countend = document.cookie.indexOf(';',index)
		
		if (countend == -1) {
			countend=document.cookie.length
		}

		document.write ("countstart " + countstart + ",  ")
		document.write ("countend " + countend + ",  ")		

		if ( countend == -1 ) {
			fulllist = ""
		}
		else {
			fulllist = document.cookie.substring(countstart,countend);
		}	
	}
	
	document.write ( "contents " + fulllist + ",  ")
	
	if ( fulllist.length > 0 ) {
		itemcount=1
		startpoint=1
		while ( fulllist.indexOf(',',startpoint)!= -1 ){
			itemcount=itemcount+1;
			startpoint=fulllist.indexOf(',',startpoint)+1
		}
	}
	
	else itemcount=0;
	
	document.write ("BADitemcount " + itemcount + "<BR>")
	
}

//
//
//
//
//
//
//
//
//

function DisplayTotals() {

	var countitems = GetCountItemTypes("MLB1")
	
	var pricetotal = 0
	var taxtotal = 0
	var finaltotal = 0
	
	if (countitems != 0) {

		document.write("<HR WIDTH='500'>")
	
		for (var i=1 ; i<=countitems ; i=i+1) {
			pricetotal = pricetotal + parseFloat(GetMLBxItem("MLB6",i))*parseFloat(GetMLBxItem("MLB2",i))	
			taxtotal = taxtotal + parseFloat(GetMLBxItem("MLB5",i))*parseFloat(GetMLBxItem("MLB2",i))	
		}
		pricetotal = pricetotal + 0.0     // price for shipping
		taxtotal =   taxtotal +   0.0     // tax for shipping
		finaltotal = pricetotal
	

		document.write("<table width='500' border='0'><tr>")
	
		document.write("<td width='415' align='center'><font class='AA14'><B>TOTAL AMOUNT DUE IN £:</B></font></td>")
		document.write("<td width='85' align='center'><font class='AA14'><B>" + format(finaltotal,2) + "</B></font></td></tr>") 
		
		document.write("</table>")

	}
	else {
		document.write("<center><B><font class='AA12'><font color='#FF0000'><B>Your shopping basket is currently empty.</B></font></font></Center>")
		document.write("<center><font class='AA12'>To add a product to the shopping basket click the 'Add to Basket'</font></Center>")
		document.write("<center><font class='AA12'>button associated with the product you wish to order.</font></Center>")
		document.write("<center><font class='AA12'><font color='#FF0000'>If you are experiencing problems see Shopping Help below.</font></font></Center>")
	}
}

function DisplayCookieSpecialAll() {
	// ComplexAddMLB("SHIP",1,"carriage cost",4.5,0,0,0,0,0)
	
	var countitems = GetCountItemTypes("MLB1")

  //  alert(countitems)

	if (countitems != 0) {

		document.write("<table width=500 border=0>")

		for (var i=1;i<=countitems;i++) {
			DisplayCookieSpecial(i)
		}

		document.write("</table>")
	}

	// DeleteMLB1("SHIP")
}



function DisplayCookieSpecial(item) {	

	var flt
	document.write("<tr>")


	document.write("<td width=80 align=Center><font class='AA12'>")

	document.write( GetMLBxItem("MLB1"	,item) )   // Code
	
	document.write("</font></td><td width=205 align=Center><font class='AA12'>")

	document.write( GetMLBxItem("MLB3"	,item) )   // Desc
	
	document.write("</font></td><td width=60 align=Center><font class='AA12'>")

	var str1 = "q" + item
	var str2 = GetMLBxItem("MLB2",item)
	document.write( "<input type=text size=2 name=" + str1 + " value=" + str2 + " >"  )   // Quantity
	
	document.write("</font></td><td width=70 align=Center><font class='AA12'>")

	flt=parseFloat(GetMLBxItem("MLB6",item))
	document.write( format(flt,2) )   // Price

	document.write("</font></td><td width=85 align=Center><font class='AA12'>")

	flt=parseFloat(GetMLBxItem("MLB6",item))*parseFloat(GetMLBxItem("MLB2",item))
	document.write( format(flt,2) )   // Price*Quantity

	document.write("</font></td></tr>")
}

function UpdateBasket() {

	var countitems = GetCountItemTypes("MLB2")

	var cookdate = new Date()
	cookdate.setTime(cookdate.getTime()+1000*60*30) // 30 minutes


	var str = "MLB2="
	if ( countitems > 0 ) {
		
		for (var i=1 ; i<=countitems ; i=i+1) {

			var qty = parseInt(document.itemsform.elements["q"+i].value)

			if (isNaN(qty)) {
				alert("Sorry, item order number " + i + " is invaild, please correct UPDATE again.")
				return
			}
			if (qty<0 || qty>99) {
				alert("Sorry, item order number " + i + " is outside 0 to 99 units, please correct UPDATE again.")
				return
			}


			str = str + document.itemsform.elements["q"+i].value
		
			// caREFUL ONLY WORKS WITH 20 ITEM ORDERS
			//if ( i == 1 )	str = str + document.itemsform.q1.value
			//if ( i == 2 )	str = str + document.itemsform.q2.value
			//if ( i == 3 )	str = str + document.itemsform.q3.value
			//if ( i == 4 )	str = str + document.itemsform.q4.value
			//if ( i == 5 )	str = str + document.itemsform.q5.value
			//if ( i == 6 )	str = str + document.itemsform.q6.value
			//if ( i == 7 )	str = str + document.itemsform.q7.value
			//if ( i == 8 )	str = str + document.itemsform.q8.value
			//if ( i == 9 )	str = str + document.itemsform.q9.value
			//if ( i == 10 )	str = str + document.itemsform.q10.value
			//if ( i == 11 )	str = str + document.itemsform.q11.value
			//if ( i == 12 )	str = str + document.itemsform.q12.value
			//if ( i == 13 )	str = str + document.itemsform.q13.value	
			//if ( i == 14 )	str = str + document.itemsform.q14.value
			//if ( i == 15 )	str = str + document.itemsform.q15.value
			//if ( i == 16 )	str = str + document.itemsform.q16.value
			//if ( i == 17 )	str = str + document.itemsform.q17.value
			//if ( i == 18 )	str = str + document.itemsform.q18.value
			//if ( i == 19 )	str = str + document.itemsform.q19.value
			//if ( i == 20 )	str = str + document.itemsform.q20.value
	
			// alert ( str )
			if ( i != countitems ) {
				str = str + ","
			}
		}
		document.cookie = str ; expires = cookdate.toGMTString()
		
		// Check for Zeros!
		for ( i=countitems ; i>=1 ;  i=i-1 ) {
	
			if ( GetMLBxItem("MLB2",i) == 0 ) {
				var code = GetMLBxItem("MLB1",i)
				DeleteMLB1(code)				
			}
				
		}
	}

	// Call to redraw the screen
        

	location.href="viewbasket.asp"
}

function format ( fltnumber, decplaces ) {

	var str = "" + Math.round( eval(fltnumber)*Math.pow(10,decplaces) )
	while (str.length <= decplaces ) {
		str = "0" + str
	}
	var decpoint = str.length - decplaces
	return str.substring(0,decpoint)+ "." + str.substring(decpoint,str.length)

}

function GoToOrderItems() {
	if ( GetCountItemTypes("MLB2") > 0 ) {
		top.location.href="https://www.secure-server-hosting.com/secutran/secureforms/SH201183/purchase.html"
		return
	}
	alert( "No Items in Shopping Basket")

}

function BuildSecuitems()
{

	var transactiontax = 0.0
	var transactionamount = 0.0


	var str = "<INPUT TYPE='HIDDEN' NAME='secuitems' VALUE='"

	document.write( str )
	//alert ( str )

	for ( var i = 1 ; i <= GetCountItemTypes("MLB1") ; i=i+1 ) {
		var str = "["	
		str = str + GetMLBxItem("MLB1",i) + "||"
		str = str + GetMLBxItem("MLB3",i) + "|"
		str = str + GetMLBxItem("MLB4",i) + "|"
		str = str + GetMLBxItem("MLB2",i) + "|"
		str = str + GetMLBxItem("MLB4",i)*GetMLBxItem("MLB2",i)
		str = str + "]"
		document.write( str )

		transactiontax    = transactiontax    + GetMLBxItem("MLB5",i)*GetMLBxItem("MLB2",i)
		transactionamount = transactionamount + GetMLBxItem("MLB6",i)*GetMLBxItem("MLB2",i)

//		alert ( str )
	}

	str = "'>"
	document.write( str )
//	alert ( str )


// --------------  Shipping Tax and Total ----------

	transactiontax = format(transactiontax,2)
	transactionamount = format(transactionamount,2)


	str = "<INPUT TYPE='HIDDEN' NAME='shippingcharge' VALUE='0.00'>"
	document.write( str )

	str = "<INPUT TYPE='HIDDEN' NAME='transactiontax' VALUE='" + transactiontax + "'>"
	document.write( str )

	str = "<INPUT TYPE='HIDDEN' NAME='transactionamount' VALUE='" + transactionamount + "'>"
	document.write( str )

	str = "<INPUT TYPE='HIDDEN' NAME='transactiontwo' VALUE='" + transactionamount + "'>"
	document.write( str )
}
