var g_stainguard = 1;
function testjavascript() 
{
   rc = alert("JavaScript is working.");
}
function presentValue(value)
{
	if(value<0)
	{
		neg='-';
		value=value*-1;
	}
	else
	{
		neg='';
	}
   if(value<=0.9999)
   {
      newPounds='0';
   }
   else
   {
      newPounds=parseInt(value);
   }

   if (value>0)
   {
      newPence=Math.round((value+.000008 - newPounds)*100);
   }
   else
   {
      newPence=0;
   }
   if (eval(newPence) >= 100) 
   {
      newPence='0';
      newPounds=eval(newPounds)+1;
   }
   if (eval(newPence) <= 9) newPence='0'+newPence;

   newString= neg + '&pound;' + newPounds + '.' + newPence + '';
   return (newString);
}

function removeBasketVouchers()
{
	//remove all vouchers from basket - they must only be added before payment, otherwise could get discount not entitled to
   index=document.cookie.indexOf('TheBasket');
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);

   if (countend==-1) { countend=document.cookie.length; }

   fulllist=document.cookie.substring(countbegin,countend);	
   newItemList=null;
   itemlist=0;
   for(var i=0;i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)=='[')
      {
         thisitem=1;
         itemstart=i+1;
         linestart=i+1;         
      }
      else if (fulllist.substring(i,i+1)==']')
      {
         itemend=i;
         theitem = fulllist.substring(itemstart,itemend);
         itemlist=itemlist+1;
         if(theCode!="DS100")
         {
            newItemList = newItemList+'['+fulllist.substring(linestart,itemend)+']';
         }
      }
      else if(fulllist.substring(i,i+1)=='|')
      {
         if (thisitem== 4) theCode = fulllist.substring(itemstart,i);  
         thisitem++; itemstart=i+1;
      }      
   }
   index = document.cookie.indexOf('TheBasket');
   document.cookie='TheBasket='+newItemList;
 
}

function showBasketQty()
{
   index=document.cookie.indexOf('TheBasket');
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);

   if (countend==-1) { countend=document.cookie.length; }

   fulllist=document.cookie.substring(countbegin,countend);
   itemlist=0;
   totprice=0;
   theprice=0;

   for (var i=0; i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)=='[')
      {
         itemstart=i+1;
         thisitem=1;
      }
      else if (fulllist.substring(i,i+1)=='|')
      {

         if (thisitem==2) theprice = fulllist.substring(itemstart,i);
         itemstart=i+1;
         thisitem++;

      }
      else if (fulllist.substring(i,i+1)==']')
      {
         itemend=i;
         thequantity=fulllist.substring(itemstart,itemend);
         totprice=totprice+eval(theprice*thequantity);
         itemlist=itemlist+eval(thequantity);
      }
   }
   if (itemlist==0) {
      document.writeln('<font face="Verdana,Arial" size=1><span id="pagebasket">&nbsp;Basket is empty</span>&nbsp;</font>');
   }
   else {
      document.writeln('<font face="Verdana,Arial" size=1><span id="pagebasket">&nbsp;'+
                       itemlist+' Items <br> ' + presentValue(totprice) + '<span id="pagebasket">&nbsp;</font>');
   }
}

function showBasketQtyUpdate()
{
   index=document.cookie.indexOf('TheBasket');
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);

   if (countend==-1) { countend=document.cookie.length; }

   fulllist=document.cookie.substring(countbegin,countend);
   itemlist=0;
   totprice=0;
   theprice=0;

   for (var i=0; i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)=='[')
      {
         itemstart=i+1;
         thisitem=1;
      }
      else if (fulllist.substring(i,i+1)=='|')
      {

         if (thisitem==2) theprice = fulllist.substring(itemstart,i);
         itemstart=i+1;
         thisitem++;

      }
      else if (fulllist.substring(i,i+1)==']')
      {
         itemend=i;
         thequantity=fulllist.substring(itemstart,itemend);
         totprice=totprice+eval(theprice*thequantity);
         itemlist=itemlist+eval(thequantity);
      }
   }
	var mypagebasket=document.getElementById('pagebasket');
	
   if (itemlist==0) {	
		mypagebasket.innerHTML="&nbsp;Basket is empty";
   }
   else {
		mypagebasket.innerHTML="&nbsp;"+itemlist+" Items <br> " + presentValue(totprice);
   }	
	
}

