var prodId="",descrip,price,flag1=0,flag2,quant=1,pCol,pSetFlag,extras="NA",sp= "_",quantFlag=1,blurFlag =1,colour="";
var k_hide = 1,haveExtras,haveSet=0,haveType,low_=0,high_=0,lowEx,highEx,haveLets="n",haveMat=0,ex_,tempForTroll="";
var trollyArray = new Array(),trollyArray2 = new Array(); removals=new Array();
var buttonForContinue='<input type="button" id="continueS" value="Continue Shopping" onClick="shiftBack()" style="color:white;background-color:#e87511" />';
//var buttonForShow='<input type="button" name="Button" onClick="expandLayer('+'"tabLayer"'+')" value="Show Trolley" //style="color:white;background-color:#e87511">';
var closeButton;
var bigTrolleyOpen=0;

function getY( oElement )
{
var iReturnValue = 0;
while( oElement != null ) {
iReturnValue += oElement.offsetTop;
oElement = oElement.offsetParent;
}
return iReturnValue;
}




function moveLayer(){

var aTop = getY( document.getElementById('iAnchor'));
//alert(aTop);

var cOffLeft =document.getElementById('mainTable').offsetLeft;

var cOff =document.getElementById('mainTable').offsetWidth;
var tOff=document.getElementById('tabLayer2').offsetWidth;
var goodLeft= cOff-tOff+cOffLeft-3;
//aTop = document.getElementById("iAnchor").style.top+1;


document.getElementById("tabLayer2").style.left= goodLeft;
document.getElementById("tabLayer2").style.top= aTop;




 cOff =document.getElementById('mainTable').offsetWidth;
 tOff=document.getElementById('tabLayer').offsetWidth;
 goodLeft= cOff-tOff+cOffLeft-1;



document.getElementById("tabLayer").style.left=goodLeft;
document.getElementById("tabLayer").style.top= aTop;

}


function getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

function tote(){
	   var countX,low,high;
       var tote_=0;
 if(quant>1){
	  
	  with(document.f1){
	  for ( u=0;u<elements.length;u++) { 
	     if(elements[u].name == low_)  low = u; 
	     if( elements[u].name == high_)  high = u;
		 	  //alert(u + " "  +"!"+elements[u].name+"!"+ "   " +"!"+ low_ +"!"+ "  " + low);

		 }
	  }
	   with(document.f1){
	   
         for(countX=low;countX<=high;countX++){
              tote_ = tote_+parseInt(elements[countX].value);
		 }
	   }	  

       if ( quant!=tote_ ) {
	   alert("Hi. The number of colors you have chosen does not tally with the number of bins "+tote_+"   "+quant);
	   return(0);
       }
	   else  { return(1);}

}        
       return(1); 
}
function chLayer(){

 with(document.f1.s1){
 if(parseInt(options[options.selectedIndex].value) > 1)
      {document.getElementById('l1').style.visibility='hidden'; 

       document.getElementById('l2').style.visibility ='visible'
	   quant= parseInt(options[options.selectedIndex].value)}
 
 else {
       document.getElementById('l1').style.visibility ='visible'; 

       document.getElementById('l2').style.visibility ='hidden';  
	   quant = 1
      }
 
 }
	


}
function finishIt(){
       colour= "";
        var u,countY;
        with(document.f1){
		
         for ( u=0;u<elements.length;u++)
          { if(elements[u].name == low_) var low = u;
		    
		    if( elements[u].name == high_) var high = u;
		    
			
		  }		
		
         for(countY=low;countY<=high;countY++){
              if(parseInt(elements[countY].value) != 0) colour += elements[countY].name +parseInt(elements[countY].value)+ "!";
          }
	    
	 
      
 }
colour = colour.substr(0,colour.length-1);
}
function checkClick(){
var chFlag = 0;
if(   flag1==0 && haveType==1    )  {chFlag = 0; alert("You have to choose a type"); return ; }//return()
	else chFlag = 1;
if(   pSetFlag==0 && haveSet==1  )  {chFlag = 0; alert("You have to choose a set") }
    else chFlag = 1;
	
	
	
	
if (chFlag==1){	addTrolley(prodId,descrip,price)
	chFlag=0;}
	
}

function checkCook(s1,s2){
    var gCookie = gC("Trolley=",8);
	if("x"==gC("Trolley=",8)){return(true)}
	if(comp(s1,s2)==0) { 
	sC(document.f1.Trolley.value);}
}

