//-----------------------------------------------------------------------------------
/** TODO: Use a different bean for these "actions" 
 * and create separate beans (or use existing ones) for each action.
 * This will then allow the form to work as a real form and not 
 * something else.
 */

var win;

var photoGenerated = new Array();
var frameGenerated = new Array();
var flexDebug;

function buyFrame(photoId) {
	document.ofoto_viewcartform.submission_action.value = "select_frames";
	document.ofoto_viewcartform.photoid.value = photoId;
	document.ofoto_viewcartform.submit();
	return false;
}

function buyCard(photoId) {
	document.ofoto_viewcartform.submission_action.value = "select_cards";
	document.ofoto_viewcartform.photoid.value = photoId;
	document.ofoto_viewcartform.submit();
	return false;
}

function reviewOutboundOrder(itemid) {
   document.ofoto_viewcartform.submission_action.value = "review_from_cart";
   document.ofoto_viewcartform.itemid.value = itemid;
   document.ofoto_viewcartform.submit();
   return false;
}


function openCenteredWindow(url, width, height, parms) {
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
	if (parms) { winParms += "," + parms; }
	win = window.open(url, "cartPreview", winParms);
	win.resizeTo(width,height);
	win.moveTo(left,top);
	win.focus();
}

function disableTwin(printSize, printQuantity) {
	var isOppositeChecked=false;
	if (printQuantity == 1) {
		isOppositeChecked=eval("document.ofoto_viewcartform.doubles" + printSize + ".checked");
		eval ("document.ofoto_viewcartform.doubles" + printSize + ".checked = false;");
	} else if (printQuantity == 2) {
		isOppositeChecked=eval("document.ofoto_viewcartform.singles" + printSize + ".checked");
		eval ("document.ofoto_viewcartform.singles" + printSize + ".checked = false;");
	}
	return isOppositeChecked;
}



var newWin;

function removeFromCart(photoid, itemid, isWhistler) {
	if(isWhistler)
		removeFromTransferManifest(photoid);

	document.ofoto_viewcartform.submission_action.value = "remove_from_cart";
	document.ofoto_viewcartform.photoid.value = photoid;
	document.ofoto_viewcartform.itemid.value = itemid;
	document.ofoto_viewcartform.submit();
	return false;
}	

// This function is referenced from fake "links" 
// That exist in html stored in a bean.
function preview(itemid, type) {
	if (type == INVENTORY_TYPE_PHOTO_CALENDAR) {
		// Photo Calendar
		var url = "CalPreview.jsp?itemid=" + itemid + "&localeid=" + locale + "&UV=" + gUV + "&";
		openCenteredWindow(url, 390, 600, "toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes");
	} else if (type == INVENTORY_TYPE_PHOTO_BOOK || type==44) {
		// Photo Books
		var url = "PPPreviewPopup.jsp?itemid=" + itemid + "&localeid=" + locale + "&UV=" + gUV + "&";
		openCenteredWindow(url, 720, 790, "toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes");
	}
	else if (type == INVENTORY_TYPE_CALENDAR_TNG ) {
		
		// Photo Calendar TNG
		var url = "PPCalPreviewPopup.jsp?itemid=" + itemid + "&localeid=" + locale + "&UV=" + gUV + "&";
		openCenteredWindow(url, 645, 790, "toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes");
	}

	return false;
}

function clearCart() {
	document.ofoto_viewcartform.submission_action.value = "clear_cart";
	document.ofoto_viewcartform.submit();
	return false;
}
function cartSubmit() {
	var expressCode="&amp;name=";

	if(document.ofoto_viewcartform.singles4x6) {
	
		if (document.ofoto_viewcartform.singles4x6.checked) 
			expressCode += "singles4x6";
	
		if (document.ofoto_viewcartform.doubles4x6.checked) 
			expressCode += "doubles4x6";
	
		if (document.ofoto_viewcartform.singles5x7.checked) 
			expressCode += "singels5x7";
	
		if (document.ofoto_viewcartform.doubles5x7.checked) 
			expressCode += "doubles5x7";
	
		if (expressCode != "&amp;name=") 
			document.ofoto_viewcartform.action += expressCode;
	}

	document.ofoto_viewcartform.submission_action.value = "next";
	document.ofoto_viewcartform.submit();
	return false;
}

function cartSignin(){
	document.getElementById('signin').value = 'true';
	document.getElementById("submission_action").value="signin";
	document.getElementById("ofoto_viewcartform").submit();
	return true;
}
 

function recalculate() {
	document.ofoto_viewcartform.submission_action.value = "recalculate";
	document.ofoto_viewcartform.submit();
	return false;
}


