﻿function Remoting(type) {
	this.path = "http://www.loew-cornell.com";
	Remoting.http_request = false;
	Remoting.type = type;
	this.type = type;
}

Remoting.http_request = false;
Remoting.type = "";

function Remoting_send(parameters) {	
	Remoting.http_request = false;
	
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      Remoting.http_request = new XMLHttpRequest();
      if (Remoting.http_request.overrideMimeType) {
         Remoting.http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         Remoting.http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            Remoting.http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!Remoting.http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
	
	switch(this.type){
		case "faqs":
			this.sendAjaxForm(this.path + '/submission/submitQuestion.pl', parameters);
			break;
		case "submitretailer":
			this.sendAjaxForm(this.path + '/submission/submitRetailer.pl', parameters);
			break;
		case "register":
			this.sendAjaxForm(this.path + '/submission/submitMember.pl', parameters);
			break;
		case "validate":
			this.sendAjaxForm(this.path + '/submission/submitMember.pl', parameters);
			break;
		default:
			alert("invalid remoting type = " + this.type);
	}
}

function sendAjaxForm(url, parameters) {	
	if (Remoting.http_request) {
		 Remoting.http_request.onreadystatechange = this.sendAjaxFormResult;
	   
	   Remoting.http_request.open('POST', url, true);
	   Remoting.http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	   
	   try {
	   	Remoting.http_request.send(parameters);
	   } catch (e) {
	   	document.getElementById('formContainer').innerHTML 
	         										= "<div class='textParagraph'>" +
																"	<h2>" +
																"		Your web browser is not fully supported by IndoRelation." +
																"		<br />Please upgrade to a newer version of your current browser." +
																"	</h2>" +
																"</div>";
	  }
	}
}

function returnResult() {
   if (Remoting.http_request.readyState == 4) { 
      if (Remoting.http_request.status == 200) {

         var xmldoc = Remoting.http_request.responseXML;
         
         var root = xmldoc.getElementsByTagName('root').item(0);
         
         for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
         	var node = root.childNodes.item(iNode);
         	
         	if (node.tagName) {
         		if (node.tagName == "status") {
         			return node.childNodes.item(0).data;
         		} 
         	}
         }
         
      }
   }
}

function sendAjaxFormResult() {
	var statusID = 0;
	
   if (Remoting.http_request.readyState == 4) { 
      if (Remoting.http_request.status == 200) {

         var xmldoc = Remoting.http_request.responseXML;
         
         var root = xmldoc.getElementsByTagName('root').item(0);
         
         for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
         	var node = root.childNodes.item(iNode);
         	
         	if (node.tagName) {
         		if (node.tagName == "statusID") {
         			statusID = parseInt(node.childNodes.item(0).data);
         		} else if (node.tagName == "status") {         			
         			if (statusID == 1) {
         				document.getElementById('formContainer').innerHTML 
												= node.childNodes.item(0).data;
         			} else {
         				if (Remoting.type == "validate") {
         					document.getElementById('validateErrorMsg').innerHTML 
												= node.childNodes.item(0).data;
         				} else if (Remoting.type == "register") {
         					document.getElementById('registerErrorMsg').innerHTML 
												= node.childNodes.item(0).data;
         				} else {
									document.getElementById('errorMsg').innerHTML 
												= node.childNodes.item(0).data;
								}
         			}
         		} 
         	}
         }
         
      }
   }
}

Remoting.prototype.send = Remoting_send;
Remoting.prototype.sendAjaxForm = sendAjaxForm;
Remoting.prototype.returnResult = returnResult;
Remoting.prototype.sendAjaxFormResult = sendAjaxFormResult;