var g_stainguard = 1;
function testjavascript() 
{
   rc = alert("JavaScript is working.");
}

function presentValue(value)
{
   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='£' + newPounds + '.' + newPence + '';
   return (newString);
}

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>&nbsp;'+
                       ' Basket is empty&nbsp;</font>');
   }
   else {
      document.writeln('<font face="Verdana,Arial" size=1><b><u>Basket</u>:&nbsp;'+
                       itemlist+' Items / ' + presentValue(totprice) + '</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.');
   }
   else
   {
      rc = alert('Cookies are **NOT** working');
   }   
}
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='';
      }
      if (confirm('Add '+newQuantity+' x '+newItem+' to basket? ')) 
      {
         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;
                  tempquantity=eval(thequantity)+eval(newQuantity);
                  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+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
         if (newwarranty==1 && g_stainguard==1)
         {
            g_stainguard = 0;
            //showwarranty();
         }
         self.location.href = self.location.href;  // refresh the document
      }
   }
}

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.diningandkitchen.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 show_longdate(fcolor) 
{
   var now = new Date();
   var month = now.getMonth();
   var date = now.getDate();
   var day = now.getDay();
   var monthname;
   var dayname;

   if (month == 0) monthname = "Jan";
   if (month == 1) monthname = "Feb";
   if (month == 2) monthname = "Mar";
   if (month == 3) monthname = "Apr";
   if (month == 4) monthname = "May";
   if (month == 5) monthname = "Jun";
   if (month == 6) monthname = "Jul";
   if (month == 7) monthname = "Aug";
   if (month == 8) monthname = "Sep";
   if (month == 9) monthname = "Oct";
   if (month == 10) monthname = "Nov";
   if (month == 11) monthname = "Dec";

   if (day == 0) dayname = "Sun";
   if (day == 1) dayname = "Mon";
   if (day == 2) dayname = "Tue";
   if (day == 3) dayname = "Wed";
   if (day == 4) dayname = "Thu";
   if (day == 5) dayname = "Fri";
   if (day == 6) dayname = "Sat";
   
    var year = now.getFullYear();

   document.write("<font face='Arial' size='2' color="+fcolor+"><b>&nbsp;");
   document.write(dayname + "&nbsp; "+ date + "&nbsp; " + monthname +  "," + "&nbsp;" + year);
   document.write("</b></font>");
}

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');

}
