//Functions for swapping out images for jewelry-chest 
//given product image names that look like 01.1.1.01.1.1.jpg
//Code: Order Design,Wood,Top,Shot,Contents,Image-Size

/*purpose: make sure that the image has only changed in the positions specified in the list
if the images differ in positions not specified in the list then return true
i.e.
 
 postionlist= 2,4     
 	        Positions: 1.2.3. 4.5.6.7
 originalimage name = 03.1.3.01.1.1.jpg
 imagename          = 03.1.2.02.1.1.jpg
 Returns True - changes : because postion 3 is different; postion 2, 4 are skipped
 
 
  postionlist= 2,4     
 	        Positions: 1.2.3. 4.5.6.7
 originalimage name = 03.1.3.01.1.1.jpg
 imagename          = 03.2.3.03.1.1.jpg
 Returns False - No changes : because postion 2, 4 are skipped
                	  
*/
function checkImageForChanges(imagename, positionlist, originalimage)
{
	var imageObj = document.getElementById(imagename);
	var imageSrc = imageObj.src;
	var skip = false;
  //split up the source into an array
  srcArray = imageSrc.split('/');
  //keep the last element - i.e. the image name
  imageName = srcArray[srcArray.length-1];
  imageArray = imageName.split('.');
  
  
  originalArray = originalimage.split('/');
  originalName = originalArray[originalArray.length-1];
  originalImageArray = originalName.split('.');
  
  positionArray = positionlist.split(',');
  //image names should be the same length but not the exact same
  if( originalImageArray.length == imageArray.length && originalName != imageName )
  {
	  //loop through each image array element, skipping each postion in the postion array
	  //
	  for(var j =0; j <= originalImageArray.length;j=j+1)
	  { 	
	  	
			//loop through each postion element and see if it matches the current array index
			// if it does then skip that postion
	  		for(var i =0; i < positionArray.length;i=i+1)
			{
				if(j == (positionArray[i]-1))
				{ 
					skip = true;
					break;
				}
				skip = false;
			}
			if( skip == true)
			{ 	skip=false;
				 continue;
			}
		 	  
			if(originalImageArray[j] != imageArray[j] )
			{	// postions were different exit
				 
			    return true;
			}
	  }
  }
  // images are the same dont do anything
  return false;
}

//changes product image given (,'','numeric position')
/*


param1: this
param2: image name to change
param3: numeric postion in the image to change

i.e. change position 4 to the selected index value of 03
'01.1.1.01.1.1.jpg'
*/
function changeProductSelect(obj,imagename,position, originalPhoto,resetSelect,product_id)
{
  
var temp ='';
var newimage ='';
var imageObj = document.getElementById(imagename);
var imageName ="";
var originalSrc="";

  imageSrc = imageObj.src;
  //split up the source into an array
  srcArray = imageSrc.split('/');
  //keep the last element
  imageName = srcArray[srcArray.length-1];
  
  //keep the image source path - everything in the Array except the last element
  for(var j=0; j< srcArray.length -1;j =j+1)
  {
  	originalSrc +=  srcArray[j]+'/';
  }
 
	//make sure that there is a selected index
	if(obj.selectedIndex)
	{
		updateIndex = obj.selectedIndex;
		updateValue = obj.options[updateIndex].value;
		 
		 
		//temp = imageName.split('.');
		 
		 	//javascript arrays start at 0 so adjust the postion
			//temp[position-1] = updateValue;
		 
		//newimage = temp.toString();
		//newimage = newimage.replace(/,/g,'.');
		newimage = updateValue;
		imageObj.src = originalSrc +''+newimage;
		imageObj.parentNode.href= originalSrc+'/largeImages/' +''+newimage;
		
		 imageObj.parentNode.onclick=new Function( "openWin('/index.cfm/fa/product.zoom&imagePath="+ newimage  +"&popup=1&product_id="+product_id+"'); return false;"); //originalSrc+'/largeImages/' +''+newimage;
		
		//change the Zoom link to popup the zoom thingy
		var zoomLink = document.getElementById('zoom');
		 
		zoomLink.onclick = new Function( "openWin('/index.cfm/fa/product.zoom&imagePath="+ newimage  +"&popup=1&product_id="+product_id+"'); return false;  ");
 	 
		 //alert(newimage);
 	  //testdiv = document.getElementById('testimage');
	   //testdiv.innerHTML  = imageObj.src + '<br>';
	 
	} 
	
}

//changes product image given (,'','numeric position')
/*
param1: image name to change
param2: full path of new image
param3: numeric postion in the image to change
param4: value to update
i.e. change position 6 to the value of 2

Code: Order Design,Wood,Top,Shot,Contents,Image-Size

'01.1.1.01.1.2.jpg'
'01.1.1.01.1.1.jpg'

*/
function changeProductImg(imagename,newsource,position,updateValue,product_id)
{
 
var temp ='';
var newimage ='';
var imageObj = document.getElementById(imagename);
var imageName ="";
var originalSrc="";

  imageSrc = imageObj.src;
  //split up the source into an array
  srcArray = imageSrc.split('/');
  //keep the last element
  imageName = newsource;
  
  //keep the image source path - everything in the Array except the last element
  for(var j=0; j< srcArray.length -1;j =j+1)
  {
  	originalSrc +=  srcArray[j]+'/';
  }
 
	//make sure that there is a selected index
	if( position != '' && updateValue != '' )
	{ 
		 
		temp = imageName.split('.');
		 
		 	//javascript arrays start at 0 so adjust the postion
			temp[position-1] = updateValue;
		 
		newimage = temp.toString();
		newimage = newimage.replace(/,/g,'.');
		imageObj.src = originalSrc +''+newimage;
		imageObj.parentNode.onclick=new Function( "openWin('/index.cfm/fa/product.zoom&imagePath="+ newimage  +"&popup=1&product_id="+product_id+"'); return false;"); //originalSrc+'/largeImages/' +''+newimage;
		
		//change the Zoom link to popup the zoom thingy
		var zoomLink = document.getElementById('zoom');
		 
		zoomLink.onclick = new Function( "openWin('/index.cfm/fa/product.zoom&imagePath="+ newimage  +"&popup=1&product_id="+product_id+"'); return false;  ");
 	    //scroll to the top of the page
		window.scrollTo(0,0);
		
	} 
	
}

 function openWin( windowURL ) 
 {

 window.open( windowURL, '','toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1' ) ;
  return false;
}

 function popWin(URL,name,w,h){
    window.open(URL,name,'scrollbars=1,toolbar=0,menubar=0,statusbar=0,addressbar=0,resizable=1,width=' + w + ',height= ' + h)
  }