function specialoffervalue(offer,qty,offerTot){
switch (offer){

case "a":
disname="three cds discount";
disval=1.323333;
discqty=3;
loc=7770;
disval=correctParse(disval*qty);
break

case "b":
disname="hearts 5 for 10 pounds"

disval=0.99;
discqty=5;
loc=7771;
disval=correctParse(disval*qty);
break
 
case "c":
disname="3 for 2 offer wallets"
 
lowerprice=13.99;
middleprice=14.99;
upperprice=17.99;
disval=0;
lqty=0;
mqty=0;
upqty=0;

orderstr=""
shopstr=getCookie("mooshop")
shopstrsplit=shopstr.split(";")

for (i=0;shopstrsplit.length>i;++i){
shopsubstring=shopstrsplit[i].split("#")
shopLoc=shopsubstring[4]
if ("c"==shopLoc.charAt(0) && parseFloat(shopsubstring[2])==13.99){
lqty+=parseInt(shopsubstring[1])}
if ("c"==shopLoc.charAt(0) && parseFloat(shopsubstring[2])==14.99){
mqty+=parseInt(shopsubstring[1])}
if ("c"==shopLoc.charAt(0) && parseFloat(shopsubstring[2])==17.99){
upqty+=parseInt(shopsubstring[1])}

}
offerqty=Math.floor(qty/3);

discqty=3;
loc=7772;
//lqty=correctParse((offerTot-(qty*upperprice))/(lowerprice));
//upqty=qty-lqty;

	if ((lqty-offerqty)>=0){
		disval=correctParse(lowerprice*offerqty);
		offerqty=0;
	}else{
		offerqty=offerqty-lqty;
		disval=correctParse(lowerprice*lqty);
	}
	
	if (offerqty>0){
		if ((mqty-offerqty)>=0){
		disval+=correctParse(middleprice*offerqty);
		offerqty=0;
	}else{offerqty=offerqty-mqty;
	disval+=correctParse(middleprice*mqty);
	}}
if (offerqty>0){
		if ((upqty-offerqty)>=0){
		disval+=correctParse(upperprice*offerqty);
		offerqty=0;
	}else{offerqty=offerqty-upqty;
	disval+=correctParse(upperprice*upqty);
	
	}
	}

//disval=correctParse(lowerprice*(offerqty-midqty-upqty)+middleprice*midqty+upperprice*upqty);


break


case "d":
disname="four freds discount"
disval=.45
discqty=4
loc=7773
break

//general discount 
case "z":
//promotions are checked.
if (getCookie("promo")){
disname=getCookie("promo")
totexdis=totalexdis()
disval=totexdis*20/100
discqty=1
if(totexdis<0.05){discqty=10}
loc=7774
}else{
	
	disname="20% sale";
	totexdis=totalexdis();
disval=totexdis*20/100;
discqty=999999;
if(totexdis<0.05){discqty=10000}
loc=7776	
//loc=7776	
//changed to 7774	
//7775 free chocolate!
}
break
}

if (qty>=discqty){

addproduct('z1000',disname,1,-disval,loc)
}
else{
addproduct('z1000',disname,0,-disval,loc)}

}
/////////////////////////////////////////////////////////////////////////free good
function freegood(){
Totalcost=totalorder()
if (Totalcost>=100000000000){
addproduct('z1000',"free chocolate bar",1,0.001,7775)
}
else
{
addproduct('z1000',"free chocolate bar",0,0,7775)
}
}
///////////////////////////////////////////////////////////////////////////////ex discount
function totalexdis(){
Totalcost=0
if (shopstr=getCookie("mooshop")){
shopstrsplit=shopstr.split(";")
for (i=0;shopstrsplit.length>i;++i){
shopsubstring=shopstrsplit[i].split("#")
// alert(parseInt(shopsubstring[4]))
if (isNaN((shopsubstring[4].charAt(0)))){continue}
		if (parseInt(shopsubstring[4])>410000 && parseInt(shopsubstring[4])<410051){continue}					  
subtotalPrice=parseFloat(shopsubstring[1])*parseFloat(shopsubstring[2])
Totalcost+=subtotalPrice }}
return Totalcost;
}
/////////////////////////////////////////////////////////////////////////////special offer
function specialoffer(offer,qty){
offer+=""
offerqty=0
offerCost=0
Totalcost=0
offer=offer.charAt(0)

if (isNaN(offer)){
orderstr=""
shopstr=getCookie("mooshop")
shopstrsplit=shopstr.split(";")

for (i=0;shopstrsplit.length>i;++i){
shopsubstring=shopstrsplit[i].split("#")
shopLoc=shopsubstring[4]
if (offer==shopLoc.charAt(0)){
offerCost+=parseFloat(shopsubstring[1])*parseFloat(shopsubstring[2])
offerqty+=parseInt(shopsubstring[1])}
}

offerqty+=parseInt(qty)
specialoffervalue(offer,offerqty,offerCost)
}
//general discount comment out if not applicable below DO NOT COMMENT OUT THIS LINE
else{specialoffervalue("z",1,0)}
}
////////////////////////////////////////////////////////////////////////
function removeorder(){

delCookie("mooshop")
}
////////////////////////////////////////////////////////////////////////
function deleteOrder()
{
if (getCookie("mooshop")){

delCookie("mooshop")}
location.reload(true)
}
////////////////////////////////////////////////////////////////////////////////currency
function currency(myVal) {
      myVal=myVal+""
        if (myVal.indexOf(".")==-1){
      myVal+=".00"}

      else{
        myVal=myVal.split(".");
        myVal[1]+="00"
        myVal[1]=myVal[1].substring(0,2)
        myVal=myVal.join(".")
                  }
         myVal=myVal+" UK Pounds Sterling";

         return myVal}
