  <script type="text/javascript">
  <!-- 
  //Browser Support Code
  function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			// put stuff in dropdown..
	 	 	var ajaxList = document.getElementById("staff_deptpkey");
			// clear the list 
			for (var count = ajaxList.options.length-1; count >-1; count--)
			{
				ajaxList.options[count] = null;
			}
							
			var deptlist=ajaxRequest.responseText;
			deptarray = deptlist.split("~");
						
			// populate the dropdown list..
			var idValue;
			var textValue; 
			var optionItem;
			for (var count = 0; count<deptarray.length; count++)
			{	
				mydept = new String(deptarray[count]);
				mydeptdata = mydept.split("|");
				optionValue = new String(mydeptdata[0]);
				if (mydeptdata[1]!="") {
					optionText = new String(mydeptdata[1]);
				} else {
					optionText = new String(mydeptdata[2]);
				}
																			
				optionItem = new Option(optionText,optionValue, false, false);
				ajaxList.options[ajaxList.length] = optionItem;
				
			}
		}
	}
	ajaxRequest.open("GET", "ajx_getdepartments.php?itemid="+document.userform.staff_divpkey.value, true);
	ajaxRequest.send(null); 
  }
  //-->
  </script>

  <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
  <form action="controllers/inputController.php" method="post" name="tradeform" id="tradeform">
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Main Category</td>
  <td valign=top width=610 class=formvalue>
  <input name="tradepkey" id="tradepkey" type="hidden" value="<?=$trade_data['tradepkey'];?>">
  <input name="tradeparentpkey" id="tradeparentpkey" type="hidden" value="<?=$trade_data['tradeparentpkey'];?>">
  <select name="tradepkey1" id="tradepkey1" style="width: 360px;" onChange="ajaxFunction(1);">
  <? $objtradepkey1=$trade_data['tradepkey1']; ?>
  <? include_once('object-selecttrade1.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Sub Main Category</td>
  <td valign=top width=610 class=formvalue>
  <select name="tradepkey2" id="tradepkey2" style="width: 360px;" onChange="ajaxFunction(2);">
  <? $objtradepkey2=$trade_data['tradepkey2']; ?>
  <? include_once('object-selecttrade2.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Sub Category</td>
  <td valign=top width=610 class=formvalue>
  <select name="tradepkey3" id="tradepkey3" style="width: 360px;" onChange="ajaxFunction(3);">
  <? $objtradepkey3=$trade_data['tradepkey3']; ?>
  <? include_once('object-selecttrade3.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Sub-Sub Category</td>
  <td valign=top width=610 class=formvalue>
  <select name="tradepkey4" id="tradepkey4" style="width: 360px;" onChange="ajaxFunction(4);">
  <? $objtradepkey4=$trade_data['tradepkey4']; ?>
  <? include_once('object-selecttrade4.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Tri-Sub Category</td>
  <td valign=top width=610 class=formvalue>
  <select name="tradepkey5" id="tradepkey5" style="width: 360px;" onChange="ajaxFunction(5);">
  <? $objtradepkey5=$trade_data['tradepkey5']; ?>
  <? include_once('object-selecttrade5.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Title</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_title" id="staff_title" type="text" value="<?=$staff_data['staff_title'];?>" style="width: 180px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Name</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_name" id="staff_name" type="text" value="<?=$staff_data['staff_name'];?>" style="width: 360px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>NRIC No</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_nricno" id="staff_nricno" type="text" value="<?=$staff_data['staff_nricno'];?>" style="width: 360px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Phone No</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_phone" id="staff_phone" type="text" value="<?=$staff_data['staff_phone'];?>" style="width: 360px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Email</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_email" id="staff_email" type="text" value="<?=$staff_data['staff_email'];?>" style="width: 360px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Designation</td>
  <td valign=top width=610 class=formvalue>
  <input name="staff_designation" id="staff_designation" type="text" value="<?=$staff_data['staff_designation'];?>" style="width: 360px;">
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Division</td>
  <td valign=top width=610 class=formvalue>
  <select name="staff_divpkey" id="staff_divpkey" style="width: 360px;" onChange="ajaxFunction();">
  <? $objdivpkey=$staff_data['staff_divpkey']; ?>
  <? include_once('object-selectdivision.tpl'); ?>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Department</td>
  <td valign=top width=610 class=formvalue>
  <select name="staff_deptpkey" id="staff_deptpkey" style="width: 360px;">
  <? if ($staff_data['staff_deptpkey']!="") { ?>
    <? $objdivpkey=$staff_data['staff_divpkey']; ?>
    <? $objdeptpkey=$staff_data['staff_deptpkey']; ?>
    <? include_once('object-selectdepartment.tpl'); ?>
  <? } else { ?>
    <option value="">--Select Department--</option>
  <? } ?>
  </select>
  </td>
  </tr>
  <? if ($pubtask=="staffedit") { ?>
    <!--
    <tr valign=top>
    <td valign=top width=150 class=formlabel>User Name</td>
    <td valign=top width=610 class=formvalue>
    <?=$staff_data['user_name'];?>&nbsp;
    </td>
    </tr>
    -->
  <? } ?>
  <!--input name="job" id="job" type="hidden" value=""-->
  <!--/form-->
  </table>

  <br>
  <table border=1 cellspacing=1 cellpadding=2 width=760 style="border: 1px inset;">
  <input name="user_pkey" id="user_pkey" type="hidden" value="<?=$user_data['user_pkey'];?>">
  <input name="user_type" id="user_type" type="hidden" value="<?=$user_data['user_type'];?>">
  <input name="user_password" id="user_password" type="hidden" value="<?=$user_data['user_password'];?>">
  <input name="user_status" id="user_status" type="hidden" value="<?=$user_data['user_status'];?>">
  <tr valign=top>
  <td valign=top width=150 class=formlabel>User Name</td>
  <td valign=top width=610 class=formvalue>
  <input name="user_name" id="user_name" type="text" value="<?=$user_data['user_name'];?>" <? if (($pubuserrole<2)||($pubtask=="selfedit")) { echo "disabled"; } ?> style="width: 240px;" onkeypress="alphanumericOnly();"><? if (($pubuserrole<2)||($pubtask=="selfedit")) { echo "&nbsp;"; } else { echo "&nbsp;&nbsp;<i>(lowercase a-z and numbers only)</i>"; } ?>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>User Level</td>
  <td valign=top width=610 class=formvalue>
  <select name="user_role" id="user_role" <? if (($pubuserrole<2)||($pubtask=="selfedit")) { echo "disabled"; } ?> style="width: 240px;">
  <option value="0" selected>--Select Level--</option>
  <option value="1" <? if ($user_data['user_role']==1) echo "selected"; ?>>Standard User</option>
  <option value="2" <? if ($user_data['user_role']==2) echo "selected"; ?>>System Administrator</option>
  </select>
  </td>
  </tr>
  <tr valign=top>
  <td valign=top width=150 class=formlabel>Access Group</td>
  <td valign=top width=610 class=formvalue>
  <select name="user_grouppkey" id="user_grouppkey" <? if (($pubuserrole<2)||($pubtask=="selfedit")) { echo "disabled"; } ?> style="width: 240px;">
  <? include_once('object-selectusergroup.tpl'); ?>
  </select>
  </td>
  </tr>
  <input name="job" id="job" type="hidden" value="">
  </form>
  </table>

  <script type="text/javascript">

  function validateForm() {

    var isvalid=true;
    var errmsg="";
    var staffname=document.getElementById('staff_name');
    var staffdiv=document.getElementById('staff_divpkey');
    var staffdept=document.getElementById('staff_deptpkey');
    var username=document.getElementById('user_name');
    var userrole=document.getElementById('user_role');
    var usergroup=document.getElementById('user_grouppkey');

    //alert('~'+staffdept.value.length+'~');

    if ((staffname.value=="")||(staffdiv.value=="")||(staffdept.value=="")||(staffdept.value=="      ")) {
      errmsg='Staff Name, Department and Division must be specified!\n';
      isvalid=false;
    }
    if (username.value!="") {
      if ((userrole.value=="0")||(usergroup.value=="")) {
        errmsg+='User Level and Access Group must be specified!\n';
        isvalid=false;
      }
      if (isNumeric(username.value)) {
        errmsg+='Invalid User Name specified!\nUser Name must contain the letters a-z!';
        isvalid=false;
      }
    }
    if (isvalid==false) alert('Error:\n'+errmsg);
    return isvalid;

  }

  </script>
