var xmlHttp;

var xmlHttpCountry=GetXmlHttpObject();
var xmlHttpCity=GetXmlHttpObject();
var xmlHttpType=GetXmlHttpObject();
var xmlHttpEvent=GetXmlHttpObject();

var filCountry = false;
var filCity = false;
var filType = false;

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function getMonthName(no)
{
	switch(no)
	{
		case '01': return "January";
		case '02': return "February";
		case '03': return "March";
		case '04': return "April";
		case '05': return "May";
		case '06': return "June";
		case '07': return "July";
		case '08': return "August";
		case '09': return "September";
		case '10': return "October";
		case '11': return "November";
		case '12': return "December";
	}
}
/*
function _filterDate(year, month, range, message){
	showAll = document.getElementById("date").value == "0"
	yy = mm = "";
	year-=1;
	range+=12; //clear previous year to make sure events that started during previous 12 months are not shown
	if(year < 10) yy = "0"+year;
	if(month < 10) mm = "0"+month;
	for(i=0; i<range; i++){
		if(month > 12){
			month = 1;
			year = year + 1;
			if(year < 10) yy = "0"+year;
			else yy = year;
		}
		if(month < 10) mm = "0"+month;
		else mm = month;
		if(toRemove = document.getElementById(""+yy+mm))
			toRemove.style.display = (showAll ? '' : 'none');
		month = month+1;
		if(month < 10) mm = "0"+month;
	}
	eventsExist = true;
	if(!showAll){
		if(showDate = document.getElementById("date").value){
			if(toShow = document.getElementById(showDate)){
				if(document.getElementById(showDate).style.display == 'none')
					toShow.style.display = '';
			}
			else
				eventsExist = false;
		}
	}
	if(!eventsExist)
		document.getElementById("message").innerHTML = "<br /> " + message + " " + document.getElementById("date").value.substr(2, 2)+"/"+(document.getElementById("date").value.substr(0, 2)*1+2000); // getMonthName(document.getElementById("date").value.substr(2, 2))+", "+(document.getElementById("date").value.substr(0, 2)*1+2000);
	else
		document.getElementById("message").innerHTML = "";
}
*/
function changeBG(tblID, clicked){
	tab = document.getElementById("tab["+tblID+"]");
	box = document.getElementById("box["+tblID+"]");
	frame = document.getElementById("frame["+tblID+"]");
	empty = document.getElementById("empty["+tblID+"]");
	tab.style.backgroundColor = "#CCCCCC";
	frame.style.borderColor = "#3f5f80";
	empty.style.borderColor = "#3f5f80";
	tab.style.borderColor = "#3f5f80";
	
	if(clicked && box.style.display == 'none')
		frame.style.backgroundColor = "#CCCCCC";
	else
		frame.style.backgroundColor = "#F5F5F5";
	if(box.style.display == 'none')
		frame.style.backgroundColor = "#CCCCCC";
	if(clicked && box.style.display == 'none')
		document.getElementById("plusminus["+tblID+"]").src = "http://www.microStrategy.com/events/graphics/DIVExpander_plus.gif";
	if(clicked && box.style.display != 'none')
		document.getElementById("plusminus["+tblID+"]").src = "http://www.microStrategy.com/events/graphics/DIVExpander_minus.gif";
}
function restoreBG(tblID){
	tab = document.getElementById("tab["+tblID+"]");
	frame = document.getElementById("frame["+tblID+"]");
	empty = document.getElementById("empty["+tblID+"]");
	tab.style.backgroundColor = "#DADADA";
	frame.style.backgroundColor = "#F5F5F5";
	frame.style.borderColor = "#DADADA";
	empty.style.borderColor = "#DADADA";
	tab.style.borderColor = "#DADADA";
}

function show_hide(tblid) {
	if (tbl = document.getElementById(tblid)) {
		tbl.style.display = (tbl.style.display == 'none' ? '' : 'none');
		}
	if (tbl = document.getElementById("c"+tblid)) {
		tbl.style.display = (tbl.style.display == 'none' ? '' : 'none');
		}
	}

function listCountries(first){ 
	var type = document.getElementById("type").value;
	var city = document.getElementById("city").value;
	var ctry = "0";
	if(first)
	{
		type = city = "0";
		ctry = getCookie("calCtry");
	}
	xmlHttpCountry=GetXmlHttpObject();
	if (xmlHttpCountry==null){
		alert ("Your browser does not support AJAX");
		return;
	} 
	var url="queryFiles/get_countries.asp";
	url=url+"?type="+type;
	url=url+"&city="+city;
	url=url+"&ctry="+ctry;
	url=url+"&sid="+Math.random();
	xmlHttpCountry.onreadystatechange=stateChanged1;
	xmlHttpCountry.open("GET",url,true);
	xmlHttpCountry.send(null);
}

function listCities(first){ 
	var country = document.getElementById("country").value;
	var type = document.getElementById("type").value;
	if(first)
		type = country = "0";
	xmlHttpCity=GetXmlHttpObject();
	if (xmlHttpCity==null){
		alert ("Your browser does not support AJAX");
		return;
	} 
	var url="queryFiles/get_cities.asp";
	url=url+"?country="+country;
	url=url+"&type="+type;
	url=url+"&sid="+Math.random();
	xmlHttpCity.onreadystatechange=stateChanged2;
	xmlHttpCity.open("GET",url,true);
	xmlHttpCity.send(null);
}

