function GetXmlHttpObject() {

	var request_type;

	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer"){
	
	request_type = new ActiveXObject("Microsoft.XMLHTTP");
	
	}else{
	
	request_type = new XMLHttpRequest();
	
	}

return request_type;

}

var http = GetXmlHttpObject(); // We create the HTTP Object

function load_food_diary(date)
{
	if(date == 'other')
	{
		TabbedPanels1.showPanel(4);
		return;
	}
	document.getElementById('home_container').innerHTML = '';
	http.open("GET", "diary_food.php?date="+escape(date), true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			document.getElementById('food_diary_container').innerHTML = http.responseText;
			TabbedPanels1.showPanel(1);
		};
	}
	http.send(null);
}

function load_home()
{
	document.getElementById('food_diary_container').innerHTML = '';
	http.open("GET", "diary_home.php", true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			document.getElementById('home_container').innerHTML = http.responseText;
			TabbedPanels1.showPanel(0);
		};
	}
	http.send(null);
}

function load_diet_calendar()
{
	document.getElementById('food_diary_container').innerHTML = '';
	http.open("GET", "diary_calendar.php", true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			document.getElementById('diary_calendar_container').innerHTML = http.responseText;
			TabbedPanels1.showPanel(3);
		};
	}
	http.send(null);
}

function add_food_item(type, cont)
{
	if(document.getElementById('add_item_cont').parentNode != cont)
	{
		cont.style.background = '#e5e6e8';
		document.getElementById('add_item_cont').parentNode.style.background = '';
		cont.appendChild(document.getElementById('add_item_cont'));
		document.getElementById('food_type1').value = type;
		document.getElementById('add_item_cont').style.display = '';
		
	}
	else
	{
		
		cont.style.background = '';
		if(document.getElementById('add_item_cont').style.display == 'none')
		{
			document.getElementById('add_item_cont').style.display = 'block';
			cont.style.background = '#E4F9FB';
		}
		else
		document.getElementById('add_item_cont').style.display = 'none';
	}
	
}

function submit_yesterdays_food(todayDate)
{
	http.open("GET", "copy_yesterdays.php?nocache="+Math.random(), true);
	http.onreadystatechange = function () 
								{
									if (http.readyState == 4)
									{
										//alert(http.responseText);
										
										load_food_diary(todayDate);
										TabbedPanels1.showPanel(1);
									}
								};
	http.send(null);
}

function submit_food()
{
	food_id = document.getElementById('inputString-id').value;
	food_qty = document.getElementById('food_qty1').value;
	food_name = document.getElementById('inputString').value;
	food_date = document.getElementById('food_date1').value;
	food_type = document.getElementById('food_type1').value;
	
	if(food_name == '')
	{
		alert("Enter a food item.");
		return false;
	}
	var regNum = /^([0-9]+)$/;
	if(food_qty == '' || !regNum.test(food_qty))
	{
		alert("Enter a valid quantity.");
		return false;
	}
	if(food_id == '')
	{
		alert("please select a food");
		return false;
	}
	
	queryString = "food_name="+food_name+"&food_id="+food_id+"&food_qty="+food_qty+"&food_date="+food_date+"&food_type="+food_type+"&formact=Add";
	
	//alert("food_diary_add.php?"+queryString)
	http.open("GET", "diary_food_add.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			load_food_diary(food_date);
			TabbedPanels1.showPanel(1);
		};
	}
	http.send(null);
}

