// Multi Image Upload V2.04
var numimages = 15;				// max # images - changes here must also be made to mult_uploadimagelist.php
var uploadfilenames = new Array();
for (var i=0; i<numimages; i++) uploadfilenames.push('');

function popupuploadform(img){		// 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 idbits = img.id.match(/_(\d+)_(.*)/);	// image has id="uimg_<index>_<ProductID>
  var seq = idbits[1];
  var ref = idbits[2];
  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 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 : defaulticon;	// load appropriate image
      }
    }
}

function checkimages(productid, mincount){		// see if all required images are uploaded
  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('inf_' + productid).value = imagelist;//  seq:title:filename|seq:title:filename|...
  return true;
}

function checkoptionalimages(productid, mincount){		// optional images form submit check
  var errmsg = '';
  var imagelist = '';
  var validimages = 0;
  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
      var thispostdata = imageseq + ':' + thisimage.alt + ':';	// remember its sequence, name
      var thisfilename = uploadfilenames[imageseq - 1];		// the file name
      thispostdata += thisfilename;				// add to cart list
      if ( thisfilename != '' )	
      	{				// have we an uploaded file name?
      	if ( imagelist != '' ) imagelist += '|';
	imagelist += thispostdata;
	validimages++;
      	}
      }	
    }
  if ( validimages < mincount  ) 
    {
    alert('You must upload at least ' + mincount + (mincount > 1 ? ' images!' : ' image!'));
    return false;
    }
  document.getElementById('inf_' + productid).value = imagelist;//  seq:title:filename|seq:title:filename|...
  return true;
}

