function showAdjustCroppingLink(item_id,photo_id,crop) {
	//if(crop == 'Fit') { closePopup(); }
	document.getElementById('adjust_crop_link_'+item_id).style.visibility = (crop == 'Crop')?'visible':'hidden';
}
function showAdjustCroppingDivPop(item_id,photo_id,product_id,winW,winH,form_name) {
	form = eval('document.'+form_name);
	crop_action = eval('form.crop_action_'+item_id+'.value');
	crop_x = eval('form.crop_x_'+item_id+'.value');
	crop_y = eval('form.crop_y_'+item_id+'.value');
	crop_w = eval('form.crop_w_'+item_id+'.value');
	crop_h = eval('form.crop_h_'+item_id+'.value');
	crop_offset = eval('form.crop_offset_'+item_id+'.value');
	palette = eval('form.palette_'+item_id+'.value');
	border_id = eval('form.border_id_'+item_id+'.value');
	openPopup('/includes/modules/custom_cropping_new.php?action='+crop_action+'&size=l&photo_id='+photo_id+'&product_id='+product_id+'&item_id='+item_id+'&form_name='+form_name+'&crop_x='+crop_x+'&crop_y='+crop_y+'&crop_w='+crop_w+'&crop_h='+crop_h+'&crop_offset='+crop_offset+'&palette='+palette+'&border_id='+border_id+'',winW,winH)
}
function initCroppingPreview(item_id,pid,form,editmode,showlarge,moveToX,moveToY) {
	photoWidth[item_id] =  parseFloat($('photo_width_'+item_id).value );
	photoHeight[item_id] = parseFloat($('photo_height_'+item_id).value);
	thumbWidth[item_id] =  parseFloat($('thumb_width_' + item_id).value );
	thumbHeight[item_id] = parseFloat($('thumb_height_' + item_id).value);
	imgRotated[item_id] =  eval('imgRotated_'+item_id);
	photoURL[item_id] =    eval('photoURL_'+item_id);
	if(imgRotated[item_id] == 1) {
		arr = genSwapImgDim(photoWidth[item_id],photoHeight[item_id]);
		photoWidth[item_id] = arr[0];
		photoHeight[item_id] = arr[1];
	}
	photoIsPortrait[item_id] = (photoHeight[item_id] > photoWidth[item_id])?1:0;

	oRatio[item_id] = 1;
	tRatio[item_id] = thumbWidth[item_id] / photoWidth[item_id];
	
	thumb_max_fit[item_id] = thumb_max - (fitBorder * 2);
	
	changeProduct(item_id,pid,form,null,editmode,showlarge);
}
function changeProduct(item_id,pid,form,onchange,editmode,showlarge) {
	canvas_product_id = 0;
	for(canvas_pid in a_canvas_products) {
		canvas_specs = a_canvas_products[canvas_pid];
		if(canvas_pid == pid) {
			canvas_product_id = pid;
		}
	}
	
	productMinWidth[item_id] = productsArr[pid][0];
	productMinHeight[item_id] = productsArr[pid][1];
	
	if(photoIsPortrait[item_id] && productMinWidth[item_id] > productMinHeight[item_id]) {
		arr = genSwapImgDim(productMinWidth[item_id],productMinHeight[item_id]);
		productMinWidth[item_id] = arr[0];
		productMinHeight[item_id] = arr[1];
	}
	cropWidth[item_id] = productMinWidth[item_id];
	cropHeight[item_id] = productMinHeight[item_id];
	arr = genResizeImgDim(cropWidth[item_id],cropHeight[item_id],photoWidth[item_id],photoHeight[item_id],1);
	cropWidth[item_id] = arr[0];
	cropHeight[item_id] = arr[1];
	arr = genResizeImgDim(cropWidth[item_id],cropHeight[item_id],photoWidth[item_id],photoHeight[item_id]);
	cropWidth[item_id] = arr[0];
	cropHeight[item_id] = arr[1];
		
	if(!showlarge) {
		Crop_specs = eval('Crop_specs_'+item_id);
		crop_x = (Crop_specs && !onchange)?Crop_specs[0]:((photoWidth[item_id] - cropWidth[item_id]) / 2);
		crop_y = (Crop_specs && !onchange)?Crop_specs[1]:((photoHeight[item_id] - cropHeight[item_id]) / 2);
		crop_w = (Crop_specs && !onchange)?Crop_specs[2]:cropWidth[item_id];
		crop_h = (Crop_specs && !onchange)?Crop_specs[3]:cropHeight[item_id];
		//~ if(crop_w > 100 || crop_h > 100) {
			//~ //crop_x = Math.round((crop_x / photoWidth[item_id] ) * 100);
			//~ //crop_y = Math.round((crop_y / photoHeight[item_id]) * 100);
			//~ //crop_w = Math.round((crop_w / photoWidth[item_id] ) * 100);
			//~ //crop_h = Math.round((crop_h / photoHeight[item_id]) * 100);
		//~ }
		crop_xObj = eval('form.crop_x_'+item_id);
		crop_yObj = eval('form.crop_y_'+item_id);
		crop_wObj = eval('form.crop_w_'+item_id);
		crop_hObj = eval('form.crop_h_'+item_id);
		crop_xObj.value = Math.round(crop_x);
		crop_yObj.value = Math.round(crop_y);
		crop_wObj.value = Math.round(crop_w);
		crop_hObj.value = Math.round(crop_h);
	}
	updateCroppingPreview(item_id,form,editmode,showlarge)
}
function changeCroppingType(item_id,photo_id,cropping,form,editmode,showlarge) {
	showAdjustCroppingLink(item_id,photo_id,cropping);
	updateCroppingPreview(item_id,form,editmode,showlarge);
}
function updateCroppingPreview(item_id,form,editmode,showlarge) {
	crop_wObj = eval('form.crop_w_'+item_id);
	crop_hObj = eval('form.crop_h_'+item_id);
	crop_xObj = eval('form.crop_x_'+item_id);
	crop_yObj = eval('form.crop_y_'+item_id);
	crop_offsetObj = eval('form.crop_offset_'+item_id);
	
	crop_w = crop_wObj.value * tRatio[item_id];
	crop_h = crop_hObj.value * tRatio[item_id];
	crop_x = crop_xObj.value * tRatio[item_id];
	crop_y = crop_yObj.value * tRatio[item_id];
	crop_offset = crop_offsetObj.value * tRatio[item_id];
	if(crop_offset) {
		crop_x += crop_offset;
		crop_y += crop_offset;
		crop_w -= (crop_offset * 2);
		crop_h -= (crop_offset * 2);
	}

	crop_typeObj = eval('form.crop_'+item_id);
	crop_type = crop_typeObj.options[crop_typeObj.options.selectedIndex].value;

	crop_preview_Obj = document.getElementById('crop_preview_'+item_id);
	crop_area_preview_Obj = document.getElementById('crop_area_preview_'+item_id);
	fit_preview_Obj = document.getElementById('fit_preview_'+item_id);
	fit_photo_preview_Obj = document.getElementById('fit_photo_preview_'+item_id);

	if(crop_preview_Obj) { crop_preview_Obj.style.visibility = (crop_type == 'Crop')?'visible':'hidden'; }
	if(fit_preview_Obj) { fit_preview_Obj.style.visibility = (crop_type == 'Fit')?'visible':'hidden'; }

	
	if(crop_type == 'Crop') {
		crop_preview_Obj.style.width = photoWidth[item_id] * tRatio[item_id];
		crop_preview_Obj.style.height = photoHeight[item_id] * tRatio[item_id];
		if(!showlarge) {
			crop_w = crop_w / oRatio[item_id];
			crop_h = crop_h / oRatio[item_id];
			crop_x = crop_x / oRatio[item_id];
			crop_y = crop_y / oRatio[item_id];
    	
			// Fix div pos/dim to display crop border precisely
			crop_w -= (document.all)?0:cropBorder;
			crop_h -= (document.all)?0:cropBorder;
			crop_x -= cropBorder / 2;
			crop_y -= cropBorder / 2;
			// End fix
    	
			crop_preview_Obj.style.background = 'url(\''+eval('photoURL_'+item_id)+'\') no-repeat';
			if(crop_offset && false) {
				crop_area_preview_Obj.style.width = canvas_crop_w+'px';
				crop_area_preview_Obj.style.height = canvas_crop_h+'px';
				crop_area_preview_Obj.style.top = canvas_crop_y+'px';
				crop_area_preview_Obj.style.left = canvas_crop_x+'px';
			}
			else {
				crop_area_preview_Obj.style.width = crop_w+'px';
				crop_area_preview_Obj.style.height = crop_h+'px';
				crop_area_preview_Obj.style.top = crop_y+'px';
				crop_area_preview_Obj.style.left = crop_x+'px';
			}
		}
	}
	else {
		if(!showlarge || true) {
			output = '';

			output += 'Item ID: '+item_id+'<br />\n';
			productWidth = productMinWidth[item_id];
			productHeight = productMinHeight[item_id];
			output += 'Product: '+productWidth+'x'+productHeight+'<br />\n';

			
			arr = genResizeImgDim(productWidth,productHeight,photoWidth[item_id],photoHeight[item_id]);
			productWidth = arr[0];
			productHeight = arr[1];
			output += 'Product (resized): '+productWidth+'x'+productHeight+'<br />\n';
			arr = genResizeImgDim(productWidth,productHeight,photoWidth[item_id],photoHeight[item_id],1);
			productWidth = arr[0];
			productHeight = arr[1];
			output += 'Product (resized up): '+productWidth+'x'+productHeight+'<br />\n';
			product_fit_w = Math.round(productWidth  * tRatio[item_id]);
			product_fit_h = Math.round(productHeight * tRatio[item_id]);
			output += 'Product fit div: '+product_fit_w+'x'+product_fit_h+'<br />\n';

			
			output += 'Photo: '+photoWidth[item_id]+'x'+photoHeight[item_id]+'<br />\n';

			output += 'Photo: '+photoWidth[item_id]+'x'+photoHeight[item_id]+'<br />\n';
			fit_w = productWidth - (fitBorder * 2) ;
			fit_h = productHeight - (fitBorder * 2) ;
			fit_x = (fit_w - photoWidth[item_id])/2;
			fit_y = (fit_h - photoHeight[item_id])/2;
			output += 'Fit div: '+fit_w+'x'+fit_h+'+'+fit_x+'+'+fit_y+'<br />\n';
			fit_w = Math.round(fit_w * tRatio[item_id]);
			fit_h = Math.round(fit_h * tRatio[item_id]);
			fit_x = Math.round(fit_x * tRatio[item_id]);
			fit_y = Math.round(fit_y * tRatio[item_id]);
			output += 'Fit div: '+fit_w+'x'+fit_h+'+'+fit_x+'+'+fit_y+'<br />\n';
			if(
				navigator.userAgent.toLowerCase().indexOf('msie') != -1 // include IE
				&& !(navigator.userAgent.toLowerCase().indexOf('opera') != -1) // exclude Opera posing as IE
			) {

				fit_w+=2;
				fit_h+=2;
			}
			if(fit_x==1&&fit_y==0) {
				output += 'Fix w +1'+'<br />\n';
				fit_x=0;
				fit_y=0;
				fit_w++;
			}
			if(fit_x==0&&fit_y==1) {
				fit_x=0;
				fit_y=0;
				fit_h++;
				output += 'Fix h +1'+'<br />\n';
			}
			output += 'Fit div: '+fit_preview_Obj.style.width+' x '+fit_preview_Obj.style.height+' + '+fit_preview_Obj.style.left+' + '+fit_preview_Obj.style.top+'<br />\n';
			output += 'Fit photo div: '+fit_photo_preview_Obj.style.width+' x '+fit_photo_preview_Obj.style.height+' + '+fit_photo_preview_Obj.style.left+' + '+fit_photo_preview_Obj.style.top+'<br />\n';
			output += 'Fit div (new): '+fit_w+'px x '+fit_h+'px + '+fit_x+'px + '+fit_y+'px<br />\n';

			fit_preview_Obj.style.width  = fit_w+'px';
			fit_preview_Obj.style.height = fit_h+'px';
			fit_photo_preview_Obj.style.left   = fit_x+'px';
			fit_photo_preview_Obj.style.top    = fit_y+'px';

			output += 'Fit div: '+fit_preview_Obj.style.width+' x '+fit_preview_Obj.style.height+' + '+fit_preview_Obj.style.left+' + '+fit_preview_Obj.style.top+'<br />\n';
			output += 'Fit photo div: '+fit_photo_preview_Obj.style.width+' x '+fit_photo_preview_Obj.style.height+' + '+fit_photo_preview_Obj.style.left+' + '+fit_photo_preview_Obj.style.top+'<br />\n';
			
			fit_photo_preview_Obj.style.background = 'url(\''+eval('photoURL_'+item_id)+'\') no-repeat';
			
			//alert(output);
		}
	}
}
function cartDivPopCallBack(item_id,form,editmode,showlarge) {
	toggleProcessingWindow(1);
	updateCroppingPreview(item_id,form,editmode,showlarge);
	toggleProcessingWindow(0);
}