function navigatePage(pageNumber) {
	var pageNumberObj
	if (pageNumberObj = document.getElementById("pageNumber")) {
		pageNumberObj.value = pageNumber;
		document.ofoto_viewcartform.submission_action.value = "recalculate";
		document.ofoto_viewcartform.submit();
	}  
	return false;
}


function continueShopping() {
	document.ofoto_viewcartform.submission_action.value = "continue_shopping";
	document.ofoto_viewcartform.submit();
	return false;
}

function advancedPrintOptions() {
	document.ofoto_viewcartform.submission_action.value = "advanced_options";
	document.ofoto_viewcartform.submit();
	return false;
}

function showLargerPrintSizes() {
	document.ofoto_viewcartform.submission_action.value = "showLargerPrintSizes";
	document.ofoto_viewcartform.submit();
	return false;
}


function openPrintPreviewWindow(photoid, itemType, productSize) {
	var url = "PulpPrintPreview.jsp?photoid=" + photoid
			+ "&cartpreview=1" 
			+ "&item_type=" + itemType
			+ "&product_size=" + productSize
			+ "&rand=" + Math.floor(Math.random() * 9999999) 
			+ "&" + gST;
	// "preview", "scrollbars,resizable,width=525,height=720");

	var params = "scrollbars=yes,resizable=yes"

	openCenteredWindow(url, 535, 720, params);

	return false;
}

function openPrintPreviewWindowInv(photoid, inventoryId){
	var url = "PulpPrintPreview.jsp?photoid=" + photoid
			+ "&cartpreview=1" 
			+ "&inventoryId=" + inventoryId
			+ "&rand=" + Math.floor(Math.random() * 9999999) 
			+ "&" + gST;
	// "preview", "scrollbars,resizable,width=525,height=720");

	var params = "scrollbars=yes,resizable=yes"

	openCenteredWindow(url, 535, 720, params);

	return false;
}

function openEditPrintWindow(photoid, itemid,inventoryId,flash) {
	var url = "EditPrint.jsp?photoid=" + photoid
			+ "&cartpreview=1" 
			+ "&itemid=" + itemid
			+ "&inventoryId=" + inventoryId
			+ "&rand=" + Math.floor(Math.random() * 9999999)
			+ "&flash=" + flash // testing no Flash version			
			+ "&" + gST;
	if (flexDebug == "true") {
		url+= "&flexDebug=" + flexDebug;
	}

	var params = "scrollbars=yes,resizable=yes"

	openCenteredWindow(url, 625, 720, params);

	return false;
}

//PQTODO refactor so this isn't all duped code
function openEditPrintWindowForDynPhoto(photoid,inventoryId,flash,addToCart, virtualItemId) {
	
	// check to see if this window was opened previously

	if (photoGenerated[photoid + "_" + inventoryId] != undefined){
		return openEditPrintWindow(photoid,photoGenerated[photoid + "_" + inventoryId],inventoryId,flash);
		
	}

	var url = "EditPrint.jsp?photoid=" + photoid
			+ "&cartpreview=1" 
			+ "&inventoryId=" + inventoryId
			+ "&rand=" + Math.floor(Math.random() * 9999999)
			+ "&flash=" + flash // testing no Flash version
			+ "&virtualItemId=" + virtualItemId
			+ "&" + addToCart
			+ "&" + gST;
	if (flexDebug == "true") {
		url+= "&flexDebug=" + flexDebug;
	}

	var params = "scrollbars=yes,resizable=yes"

	openCenteredWindow(url, 625, 720, params);

	return false;
}


function openGiftCertificatePreviewWindow(itemid) {
	var url = "GiftCertificatePreview.jsp?itemid="+itemid + "&" + gST;
	var params = "scrollbars=yes,resizable=yes" 

	openCenteredWindow(url, 535, 415, params);
    return false;
}


function openGiftPreviewWindow(photoid,item,invid,desc) {	
	var url = "GiftStorePreview.jsp?photoid=" + photoid
    + "&item=" + item
	+ "&desc=" + desc
	+ "&invid=" +invid
    + "&rand=" + Math.floor(Math.random() * 9999999) 
	+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes"
	openCenteredWindow(url, 535, 525, params);

	return false;
}

function openGiftPreviewWindowwithcolor(photoid,item,invid,desc,colorid) {	
	var url = "GiftStorePreview.jsp?photoid=" + photoid
    + "&item=" + item
	+ "&desc=" + desc
	+"&invid=" + invid
	+"&color=" + colorid	
    + "&rand=" + Math.floor(Math.random() * 9999999) 
	+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes"
	openCenteredWindow(url, 535, 525, params);

	return false;
}

