/**************************************************
 * @name 		AJAX Administration               *
 * @version 	2.3.2                             *
 * @author 	    Essam Alahmadi                        *
 * @copyright 	alserb.com 2008 (C)              *
 * @mail        alserb@alserb.com            *
 * @TEL         +966500057888                    *
 * @date        22-02-2008                        *
 **************************************************/

var Objects = new Array();
var http = createRequestObject();
var whereajax = "";
var ajaxmessage = "";

function show_hide_element( name, status ){
   var curelement = FetchObject( name );
   if ( status == 'show' ){
      curelement.style.display = "inline";
   }
   else{
      curelement.style.display = "none";
   }
}

//  ---------------------------------------

function FetchObject( idname, forcefetch )
{
   if( forcefetch || typeof( Objects[idname] ) == "undefined" )
   {
      Objects[idname] = document.all[idname];
   }

   return Objects[idname];
}

//  ---------------------------------------

function createRequestObject(){
   var request_;
   var browser = navigator.appName;

   if( browser == "Microsoft Internet Explorer" || typeof ActiveXObject != "undefined" ){
      request_ = new ActiveXObject( "Microsoft.XMLHTTP" );
   }
   else{
      request_ = new XMLHttpRequest();
   }
   return request_;
}

//  ---------------------------------------

function ajax( plugin, myvalue, reference ){
   http.open( 'get', 'plugins/' + plugin + '/ajax.php?action=' + reference + '&value=' + myvalue );
   http.onreadystatechange = catInfoajax;
   http.send( null );
}

//  ---------------------------------------

function catInfoajax(){

   if( http.readyState == 1 ){
      document.getElementById( whereajax ).innerHTML = ajaxmessage;
   }

   if( http.readyState == 4 ){
      if( http.status == 200 ){
         var response = http.responseText;
         document.getElementById( whereajax ).innerHTML = response;
      }
   }
}

//  ---------------------------------------

function ajax_div(ajax_dst, input_value, plugin, reference){
   if ( input_value == "" || input_value == "0"){
      show_hide_element( ajax_dst, 'hide' );
   }
   else{
      whereajax = ajax_dst;
      ajaxmessage = '<div align="center" style="font-family:Tahoma;font-size:10px"><img src="images/ajax.gif" />  جاري جلب البيانات المطلوبة</div>';

      ajax( plugin, input_value, reference );
      show_hide_element( ajax_dst, 'show' );
   }
}

//  ---------------------------------------

var http_request = false;

function makePOSTRequest(parameters){

   var url = 'ajax.php';

   http_request = false;
   if (window.XMLHttpRequest) {
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {

         http_request.overrideMimeType('text/html');
      }
   }
   else if (window.ActiveXObject) {
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e) {
         }
      }
   }
   if ( ! http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }

   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true); 
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}

//  ---------------------------------------

function alertContents(){
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
         result = http_request.responseText;
         document.getElementById(whereajax).innerHTML = result;
      }
      else {
         alert('هناك مشكلة في العملية السابقة');
      }
   }
}

// -----------------------------------------

function gotopage( url, plugin , id){
   parent.frames.admin.location = 'admin.php?file=' + url + '&plugins=' + plugin + '&id=' + id;
}