function endTroll(){
      if(haveExtras==1)getExtras();
      if((quant == 1)&& (haveSet==0)) { chkRad(document.f1.colRad);checkClick()};
      if( ((quant > 1) && (haveSet==0) )  && tote()==1  ){finishIt();checkClick()}
      if(haveSet==1)checkClick();

//( (&& (quantFlag == 2 ) ) || quant==1){finishIt();checkClick();alert(colour)}

}

function sC(k){

		document.cookie = 'Trolley=' + escape(k);
		// no we need some code to set the temp store which allows multiple purchases from same page
		//
	//	tempForTroll = "";

}
function hideIt(){

if(k_hide== 1) { document.all.t1.style.visibility = "hidden" ;k_hide=0}
 else {document.all.t1.style.visibility = "visible"; k_hide = 1}

}
//The function below is used to check that there is something
// in the Trolley before the checkout page is called. It stops the checkout page
// being called with nothing in the Trolley.
function owtInTrolley(){
//alert("got herer");
	if( ""==document.f1.Trolley.value){ alert("There is nothing in Trolley: Please press Add to Trolley after making selection"); return(false) }
	else {
		
		  sC("x");
		  trollyArray2.splice(0,trollyArray2.length);
		   trollyArray.splice(0,trollyArray.length);
		 //  writeArray();
		//   alert("bye bye Mark")

		  return(true);
		  
		  }


	}
	
function setHide(){
var j_hide = setInterval("hideIt()",500);

}

// The value this function creates is used as a flag and the colour variable
function chkBox(boX){

	boX.click();
	boX.click();
    

}
function chkRad(raP){
var flagR = 0;
var kk
	for(kk=0;kk<raP.length;kk++){
    if(raP[kk].checked)   raP[kk].click()
	
}

}
function cLoad(){
	   var k = gC("Trolley=",8);
       if (k)
	   
	    {
			if(k == 'x') {sC("");return("")};
			if(k != 'x') return gC("Trolley=",8);
		
		}
		
	   return "";     

	   }
function comp(str1,str2){

			if(str1 == str2) return 1 
			else  return 0;


}
function fMoney(k){
		if (k.match(/^(\d+)$/)){
		    l = "£"+ k.replace(/^(\d+)$/,"$1.00");
	         return(l);
	    }

       l = "£" + k.replace(/(^\d+\.\d$)/,"$10");
       return(l);
}



function gC(cName, Cnum){


	var allCookies = document.cookie;
	if(allCookies == "") return("");
	var pos = allCookies.indexOf(cName);
	
    if(pos != -1){
	
	var start = pos + Cnum;
	var end = allCookies.indexOf(";",start);
	if(end == -1) end = allCookies.length;
	var value = allCookies.substring(start,end);
	value = unescape(value);
	return(value)
	
	}
}





function augmentC(auG){


		var cook = gC();
		
		var newCook = cook +auG;
		sC(newCook)
		
	



}
function checkNum(x){
        num= x.value;
		var k = parseInt(num)
		if(isNaN(k)   ){ 
				alert("You must insert a number");
				if( blurFlag ==1){
				x.focus();
				blurFlag = 0;
				}
				
		} blurFlag = 1;

}
function optionsWrite(){
	for(i=2;i<1001;i++){stringS = "<option value="+i+">"+i+"</option>" 
    document.write(stringS)
	}
}	


function loadBoxes(){
document.f1.Trolley.value = cLoad(); document.f1.t2.value = cLoad();//alert(document.f1.Trolley.value +"this is from loadboxes");
// there now follows some code neeed to load the shopping Trolley display
var tempStr = "";
var strT = document.f1.Trolley.value ;
if(strT){
tempStr =strT.replace(/_/g,' ');
	tempStr =tempStr.replace(/[A-Z][A-Z]+\w*\b/gm,'');
	tempStr = tempStr.replace(/\n/g,'<br>' );
	tempStr =tempStr.replace(/\b([a-zA-Z]\d+)+\b/g,'');
	tempStr =tempStr.replace(/\bz\w\b/g,'');
	tempStr =tempStr.replace(/!/g,'');
	tempStr =tempStr.replace(/\bu\b/g,'');
	// document.getElementById('trol').innerHTML = tempStr;
}


}

