function sortSelectedModels() {
	selectObj = document.getElementById('sort');	
	var sortMode = selectObj[selectObj.selectedIndex].value;
	switch(sortMode)
	{
	case "0":		
		sortProducts(sortBySeriesAndScreenSizeHighToLow);
		break;
	case "1":
	 	sortProducts(sortByPriceHighToLow);
	 	break;
	case "2":
	  	sortProducts(sortByPriceLowToHigh);
		break;  
	case "3":
	  	sortProducts(sortByScreenSizeHighToLow);
		break;
	case "4":
	 	sortProducts(sortByScreenSizeLowToHigh);
	  	break;
	case "5":
	  	sortProducts(sortByDate);
	  	break; 
	case "6":
	 	sortProducts(sortByCapacityHighToLow);
	  	break;
	case "7":
	 	sortProducts(sortByCapacityLowToHigh);
	  	break;	  	
	case "8":
	 	sortProducts(sortByAlphabeticalModelNamesLowToHigh);
	  	break;
	case "9":
		sortProducts(sortByMainRecordingMedia);
		break;
	case "10":
		sortProducts(sortByMegapixel);
		break;
	case "11":
		sortProducts(sortByWeight);
		break;
	case "12":
		sortProducts(sortByAudienceBeginnerToAdvanced);
		break;
	case "13":
		sortProducts(sortByAudienceAdvancedToBeginner);
		break;
	case "14":
		sortProducts(sortByMinOpticalFocalLengthLowToHigh);
		break;
	case "15":
		sortProducts(sortByMaxOpticalFocalLengthHighToLow);
		break;
	}
	
	// This operation is needed to recheck already checked products
	// which are unchecked due to a bug of IE6.
	//psCompareButton.makeCheckedSelectedProducts();
	//psCompareButton.showAndHideScroller(productSelector);
}

function sortProducts(sortBy){		
	
	var allProductsAsGrouped = new Array();
	
	//if ($('tvoe-products-with-price').value != "") {
		//var products = $('tvoe-products-with-price').value.split(';');
		//allProducts = products;
	//}
	
	//if ($('tvoe-products-without-price').value != "") {
		//var productsWithNoPrice = $('tvoe-products-without-price').value.split(';');
		//allProducts = allProducts.concat(productsWithNoPrice);
	//}
	
	if ($('tvoe-all-products-as-grouped').value != "") {
		allProductsAsGrouped = $('tvoe-all-products-as-grouped').value.split(';');
	}
	
	var target = 'productContainer';	
	//var targetNoPrice = 'productContainer-NoPrice';
	
	//allProducts.sort(sortBy);
	allProductsAsGrouped.sort(sortBy);
	
	//if(products){
		//productSelector.updateAfterSortForPrice(products);
	//}
	//if(productsWithNoPrice){
		//productSelector.updateAfterSortForNonPrice(productsWithNoPrice);
	//}
	
	if(allProductsAsGrouped){
		productSelector.updateAfterSortAsGrouped(allProductsAsGrouped);
	}
	
	productSelector.showProducts();
	//ProductCompare.init();  //deleted for perfomance problems in IE . in fact no need this call.
	
	// Reclick previously selected colours after each click.
	var colours = $('selectedColours');
	 if(colours != null && colours.value != null && colours.value != '') {
		var selectedColours = colours.value.split(',');
		reClickSelectedColoursFromPalette(selectedColours);
	}
	
}

function genericCompare(a, b) {
    return (b < a) - (a < b);
}

function sortBySeriesAndScreenSizeHighToLow(uniqueIDofA,uniqueIDofB){	
	
	var rankA = productSelector.getProductBoxById(uniqueIDofA).sortSerie;
	var rankB = productSelector.getProductBoxById(uniqueIDofB).sortSerie;
	
	if(rankA!= null && rankB!=null){
		var difference = rankA-rankB ;
		if(difference == 0){
			
			// if rank is the same, then sort by series
			var serieA = productSelector.getProductBoxById(uniqueIDofA).sortSerieText;
			var serieB = productSelector.getProductBoxById(uniqueIDofB).sortSerieText;
			difference = genericCompare(serieA,serieB) ;			
			if(difference == 0){
				return  sortByScreenSizeHighToLow(uniqueIDofA,uniqueIDofB);
			}
		}	
		return difference;
	}	
}

