
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object



function validateLevel(){
	
	if((document.getElementById('level').value=="account")	|| (document.getElementById('level_id').value>0))
		return true;
	else{
		alert("Invalid Entry, please select either top level or an option from the second dropdown");
		return false;
	}
}

function setLevelId(value){
	document.getElementById('level_id').value = value;
}

function handleDisplayLevelMenu(){
	  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText;
	document.getElementById('level_div').innerHTML = results;
  }  
}

function displayLevelMenu(level){
		var url='/ajax/getLevelMenu.php?level='+level;
		http.open("POST", url, true);
		http.onreadystatechange = handleDisplayLevelMenu;
		http.send(null);	

}

function handlegetLocationMenu(){
	  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText;
	document.getElementById('location_dropdown').innerHTML = results;
  }  
}

function getLocationMenu(parent_id){
		var url='../ajax/getLocationMenu.php?group_id='+parent_id;
		http.open("POST", url, true);
		http.onreadystatechange = handlegetLocationMenu;
		http.send(null);	
}

function handlegetDepartmentMenu(){
	  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText;
	document.getElementById('department_dropdown').innerHTML = results;
  }  
}

function getDepartmentMenu(parent_id){
		var url='/ajax/getDepartmentMenu.php?location_id='+parent_id;
		http.open("POST", url, true);
		http.onreadystatechange = handlegetDepartmentMenu;
		http.send(null);	
}

function handlesetPermission(){
	  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText;
	document.getElementById('permissions').innerHTML = results;
	//alert(results);
  }  
}

function setPermission(type){
	var dept;
	var loc;
	var grp;
	var id=0;
	var level="";
	if((type!="admin") && (type!="auditor")){
		try{ 
			dept = document.getElementById('department').value;
		}
		catch(myError){
			dept="";
		}
		try{
		
			loc = document.getElementById('location').value;
		}
		catch(myError) {
			loc="";
		}
		grp = document.getElementById('parent_id').value;
		id=dept;
		level='department';
		if(id==""){
			id=loc;
			level = 'location';
		}
		if(id==""){
			id=grp;
			level='group';
		}
		if(id==""){
			level="account";
		}
	}
		var url='../ajax/setPermission.php?type='+type+'&level='+level+'&level_id='+id;
		http.open("POST", url, true);
		http.onreadystatechange = handlesetPermission;
		http.send(null);	
}	

function dropPermission(index, type){
		var url='../ajax/dropPermission.php?index='+index+'&type='+type;
		http.open("POST", url, true);
		http.onreadystatechange = handlesetPermission;
		http.send(null);	
}
function handleDropPermissionByType(){
	  if (http.readyState == 4) {
    // Split the comma delimited response into an array
    results = http.responseText;
  }  
}
function dropPermissionByType(type){
		var url='../ajax/dropPermissionByType.php?type='+type;
		http.open("POST", url, true);
		http.onreadystatechange = handleDropPermissionByType;
		http.send(null);	
}

function addOrDropPermission(permission, value){
	if(value)
		setPermission(permission);
	else
		dropPermissionByType(permission);
}
