<?php
include("../init.php");
include("../_support/class.db.php");

$db = new db;
$db->SetDatabaseConnection($MySQL_User,$MySQL_Pass,'Warpit_Webuserspool',"localhost");


if (!$_POST[treeName]) echo "{ success : false } ";

if ($_POST[editLabel]) {
	$sql = "UPDATE  ".$_POST[treeName]." SET idLab = '".$_POST[newValue]."' WHERE id = ".$_POST[nodeId];
	$db->SQLexecute($sql);
	echo " { uspesno: 1  }";	
} elseif ($_POST[deleteLabel]) {
	$sql = "DELETE FROM  ".$_POST[treeName]." WHERE id = ".$_POST[nodeId];
	//echo $sql;
	$db->SQLexecute($sql);
	echo " { uspesno: 1  }";	
} elseif ($_POST[moveLabel]) {

	$sql = "UPDATE  ".$_POST[treeName]." SET  orderBy = orderBy + 1 WHERE orderBy = ".$_POST[orderBy]." AND id_sup = ".$_POST[newParent];
	$db->SQLexecute($sql);
	$sql = "UPDATE  ".$_POST[treeName]." SET id_sup = ".$_POST[newParent].",orderBy = ".$_POST[orderBy]."  WHERE id = ".$_POST[nodeId];
	$db->SQLexecute($sql);
	echo " { uspesno: 1  }";

} elseif ($_POST[AddNewNode]) {
	
	$sql = "INSERT INTO  ".$_POST[treeName]." SET id_sup = ".$_POST[nodeId].", idLab='".$_POST[nodeText]."'";
	$db->SQLexecute($sql);
	$last = $db->GetLastInsertId();
	$sql = "UPDATE  ".$_POST[treeName]." SET orderBy = ".$last." WHERE id = ".$last;  
	$db->SQLexecute($sql);
	
	echo " { insertRecord: { id: ".$last." }  }";

}


?>