dateObj=new Date();
dayList=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var curYear=dateObj.getFullYear()
leagueList={1:"Men's",2:"Women's",3:"COED"};

curYear=2008;	// DELETE AFTER 2009 SCHEDULE IS COMPLETED

function tournamentCalendar() {
	calendarTable="<table id='tournamentCalendar' border=0 cellpadding=0 cellspacing=0>";
	calendarTable+="<tr><td class='titleRow' width=100px>Date</td>";
	calendarTable+="<td class='titleRow' width=400px>Tournament</td>";
	calendarTable+="<td class='titleRow' width=150px>Location</td>";
	calendarTable+="<td class='titleRow' >Divisions</td>";
	calendarTable+="<td class='titleRow' width=210px>Contact</td>";
	calendarTable+="<td class='titleRow' >Fee</td></tr>";

	$.ajax({
	  url: "apps/tournamentCalendar/loadTournaments.php",
	  cache: false,
	  type: "POST",
	  data: "curYear="+curYear,
	  dataType: "json",
	  success: function(data,textStatus) {
		for(i in data) {
			if( (i/2) != Math.floor(i/2))
				cellClass='dataRow evenRow';
			else
				cellClass='dataRow';
				
			// PREP DATA
			startDate=prepDate(data[i]['startDate']);
			endDate=prepDate(data[i]['endDate']);
			// time
			if(data[i]['startTime']!=null)
				startTime=prepTime(data[i]['startTime']);
			else
				startTime='';
			if(data[i]['endTime']!=null)
				endTime=prepTime(data[i]['endTime']);
			else
				endTime='';
			if(startTime!='' && endTime!='')
				timeDisplay="<BR>"+startTime+"-"+endTime;
			else if(startTime!='')
				timeDisplay="<BR>Starts: "+startTime;
			else if(endTime!='')
				timeDisplay="<BR>Ends: "+endTime;
			else
				timeDisplay='';
			// location
			fieldLocation=data[i]['city']+", "+data[i]['state'];
			if(data[i]['field']!=null)
				fieldLocation=data[i]['field']+"<BR>"+fieldLocation;
			// divisions
			if(data[i]['divisions']!=null) {
				curLeague='';
				divs='';
				for(j in data[i]['divisions']) {
					if(curLeague!=data[i]['divisions'][j]['league']) {
						divs+=printLeague(data[i]['divisions'][j]['league'])+"<BR>";
						curLeague=data[i]['divisions'][j]['league'];
					}
					if(j!=0)
						divs+=", ";
					divs+=data[i]['divisions'][j]['name'];
				}
			} else
				divs='';
			// contact
			if(data[i]['contact']!=null)
				contact=data[i]['contact'];
			else
				contact='';
			if(data[i]['phone1']!=null && data[i]['phone2']!='')
				contact+="<BR>"+data[i]['phone1']+" "+data[i]['phone2'];
			else if(data[i]['phone1']!=null && data[i]['phone1']!='')
				contact+="&nbsp;"+data[i]['phone1'];
			else if(data[i]['phone2']!=null && data[i]['phone2']!='')
				contact+="&nbsp;"+data[i]['phone2'];
			if(data[i]['email']!=null)
				contact+="<BR><a href='mailto:"+data[i]['email']+"'>"+data[i]['email']+"</a>";
			// fee
			if(data[i]['fee']!=null)
				fee="$"+data[i]['fee'];
			else
				fee='&nbsp;';

			calendarTable+="<tr><td class='"+cellClass+"'>"+startDate+" - "+endDate+timeDisplay+"</td>";
			calendarTable+="<td class='"+cellClass+"'>"+data[i]['name']+"</td>";
			calendarTable+="<td class='"+cellClass+"'>"+fieldLocation+"</td>";
			calendarTable+="<td class='"+cellClass+"'>"+divs+"</td>";
			calendarTable+="<td class='"+cellClass+"'>"+contact+"</td>";
			calendarTable+="<td class='"+cellClass+" rightCell'>"+fee+"</td></tr>";
			if(data[i]['notes']!=null && data[i]['notes']!='')
				calendarTable+="<tr><td class='"+cellClass+" notesRow' colspan=6>"+data[i]['notes']+"</td></tr>";
		}
	    calendarTable+="</table>";
		$("#tournamentCalContainer").html(calendarTable)
	  }
	});


} // displayTournaments

function prepDate(date) {
	month=date.substr(4,2);
	day=date.substr(6,2);
	return month+"/"+day;
}

function prepTime(time) {
	if (time<100) {
		hr=12;
		mn=time.substr(1,2);
		ampm='AM';
	}
	else if(time<1000) {
		hr=time.substr(0,1);
		mn=time.substr(1,2);
		ampm='AM';
	} else if(time<1200) {
		hr=time.substr(0,2);
		mn=time.substr(1,2);
		ampm='AM';
	} else {
		hr=parseInt(time.substr(0,2))-12;
		mn=time.substr(1,2);
		ampm='PM';
	}
	return hr+":"+mn+" "+ampm;
}

function printLeague(league) {
	switch(league) {
		case '1':	return "Men's"; break;
		case '2':	return "Women's"; break;
		case '3':	return "COED"; break;
	}
}