
// START Dynamic Product Images on Radio Buttons
// 03-09-05 Bugfix. SELECT code failed with Duplicate products
// 19-11-04 Actinic V7 compatibility
// 27-09-04 Removed redundant code (cprodref, selcode)
// 26-09-04 Rewrite of SELECT code to minimise page size
// 25-09-04 amended to allow for images within SELECT list options
var current = '';
var scaledown;
var colcount;
var colpos;
var textunder;
var buttonunder;
var currentselect;						// used to hold the id of the <select..> statement under construction

function selectimage(attrib, index){				// called from onchange event in the <SELECT..> statement
  var cleanattrib = attrib.replace(/\!/g, "_");			// remove invalid characters caused by Duplicates
  var imgref = eval(cleanattrib + '[0]');			// 1st item has the main image name
  var thisimg = eval(cleanattrib + '[' + (index + 1) + ']');	// extract the image
  if ( thisimg ) document.getElementById(imgref).src = thisimg;	// and replace main image with it
}

function StartImageSelect(attribname){				// called from Act_VariantListHeader.html
  currentselect = attribname;					// save name of current array
  var cleanattribname = currentselect.replace(/\!/g, "_");	// remove invalid characters caused by Duplicates
  eval(cleanattribname + ' = new Array();');			// make an array for the images using unique attribute name
  eval('var thisarray = ' + cleanattribname);
  thisarray.push(current);					// current image name is 1st value
}

function EndImageSelect(){					// called from Act_VariantListFooter.html
  var thissel = document.getElementById(currentselect); 	// the <select> statement in question
  var cleanattribname = currentselect.replace(/\!/g, "_");	// remove invalid characters caused by Duplicates
  eval('var thisarray = ' + cleanattribname);			// the array for any image choices
  for ( i = 0; i < thissel.options.length; i++)
    {
    var opt = thissel.options[i].text;
    var styleval = opt.match(/(.*)\{(.*)\}(.*)/);		// have we a "{" and "}" in the text?
    if ( styleval != null )
      {
      thisarray.push(styleval[2]);				// save the image name
      thissel.options[i].text = styleval[1] + styleval[3]   	// and remove the image name from the options list
      }
    else
      {
      thisarray.push('');					// null image
      }
    }

}

function radioimage(starthtml, midhtml, choicename){	// called from Act_VariantRadioButton.html
   var radiohtml = '<INPUT TYPE=RADIO NAME="' + starthtml + '"';	// recreate the basic HTML
   radiohtml += ' VALUE="' + midhtml;			// and the value
   var result = choicename.match(/(.*)\{(.*)\}(.*)/);   // see if HTML for Name contains {.....}
   if ( result != null )
     {
     var textsep = textunder ? '<br>' : ''; 		// do we want text under button
     scaledown = scaledown - 0;
     var twidth = Math.floor(100 / colcount);
     colpos++;
     if ( isNaN(scaledown) || scaledown < 1 ) scaledown = 4;   	// set a default if bad scaledown
     var cpath = '';
     var curbits = current.match(/(.*\/)(.*)/);
     if ( curbits != null ) cpath = curbits[1];
     var thisimage = result[2];      			// the text within { }
     // add in image substitution code
     radiohtml += ' onClick="document.getElementById(\'' + current + '\').src=\'' + cpath + thisimage + '\'">';
     var thiswidth = Math.floor(iwidth.replace(/WIDTH=(.*)/,"$1") / scaledown);	// the width
     var thisheight = Math.floor(iheight.replace(/HEIGHT=(.*)/,"$1") / scaledown);	// the height
     document.write('<table width="' + twidth + '%" align=left>');		// start a table if appropriate width
     document.write('<tr><td><img src="' + cpath + thisimage 			// first cell with image
                  + '" width=' + thiswidth + ' height=' + thisheight + '>');
     if ( buttonunder )							// where do we want the button text 
       {
       document.write('<br>' + radiohtml + textsep + result[1] + result[3]);	// button under image
       }
     else
       {
       document.write('</td><td width="100%" valign=top>');		// new cell next to image
       document.write(radiohtml + textsep + result[1] + result[3]);     // then radio button and description
       }
     document.write('</td></tr></table>');				// finally end table
     if ( colpos >= colcount ) 
       {
       document.write('<br clear=all>');
       colpos = 0;
       }
     }
   else  
     {
     result = choicename.match(/(.*)\[(.*)\](.*)/);   			// see if HTML for Name contains [.....]
     if ( result != null )
       {
       var textsep = textunder ? '<br>' : ''; 				// do we want text under button
       var twidth = Math.floor(100 / colcount);
       colpos++;
       var cpath = '';
       var curbits = current.match(/(.*\/)(.*)/);
       if ( curbits != null ) cpath = curbits[1];
       var thisimage = result[2];      					// the text within [ ]
       radiohtml += '>';
       document.write('<table width="' + twidth + '%" align=left>');	// start a table if appropriate width
       document.write('<tr><td><img src="' + cpath + thisimage + '">');	// first cell with image
       if ( buttonunder )						// where do we want the button text 
         {
         document.write('<br>' + radiohtml + textsep + result[1] + result[3]);	// button under image
         }
       else
         {
         document.write('</td><td width="100%" valign=top>');		// new cell next to image
         document.write(radiohtml + textsep + result[1] + result[3]);  	// then radio button and description
         }
       document.write('</td></tr></table>');				// finally end table
       if ( colpos >= colcount ) 
         {
         document.write('<br clear=all>');
         colpos = 0;
         }
       }
     else								// no brackets of any kind
       {
       document.write(radiohtml + '>' + choicename);  			// so write the unaltered choice text
       }
    }
}
// (V11) END Dynamic Product Images on Radio Buttons


