<?
/* index.php: $pubmodule, $pubtask, $pubitemid */
/*
$pubsubjectid = $_REQUEST["subjectid"];
$pubtypeid = $_REQUEST["typeid"];
$pubgroupid = $_REQUEST["groupid"];
*/

// filter forms..
$filterclientname=$_POST['clientname'];
?>

<script type="text/javascript">

// ................................................................................
// user-interaction handlers..
// ................................................................................

function client_button_search() {
  document.clientform.job.value='client_search';
  document.clientform.submit();
}

function client_button_add() {
  /* document.clientform.job.value='client_add';
  document.clientform.submit(); */
  document.location='index.php?module=client&task=add';
}

function client_button_edit() {
  document.clientform.job.value='client_edit';
  document.clientform.submit();
}

function client_button_remove() {
  document.clientform.job.value='client_remove';
  document.clientform.submit();
}

function client_button_back() {
  history.go(-1);
}

function client_button_save() {
  if (validateForm()==true) {	
    document.clientform.job.value='client_save'; 
    document.clientform.submit();
  }
}

function client_button_update() {
  if (validateForm()==true) {	
    document.clientform.job.value='client_save_edit'; 
    document.clientform.submit();
  }
}

function client_button_purge() {
	document.clientform.job.value='client_remove';
	document.clientform.submit();
}	 

function validateForm() {
  var isvalid=true;
  var errmsg="";
  var clientname=document.getElementById('clientname');
  
  if ( clientname.value == "" ) {
    errmsg='Client Name must be specified!\n';
    isvalid=false;
  }
  if (isvalid==false) alert('Error:\n'+errmsg);
  return isvalid;
}
</script>

<?

// ................................................................................
// model..
// ................................................................................

include_once('models/clientModel.php');
$myclientModel=new clientModel;

?>

<?

// ................................................................................
// default to list view if none specified..
// ................................................................................
if ($pubtask=="") $pubtask="main";

// ................................................................................
// display page header..
// ................................................................................

$pagetitle="Master Data";
if (($pubtask=="main")||($pubtask=="submenu")) {
  $subtitle="Clients List";
} else if ($pubtask=="view") {
  $subtitle="Client Information";
} else if ($pubtask=="add") {
  $subtitle="Add Client Information";
} else if ($pubtask=="edit") {
  $subtitle="Edit Client Information";
} else if ($pubtask=="delete") {
  $subtitle="Delete Client Information";
} else {
  $subtitle="Error: Invalid Option";
}

$headercaption=$pagetitle." --&raquo; ".$subtitle;
include_once('header.php');

include_once('views/general-header.tpl');


// TODO: Later to show list of available variables here if in DEBUG mode!!

// ................................................................................
// display main area..
// ................................................................................

if (($pubtask=="main")||($pubtask=="submenu")) {

  $client_list = $myclientModel->getlist_clients();
  include_once('views/client-list.tpl');

} else if ($pubtask=="view") {

  $client_data = $myclientModel->getdata_client($pubitemid);
  include_once('views/client-view.tpl');
# myapit begin  
} else if ($pubtask=="edit") {

  $client_data = $myclientModel->getdata_client($pubitemid);
  include_once('views/client-edit.tpl.php');
  
} else if ($pubtask=="add") {

  $client_data = $myclientModel->getdata_client($pubitemid);
  include_once('views/client-edit.tpl.php');  
  
} else if ($pubtask=="delete") {
  $client_data = $myclientModel->getdata_client($pubitemid);
  include_once('views/client-view.tpl'); 
  
# myapit end
} else {
  include_once('views/general-invalidtask.php');
}

// ...............................................................................
// display button options area..
// ...............................................................................

$prevurl = explode('/',$_SESSION['Xpreviousurl']);
$prevfile = $prevurl[count($prevurl)-1];
if ($prevfile=='inputController.php') $backstatus="style='display: none;'";

echo "<br>";

if (($pubtask=="main")||($pubtask=="submenu")) {

  echo "<br><hr size=1>";
  //echo "<button name=\"print\" id=\"print\" onClick=\"javascript:client_button_printlist();\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print&nbsp;</button>&nbsp;";
  
  if(($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2)) {
   echo "<button name=\"add\" id=\"add\" onClick=\"client_button_add();\"><img src='gfx/options/b_add.png' class='actionbutton'>&nbsp;&nbsp;Add New Client</button>&nbsp;"; 
  }
  // edit 17-08-2010
  //echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"client_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";
  echo "<br><hr size=1><br>";

} else if ($pubtask=="view") {

  echo "<br><hr size=1>";
  //echo "<button name=\"print\" id=\"print\" onClick=\"javascript:client_button_print('".$pubitemid."');\">&nbsp;<img src='gfx/options/b_print1.png' class='actionbutton'>&nbsp;&nbsp;Print&nbsp;</button>&nbsp;";
   if (($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2)) { 
    echo "<button name=\"edit\" id=\"edit\" onClick=\"window.location.href='index.php?module=client&task=edit&itemid=".$pubitemid."';\">&nbsp;<img src='gfx/options/b_edit.png' class='actionbutton'>&nbsp;&nbsp;Edit&nbsp;</button>&nbsp;";
    echo "<button name=\"delete\" id=\"delete\" onClick=\"window.location.href='index.php?module=client&task=delete&itemid=".$pubitemid."';\"><img src='gfx/options/b_drop.png' class='actionbutton'>&nbsp;&nbsp;Delete</button>&nbsp;";
   }
  echo "<button name=\"back\" id=\"back\" ".$backstatus." onClick=\"client_button_back();\">&nbsp;<img src='gfx/options/b_back.png' class='actionbutton'>&nbsp;&nbsp;Back&nbsp;</button>&nbsp;";  
  echo "<br><hr size=1><br>";

  
 } else if (($pubtask=="add")&&(($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"save\" id=\"save\" onClick=\"client_button_save();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Save Client</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"client_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

 } else if (($pubtask=="edit")&&(($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2)) {
    echo "<button name=\"save\" id=\"save\" onClick=\"client_button_update();\"><img src='gfx/options/b_save.png' class='actionbutton'>&nbsp;&nbsp;Update Client</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"client_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<br><hr size=1><br>";

  
} else if (($pubtask=="delete")&&(($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2))) {

  echo "<br><hr size=1>";
  if (($pubuseraccess['access_client_edit']==1)||($pubuserrole>=2)) {
    echo "<font color='#ff0000'><b>ARE YOU SURE TO PERMANENTLY DELETE THIS CLIENT INFORMATION  ?</b></font><br><br>";
    echo "<button name=\"delete\" id=\"delete\" onClick=\"client_button_purge();\">&nbsp;Yes, Delete Client Information&nbsp;</button>&nbsp;";
  }
  echo "<button name=\"cancel\" id=\"cancel\" onClick=\"client_button_back();\"><img src='gfx/options/b_cancel.png' class='actionbutton'>&nbsp;&nbsp;Cancel</button>&nbsp;";
  echo "<brp><hr size=1><br>";
}else {
  
}

// ................................................................................
// display page footer..
// ................................................................................

include_once('views/general-footer.tpl');

?>