/**
 *  File for printing resultlist for searching in RAK on searchResult.jsp
 *
 */

function rakReceiverFunc(searchresult){

  //create and set values for parsing next hit in result
  var fmr=0,lastListhit=0, htmlTextVars = "", htmlTextSerie = "", nbOfImages="0", htmlTextDocumentAccessCode = "", htmlTextBladId = "", htmlTextBladNamn = "", htmlTextAr = "", htmlTextBas = "", htmlKtun = "", htmlText = "";
  fmr = parseInt(document.getElementById('firstMatchToReturnRAK').value);

  if(fmr > (searchresult.totalMatches - (fmr + 10))){
    if(searchresult.totalMatches - fmr >= 10){
      lastListhit = fmr + 10 - 1;
    }
    else{lastListhit = searchresult.totalMatches; }
  }
  else{lastListhit = fmr+9;}

  if(searchresult.totalMatches==0){
    fmr=0;
  }

  if(document.getElementById("user").value!="member"){//if user is nonmember
    //parsing hits and adding rows to table
    for(var i=0; i<searchresult.documents.length; i++)
    {
      for(var j=0; j<searchresult.documents[i].paragraphs.length; j++)
      {
        if(searchresult.documents[i].paragraphs[j].name == "ANBI"){
          nbOfImages=searchresult.documents[i].paragraphs[j].text;
        }
      }
    }

    if(parseInt(searchresult.totalMatches)==0 || parseInt(nbOfImages)==0){
      fmr=0;
      searchresult.totalMatches=0;
    }
  }


  //deletes loading-image
  while (document.getElementById("RAKhits").childNodes.length > 0)
  document.getElementById("RAKhits").removeChild(document.getElementById("RAKhits").firstChild);

  //adds header for RAK
  var tbod=document.getElementById("RAKhits");
  var trow = document.createElement("tr");
  var th1 = document.createElement("th");
  if(document.getElementById("user").value=="member"){
      th1.setAttribute("colSpan", "4");
  }
   else{th1.setAttribute("colSpan", "3");}

  th1.appendChild(document.createTextNode(propertyRAKName));
   if(fmr>0){
    var span = document.createElement("span");
    if(searchresult.totalMatches>10)
      span.appendChild(document.createTextNode(" "+fmr + "-" + (lastListhit) + " "+propertyTotal+" " + searchresult.totalMatches+" "+propertyHits));
    else if(searchresult.totalMatches==1)
      span.appendChild(document.createTextNode(" "+searchresult.totalMatches+" "+propertyHit));
    else
      span.appendChild(document.createTextNode(" "+searchresult.totalMatches+" "+propertyHits));
    th1.appendChild(span);
   }
   var linkInfo = document.createElement("a");
      linkInfo.setAttribute("href", "javascript:showHideRAKInfo();");
       var imgInfo = document.createElement("img");
      imgInfo.src="../images/info.gif";
      imgInfo.alt=propertyHelptext;
      imgInfo.title=propertyHelptext;
      linkInfo.appendChild(imgInfo);
      th1.appendChild(linkInfo);
      trow.appendChild(th1);
      tbod.appendChild(trow);


    var trowInfo = document.createElement("tr");
    trowInfo.setAttribute("id","rakInfo");
     trowInfo.className='descriptionHidden';
    var tdInfo = document.createElement("td");
	 tdInfo.setAttribute("id","rakInfoTD");
    if(document.getElementById("user").value=="member"){
      tdInfo.setAttribute("colSpan", "4");
    }
    else{tdInfo.setAttribute("colSpan", "3");}
     var spanInfo = document.createElement("span");
     spanInfo.appendChild(document.createTextNode(propertyRAKDescription));
     tdInfo.appendChild(spanInfo);
     trowInfo.appendChild(tdInfo);
     tbod.appendChild(trowInfo);

  if(fmr==0){
    tbod.appendChild(createInfoRow(propertyNoHits));
  }else if(fmr > 0 && searchresult.documents.length == 0){
    tbod.appendChild(createInfoRow(propertyToManyHits));
  }else{

  //parsing hits and adding rows to table
  for(var i=0; i<searchresult.documents.length; i++)
  {
   htmlTextSerie = ""
   htmlTextAr ="";
   nbOfImages="0";
    for(var j=0; j<searchresult.documents[i].paragraphs.length; j++)
    {
        if(searchresult.documents[i].paragraphs[j].name == "RAKI"){
        	htmlTextBladId = searchresult.documents[i].paragraphs[j].text;
        	htmlTextVars += "&page=" + searchresult.documents[i].paragraphs[j].text;
			var serie="";
			serie=htmlTextBladId.substring(0,3);
			if(serie=="J11"){
				htmlTextSerie=propertyHundred;
			}
			else if(serie=="J13"){
			 	htmlTextSerie=propertyEconomical;
			}
			else{
				htmlTextSerie=propertyGeneral;
			}
        }

        if(searchresult.documents[i].paragraphs[j].name == "BLAD"){
        htmlTextBladNamn = searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "BASE"){
        htmlTextBas = searchresult.documents[i].paragraphs[j].text;
        htmlTextVars += "&database=" + searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "KAAR"){
        htmlTextAr =  searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "DAAR"){
        htmlTextAr =  searchresult.documents[i].paragraphs[j].text;
        }
		
        if(searchresult.documents[i].paragraphs[j].name == "ANBI"){
          nbOfImages=searchresult.documents[i].paragraphs[j].text;
        }
		if(searchresult.documents[i].paragraphs[j].name == "KTUN"){
          htmlKtun=searchresult.documents[i].paragraphs[j].text;
        }

    }
    //create and adds table cells with values
     var trow = document.createElement("tr");
     var td1 = document.createElement("td");
     var images="";
     if(nbOfImages=="1")
      images=propertyImage;
     else
      images=propertyImages;
    if(searchresult.documents[i].documentAccessCode != "" && nbOfImages!="0"){
      var link = document.createElement("a");
      link.setAttribute("href", "show.html?showmap=true&archive=RAK&sd_base="+htmlTextBas+"&sd_ktun="+htmlKtun+"&archive=RAK");
       if(document.getElementById("user").value=="member")
        link.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId+" ("+nbOfImages+" "+images+")"));
       else
        link.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId));
      td1.appendChild(link);
    }
    else{
      if(document.getElementById("user").value=="member"){
        td1.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId+" ("+nbOfImages+" "+images+")"));
      }
      else{
        td1.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId));
      }
    }

     var td2 = document.createElement("td");
     if(htmlTextSerie!=""){
      td2.appendChild(document.createTextNode(htmlTextSerie));
     }
     else{
        td2.appendChild(document.createTextNode("-"));
      }

     var td3 = document.createElement("td");
     if(htmlTextAr!=""){
      td3.appendChild(document.createTextNode(htmlTextAr));
     }
     else{
        td3.appendChild(document.createTextNode("-"));
      }

     trow.appendChild(td1);
     trow.appendChild(td2);
     trow.appendChild(td3);
     if(document.getElementById("user").value=="member"){
       var td4 = document.createElement("td");
       var link = document.createElement("a");
       link.setAttribute("href", "show.html?showmap=false&sd_base="+htmlTextBas+"&sd_ktun="+htmlKtun+"&archive=RAK");
       link.appendChild(document.createTextNode("Info"));
       td4.appendChild(link);
       trow.appendChild(td4);
      }
     tbod.appendChild(trow);

   }

   //Next hit in searchresultlist
   var l=1;
   var bolForegaende=false;
   var current=0;
   var htmlTextF="";

  trow = document.createElement("tr");
  td1 = document.createElement("td");
  if(document.getElementById("user").value=="member"){
   td1.setAttribute("colSpan", "4");
   }
   else{td1.setAttribute("colSpan", "3");}
  td1.className='pages';


   if( Math.ceil(fmr/10)  > 10+1){//sets start-value for l in for-loop
   l = Math.ceil(fmr/10) - 10;
 }

 if(lastListhit>=10){//Dont write any numbers if lastListhit>=10

 for(l; l<=(Math.ceil(searchresult.totalMatches/10)); l++){

    if((l*(10-1)) > searchresult.totalMatches+10 || (l >= Math.ceil(fmr/10) + 10))
    {
      break;
    }
    else{
      if(((l*10) >= fmr) && ((l*10)<= fmr+10)){
        if(parseInt(searchresult.totalMatches)!=10){
          var strongele = document.createElement("strong");  //create link with number
          strongele.appendChild(document.createTextNode(parseInt(l)));
          td1.appendChild(strongele);
        }
        current=l;
      }
      else{
        var link = document.createElement("a");  //create link with number
        link.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(((l*10)+1)-10)+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
        link.appendChild(document.createTextNode(parseInt(l)));
        td1.appendChild(link);
      }
    }
  }

  if((l-1)!=current || lastListhit<searchresult.totalMatches){
    var link = document.createElement("a");  //create link with next
    link.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(fmr+10)+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    link.appendChild(document.createTextNode(propertyNext));
    td1.appendChild(link);
  }
  if(current>1){
    //create link with previous
    var linkPrevious = document.createElement("a");
    linkPrevious.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(fmr-10)+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    linkPrevious.appendChild(document.createTextNode(propertyPrevious));
    td1.insertBefore(linkPrevious,td1.firstChild);
    //create link with first
    var linkFirst = document.createElement("a");
    linkFirst.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK=1&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    linkFirst.appendChild(document.createTextNode(propertyFirst));
    td1.insertBefore(linkFirst,td1.firstChild);
   }
}
  if(lastListhit>=10 && (searchresult.totalMatches <10 || searchresult.totalMatches >10)){
    trow.appendChild(td1);
    tbod.appendChild(trow);
  }
  }

}

