// imageuploadsupport V8.00
var pending = new Array();			// contains list of inf_ formfields and their data			
var pindex = 0;
var products = new Object();			// all image upload products and thier parameters			

function resetinf(){				// reset all form inf_ variables to calculated values - display new images
  for ( var i=0; i < pending.length; i++)	//  this gets around IE's Refresh bug 
    {						
    var img = pending[i].imgbeg + '?pn=' + pending[i].imgmain + pending[i].imgmid + '&in=' + pending[i].imgtail;	// recover image name
    document.getElementById('inf_' + pending[i].ref).value = img;    	// overwrite FORM fields
    if ( location.href.indexOf('PreviewHTML') == -1 )
      {
      if (document.getElementById('img$' + pending[i].ref).src.indexOf('&in=' + pending[i].imgtail) == -1)	// if this is a new image
        {
        document.getElementById('img$' + pending[i].ref).src = img;	// refresh generated image
        }
      }
    }
}

function showdummyicon(image, ref){		// show a dummy upload image (only called on Preview)
  var icon = document.getElementById('icon$' + ref);
  var obj = products[ref];				// object containing upload image parameters
  icon.style.width = obj.iconw + "px";
  icon.style.height = obj.iconh + "px";
  icon.style.left = Math.round(obj.iconx - (obj.iconw / 2)) + 'px';
  icon.style.top = Math.round((obj.icony) - (obj.iconh / 2)) + 'px';
  icon.style.visibility = 'visible';
}

function updateimage(ref, image){		// update selected image with new PHP generated one
  for ( var i=0; i < pending.length; i++)	//  set all image fields to new image 
    {						
    pending[i].imgtail = image;
    }
  resetinf();					// now redisplay them all					
}

function popupimageuploadform(ref){		// 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 + 'image_upload_form.html?ref=_' + ref + '_', 'upform', 'height=500,width=500,toolbar=no');
  popwin.focus();
}

function displayimage(ref){			// Display the upload (or default) image
  prod = products[ref]; 
  prod.uploadfilename = (uploadfilename == '') ? 'imagedefault.jpg' : uploadfilename;

  var imgbeg  = 'imagedisplay.php';
  var imgmain =  prod.mainimage.replace(/.*\\/, '');		// strip any path info
  var imgmid =  '&ix=' + prod.iconx
  	       + '&iy=' + prod.icony
  	       + '&iw=' + prod.iconw
               + '&ih=' + prod.iconh;
  var imgtail = prod.uploadfilename; 	// image must be last param
  var startimg = imgbeg + '?pn=' + imgmain + imgmid + '&in=' + imgtail;
      
  document.getElementById("inf_" + ref).value = startimg;

  pending[pindex] = new Object();		// save list of inf_ formfields and their data
  pending[pindex].ref = ref;
  pending[pindex].imgbeg = imgbeg;
  pending[pindex].imgmain = imgmain;
  pending[pindex].imgmid = imgmid;
  pending[pindex].imgtail = imgtail;
  pindex++;

  if ( location.href.indexOf('PreviewHTML') > -1 )
    {
    var pathfrompreview = '..\\';
    if ( prod.mainimage != '' )
      {
      var imgpath = ( prod.mainimage.indexOf(':') > -1 ) ? '' : pathfrompreview;
      document.write('<img id="img$' + ref + '" src="' + imgpath + prod.mainimage + '" onload="showdummyicon(this, \'' + ref + '\')">');	// just show background image if previewing
      }
    else 
      {
      document.write('<img src="' + pathfrompreview + 'imagedefault.jpg" width=' + prod.iconw + ' height=' + prod.iconh + '>');
      }
    }
  else
    {
    document.write('<img id="img$' + ref + '" src="' + startimg  + '">');	// show generated image
    }
}
