var httpRequest;

function getItems(num, itemSelected){


  cleardirection(num,itemSelected);

  if(itemSelected==''){
    //cleardirection(obj);
    //alert(itemSelected);
    return;
  }

	// Create HTTP request object to communicate with the servlet
	var queryStr = "";
	if (num == 1) {
		queryStr += 'categorySelected=' + itemSelected;
	} else if (num == 2) {
		queryStr += 'categorySelected=' +
					document.getElementById('select_city').options[document.getElementById('select_city').selectedIndex].value +
					'&subjectSelected=' + itemSelected;
	} else if (num == 3) {
		queryStr += 'categorySelected=' +
					document.getElementById('select_city').options[document.getElementById('select_city').selectedIndex].value +'&subjectSelected=' +
					document.getElementById('select_service').options[document.getElementById('select_service').selectedIndex].value + '&topicSelected=' + itemSelected;
	} else if (num == 4) {
		queryStr += 'categorySelected=' +
					document.getElementById('select_city').options[document.getElementById('select_city').selectedIndex].value +
					'&subjectSelected=' +
					document.getElementById('select_service').options[document.getElementById('select_service').selectedIndex].value +
					'&topicSelected=' +
					document.getElementById('select_other').options[document.getElementById('select_other').selectedIndex].value +
					'&lessonSelected=' + itemSelected;
	} else {
		// invalid choice
	}
	

	//var url = 'BookInfo?' + queryStr;
	//var url = 'http://flyworld.com/testprep/BookInfo?categorySelected=Science';
	//var url = 'http://cipadev/0381_trousse/007/data_request.php?categorySelected=5726';
	var url = 'data_request.php?'+queryStr;
        //alert(url);

	if (window.ActiveXObject) {            
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		httpRequest = new XMLHttpRequest();
	}


        //if(req_groups!='n/a'){
        //data_request.php?categorySelected=5726&groups=5726
        //alert(req_groups);
         //alert(url + '&groups='+itemSelected);
	  //httpRequest.open("GET", url, true);
	  httpRequest.open("POST", url + '&groups='+itemSelected, true);
	  httpRequest.onreadystatechange = function() {processSubjectRequest(num); } ;
	  httpRequest.send(null);	
        //}
}

var req_groups='';
// The call back method - if the call is completed when the readyState is 4 and if
// the HTTP is successfull when the status is 200 update the html element(s).
function processSubjectRequest(num){
	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			// Get the response sent by the servlet and update
			// the appropriate HTML box
                        //alert(httpRequest.responseText);
			//updateSelectBox(num, httpRequest.responseText);


                        req_groups=getVar('groups',httpRequest.responseText);

			updateSelectBox(num, getVar('lst',httpRequest.responseText),req_groups);

		} else {
			alert("Error loading page\n" + httpRequest.status + ":" + httpRequest.statusText + ". Please reload the page.");
		}
	}
}

function openPDF(testNum){
	//var theBox = document.getElementById('testBox');

	//window.open('PDFs/' + theBox.options[testNum - 1].text);
}


function disableGetTestBtn(flag){
	//var theBox = document.getElementById('testBox');
	//if (theBox.options[theBox.selectedIndex].text == 'select test')
	//	return;
	var getTestBtn = document.getElementsByName('getTestBtn');
	getTestBtn[0].disabled = flag;
}