function addTrolley(code,prod,price){
    var tempStr;tempSt2="";
	
	var tPrice;
	if(colour == "") { alert("You have not chosen colour(s)")} else{
	//tempForTroll= document.f1.Trolley.value;	
	
	
	
//	if(tempForTroll == "" ){
		// document.f1.Trolley.value = document.f1.t2.value  ;
		//}
		//document.f1.Trolley.value=document.f1.Trolley.value
		tempSt2=tempSt2+ code + sp + quant + sp   +  prod +  sp + colour +sp+ extras +  '\n'; 

		tempForTroll= "   something ";	

	
	tempStr =tempSt2.replace(/_/g,' ');
	tempStr =tempStr.replace(/[A-Z][A-Z]+\w*\b/gm,'');
	tempStr = tempStr.replace(/\n/g,'<br>' );
	tempStr =tempStr.replace(/\b([a-zA-Z]\d+)+\b/g,'');
	tempStr =tempStr.replace(/\bz\w\b/g,'');
	tempStr =tempStr.replace(/!/g,'');
	tempStr =tempStr.replace(/\bu\b/g,'');
	// document.all.trol.innerHTML  = tempStr;   
	// document.getElementById('trol').innerHTML =  tempStr;
	
	
	//code to move trolly here|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	
	
	////|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
	fillBoxes(tempSt2,tempStr);	shiftLayer();


    }
	
}





function getExtras(){
var countEx=0, low,high;
extras = "";
 with(document.f1){
		
         for ( u=0;u<elements.length;u++){
               if(elements[u].name == lowEx) low = u;
		       if( elements[u].name == highEx)  high = u;
         }
         for(countEx=low;countEx<=high;countEx++){
		  if(elements[countEx].checked){
		     extras = extras + elements[countEx].value;
			 extras +=" ";
			 }
		 }

}//alert(document.f1.elements[low].value);
if(highEx==0 && (document.f1.elements[low].checked)){extras=document.f1.elements[low].value+";"}
extras = extras.substr(0,extras.length-1);
if(extras == ""){extras="NA"};
return;
}


function removeTroll(){
	document.all.trol.innerHTML ="";

loadBoxes();
	
	
	
	
	
	
	
	
	
	}
		
	
	
	
	
	function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);}
  
  
  
  


function removeItem(itemX){
	//alert(removals+ "= removals  array ="+ trollyArray2.length );
	if(trollyArray.length==0){alert("There is nothing in trolly"); return;}
	var j,i;u=trollyArray.length;
	for(i=trollyArray2.length;i>=0;i--){
		
		for(j=0;j<removals.length;j++){
			//alert(i+"= i"+j+"=j");
			
			if(i==removals[j]){trollyArray2.splice(removals[j],1);trollyArray.splice(removals[j],1)}
			
			//alert(removals[j]+"     "+i);
			}
		
		
		}
	
	//alert("now the arrray has this many elements: "+trollyArray2.length)
	
	writeArray();
    removals.splice(0,removals.length); // remove all elements from array

	//alert(removals.length)
	}
	
	function addRemove(numX){
		
	 var k=document.getElementById(numX).tabIndex;
	if(document.getElementById(numX).checked)

	{removals[k]=k; }	//alert( removals.length+"  checked  "+ removals+ "      "+k) }
	
	
	 else {removals.splice(k,1,"z");}
	
//	alert(removals.length+" not checked    "+ removals+ "      "+k);/// this is now outside else loop
	}

function writeArray(){
	
	
var newStringFill2="",x,newStringFill="";
for(x=0;x<trollyArray2.length;x++){
	var checkName="ch"+x, tabId="tabIndex="+x, tempSt;

	
	if( trollyArray2[x]!=null){
	
		
		newStringFill2=newStringFill2+'<input type="checkbox"'+tabId+' id='+checkName+' value="checkbox" tabindex='+'"x" onClick="addRemove(this.id)"/>'+trollyArray2[x];
		newStringFill=newStringFill+trollyArray[x];
		//alert(trollyArray[x]);
	
}
}

	
	
	if(newStringFill2=="")newStringFill2="(Empty)";
	document.getElementById('newTroll').innerHTML = newStringFill2;
	//	document.getElementById('newTroll2').innerHTML = newStringFill2;

    document.f1.Trolley.value=newStringFill;
	   // document.f1.t2.value=newStringFill;

	}






