var CountryCode = 1
var yourSelectionIndex = 0
var maxSelectionIndex = 0
var maxSelection = 5
var 	tbl

function CountryDecode( field ) {
	return ( field.value == 'UK' ) ? 1 : 2
}

function ClassCat(asId, asSourceList, asDestList, asCountryCodeId) {
  GetContents(asId, asDestList, ClassCatArray, asCountryCodeId)
} 

function ClassVintage(asId, asSourceList, asDestList, asCountryCodeId){
  GetContents(asId, asDestList, ClassVintArray, asCountryCodeId)
} 

function CatWine(asId, asSourceList, asDestList, asCountryCodeId){
  GetContents(asId, asDestList, CatWineArray, asCountryCodeId)
} 

function CountryClass(asId, asSourceList, asDestList){
  GetContents(asId, asDestList, CountryClassArray, asId)
} 

function ClassSelected(asSourceList, asClassList, asVintageList, asCountryCodeId){
 sId = asSourceList.options[asSourceList.selectedIndex].value;
 iResult = ClassCat(sId,asSourceList,asClassList, asCountryCodeId) 
 iResult = ClassVintage(sId,asSourceList,asVintageList, asCountryCodeId) 
 
 if (asSourceList.options[asSourceList.selectedIndex].name = "Please choose"){  
  document.wineSelection.Wine.length = 0
  document.wineSelection.Wine.options[0] = new Option("Please choose","Please choose",0,1)
 }
 
}

function CategorySelected(asSourceList, asWineList, asCountryCodeId) {
 sId = asSourceList.options[asSourceList.selectedIndex].value;
 iResult = CatWine(sId,asSourceList,asWineList, asCountryCodeId) 
}

function CountrySelected(asSourceList, asClassList) {
 sId = asSourceList.options[asSourceList.selectedIndex].value;
 CountryCode = sId;
 iResult = CountryClass(sId,asSourceList,asClassList, CountryCode) ;
} 

function GetContents(asId, asDestList, asArray, asCountryCodeId){
  asDestList.length = 0; //Clear all
  asDestList.options[0] = new Option("Please choose","Please choose",0,1);

  // for every element in the array
  for (iCount=0;iCount<asArray.length;iCount++)
    if ( (asArray[iCount][0] == asId) && ( asArray[iCount][3] == asCountryCodeId) )
      asDestList.options[asDestList.options.length] = new Option(asArray[iCount][2],asArray[iCount][1],0,0);

}

function ResetLists(){
  document.wineSelection.lstClass.selectedIndex = 0;      
  document.wineSelection.Category.selectedIndex = 0;
  document.wineSelection.Category.length = 1
  document.wineSelection.Wine.selectedIndex = 0;
  document.wineSelection.Wine.length = 1
  document.wineSelection.Vintage.selectedIndex = 0;
  document.wineSelection.Vintage.length = 1
}

function selectLookupValue( box ) {
	if( box.options.length < 0 ) return '' 
	if( box.selectedIndex < 0 ) return '' //box.SelectedIndex = 0;
	return box.options[ box.selectedIndex ].value == 'Please choose' ? '' : box.options[ box.selectedIndex ].value
}

function selectLookupKey( box ) {
	if( box.options.length < 0 ) return '' 
	if( box.selectedIndex < 0 ) return '' //box.SelectedIndex = 0;
	return box.options[ box.selectedIndex ].value == 'Please choose' ? '' : box.options[ box.selectedIndex ].text
}

function getSelectedKeys() {

  var val = selectLookupValue( document.wineSelection.lstClass )
  if( val == 'Please choose' || val == 0 ) val ='';	
  var str = val + ':' 

  val = selectLookupValue( document.wineSelection.Category )
  if( val == 'Please choose' || val == 0 ) val =''; 
  str = str + val + ':' 

  val = selectLookupValue( document.wineSelection.Wine )
  if( val == 'Please choose' || val == 0 ) val =''; 
  str = str + val + ':' 

  val = selectLookupKey( document.wineSelection.Vintage )
  if( val == 'Please choose' || val == 0 ) val =''; 

  return str + val
}

function addSelection() {
	// Add item to list
	if ( maxSelectionIndex < maxSelection ) {
		var graphList = document.TrackerSearch.graph.value.length > 0 ? document.TrackerSearch.graph.value.split( ';' ) : new Array();
		var str = getSelectedKeys()
	
		graphList[ graphList.length ] = str;
		document.TrackerSearch.graph.value = graphList.join( ';' )

		addToYourSelection( yourSelectionIndex++ );
		maxSelectionIndex++;	
		ResetLists();
	} else {
		alert( 'Sorry, you can only select ' + maxSelection + ' items to view.' )
	}
}

function removeSelection( id ) {
	// Remove nth element from list
	var graphList = document.TrackerSearch.graph.value.split( ';' ) 
	var newGraphList = new Array;
	
	for( var i = 0; i < graphList.length; i++ ) {
		if( id != i ) newGraphList.push( graphList[ i ] )
	}
			
	document.TrackerSearch.graph.value = newGraphList.join( ';' )
	maxSelectionIndex--;	
	 removeFromYourSelection( id )
}