function listTypes(first){ 
	var country = 	document.getElementById("country").value;
	var city = 		document.getElementById("city").value;
	if(first)
	{
		type = country = city = "0";
	}
	xmlHttpType=GetXmlHttpObject();
	if (xmlHttpType==null){
		alert ("Your browser does not support AJAX");
		return;
	} 
	var url="queryFiles/get_types.asp";
	url=url+"?country="+country;
	url=url+"&city="+city;
	url=url+"&sid="+Math.random();
	xmlHttpType.onreadystatechange=stateChanged3;
	xmlHttpType.open("GET",url,true);
	xmlHttpType.send(null);
}

function printEvents(first){ 
	var country = 	document.getElementById("country").value;
	var city = 		document.getElementById("city").value;
	var type = 		document.getElementById("type").value;
	var ctry = "";
	xmlHttpEvent=GetXmlHttpObject();
	if (xmlHttpEvent==null)
	{
		alert ("Your browser does not support AJAX");
		return;
	}
	switch(getCookie("calCtry"))
	{
		case "ar":
			ctry = "argentina";
			break;
		case "au":
			ctry = "australia";
			break;
		case "be":
			ctry = "belgium";
			break;
		case "br":
			ctry = "brasil";
			break;
		case "ca":
			ctry = "canada";
			break;
		case "fr":
			ctry = "france";
			break;
		case "de":
			ctry = "germany";
			break;
		case "dk":
			ctry = "denmark";
			break;
		case "it":
			ctry = "italy";
			break;
		case "lu":
			ctry = "luxembourg";
			break;
		case "mx":
			ctry = "méxico";
			break;
		case "pt":
			ctry = "portugal";
			break;
		case "si":
			ctry = "singapore";
			break;
		case "za":
			ctry = "south africa";
			break;
		case "sp":
			ctry = "spain";
			break;
		case "sw":
			ctry = "sweden";
			break;
		case "sz":
			ctry = "switzerland";
			break;
		case "uk":
			ctry = "united kingdom";
			break;
		default: 
			ctry = "0";
			break;
		}
	var url="queryFiles/get_events.asp";
	if(first)
		url=url+"?country="+ctry;		//preset country, specified in URL
	else
		url=url+"?country="+country;	//display all countries/selected country
	url=url+"&city="+city;
	url=url+"&type="+type;
	url=url+"&sid="+Math.random();
	url=url+"&CID="+getURLParam("cid");
	xmlHttpEvent.onreadystatechange=stateChanged4;
	xmlHttpEvent.open("GET",url,true);
	xmlHttpEvent.send(null);
	document.getElementById("date").selectedIndex = 0;
}

function stateChanged1() { 
	if (xmlHttpCountry.readyState==4)
		document.getElementById("countryBox").innerHTML=xmlHttpCountry.responseText;
}

function stateChanged2() { 
	if (xmlHttpCity.readyState==4)
		document.getElementById("cityBox").innerHTML=xmlHttpCity.responseText;
}

function stateChanged3() {
	if (xmlHttpType.readyState==4)
		document.getElementById("typeBox").innerHTML=xmlHttpType.responseText;
}

function stateChanged4() {
	if (xmlHttpEvent.readyState==4)
		document.getElementById("printContents").innerHTML=xmlHttpEvent.responseText;
}

function startup(first){
	document.getElementById("resetButton").disabled = true;
	if(first)
	{
		listCountries(first);
		setTimeout("listCities(1);", 300);
	}
	else
	{
		setTimeout("listCountries();", 200);
		listCities(1);
	}
	listTypes(1);
	setTimeout("printEvents("+first+");", 500);	
	setTimeout("document.getElementById(\"resetButton\").disabled = false;",500);
	filCountry = filCity = filType = false;
	document.getElementById("date").selectedIndex=0;
}
function countryChange(){
	if(!filCity)
		setTimeout("listCities();",50);
	if(!filType)
		setTimeout("listTypes();",50);
	setTimeout("printEvents();",100);
}
function cityChange(){
	if(!filType)
		listTypes();
	if(!filCountry)
		setTimeout("listCountries();",100);
	setTimeout("printEvents();",150);
}
function typeChange(){
	if(!filCity)
		listCities();
	if(!filCountry)
		setTimeout("listCountries();",100);
	setTimeout("printEvents()",150);
}
function fixCountry(){
	contents = document.getElementById("country").value;
	contents+= "<input type=\"hidden\" value=\""+document.getElementById("country").value+"\" id=\"country\" />";
	document.getElementById("countryBox").innerHTML=contents;
	countryChange();
	filCountry = true;
}
function fixCity(){
	myCity = document.getElementById("city").value;
	if(myCity == "Sao Paulo")
		myCity = "S&atilde;o Paulo";
	contents = myCity;
	contents+= "<input type=\"hidden\" value=\""+document.getElementById("city").value+"\" id=\"city\" />";
	document.getElementById("cityBox").innerHTML=contents;
	cityChange();
	filCity = true;
}
function fixType(){
	myValue = document.getElementById("type").value;
	if(myValue == "0")
		myValue = "Showing all event types";
	if(myValue == "MicroStrategy Business Intelligence Symposium")
		myValue = "MicroStrategy BI Symposium";
	contents = myValue;
	contents+= "<input type=\"hidden\" value=\""+document.getElementById("type").value+"\" id=\"type\" />";
	document.getElementById("typeBox").innerHTML=contents;
	typeChange();
	filType = true;
}
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if (strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for (var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
} 
