var composerList;
var conductorList;
var venueList;

var composerArray = new Array();
var conductorArray = new Array();
var venueArray = new Array();
var i;


var compoValue,condValue,venueValue,startDate,endDate;

   
function getDbOption(p_compoValue,p_condValue,p_venueValue,p_startDate,p_endDate){
	var ajaxRequest;  // The variable that makes Ajax possible!
		
	compoValue = p_compoValue;
	condValue  = p_condValue ;
	venueValue = p_venueValue;
	startDate  = p_startDate ;
	endDate    = p_endDate   ;
	
	try{ // Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} 
	catch (e){ // Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e){ // Something went wrong
				
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function()
	{
		if(ajaxRequest.readyState >= 4)
		{
			//alert(ajaxRequest.responseText);
			var fullString = ajaxRequest.responseText;
			var fullArray = fullString.split('~~~');
			composerList = fullArray[0];
			conductorList = fullArray[1];
			venueList = fullArray[2];

			composerArray = composerList.split('|||');
			conductorArray = conductorList.split('|||');
			venueArray = venueList.split('|||');

			for (var j=0; j<composerArray.length /*&& j<157*/; j++) {
				var l;
				var sel;
				l = document.getElementById('composer').length;
				sel=(compoValue.replace("ampampamp","&")==composerArray[j])?true:false;
				document.getElementById('composer')[l] = new Option("",composerArray[j],false,sel);
				document.getElementById('composer')[l].innerHTML = composerArray[j];
				//var composerOptn = document.createElement("OPTION");
				//composerOptn.text = composerArray[j];
				//composerOptn.value = composerArray[j];
				//document.getElementById('composer').options.add(composerOptn);
			}

			for (var j=0; j<conductorArray.length; j++) {
				var l;
				var sel;
				l = document.getElementById('conductor').length;
				sel=(condValue.replace("ampampamp","&")==conductorArray[j])?true:false;
				document.getElementById('conductor')[l] = new Option("",conductorArray[j],false,sel);
				document.getElementById('conductor')[l].innerHTML = conductorArray[j];
				//var conductorOptn = document.createElement("OPTION");
				//conductorOptn.text = conductorArray[j];
				//conductorOptn.value = conductorArray[j];
				//document.getElementById('conductor').options.add(conductorOptn);
			}

			for (var j=0; j<venueArray.length; j++) {
				var l;
				var sel;
				l = document.getElementById('venue').length;
				sel=(venueValue.replace("ampampamp","&")==venueArray[j])?true:false;
				document.getElementById('venue')[l] = new Option("",venueArray[j],false,sel);
				document.getElementById('venue')[l].innerHTML = venueArray[j];
				//var venueOptn = document.createElement("OPTION");
				//venueOptn.text = venueArray[j];
				//venueOptn.value = venueArray[j];
				//document.getElementById('venue').options.add(venueOptn);
			}
		}
	}

	//ajaxRequest.open("GET", "/_ajax/data.php", true);
	
	
	//alert("/_ajax/data.php?composer="+compoValue.replace("&","ampampamp")+"&conductor="+condValue.replace("&","ampampamp")+"&venue="+venueValue.replace("&","ampampamp")+"&start="+startDate+"&end="+endDate);
	
	ajaxRequest.open("GET", "/_ajax/data.php?composer="+compoValue.replace("&","ampampamp")+"&conductor="+condValue.replace("&","ampampamp")+"&venue="+venueValue.replace("&","ampampamp")+"&start="+startDate+"&end="+endDate, true);

	ajaxRequest.send(null); 
}


var months = new Array("January", "February", "March", "April","May","June",
"July","August","September","October","November","December"); 
	
	var date;
	var month;
	var year;
	
	var getDaysCurrent;
	var getDaysAhead;
	var nextYear;
	
function getSelectOpts(){
	
	date = new Date();
	month = date.getMonth();
	year = date.getYear();
	
	for (var j=0; j<12; j++) {
		var dateOptn1 = document.createElement("OPTION");
		dateOptn1.text = months[j];
		dateOptn1.value = months[j];
		document.getElementById('datefrom').options.add(dateOptn1);
	}
	document.getElementById('datefrom').selectedIndex = month;
	
	if (date.getMonth() > 11) {
		monthsAhead = (date.getMonth() + 1) - 12;
		yearAhead = year + 1;
	} else {
		monthsAhead = date.getMonth() + 1;
		yearAhead = year;
	}
	
	for (var j=0; j<12; j++) {
		var dateOptn2 = document.createElement("OPTION");
		dateOptn2.text = months[j];
		dateOptn2.value = months[j];
		document.getElementById('dateto').options.add(dateOptn2);
	}
	if (date.getMonth() > 5) {
		document.getElementById('dateto').selectedIndex = month - 6;
	} else {
		document.getElementById('dateto').selectedIndex = month + 6;
	}
	
	getDaysCurrent = days_in_month(year, month);
	getDaysAhead = days_in_month(yearAhead, monthsAhead);
		
	for (var j=1; j<=getDaysCurrent; j++) 
	{ 
		var dayOptn1 = document.createElement("OPTION"); 
		dayOptn1.text = j; 
		dayOptn1.value = j; 
		document.getElementById('dayfrom').options.add(dayOptn1); 
	} 

	document.getElementById('dayfrom').selectedIndex = date.getDate()-1;
	
	
	for (var j=1; j<=getDaysAhead; j++) 
	{ 
		var dayOptn2 = document.createElement("OPTION"); 
		dayOptn2.text = j; 
		dayOptn2.value = j; 
		document.getElementById('dayto').options.add(dayOptn2); 
	}

	if(date.getDate()>getDaysAhead)
	{
		document.getElementById('dayto').selectedIndex = 0;
		//document.getElementById('dateto') = (document.getElementById('dateto')+1)%12;
	}
	else
		document.getElementById('dayto').selectedIndex = date.getDate()-1;
	
	for (var j=0; j<=5; j++) {
		var yearOptn1 = document.createElement("OPTION");
		yearOptn1.text = date.getFullYear() + j;
		yearOptn1.value = date.getFullYear() + j;
		document.getElementById('yearfrom').options.add(yearOptn1);
	}
	for (var j=0; j<=5; j++) {
		var yearOptn2 = document.createElement("OPTION");
		yearOptn2.text = date.getFullYear() + j;
		yearOptn2.value = date.getFullYear() + j;
		document.getElementById('yearto').options.add(yearOptn2);
	}
	
	if (month > 5) {
		document.getElementById('yearto').selectedIndex=1;
	} 
}

function days_in_month (y, m) {
     return 32 - new Date(y, m, 32).getDate();
}

function resetDate() {
var date = new Date();
var month = date.getMonth();
var year = date.getYear();
var getDaysAhead = days_in_month(yearAhead, monthsAhead);

	if (date.getMonth() > 5) {
		document.getElementById('dateto').selectedIndex = month - 6;
		document.getElementById('yearto').selectedIndex = 1;
	} else {
		document.getElementById('dateto').selectedIndex = month + 6;
		document.getElementById('yearto').selectedIndex = 0;
	}
	
	if(date.getDate()>getDaysAhead) {
		document.getElementById('dayto').selectedIndex = 0;
		document.getElementById('dateto') = (document.getElementById('dateto')+1)%12;
	} else {
		document.getElementById('dayto').selectedIndex = date.getDate()-1;
	}
	
	document.getElementById('yearfrom').selectedIndex = 0;
	document.getElementById('datefrom').selectedIndex = month;
	document.getElementById('dayfrom').selectedIndex = date.getDate()-1;


//alert(document.getElementById('dayfrom').selectedIndex+','+document.getElementById('datefrom').selectedIndex+','+document.getElementById('yearfrom').selectedIndex);
//alert(document.getElementById('dayto').selectedIndex+','+document.getElementById('dateto').selectedIndex+','+document.getElementById('yearto').selectedIndex);

}

//var month;
//var year;
function nextMonth() {
	if (month < 12) {
		month += 1;
	} else {
		month = 1;
		year += 1;
	}
	setCal(month);
}
function prevMonth() {
	if (month > 1) {
		month -= 1;
	} else {
		month = 12;
		year -= 1;
	}
	setCal(month);
}

function setCal(mon) {
	if (mon == null) {
		var d = new Date();
		month = d.getMonth();
		month += 1;
		year = d.getFullYear();
	} else {
		month = mon;
	}
	var browserName = whichBrs(); 
	
	if (browserName == 'Safari') {
		var b_version=navigator.appVersion;
		var version_array=b_version.split("/");
		var version = version_array[version_array.length-2].substring(0,1);
		//alert(version);
		
	}


	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState < 4){
			document.getElementById('calInner').innerHTML = "<p>loading calendar...</p>";
		}
		if(ajaxRequest.readyState >= 4){
			var ajaxDisplay = document.getElementById('calInner');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "/_ajax/calendar.php?month=" + month + "&year=" + year + "&rand=" + Math.random(), true);
	ajaxRequest.send(null); 
}

function loadConcert(conId) {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState >= 4){
			var ajaxDisplay = document.getElementById('calInner');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "/_ajax/ajax.php?id="+conId, true);
	ajaxRequest.send(null); 
}
	

function setPage(p_compoValue,p_condValue,p_venueValue,p_startDate,p_endDate) {
	//alert('1');
	getDbOption(p_compoValue,p_condValue,p_venueValue,p_startDate,p_endDate);
	//alert('2');
	setTimeout("getSelectOpts()", 100);
	//alert('3');
	setTimeout("setCal()", 200);
	//alert('4');
}