///////////////////////////////////////////correct decimal round
        function correctParse(myNum){
          myNum=parseFloat(myNum)*100
          myNum=Math.round(myNum)
          myNum=myNum/100
          return myNum}

/////////////////////////////////////////////////////////////////////update page
function updatePage()   {
if (getCookie("mooshop"))	{
shopstr=getCookie("mooshop");

shopstrsplit=shopstr.split(";")
for (i=0;shopstrsplit.length>i;++i){
shopsubstring=shopstrsplit[i].split("#")
shopLoc=shopsubstring[0];
qtyLoc="Q"+shopLoc;
if(document.forms[0].elements[qtyLoc]){document.forms[0].elements[qtyLoc].value=shopsubstring[1];}
}//end of for
				}
			}
/////////////////////////////////////////////////////////////////////////getcookie

function getCookie(NameOfCookie)
{ if (document.cookie.length > 0) 
{ begin = document.cookie.indexOf(NameOfCookie+"="); 
if (begin != -1) 
{ begin += NameOfCookie.length+1; 
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); } 
}
return null; 
}

///////////////////////////////////////////////////////////////////////////////setcookie

function setCookie(NameOfCookie, value, expiredays) 
{ var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) + 
((expiredays == null) ? "" : ";path=/; expires=" + ExpireDate.toGMTString());
}
/////////////////////////////////////////////////////////////////////////del cookie

function delCookie (NameOfCookie) 
{ if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
";path=/; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

}
/////////////////////////////////////////////////////////////total order

function totalorder(){
Totalcost=0
if (shopstr=getCookie("mooshop")){
shopstrsplit=shopstr.split(";")
for (i=0;shopstrsplit.length>i;++i){

shopsubstring=shopstrsplit[i].split("#")
subtotalPrice=parseFloat(shopsubstring[1])*parseFloat(shopsubstring[2])
Totalcost+=subtotalPrice }}
return Totalcost;
}
/////////////////////////////////////////////////////////add product

function addproduct(PID,Pname,Pqty,Pprice,Oloc){
Neworder=0
//Neworder=1 if product already ordered
orderstr=""
shopstr=getCookie("mooshop")
if (shopstr)	{
shopstrsplit=shopstr.split(";");


for (i=0;shopstrsplit.length>i;++i)        {
shopsubstring=shopstrsplit[i].split("#")
shopLoc=shopsubstring[0];

if (Oloc==shopLoc)	{
shopsubstring[1]=Pqty;
if (isNaN(parseInt(Pprice))==false){shopsubstring[2]=Pprice;}
Neworder=1
shopstrsplit[i]=shopsubstring.join("#")
			}

if (shopsubstring[1]>0)	{
if (orderstr==""){orderstr=shopstrsplit[i]}
else{orderstr=orderstr+";"+shopstrsplit[i]}	
			}

                                           }//end of for	
		}//end if (shopstr)
//adds a new product
if (Neworder==0)                       	{
if (Pqty>0){
cookStr=Oloc+"#"+Pqty+"#"+Pprice+"#"+Pname+"#"+PID;
orderstr=shopstr+";"+cookStr;}		}

setCookie("mooshop",orderstr,0.25)                }
/////alt buy////
function altBuy(PID,Pname,Pprice,Oloc){
	qtyLoc="Q"+Oloc;
Pqty=document.forms[0].elements[qtyLoc].value;
Buy(PID,Pname,Pqty,Pprice,Oloc)
return false;
}


//////////////////////////////////////////////////// buy /////////////////////////////

function Buy(PID,Pname,Pqty,Pprice,Oloc){

setCookie("test", "none", 0.25);
if (getCookie("test")=="none" )
{
delCookie("test");
Totalcost=0;
 if (getCookie("mooshop")==null){
 shopstr=0;
 orderlen=0;                     }
 else                           {
 shopstr=getCookie("mooshop")
 shopstrsplit=shopstr.split(";")
 orderlen=shopstrsplit.length   }

if (orderlen>51){
alert("Only a maximum of 50 products maybe ordered at one time.\nIf you need to order more, checkout and start again.")
return false;}
else{
if (Pqty<0||isNaN(Pqty)==true){
alert("please enter a valid number");
return false;
                              }
else {

if (shopstr==0)
    {
if (Pqty==0){
Pqty=1;
qtyLoc="Q"+Oloc;
document.forms[0].elements[qtyLoc].value=1;
            } 
cookStr=Oloc+"#"+Pqty+"#"+Pprice+"#"+Pname+"#"+PID;
setCookie("mooshop",cookStr,0.25);
//return false;
    }
else
 {
if (chqty(Pqty,Oloc)==1){Pqty=1;}
addproduct(PID,Pname,Pqty,Pprice,Oloc);
//return false;
}

specialoffer(PID,0);
Totalcost=totalorder();
freegood();
alert("You have ordered "+Pqty+" "+Pname+" @ "+currency(Pprice)+"\nYour total order excluding postage is: "+currency(Totalcost)+"\nTo check out go to shopping basket - top left menu");
return false;
}}
}
else{
alert("sorry you need to have cookies to buy on this site");
return false;
}
return false;}
///////////////////////////////////////////////////// if the qty field is null check for///
function chqty(cqty,Oloc){
cqty="5"+cqty
if (cqty==5){
qtyLoc="Q"+Oloc
document.forms[0].elements[qtyLoc].value=1
return 1
            }else 
{return 0} 
}

/////////////////////////////////////////////////////////////////////end buy 




