var http_request = false; 
var attContTitle="";
   
   function makeRequest(url,docTitle) {
  // alert("in make request");
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
	  	 //alert("in Mozilla");
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
	  //alert("in IE");
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         //alert('Cannot create XMLHTTP instance');
         return false;
      }
	  //alert("Final url is "+url);
      http_request.onreadystatechange = parseXmlRead;
      http_request.open('GET', url , true);
      http_request.send(null);
   }

function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link");
  fileref.setAttribute("rel", "stylesheet");
  fileref.setAttribute("type", "text/css");
  fileref.setAttribute("href", filename);
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref);
}


function doSearch(roadMapID,attDocType,docTitle) {


	loadjscssfile(location.host+"/c/dam/en/us/support/docs/common/cdcPreview.css","css");
	attContTitle = docTitle;
	 //alert("in do search roadMapID "+roadMapID); 
	 //alert("in do search  attDocType "+attDocType); 
	 //alert("in do search docTitle "+docTitle);
	 var url = findUrl(attDocType, roadMapID);	
	 if (url != "")
	 makeRequest(url,docTitle);
	 else  
	 displayOldDocList();
}
function addrow(tablename, arr) {
	var unOrderdList = document.getElementById(tablename);
	var count=0;

	for (r = 0; r < arr.length; r = r + 2) {
		if(count==5)
			break;

		//alert("currnet Title:"+arr[r + 1]+"\t"+"document Title:"+attContTitle);
		if ((arr[r + 1]).toUpperCase()  == attContTitle.toUpperCase()){
		
		//alert("Ignore matched title :"+attContTitle);
			continue;
		}

		var li = document.createElement("li");

		//var p = document.createTextNode('\u2022' + ' ');
		//li.appendChild(p);

		var anchorTag = document.createElement('a');
		anchorTag.setAttribute('target', "_blank");
		anchorTag.setAttribute('href', arr[r]+"?referring_site=smartnavRD");
		if(count==4)
		anchorTag.appendChild(document.createTextNode(arr[r + 1]+"."));
		else
 		anchorTag.appendChild(document.createTextNode(arr[r + 1]));
		li.appendChild(anchorTag);

		unOrderdList.appendChild(li);
		count++;	
		//alert("Rows added :"+count);
	}	
}

function parseXmlRead() {

	      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
		var xml = http_request.responseText;
//alert("in parseXmlRead  "+xml);
var foundHIT = false;
var arr = new Array();
//alert("Result XML = "+xml);
var jsonObject = eval("("+ xml + ")");
//alert("jsonObject.RESULTSET.HIT.TITLE  "+jsonObject.RESULTSET.HIT[0].FIELD.length );
// document.write(jsonObject.RESULTSET.HIT[0]['@title']);
// document.write('<br/>');

var hitField = jsonObject.RESULTSET.HIT;

//alert("hitField.length "+hitField.length);
for (var h in hitField) {
var p =jsonObject.RESULTSET.HIT[h].FIELD;
/*if(h==0){
	alert('Document 111111 TITLE:'+attContTitle);
	alert('First 111111111TITLE: hitField[h][@title] '+hitField[h]['@title']);
}*/
if(attContTitle==hitField[h]['@title']){
	//alert('FOUND xml TITLE:'+attContTitle);
	var countkey =1;
	var countkeyCount =12;

for (var key in p) { foundHIT=true; 
    
	if(attContTitle == p[key]['#']){		
		countkeyCount = 12;
		arr.splice(arr.length-1,1);		
		countkey = countkey +1;		
		continue;
	}
  
  if (p.hasOwnProperty(key)) {
	   arr[arr.length] = p[key]['#'];
	}
	   if(countkey==countkeyCount) break;
	   countkey = countkey +1;
  }
  break; // the HIT loop
}

  }
 
}
addrow("relatedDocumentsUL", arr);

	if (!foundHIT) {
		//alert("Calling default values to display:"+foundHIT);
		displayOldDocList();
	}else{
		toggle('firstFiveDcosRelDocs');
	}

}
}





function displayOldDocList() {
	var firstFiveDcosRelDocs = document.getElementById("firstFiveDcosRelDocs");
	firstFiveDcosRelDocs.style.display = '';
}

function addDiscutionURLs(ciscoReadWrite){if(typeof(ciscoReadWrite)=='undefined' || ciscoReadWrite == null){  toggle('discussionMainHead'); return;}var threadFlag='false'; var ciscoThreads=ciscoReadWrite.threads; var out=""; var discURLs = new String();discURLs = discURLs+'<ul>';var disCount=0; for (var wordss in ciscoThreads){ if(disCount>=5){break;}disCount=disCount+1; out=ciscoThreads[wordss]; var name = out['name']; var url = out['url']; threadFlag = 'true';  discURLs = discURLs +'<li>&#8226; <a href="'+url+'?referring_site=smartnav">'+name+'</a></li>'; } discURLs = discURLs+'</ul>';document.getElementById('discussionURLS').innerHTML = discURLs;if(threadFlag=='true'){ toggle('discussionMainHead'); } }



function toggle(obj) {  var el = document.getElementById(obj); el.style.display = (el.style.display != 'none' ? 'none' : '' ); }

function removejscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement);
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
 
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1){
   allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
   } 
 }
}

removejscssfile("painted_libra.css", "css"); //remove all occurences "somestyle.css" on page

