// JavaScript Document
/**
 * Wine collection Class
 */
function WineCollection() {
	
	this.collection = new Array();
	
	this.getGrapes = function() {
		var returnArray = new Array();
		for(var i=0;i<this.collection.length;i++) {

			for(var j=0;j<this.collection[i].arrayOfGrapes.length;j++) {

				if(!returnArray.contains(this.collection[i].arrayOfGrapes[j].name)) {
					returnArray[returnArray.length]=this.collection[i].arrayOfGrapes[j].name;
				}
			}
			
		}
		return returnArray;
		
	}
	
	this.getCountries = function() {
		var returnArray = new Array();
		for(var i=0;i<this.collection.length;i++) {
			
			if(!returnArray.contains(this.collection[i].country)) {
				returnArray[returnArray.length]=this.collection[i].country;
			}
			
		}
		return returnArray;
	}
	
	this.getColors = function() {
		var returnArray = new Array();
		for(var i=0;i<this.collection.length;i++) {
			
			if(!returnArray.contains(this.collection[i].color)) {
				returnArray[returnArray.length]=this.collection[i].color;
			}
			
		}
		return returnArray;
	}	
	
	this.getSelection = function(aColor, aGrapetype, aCountry, searchwords) {
		// change this to return a collection
		var returnCollection = new WineCollection();

		if(typeof searchwords!='undefined') {
			searchwords=searchwords.replace(/^\s+|\s+$/, '').toLowerCase();
		}

		for(var i=0;i<this.collection.length;i++) {
			var addToOutput=true;
			
			var wine = this.collection[i];
			
			// check color			
			if(addToOutput && (typeof aColor!='undefined') && (aColor!='')) {
//				alert('aColor');
				if(wine.color != aColor) addToOutput=false;
			}

			// check locationpk
			if(addToOutput && (typeof aCountry!='undefined') && (aCountry!='')) {
//				alert('aLocation');
				if(wine.country != aCountry) addToOutput=false;
			}
			
			// check grapetype
			if(addToOutput && (typeof aGrapetype!='undefined') && (aGrapetype!='')) {
//				alert('aGrapetype');
				if(!wine.hasGrape(aGrapetype)) addToOutput=false;
			}
			
			// check searchwords
			if(addToOutput && (searchwords!='undefined') && (searchwords!='')) {
//				if(wine.locationpk != locationpk) addToOutput=false;
				if(wine.name.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else if((wine.shortdescription) && wine.shortdescription.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else if((wine.longdescription) && wine.longdescription.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else if((wine.flavourdescription) && wine.flavourdescription.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else if((wine.foodsuggestion) && wine.foodsuggestion.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else if((wine.namestory) && wine.namestory.toLowerCase().search(searchwords)>=0) {
					addToOutput = true;
				} else {
					addToOutput = false;
				}
			}
			
			if(addToOutput) {
				returnCollection.add(wine);
			}
			
		}
		
		return returnCollection;
		
	}
	
	this.load = function() {
		var data = getData('./XML/winelist.xml','');
		if(typeof data.response!='undefined') {
		
			if(data.response.header.status == 0) {

				if((typeof data.response.body != 'undefined') && (typeof data.response.body.products != 'undefined'))  { // 
					if(typeof data.response.body.products.product.length != 'undefined') {
						for(var i=0;i<data.response.body.products.product.length;i++) {
							var myProduct = data.response.body.products.product[i];
							var wine = new Wine(myProduct.winepk, myProduct.name, myProduct.producttype, myProduct.color, myProduct.shortdescription, myProduct.longdescription, myProduct.flavourdescription, myProduct.foodsuggestion, myProduct.namestory, myProduct.country, myProduct.area, myProduct.imagename);
							
							var grapes = new Array();
							
							if(typeof myProduct.grapes.grape.length != 'undefined') {
								for(var j=0;j<myProduct.grapes.grape.length;j++) {
									grapes[grapes.length]=new Grape(myProduct.grapes.grape[j].name,myProduct.grapes.grape[j].percentage);
								}
							} else {
								grapes[grapes.length]=new Grape(myProduct.grapes.grape.name,myProduct.grapes.grape.percentage);
							}
							
							wine.arrayOfGrapes = grapes;

							this.add(wine);
							
						}
					} else {
						var myProduct = data.response.body.products.product;
						var wine = new Wine(myProduct.winepk, myProduct.name, myProduct.producttype, myProduct.color, myProduct.shortdescription, myProduct.longdescription, myProduct.flavourdescription, myProduct.foodsuggestion, myProduct.namestory, myProduct.country, myProduct.area, myProduct.imagename);
						var grapes = new Array();
						
						if(typeof myProduct.grapes.grape.length != 'undefined') {
							for(var j=0;j<myProduct.grapes.grape.length;j++) {
									grapes[grapes.length]=new Grape(myProduct.grapes.grape[j].name,myProduct.grapes.grape[j].percentage);
							}
						} else {
							grapes[grapes.length]=new Grape(myProduct.grapes.grape.name,myProduct.grapes.grape.percentage);
						}
						
						wine.arrayOfGrapes = grapes;
						this.add(wine);
					}
				}
				
			} else {
				alert(data.response.header.message);
			}
			
		} else {
			alert('Er is een onbekende fout opgetreden. Vermoedelijk is er geen verbinding met de wijndatabase. Probeer het later nogmaals');
		}
	}
	
	this.add = function(aWine) {
		this.collection[this.collection.length]=aWine;
	}
	
	this.clear = function() {
		this.collection = new Array();	
	}
	
}


/**
 *	Wine class
 */
function Wine(aWinepk, aName, aProducttype, aColor, aShortdescription, aLongdescription, aFlavourdescription, aFoodsuggestion, aNamestory, aCountry, anArea, anImageName, anArrayOfGrapes) {
	
	this.winepk					=	aWinepk;
	this.name					=	aName;
	this.color					=	aColor;
	this.shortdescription		=	aShortdescription;
	this.longdescription		=	aLongdescription?aLongdescription:'';
	this.flavourdescription		=	aFlavourdescription;
	this.foodsuggestion			=	aFoodsuggestion;
	this.namestory				=	aNamestory;
	this.location				=	anArea +', '+aCountry;
	this.area					=	anArea;
	this.country				=	aCountry;
	this.arrayOfGrapes			=	anArrayOfGrapes;
	this.imageName				=	anImageName;
	
	this.hasGrape = function (grapeName) {
		var retVal = false;
		for(var	i=0;i<this.arrayOfGrapes.length;i++) {
			if(this.arrayOfGrapes[i].name==grapeName) retVal = true;	
		}
		return retVal;
	}
	
	this.getGrapes=function(percentage) {
		var retVal ='';
		for(var i=0;i<this.arrayOfGrapes.length;i++) {
		
			retVal += this.arrayOfGrapes[i].name.toLowerCase();
			
			if((typeof(percentage)!='undefined') && percentage && (this.arrayOfGrapes[i].percentage>0) && (this.arrayOfGrapes[i].percentage<100)) {
 				retVal +=' ('+this.arrayOfGrapes[i].percentage+'%)';	
			}
			
			if(i==this.arrayOfGrapes.length-2) {
				retVal +=' en ';	
			} else if(i==this.arrayOfGrapes.length-1) {
				retVal +='.';	
			} else {
				retVal +=', ';	
			}
			
		}
		return retVal;
	}
	
}

/**
 * Grape class
 */
function Grape(aName, aPercentage) {
	this.name = aName;
	this.percentage = aPercentage;
}

/**
 * Grapes collection
 */

function GrapeCollection() {

	this.collection = new Array();
	
	this.getSelection = function(aName) {

		var returnCollection = new Array();
		
		for(var i=0;i<this.collection.length;i++) {
			var addToOutput=true;
			
			var grape = this.collection[i];
			
			// check name			
			if(addToOutput && (typeof aName!='undefined')) {
//				alert('aColor');
				if(grape.name != aName) addToOutput=false;
			}
			
			if(addToOutput) {
				returnCollection[returnCollection.length]=grape;
			}
			
		}
		
		return returnCollection;
		
	}
	
	this.load = function() {
		var data = getData(config.serviceURL,'servicecall=wine:getGrapeTypes');
		if(typeof data.response!='undefined') {
		
			if(data.response.header.status == 0) {

				if((typeof data.response.body != 'undefined') && (typeof data.response.body.grapetypes != 'undefined') && (data.response.body.grapetypes.grapetype.length > 0))  { // 
					for(var i=0;i<data.response.body.grapetypes.grapetype.length;i++) {
						var myProduct = data.response.body.grapetypes.grapetype[i];
						this.add(myProduct.name);
					}
				}
				
			} else {
				alert(data.response.header.message);
			}
			
		} else {
			alert('Er is een onbekende fout opgetreden. Vermoedelijk is er geen verbinding met de wijndatabase. Probeer het later nogmaals');
		}
	}
	
	this.add = function(aName) {
		this.collection[this.collection.length]=aName;
	}
	
	this.clear = function() {
		this.collection = new Array();	
	}
		
}