function fillBoxes(fillString,fillString2){ // new function for filling boxes with array
//alert(fillString2);
var newStringFill="",newStringFill2="", x,z;
trollyArray[trollyArray.length]=fillString;
trollyArray2[trollyArray2.length]=fillString2;
//alert(trollyArray2.length);
//alert(trollyArray2[trollyArray2.length]);
//alert(trollyArray[0]);
for(x=0;x<trollyArray.length;x++){
	
	if( trollyArray[x]!=null){
	
	var checkName="ch"+x, tabId="tabIndex="+x, tempSt;
newStringFill=newStringFill+trollyArray[x];
tempSt='<input type="checkbox"'+tabId+' id='+checkName+' value="checkbox" tabindex='+'"x" onClick="addRemove(this.id)"/>'+trollyArray2[x];
		
newStringFill2=newStringFill2+tempSt;
	}
	
//	if(trollyArray[x]!=null)
//	newStringFill=newStringFill+trollyArray[x]; ////this is the new big
    
	
	
}
document.f1.Trolley.value=newStringFill;

document.getElementById('newTroll').innerHTML = newStringFill2;
//document.getElementById('newTroll2').innerHTML = newStringFill2;

//alert(newStringFill2);
}


function arrayCookie(){
	
var i=0, str ="",str2="";

for(i=0;i<trollyArray.length;i++){
	
	
	str=str+trollyArray[i];
	     if(i!=trollyArray.length-1)str=str+"¬";
		str2=str2+trollyArray2[i];
			     if(i!=trollyArray.length-1)str2=str2+"¬";


	}
	
	
	if(str!=""){
		
		
		    document.cookie = "array1=" + escape(str); 
		    document.cookie = "array2=" + escape(str2); 
//alert(document.cookie+ "this is from arrayCookie");
		
		}
	
	
	
	
	
	
	}
	
	
	
	
	
	
	
function arrayFill(){
	
	

var st1,st2,flg=0,i=0,z=0,a = new Array(),b = new Array();
	st2 = gC("array2=",7);
		st1 = gC("array1=",7);

	if(st1)
	{ a = st2.split("¬");b = st1.split("¬");
//alert(st1+" ----------------------- "+st2+"----------------------"+a.length+"-----------------------"+a[0]+"-------------+++++++++"+a[1]);
}

	for(i=0;i<a.length;i++){
       
	    flg=1;					
		trollyArray2[i]	= a[i]			
		}
	for(z=0;z<b.length;z++){
       
	    flg=1;					
		trollyArray[z]	= b[z]	
	//	alert(trollyArray[z]);
		}
	
	
	if(flg==1)writeArray();

	
	
	}
	
	
	
function shiftLayer(){
if(bigTrolleyOpen==1)return;	
//closeButton=document.getElementById("buttCell").innerHTML;
//document.getElementById("buttCell").innerHTML=buttonForContinue;

var tempStr = document.getElementById("holder").innerHTML;
//alert(document.getElementById("holder").innerHTML);
document.getElementById("holder").innerHTML = "";
document.getElementById("holder2").innerHTML=tempStr;
document.getElementById("trollFormLayer").style.height="200px";
document.getElementById("trollFormLayer").style.width="700px";
document.getElementById("trollFormLayer").style.visibility="visible";
document.getElementById("tabLayer2").style.visibility="hidden";

//code follows to make trolly display larger:
//document.getElementById("tabTroll").className="bigTableText";
//document.getElementById("remove").className="bigButton";
//document.getElementById("continueS").className="bigButton";
//document.getElementById("checkoutX").className="bigButton";
////document.getElementById("tabTroll").setAttribute("class", "trolleyTable");

//new code for expanding the size of the big trolley
var ax = getStyle("tabTroll2","height");
//alert(ax);

ax=parseInt(ax);
//ax+=26;
ax+="px";

document.getElementById("trollFormLayer").style.height=ax;

bigTrolleyOpen=1;

}

function shiftBack(){

//var tempStr = document.getElementById("trollFormLayer").innerHTML;
//document.getElementById("trollFormLayer").innerHTML = "";
//document.getElementById("trollFormLayer").style.height = "0px";
//document.getElementById("trollFormLayer").style.width = "0px";
var tempStr = document.getElementById("holder2").innerHTML;

document.getElementById("trollFormLayer").style.visibility = "hidden";

document.getElementById("holder").innerHTML=tempStr;
document.getElementById("tabLayer").style.visibility="hidden";

//alert(buttonForShow);
//document.getElementById("buttCell").innerHTML=closeButton;
document.getElementById("tabLayer2").style.visibility="visible";
document.getElementById("tabTroll").className="";
document.getElementById("remove").className="";
//document.getElementById("continueS").className="";
document.getElementById("checkoutX").className="";


bigTrolleyOpen=0;
}


function expandLayer(tL){


document.getElementById(tL).style.height="200px"


}

function setButton(){
	
	
	closeButton=document.getElementById("buttCell").innerHTML;
	
	
	
	}
