<?php

function get_case_info($menu_id, $type)
{
	$case = ""; 
	
 
	
	include('config/db_connect.php');
	$query = " Call usp_select_menu( $_SESSION[b_id], 0,$menu_id,0,  $type)";
	$result = $mysql->query($query);
	
	
	while($row = mysqli_fetch_array($result)) 
	{
		$case_address = "view_".$row['fnc_param_one'];
		$case .= "case '$case_address':\n";
		$case .= "pageAddress = '$row[page_address]' + generic_address;\n";
		$case .= "document.getElementById('heading').style.visibility='hidden';\n";
		$case .= "document.getElementById('header').innerHTML='';\n";
		$case .= "break;\n";
		include('config/db_connect.php');
		$query = " Call usp_select_menu( $_SESSION[b_id], $row[0], $menu_id, 1,  $type)";
		$sub_result = $mysql->query($query);
			
	
		if(mysqli_num_rows($sub_result) > 0)
		{
			while($sub_row = mysqli_fetch_array($sub_result)) 
			{
				$case_address = "view_".$sub_row['fnc_param_one'];
				$case .= "case '$case_address':\n";
				$case .= "pageAddress = '$sub_row[page_address_one]' + generic_address;\n";
				$case .= "document.getElementById('heading').innerHTML='$sub_row[menu_name]';\n";
				$case .= "break;\n";	
				
				$case_address = "new_".$sub_row['fnc_param_one'];				
				$case .= "case '$case_address':\n";
				$case .= "pageAddress = '$sub_row[page_address_two]' + generic_address;\n";
				$case .= "document.getElementById('heading').innerHTML='New ' + '$sub_row[menu_name]';\n";
				$case .= "break;\n";
				
				$case_address = "edit_".$sub_row['fnc_param_one'];				
				$case .= "case '$case_address':\n";
				$case .= "pageAddress = '$sub_row[page_address_three]' + generic_address;\n";
				$case .= "document.getElementById('heading').innerHTML='Edit ' + '$sub_row[menu_name]';\n";
				$case .= "break;\n";
				
				include('config/db_connect.php');
				$query = " Call usp_select_menu( $_SESSION[b_id], $sub_row[0], $menu_id, 2,  $type)";
				$sub_sub_result = $mysql->query($query);
 
 		 
				if(mysqli_num_rows($sub_sub_result) > 0)
				{
					while($sub_sub_row = mysqli_fetch_array($sub_sub_result)) 
					{
						$case_address = "view_".$sub_sub_row['fnc_param_one'];
						$case .= "case '$case_address':\n";
						$case .= "pageAddress = '$sub_sub_row[page_address_one]' + generic_address;\n";
						$case .= "document.getElementById('heading').innerHTML='$sub_sub_row[menu_name]';\n";
						$case .= "break;\n";
						
						
						
					}
				}
			} 
		}
	}
	
	return $case; 
}


$view_js = " <script type='text/javascript' language='javascript'>";


$view_js .= " function getPageAddress(page_id, element_id, sub_element_id, new_page, edit_page, tag, menu_id, access_id)
{	
	var pageAddress = '';
	
	var generic_address = '?page_id=' + page_id + '&element_id=' +  element_id + '&sub_element_id=' + sub_element_id + '&new_page=' + new_page + '&edit_page=' + edit_page + '&menu_id=' + menu_id +  '&access_id=' + access_id;
	document.getElementById('loder').style.visibility='visible';
	document.getElementById('heading').style.visibility='visible';
 
 	
 	if(tag == 0)
		page_id = 'view_' + page_id;
	else if(tag == 1)
		page_id = 'new_' + page_id;
	else if(tag == 2)
		page_id = 'edit_' + page_id;

 
	
	switch(page_id)
	{";
	
		$view_js .= get_case_info(1, 0);
		 
		 
$view_js .= "}
	
	
	
	return pageAddress;
} 
 

function loadPage(page_id, element_id, sub_element_id, new_page, edit_page, tag, menu_id, access_id)
{
 
//	alert(page_id);
//	alert(element_id);
//	alert(sub_element_id);
//	alert(new_page);
//	alert(edit_page);
// 	alert(tag);
// 	alert(menu_id);
// 	alert(access_id);
 
 	var pageAddress = '';
	
	
	var xmlhttp = getXmlhttp();
 	 
	xmlhttp.onreadystatechange=function()
	{	  
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{ 
			if( xmlhttp.responseText != -1 )
			{
				document.getElementById('loder').style.visibility='hidden';
				document.getElementById('ajaxDiv').innerHTML = xmlhttp.responseText;
				if(tag == 0)
			 		searchOperationPage(page_id, element_id, sub_element_id, 1, 25, menu_id, access_id);
 
			}
			else
			{
				document.getElementById('loder').style.visibility='hidden';
				alert('Unable to perform this operation'); 
			}
		}
	}
 
  
 	pageAddress = getPageAddress(page_id, element_id, sub_element_id, new_page, edit_page, tag, menu_id, access_id);
	
//  alert(pageAddress);
 
 	if(pageAddress != '')
	{
		xmlhttp.open('GET',pageAddress,true);
		xmlhttp.send();
	}
	else
		document.getElementById('loder').style.visibility='hidden';
} ";
 
$view_js .= " </script> ";
 
echo $view_js; 

?>