function makeSelection(arg) {
  if (arg == '1') {
   var clabssi = document.getElementById('clabssi');
   clabssi.style.display = 'block';
   var profilehospasc = document.getElementById('profilehospasc');
   profilehospasc.style.display = 'none'; 
   var profilehospascdiv = document.getElementById('profilehospascdiv');
   profilehospascdiv.style.display = 'none'; 
  } else if(arg == '2') {
   var clabssi = document.getElementById('clabssi');
   clabssi.style.display = 'none'; 
   var mainclabdiv = document.getElementById('mainclabdiv');
   mainclabdiv.style.display = 'none'; 
   var mainssidiv = document.getElementById('mainssidiv');
   mainssidiv.style.display = 'none'; 
   var ssihospascdiv = document.getElementById('ssihospascdiv');
   ssihospascdiv.style.display = 'none'; 
   var dis_region = document.getElementById('dis_region');
   dis_region.style.display = 'none'; 
   var profilehospasc = document.getElementById('profilehospasc');
   profilehospasc.style.display = 'block'; 
   var getid = document.getElementById('mainvapdiv');
   getid.style.display = 'none';
  }
}

function displayClabs(arg) {
 var mainssidiv = document.getElementById('mainssidiv');
 mainssidiv.style.display = 'none';
 var ssihospascdiv = document.getElementById('ssihospascdiv');
 ssihospascdiv.style.display = 'none';
 var getid = document.getElementById('mainclabdiv');
 getid.style.display = 'block';
 var stepnum = document.getElementById('stepnum');
 stepnum.innerHTML = 'Step Four';
 var dis_region = document.getElementById('dis_region');
 dis_region.style.display = 'block';
}

function displaySsi(arg) {
 var mainssidiv = document.getElementById('mainssidiv');
 mainssidiv.style.display = 'block';
 var getid = document.getElementById('mainclabdiv');
 getid.style.display = 'none';
 var dis_region = document.getElementById('dis_region');
 dis_region.style.display = 'none';
}


function displayVaps(arg) {
 var mainssidiv = document.getElementById('mainssidiv');
 mainssidiv.style.display = 'none';
 var ssihospascdiv = document.getElementById('ssihospascdiv');
 ssihospascdiv.style.display = 'none';
 var getid = document.getElementById('mainvapdiv');
 getid.style.display = 'block';
 var stepnum = document.getElementById('stepnum');
 stepnum.innerHTML = 'Step Four';
 var dis_region = document.getElementById('dis_region');
 dis_region.style.display = 'block';
}

function makeHospitalAsc(arg) {
 var ssihospascdiv = document.getElementById('ssihospascdiv');
 if (arg == 'ssihospdiv') {
   ssihospascdiv.style.display = 'block';
   var control = document.getElementById('ssihospascselect');

   for (var q=control.options.length; q>=0; q--) {  
     control.options[q]=null;
   }

   var elem = document.createElement('option');
   var elemtxt = document.createTextNode('---- Select surgery type ----');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","");
   control.appendChild(elem);
   
   elem = document.createElement('option');
   elemtxt = document.createTextNode('Abdominal Hysterectomy');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","ABDOMINAL HYSTERECTOMY:H");
   control.appendChild(elem);
   elem = document.createElement('option');
   
   elemtxt = document.createTextNode('Hip Repair');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","HIP PROSTHESIS:H");
   control.appendChild(elem);
   elem = document.createElement('option');
   elemtxt = document.createTextNode('Coronary artery bypass surgery');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","CABG:H");
   control.appendChild(elem);
 } else if( arg == 'ssiascdiv') {
   ssihospascdiv.style.display = 'block';
   var control = document.getElementById('ssihospascselect');

   for (var q=control.options.length; q>=0; q--) {
     control.options[q]=null;
   }
   
   var elem = document.createElement('option');
   var elemtxt = document.createTextNode('---- Select surgery type ----');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","");
   control.appendChild(elem);

   elem = document.createElement('option');
   elemtxt = document.createTextNode('Breast Surgery');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","BREAST SURGERY:A");
   control.appendChild(elem);
   elem = document.createElement('option');
   elemtxt = document.createTextNode('Hernia Repair');
   elem.appendChild(elemtxt);
   elem.setAttribute("value","HERNIORRHAPHY:A");
   control.appendChild(elem);
 }
 var stepnum = document.getElementById('stepnum');
 stepnum.innerHTML = 'Step Five';
 var dis_region = document.getElementById('dis_region');
 dis_region.style.display = 'block';
}
 function form2submit(idx) {

  document.newform.region.selectedIndex = idx;
  if (document.getElementById('mainclabdiv').style.display == 'block') {

	if(document.newform.icu_type.value == "") {
		alert("Please select an ICU type in Step Three.");
		
		return false;
	}

    document.newform.action = "main.php";
  } else if (document.getElementById('ssihospascdiv').style.display == 'block') {
    document.newform.action = "main_ssi.php";
    if(document.newform.ssihospascselect.value == "") {
		alert("Please select a surgery type in Step Four.");
		
		return false;
	}
  } else if (document.getElementById('mainvapdiv').style.display == 'block'){
	  
	  if(document.newform.vap_icu_type.value == "") {
		alert("Please select an ICU type in Step Three.");
		
		return false;
	   }
	   
	   document.newform.action = "vap_comp.php";
	  
  }
  
   else {
    document.newform.action = "main.php";
  }
  document.newform.target='_top';
  document.newform.submit();
 }

 function formsubmit() {
  document.newform.action = "main_profile.php";
  document.newform.target='_top';
  document.newform.submit();
 }