function openMiniBookPreviewWindow(itemid) {

	var url = "PreviewMiniBookCart.jsp?itemid=" + itemid
    + "&rand=" + Math.floor(Math.random() * 9999999) 
	+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes" 

	openCenteredWindow(url, 535, 525, params);

	return false;
}

function openSDcardPreviewWindow(itemid) {

	var url = "PreviewSDcardCart.jsp?itemid=" + itemid
    + "&rand=" + Math.floor(Math.random() * 9999999) 
	+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes" 

	openCenteredWindow(url, 535, 660, params);

	return false;
}

function openComboDPFPreviewWindow(itemid) {

	var url = "PreviewComboDPFCart.jsp?itemid=" + itemid
    + "&rand=" + Math.floor(Math.random() * 9999999) 
	+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes" 

	openCenteredWindow(url, 535, 660, params);

	return false;
}

function openFoldedCardPreviewWindow(projectid) {
	var url = "EnlargedCardReview.action?projectid=" + projectid
			+ "&pageIndex=0&edit=false&rand=" + Math.floor(Math.random() * 9999999)
			+ "&" + gST;
	var params = "scrollbars=yes,resizable=yes"

	openCenteredWindow(url, 575, 550, params);
	return false;
}

function showHideLargePrintSizesMacIe(cName) {
	var largeFormatClassName = "largeFormat" + cName;
	var trs = document.getElementsByTagName("tr");

	for (i=0;i<trs.length;i++) {
		if (trs[i].className==largeFormatClassName) {
			if (trs[i].style.display=="none") {
			   trs[i].style.display = "block";
			} else {
			   trs[i].style.display = "none";
			}
		}
	}
	
}

function showHideLargePrintSizesDefault(cName){
	var largeFormatClassName = "largeFormat" + cName;
	var tableName = "tableID" + cName;
	var myTable = document.getElementById(tableName);
	
	var trs = myTable.rows;
	
	for (i=0;i<trs.length;i++){
		if (trs[i].className==largeFormatClassName){
			if (trs[i].style.display=="none"){
			    // get cell contents
				var cells = trs[i].cells;
				var cellContents = new Array();
				var cellClasses = new Array();
				
				for(j=0;j<cells.length;j++){
					cellContents[j] = cells[j].innerHTML;
					cellClasses[j] = cells[j].className;
				}
				
				//recreate row				
				var x = myTable.insertRow(i);
				myTable.deleteRow(i + 1);
				
				//repopulate row contents
				for(j=0;j<cellContents.length;j++){
					var y = x.insertCell(j);
					y.innerHTML = cellContents[j];
					y.className = cellClasses[j];

				}

				x.className =  largeFormatClassName;
			} else {
				trs[i].style.display = "none";
			}
			
		}
	}
}

function showHideLargePrintSizes(cName){
	
	if (is_mac && is_ie) {
		showHideLargePrintSizesMacIe(cName);
	} else {
		showHideLargePrintSizesDefault(cName);
	}
	return false;
}



