/* The following function creates an XMLHttpRequest object... */

function comment(){
	document.getElementById('comment').innerHTML = "Comment<br><input name='comment' type='text' title='i.e. Started taking xyz medicine today.' size='32'></input><br><br>";
	isCommentShowing = true;
}

function bmi() {
	document.getElementById('BMI').innerHTML = "BMI<br> <input name='bmi' type='text size='3'></input><br>";
	isBMIShowing = true;
}

function fat() {
        document.getElementById('body-fat').innerHTML = "Body-Fat<br> <input name='fat' type='text size='3'></input>%<br>";
        isFatShowing = true;
}

function tanita() {
        document.getElementById('tanita').innerHTML = " <iframe src='http://rcm.amazon.com/e/cm?t=bloodpressu02-20&o=1&p=8&l=as1&asins=B0009V1YPK&nou=1&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=ffffff&bg1=ffffff&f=ifr' style='width:120px;height:240px;' scrolling='no' marginwidth='0' marginheight='0' frameborder='0'></iframe> <br><br>";
}

function scales2() {
        document.getElementById('scales').innerHTML = " <iframe src='http://rcm.amazon.com/e/cm?t=bloodpressu02-20&o=1&p=8&l=as1&asins=B0007ZH18M&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=ffffff&bg1=ffffff&f=ifr' style='width:120px;height:240px;' scrolling='no' marginwidth='0' marginheight='0' frameborder='0'></iframe> <br><a href='javascript:scales()'>cheaper model </a> | more expensive model<br><br>";
}

function scales() {
        document.getElementById('scales').innerHTML = " <iframe src='http://rcm.amazon.com/e/cm?t=bloodpressu02-20&o=1&p=8&l=as1&asins=B0009V1YPK&nou=1&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=ffffff&bg1=ffffff&f=ifr' style='width:120px;height:240px;' scrolling='no' marginwidth='0' marginheight='0' frameborder='0'></iframe> <br><a href='javascript:scales1()'>cheaper model </a>|<a href='javascript:scales2()'> more expensive model</a><br><br>";
}

function scales1() {
        document.getElementById('scales').innerHTML = " <iframe src='http://rcm.amazon.com/e/cm?t=bloodpressu02-20&o=1&p=8&l=as1&asins=B0007Y6BR0&fc1=000000&IS2=1&lt1=_blank&lc1=0000ff&bc1=ffffff&bg1=ffffff&f=ifr' style='width:120px;height:240px;' scrolling='no' marginwidth='0' marginheight='0' frameborder='0'></iframe> <br>cheaper model | <a href='javascript:scales()'>more expensive model</a><br><br>";
	}

function showEmailForm(){
        document.getElementById('emailForm').innerHTML = "Recipient's Email Address<br><input name='email' type='text' size='32'></input><br><input value='Send' type='button' onclick='emailData();'> </input>";
}


function showDateForm(){
    document.getElementById('dateForm').innerHTML = "Start Date<br><form name='form_delete'><input name='start'id='start_f_date_c' type='text' readonly='1'></input>    <img src='jscalendar-1.0/img.gif' id='start2_f_trigger_c' style='cursor: pointer; border: 1px solid red;' title='Date selector' onmouseover='this.style.background=&#39;Red&#39;;'onmouseout='this.style.background=&#39;&#39;'  />  <br>End Date<br><input name='end' id='end_f_date_c' type='text' readonly='1'></input> <img src='jscalendar-1.0/img.gif' id='end2_f_trigger_c' style='cursor: pointer; border: 1px solid red;' title='Date selector' onmouseover='this.style.background=&#39;Red&#39;;'onmouseout='this.style.background=&#39;&#39;'  /> <br><input value='Delete' type='button' onclick='deleteData();'> </input></form><div id='calendar-container'></div>";
Calendar.setup({ inputField : 'start_f_date_c', ifFormat : '%m/%d/%Y %I:%M %p', button : 'start2_f_trigger_c', align : 'Bc', singleClick : false, showsTime : true });
Calendar.setup({ inputField : 'end_f_date_c', ifFormat : '%m/%d/%Y %I:%M %p', button : 'end2_f_trigger_c', align : 'Bc', singleClick : false, showsTime : true });

}

function emailData(){
       http.open('get', 'https://www.weight-tracker.com/send_data.php?action=send&email=' + document.form_email_stats.email.value);
       http.onreadystatechange = handleEmail;
       document.getElementById('emailForm').innerHTML = "sending email...<br><img src='./ActiveWidgets/runtime/styles/classic/loading.gif' >";
       http.send(null);
}