function showBasketQtyHid()
{
	/* specifically for basket.php3 - creates a field containing the total amount so ccan be used in form */
   index=document.cookie.indexOf('TheBasket');
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);

   if (countend==-1) { countend=document.cookie.length; }

   fulllist=document.cookie.substring(countbegin,countend);
   itemlist=0;
   totprice=0;
   theprice=0;
   theCode="";

   for (var i=0; i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)=='[')
      {
         itemstart=i+1;
         thisitem=1;
      }
      else if (fulllist.substring(i,i+1)=='|')
      {

         if (thisitem==2) theprice = fulllist.substring(itemstart,i);
         if (thisitem==4) theCode = fulllist.substring(itemstart,i);         
         itemstart=i+1;
         thisitem++;

      }
      else if (fulllist.substring(i,i+1)==']')
      {
         itemend=i;
         thequantity=fulllist.substring(itemstart,itemend);
         if(theCode!="DS100")
         {
         	/* ignore discount code DS100 */
         	totprice=totprice+eval(theprice*thequantity);
         }	
         itemlist=itemlist+eval(thequantity);
      }
   }
   if (itemlist!=0) {
      document.writeln('<input type="hidden" name="baskettotalhid" value="' + totprice + '">');
   }
}
function testcookies() 
{
   // Test cookies by writing a unique number (the time, seconds since 1970)
   // to the cookie and then trying to retrieve it.
   var now = new Date();
   var timenow = now.getTime();
   index = document.cookie.indexOf('CookieTest'); 
   document.cookie='CookieTest=' + String(timenow) + '.;';  
   countbegin=(document.cookie.indexOf(String(timenow),index)+1);
   
   if (countbegin>=1)
   {
      //rc = alert('Cookies are working.');
		return 1;
   }
   else
   {
      rc = alert('Cookies are **NOT** working\nTo enable cookies in Internet Explorer go to Tools/Internet Options\nand click on the Privacy tab.\nEither reduce the security setting to Medium High\nor add our site to the list of sites allowed to use cookies');
		return 0;
   }   
}
function changeSpaces(tstring) 
{
   nstring='';
   for (var i=0; i <= tstring.length; i++) 
   {
      if (tstring.charAt(i)==' ') 
      {
         nstring=nstring+'^';
      } 
      else 
      { 
         nstring=nstring+tstring.charAt(i); 
      }
   }
   return nstring;
}

