var g_stainguard = 1;
function testjavascript() 
{
   rc = alert("JavaScript is working.");
}
function popup_page(szURL) 
{ 
  szFeatures = 'toolbar=no,status=no,width=620,height=440,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no';
  szWin = window.open(szURL, 'Testimonials', szFeatures);
}
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 + '£' + 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==1) theitem  = fulllist.substring(itemstart,i);
         if (thisitem==2) theprice = fulllist.substring(itemstart,i);
         itemstart=i+1;
         thisitem++;

      }
      else if (fulllist.substring(i,i+1)==']')
      {
         if(theitem=='CUSTNO')
         {
            // dont display customer number
         }
         else
         {       	
            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>&nbsp;'+
                       ' Basket is empty&nbsp;</font>');
   }
   else {
      document.writeln('<font face="Verdana,Arial" size=1><b>&nbsp;'+
                       itemlist+' Items / ' + presentValue(totprice) + ' &nbsp;</b></font>');
   }
}
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,newStain,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 a colour 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='';
      }
      doconfirm=0;
      if ((newCode=="DS100") || (newItem=='CUSTNO')){      
      //if ((newItem=='CUSTNO')){
         // Dont display confirmation box for the customer number or discount voucher
         doconfirm=1;
      }
      else{
         if (confirm('Add '+newQuantity+' x '+newItem+' to basket? ')) {
            doconfirm=1;
         }
      }
      if(doconfirm==1)     
      {
			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) 
				{
					newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newStain+'|'+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();
					self.location.href = self.location.href;  // refresh the document
				}         
				//if (newItem!='CUSTNO'){
				//   self.location.href = self.location.href;  // refresh the document
				//}
         }
      }
   }
}

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 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.elegantfurniture.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');

}

function goTolink()
	{
	location.href = document.form1.products.options[document.form1.products.selectedIndex].value;
	}

function goTolink1()
	{
	location.href = document.form1.Tables.options[document.form1.Tables.selectedIndex].value;
	}
	
function goTolink2()
	{
	location.href = document.form1.information.options[document.form1.information.selectedIndex].value;
	}

function goTolink3()
	{
	location.href = document.form1.search.options[document.form1.search.selectedIndex].value;
	}

function goTolink4()
	{
	location.href = document.form1.seats.options[document.form1.seats.selectedIndex].value;
	}

function goTolink5()
	{
	location.href = document.form1.cabs.options[document.form1.cabs.selectedIndex].value;
	}
	
function goTolinkList() 
{
   var   txt='<select name="products" onchange="goTolink();" >';
   txt = txt+'<option selected>--Select Product--</option>';
   txt = txt+'<option value="../suites_all.php3">Sofas and Suites</option>';
   txt = txt+'<option value="../dining_room.php3">Tables and Cabinets</option>';   
   txt = txt+'<option value="../bedroom.php3">Beds</option>';
   txt = txt+'<option value="../bedroom_furniture.php3">Bedroom Furniture</option>';
   txt = txt+'<option value="../ultrassage_beds.php3">Adjustable Beds</option>';
   txt = txt+'<option value="../home_office.php3">Home Office</option>';
   txt = txt+'<option value="../lounge.php3">Occasional</option>';
   txt = txt+'<option value="../dining_ranges.php3">Collections</option>';
   txt = txt+'<option value="../special_offer.php3">Special Offers</option>';
   txt = txt+'</select>';
   document.write(txt);
}

function goTolinkList2() 
{
   var   txt='<select name="information" onchange="goTolink2();" >';
   txt = txt+'<option selected>--Information--</option>';
   txt = txt+'<option value="../faqs.php3">FAQs</option>';
   txt = txt+'<option value="../about.php3">About us</option>';
   txt = txt+'<option value="../faqs.php3#contactus">Contact us</option>';
   txt = txt+'<option value="mailto:feedback@elegantfurniture.co.uk">Email us</option>';   
   txt = txt+'<option value="../faqs.php3#payingus">Payment</option>';
   txt = txt+'<option value="../delivery.php3">Delivery</option>';
   txt = txt+'<option value="../about.php3#Finance">Finance</option>';
   txt = txt+'<option value="../faqs.php3#moneyback">14 Day Money Back</option>';
   txt = txt+'</select>';
   document.write(txt);
}

function goTolinkList3() 
{
   var   txt='<select name="search" onchange="goTolink3();" >';
   txt = txt+'<option selected>--Tables By Price Range--</option>';
   txt = txt+'<option value="dining_tables_under300.php3">Under £300.00</option>';
   txt = txt+'<option value="dining_tables_under500.php3">Between £300 and £500.00</option>';
   txt = txt+'<option value="dining_tables_under700.php3">Between £500 and £700.00</option>';
   txt = txt+'<option value="dining_tables_over700.php3">Over £700.00</option>';   
   txt = txt+'</select>';
   document.write(txt);
}

function goTolinkList4() 
{
   var   txt='<select name="seats" onchange="goTolink4();" >';
   txt = txt+'<option selected>--By Seating Capacity--</option>';
   txt = txt+'<option value="table_4.php3">Max 4 persons</option>';
   txt = txt+'<option value="table_6.php3">Max 6 persons</option>';   
   txt = txt+'<option value="table_8.php3">Max 8 persons</option>';
   txt = txt+'<option value="table_10.php3">Max 10 persons</option>';
   txt = txt+'</select>';
   document.write(txt);
}

function goTolinkList5() 
{
   var   txt='<select name="cabs" onchange="goTolink5();" >';
   txt = txt+'<option selected>--Cabinets By Price Range--</option>';
   txt = txt+'<option value="cabinets_under_399.php3">Under £399.00</option>';
   txt = txt+'<option value="cabinets_over_399.php3">Over £399.00</option>';   
   txt = txt+'</select>';
   document.write(txt);
}