function sortByMainRecordingMedia(uniqueIDofA, uniqueIDofB){
	var rankA = productSelector.getProductBoxById(uniqueIDofA).sortMedia;
	var rankB = productSelector.getProductBoxById(uniqueIDofB).sortMedia;
	
	if(rankA!= null && rankB!=null){
		var difference = rankA-rankB ;
		if(difference == 0){
			return sortByDate(uniqueIDofA, uniqueIDofB);
		}	
		return difference;
	}	
	
}

function sortBySeries(uniqueIDofA,uniqueIDofB){
	
	var rankA = productSelector.getProductBoxById(uniqueIDofA).sortSerie;
	var rankB = productSelector.getProductBoxById(uniqueIDofB).sortSerie;
	
	if(rankA!= null && rankB!=null){
	
		var difference = rankA-rankB ;
		if(difference == 0){
			
			// if rank is the same, then sort by series
			var serieA = productSelector.getProductBoxById(uniqueIDofA).sortSerieText;
			var serieB = productSelector.getProductBoxById(uniqueIDofB).sortSerieText;
			difference = genericCompare(serieA,serieB) ;			
			if(difference == 0){
				// if series is the same, then sort by display name
				var displayNameA = productSelector.getProductBoxById(uniqueIDofA).sortName;
				var displayNameB = productSelector.getProductBoxById(uniqueIDofB).sortName;
				difference = genericCompare(displayNameA,displayNameB) ;
			}
		}	
		return difference;
	}	
}

function sortByScreenSizeLowToHigh(uniqueIDofA,uniqueIDofB){
	
	var sizeA = productSelector.getProductBoxById(uniqueIDofA).sortScreen;
	var sizeB = productSelector.getProductBoxById(uniqueIDofB).sortScreen;
	
	var difference = sizeA-sizeB;
	if(difference == 0){
		return sortByDate(uniqueIDofA,uniqueIDofB);
	}
	return difference;
}

function sortByScreenSizeHighToLow(uniqueIDofA,uniqueIDofB){	
	
	var sizeA = productSelector.getProductBoxById(uniqueIDofA).sortScreen;
	var sizeB = productSelector.getProductBoxById(uniqueIDofB).sortScreen;
	
	var difference = sizeB-sizeA;
	if(difference == 0){
		return sortByDate(uniqueIDofA,uniqueIDofB);
	}	
	return difference;
}

function sortByPriceLowToHigh(uniqueIDofA,uniqueIDofB){	
	
	var priceA = productSelector.getProductBoxById(uniqueIDofA).sortPrice;
	var priceB = productSelector.getProductBoxById(uniqueIDofB).sortPrice;	
	
	if (priceA == null && priceB != null)
		return 1;
	
	if (priceB == null && priceA != null)
		return -1;
	
	if(priceA != null && priceB != null){	
		var difference = priceA-priceB;
		if(difference == 0){
			return sortByDate(uniqueIDofA,uniqueIDofB);
		}
		return difference;
	}	
	return sortByDate(uniqueIDofA,uniqueIDofB);// if the product has no price definition, we have to sort by date
}

function sortByPriceHighToLow(uniqueIDofA,uniqueIDofB){	
	
	var priceA = productSelector.getProductBoxById(uniqueIDofA).sortPrice;
	var priceB = productSelector.getProductBoxById(uniqueIDofB).sortPrice;	

	if (priceA == null && priceB != null)
		return 1;
	
	if (priceB == null && priceA != null)
		return -1;
		
	if(priceA != null && priceB != null){	
		var difference = priceB-priceA;
		if(difference == 0){
			return sortByDate(uniqueIDofA,uniqueIDofB);
		}	
		return difference;
	}	
	return sortByDate(uniqueIDofA,uniqueIDofB);// if the product has no price definition, we have to sort by date
}

function sortByDate(uniqueIDofA,uniqueIDofB){	
	
	var dateA = productSelector.getProductBoxById(uniqueIDofA).sortDate;
	var dateB = productSelector.getProductBoxById(uniqueIDofB).sortDate;
	
	return dateB-dateA;
}

