/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject_b() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq_b = getXmlHttpRequestObject_b();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function add_concerts_to_tour(concert_available_tour) {
	if (searchReq_b.readyState == 4 || searchReq_b.readyState == 0) {
		var concert_available_tour = concert_available_tour;
		var tours_id = escape(document.getElementById('tours_id').value);
		var str = escape(document.getElementById('concert_available_tour').value);
		// /scripts/get_data.php?choice=venue&venue=

		//var txtSelectedValuesObj = document.getElementById('txtSelectedValues');
		var selectedArray = new Array();
		var selObj = document.getElementById('concert_available_tour');
		var i;
		var count = 0;
		for (i=0; i<selObj.options.length; i++) {
		  if (selObj.options[i].selected) {
		    selectedArray[count] = selObj.options[i].value;
		    count++;
		  }
		}
		var url = '/scripts/t_c_p.php?tours_id='+tours_id+'&action=add&choice=' + selectedArray;
		//alert(selectedArray);
		//alert(url);
		searchReq_b.open("GET", url, true);
		searchReq_b.onreadystatechange = handleSearchSuggest_b; 
		searchReq_b.send(null);
	}		
}

function remove_concerts_from_tour(concert_in_tour) {
	if (searchReq_b.readyState == 4 || searchReq_b.readyState == 0) {
		var concert_in_tour = concert_in_tour;
		var tours_id = escape(document.getElementById('tours_id').value);
		var str = escape(document.getElementById('concert_in_tour').value);
		// /scripts/get_data.php?choice=venue&venue=

		//var txtSelectedValuesObj = document.getElementById('txtSelectedValues');
		var selectedArray = new Array();
		var selObj = document.getElementById('concert_in_tour');
		var i;
		var count = 0;
		for (i=0; i<selObj.options.length; i++) {
		  if (selObj.options[i].selected) {
		    selectedArray[count] = selObj.options[i].value;
		    count++;
		  }
		}
		var url = '/scripts/t_c_p.php?tours_id='+tours_id+'&action=remove&choice=' + selectedArray;
		//alert(selectedArray);
		//alert(url);
		searchReq_b.open("GET", url, true);
		searchReq_b.onreadystatechange = handleSearchSuggest_b; 
		searchReq_b.send(null);
	}		
}


//Called when the AJAX response is returned.
function handleSearchSuggest_b() {
	if (searchReq_b.readyState == 4) {
		var str = searchReq_b.responseText;
		//alert(str);
		setSearch_b(str)
	}
}


//Click function
function setSearch_b(str) {
	var shows=str;
	//alert(shows); 
	//document.getElementById('concert_in_tour').value = shows;
	document.getElementById('concert_in_tour').innerHTML = shows;

}

