/*!
 * Ardeo JavaScript Library 
 * This file makes ajax call to display series instances.
 */
 
 	 
	 
	function sendRequest(accessionNumber){
		//var accessionNumber = document.getElementById('accessionNumberID').value;
		var csrfdata=document.getElementById("csrfparameterName").value;
		csrfdata=csrfdata+"="+document.getElementById("csrfparametervalue").value;
		$.ajax({
		   type: "POST",
		   url: "../referrals/applet/loadImage.form?" + csrfdata,
		   data: "accessionNumber="+accessionNumber,
		   success: function(msg){
		   stopProcessImage();
		   var listSize = msg.dcmQrDTOListSize;
		   if(listSize>0){
		    processRequest(msg);  
		   }else{
		   noDataFound();
		   }	  
  		 }
 		});
	 }	
	 
	 
	function processRequest(msg){
		var rowCount=msg.dcmQrDTOListSize ;
		var i=0;
		for (i=0;i<=rowCount;i++){
			addRowToTable(msg.dcmQrDTOList[i]);
		}		
	}
	
	function disableLoadImage(accessionNumber){
	
		document.getElementById('Div'+accessionNumber+'ID1').style.display = 'none';
		document.getElementById('Div'+accessionNumber+'ID2').style.display = 'block';
	}
	
	function addRowToTable(resultRow){
 	  var tbl = document.getElementById('ajaxDataTable');
 	  var accessionNumber = resultRow.accessionNumber;
 	  var modality=resultRow.modality;
 	  var seriesDescription = resultRow.seriesDescription;
 	  var set_seriesDescription;
 	  if(seriesDescription==null || seriesDescription==''){
 	  	set_seriesDescription="No Description Available";
 	  	}
 	  else{
 	  	set_seriesDescription=seriesDescription;
 	  	}
 	 
 	 
 	  
 	  var seriesNumber = resultRow.seriesNumber;
 	 
 	  var numberOfSeriesInstances = resultRow.numberOfSeriesRelatedInstances;
 	  var lastRow = tbl.rows.length;
 	  // if there's no header row in the table, then iteration = lastRow + 1
	  var iteration = lastRow;
	  var row = tbl.insertRow(lastRow);
	  
	  // First TD
	   var contentTD1 = document.createTextNode(modality);
	  var TD1 = document.createElement("TD");
	  TD1.appendChild(contentTD1);
	  row.appendChild(TD1);
	  
	  // Second TD
	  var contentTD2 = document.createTextNode(set_seriesDescription);
	  var TD2 = document.createElement("TD");
	  TD2.appendChild(contentTD2);
	  row.appendChild(TD2);
	  
	   // Third TD
	  var contentTD3 = document.createTextNode(numberOfSeriesInstances);
	  var TD3 = document.createElement("TD");
	  TD3.appendChild(contentTD3);
	  row.appendChild(TD3);
	  
	  // Forth TD
	  var newlink = document.createElement('a');
	  newlink.appendChild (document.createTextNode ('View'));
      newlink.setAttribute('href', "../referrals/applet/loadImageSeries.form?accessionNumber="+accessionNumber+"&seriesDescription="+seriesDescription+"&seriesNumber="+seriesNumber+"&seriesIUID="+resultRow.seriesIUID);

	  var TD4 = document.createElement("TD");
	  TD4.appendChild(newlink);
	  row.appendChild(TD4);
	 }
	
	function startProcessImage(){	

	document.getElementById('processImageID').style.display = 'block';
	
	}
	
	function stopProcessImage(){
		document.getElementById('processImageID').style.display = 'none';
		$('#seriesDetails').show("fast");
	}
	
	function noDataFound(){
		document.getElementById('noDataFoundID').style.display = 'block';
	}
	
	function callAjaxRequest(accessionNumber){
  		startProcessImage();
  		disableLoadImage(accessionNumber);
	    sendRequest(accessionNumber);
	    		
	}