function sortByCapacityLowToHigh(uniqueIDofA,uniqueIDofB){	
	
	var capacityA = productSelector.getProductBoxById(uniqueIDofA).sortCapacity;
	var capacityB = productSelector.getProductBoxById(uniqueIDofB).sortCapacity;	

	if(capacityA != null && capacityB != null){		
		var difference = capacityA-capacityB;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}
		return difference;
	}	
	return 0;
}

function sortByCapacityHighToLow(uniqueIDofA,uniqueIDofB){	
	
	var capacityA = productSelector.getProductBoxById(uniqueIDofA).sortCapacity;
	var capacityB = productSelector.getProductBoxById(uniqueIDofB).sortCapacity;	

	if(capacityA != null && capacityB != null){		
	
		var difference = capacityB-capacityA;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}	
		return difference;
	}	
	return 0;
}
function sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB){	
		
		var displayNameA = productSelector.getProductBoxById(uniqueIDofA).sortName;
		var displayNameB = productSelector.getProductBoxById(uniqueIDofB).sortName;
		
		var difference = genericCompare(displayNameA,displayNameB) ;
	
		return difference;
	
}
function sortByMegapixel(uniqueIDofA,uniqueIDofB) {

	var megapixelA = productSelector.getProductBoxById(uniqueIDofA).sortMegapixel;
	var megapixelB = productSelector.getProductBoxById(uniqueIDofB).sortMegapixel;
	
	var difference = megapixelB - megapixelA;
	
	return difference;
}
function sortByWeight(uniqueIDofA,uniqueIDofB) {
	var weightA = productSelector.getProductBoxById(uniqueIDofA).sortWeight;
	var weightB = productSelector.getProductBoxById(uniqueIDofB).sortWeight;
	
	var difference = weightA - weightB;
	
	return difference;
}
function sortByAudienceBeginnerToAdvanced(uniqueIDofA,uniqueIDofB){	
	
	var capacityA = productSelector.getProductBoxById(uniqueIDofA).sortAudience;
	var capacityB = productSelector.getProductBoxById(uniqueIDofB).sortAudience;	

	if(capacityA != null && capacityB != null){		
	
		var difference = capacityA-capacityB;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}	
		return difference;
	}	
	return 0;
}
function sortByAudienceAdvancedToBeginner(uniqueIDofA,uniqueIDofB){	
	
	var capacityA = productSelector.getProductBoxById(uniqueIDofA).sortAudience;
	var capacityB = productSelector.getProductBoxById(uniqueIDofB).sortAudience;	
	
	if(capacityA != null && capacityB != null){		
	
		var difference = capacityB-capacityA;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}	
		return difference;
	}	
	return 0;
}

function sortByMinOpticalFocalLengthLowToHigh(uniqueIDofA,uniqueIDofB){
	var lenghtA = productSelector.getProductBoxById(uniqueIDofA).sortMinFocalLength;
	var lenghtB = productSelector.getProductBoxById(uniqueIDofB).sortMinFocalLength;
	
	if(lenghtA != null && lenghtB != null){
		var difference = lenghtA - lenghtB;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}
		return difference;
	}
	return 0;
}

function sortByMaxOpticalFocalLengthHighToLow(uniqueIDofA,uniqueIDofB){
	var lenghtA = productSelector.getProductBoxById(uniqueIDofA).sortMaxFocalLength;
	var lenghtB = productSelector.getProductBoxById(uniqueIDofB).sortMaxFocalLength;
	
	if(lenghtA != null && lenghtB != null){
		var difference = lenghtB - lenghtA;
		if(difference == 0){
			return sortByAlphabeticalModelNamesLowToHigh(uniqueIDofA,uniqueIDofB);
		}
		return difference;
	}
	return 0;
}


function sortAllProductSkus() {
	
	var list = document.getElementById('sortedProductSkuList');
	
	if (list != null) {
		var items = list.options.length-1;
		var firstText = list.options[0].text;
		
		// create array and make copies of options in list
		var tmpArray = new Array(items);
		
		for (i=0; i<items; i++) 
			tmpArray[i] = new Option(list.options[i+1].text,list.options[i+1].value);
			
		// sort options using given function
		tmpArray.sort(compareOptionText);
		
		// make copies of sorted options back to list
		for (i=0; i<items; i++ )
			list.options[i+1] = new Option(tmpArray[i].text,tmpArray[i].value);
	}
}

function compareOptionText(a,b) {
    return (b.text < a.text) - (a.text < b.text);
}
