
//<![CDATA[		
//Separator for ordered formats in description field of an item.
var separator = "...";    
    
var maxFormatCount = 3;  

var imageUploader1 = null;
function roundNumberr(valor) {
	var numberField = valor; // Field where the number appears
	var rnum = valor;
	var rlength = 2; // The number of decimal places to round to
	var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}
//This class represents an information about ordered print format.
function Format(name, paper, amount){
	this.name = name; //Print format name
	this.paper = paper; //Paper type.
	this.amount = amount; //Number of copies of this format.
}

//This function parses a string displayed as a description of Image Uploader item
//and returns an array of ordered formats.
function parseDescription(description){
	var formats=new Array();
	if (description!=""){		
		var arrDescription = description.split(separator);

		for (var z = 0; z < arrDescription.length; z++){
			arrFormat = arrDescription[z].split(" ");
			formats.push(new Format(arrFormat[0], arrFormat[1], arrFormat[3]));					
		} 
	}	
	return formats;
}

function fullPageLoad(){	
	imageUploader1=getImageUploader("ImageUploader1");
}
//In this SelectionChange event handler we display information about the order for selected
//items if available (print format, amount, paper type). 
function ImageUploader_SelectionChange() {
	if (imageUploader1) {
		var selectedFileIndex = null;

		//This array stores all ordered formats for selected items.

		

		//Iterate all upload list items and get common format information for all of them (i.e.
		//format information which equals for all items).
		for (var i = 1; i <= imageUploader1.getUploadFileCount(); i++){
			if (imageUploader1.getUploadFileSelected(i)){				
				//For the first selected item just copy the current format...
				if (selectedFileIndex == null){
					selectedFileIndex = i;
				}
				//...and for all further selected items verify whether the current format is the same as 
				//for previous items. If yes, use it, otherwise the empty string is used.
				else{
					selectedFileIndex = "multiple"
								
				}
			}	
		}

		//If none files selected (i.e. selectedFileIndex is null), disable HTML form fields 
		//which edit print formats.
		var disabled = selectedFileIndex == null;

		
		var thumbnail1 = getImageUploader("Thumbnail1");
		var a
		
		if (selectedFileIndex){
		
			//Display format data in those fields which edit the same format settings.

			
			//Display selected image preview using <img> tag when only one item is selected. For multiple items blank area is displayed.
			if (typeof(selectedFileIndex) == "number")
			{			
				thumbnail1.setGuid(imageUploader1.getUploadFileGuid(selectedFileIndex));
				var width = imageUploader1.getUploadFileWidth(selectedFileIndex);
				var height = imageUploader1.getUploadFileHeight(selectedFileIndex);
				var size = imageUploader1.getUploadFileSize(selectedFileIndex);
				document.getElementById("fotoSize").innerHTML = roundNumberr(size/1024) + ' KBytes'
				document.getElementById("fotoW").innerHTML = width + ' px'
				document.getElementById("fotoH").innerHTML = height + ' px'

				var imageWidth1 = width
				var imageHeight1 = height

// FOTO 1
	for (var i = 1; i <= 3; i++){
			var formatWidth1 = document.getElementById("maxWidth" + i).value;
			var formatHeight1 = document.getElementById("maxHeight" + i).value;
			if (Math.round(formatWidth1) < Math.round(formatHeight1))
			{
				var formatWidth = formatHeight1;
				var formatHeight = formatWidth1;
			}
			else{
				var formatWidth = formatWidth1;
				var formatHeight = formatHeight1;
			}
			var ar = roundNumberr(formatWidth/formatHeight)
			if (Math.round(imageWidth1) < Math.round(imageHeight1))
			{
				var thirdGlass = imageWidth;
				var imageWidth = imageHeight1;
				var imageHeight = imageWidth1; 
			}
			else{
				var imageWidth = imageWidth1;
				var imageHeight = imageHeight1;
			}
			if (   imageWidth >= formatWidth && imageHeight >= formatHeight)
			{
				document.getElementById("qualidadeImp"+i).src = '../imagens/icons/bullet_verde.png'
				document.getElementById("qualidadeImp"+i).alt = 'Impressão óptima neste formato'
				document.getElementById("descQualidade"+i).innerHTML = 'IDEAL'

			}
			else
			{
				if (   roundNumberr(imageWidth * ar) >= formatWidth && roundNumberr(imageHeight * ar) >= formatHeight)
				{
					document.getElementById("qualidadeImp"+i).src = '../imagens/icons/bullet_amarela.png'
					document.getElementById("qualidadeImp"+i).alt = 'Impressão aceitável neste formato'
					document.getElementById("descQualidade"+i).innerHTML = 'ACEITAVEL'

				}
				else
				{
					document.getElementById("qualidadeImp"+i).src = '../imagens/icons/bullet_vermelha.png'
					document.getElementById("qualidadeImp"+i).alt = 'Impressão de má qualidade neste formato'
					document.getElementById("descQualidade"+i).innerHTML = 'FRACA'
				}
			}
	}
	detalhes.style.display = ''
			}
			else{
				thumbnail1.setGuid("");
				document.getElementById("detalhes").style.display = 'none'
			}
		}
		else{
			thumbnail1.setGuid("");
			document.getElementById("detalhes").style.display = 'none'
		}
	}

}

//This function is called when the user changes some order settings (like format name, paper type, or amount of items).
//Ordered format information is stored in the description field of Image Uploader items.
function itemChange(){
	if (imageUploader1) {
		
		//Compile the string with ordered formats.
		var description = "";
		for (var i = 1; i <= maxFormatCount; i++){
			var SelectFormatN = document.getElementById("SelectFormat" + i);
			var SelectPaperN = document.getElementById("SelectPaper" + i);
			var InputAmountN = document.getElementById("InputAmount" + i);

			var amount = parseInt(InputAmountN.value);
				
			if (SelectFormatN.selectedIndex != 0 && SelectPaperN.selectedIndex != 0 && InputAmountN.value != "" && amount){
				var format = SelectFormatN.options[SelectFormatN.selectedIndex].value;
				var paper = SelectPaperN.options[SelectPaperN.selectedIndex].value;
			
				if (description != ""){
					description = description + separator;
				}
			
				description = description + format + " " + paper + " x " + amount;
			}
		}
			
		//Update desriptions of selected items and write ordered formats information into it. 
		for (var i = 1; i <= imageUploader1.getUploadFileCount(); i++){
			if (imageUploader1.getUploadFileSelected(i)){
				imageUploader1.setUploadFileDescription(i, description);
			}
		}
	}	
}
function ImageUploader_BeforeUpload(){
	var guidIndexHash = getGuidIndexHash();
	
	var UploadPane = document.getElementById("UploadPane");
		
	for (var i = 0; i < UploadPane.childNodes.length; i++){
		var div = UploadPane.childNodes[i];
		
		var index = guidIndexHash[div._guid];		
		
		//Description will be sent as a native Description POST field 
		//provided by Image Uploader.
		imageUploader1.setUploadFileDescription(index, 
			document.getElementById("Description" + div._uniqueId).value);

		//Title will be sent as a custom Title_N POST field, where N is an 
		//index of the file.
		imageUploader1.AddField("Title_" + index, document.getElementById("Title" + div._uniqueId).value);
	}	
}
//]]>