function removeLatest(){
       http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=remove&date=' + document.form_category_select.date.value);
       http.onreadystatechange = handleProducts;
         document.getElementById('chart').innerHTML = "<img src='./ActiveWidgets/runtime/styles/classic/loading.gif' >";
       http.send(null);
}


function deleteData(){
       http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=deleteRange&startDate=' + document.form_delete.start_f_date_c.value + '&endDate=' + document.form_delete.end_f_date_c.value  );
       http.onreadystatechange = handleProducts;
       document.getElementById('chart').innerHTML = "<img src='./ActiveWidgets/runtime/styles/classic/loading.gif' >";
       http.send(null);
}

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
	/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 
var http2 = createRequestObject();
var isCommentShowing = false;
var isBMIShowing = false;
var isFatShowing = false;

function updateChart(){
	http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=refresh&systolic=' + document.form_category_select.weight.value + '&diastolic=' + '&date=' + document.form_category_select.date.value );
        http.onreadystatechange = handleProducts;
	http.send(null);
}


function hideAvg(){
        http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=hideAvg&systolic=' + document.form_category_select.weight.value + '&date=' + document.form_category_select.date.value);
        http.onreadystatechange = handleHide;
        http.send(null);
}
function showAvg(){
        http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=showAvg&date=' + document.form_category_select.date.value);
        http.onreadystatechange = handleShow;
        http.send(null);
}
			


function reportChart(){
        http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=refresh');
        http.onreadystatechange = handleReport;
       http.send(null);
}

function reportChartSteve(){
        http.open('get', 'http://www.weight-tracker.com/health_wrapper3.php?action=refresh_steve');
        http.onreadystatechange = handleReport;
       http.send(null);
 }

function updateGrid(){
	http2.open('get', 'https://www.weight-tracker.com/basic_wrapper4.php');
        http2.onreadystatechange = handleGrid;
        document.getElementById('grid').innerHTML = "<img src='./ActiveWidgets/runtime/styles/classic/loading.gif' >";
        http2.send(null);

}

function insertData() {
	var comment = '';
	var bmi = '';
	var fat = '';
	if (isCommentShowing == true) {
		comment = document.form_category_select.comment.value;	
	}
	if (isBMIShowing == true) {
		bmi = document.form_category_select.bmi.value;
	}
	if (isFatShowing == true) {
		fat = document.form_category_select.fat.value;
	}
	http.open('get', 'https://www.weight-tracker.com/health_wrapper3.php?action=insert&weight=' + document.form_category_select.weight.value + '&bmi=' + bmi + '&date=' + document.form_category_select.date.value + '&comment=' + comment + '&fat=' + fat);	
	http.onreadystatechange = handleProducts;
          document.getElementById('chart').innerHTML = "<img src='./ActiveWidgets/runtime/styles/classic/loading.gif' >";
	http.send(null);
}

function handleProducts(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
	has a property called readyState with several states:
 	0: Uninitialized
	1: Loading
	2: Loaded
	3: Interactive
	4: Finished */

	if(http.readyState == 4){ //Finished loading the response
	/* We have got the response from the server-side script,
	let's see just what it was. using the responseText property of 
	the XMLHttpRequest object. */
	var response = http.responseText;
	/* And now we want to change the product_categories <div> content.
	we do this using an ability to get/change the content of a page element 
	that we can find: innerHTML. */
	updateGrid();
	document.getElementById('chart').innerHTML = response;
	}
}

function handleReport() {
     if (http.readyState == 4) {
         var response = http.responseText;
         document.getElementById('chart').innerHTML = response;
     }
}

function handleEmail() {
	if (http.readyState == 4) {
	    var response = http.responseText;
	    document.getElementById('emailForm').innerHTML = response;
	}
}

function handleGrid() {
        if (http2.readyState == 4) {
             var response = http2.responseText;
             document.getElementById('grid').innerHTML = response;
        }
}

function handleShow() {
        if (http2.readyState == 4) {
             var response = http.responseText;
	      document.getElementById('chart').innerHTML = response;
             document.getElementById('movingAvg').innerHTML = "<a href='javascript:hideAvg()'>hide moving averages</a>";
          }
}

function handleHide() {
   if (http2.readyState == 4) {
       var response = http.responseText;
       document.getElementById('chart').innerHTML = response;
      document.getElementById('movingAvg').innerHTML = "<a href='javascript:showAvg()'>show moving averages</a>";
    }
}
							  
function openWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
  return false;
  }
