window.onbeforeunload = function () {   clearselections(); }
	
			
	function updatefilters(trigger) {
		document.body.style.cursor = 'wait';
		var dd_varietals 	= document.getElementById("ddGrapes");
		var dd_wineries 	= document.getElementById("ddWineries");
		var dd_appellations = document.getElementById("ddAppellation");
		var dd_style		= document.getElementById("ddType");
		
		var selectedWinery 		= 0;
		var selectedVarietal 	= 0;
		var selectedAppellation	= 0;
		var selectedStyle		= 0;
		var winestoreOnly		= 0;
		
		if(dd_wineries.selectedIndex!=-1) selectedWinery = dd_wineries.options[dd_wineries.selectedIndex].value;
		if(dd_varietals.selectedIndex!=-1) selectedVarietal = dd_varietals.options[dd_varietals.selectedIndex].value;
		if(dd_appellations.selectedIndex!=-1) selectedAppellation = dd_appellations.options[dd_appellations.selectedIndex].value;
		if(dd_style.selectedIndex!=-1) selectedStyle = dd_style.options[dd_style.selectedIndex].value;

		
		if(trigger=='appellation') {
			fetch_wineries(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedWinery,winestoreOnly);
			fetch_varietals(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedVarietal,winestoreOnly);
			fetch_styles(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedStyle,winestoreOnly);
			calculateResults(selectedWinery,selectedVarietal,selectedAppellation,selectedStyle,winestoreOnly);
		}
		
		if(trigger=='varietal') {
			fetch_wineries(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedWinery,winestoreOnly);
			fetch_appellations(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedAppellation,winestoreOnly);
			fetch_styles(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedStyle,winestoreOnly);
			calculateResults(selectedWinery,selectedVarietal,selectedAppellation,selectedStyle,winestoreOnly);
		}
				
		if(trigger=='winery') {
			fetch_varietals(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedVarietal,winestoreOnly);
			fetch_appellations(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedAppellation,winestoreOnly);
			fetch_styles(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedStyle,winestoreOnly);
			calculateResults(selectedWinery,selectedVarietal,selectedAppellation,selectedStyle,winestoreOnly);
		}
		
		if(trigger=='style') {
			fetch_varietals(selectedVarietal,selectedWinery,selectedVarietal,selectedAppellation,selectedVarietal,winestoreOnly);
			fetch_wineries(selectedVarietal,selectedWinery,selectedAppellation,selectedStyle,selectedWinery,winestoreOnly);
			//fetch_appellations(selectedVarietal,selectedWinery,selectedVarietal,selectedAppellation,selectedAppellation,winestoreOnly);
			
			calculateResults(selectedWinery,selectedVarietal,selectedAppellation,selectedStyle,winestoreOnly);
		}
		
	}
	
	
	
	function fetch_varietals(param_varietal,param_winery,param_appellation,param_style,defaultvalue,winestoreOnly) {
		var ajax = new sack();
		ajax.requestFile = "../awardsMedalsVarietals.aspx?varietalId="+param_varietal+"&wineryId="+param_winery+"&appellationId="+param_appellation+"&styleId="+param_style+"&val="+defaultvalue+"&winestoreOnly="+winestoreOnly;
		ajax.onCompletion = function(){ displayVarietals(ajax,defaultvalue); };	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
	
	function fetch_wineries(param_varietal,param_winery,param_appellation,param_style,defaultvalue,winestoreOnly) {
		var ajax = new sack();
		ajax.requestFile = "../awardsMedalsWineries.aspx?varietalId="+param_varietal+"&wineryId="+param_winery+"&appellationId="+param_appellation+"&styleId="+param_style+"&val="+defaultvalue+"&winestoreOnly="+winestoreOnly;
		ajax.onCompletion = function(){ displayWineries(ajax,defaultvalue); };	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
	
	function fetch_appellations(param_varietal,param_winery,param_appellation,param_style,defaultvalue,winestoreOnly) {
		var ajax = new sack();
		ajax.requestFile = "../awardsMedalsAppellations.aspx?varietalId="+param_varietal+"&wineryId="+param_winery+"&appellationId="+param_appellation+"&styleId="+param_style+"&val="+defaultvalue+"&winestoreOnly="+winestoreOnly;
		ajax.onCompletion = function(){ displayAppellations(ajax,defaultvalue); };	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}

	function fetch_styles(param_varietal,param_winery,param_appellation,param_style,defaultvalue,winestoreOnly) {
		var ajax = new sack();
		ajax.requestFile = "../awardsMedalsType.aspx?varietalId="+param_varietal+"&wineryId="+param_winery+"&appellationId="+param_appellation+"&styleId="+param_style+"&val="+defaultvalue+"&winestoreOnly="+winestoreOnly;
		ajax.onCompletion = function(){ displayStyles(ajax,defaultvalue); };	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
	
	function displayVarietals(ajax,defaultValue)
	{
		var xml = ajax.response;
		xml = xml.replace(/\n/gi,'');

		document.getElementById("ddGrapes").options.length = 0;
		
		var options = xml.split(/<appellation>/gi);
		var valueValuue = "";
		var textValue = "";
		
		document.getElementById("ddGrapes").options[0] = new Option("All",0);
		
		for(var no=1;no<options.length;no++){
		
			var elements = options[no].split(/</gi);
			
			for(var no2=0;no2<elements.length;no2++){
				if(elements[no2].substring(0,1)!='/'){
					var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
					var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
			
					if(key.indexOf('id')>=0){
						valueValuue = value;
					}
					
					if(key.indexOf('avadva')>=0){
						textValue = value;
					}
					
				}
			}
			document.getElementById("ddGrapes").options[(no-1)+1] = new Option(textValue,valueValuue);
		}
		if(defaultValue!=-1) {
			var dd = document.getElementById("ddGrapes");
			for(var j=0;j<dd.length;j++) {
				if(dd.options[j].value==defaultValue) {
					dd.options[j].selected=true;
				}
			}
		}
	}
	
	
	function displayWineries(ajax,defaultValue)
	{
		var xml = ajax.response;
		xml = xml.replace(/\n/gi,'');

		document.getElementById("ddWineries").options.length = 0;
		
		var options = xml.split(/<appellation>/gi);
		var valueValuue = "";
		var textValue = "";
		
		document.getElementById("ddWineries").options[0] = new Option("All",0);
		
		if(options.length>0) {
		for(var no=1;no<options.length;no++){
		
			var elements = options[no].split(/</gi);
			
			for(var no2=0;no2<elements.length;no2++){
				if(elements[no2].substring(0,1)!='/'){
					var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
					var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
			
					if(key.indexOf('id')>=0){
						valueValuue = value;
					}
					
					if(key.indexOf('avadva')>=0){
						textValue = replaceIt(value,"&amp;","&");
					}
					
				}
			}
			document.getElementById("ddWineries").options[(no-1)+1] = new Option(textValue,valueValuue);
		}
		}
		if(defaultValue!=-1) {
			var dd = document.getElementById("ddWineries");
			for(var j=0;j<dd.length;j++) {
				if(dd.options[j].value==defaultValue) {
					dd.options[j].selected=true;
				}
			}
		}
	}
	
	
	function displayAppellations(ajax,defaultValue)
	{
		var xml = ajax.response;
		xml = xml.replace(/\n/gi,'');

		document.getElementById("ddAppellation").options.length = 0;
		
		var options = xml.split(/<appellation>/gi);
		var valueValuue = "";
		var textValue = "";
		
		document.getElementById("ddAppellation").options[0] = new Option("All",0);
		
		if(options.length>0) {
		for(var no=1;no<options.length;no++){
		
			var elements = options[no].split(/</gi);
			
			for(var no2=0;no2<elements.length;no2++){
				if(elements[no2].substring(0,1)!='/'){
					var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
					var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
			
					if(key.indexOf('id')>=0){
						valueValuue = value;
					}
					
					if(key.indexOf('avadva')>=0){
						textValue = replaceIt(value,"&amp;","&");
					}
					
				}
			}
			document.getElementById("ddAppellation").options[(no-1)+1] = new Option(textValue,valueValuue);
		}
		}
		if(defaultValue!=-1) {
			var dd = document.getElementById("ddAppellation");
			for(var j=0;j<dd.length;j++) {
				if(dd.options[j].value==defaultValue) {
					dd.options[j].selected=true;
				}
			}
		}
	}
	
	function displayStyles(ajax,defaultValue)
	{
		var xml = ajax.response;
		xml = xml.replace(/\n/gi,'');

		document.getElementById("ddType").options.length = 0;
		
		var options = xml.split(/<appellation>/gi);
		var valueValuue = "";
		var textValue = "";
		
		document.getElementById("ddType").options[0] = new Option("All",0);
		
		if(options.length>0) {
		for(var no=1;no<options.length;no++){
		
			var elements = options[no].split(/</gi);
			
			for(var no2=0;no2<elements.length;no2++){
				if(elements[no2].substring(0,1)!='/'){
					var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
					var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
			
					if(key.indexOf('id')>=0){
						valueValuue = value;
					}
					
					if(key.indexOf('avadva')>=0){
						textValue = value;
					}
					
				}
			}
			document.getElementById("ddType").options[(no-1)+1] = new Option(textValue,valueValuue);
		}
		}
		if(defaultValue!=-1) {
			var dd = document.getElementById("ddType");
			for(var j=0;j<dd.length;j++) {
				if(dd.options[j].value==defaultValue) {
					dd.options[j].selected=true;
				}
			}
		}
	}
	
	function replaceIt(sString, sReplaceThis, sWithThis) { 
		if (sReplaceThis != "" && sReplaceThis != sWithThis) { 
			var counter = 0; 
			var start = 0; 
			var before = ""; 
			var after = ""; 
			while (counter<sString.length) { 
				start = sString.indexOf(sReplaceThis, counter); 
				if (start == -1) { 
					break; 
				} else { 
					before = sString.substr(0, start); 
					after = sString.substr(start + sReplaceThis.length, sString.length); 
					sString = before + sWithThis + after; 
					counter = before.length + sWithThis.length; 
				} 
			} 
		} 
		return sString; 
	}
	

	
	function calculateResults(wineryId,varietalId,appellationId,styleId,winestoreOnly)
	{
		if(appellationId>=0) {
			var ajax = new sack();
			ajax.requestFile = "../calculateAwardsMedalSearch.aspx?wineryId="+wineryId+"&varietalId="+varietalId+"&appellationId="+appellationId+"&styleId="+styleId+"&winestoreOnly="+winestoreOnly;	// Specifying which file to get
			ajax.onCompletion = function(){ displayCalculationResult(ajax); };	// Specify function that will be executed after file has been found
			ajax.runAJAX();		// Execute AJAX function
		}
	}

	function displayCalculationResult(ajax)
	{
		var xml = ajax.response;
		xml = xml.replace(/\n/gi,'');
		var options = xml.split(/<appellation>/gi);
		var valueValuue = "";
		var textValue = "";

		
		for(var no=0;no<options.length;no++){
		
			var elements = options[no].split(/</gi);
			for(var no2=0;no2<elements.length;no2++){
				if(elements[no2].substring(0,1)!='/'){
					var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
					var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');

					if(key.indexOf('id')>=0){
						valueValuue = value;
					}
					
					if(key.indexOf('avadva')>=0){
						textValue = value;
					}
					
				}
			}

			if(valueValuue==0) {
				document.getElementById("searchBtn").value = "no wines";
				document.getElementById("searchBtn").disabled=true;
			}
			else {
				document.getElementById("searchBtn").value = valueValuue+" wines: create list";
				document.getElementById("searchBtn").disabled=false;
			}
		}
		document.body.style.cursor = 'default';
	}
	
	
	function clearselections() {
		document.getElementById("ddWineries").selectedIndex=0;
		document.getElementById("ddGrapes").selectedIndex=0;
		document.getElementById("ddAppellation").selectedIndex=0;
		document.getElementById("ddType").selectedIndex=0;
		
		document.getElementById("searchBtn").value = "please wait";
		document.getElementById("searchBtn").disabled=true;
		updatefilters('varietal');
		updatefilters('winery');
		updatefilters('appellation');
		updatefilters('style');
	}
	
	
	function searchawards(strpath) {
		var dd_varietals 	= document.getElementById("ddGrapes");
		var dd_wineries 	= document.getElementById("ddWineries");
		var dd_appellations = document.getElementById("ddAppellation");
		var dd_style 		= document.getElementById("ddType");
		
		var selectedWinery = 0;
		var selectedVarietal = 0;
		var selectedAppellation = 0;
		var selectedStyle = 0;
		var winestoreOnly		= 0;
		
		if(dd_wineries.selectedIndex!=-1) selectedWinery = dd_wineries.options[dd_wineries.selectedIndex].value;
		if(dd_varietals.selectedIndex!=-1) selectedVarietal = dd_varietals.options[dd_varietals.selectedIndex].value;
		if(dd_appellations.selectedIndex!=-1) selectedAppellation = dd_appellations.options[dd_appellations.selectedIndex].value;
		if(dd_style.selectedIndex!=-1) selectedStyle = dd_style.options[dd_style.selectedIndex].value;

		if(typeof(strpath)=="undefined") strpath=""; 
		location.href=strpath+"awards.aspx?search=true&wineryId="+selectedWinery+"&varietalId="+selectedVarietal+"&appellationId="+selectedAppellation+"&styleId="+selectedStyle+"&winestoreOnly="+winestoreOnly;
	}
	