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(); 

function loadBMIForm(){
	
	   if ( document.chooseunits.units[0].checked )
	      units="imperial";
	   else
	      units="metric";

     var url = './bmi.php?mode=drawform&units=' + units;

     http.open('post', url);
          
     http.onreadystatechange = refreshForm;
     /* Send the data. We use something other than null when we are sending using the POST method. */
     try {
       http.send("var=1");
     } catch(e) {}
}

function refreshForm(){
	/* 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;

		
		document.getElementById('bmi').innerHTML = response;
   
	}
}


function getBMI() {
     if ( document.chooseunits.units[0].checked )
	      units="imperial";
	   else
	      units="metric";
	      
	   if ( ValidateForm(units) ) {
       if ( document.bmiform.gender[0].checked )
	       gender="f";
	     else
	       gender="m";
       
       var url = './bmi.php?mode=calcbmi&units=' + units + "&gender=" + gender;
       if (units == "imperial") {
         feet = document.bmiform.heightfeet.options[document.bmiform.heightfeet.selectedIndex].value
         inches = document.bmiform.heightinches.options[document.bmiform.heightinches.selectedIndex].value
         stone = document.bmiform.weightstone.options[document.bmiform.weightstone.selectedIndex].value
         pounds = document.bmiform.weightpounds.options[document.bmiform.weightpounds.selectedIndex].value
         url += "&feet=" + feet + "&inches=" + inches + "&stone=" + stone + "&pounds=" + pounds;
       } else {
         url += "&cms=" + document.bmiform.heightcms.value + "&kilos=" + document.bmiform.weightkilos.value;
       }

       http.open('post', url);
          
       http.onreadystatechange = refreshBMI;
       /* Send the data. We use something other than null when we are sending using the POST method. */
       try {
         http.send("var=1");
       } catch(e) {}
     } 	
}

function refreshBMI(){
	/* 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;

		
		document.getElementById('bmicalcinner').innerHTML = response;
   
	}
}

function resetBMICalculator(units) {
     
    var url = './bmi.php?mode=reset&units=' + units;
    http.open('post', url);
    
    http.onreadystatechange = resetBMICalculator2;
    /* Send the data. We use something other than null when we are sending using the POST method. */
    try {
      http.send("var=1");
    } catch(e) {}
    
}

function resetBMICalculator2(){
	/* 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;

		
		document.getElementById('bmicalcinner').innerHTML = response;
   
	}
}


function ValidateForm(units) {
	
	    
  errors=false;	 
  message='';  
	      
	if ( units == "imperial" ) {
		 feet = document.bmiform.heightfeet.options[document.bmiform.heightfeet.selectedIndex].value
     inches = document.bmiform.heightinches.options[document.bmiform.heightinches.selectedIndex].value
     stone = document.bmiform.weightstone.options[document.bmiform.weightstone.selectedIndex].value
     pounds = document.bmiform.weightpounds.options[document.bmiform.weightpounds.selectedIndex].value
		if ( feet == 0  ) {
			errors=true;
			message += "Please select your height in feet and inches\n";
	  }
	  if ( stone == 0 ) {
			errors=true;
			message += "Please select your weight in stones and pounds\n";
	  }
	 
		  
  } else {
  	if ( document.bmiform.heightcms.value == '' ) {
			errors=true;
			message += "Please select your height in centimetres\n";
	  }
	  if ( document.bmiform.weightkilos.value == '' ) {
			errors=true;
			message += "Please select your weight in kilos\n";
	  }
	 
  	
  }
  if ( errors ) {
	  alert(message);
		return false;
	} else
	  return true;
	
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function validateForm(myform) {
  hasErrors=false;

  var myRe= new RegExp("^[a-zA-Z0-9_\+-]+(\.[a-zA-Z0-9_\+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$");
  if ( ! myRe.test(myform.email.value) ) {
    hasErrors = true;
  }
  if ( hasErrors ) {
     alert("Please enter a valid email address.");
     return false;
  } else {
     return true;

  }

}

function windowOpener(url, name, args) {
  if (typeof(popupWin) != "object"){
    popupWin = window.open(url,name,args);
  } else {
    if (!popupWin.closed){
      popupWin.location.href = url;
    } else {
      popupWin = window.open(url, name,args );
    }
  }
  popupWin.focus();
}
