	function makeSublist(HeadlineBands,Tours,isSubselectOptional)
	{
		var selected = '';
		
		$("body").append("<select style='display:none' id='"+HeadlineBands+Tours+"'></select>");
		$('#'+HeadlineBands+Tours).html($("#"+Tours+" option"));
		
		$('#'+Tours).html("<option> --- </option>");		
		$('#'+HeadlineBands).change(
			function()
			{
				
				var parentValue = $('#'+HeadlineBands).attr('value');	
				$('#'+Tours).html($("#"+HeadlineBands+Tours+" .sub_"+parentValue).clone());		
				if(isSubselectOptional) $('#'+Tours).prepend("<option selected> --- </option>");
				$('#Tours').val(selected);
			}
		);
	}


	function getCityList(countryID)
	{
		var html = $.ajax({ 
			type: "POST",
			url: '/venues/getCityByCountry', 
			async: false,
			data: {'data[country]' : countryID}	
		}).responseText;

		
		if(html == "")
		{
			$('#City >option').remove();
			$('#City').append(
				$('<option></option').val("").html("---")
			);
		}
		else
		{			
		
		//console.debug(html);

			eval("json = " + html)
			if(json.length == 0)
			{
			}
			else
			{
				if(json[0]["state"] != undefined)
				{
					$('.stateRow').show();

					$('#City >option').remove();
					$('#City').append(
						$('<option></option').val("").html("---")
					);
					
					$('#Town >option').remove();
			
					$('#Town').append(
						$('<option></option').val("").html("---")
					);
					
					$('#State >option').remove();
				
					$('#State').append(
						$('<option></option').val("").html("---")
					);			
					$.each(json, function(key, value) { 								 
									$('#State').append(
									$('<option></option').val(value["state"]).html(value["state"])
								);
							  } )

				}
				else
				{
					$('#City >option').remove();
					$('.stateRow').hide();
					$('#City').append(
						$('<option></option').val("").html("---")
					);
					
					$('#Town >option').remove();
			
					$('#Town').append(
						$('<option></option').val("").html("---")
					);
				
					$.each(json, function(key, value) { 								 
									$('#City').append(
									$('<option></option').val(value["city"]).html(value["city"])
								);
							  } )
				}
			}
		}
	}
	
	function getAreaList(country, state, city)
	{
		
		var html = $.ajax({ 
			type: "POST",
			url: '/venues/getAreaByCountryAndCity', 
			async: false,
			data: {'data[country]' : country, 'data[state]' : state, 'data[city]' : city}	
		}).responseText;
		
		//console.debug(html);
		if(html == "[]")
		{
			//alert("No Towns")

			$('#Town >option').remove();
			
			$('#Town').append(
				$('<option></option').val("").html("---")
			);			
			
			fillVenuList();
		}
		else
		{
			//alert("Got Towns")
			eval("json = " + html)
	
			$('#Town >option').remove();
			
			$('#Town').append(
				$('<option></option').val("").html("-- All Areas --")
			);			
			$.each(json, function(key, value) { 								 
								$('#Town').append(
								$('<option></option').val(value["town"]).html(value["town"])
								//	$('<option></option').val(value["country"] + "_"+ value["city"] + "_"+ value["town"].replace(/ /gi,"_")).html(value["town"])
							);
						  } )
			fillVenuList()
		}
	}
	
	function getCityByState(countryID, state)
	{
		
		var html = $.ajax({ 
			type: "POST",
			url: '/venues/getCityByCountryState', 
			async: false,
			data: {'data[country]' : $('#Country').val(), 'data[state]' : $('#State').val()}	
		}).responseText;
		eval("json = " + html)
		$('#City >option').remove();
				
					$('#City').append(
						$('<option></option').val("").html("---")
					);			
					$.each(json, function(key, value) { 								 
									$('#City').append(
									$('<option></option').val(value["city"]).html(value["city"])
								);
							  } )
		getStateVal();			
		getCityVal();
		getVenueID();
	}
	

	function fillVenuList()
	{
	
		state = ($('#State').is(":hidden")) ? "" : $('#State').val();

		var html = $.ajax({ 
			type: "POST",
			url: '/venues/getVenue', 
			async: false,
			data: {'data[country]' : $('#Country').val(), 'data[state]' : state, 'data[city]' :  $('#City').val(), 'data[town]' :  $('#Town').val()}	
		}).responseText;
		
	
		eval("json = " + html)
		//console.debug(json);
		$('#Venue >option').remove();
				
					$('#Venue').append(
						$('<option></option').val("").html("---")
					);			
					$.each(json, function(key, value) { 								 
									$('#Venue').append(
									$('<option></option').val(value["venue_id"]).html(value["name"])
								);
							  } )
		getVenueID();
	}
	
	function fillCityList()
	{
		//alert($('#Country').val());
		getCityList($('#Country').val(), "");
		//$('<option></option').val(value["country"] + "_"+ value["city"] + "_"+ value["town"].replace(/ /gi,"_")).html(value["town"])
		$('#Venue >option').remove();
		$('#Venue').append(
						$('<option></option').val("").html("---")
		);
		
		getCountryVal();
		getCityVal('');
		getVenueID('');
	}
	
	function makeSublistCity(Country,City,Town,isSubselectOptional)
	{
		$("body").append("<select style='display:none' id='"+Country+City+"'></select>");
		$('#'+Country+City).html($("#"+City+" option"));
		
		$('#City').append(
						$('<option></option').val("").html("---")
		);
		
		$('#Town').append(
						$('<option></option').val("").html("---")
		);
		
		$('#Venue').append(
						$('<option></option').val("").html("---")
		);
		
		getCountryVal();
		getCityVal();
		getVenueID();
	}


	function makeSublisTown(City,Town,isSubselectOptional)
	{
		
		//
		if($('.stateRow').is(":hidden"))
		{
			getAreaList($('#Country').val(), "", $('#City').val() )
		}
		else
		{
			//Last Changed
			//alert($('#City').val());
			getAreaList($('#Country').val(), $('#State').val(), $('#City').val())
				
		}
		
		getCityVal();
	}

	$(document).ready(function()
	{
		makeSublist('HeadlineBands','Tours',true);	
		
		makeSublistCity('Country','City','Town',true);
	
		$('#State').bind("change", getCityByState);
		$('.stateRow').hide();
		
		
		$('#Country').bind("change", fillCityList);
		$('#City').bind("change", makeSublisTown);
		$('#Town').bind("change", fillVenuList);
		$('#Venue').bind("change", getVenueID);
					
	});	
	
	function getCountryVal()
	{
		$('#VenueCountry').val($("#Country").val());
	}
	
	function getStateVal()
	{
		$('#VenueState').val($("#State").val());
	}
	
	function getCityVal()
	{
		$('#VenueCity').val($("#City").val());
	}
	
	function getVenueID()
	{
		$('#VenueData').val($("#Venue").val());
	}	
	
	
	function loadStoryPage()
	{
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/admin/gigs/viewstory/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 126px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	function loadBandPage()
	{		
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/gigs/newband/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 126px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};

	function loadTourPage()
	{
		 //alert("test");
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/gigs/newtour/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 126px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	
	function loadVenuePage()
	{
		 //alert("test");
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/gigs/newvenue/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 320px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	
	function loadAdminBandPage()
	{
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/admin/gigs/newband/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 126px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	function loadAdminTourPage()
	{
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/admin/gigs/newtour/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 126px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	function loadAdminVenuePage()
	{


		 //alert("test");
		 lastClickedDiv = this;
		 var a = document.createElement("a");
		 a.href = "/admin/gigs/newvenue/";
		 a.rel = "lyteframe";
		 a.title = "Page Library";
		 a.rev = "width: 520px; height: 309px; scrolling: no;";
		 myLytebox.start( a, false, true); 
	};
	
	
	function loadPageStory(value)
	{
		 myLytebox.end(); 
		 
		 document.getElementById("HeadlineBands").options.add(new Option(value,value))
		 document.getElementById("HeadlineBands").options.selectedIndex = document.getElementById("HeadlineBands").options.length - 1;	
	};
	
	function loadPageBand(value)
	{
		 myLytebox.end(); 
		 
		 document.getElementById("HeadlineBands").options.add(new Option(value,value))
		 document.getElementById("HeadlineBands").options.selectedIndex = document.getElementById("HeadlineBands").options.length - 1;	
	};
	
	
	function loadPageTour(value)
	{
		myLytebox.end(); 
		 
		document.getElementById("Tours").options.add(new Option(value,value))
		document.getElementById("Tours").options.selectedIndex = document.getElementById("Tours").options.length - 1;
	};
	
	function loadPageVenue(valueName, valueCity, valueTown, valueState, valueCountry)
	{
		myLytebox.end(); 

		var updateVenue = new Array();
		
		document.getElementById("Country").options.add(new Option(valueCountry,valueCountry))
		document.getElementById("Country").options.selectedIndex = document.getElementById("Country").options.length - 1;
		
		document.getElementById("City").options.add(new Option(valueCity,valueCity))
		document.getElementById("City").options.selectedIndex = document.getElementById("City").options.length - 1;
		
		document.getElementById("Town").options.add(new Option(valueTown,valueTown))
		document.getElementById("Town").options.selectedIndex = document.getElementById("Town").options.length - 1;
		
		document.getElementById("Venue").options.add(new Option(valueName,valueName))
		document.getElementById("Venue").options.selectedIndex = document.getElementById("Venue").options.length - 1;	
		
		var valueDistrict = "";
		
		var updateVenue =new Array(valueName,valueTown,valueDistrict,valueCity,valueState,valueCountry);
		document.getElementById("VenueData").value = updateVenue;
	};
	

	function addRowToTable()
	{	
		
		var bandId		= document.getElementById("HeadlineBands").value; 					//bandId
		var bandRefId	= document.getElementById("HeadlineBands").selectedIndex; 			
		var bandName	= document.getElementById("HeadlineBands").options[bandRefId].text;	//bandName
		
		//var tourId		= document.getElementById("Tours").value; 							//tourId
		//var tourRefId	= document.getElementById("Tours").selectedIndex; 					
		//var tourName	= document.getElementById("Tours").options[tourRefId].text;			//tourName
		
		//var updateBand =new Array(bandId,bandName,tourId,tourName);
		
		var updateBand =new Array(bandId,bandName);
		
		var tbl = document.getElementById('tblSample');
		var lastRow = tbl.rows.length;
		
		if((bandName == "Select Band") || (bandName == "")){
		
			alert("Please select a valid Band Name")
		}
		else
		{
	
			document.getElementById("addNewBandRow").style.display = 'block';
			
			if (tbl.rows.length <= 1) 
			{
			  var iteration = 1;
			}
			else
			{
				var rowCount = parseFloat(document.getElementById("rowCount").value);
				var addOneRow = 1;
				var newRowCount = rowCount + addOneRow;
				document.getElementById("rowCount").value = newRowCount;
				var iteration = newRowCount;
			}
		   
			// if there's no header row in the table, then iteration = lastRow + 1
			var row = tbl.insertRow(lastRow);
			
	
			// first cell
			var cellRight = row.insertCell(0);
			
			//Number
			var textNode = document.createTextNode(iteration);
			//cellRight.appendChild(textNode);
			
			//Band Name
			var el = document.createElement('input');
			el.setAttribute('type', 'hidden');
			el.setAttribute('name', 'data[Details][Band_'+iteration+']');
			el.setAttribute('id', 'DetailsBand'+iteration+'');
			el.setAttribute('class', 'admin_inputfield');
			el.setAttribute('size', '37');
			el.setAttribute('value', bandName);
			cellRight.appendChild(el);
			
			$(cellRight).append(bandName);
			
			//Hidden attribute
			var rowIndex = document.createElement('input');
			rowIndex.setAttribute('type', 'hidden');
			rowIndex.setAttribute('name', 'rowId'+ iteration);
			rowIndex.setAttribute('id', 'rowIndex'+ iteration);
			rowIndex.setAttribute('value', 'rowIndex'+ iteration);
			cellRight.appendChild(rowIndex);
			
			// Second cell
			//var cellRightSel = row.insertCell(1);
//			var sel = document.createElement('input');
//			sel.setAttribute('type', 'hidden');
//			sel.setAttribute('name', 'data[Details][Tour_'+iteration+']');
//			sel.setAttribute('id', 'DetailsTour'+iteration+'');
//			sel.setAttribute('size', '37');
//			sel.setAttribute('value', tourName); 
//			cellRightSel.appendChild(sel);
//			
//			$(cellRightSel).append(tourName);
			
			
			var cellRightHidden = row.insertCell(1);
			var selData = document.createElement('input');
			selData.setAttribute('type', 'hidden');
			selData.setAttribute('name', 'data[NewBand][Data_'+iteration+']');
			selData.setAttribute('id', 'NewBandData'+iteration+'');
			selData.setAttribute('class', 'admin_inputfield');
			selData.setAttribute('value', updateBand); 
			cellRightHidden.appendChild(selData);
			
			var selDelete = document.createElement('button');
			selDelete.setAttribute('type', 'button');
			selDelete.setAttribute('name', 'addSec');
			selDelete.setAttribute('class', 'removeBtnCss');
			selDelete.innerHTML = 'Remove';
			//selDelete.setAttribute('value', 'Remove');
			$(selDelete).bind("click", function() { removeRowFromTable($(this)) });
			//$(selDelete).bind("click", function() { editRowFromTable(iteration) });
			cellRightHidden.appendChild(selDelete);
			
		}
	}
	
	function removeRowFromTable(button)
	{	
		$(button).parent().parent().remove();
	}
	