var K0=window,Q1=document,O2=value='';
var url , title;
var url1='http://india.gov.in/sitemap.php' ;
var title1='Site Map National Portal of India';

function addBookmark(url,title) 
{
	//alert('inside the Bookmark');
  if (window.sidebar) 
    {
        window.sidebar.addPanel(title, url,"");
    } 
	
 else if( document.all ) 
   {
      window.external.AddFavorite( url, title);
   } 
   
 else if( window.opera && window.print ) 
 {
return true;
} 
else if( document.layers ) 
{
window.alert( 'Please click OK and then press Ctrl+D to create a bookmark or \n click Bookmarks | Add Bookmark' );
} 

else {
           window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
       }
}

function collpase(id,O2)
{
	Q1.getElementById(id).style.display="none";
	var str='symbol_'+id.replace(/_/g,'');
	
		if(Q1.getElementById(str))
			{
				var mainid=id.replace(/_/g,'');
				var symbolhref='<span id="symbol_'+mainid+'"><a href="javascript:expand(\''+id+'\','+O2+');" class="nodecls">'+UI.expand+'</a>'+UI.collimage+'</span>';
				Q1.getElementById(str).innerHTML=symbolhref;
			}
}




function collpase1(id,O2)
{
	Q1.getElementById(id).style.display="none";
	var str='symbol_'+id.replace(/_/g,'');
	
		if(Q1.getElementById(str))
			{
				var mainid=id.replace(/_/g,'');
				var symbolhref='<span id="symbol_'+mainid+'"><a href="javascript:expand1(\''+id+'\','+O2+');" class="nodecls">'+UI.expand+'</a>'+UI.folderarrowclose+'</span>';
				Q1.getElementById(str).innerHTML=symbolhref;
			}
}

// this function is called inside the generatechild() method where pass two argument id='tree1_0' and 02=1	when clike on ctizens		
function expand(id,O2)
	{
		
		loadChild(id,O2);
		
		 // external code
		 //   alert("id of expand function="+   id);
			// finished
		
		Q1.getElementById(id).style.display="block";
		
		// when i am clike business then id is passed from id=tree1_1 from above then pass replace underscore symbol to the blank the  id become as str=symbol_tree11
		var str='symbol_'+id.replace(/_/g,'');
		
		 // external code
		   // alert("str of expand function="+   str);
			// finished
		
		if(Q1.getElementById(str))
		{
			var mainid=id.replace(/_/g,'');
			var symbolhref='<span id="symbol_'+mainid+'"><a href="javascript:collpase(\''+id+'\','+O2+');" class="nodecls">'+UI.collapse+'</a>'+UI.expimage+'</span>';
			Q1.getElementById(str).innerHTML=symbolhref;
		}
		
	}
	
	
	
	
	
	function expand1(id,O2)
	{
		
		loadChild(id,O2);
		
		 // external code
		 //   alert("id of expand function="+   id);
			// finished
		
		Q1.getElementById(id).style.display="block";
		
		// when i am clike business then id is passed from id=tree1_1 from above then pass replace underscore symbol to the blank the  id become as str=symbol_tree11
		var str='symbol_'+id.replace(/_/g,'');
		
		 // external code
		   // alert("str of expand function="+   str);
			// finished
		
		if(Q1.getElementById(str))
		{
			var mainid=id.replace(/_/g,'');
			var symbolhref='<span id="symbol_'+mainid+'"><a href="javascript:collpase1(\''+id+'\','+O2+');" class="nodecls">'+UI.collapse+'</a>'+UI.folderarrowopen+'</span>';
			Q1.getElementById(str).innerHTML=symbolhref;
		}
		
	}
	
// This function is access by index.php with parameter id=tree1(id of div element) , 02=0 zero	
function loadChild(id,O2)
{
    // call again again service.php with query string parameter as method=catid , id=tree1 , catid=zero (from intial calling this method in index.php)
	var strParam="siteFetch.php?method=getCat&id="+id+"&catid="+O2;
	// send the request with url and function generate the children , Ajax.Request is an function which take two argument as url and callbackmethod,which is describe in ajax.js
	Ajax.Request(strParam,generateChild);
	
}

