
function categories_delete(id){
   
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Delete&id='+ id,
		    onSuccess: after_categories_delete,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/categories_operation.php', opt);
	
}
function after_categories_delete(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_categories.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }
function categories_modify(){
   var id = getObj("id").value;
   var category_name = getObj("category_name").value;
   var sort = getObj("sort").value;
   var category_name_old = getObj("category_name_old").value;
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Modify&id='+ id + "&category_name=" + encodeURI(category_name)+"&category_name_old="+encodeURI(category_name_old)+"&sort="+sort,
		    onSuccess: after_categories_modify,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/categories_operation.php', opt);
	
}
function after_categories_modify(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_categories.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }

 function categories_add(){
   var category_name = getObj("category_name").value;
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Insert&category_name=' + encodeURI(category_name),
		    onSuccess: after_categories_add,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/categories_operation.php', opt);
	
}
function after_categories_add(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_categories.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }





function items_delete(id){
   
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Delete&id='+ id,
		    onSuccess: after_item_delete,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/items_operation.php', opt);
	
}
function after_item_delete(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_items.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }
 
 
function items_modify(){
   var id = getObj("id").value;
   var item_name = getObj("item_name").value;
   var item_name_old = getObj("item_name_old").value;
   var category_id =getObj("category_id").value;
   var item_number =getObj("item_number").value;
   var units =getObj("units").value;
   var sort =getObj("sort").value;
   var slot =getObj("slot").value;
   
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Modify&id='+ id + "&item_name=" + encodeURI(item_name)+"&category_id="+encodeURI(category_id) +"&units="+encodeURI(units)+"&item_number="+encodeURI(item_number) +"&item_name_old="+encodeURI(item_name_old) +"&sort="+sort +"&slot="+slot,
		    onSuccess: after_item_modify,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/items_operation.php', opt);
	
}


function after_item_modify(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_items.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }

 function items_add(){
   var item_name = getObj("item_name").value;
   var category_id =getObj("category_id").value;
   var item_number =getObj("item_number").value;
   var units =getObj("units").value;
   var slot = getObj("slot").value;
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Insert&item_name=' + encodeURI(item_name)+"&category_id="+encodeURI(category_id) +"&units="+encodeURI(units)+"&item_number="+encodeURI(item_number)+"&slot="+encodeURI(slot),
		    onSuccess: after_item_add,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/items_operation.php', opt);
	
}
function after_item_add(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_items.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      } 	
 }

 
 
 function admin_resetpassword(name,passwordold,password)
 {
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Resetpassword&name='+ encodeURI(name) + "&passwordold=" + encodeURI(passwordold)+"&password="+encodeURI(password),
		    onSuccess: after_reset,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/admin_login.php', opt);
 }
 function after_reset(oRes){
		var data = oRes.responseText;
    	if (data == 'true')
    	{
    		alert("Reset admin password success!");
      }
      else
      {
    		alert(data);
      }
 	
 }

function admin_login(name,password)
 {
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Login&name='+ encodeURI(name) + "&password=" + encodeURI(password),
		    onSuccess: after_login,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/admin_login.php', opt);
 }
 function after_login(oRes){
		var data = oRes.responseText;
    	if (data == 'true')
    	{
    		alert("Login success!");
      }
      else
      {
    		alert("Login failure.");
      }
 	
 }
function send_password(email)
 {
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Sendpassword&email=' + encodeURI(email),
		    onSuccess: after_send,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
	  window.status ='Send email,please wait...';	
	  new Ajax.Request('function/accounts_operation.php', opt);
 }
 function after_send(oRes){
	var data = oRes.responseText;
	window.status ="";
   	alert(data); 
 }

 
function accounts_login(email,account_number)
 {
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Login&email='+encodeURI(email)+"&account_number="+encodeURI(account_number),
		    onSuccess: after_accounts_login,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('function/accounts_operation.php', opt);
 }
function after_accounts_login(oRes)
	{
	var data = oRes.responseText;
    if (data.substring(0,1) == '1')    	
    	{
		data = data.substring(1,data.length);
		alert(data);
 		window.location.href = "order.php";
  		}
  	else
  		{
		alert(data);
  		}
	
 	}
 
 
function accounts_add()
 {
   var name = getObj("name").value;
   var account_number = getObj("account_number").value;
   var email = getObj("email").value;
   //var password = getObj("password").value;
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Insert&name='+ encodeURI(name) +'&account_number=' + encodeURI(account_number) +'&email='+encodeURI(email),
		    onSuccess: after_accounts_add,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('function/accounts_operation.php', opt);
 }
 function after_accounts_add(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
	 		window.history.back();
      }
      else
      {
    		alert(data);
      }
 	
 }

function accounts_modify()
 {
   var id = getObj("id").value;
   var name = getObj("name").value;
   var account_number = getObj("account_number").value;
   var email = getObj("email").value;
   var email_old = getObj("email_old").value;
   //var napasswordme = getObj("password").value;
   var opt = {
		    method: 'post',
		    postBody: 'request_opt=Modify&id='+ id +'&name='+ encodeURI(name) +'&account_number=' + encodeURI(account_number) +'&email='+encodeURI(email)+"&email_old=" + encodeURI(email_old),
		    onSuccess: after_accounts_modify,
		    on404: function(t) {
		        alert('Error 404: location "' + t.statusText + '" was not found.');
		    },
		    onFailure: function(t) {
		        alert('Error ' + t.status + ' -- ' + t.statusText);
		    }
		}
		
	  new Ajax.Request('../function/accounts_operation.php', opt);
 }
 function after_accounts_modify(oRes){
		var data = oRes.responseText;
    	if (data.substring(0,1) == '1')    	
    	{
    		data = data.substring(1,data.length);
    		alert(data);
    		var url = "edit_accounts.php?page=" + getObj("page").value
		 	if (document.all)
		 		window.location.href =  url;
		 	else
		 		window.location = url;
      }
      else
      {
    		alert(data);
      }
 	
 }
 


function getObj(n,d) {

  var p,i,x; 

  if(!d)

      d=document;

   
   if(n != undefined)
   {
	   if((p=n.indexOf("?"))>0&&parent.frames.length) {

		   d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);

	   }
   }



  if(!(x=d[n])&&d.all)

      x=d.all[n];

 

  for(i=0;!x&&i<d.forms.length;i++)

      x=d.forms[i][n];

 

  for(i=0;!x&&d.layers&&i<d.layers.length;i++)

      x=getObj(n,d.layers[i].document);

 

  if(!x && d.getElementById)

      x=d.getElementById(n);


  return x;

}
   today = function (flag)
    {
    	var oDate = new Date();
    	var year = oDate.getFullYear();
    	var month = oDate.getMonth() + 1;
    	if(month < 10) month = "0" + month;
    	var day = oDate.getDate();
    	if(day < 10) day = "0" + day;
    	if (flag)
    		return year + "-" + month +"-" + day +" " + oDate.getHours() +":" + oDate.getMinutes() +":" + oDate.getSeconds();
    	else
	    	return year + "-" + month +"-" + day;
    }
    
    testEmail =  function (oEle) 
	{
		if (oEle.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
			return true;
		else{
			alert("Error email format.");
			oEle.focus();
			return false;
		}
	}
