/**
*@copyright 2009 Tech Support Holdings Limited
*@author Chris Harrison <chris@techsupportuk.com>
*@package Ajax main
*This is the main file for ajax functions
**/

//define the ajax function
function Ajax() {
  this.req = null;
  this.url = null;
  this.status = null;
  this.statusText = '';
  this.method = 'GET';
  this.async = true;
  this.dataPayload = null;
  this.readyState = null;
  this.responseText = null;
  this.responseXML = null;
  this.handleResp = null;
  this.responseFormat = 'xml', // 'text', 'xml', 'object'
  this.mimeType = null;
  this.headers = [];

  
  this.init = function() {
    var i = 0;
    var reqTry = [ 
      function() { return new XMLHttpRequest(); },
      function() { return new ActiveXObject('Msxml2.XMLHTTP') },
      function() { return new ActiveXObject('Microsoft.XMLHTTP' )} ];
      
    while (!this.req && (i < reqTry.length)) {
      try { 
        this.req = reqTry[i++]();
      } 
      catch(e) {}
    }
    return true;
  };
  this.doGet = function(url, hand, format) {
    this.url = url;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.doReq();
  };
  this.doPost = function(url, dataPayload, hand, format) {
    this.url = url;
    this.dataPayload = dataPayload;
    this.handleResp = hand;
    this.responseFormat = format || 'text';
    this.method = 'POST';
    this.doReq();
  };
  this.doReq = function() {
    var self = null;
    var req = null;
    var headArr = [];
    
    if (!this.init()) {
      alert('Could not create XMLHttpRequest object.');
      return;
    }
    req = this.req;
    req.open(this.method, this.url, this.async);
    if (this.method == "POST") {
      this.req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    if (this.method == 'POST') {
      req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    self = this;
    req.onreadystatechange = function() {
      var resp = null;
      self.readyState = req.readyState;
      if (req.readyState == 4) {
        
        self.status = req.status;
        self.statusText = req.statusText;
        self.responseText = req.responseText;
        self.responseXML = req.responseXML;
        
        switch(self.responseFormat) {
          case 'text':
            resp = self.responseText;
            break;
          case 'xml':
            resp = self.responseXML;
            break;
          case 'object':
            resp = req;
            break;
        }
        
        if (self.status > 199 && self.status < 300) {
          if (!self.handleResp) {
            alert('No response handler defined ' +
              'for this XMLHttpRequest object.');
            return;
          }
          else {
            self.handleResp(resp);
          }
        }
        
        else {
          self.handleErr(resp);
        }
      }
    }
    req.send(this.dataPayload);
  };
  this.abort = function() {
    if (this.req) {
      this.req.onreadystatechange = function() { };
      this.req.abort();
      this.req = null;
    }
  };
  this.handleErr = function() {
    var errorWin;
    // Create new window and display error
    try {
      errorWin = window.open('', 'errorWin');
      errorWin.document.body.innerHTML = this.responseText;
    }
    // If pop-up gets blocked, inform user
    catch(e) {
      alert('An error occurred, but the error message cannot be' +
      ' displayed because of your browser\'s pop-up blocker.\n' +
      'Please allow pop-ups from this Web site.');
    }
  };
  this.setMimeType = function(mimeType) {
    this.mimeType = mimeType;
  };
  this.setHandlerResp = function(funcRef) {
    this.handleResp = funcRef;
  };
  this.setHandlerErr = function(funcRef) {
    this.handleErr = funcRef; 
  };
  this.setHandlerBoth = function(funcRef) {
    this.handleResp = funcRef;
    this.handleErr = funcRef;
  };
  this.setRequestHeader = function(headerName, headerValue) {
    this.headers.push(headerName + ': ' + headerValue);
  };
  
}

//General function area:
function requestpassword(){
		var poststr = "";
		poststr = poststr + "email=" + document.getElementById("username").value;
   	 	strhand = function(strings) {
   	 		document.getElementById("loginbox").innerHTML = strings;
   	 	}
		var xmlajax = new Ajax();
	  xmlajax.setMimeType('text/text');
      xmlajax.doPost('scripts/resetpassword.php', poststr, strhand, 'text');
	  poststr = '';
}
function member_list(){
		var poststr = "";
		poststr = poststr + "orgname=" + document.getElementById("orgname").value;
		poststr += "&orgsize=" + document.getElementById("orgsize").value;
		poststr += "&orgdiscount=" + document.getElementById("orgdiscount").checked;
		poststr += "&keywords=" + document.getElementById("keywords").value;
   	 	strhand = function(strings) {
   	 			document.getElementById("member_list").innerHTML = strings;
   	 	}
		var xmlajax = new Ajax();
	  	xmlajax.setMimeType('text/text');
      	xmlajax.doPost('scripts/memberlist.php', poststr, strhand, 'text');
}
function login_member(){
		
		var poststr = "";
		poststr = poststr + "email=" + document.getElementById("username").value;
		poststr += "&password=" + document.getElementById("password").value;
   	 	strhand = function(strings) {
   	 		if (strings=="ERRLOGIN"){
   	 			document.getElementById("loginbox").innerHTML = "Incorrect login";
   	 		} else {
   	 			
   	 			window.location = "members.php";
   	 		}
   	 	}
		var xmlajax = new Ajax();
	  xmlajax.setMimeType('text/text');
      xmlajax.doPost('scripts/login.php', poststr, strhand, 'text');
	  poststr = '';
}
function forgotpassword(){
	str_display = "<h2>Members Zone</h2>";
	str_display += "<form id=\"memberloginform\">";
	str_display += "<label for=\"username\">Email Address</label>";
    str_display += "<input type=\"text\" name=\"username\" id=\"username\" maxlength=\"50\" />";
    str_display += "<input type=\"button\" style=\"width:auto;\" value=\"Request\" name=\"request\" class=\"frontlogin\" onclick=\"javascript:requestpassword();\" />";
    str_display += "</form>";
	document.getElementById("loginbox").innerHTML = str_display;
}
function check_details() {
   	 var poststr = '';
			poststr = poststr + "number=" + document.getElementById("user_incorpnumber").value + "&";
			poststr = poststr + "wbfa=" + document.getElementById("user_wbfa").value;
      
   	 	strhand = function(strings) {
   	 		if (strings=="error"){
   	 			document.getElementById("error").innerHTML = "INVALID WEB FILING CODE";
   	 			document.getElementById("user_name").value = "";
   	 			document.getElementById("select").disabled = true;
   	 		} else {
   	 			document.getElementById("error").innerHTML = "";
   	 			document.getElementById("user_name").value = strings;
   	 			document.getElementById("select").disabled = false;
			}
   	 	}
		var xmlajax = new Ajax();
	  xmlajax.setMimeType('text/text');
      xmlajax.doPost('../scripts/phpscripts/check_details.php', poststr, strhand, 'text');
	  poststr = '';
   }
   
   function request_logo() {
   	 var poststr = '';
     poststr = "used=" + document.getElementById("used").value;
   	 	strhand = function(strings) {
   	 		document.getElementById("result").value = strings;
   	 	}
	  var xmlajax = new Ajax();
	  xmlajax.setMimeType('text/text');
      xmlajax.doPost('requestlogo.php', poststr, strhand, 'text');

   }
   
   function save_new_details(name,wbfa,number,userid) {
   	 var poststr = '';
		poststr = poststr + "number=" + number + "&";
		poststr = poststr + "wbfa=" + wbfa + "&";
		poststr = poststr + "userid=" + userid + "&";
		poststr = poststr + "name=" + name;
   	 	strhand = function(strings) {
   	 		document.getElementById("incorp_id").value = strings;
   	 	}
		var xmlajax = new Ajax();
	  xmlajax.setMimeType('text/text');
      xmlajax.doPost('../scripts/phpscripts/save_new_details.php', poststr, strhand, 'text');
	  poststr = '';
   }