function generateChild()
{
	var active='Active';
	var nolink='No_Link';
	var addToFavorites='Add to Favorites';
	//var nlf ='NLF';
    //var inactive='Inactive';
	Ajax.setShowMessage(1);
	Ajax.setMessage("Loaded..");
	
	//  in this method CheckReadyState(object) got some response according to the value of readystate , 
	if(Ajax.CheckReadyState(Ajax.request))
	  {
		  
		  // Ajax Objects as Ajax.Request loaded response as responseText which is in Stringtext format , the eval() convert into JSON object format
		  // value of respone variable is [object , object]
		 //  var	response=eval('('+Ajax.request.responseText+')');
		   var response=JSON.parse(Ajax.request.responseText); 
	
		   var str='';
		   var i=0;
		 
		 //alert(response);
		  
		  // if response length is zero then the visibility of div element having(id=tree1)where response has been renedr is hide.
		  // check the length of array named as data where stored the all the fetchable data from mysql
		  if(response.data.length==0)
		  	{
				Q1.getElementById(response.id).style.display="none";
			}
		
		// Thsi is inbuilt of javascript method replace(/findstring/ , newstring) eg. g or i as g=global search(if string contains a words 5 times then it replace all        //  the location) and i=incasesensitive (without woory about caps and small letter), defaul replace work as casesensitive.
		 // in below code search _ symbol and replace with blank ' ' .
		 // when clike on +citizen(mainid=tree10) , +education(mainid=tree100)
		var mainid=response.id.replace(/_/g,'');
		
		// external code
		// alert("mainid="+   mainid);
		// alert("step2");
		// alert("response.data.length="+   response.data.length  );
		 // finsihed
		 
		// response.data.length as first time=9(Total first level element) , clike on +citizen=6 (No of child element in) etc.
		for(i=0;i < response.data.length;i++)
		   {
			   // str variable hold the div element with id as mainid+value of i=0 =(id=tree10)
			   str+='<div id="'+mainid+''+i+'" style="padding-left:20px;">';
			   
			   // external code
		  //  alert("first str="+   str);
			//alert("response.id="+   response.id);
			//alert("response.data[i].NodeStatus="+ response.data[i].name+  "= " + response.data[i].NodeStatus);
			//alert("UI.expand="+   UI.expand);
			//alert("response.data[i].name="+   response.data[i].name);
			// finished
			
			// hold str variable=( old str) + (span id=symbol_tree10) value of this span is UI.expand= +expand and hyperreference to this +expand to javascripot function expand('+response.id+ '_' +i+'='tree1_0'(value) , response.data[i].id=1(value)) and response.data[i].name=citizens
			
			if(response.data[i].count==0)
			{
				if(response.data[i].name==addToFavorites)
				  {
					 if(response.data[i].linkstatus==active)
				      {
					    //  alert('inside the active');
	            str+='<span id="symbol_'+mainid+''+i+'">'+UI.page+'</span>';
			   str+='<a href="javascript:addBookmark(url1,title1);">'+response.data[i].name+'</a></div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>';
					         
					     
				      }
				    else
				     {
					 //  alert('inside the Inactive');
	 
					    str+='<span id="symbol_'+mainid+''+i+'">'+UI.page+'</span>';
			   str+='<a href="javascript:addBookmark(url1,title1);">'+response.data[i].name+'</a></div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>';
					         
					      
					   
				     } 
				  }
				else
				{
				
				if(response.data[i].linkstatus==active)
				 {
					  if(response.data[i].link==nolink)
				       {
	            str+='<span id="symbol_'+mainid+''+i+'">'+UI.page+'</span>';
			   str+=''+response.data[i].name+'</div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>';
					   }
					   else
					   {
						  str+='<span id="symbol_'+mainid+''+i+'">'+UI.page+'</span>';
			   str+='<a href="'+response.data[i].link+'">'+response.data[i].name+'</a></div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>'; 
						   
						}
				 }
				 
  // This code is working when Item become Inactive	For simply making div			 
				 else
				 {
					  
					    if(response.data[i].link==nolink)
				       {
					       str+='<span id="symbol_'+mainid+'"></span>';
						   str+='</div>';
						   str+='<div id="'+response.id+'" style="padding-left:20px;display:none"></div>';
					   }
					   else
					   {
						   str+='<span id="symbol_'+mainid+'"></span>';
			               str+='</div>';
			               str+='<div id="'+response.id+'" style="padding-left:20px;display:none"></div>';
						   
						}
					   
				 } // End of Inactive code
				
				}
				
		} 
			
			else 
			
			{
				
				if(response.data[i].linkstatus==active)
				 {
					 
					 
					 if(response.data[i].link==nolink)
				       {
				  str+='<span id="symbol_'+mainid+''+i+'"><a href="javascript:expand1(\''+response.id+'_'+i+'\','+response.data[i].id+');" class="nodecls">'+UI.expand+'</a>'+UI.folderarrowclose+'</span>';
			  // str+='<a href="javascript:expand1(\''+response.id+'_'+i+'\','+response.data[i].id+');" class="nodecls">'+response.data[i].name+'</a></div>';
			    str+=''+response.data[i].name+'</div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>';
						   
	          
					   }
					   else
					   {
						   
						   str+='<span id="symbol_'+mainid+''+i+'"><a href="javascript:expand(\''+response.id+'_'+i+'\','+response.data[i].id+');" class="nodecls">'+UI.expand+'</a>'+UI.collimage+'</span>';
			   str+='<a href="'+response.data[i].link+'">'+response.data[i].name+'</a></div>';
			   str+='<div id="'+response.id+'_'+i+'" style="padding-left:20px;display:none"></div>';
						   
						   	
	        
					   }
			         
			   
			          
				 }
// This code is working when Item become Inactive For simply making div					
				else
				 
				 {
					 
					 if(response.data[i].link==nolink)
					   {
					      str+='<span id="symbol_'+mainid+'"></span>';
			        //   str+='<a href="javascript:expand1(\''+response.id+'_'+i+'\','+response.data[i].id+');" class="nodecls">'+response.data[i].name+'</a></div>';
			              str+='</div>';
			              str+='<div id="'+response.id+'" style="padding-left:20px;display:none"></div>';
					 
			   
					   }
					   else
					   {
						   str+='<span id="symbol_'+mainid+'"></span>';
			              //str+='<a href="javascript:expand(\''+response.id+'_'+i+'\','+response.data[i].id+');" class="nodecls">'+response.data[i].name+'</a></div>';
			              str+='</div>';
			              str+='<div id="'+response.id+'" style="padding-left:20px;display:none"></div>';
						}
				
				
				 } // End of Inactive code
				
				
				
		 }
			
			
	 	}
		
		// what ever the result stored in str(hold the output in format of html) . dfispaly in the div element which have pass id as tree1 through innerHTML(we can change the data of any html element(here div element with the id =tree1 changed))
		Q1.getElementById(response.id).innerHTML=str;
		
	}
	
}