function buyItem(newItem,newPrice,new24,newCode,newFabric,newQuantity,newpage,newwarranty) 
{

   if(newPrice=="OutOfStock") 
   {
      rc = alert('This Item is currently Out Of Stock');
   }
   else if(newQuantity<=0) 
   {
      rc = alert('The quantity entered is incorrect');
   } 
   else 
   {

      switch(newFabric)
      {
         case "a" :
            if (document.form1.fabriccoloura.selectedIndex==0)
            {
               alert('Please Select Storage Option Required');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccoloura.options[document.form1.fabriccoloura.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "b" :
            if (document.form1.fabriccolourb.selectedIndex==0)
            {
               alert('Please Select Storage Option Required');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolourb.options[document.form1.fabriccolourb.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "c" :
            if (document.form1.fabriccolourc.selectedIndex==0)
            {
               alert('Please Select Storage Option Required');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolourc.options[document.form1.fabriccolourc.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "d" :
            if (document.form1.fabriccolourd.selectedIndex==0)
            {
               alert('Please Select Storage Option Required');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolourd.options[document.form1.fabriccolourd.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;   
         case "e" :
            if (document.form1.fabriccoloure.selectedIndex==0)
            {
               alert('Please Select Storage Option Required');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccoloure.options[document.form1.fabriccoloure.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;        	
         case "1" :	
            if (document.form1.fabriccolour.selectedIndex==0)
            {
               alert('You must make an option choice before ordering');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "2" :
            if (document.form1.fabriccolour2.selectedIndex==0)
            {
               alert('You must make a colour choice before ordering');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "3" :
            if (document.form1.fabriccolour3.selectedIndex==0)
            {
               alert('You must make a colour choice before ordering');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolour3.options[document.form1.fabriccolour3.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "4" :
            if (document.form1.fabriccolour4.selectedIndex==0)
            {
               alert('You must make a colour choice before ordering');
               return;
            }
            else
            {
               newFabric=document.form1.fabriccolour4.options[document.form1.fabriccolour4.selectedIndex].text;
               newItem=newItem+' ('+newFabric+')';
            }
            break;
         case "5" :
            if (document.form1.fabriccolour.selectedIndex==0)
            {
               alert('You must make a fabric colour choice before ordering');
               return;
            }
            if (document.form1.woodcolour.selectedIndex==0)
            {
               alert('You must make a wood colour choice before ordering');
               return;
            } 
            newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text+" fabric/"+document.form1.woodcolour.options[document.form1.woodcolour.selectedIndex].text+" wood";
            newItem=newItem+' ('+newFabric+')';
            break;
         case "6" :
            if (document.form1.fabriccolour.selectedIndex==0)
            {
               alert('You must make a fabric colour choice before ordering');
               return;
            }
            if (document.form1.fabriccolour2.selectedIndex==0)
            {
               alert('You must make a cushion colour choice before ordering');
               return;
            } 
            newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text+" with "+document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].text+" cushions";
            newItem=newItem+' ('+newFabric+')';  
            break;    
         case "7" :
            if (document.form1.fabriccolour.selectedIndex==0)
            {
               alert('You must make a fabric colour choice before ordering');
               return;
            }
            if (document.form1.fabriccolour2.selectedIndex==0)
            {
               alert('You must make a frame colour choice before ordering');
               return;
            } 
            newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text+" with "+document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].text+" frame";
            newItem=newItem+' ('+newFabric+')';    
            break;
         case "8" :
            if (document.form1.fabriccolour.selectedIndex==0)
            {
               alert('You must make a fabric colour choice before ordering');
               return;
            }
            if (document.form1.fabriccolour2.selectedIndex==0)
            {
               alert('You must choose a Chair Type before ordering');
               return;
            } 
            newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text+" with "+document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].text+" frame";
            newItem=newItem+' ('+newFabric+')'; 
            break;
         default :
            newFabric='';
      }
      if ((newCode=="DS100") || (confirm('Add '+newQuantity+' x '+newItem+' to basket? ')))          
      {
		
			testcook = testcookies();
			if(testcook==1){
				index=document.cookie.indexOf('TheBasket');
				countbegin=(document.cookie.indexOf('=',index)+1);
				countend=document.cookie.indexOf(';',index);
				if(countend==-1) { countend=document.cookie.length; }
				fulllist = document.cookie.substring(countbegin,countend);
				amended = false;
				newItemList=''; itemlist=0;
				for (var i=0;i<=fulllist.length;i++) 
				{
					if (fulllist.substring(i,i+1) == '[') 
					{
						thisitem=1;
						itemstart=i+1;
						fullstart=i+1;
					} 
					else if (fulllist.substring(i,i+1) == ']') 
					{
						itemend=i;
						thequantity=fulllist.substring(itemstart,itemend);
						itemlist++;
						if (theItem==newItem ) 
						{
							amended=true;
							if(newCode!="DS100")
							{	
								//add to existing item - except when a discount
								tempquantity=eval(thequantity)+eval(newQuantity);
							}
							else
							{
								//discount code must have quantity of 1
								tempquantity=1;
							}
							newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theStain+'|'+theCode+'|'+theFabric+'|'+thepage+'|'+tempquantity+']';
						} 
						else 
						{
							newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theStain+'|'+theCode+'|'+theFabric+'|'+thepage+'|'+thequantity+']';
						}
					} 
					else if (fulllist.substring(i,i+1)=='|') 
					{
						if (thisitem==1) theItem=fulllist.substring(itemstart,i);
						if (thisitem== 2) thePrice=fulllist.substring(itemstart,i);
						if (thisitem== 3) theStain=fulllist.substring(itemstart,i);
						if (thisitem== 4) theCode=fulllist.substring(itemstart,i);
						if (thisitem== 5) theFabric=fulllist.substring(itemstart,i);
						if (thisitem== 6) {
							thepage=fulllist.substring(itemstart,i);
						}
						thisitem++;itemstart=i+1;
					}
				}
				if (amended==false) 
				{
					newpage=newpage.replace(/_/g, "-");
					newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+new24+'|'+newCode+'|'+newFabric+'|'+newpage+'|'+newQuantity+']'; 
				}
				index = document.cookie.indexOf('TheBasket');
				document.cookie='TheBasket='+newItemList; 
				if (newwarranty==1 && g_stainguard==1)
				{
					g_stainguard = 0;
					//showwarranty();
				}

				if(newCode=="DS100")
				{
					self.location.href = "basket.php3";
				}
				else 
				{
					//remove any vouchers - they must be added last
					removeBasketVouchers();
					showBasketQtyUpdate();
				}
			}
      }
   }
}

function showwarranty()
{ 
   fmwin1=window.open('regency.htm','Warranty','toolbar=no,status=no,width=520,height=330,directories=no,scrollbars=no,location=no,resizable=yes,menubar=no');

}
function pricepromise()
{ 
   fmwin2=window.open('price_promise.htm','PricePromise','toolbar=no,status=no,width=480,height=280,left=20,top=20,directories=no,scrollbars=no,location=no,resizable=no,menubar=no');
}
function freesample()
{ 
   if (document.form1.fabriccolour.selectedIndex==0)
   {
      alert('You must make a colour choice before requesting a free colour sample');
      return;
   }
   else
   {
      var newFabric=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].text;
      var newImage=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].value;
      var oldPage=new String(document.location);
      // remove the full address to leave just the page name
      var searchstr=/.*\//;
      var newPage=oldPage.replace(searchstr,"");
      fmwin1=window.open('http://www.ivyfurniture.co.uk/cgi-bin/free_sample.cgi?image='+newImage+'&fabric='+newFabric+'&page='+newPage,'FreeSample','toolbar=no,status=no,width=410,height=360,directories=no,scrollbars=no,location=no,resizable=no,menubar=no');
   }
}

function goFabric()
{ 
   document.form1.fabriccolorimg.src=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].value;
}

function goFabric2()
{ 
document.form1.fabriccolorimg2.src=document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].value;
}

function goWood()
{ 
document.form1.woodcolorimg.src=document.form1.woodcolour.options[document.form1.woodcolour.selectedIndex].value;
}

function showbigcolour()
{
   if (document.form1.fabriccolour.selectedIndex != 0)
   {
      var oldpageimg=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].value;
      // remove the _small
      var newpageimg=oldpageimg.replace("_small.jpg",".jpg");
      fmwin1=window.open(newpageimg,'FabricColour','toolbar=no,status=no,width=460,height=360,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');
   }
}

function showbigcolour2()
{ 
   if (document.form1.fabriccolour2.selectedIndex != 0)
   {
      var oldpageimg=document.form1.fabriccolour2.options[document.form1.fabriccolour2.selectedIndex].value;
      // remove the _small
      var newpageimg=oldpageimg.replace("_small.jpg",".jpg");
      fmwin2=window.open(newpageimg,'FabricColour2','toolbar=no,status=no,width=460,height=360,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');
   }
}

function showbigcolour3()
{
      var oldpageimg=document.form1.fabriccolour.options[document.form1.fabriccolour.selectedIndex].value;
      // remove the _small
      var newpageimg=oldpageimg.replace("_small.jpg",".jpg");
      fmwin4=window.open(newpageimg,'WoodColour','toolbar=no,status=no,width=460,height=360,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}          

function showbigwoodcolour()
{
      var oldpageimg=document.form1.woodcolour.options[document.form1.woodcolour.selectedIndex].value;
      // remove the _small**.jpg
      var searchstr=/_small\.jpg/;
      var newpageimg=oldpageimg.replace(searchstr,".jpg");
      fmwin3=window.open(newpageimg,'WoodColour','toolbar=no,status=no,width=645,height=335,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}          

function showhighres()
{
      var oldpageimg=document.form1.bigpicture.src;
      // remove the .jpg
      var newpageimg=oldpageimg.replace(/\.jpg/,"_big.jpg");
      swidth="780";
      sheight="520";
      if (window.screen.width == "640") {
         swidth="620";
         sheight="420";
      }
      fmwin4=window.open(newpageimg,'HighResPicture','toolbar=no,status=no,width=' + swidth + ',height=' + sheight + ', left=3, top=3, directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}   

function showbigpic(imagename,widthx,heighty)
{
      if (window.screen.width == "640") {
         widthx="620";
         heighty="420";
      }
      fmwin4=window.open(imagename,'HighResPicture','toolbar=no,status=no,width=' + widthx + ',height=' + heighty + ', left=3, top=3, directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}

function recommend()
{ 

      var oldPage=new String(document.location);
      // remove the full address to leave just the page name
      var searchstr=/.*\//;
      var newPage=oldPage.replace(searchstr,"");
      fmwin1=window.open('recommend.htm?page='+newPage,'Recommend','toolbar=no,status=no,width=420,height=280,left=20,top=20,directories=no,scrollbars=no,location=no,resizable=no,menubar=no');

}

function showterms()
{
   fmwin1=window.open('terms.php3','Terms','toolbar=no,status=no,width=400,height=300,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}

