var separator = ";\n\r";
var formats = Array('9x13','10x15','13x18','20x30','30x45');
var maxFormatCount = 3;

var imageUploader1 = null;

//This class represents an information about ordered print format.
function Format(name, amount){
	this.name = name; //Print format name
	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].substring(0,arrFormat[1].indexOf('б'))));
		}
	}
	return formats;
}

function fullPageLoad(){
	imageUploader1=getImageUploader("ImageUploader1");
  return;
}

//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.
		var formats = new Array();
    var gallery = 0;

		for (var i = 0; i < maxFormatCount; i++){
			formats.push(new Format("", ""));
		}

		//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)){
			  var description = new String(imageUploader1.getUploadFileDescription(i));
			  description = description==''?'10x15 - 1бр':description;

				var currentFormats = parseDescription(description);
        var currentGallery = description.indexOf('галерия')==-1?0:1;

				//For the first selected item just copy the current format...
				if (selectedFileIndex == null){
					selectedFileIndex = i;
					for (var j = 0; j < currentFormats.length; j++){
						formats[j] = currentFormats[j];
					}

				  gallery = currentGallery;
				}
				//...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"
					for (var j = 0; j < currentFormats.length; j++){
						if (currentFormats[j].name != formats[j].name){
							formats[j].name = "";
						}
						if (currentFormats[j].amount != formats[j].amount){
							formats[j].amount = "";
						}
					}

					for (var j = currentFormats.length; j < maxFormatCount; j++){
						formats[j].name = "";
						formats[j].amount = "";
					}
				}
			}
		}

		//If none files selected (i.e. selectedFileIndex is null), disable HTML form fields
		//which edit print formats.
		var disabled = selectedFileIndex == null;

		var aSelectFormat = new Array();
		var aInputAmount = new Array();

		for (var i = 1; i <= maxFormatCount; i++){
			aSelectFormat[i] = document.getElementById("SelectFormat" + i);
			aSelectFormat[i].disabled = disabled;
			aInputAmount[i] = document.getElementById("InputAmount" + i);
			aInputAmount[i].disabled = disabled;
			aInputGallery = document.getElementById("Gallery");
			aInputGallery.disabled = disabled;
		}

		if (selectedFileIndex){
			//Display format data in those fields which edit the same format settings.
			for (var i = 1; i <= maxFormatCount; i++){
				for (var j = 0; j < aSelectFormat[i].options.length; j++){
					if (aSelectFormat[i].options[j].value == formats[i- 1].name){
						aSelectFormat[i].selectedIndex = j;
					}
				}
				aInputAmount[i].value = formats[i - 1].amount;
			}
		  aInputGallery.checked = gallery;
		}
	}
}

//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 = "";
	  var InputGallery = document.getElementById("Gallery");
    var gallery = InputGallery.checked?'галерия':'';

		for (var i = 1; i <= maxFormatCount; i++){
			var SelectFormatN = document.getElementById("SelectFormat" + i);
			var InputAmountN = document.getElementById("InputAmount" + i);

			if (SelectFormatN.selectedIndex != 0 && InputAmountN.value == ""){
			  InputAmountN.value = "1";
			}else if (SelectFormatN.selectedIndex == 0 && InputAmountN.value != ""){
			  InputAmountN.value = "";
			}

			var amount = parseInt(InputAmountN.value);

			if (SelectFormatN.selectedIndex != 0 && InputAmountN.value != "" && amount){
				var format = SelectFormatN.options[SelectFormatN.selectedIndex].value;

				if (description != ""){
					description = description + separator;
				}
				description = description + format + " - " + amount + "бр";
			}
		}
    description += ' ' + gallery;

		//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 ImageUploaderID_UploadFileCountChange(){
  if (imageUploader1) {
    for (var i = 1; i <= imageUploader1.getUploadFileCount(); i++){
      description = imageUploader1.getUploadFileDescription(i);
      if(description == ''){
        imageUploader1.setUploadFileDescription(i, '10x15 - 1бр');
      }
    }
  }
}

function ImageUploaderID_BeforeUpload() {
  for (var i = 1; i <= imageUploader1.getUploadFileCount(); i++){
    description = imageUploader1.getUploadFileDescription(i);
    if(description != ''){
      var currentFormats = parseDescription(new String(description));
      var currentGallery = new String(description).indexOf('галерия')==-1?0:1;
      imageUploader1.AddField('SourceFile_'+i+'[gallery]', currentGallery);
      for (var j = 0; j < currentFormats.length; j++){
				imageUploader1.AddField('SourceFile_'+i+'[format]['+j+']', currentFormats[j].name);
				imageUploader1.AddField('SourceFile_'+i+'[count]['+j+']', currentFormats[j].amount);
				for(format in formats){
  				if(currentFormats[j].name == formats[format]){
  				  formatId = formats[format];
  				  break;
  				}
  			}
				order.list[format] = Number(order.list[format]) + Number(currentFormats[j].amount);
			}
    }
  }
}

function prepare_order() {
  order.list = [0,0,0,0,0];
  for (var i = 1; i <= imageUploader1.getUploadFileCount(); i++){
    description = imageUploader1.getUploadFileDescription(i);
    if(description != ''){
      var currentFormats = parseDescription(new String(description));
      for (var j = 0; j < currentFormats.length; j++){
				for(format in formats){
  				if(currentFormats[j].name == formats[format]){
  				  formatId = formats[format];
  				  break;
  				}
  			}
				order.list[format] = Number(order.list[format]) + Number(currentFormats[j].amount);
			}
    }
  }
}