function addToYourSelection( id ) {
	tbl = document.getElementById('yourSelection');
	var div = document.createElement("div");
	var del = document.createElement("img");
	
	var wClass = document.createElement("span");
	var wCategory = document.createElement("span");
	var wVintage = document.createElement("span");
	var wWine= document.createElement("span");
	var wDelete = document.createElement("span");

	wClass.style.width = 102 + 'px'
	wClass.style.align = 'left'
	wClass.style.verticalAlign = 'top'	
	wClass.appendChild( document.createTextNode( selectLookupKey( document.wineSelection.lstClass ) ) )
	
	wCategory.style.width=122 + 'px'
	wCategory.align = 'left'
	wCategory.style.verticalAlign = 'top'
	wCategory.appendChild( document.createTextNode( ' - ' + selectLookupKey( document.wineSelection.Category ) ) )
	
	wWine.style.width=142 + 'px'
	wWine.align = 'left'
	wWine.style.verticalAlign = 'top'
	wWine.borderBottomWidth = 2 + 'px'
	wWine.borderBottomStyle = 'solid'
	wWine.borderBottomColor = 'black'
	wWine.appendChild( document.createTextNode( ' - ' + selectLookupKey( document.wineSelection.Wine ) ) )
		
	wVintage.style.width=42 + 'px'
	wVintage.align = 'left'
	wVintage.style.verticalAlign = 'top'
	wVintage.appendChild( document.createTextNode( ' - ' + selectLookupKey( document.wineSelection.Vintage ) ) )
	
	// Delete icon
	del.src="/images/wt_delete.gif";
	del.className="cross";
	del.border=0
	
	deleteButton = document.createElement("a");
	deleteButton.href = "javascript:removeSelection(" + id + ")";
	deleteButton.appendChild( del )
	
	wDelete.style.width=40 + 'px'
	wDelete.align = 'center'
	wDelete.appendChild( deleteButton )	

	div.id = "sel" + id;
	div.className="tablebody";
	div.appendChild( wClass )
	div.appendChild( wCategory )
	div.appendChild( wWine )
	div.appendChild( wVintage )
	div.appendChild( wDelete )
	
	// Add div to page.
	tbl.appendChild( div );
}

function removeFromYourSelection( id ) {
	var divs = tbl.getElementsByTagName('div');
	for( var i = 0; i < divs.length; i++ )
		if ( divs[ i ].id == "sel" + id ) tbl.removeChild( divs[ i ] );
}

function removeAllSelection() {	
	for ( var i = yourSelectionIndex; i >= 0; i-- ) removeSelection( i );
}

function populateDate( targetField, monthBox, yearBox ) {
	targetField.value =  String( selectLookupValue( yearBox ) ) + String( selectLookupValue( monthBox ) )
}

function selectValue( box, searchValue ) {
	box.selectedIndex = -1;

	if ( searchValue == '' ) searchValue = 'Please choose'

	for( var  i = 0; i < box.options.length; i++ ) {
		if ( box.options[ i ].value == searchValue ) {
			box.options[ i ].selected = true
			break
		}

	}
	
	return ( box.selectedIndex == -1 )  ? false : true
}

function selectKey( box, searchValue) {
	box.selectedIndex = -1;
	if (searchValue == '') searchValue = 'Please choose'
	for (var i = 0; i < box.options.length; i++) {
		if ( box.options[i].text == searchValue ) {
			box.options[i].selected = true;
			break;
		}
	}
	return (box.selectedIndex == -1) ? false : true;
}

function validDropDownValue( str ) {
	return ( str != '' && str.length > 0 && str != 'Please choose' && str != 0 ) ? true : false;
}

function populateSelection( fieldList ) { 
	
	if ( fieldList.length > 0 ) {

		// Split in to seperate items to be tracked.
		var wines = new Array()
		wines =  fieldList.split( ';' )

		for( var i = 0; i < wines.length; i++ ) {
			
			// Split into attributes
			var details = wines[ i ].split( ':' )
			var prevFlag = true // This returns the success or failure of the previous outcome

			// Find elements in drop down..
			if ( validDropDownValue( details[ 0 ] ) ) prevFlag = selectValue( document.wineSelection.lstClass, details[ 0 ] );
			ClassSelected( document.wineSelection.lstClass, document.wineSelection.Category, document.wineSelection.Vintage, CountryCode )

			if ( prevFlag && validDropDownValue( details[ 1 ] ) )  prevFlag = selectValue( document.wineSelection.Category, details[ 1 ] );
			CategorySelected( document.wineSelection.Category, document.wineSelection.Wine, CountryCode )

			if ( prevFlag && validDropDownValue( details[ 2 ] ) )  prevFlag = selectValue( document.wineSelection.Wine, details[ 2 ] );
						
			if ( prevFlag && validDropDownValue( details[ 3 ] ) )  prevFlag = selectKey( document.wineSelection.Vintage, details[ 3 ] );
			
			addSelection() // Add elements to drop down

		} // End of for loop on wines
		
	} // End of if contents present.

}

function populateDateField( pricePoint, monthBox, yearBox ) {
	if( pricePoint.length > 0 ) {
		selectValue( monthBox, pricePoint.substr( 0, 2 ) )
		selectValue( yearBox, pricePoint.substr( 2, 4 ) )
	}
}