function rakReceiverFuncAdvanced(searchresult)
{
 //create and set values for parsing next hit in result
  var fmr=0,lastListhit=0, htmlTextVars = "", nbOfImages="0"; htmlTextSerie = "",  htmlKtun = "", htmlTextDocumentAccessCode = "", htmlTextBladId = "", htmlTextBladNamn = "", htmlTextAr = "", htmlTextBas = "", htmlText = "";
  fmr = parseInt(document.getElementById('firstMatchToReturnRAK').value);
  
  if(document.getElementById("series") && document.getElementById("series").value =="1"){//set maptype
  	htmlTextSerie = "Ekonomiska kartan";
  }
  else if(document.getElementById("series") && document.getElementById("series").value =="2"){//set maptype
  	htmlTextSerie = "Häradsekonomiska kartan";
  }
  else if(document.getElementById("series") && document.getElementById("series").value =="3"){//set maptype
  	htmlTextSerie = "Generalstabskartan";
  }


  if(fmr > (searchresult.totalMatches - (fmr + 10))){
    if(searchresult.totalMatches - fmr >= 10){
      lastListhit = fmr + 10 - 1;
    }
    else{lastListhit = searchresult.totalMatches; }
  }
  else{lastListhit = fmr+9;}

  if(searchresult.totalMatches==0){
    fmr=0;
  }

  if(document.getElementById("user").value!="member"){//if user is nonmember
    //parsing hits and adding rows to table
    for(var i=0; i<searchresult.documents.length; i++)
    {
      for(var j=0; j<searchresult.documents[i].paragraphs.length; j++)
      {
        if(searchresult.documents[i].paragraphs[j].name == "ANBI"){
          nbOfImages=searchresult.documents[i].paragraphs[j].text;
        }
      }
    }

    if(parseInt(searchresult.totalMatches)==0 || parseInt(nbOfImages)==0){
      fmr=0;
      searchresult.totalMatches=0;
    }
  }

  //deletes loading-image
  while (document.getElementById("RAKhits").childNodes.length > 0)
  document.getElementById("RAKhits").removeChild(document.getElementById("RAKhits").firstChild);

  //adds header for RAK
  var tbod=document.getElementById("RAKhits");
  var trow = document.createElement("tr");
  var th1 = document.createElement("th");

  if(document.getElementById("user").value=="member"){
    th1.setAttribute("colSpan", "4");
  }
  else{
    th1.setAttribute("colSpan", "3");
  }

  th1.appendChild(document.createTextNode(propertyRAKName));
    var span = document.createElement("span");
    if(searchresult.totalMatches>10)
      span.appendChild(document.createTextNode(" "+fmr + "-" + (lastListhit) + " "+propertyTotal+" " + searchresult.totalMatches+" "+propertyHits));
    else if(searchresult.totalMatches==1)
      span.appendChild(document.createTextNode(" "+searchresult.totalMatches+" "+propertyHit));
    else
      span.appendChild(document.createTextNode(" "+searchresult.totalMatches+" "+propertyHits));
    th1.appendChild(span);


  trow.appendChild(th1);
  tbod.appendChild(trow);

  if(fmr==0){
    tbod.appendChild(createInfoRow(propertyNoHits));
  }else if(fmr > 0 && searchresult.documents.length == 0){
    tbod.appendChild(createInfoRow(propertyToManyHits));
  }else{

  //parsing hits and adding rows to table
  for(var i=0; i<searchresult.documents.length; i++)
  {
    for(var j=0; j<searchresult.documents[i].paragraphs.length; j++)
    {
        if(searchresult.documents[i].paragraphs[j].name == "RAKI"){
        	htmlTextBladId = searchresult.documents[i].paragraphs[j].text;
        	htmlTextVars += "&page=" + searchresult.documents[i].paragraphs[j].text;
			var serie="";
			serie=htmlTextBladId.substring(0,3);
			if(serie=="J11"){
				htmlTextSerie=propertyHundred;
			}
			else if(serie=="J13"){
			 	htmlTextSerie=propertyEconomical;
			}
			else{
				htmlTextSerie=propertyGeneral;
			}
        }

        if(searchresult.documents[i].paragraphs[j].name == "BLAD"){
        htmlTextBladNamn = searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "BASE"){
        htmlTextBas = searchresult.documents[i].paragraphs[j].text;
        htmlTextVars += "&database=" + searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "KAAR"){
        htmlTextAr =  searchresult.documents[i].paragraphs[j].text;
        }

        if(searchresult.documents[i].paragraphs[j].name == "DAAR"){
        htmlTextAr =  searchresult.documents[i].paragraphs[j].text;
        }
		
        if(searchresult.documents[i].paragraphs[j].name == "ANBI"){
          nbOfImages=searchresult.documents[i].paragraphs[j].text;
        }
		
		if(searchresult.documents[i].paragraphs[j].name == "KTUN"){
          htmlKtun=searchresult.documents[i].paragraphs[j].text;
        }
		
    }

    //create and adds table cells with values
     var trow = document.createElement("tr");

     var td1 = document.createElement("td");
     var images="";
     if(nbOfImages=="1")
        images=propertyImage;
     else
        images=propertyImages;
    if(searchresult.documents[i].documentAccessCode != "" && nbOfImages!="0"){
      var link = document.createElement("a");
      link.setAttribute("href", "show.html?showmap=true&archive=RAK&sd_base="+htmlTextBas+"&raki="+htmlTextBladId+"&sd_ktun="+htmlKtun+"&nbOfImages="+nbOfImages);

      if(document.getElementById("user").value=="member")
        link.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId+" ("+nbOfImages+" "+images+")"));
      else
        link.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId));
      td1.appendChild(link);
    }
    else{
      if(document.getElementById("user").value=="member"){
        td1.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId+" ("+nbOfImages+" "+images+")"));
      }
      else{
        td1.appendChild(document.createTextNode(htmlTextBladNamn+" "+htmlTextBladId));
      }
    }

     var td2 = document.createElement("td");
     if(htmlTextSerie!=""){
      td2.appendChild(document.createTextNode(htmlTextSerie));
     }
     else{
        td2.appendChild(document.createTextNode("-"));
      }

     var td3 = document.createElement("td");
     if(htmlTextAr!=""){
      td3.appendChild(document.createTextNode(htmlTextAr));
     }
     else{
        td3.appendChild(document.createTextNode("-"));
      }

      if(document.getElementById("user").value=="member"){
        var link = document.createElement("a");
        link.setAttribute("href", "show.html?showmap=false&archive=RAK&sd_base="+htmlTextBas+"&raki="+htmlTextBladId+"&sd_ktun="+htmlKtun+"&nbOfImages="+nbOfImages);
        link.appendChild(document.createTextNode("Info"));
        var td4 = document.createElement("td");
        td4.appendChild(link);
      }


     trow.appendChild(td1);
     trow.appendChild(td2);
     trow.appendChild(td3);
     if(document.getElementById("user").value=="member"){
      trow.appendChild(td4);
     }
     tbod.appendChild(trow);

   }  //Next hit in searchresultlist
   var l=1;
   var bolForegaende=false;
   var current=0;
   var htmlTextF="";

  trow = document.createElement("tr");
  td1 = document.createElement("td");

  if(document.getElementById("user").value=="member"){
   td1.setAttribute("colSpan", "4");
   }
   else{
    td1.setAttribute("colSpan", "3");
  }
  td1.className='pages';


   if( Math.ceil(fmr/10)  > 10+1){//sets start-value for l in for-loop
   l = Math.ceil(fmr/10) - 10;
 }

 if(lastListhit>=10){//Dont write any numbers if lastListhit>=10

 for(l; l<=(Math.ceil(searchresult.totalMatches/10)); l++){

    if((l*(10-1)) > searchresult.totalMatches+10 || (l >= Math.ceil(fmr/10) + 10))
    {
      break;
    }
    else{
      if(((l*10) >= fmr) && ((l*10)<= fmr+10)){
        var strongele = document.createElement("strong");  //create link with number
        strongele.appendChild(document.createTextNode(parseInt(l)));
        td1.appendChild(strongele);
        current=l;
      }
      else{
        var link = document.createElement("a");  //create link with number
        link.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(((l*10)+1)-10)+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
        link.appendChild(document.createTextNode(parseInt(l)));
        td1.appendChild(link);
      }
    }
  }

  if((l-1)!=current || lastListhit<searchresult.totalMatches){
    var link = document.createElement("a");  //create link with next
    link.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(fmr+10)+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    link.appendChild(document.createTextNode(propertyNext));
    td1.appendChild(link);
  }
  if(current>1){
    //create link with previous
    var linkPrevious = document.createElement("a");
    linkPrevious.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK="+parseInt(fmr-10)+"&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    linkPrevious.appendChild(document.createTextNode(propertyPrevious));
    td1.insertBefore(linkPrevious,td1.firstChild);
    //create link with first
    var linkFirst = document.createElement("a");
    linkFirst.setAttribute("href", "searchresult.html?archive="+document.getElementById('archive').value+"&firstMatchToReturnRAK=1&firstMatchToReturnREG="+document.getElementById('firstMatchToReturnREG').value+"&firstMatchToReturnLMS="+document.getElementById('firstMatchToReturnLMS').value+"&yMin="+document.getElementById('yMin').value+"&xMin="+document.getElementById('xMin').value+"&yMax="+document.getElementById('yMax').value+"&xMax="+document.getElementById('xMax').value);
    linkFirst.appendChild(document.createTextNode(propertyFirst));
    td1.insertBefore(linkFirst,td1.firstChild);
   }
}

  if(lastListhit>=10 && (searchresult.totalMatches <10 || searchresult.totalMatches >10)){
    trow.appendChild(td1);
    tbod.appendChild(trow);
  }
  }

}
