function WeightChanged() {
 var stones = document.f1.stones.options[document.f1.stones.selectedIndex].value;
 var pounds = document.f1.pounds.options[document.f1.pounds.selectedIndex].value;
 if ( stones == -1 ) stones = 0;
 if ( pounds == -1 ) pounds = 0;
 var kgs = Math.round(((stones * 14) + (pounds * 1)) / 2.205);
 var s = '(equals ' + kgs + ' kg)';
 document.getElementById('kgs').innerHTML = s;
 document.f1.currentweight.value = kgs;
 resetResult();
}

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

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
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/

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

/* Function called to get the activity categories list */
function getActivities(){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
		document contains references to all items on the page
		We can reference document.form_category_select.select_category_select and we will
		be referencing the dropdown list. The selectedIndex property will give us the 
		index of the selected item. 
	*/
	http.open('get', 'caloriev2.html?catid=' 
			+ document.f1.categories.options[document.f1.categories.selectedIndex].value);
	/* Define a function to call once a response has been received. This will be our
		handleActivities function that we define below. */
	http.onreadystatechange = handleActivities; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
        resetResult();
}



function handleActivities(){
	/* 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. */
		document.getElementById('activity_container').innerHTML = response;
	}
}

function getMETValue(){
        /* Create the request. The first argument to the open function is the method (POST/GET),
                and the second argument is the url...
                document contains references to all items on the page
                We can reference document.form_category_select.select_category_select and we will
                be referencing the dropdown list. The selectedIndex property will give us the
                index of the selected item.
        */
        http2.open('get', 'caloriev2.html?actid='
			+ document.f1.activity.options[document.f1.activity.selectedIndex].value);
        /* Define a function to call once a response has been received. This will be our
                handleMETS function that we define below. */
        http2.onreadystatechange = handleMETS;
        /* Send the data. We use something other than null when we are sending using the POST
                method. */
        http2.send(null);
        resetResult();
}

function handleMETS(){
        /* 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(http2.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 = http2.responseText;
                document.f1.mets.value = response;
        }
}


function validateAndCalculateForm() {
    var missing = 0;
    alertMsg='';
    var stones = parseFloat(document.f1.stones.options[document.f1.stones.selectedIndex].value);
    if ( stones == -1 ) {
      missing++;
      alertMsg += "Please select number of stone\n";
    }
    var pounds = parseFloat(document.f1.pounds.options[document.f1.pounds.selectedIndex].value);
    if ( pounds == -1 ) {
      missing++;
      alertMsg += "Please select number of pounds\n";
    }
    var categories = document.f1.categories.options[document.f1.categories.selectedIndex].value;
    if ( categories == 0 ) {
      missing++;
      alertMsg += "Please select an activity category \n";
    }

    try {
      var activity = document.f1.activity.options[document.f1.activity.selectedIndex].value;
      if ( activity == 0 ) {
        missing++;
        alertMsg += "Please select an activity\n";
      }
    } catch(err) {
      missing++;
      alertMsg += "Please select an activity\n";
    }
    var hours = parseFloat(document.f1.hours.options[document.f1.hours.selectedIndex].value);
    if ( hours == -1 ) {
      missing++;
      alertMsg += "Please select number of hours doing activity\n";
    }
    var mins = parseFloat(document.f1.mins.options[document.f1.mins.selectedIndex].value);
    if ( mins == -1 ) {
      missing++;
      alertMsg += "Please select number of minutes doing activity\n";
    }



    if (missing > 0) {
      alert(alertMsg);
          return false;
    } else {
      var hours_total = hours + mins/60;
      var kgs = parseFloat(document.f1.currentweight.value);
      var mets = parseFloat(document.f1.mets.value);
//alert(mins/60);
//msg = "mets: " + mets + " kgs: " + kgs + " hours: " + hours_total;
//alert(msg);
      cals = calculateCaloriesBurnt(mets, kgs, hours_total); 
      document.getElementById('result').innerHTML = cals;
      return false;
    }
}

function calculateCaloriesBurnt(mets, kgs, hours) {
  cals = Math.round(mets * kgs * hours);
  return cals;
}

function resetResult() {
  document.getElementById('result').innerHTML = '&lt;Press Calculate&gt;';
}


