// Multi Image Upload V2.00
var numimages = 15;				// max # images
var uploadfilenames = new Array('','','','','','','','','','','','','','','')
function popupuploadform(ref, seq){		// display the Image Upload Form
if ( location.href.indexOf('PreviewHTML') > -1 )
  {
  alert('Image can only be uploaded on live site');
  return;		// abandon if previewing
  }
  var popwin = window.open(basehref + 'mult_image_upload_form.html?ref=' + ref + '&seq=' + seq + '&end=1', 'upform', 'height=500,width=500,toolbar=no');
  popwin.focus();
}

function updateimage(ref, image, seq){		// update selected image with new PHP generated one
  uploadfilenames[seq - 1] = image;				// update JavaScript list
  var allimages = document.getElementsByTagName('img');
  for ( var i=0; i < allimages.length; i++)			//  look through all images on page 
    {						
    var thisimage = allimages[i];
    if ( thisimage.id.indexOf('uimg_' + seq + '_') > -1 ) 	// found matching image
      {
      thisimage.src = image;					// replace image with uploaded one
      }
    }
}

function clearimage(ref, seq){			// clear selected image with new PHP generated one
  uploadfilenames[seq - 1] = '';				// update JavaScript list
  var allimages = document.getElementsByTagName('img');
  for ( var i=0; i < allimages.length; i++)			//  look through all images on page 
    {						
    var thisimage = allimages[i];
    if ( thisimage.id.indexOf('uimg_' + seq + '_') > -1 ) 	// found matching image
      {
      thisimage.src = 'mult_clearimage.php?seq=' + seq;	  	// replace image with default one (and reset session info)
      }
    }
}

function showimages(ref){			// display waiting or default images
  var trueprodref = unescape((ref.substring(1)).replace(/_/g,'%'));		// extract ref from anchor
  document.write('<input type="hidden" id="inf_' + trueprodref + '" name="inf_' + trueprodref + '" value="" />');
  var imgspan = document.getElementById('mui_' + ref);
  var allimages = imgspan.getElementsByTagName('img');
  for ( var i=0; i < allimages.length; i++)			//  look through all images on this product 
    {						
    var thisimage = allimages[i];
    var imagebits = thisimage.id.match(/uimg_(\d+)_/) 
    if ( imagebits != null ) 					// found an upload image tag
      {
      var imageseq = imagebits[1];				// the sequence
      var thisfilename = uploadfilenames[imageseq - 1];		// the file name
      thisimage.src = thisfilename != '' ? thisfilename : 'mult_default_icon.jpg';	// load appropriate image
      }
    }
}

function checkimages(productid){		// see if all required images are uploaded
  infid = 'inf_' + unescape((productid.substring(1)).replace(/_/g,'%'));		// extract ref from anchor
  var errmsg = '';
  var imagelist = '';
  var imgspan = document.getElementById('mui_' + productid);
  var allimages = imgspan.getElementsByTagName('img');
  for ( var i=0; i < allimages.length; i++)			//  look through all images within product form
    {						
    var thisimage = allimages[i];
    var imagebits = thisimage.id.match(/uimg_(\d+)_/) 
    if ( imagebits != null ) 					// found an upload image tag
      {
      var imageseq = imagebits[1];				// the sequence
      if ( imagelist != '' ) imagelist += '|';
      imagelist += imageseq + ':' + thisimage.alt + ':';	// remember its sequence, name
      var thisfilename = uploadfilenames[imageseq - 1];		// the file name
      imagelist += thisfilename;				// add to cart list
      if ( thisfilename == '' )					// have we an uploaded file name?
        {
        errmsg += thisimage.alt + ', ';
        }
      }
    }
  if ( errmsg ) 
    {
    alert(errmsg + ' not uploaded!' );
    return false;
    }
  document.getElementById(infid).value = imagelist;//  seq:title:filename|seq:title:filename|...
  return true;
}

function setupuploadonsubmit(prodref){		// attach onsubmit check to product form
  infid = 'inf_' + unescape((prodref.substring(1)).replace(/_/g,'%'));		// extract ref from anchor
  if ( document.getElementById(infid) )
    {
    document.getElementById(infid).form.onsubmit=function(){return checkimages(prodref);}
    }
}