addEventListener("load", function(event)
{
    setTimeout(scrollTo, 0, 0, 1);
}, false);

function $(id) { return document.getElementById(id); }

function ShowAppt(id)
{	
	swipePage($('list'), $('detail'), false, function(){
		$('detail').style.display = 'block';
		$('list').style.display = 'none';
	});

	$('detailView').innerHTML = '<h2>Loading...</h2>';
	
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4) {
			$('detailView').innerHTML = xhr.responseText;
		}
	}
	xhr.open("GET","iphone.php?detail="+id,true);
	xhr.send(null);
}

function ShowPage(pageDivId, divId, requestUrl)
{	
	swipePage($('list'), $(pageDivId), false, function(){
		$(pageDivId).style.display = 'block';
		$('list').style.display = 'none';
	});

	$(divId).innerHTML = '<h2>Loading...</h2>';
	
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4) {
			$(divId).innerHTML = xhr.responseText;
		}
	}
	xhr.open("GET",requestUrl,true);
	xhr.send(null);
}


function ShowNewAppt()
{	
	ShowPage('newAppt', 'newApptView', 'iphone.php?action=newappt');
}


function ShowList()
{
	swipePage($('detail'), $('list'), true, function(){
		$('detail').style.display = 'none';
		$('newAppt').style.display = 'none';
		$('list').style.display = 'block';
	});
}

function swipePage(fromPage, toPage, backwards, finished)
{
	var animateX = -20;
	var animateInterval = 24;
	fromPage.style.display = 'block';
	toPage.style.display = 'block';
	fromPage.style.position = 'absolute';
	toPage.style.position = 'absolute';
    toPage.style.left = "100%";
    scrollTo(0, 1);
    
    var percent = 100;
    var timer = setInterval(function()
    {
        percent += animateX;
        if (percent <= 0)
        {
            percent = 0;
            clearInterval(timer);
            finished();
        }

        fromPage.style.left = (backwards ? (100-percent) : (percent-100)) + "%"; 
        toPage.style.left = (backwards ? -percent : percent) + "%"; 
    }, animateInterval);
}


function $F(id)
{
	return $(id).options[$(id).selectedIndex].value;
}

function showTimes()
{
	$('time').innerHTML = 'Loading...';
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange=function() {
		if(xhr.readyState==4) {
			$('time').innerHTML = xhr.responseText;
		}
	}
	xhr.open("GET","iphone.php?times="+$F('day')+'-'+$F('month')+'-'+$F('year')+'&t='+$F('type')+'&c='+$F('calendarID'),true);
	xhr.send(null);
}