function addorModifyReadWrite(ciscoReadWrite){
	var ciscoThreads=ciscoReadWrite.threads; 		
	for (var wordss in ciscoThreads){ 
		if(ciscoThreads[wordss]['url'].indexOf("?")>-1){
			ciscoThreads[wordss]['url'] = ciscoThreads[wordss]['url'] + "&referring_site=smartnav";
		}else{
			ciscoThreads[wordss]['url'] = ciscoThreads[wordss]['url'] + "?referring_site=smartnav";
		}
		ciscoReadWrite.threads[wordss]['url'] = ciscoThreads[wordss]['url'];
	}
	addDiscutionURLs(ciscoReadWrite);
}

/*************************For divided Json starts here*******************************/
function findUrl(attDocType, roadMapID) {
	var url  = "";
	var infotypeNames = new Array(19, 15, 21, 8, 23, 12, 11, 6,9,208);
	var infotypeFileNames = new Array("TechnologyQandAs.json",
			"TechnologyConfigurationExample.json", "TechnologyTechNote.json",
			 "ProductPasswordRecovery.json","TechnologyWhitePaper.json",
			"ProductWhitePaper.json", "ProductsTechNote.json",
			"ProductConfigurationExample.json","ProductQandAs.json","ProductExampleModule.json");


	if (document.getElementsByTagName) {
		if (attDocType == 11) {
			if (roadMapID == 1 || roadMapID == 2 || roadMapID == 3 || roadMapID == 5) { 
				xmlFleName =  "ProductTechNoteGroup1.json";
			} else if (roadMapID == 6 || roadMapID == 14 || roadMapID == 15 ){ 
				xmlFleName =  "ProductTechNoteGroup6.json";
			} else if (roadMapID == 7 || roadMapID == 8 || roadMapID == 13 ) { 		    		   
				xmlFleName = "ProductTechNoteGroup2.json";
			} else if(roadMapID == 19 ) {		
				xmlFleName = "ProductTechNoteGroup3.json";		    	   
			} else if (roadMapID == 17 || roadMapID == 18 || roadMapID == 23 || roadMapID == 25
		    	     || roadMapID == 26 || roadMapID == 27) {
				xmlFleName = "ProductTechNoteGroup4.json";
		    } else if (roadMapID > 27 || roadMapID == 20 || roadMapID == 21 || roadMapID == 22) {
    	    	xmlFleName = "ProductTechNoteGroup5.json";	    		   
			} else if (roadMapID == 16 || roadMapID == 14 || roadMapID == 15 ){ 
				xmlFleName= "ProductTechNoteGroup7.json";
			}
		} else if (attDocType == 15) { //to separate the TechnologyConfigurationExample infotype i.e. 15.
			if (roadMapID <= 14) {
				xmlFleName =  "TechnologyConfigurationExampleGroup1.json";
			} else if (roadMapID >= 15) {
				xmlFleName =  "TechnologyConfigurationExampleGroup2.json";
			}
		} else if (attDocType == 21) { 
			if (roadMapID <= 14) {
				xmlFleName = "TechnologyTechNoteGroup1.json";
			} else if (roadMapID >= 15) {
				xmlFleName = "TechnologyTechNoteGroup2.json";
			}
		} else if (attDocType == 9) { 			
			if (roadMapID <= 28 || roadMapID == 205) {
				xmlFleName = "ProductQAndAsGroup1.json";
			} else if (roadMapID ==128) {			
				xmlFleName = "ProductQAndAsGroup2.json";
			}  else if (roadMapID == 208) {
				xmlFleName = "ProductQAndAsGroup3.json";
			}
		} else if (attDocType == 6) { 			
			if (roadMapID == 6 || roadMapID == 19 || roadMapID == 2|| roadMapID == 25|| roadMapID == 5|| roadMapID == 7|| roadMapID == 13||roadMapID == 21|| roadMapID == 23|| roadMapID == 26|| roadMapID == 17) {
				xmlFleName = "ProductConfigurationExampleGroup1.json";
			} else if (roadMapID == 18 || roadMapID == 16 || roadMapID == 1 || roadMapID == 8 || roadMapID == 3 || roadMapID == 14 || roadMapID == 15 ||roadMapID == 20 || roadMapID == 22 || roadMapID == 28 || roadMapID == 153 || roadMapID == 207 || roadMapID == 27) {
				xmlFleName = "ProductConfigurationExampleGroup2.json";		    	    
			} else if ((roadMapID == 128|| roadMapID == 205)) {
				xmlFleName = "ProductConfigurationExampleGroup3.json";
			}			
		} else {
			var xmlFleName = "";
			for ( var iNode = 0; iNode < infotypeNames.length; iNode++) {
				if (attDocType == infotypeNames[iNode]) {
					xmlFleName = infotypeFileNames[iNode];
					break;
				}
			}
		}

		if (xmlFleName != "") {
			url = "/content/dam/en/us/support/docs/common/"+xmlFleName;		
		} 

	}	
	
	return url;
	
} // findUrl method

/*************************For divided Json ends here*******************************/

/*************** For More links displaying properly starts here********************/
window.onload = function() {
		var divHtml = document.getElementById('firstFiveDcos').innerHTML;
		if((divHtml == null) || (divHtml.toLowerCase().indexOf('</span>') < 0)) {
			return;
		}
		divHtml = divHtml.replace('</SPAN>', ' ');
		divHtml = divHtml.replace('</span>', ' ');
		divHtml = divHtml.replace('</ul>', '</span></ul>');
		divHtml = divHtml.replace('</UL>', '</SPAN></UL>');
		document.getElementById('firstFiveDcos').innerHTML = divHtml;

} //hideOrShowMore method
/*************** For More links displaying properly ends here********************/