function showHidePreview(invSize,photoid,path,thumbWidth,thumbHeight,aspectRatio,maxDimension,previewLinkText,flash,ztOn,addToCartParamString, virtualItemId){
	var photoInvID = photoid + "_" + invSize;
	var previewID = "preview_" + photoInvID;
	var previewDiv;

	if (previewDiv = document.getElementById(previewID)) {
		
		var numInput = 0;
		var numPrints = 0;
		var photoTable = document.getElementById("tableID" + photoid);
		var inputs = photoTable.getElementsByTagName("input");
		for (i=0;i<inputs.length;i++){
			if (inputs[i].className == "input_" + photoInvID){
				numInput += parseInt(inputs[i].value);
				if (inputs[i].alt == "print") {
					numPrints += parseInt(inputs[i].value);			
				}
			}
			
		}
		if (numInput > 0) {

			//if we've already drawn the div, skip this and simply redisplay it
			if (previewDiv.innerHTML == "") {
				var previewURL;
				if (previewLinkText != '') {
					// set the quantity if buying
					var re = new RegExp("QUANTITY");
					addToCartParamString = addToCartParamString.replace(re, numInput);
					previewURL = "openEditPrintWindowForDynPhoto(" + photoid + "," + invSize + ",'" + flash + "','" + addToCartParamString + "','" + virtualItemId + "')";
				} else {
					previewURL = "openPrintPreviewWindowInv(" + photoid + "," + invSize + ")";
				}

                
				var imgDiv = document.createElement("div");
				imgDiv.className = "thumb-wrapper";
				imgDiv.style.cssText =  getImageDivStyle(aspectRatio, maxDimension, thumbWidth, thumbHeight);
				
				var imgAnchor = document.createElement("a");
				imgAnchor.setAttribute('href', 'javascript:;');
                imgAnchor.onclick = function() {eval(previewURL)};
				imgDiv.appendChild(imgAnchor);
				
				
				var clipStyle;
				if (ztOn) {
					clipStyle = getStyleTagForZTOn(thumbWidth, thumbHeight, aspectRatio);
				} else {
					clipStyle = getStyleTagForZTOff(thumbWidth, thumbHeight, aspectRatio);
				}

				var img = document.createElement ("img");
				img.src = path;
				img.style.cssText = clipStyle;
				img.className = "clip-thumb";
				img.setAttribute ('id', 'img_' + photoid + "_" + invSize);
				img.width = thumbWidth;
				img.height = thumbHeight;
				imgAnchor.appendChild(img);
				var displayText = document.getElementById("displayText_" + photoInvID).innerHTML;
				var dText = document.createElement("div");
				dText.className = "cart-preview-text";
				dText.innerHTML = displayText;
				var text = document.createElement("div");
				text.className = "cart-preview-text";
				if (previewLinkText != '') {
                    var previewLinkHTML = '<a href="#" onclick="return ' + previewURL + '">' + previewLinkText + '</a>';
                    text.innerHTML = previewLinkHTML;
				}

				var editsText = document.createElement("div");
				editsText.className = "cart-preview-text edits-text";
				editsText.innerHTML = rbs["Edits saved"];
				editsText.id = "edits-text_" + photoInvID;


				var imgShadow = document.createElement("div");
				imgShadow.style.cssText = getImageDivStyle(aspectRatio, maxDimension, thumbWidth, thumbHeight);
				imgShadow.className="thumb-shadow";
				imgShadow.appendChild(imgDiv);

				previewDiv.appendChild(imgShadow);
				previewDiv.appendChild(editsText);
				previewDiv.appendChild(dText);
				previewDiv.appendChild(text);			
				}

			previewDiv.style.display = "block";
			var placeholderId = "placeholder_" + photoid;
			var placeholderDiv;
			if (placeholderDiv = document.getElementById(placeholderId)) {
				placeholderDiv.style.display = "none";
			}
		} else {
			//check for other print sizes for this photo

			previewDiv.style.display = "none";
			//placeholder_div: photoid_placeholder

			if (getQuantForPhoto (photoid) == 0) {
			
				var placeholderId = "placeholder_" + photoid;
				var placeholderDiv;
				if (placeholderDiv = document.getElementById(placeholderId)) {
					
					//if we've already drawn the div, skip this and simply redisplay it
					if (placeholderDiv.innerHTML == "") {
						var imgDiv = document.createElement("div");
						imgDiv.className = "thumb-wrapper";
						imgDiv.style.cssText = "width:" + thumbWidth + "px;height:" + thumbHeight + "px;";
						var img = document.createElement ("img");
						//alert ("point 1");
						img.src = path;
						img.className = "clip-thumb";
						img.setAttribute ("id", "img_" + photoInvID);
						//alert ("point 2");
						imgDiv.appendChild(img);


						var textDiv = document.createElement("div");
						textDiv.innerHTML = rbs["Original Image"];
						textDiv.className = "cart-preview-text";

						var imgShadow = document.createElement("div");
						imgShadow.className="thumb-shadow";
						imgShadow.style.cssText = "width:" + thumbWidth + "px;height:" + thumbHeight + "px;";
						imgShadow.appendChild(imgDiv);

						placeholderDiv.appendChild(imgShadow);
						placeholderDiv.appendChild(textDiv);
					}
					placeholderDiv.style.display = "block";
				}

			}
			//alert ("point 4");
		}
		
	}
		/*added to show low resolution text after quantity is selected */
	if(numPrints==0){			
		var hideresDiv = "lowresText_"+ photoid + "_" + invSize;
		document.getElementById(hideresDiv).style.display = "none";	
	}else{
		var showresDiv = "lowresText_"+ photoid + "_" + invSize;			
		document.getElementById(showresDiv).style.display = "block";
	}

	//express order uncheck
	var express;
	if (express = document.getElementById("express-table")) {
		var check;
		if ((check = document.getElementById("checkbox_singles_" + invSize)) && (numPrints != 1)) {
			check.checked = false;
		}
		if ((check = document.getElementById("checkbox_doubles_" + invSize)) && (numPrints != 2)) {
			check.checked = false;
		}
	}
}


	function getQuantForPhoto (photoid) {

		photoTable = document.getElementById("phototable_" + photoid);
		tableInputs = photoTable.getElementsByTagName('input');

		var totalQuant = 0;

		for (i=0; i < tableInputs.length; i++){
            if (tableInputs[i].getAttribute("type") == "text"){ 
				var quant = tableInputs[i].value * 1;
				totalQuant += quant;
			}
		}

		//alert (totalQuant);
		return totalQuant;
		
	}

	// printAspectRatio is defined as longside/shortside
	function getImageDivStyle (printAspectRatio, maxDimension, imageWidth, imageHeight) {
		var style;
		if (imageWidth <= imageHeight) {
			style = "height:" + maxDimension + "px;width:" + (maxDimension/printAspectRatio) + "px;";
		} else {
			style = "width:" + maxDimension + "px;height:" + (maxDimension/printAspectRatio) + "px;";
		}
		//alert (style);
		return style;

	}

	//already a java method for this?
	function noNeg(number){
	    if (number < 0) {
			return 0;
	    } else {
			return number;
	    }
	}


	function getStylesArrayForZTOff (imageWidth, imageHeight, printAspectRatio) {
		
		var backgroundWidth;
		var backgroundHeight;
		var printThumbWidth;
		var printThumbHeight;

		if ( imageWidth > imageHeight ) { // landscape
			printThumbHeight = imageWidth / printAspectRatio;
			printThumbWidth = imageWidth;

			if ( (imageWidth/imageHeight) > printAspectRatio) {
				backgroundWidth = imageWidth;
				backgroundHeight = imageHeight;
			} else { // background image shrinks
				backgroundWidth = printThumbHeight * (imageWidth / imageHeight);
				backgroundHeight = printThumbHeight;
			}

			//alert("ls: printThumbHeight " + printThumbHeight + ", printThumbWidth " + printThumbWidth);
			//alert("ls: backgroundHeight " + backgroundHeight + ", backgroundWidth " + backgroundWidth);
			//alert("ls: imageHeight " + imageHeight + ", imageWidth " + imageWidth);
		} else { // portrait
			printThumbWidth = imageHeight / printAspectRatio;
			printThumbHeight = imageHeight;

			//alert (imageHeight/imageWidth);
			//alert (printAspectRatio);

			if ( (imageHeight/imageWidth) > printAspectRatio) {
				backgroundWidth = imageWidth;
				backgroundHeight = imageHeight;
			} else { // background image shrinks
				backgroundWidth = printThumbWidth;
				backgroundHeight = printThumbWidth * (imageHeight / imageWidth);
			}

			//alert("portrait: printThumbHeight " + printThumbHeight + ", printThumbWidth " + printThumbWidth);
			//alert("backgroundHeight " + backgroundHeight + ", backgroundWidth " + backgroundWidth);
			//alert("imageHeight " + imageHeight + ", imageWidth " + imageWidth);
		}
		
		var widthCSS = backgroundWidth + "px";
		var widthStyle= new Array('width', widthCSS);
		var heightCSS = backgroundHeight + "px";
		var heightStyle = new Array ('height', heightCSS)

        var top = noNeg((printThumbHeight - backgroundHeight) * .5);
        var left = noNeg((printThumbWidth - backgroundWidth) * .5);

		//alert ('pth' + printThumbHeight);
		//alert ('bgh' + backgroundHeight);
		//alert ('top' + top);

		// reposition the object to the origin
		var topCSS = top + "px";
		var topStyle = new Array ('top', topCSS);

		var leftCSS = left + "px";
		var leftStyle = new Array ('left', leftCSS);

		// This can be either an update or freshly drawing the clipping area... since 
		// already-drawn images may have a clip area set already, we need to reset it
		// for zt off, otherwise it will hide part of the image.  So basically, we're 
		// just setting the clip area to the print thumbnail
		var clipCSS = "rect(" + 0 + "px " + printThumbWidth + "px " + printThumbHeight + "px " + 0 + "px)";;
		var clipStyle = new Array ('clip', clipCSS);

		var stylesArray = new Array (widthStyle, heightStyle, topStyle, leftStyle, clipStyle);
		return stylesArray;
	}
	
	

	function getStylesArrayForZTOn (imageWidth, imageHeight, printAspectRatio, leftCrop, topCrop, rightCrop, bottomCrop){
		//var top, right, bottom, left,aspectRatio,width,height, originalAR; 
		//calculate the original Aspect Ratio for use later    

		//imageWidth = photoManager.getImageWidth();
		//imageHeight = photoManager.getImageHeight();
        imageAspectRatio =  imageWidth / imageHeight;
		
        var printThumbWidth;	
		var printThumbHeight;
		//printAspectRatio = 1.5; // need to get inventory for print, map to aspect ratio

        var top;
		var right;
		var bottom;
		var left;
		var backgroundWidth;
		var backgroundHeight;

		//calculated output height and width
		if (imageWidth > imageHeight) { // if landscape
			printThumbWidth = imageWidth;
			printThumbHeight = imageWidth / printAspectRatio;
		} else {
			printThumbHeight = imageHeight;
			printThumbWidth = imageHeight / printAspectRatio;
		}
   
		// if the picture is extra wide or thin and the clipping has to be done on the "opposite" sides, we need to scale the image up. Hence we need originalAR
        backgroundHeight = imageHeight;
		backgroundWidth = imageWidth;
		
		if (printThumbHeight > imageHeight) {
			backgroundHeight = printThumbHeight;
			backgroundWidth = printThumbHeight * imageAspectRatio;
		} 
    
		if (printThumbWidth > imageWidth) {
			backgroundHeight = printThumbWidth / imageAspectRatio;
			backgroundWidth = printThumbWidth;
		}

		var widthCSS = backgroundWidth + "px";
		var widthStyle = new Array ('width', widthCSS);
		
		var heightCSS = backgroundHeight + "px";
		var heightStyle = new Array ('height', heightCSS);
		
        top = (backgroundHeight - printThumbHeight) * topCrop;
        left = (backgroundWidth - printThumbWidth) * leftCrop;
        right = backgroundWidth - ((backgroundWidth - printThumbWidth) * rightCrop);
		bottom = backgroundHeight - ((backgroundHeight - printThumbHeight) * bottomCrop);

		var clipCSS = "rect(" + top + "px " + right + "px " + bottom + "px " + left + "px)";
		var clipStyle = new Array ('clip', clipCSS);

		// reposition the object to the origin
		var topCSS = (0 - top) + "px";
		var topStyle = new Array ('top', topCSS);
		
		var leftCSS = (0 - left) + "px";
		var leftStyle = new Array ('left', leftCSS);
		
		var stylesArray = new Array (widthStyle, heightStyle, clipStyle, leftStyle, topStyle);
		return stylesArray;
	}


	
	function getStylesArrayForFlexAdjust (imageWidth, imageHeight, printAspectRatio, xOffsetFlex, yOffsetFlex, zoomFactor){
		//var top, right, bottom, left,aspectRatio,width,height, originalAR; 
		//calculate the original Aspect Ratio for use later    

		//imageWidth = photoManager.getImageWidth();
		//imageHeight = photoManager.getImageHeight();
        imageAspectRatio =  imageWidth / imageHeight;
		
        var printThumbWidth;	
		var printThumbHeight;

        var top;
		var right;
		var bottom;
		var left;
		var backgroundWidth;
		var backgroundHeight;

		//Flex coords are given in terms of 448px images (max dimension); we're operating on 96px images
		var xOffset = xOffsetFlex * (96/448) * -1;
		var yOffset = yOffsetFlex * (96/448) * -1;

		//alert ("xOffsetFlex: " + xOffsetFlex + ", xOffset: " + xOffset + ", (96/448): " + (96/448) + ", zoomFactor: " + zoomFactor);

		//calculated output height and width
		//Since imageHeight and imageWidth can be pulled from the DOM in such a way that js thinks they're
		//strings rather than numbers, we need to multiply by 1 to make sure printThumbWidth/printThumbHeight
		//are treated like numbers.  Otherwise we end up concatentating them with x/y offset later on
		//and creating really weird bugs.
		if (imageWidth > imageHeight) { // if landscape
			printThumbWidth = imageWidth * 1;
			printThumbHeight = imageWidth / printAspectRatio;
		} else {
			printThumbHeight = imageHeight * 1;
			printThumbWidth = imageHeight / printAspectRatio;
		}
   
		// if the picture is extra wide or thin and the clipping has to be done on the "opposite" sides, we need to scale the image up. Hence we need originalAR
  
		backgroundHeight = imageHeight * zoomFactor;
		backgroundWidth = imageWidth * zoomFactor;

		//alert (imageWidth);
		//alert (zoomFactor);
		//alert (zoomFactor):
		//alert (backgroundWidth);
		
		var widthCSS = backgroundWidth + 'px';
		var heightCSS = backgroundHeight + 'px';
		//alert (heightCSS);
		var widthStyle = new Array ('width', widthCSS);
		var heightStyle = new Array ('height', heightCSS);

		// calculate the actual area to be clipped. noNeg returns 0 if the value is negative    
		top = noNeg(yOffset);
        left = noNeg(xOffset);
        right = noNeg(printThumbWidth + xOffset);
		bottom = noNeg(printThumbHeight + yOffset);

		var clipCSS = 'rect(' + top + 'px ' + right + 'px ' + bottom + 'px ' + left + 'px)';
		var clipStyle = new Array ('clip', clipCSS);

		// reposition the object to the origin
		var leftCSS = (0 - left) + "px";
		var leftStyle = new Array ('left', leftCSS);

		var topCSS = (0 - top) + "px";
		var topStyle = new Array ('top', topCSS);

		var stylesArray = new Array (clipStyle, widthStyle, heightStyle, leftStyle, topStyle);
		return stylesArray;
	}


	function getStyleTagForZTOff (imageWidth, imageHeight, printAspectRatio) {
		return convertStyleArrayToTag (getStylesArrayForZTOff(imageWidth, imageHeight, printAspectRatio));
	}


	function getStyleTagForFlexAdjust (id, imageWidth, imageHeight, printAspectRatio, xOffsetFlex, yOffsetFlex, zoomFactor){
		return convertStyleArrayToTag (getStylesArrayForFlexAdjust (id, imageWidth, imageHeight, printAspectRatio, xOffsetFlex, yOffsetFlex, zoomFactor));
	}


	function getStyleTagForZTOn(imageWidth, imageHeight, printAspectRatio){
		return convertStyleArrayToTag (getStylesArrayForZTOn(imageWidth, imageHeight, printAspectRatio, .5, .2, .5, .8));
	}

    
	function setCSSClipForFlexAdjust (photoInvID, printAspectRatio, xOffsetFlex, yOffsetFlex, zoomFactor, itemid, rezWarning) {

		//lets the app know the dynamic photo was created
		photoGenerated[photoInvID] = itemid;

		var imgID = "img_" + photoInvID;
		var imgElement;
		imgElement = document.getElementById(imgID);
		// do i need to instantiate imgElement twice?
		
		var dimensions = getWidthHeightForSM (imgElement);
		var stylesArray = getStylesArrayForFlexAdjust(dimensions[0], dimensions[1], printAspectRatio, xOffsetFlex, yOffsetFlex, zoomFactor);
		if (imgElement.src.indexOf("SM") != 0) {
    			var re = new RegExp("SM");
    			var imgUrl = new String(imgElement.src);
    			imgUrl = imgUrl.replace(re, "ALB");
    			imgElement.src = imgUrl;
    		}
		if (rezWarning != null) {
			document.getElementById("edits-text_" + photoInvID).style.display = "block";
		}
		
		setItemPreviewStyle (photoInvID, stylesArray);

		if (rezWarning == true) {
			document.getElementById("not_recommended_" + photoInvID).style.display="inline";
		} else if (rezWarning == false) {
			document.getElementById("not_recommended_" + photoInvID).style.display="none";
		}

	}

	function setCSSClipForZTOn (photoInvID, printAspectRatio, itemid){
		

		//lets the app know the dynamic photo was created
		photoGenerated[photoInvID] = itemid;

		var imgID = "img_" + photoInvID;
		var imgElement;
		imgElement = document.getElementById(imgID);
		var dimensions = getWidthHeightForSM (imgElement);
		var stylesArray = getStylesArrayForZTOn(dimensions[0], dimensions[1], printAspectRatio, .5, .2, .5, .8);
		setItemPreviewStyle (photoInvID, stylesArray);

	}

	function setCSSClipForZTOff(photoInvID, printAspectRatio, itemid, rezWarning){
		
		
		//lets the app know the dynamic photo was created
		photoGenerated[photoInvID] = itemid;

		var imgID = "img_" + photoInvID;
		var imgElement;
		imgElement = document.getElementById(imgID);
		var dimensions = getWidthHeightForSM (imgElement);
		var stylesArray = getStylesArrayForZTOff(dimensions[0], dimensions[1], printAspectRatio);
		//alert ('params for getStylesArrayForZTOff: ' + imgElement.getAttribute('width') + ' ' + imgElement.getAttribute('height') + ' ' + printAspectRatio);
		setItemPreviewStyle (photoInvID, stylesArray);

		if (rezWarning == true) {
			document.getElementById("not_recommended_" + photoInvID).style.display="inline";
		} else if (rezWarning == false) {
			document.getElementById("not_recommended_" + photoInvID).style.display="none";
		}

	}

	function resetItemId(itemid, virtualItemId) {
		//set virtual to real
		var idInput, oldInput;
		if (idInput = document.getElementById("ID_" + virtualItemId)) {
			idInput.name = "ID_" + itemid;
			idInput.id = "ID_" + itemid;
		}
		
		if (oldInput = document.getElementById("OLD_" + virtualItemId)) {
			oldInput.name = "OLD_" + itemid;
			oldInput.id = "OLD_" + itemid;
		}
	}

	function getWidthHeightForSM (imgElement) {
		var imgWidth = imgElement.width;
		var imgHeight = imgElement.height;
		if (imgWidth < imgHeight) {
			imgWidth = 96 * imgWidth / imgHeight;
			imgHeight = 96;
		} else {
			imgHeight = 96 * imgHeight / imgWidth;
			imgWidth = 96;
		}
		return new Array (imgWidth, imgHeight);
	}				 

	// simple helper funciton that returns the long and short dimensions of  
	// a thumbnail for an aspect ratio of the form long/short
	function getLongShortByAR (aspectRatio, maxDimension) {
		return new Array (maxDimension, (maxDimension/aspectRatio));
	}



	function convertStyleArrayToTag (stylesArray) {
		var styleTag = "";
		for (i=0; i < stylesArray.length; i++){
			styleTag += stylesArray[i][0] + ':' + stylesArray[i][1] + ';';
		}
		return styleTag;
	}

	function setItemPreviewStyle (photoInvID, stylesArray) {
		var imgID = "img_" + photoInvID;
		var imgElement;
		imgElement = document.getElementById(imgID);

		for (i=0; i < stylesArray.length; i++){
			eval ('imgElement.style.' + stylesArray[i][0] + ' = "' + stylesArray[i][1] + '";');
		}
	}
	
	function setCartBorder (borderWareid, cartBorderPrefix, parentDivId, borderWareName) {
		parentDiv = document.getElementById(parentDivId);
		borderInputs = parentDiv.getElementsByTagName('input');
		borderSelectedCheckbox = document.getElementById(cartBorderPrefix + "_" + borderWareid);
		borderSelectedValue = borderSelectedCheckbox.checked;

		for (i=0; i < borderInputs.length; i++){
            if (borderInputs[i].checked && borderInputs[i] != borderSelectedCheckbox) {
				borderInputs[i].checked = false;
			}
		}
		
		cartBorderSelectedInput = document.getElementById(cartBorderPrefix);
		cartBorderSelectedInputWareName = document.getElementById(cartBorderPrefix + "_warename");
        
		if (borderSelectedValue) {
			cartBorderSelectedInput.value = borderWareid;
			cartBorderSelectedInputWareName.value = borderWareName;
		} else {
			cartBorderSelectedInput.value = -1;
			cartBorderSelectedInputWareName.value = -1;
		}
	}

	function stripAnonymousFunction(val){
		
		var regExp1 = new RegExp("function", "");
		var regExp2 = /anonymous\(\)/;
		var regExp3 = new RegExp("{","");
		var regExp4 = new RegExp("}","");
		
		var res = new String(val);
		res = res.replace(regExp1, "");
		res = res.replace(regExp2, "");
		res = res.replace(regExp3, "");
		res = res.replace(regExp4, "");

		return trim(res);
	}

		function isChecked (printSize, printQuantity) {
			var returnValue = false;
		
			if (printQuantity == 1) {
				eval ("returnValue = document.ofoto_viewcartform.singles" + printSize + ".checked;");
			} else if (printQuantity == 2) {
				eval ("returnValue = document.ofoto_viewcartform.doubles" + printSize + ".checked;");
			}

			return returnValue;
		}
		
	
		function applySelection(printSize, printQuantity) {

			var message = document.getElementById("please-wait-message");
			message.style.display = "block";
			var changeViewState = true;
			var AlreadyChecked = isChecked(printSize, printQuantity);
			if (AlreadyChecked == true) {
				changeViewState = !disableTwin(printSize, printQuantity);
			} else {
				printQuantity = 0;
			}
			setTimeout("applySelectionTimeOut('" + printSize 
                                                             + "','" 
                                                             + printQuantity 
                                                             + "'," + changeViewState
                         				     +")",1);
		}

		function generateFrameEdits(){
			var params,action;
			for (var i=0; i < frameGenerated.length;i++){
				params = "";
				for (var j=0; j < frameGenerated[i].length-1;j++) {
					params += "\"" + frameGenerated[i][j] + "\",";
				}
				params += frameGenerated[i][frameGenerated[i].length-1];
				action = "setCSSClipForFlexAdjust(" + params + ");";
				eval(action);
			}				
	}

		function displayCartSigninDialog(checkboxId, dialogId){
            		var checkbox = document.getElementById(checkboxId);
            		var dialog = document.getElementById(dialogId);

            		if (dialog.style.display != "block") {
                		dialog.style.display = "block";
                		checkbox.checked = true;
            		} else {
                		dialog.style.display = "none";
                		checkbox.checked = false;
            		}
        	}

		function synchronizeSameDayPickup(thisBoxId,otherBoxId) {
			var boxToSynch = document.getElementById(otherBoxId);
			var boxToCompare = document.getElementById(thisBoxId);

			boxToSynch.checked = boxToCompare.checked;	

		}

