function jsAJAX() { this._construct() }
(function() {
 
	jsAJAX.prototype = {
		_target: "",
		_action: "",
		_form: null,
		
		_construct: function() {  },
		
		_createFrame: function(form, events) {
			var sForm = "";
			if (form.name) sForm = "document.forms['" + form.name + "']"; 
			else sForm = "document.forms[0]";
			var frameID = 'frame' + Math.floor(Math.random() * 99999);
			var createDIV = document.createElement('DIV');
			createDIV.innerHTML = '<iframe style="display:none" src="about:blank" id="'+frameID+'" name="'+frameID+'" onload="var ojsAJAX = new jsAJAX(); ojsAJAX._form = '+sForm+'; ojsAJAX._action = \''+this._action+'\'; ojsAJAX._target = \''+this._target+'\'; ojsAJAX.param=' + (typeof(this.param) == 'string' ? '\'' + this.param + '\'' : this.param) + '; ojsAJAX._loadedFrame(\''+frameID+'\')"></iframe>';
			document.body.appendChild(createDIV);

			var oframeID = document.getElementById(frameID);
			if (events && typeof(events.onComplete) == 'function') {
				oframeID.onComplete = events.onComplete;
			}
			
			return frameID;
		},
		
 		_loadedFrame: function(id) {

			var oframeID = document.getElementById(id);
			if (oframeID.contentDocument) {
				var oContent = oframeID.contentDocument;
			} else if (oframeID.contentWindow) {
				var oContent = oframeID.contentWindow.document;
			} else {
				var oContent = window.frames[id].document;
			}
			if (oContent.location.href == "about:blank") {
				return;
			}
	
			if (typeof(oframeID.onComplete) == 'function') {
				
				this._form.setAttribute('action', this._action);
				this._form.setAttribute('target', '');
				oframeID.onComplete(oContent.body.innerHTML,oContent.forms,this.param);
				//this._parseXML(oContent.body.innerHTML)
				//document.getElementById(id).parentNode.removeChild(document.getElementById(id));
				//document.close();
			}
		},
		
		_xmlhttpPost: function(form, events) {
			
			var xmlHttpReq = false;
			var self = this;
			
			if (window.XMLHttpRequest) {
				// Mozilla/Safari
				self.xmlHttpReq = new XMLHttpRequest(); 
			} else if (window.ActiveXObject) { 
				// IE
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
			}
			
			self.xmlHttpReq.open('POST', this.action, true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			if (events && typeof(events.onComplete) == 'function') {
				self.xmlHttpReq.onreadystatechange = function() {
					if (self.xmlHttpReq.readyState == 4) {
						events.onComplete(self.xmlHttpReq.responseText);
					}
				}
			}
			
			self.xmlHttpReq.send(this._createQueryString(form));
			
			return false;
		},
		
		_createQueryString: function(form) {
			var returnString = "";
			if (form) {
				for(var i=0; i<form.length; i++) {
					if (form[i].name)
					returnString += "&" + form[i].name + "=" + escape(form[i].value);
				}
				
				if (form.action.split('?').length > 1)
					returnString += form.action.split('?')[1].split('&');
			}
			
			return returnString;
		},
		
		_initialize: function(form) {
			if(this.action == "") { this.action = form.action; } else { this._action = form.action; form.setAttribute('action', this.action); }
			return form;
		},
		
		//Properties
		action: "",
		param: null,
		
		//Procedures
		submit: function(form, events) {
			form = this._initialize(form);

			if(form.enctype == 'multipart/form-data') {
				this._form = form;
				this._target = form.target;
				form.setAttribute('target', this._createFrame(form, events));
				form.submit();
				if (events && typeof(events.onStart) == 'function') {
					return events.onStart();
				} else {
					return true;
				}
			} else {
				if (events && typeof(events.onStart) == 'function') {
					if(events.onStart()) {
						return this._xmlhttpPost(form, events);
					}
				} 
			}
		}
	}
})();


function jsAJAX_startAttach() {
	jsAJAX_resetElement('imgProg');
	document.getElementById('imgProg').style.display = 'inline';
	return true;
}
function jsAJAX_completeAttach(response, form, param) {
	jsAJAX_resetElement('imgProg');
	//alert(form["POST"]["ent_id"].value);
	if (form["afile"]["FileErr"].value == "") {
		jsAJAX_addRow(form["afile"]["FileName"].value, form["afile"]["FileType"].value, form["afile"]["FileSize"].value);
	}
	else {
		alert(form["afile"]["FileErr"].value);
	}
	document.getElementById('imgProg').style.display = 'none';
	jsAJAX_resetElement("afile");
	document.frm_form.afile.value = '';
}
function jsAJAX_attach() {
	var ojsAJAX = new jsAJAX();
	ojsAJAX.param = 'document.frm_form';
	ojsAJAX.action = "/includes/jsAJAX.php";
	ojsAJAX.submit(document.frm_form, {'onStart' : jsAJAX_startAttach, 'onComplete' : jsAJAX_completeAttach});
}

function jsAJAX_delRow(rIndex) {
	var oRow = document.getElementById(rIndex);
	oRow.parentNode.removeChild(oRow)
	
}

function jsAJAX_addRow(FileName, FileType, FileSize) {
	var oTableCell = null;
	var oTable = document.getElementById('otable');
	
	var len = oTable.rows.length;
	var oTableRow = oTable.insertRow(len);

	oTableRow.id = 'r' + len;

	oTableCell = oTableRow.insertCell(0); 
	oTableCell.innerHTML = FileName + " [" + FileType + "]" + "<input type='hidden' name='ent_FileName[]' id='ent_FileName" + len + "' value='" + FileName + ";" + FileType + ";" + FileSize + "' />";
	
	oTableCell = oTableRow.insertCell(1);
	oTableCell.innerHTML = FileSize;
	
	//oTableCell = oTableRow.insertCell(2);
	//oTableCell.innerHTML = FileSize;

	oTableCell = oTableRow.insertCell(2);
	oTableCell.align = "right";
	oTableCell.innerHTML = "<a href='javascript:jsAJAX_delRow(\"r" + len + "\")'>Remove</a>";
}

function jsAJAX_resetElement(IdName) {
	var originalInput = document.getElementById(IdName);
	var clearedInput = originalInput.cloneNode(false);
	originalInput.parentNode.insertBefore(clearedInput, originalInput);
	originalInput.parentNode.removeChild(originalInput);
}

function jsAJAX_this_addnew_org() {
	var oTableCell = null;
	var oTable = document.getElementById('otable_org');

	var len = oTable.rows.length;
	var oTableRow = oTable.insertRow(len);

	oTableRow.id = 'ro' + len;

	oTableCell = oTableRow.insertCell(0); 
	oTableCell.innerHTML = '<div class="pipe">&nbsp;</div>';
	oTableCell.innerHTML = oTableCell.innerHTML + '<div class="row"><label>Other organization</label></div>';
	oTableCell.innerHTML = oTableCell.innerHTML + '<div><input type="text" class="txt" value="" name="ent_org_other[]" /> <span>[<a href="javascript:jsAJAX_delRow(\'ro' + len + '\');">x</a>]</span></div>';
	oTableCell.innerHTML = oTableCell.innerHTML + '<div class="row"><label>Department</label></div>';
	oTableCell.innerHTML = oTableCell.innerHTML + '<div><input type="text" class="txt" value="" name="ent_org_dep_other[]" /></div>';
	/**/
}