function delete_food(food_id)
{
	queryString = "id="+food_id+"&formact=Del";
	food_date = document.getElementById('food_date1').value;
	//alert("diary_food_add.php?"+queryString)
	http.open("GET", "diary_food_add.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			load_food_diary(food_date);
			TabbedPanels1.showPanel(1);
		};
	}
	http.send(null);	
}
function load_exercise_diary(date)
{
	if(date == 'other')
	{
		TabbedPanels1.showPanel(4);
		return;
	}
	document.getElementById('home_container').innerHTML = '';
	http.open("GET", "diary_exercise.php?date="+escape(date), true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			document.getElementById('exercise_diary_container').innerHTML = http.responseText;
			TabbedPanels1.showPanel(2);
		};
	}
	http.send(null);
}
function show_sleep(){
		
		if(document.getElementById('sleeping').style.display=='none')
		{
			document.getElementById('sleeping').style.display='';
		}
		else if(document.getElementById('sleeping').style.display=='')
		{
			document.getElementById('sleeping').style.display='none';
		}
		if(document.getElementById('resting').style.display=='')
		{
			document.getElementById('resting').style.display='none';
		}
}
function show_rest(){
		
		if(document.getElementById('resting').style.display=='none')
		{
			document.getElementById('resting').style.display='';
		}
		else if(document.getElementById('resting').style.display=='')
		{
			document.getElementById('resting').style.display='none';
		}
		if(document.getElementById('sleeping').style.display=='')
		{
			document.getElementById('sleeping').style.display='none';
		}
}
function show_other(no){
		//alert(no);
		if(document.getElementById('other'+no).style.display=='none')
		{
			document.getElementById('other'+no).style.display='';
		}
		else if(document.getElementById('other'+no).style.display=='')
		{
			document.getElementById('other'+no).style.display='none';
		}
}
/*function save_some_all(no){
	some = document.getElementById('some'+no).value;
	exercise_date =document.getElementById('exerciseDate').value;
	queryString = "some="+some+"&exercise_date="+exercise_date+"&formact=Addsome";
	http.open("GET", "diary_exercise_add.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			load_exercise_diary(exercise_date);
			TabbedPanels1.showPanel(2);
		};
	}
	http.send(null);
}*/

function activitysave(no){
	dir = document.getElementById('direction'+no).value;
	hrs = document.getElementById('hrs'+no).value;
	mins = document.getElementById('mins'+no).value;
	sacrificetype = document.getElementById('sacrificetype'+no).value;
	exercise_date =document.getElementById('exerciseDate'+no).value;
	activity =document.getElementById('activity'+no).value;
	queryString = "activityid="+activity+"&dir="+dir+"&hrs="+hrs+"&mins="+mins+"&exercise_date="+exercise_date+"&sacrificetype="+sacrificetype+"&formact=Add";
	//alert(queryString);
	http.open("GET", "diary_exercise_add.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			if(http.responseText=="error"){
				alert("You have an error in selection");
			}				
			load_exercise_diary(exercise_date);
			TabbedPanels1.showPanel(2);
		};
	}
	http.send(null);
}
function change_div(divname,val){
	if(val=='up'){
		document.getElementById('addTake'+divname).innerHTML = 'Instead Of';	
	}
	if(val=='down'){
		document.getElementById('addTake'+divname).innerHTML = 'Replace with';	
	}
}
function do_activityadd(){
		if(document.getElementById('addActivityHolder').style.display=='none')
		{
			document.getElementById('addActivityHolder').style.display='';
		}
		else if(document.getElementById('addActivityHolder').style.display=='')
		{
			document.getElementById('addActivityHolder').style.display='none';
		}
	
}
function activityadd(no){
	dir = "";
	hrs = document.getElementById('hrs'+no).value;
	mins = document.getElementById('mins'+no).value;
	sacrificetype = document.getElementById('sacrificetype'+no).value;
	exercise_date =document.getElementById('exerciseDate'+no).value;
	activity =document.getElementById('othertype'+no).value;
	queryString = "activityid="+activity+"&hrs="+hrs+"&mins="+mins+"&exercise_date="+exercise_date+"&sacrificetype="+sacrificetype+"&formact=Add";
	//alert(queryString);
	http.open("GET", "diary_exercise_add.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			//alert(http.responseText);
			if(http.responseText=="error"){
				alert("You have an error in selection");
			}				
			load_exercise_diary(exercise_date);
			TabbedPanels1.showPanel(2);
		};
	}
	http.send(null);
}

function diary_details(a,b){	
	mnt =a;
	year =b;
	queryString = "m="+mnt+"&y="+year;
	//alert(queryString);
	http.open("GET", "diary_calendar_ajax.php?"+queryString, true);
	http.onreadystatechange = function () {
		if (http.readyState == 4) {
			document.getElementById('calendar').innerHTML = http.responseText;
			TabbedPanels1.showPanel(4);

		};
	}
	http.send(null);
}