function copyToClipboard(cmd) {
	prompt('Command: ', cmd);
}

function isValidName(s) {
	ok = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.1234567890#();:, \'';
	strings = new Array;
	count = 0;
	for(i=0; i < s.length ;i++) {
		if(ok.indexOf(s.charAt(i))<0){ invalid_char = s.charAt(i); return (false); }
		else if(s.charAt(i) == ',') { count++; }
		else { strings[count] += s.charAt(i); }
	}
	return true;
}
function isValidZip(s) {
	ok = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.1234567890 ';
	strings = new Array;
	count = 0;
	for(i=0; i < s.length ;i++) {
		if(ok.indexOf(s.charAt(i))<0){ invalid_char = s.charAt(i); return (false); }
		else if(s.charAt(i) == ',') { count++; }
		else { strings[count] += s.charAt(i); }
	}
	return true;
}
function isValidEmail(e) {
	ok = '1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM, ';
	emails = new Array;
	count = 0;
	for(i=0; i < e.length ;i++) {
		if(ok.indexOf(e.charAt(i))<0){ invalid_char = e.charAt(i); return (false); }
		else if(e.charAt(i) == ',') { count++; }
		else { emails[count] += e.charAt(i); }
	}
	for(i=0; i < emails.length; i++) {
		e = emails[i];
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@\$)|(\.\$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)\$/;
		if (!e.match(re) && e.match(re_two)) { return false; }
	}
	return true;
}
function isValidPhone(s) {
	ok = '-.1234567890+() ';
	strings = new Array;
	count = 0;
	for(i=0; i < s.length ;i++) {
		if(ok.indexOf(s.charAt(i))<0){ invalid_char = s.charAt(i); return false; }
		else if(s.charAt(i) == ',') { count++; }
		else { strings[count] += s.charAt(i); }
	}
	return true;
}
function copyBillingAddr(form) {
	form.sa_f_name.value = form.ba_f_name.value;
	form.sa_l_name.value = form.ba_l_name.value;
	form.sa_address1.value = form.ba_address1.value;
	form.sa_address2.value = form.ba_address2.value;
	form.sa_city.value = form.ba_city.value;
	form.sa_country.options.selectedIndex = form.ba_country.options.selectedIndex;
	loadStates(form,'span_sa_state','sa_country','sa_state',(
		(form.ba_state.options)?
			form.ba_state.options[form.ba_state.options.selectedIndex].value
			:form.ba_state.value
	));
	form.sa_zip.value = form.ba_zip.value;
	form.sa_email.value = form.ba_email.value;
	form.sa_phone.value = form.ba_phone.value;
}

var photoWidth = new Array();
var photoHeight = new Array();
var thumbWidth = new Array();
var thumbHeight = new Array();
var productMinWidth = new Array();
var productMinHeight = new Array();
var cropWidth = new Array();
var cropHeight = new Array();
var imgRotated = new Array();
var photoURL = new Array();
var photoIsPortrait = new Array();
var oRatio = new Array();
var tRatio = new Array();
var thumb_max_fit = new Array();
