<?php
if (isset($_POST['type'])) {
	for ($i = 0; $i < count($_POST['type']); $i++) {	
		$pid = mysql_real_escape_string(stripslashes($_POST['id'][$i]));
		$name_la = mysql_real_escape_string(stripslashes($_POST['orgname_la'][$i]));
		$name_eng = mysql_real_escape_string(stripslashes($_POST['orgname_eng'][$i])); 
		$short_name = mysql_real_escape_string(stripslashes($_POST['short_name'][$i])); 
		$parentid = mysql_real_escape_string(stripslashes($_POST['parentid'][$i])); 
		$unittype = mysql_real_escape_string(stripslashes($_POST['unittype'][$i])); 
	
		/*--------------------------------------------------------------*/
		
		if ($_POST['orgname_la'][$i] != "" && $_POST['parentid'][$i] != "") {	
			//echo $_POST['type'][$i]."<br>";
			switch ($_POST['type'][$i]) {	
				case "added" :	
					if (mysql_num_rows(mysql_query("SELECT * FROM orgunit WHERE orgunitname = '$name_la' AND orgunitnameeng='$name_eng'")) == 0) {
						mysql_query("INSERT INTO orgunit(orgunitname, orgunitnameeng, short_name, parentunit, unittypeid) VALUES('$name_la', '$name_eng', '$short_name', $parentid, $unittype ) ");
					}
				break;
					
				case "edited":		
					mysql_query("UPDATE orgunit SET orgunitname = '$name_la', orgunitnameeng = '$name_eng', short_name = '$short_name', parentunit = $parentid, unittypeid = $unittype WHERE orgunitid = $pid");
				break;
			}
		}	
	}
}

$result = LoadEq($whereclause, $limitclause);
if(isset($_GET["del_id"]) && $_SESSION['role_id'] == 1 ){
	//$usered = $_SESSION['user_id'];
	$pid = mysql_real_escape_string(stripslashes($_GET['del_id']));
	
	$sql = "UPDATE orgunit SET isdeleted = 1 WHERE orgunitid =$pid ";
	$result = mysql_query($sql, $conn);	
	if($result){
		header("Location: ?d=webmaster/manage/orgunit");
	}else{
		echo "<h2 class='error'>ERROR Delete</h2>";
	}
}