/* 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(); 

/* Function called to get the product categories list */
function getProducts(region,inf_type,spec_inf_type){
	/* 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', 'internal_request.php?region=' + region + '&inf_type=' + inf_type + '&spec_inf_type=' + spec_inf_type); 
	/* Define a function to call once a response has been received. This will be our
		handleProductCategories function that we define below. */
	http.onreadystatechange = handleProducts; 
	/* Send the data. We use something other than null when we are sending using the POST
		method. */
	http.send(null);
}

/* Function called to handle the list that was returned from the internal_request.php file.. */
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. */
		document.getElementById('form2region').innerHTML = response;
	}
}

function profileHospitalAsc(arg) {
  var control = document.getElementById('profilehospascselect');
   for (var q=control.options.length; q>=0; q--) {
     control.options[q]=null;
   }
  if (arg == 'hospdiv') {
     getFacilities('H');    
  } else if (arg == 'ascdiv') {
     getFacilities('A');    
  }
}

function getFacilities(type_of_facility){
	http.open('get', 'internal_array.php?type_of_facility=' + type_of_facility); 
        var msg = document.getElementById('msg');
        msg.style.display = 'block';
	http.onreadystatechange = handleFacility; 
	http.send(null);
}

function handleFacility(){
	if(http.readyState == 4 ){ //Finished loading the response
	  var response = eval('('+ http.responseText+')');
//	  document.writeln(response.facilitylisting.length);
//          return;
          var val = '';
          var profilehospascdiv = document.getElementById('profilehospascdiv');
          profilehospascdiv.style.display = 'block';
          var control = document.getElementById('profilehospascselect');
          for (var q=control.options.length; q>=0; q--) {
            control.options[q]=null;
          }
          var elem = document.createElement('option');
          elem.text = '                             - - - - - - - - - - -                ';
          elem.value = '';
          control.options.add(elem);
          for (i=0;i<(response.facilitylisting.length) ;i++) {
//          val = val + response.facilitylisting[i].county;
            populate_drp_down(response.facilitylisting[i].id,
                              response.facilitylisting[i].name,
                              response.facilitylisting[i].county,
                              document.newform.profilehospascselect);
          }
          var msg = document.getElementById('msg');
          msg.style.display = 'none';
//           document.write(val); 
	}
   return;
}
function populate_drp_down(id,name,county,control) {
   var elem = document.createElement('option');
   elem.text = name+ ' - ' + county;
   elem.value = id;
   control.options.add(elem);
}
function countMe(obj, targetID) {
     var temp = new Array();
     var target = document.getElementById(targetID);
     temp = obj.value.split(' ');
//     target.innerHTML = 'Count: ' + obj.value.length;
     target.innerHTML = '<b>Count: ' + temp.length+'</b>';
     if (temp.length > 100) {
      alert("Number of words > 100");
     }
}
function verify() {
 var target = document.getElementById('myText');
 var arr = target.value.split(' ');
 var len = arr.length;
 if (len > 100) {
   alert("Cannot submit form. Number of words > 100");
   return false;
 } else {
   document.myform.submit();
 }
}