function updateSelectBox(num, itemsToDisplay,groups_str){

   //alert('updateSelectBox\r\n' + num + '\r\n' + itemsToDisplay + '\r\n'+ groups_str);

   if(itemsToDisplay=='n/a' && groups_str=='n/a'){
     //return;
   }

	var boxName;
		
	if (num == 1) {
		boxName = 'select_service';
	} else if (num == 2) {
		boxName = 'select_service';
                getInfo();
          return;
	} else if (num == 3) {
		boxName = 'select_Institution';
	} else if (num == 4) {
		boxName = 'testBox';
	} else {
		// invalid choice
	}
	
	var returnElements = itemsToDisplay.split("~~~");
   
        try{
	   var box = document.getElementById(boxName);
           //alert(tmpselectedIndex);
	   // Clear out this select box
	   box.options.length = 0;
        }catch(e){
        }

	// Clear out all boxes that follow the one that we just filled with itemsToDisplay
	for (j = num + 1; j < 5; j++){
		var boxToClearOut;
		// Clear out Topic, Lesson, Test OR Lesson, Test OR Test only.
		if (j == 2) {
			boxToClearOut = 'select_service';
		} else if (j == 3) {
			boxToClearOut = 'select_Institution';
		} else if (j == 4) {
			boxToClearOut = 'testBox';
		} else {
			// invalid choice
		}

		//box = document.getElementById(boxToClearOut);		
		// Clear out this select box
		//box.options.length = 0;
	}

	if (itemsToDisplay == "") {
		alert('No server response for these selections.'); // TODO: leave like this for now
		return;         
	}

	for (i = 0; i < returnElements.length; i++){
		var box = document.getElementById(boxName);

		var item = returnElements[i];

                if(item!='n/a'){
                  //alert(item);
                  //alert(item+ ' ' +item.split('~'),length);
                  //alert(item);

                  if(item.split('~').length>0){
		    //item = replaceAll(item, "&#44;", ",");

                    item_ar = item.split('~');
		    //box.options[i] = new Option(item_ar[1]+item_ar[2]);
		    box.options[i] = new Option(item_ar[1]);
		    box.options[i].value = item_ar[0];
                    box.options[i].entiteid_int = item_ar[2];
                  }else{
		    //item = replaceAll(item, "&#44;", ",");
		    box.options[i] = new Option(item);
		    box.options[i].value = item;
                    box.options[i].entiteid_int = item_ar[2];
                  }
                  

		}


                if( box.options.length>1 ){
                  box.disabled=false;
                }else{
                  box.disabled=true;
                }

		if (num == 2) {
                    getInfo();
		    //var divElemItem = document.getElementById('available_tests_item' + (i + 1));
		    //var divElemItemHeader = document.getElementById('available_tests_item_header' + (i + 1));
		    //var lessonName = document.getElementById('select_Institution').options[document.getElementById('select_Institution').selectedIndex].value;
		    //divElemItem.style.display = "block";
		    //divElemItemHeader.innerHTML = lessonName + " Test " + (i + 1);
		}
	}
	
	return;		
}


function checkbox_sel(objName,oObj,oChk){

//alert(oObj.childNode(0));
  var chkVal = oObj.checked;
  //var obj = document.getElementById(objName);

  //alert(oChk.outerHTML);

  try{
    var tag = oChk.getElementsByTagName('input');
    //alert(tag.length+ ' ' + oChk.checked);
    //alert(tag.length);
    for(var x=1;x<tag.length;x++){
      tag[x].checked =  chkVal;
    }

  document.getElementById('pdf_div').style.display=((chkVal==true)?'inline':'none');

  }catch(e){
  }


}


function map_set(obj){

  //document.getElementById('pdf_div').style.display='none';

//if(obj.selectedIndex==-1){

//  return;
//}

  infofrom = document.getElementById('infofrom_inp').value;
  from           = "from="+infofrom;

  //postalcodeTo = document.getElementById('infoto_inp').value;
  to           = "&to=1040 rue du Parc paquetville";

  url = "?"+from+to;
  //alert(url);
  //map_http('map.php?'+url);
  //alert('2');  


  //var val = obj.options[obj.selectedIndex].value;
  //alert(val);
  //document.getElementById('map_div').style.display='inline';
}

var winmap=null;

function map_show(){

  from = "from=1 boul st-pierre est caraquet";
  to   = "&to=1040 rue du Parc paquetville";

  infofrom = document.getElementById('infofrom_inp').value;
  from           = "from="+infofrom;

  //postalcodeTo = document.getElementById('infoto_inp').value;
  to           = "&to=1040 rue du Parc paquetville";


  url = "?"+from+to;
  //url = "?from=1 boul st-pierre est caraquet&to=1040 rue du Parc paquetville";
  winmap = window.open('map.php'+url,'map','width=630,height=500');
}

function mapset(txt){
  document.getElementById('direction_txt').innerHTML = txt;
  document.getElementById('direction_txt').style.display='inline';
}


var maphttpRequest = null;

function map_http(url){
  	if (window.ActiveXObject) {            
		maphttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		maphttpRequest = new XMLHttpRequest();
	}
	
	//maphttpRequest.open("GET", url, true);
	maphttpRequest.open("POST", url, true);
	//maphttpRequest.open("POST", url, false);
	maphttpRequest.onreadystatechange = function() {map_httpRequest(); } ;
	maphttpRequest.send(null);	
}


function map_httpRequest(){
	if (maphttpRequest.readyState == 4) {
		if(maphttpRequest.status == 200) {

		} else {
			alert("map_http()\r\nError loading page\n" + maphttpRequest.status + ":" + maphttpRequest.statusText + ". Please reload the page.");
		}
	}
}


function info_frm(){
  document.getElementById('info_frm').submit();
}

function infomap_frm(){
   var from = document.getElementById('infofrom_inp').value;

   var oTo = document.getElementById('select_Institution');
   var to=oTo.selectedIndex;
   if(oTo.selectedIndex==-1){
     return;
   }
   var to   = oTo.options[oTo.selectedIndex].value;
   //alert(from+ ' '+ to);
   window.open('map.php?from='+from+'&to='+to+'&locale=fr','map','width=630,height=500,scrollbars=yes');
}

