function clear_values() {
	for (var i = 0; i < arguments.length; i++) 
		$(arguments[i]).value = '';
}
function toggle_display(d_area,type) {
	$(d_area).setStyle({display:type});
}
function fill_input() {
	for (var i = 0; i < arguments.length; i++) {
		var p_name = arguments[i].split('=');
		$(p_name[0]).value = $F(p_name[1]);
	}
}
function checkall(obj,bool) {
	var i=0, c;
	while (c = obj[i++]) {
		if (!c.disabled)
		c.checked = (bool ? bool : (c.checked ? false : true));
	}
}
function show_page_feedback(feedback) {
	var feedback_hldr = $('page_feedback_holder');
	var top_pos = document.documentElement.scrollTop;
	if (top_pos)
		feedback_hldr.setStyle({top : top_pos + 25 + 'px'});
	
	feedback_hldr.innerHTML = feedback;
	toggle_display('page_feedback_holder','block');
	window.setTimeout("toggle_display('page_feedback_holder','none')", 4000);
}

function clear_form() {
	fobj = document.selectionsheet;
	for (var i = 0; i < fobj.elements.length; i++) {
		alert(fobj.elements[i].name);
		
	}
}



var popup_windows = new Object();
function Agent() {
	this.url = 'include/ajax_proxy.php';
	this.style_timer='';
	this.initialize = function(freq,sobj) {
		this.start_poll(freq,sobj);
		Ajax.Responders.register({ 
								 onCreate : function(request) {Ajax.activeRequestCount++;clearTimeout(this.style_timer);this.style_timer=window.setTimeout('$(\'ajax_progress\').style.display=\'block\'',1500);request['timeoutId']=window.setTimeout(function(){if(agent.callInProgress(request.transport)){agent.abort_request(request);if(request.options['onFailure']){request.options['onFailure'](request.transport,request.json);}}},10000);},
								 onComplete : function(request) {Ajax.activeRequestCount--;clearTimeout(this.style_timer);$('ajax_progress').style.display = 'none';window.clearTimeout(request['timeoutId']);},
								 onFailure : function(request){return;}
		});
	}
	this.call = function() {
		var class_name = arguments[0];
		var sf_func = arguments[1];
	
		if (arguments.length > 3) {
			var aa_poststr = '';
			for (var i = 3; i < arguments.length; i++) {
				aa_poststr += '&aa_sfunc_args[]='+arguments[i];
				var split_it = arguments[i].split('=');
				aa_poststr += '&'+split_it[0]+'='+split_it[1];
			}
		}
		var client_func = arguments[2];
		
		v = new Ajax.Request(this.url, {//alert(resp.responseText);if(debug){alert(resp.responseText);}
							 parameters : 'aa_ajax=1&aa_sobj='+class_name+'&aa_sfunc='+sf_func+aa_poststr,
							 onSuccess : function(resp) {switch(client_func){case 'cf_loadcontent':cf_loadcontent(resp.responseXML);break;case 'show_popup_window':show_popup_window(resp.responseXML);break;case 'refresh_form':refresh_form(resp.responseXML);break;case 'nothing':nothing(resp.responseXML);break;}}
							 });
	}
	this.abort_request = function (xmlhttp) {
		return;
		Ajax.failedRequestCount++;
		xmlhttp.transport.abort();	
	}
	this.callInProgress = function(xmlhttp) {
		switch (xmlhttp.readyState) {
			case 1: case 2: case 3:
			return true;
			break;
			// Case 4 and 0
			
			default:
			return false;
			break;
		}
	}
	this.showFailureMessage = function() {
		return;
		alert('uh oh, it looks like the network is down. Try again shortly');
	}
}

var agent = new Agent();

function failit() {
	alert('Communication request has failed');
}

var d = document;
function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function submit_form(fobj,class_name,sf_func,cf_func) {
	var str = '';
	if (arguments.length > 4) {
		for(var i = 4; i < arguments.length; i++) {
			if (typeof(arguments[i]) == 'object') {
				for (var j = 0; j < arguments[i].length; j++) {
					var split_it = arguments[i][j].split('=');
					str += '&'+split_it[0]+'='+split_it[1];
				}
			} else {
				var split_it = arguments[i].split('=');
				str += '&'+split_it[0]+'='+split_it[1];
			}
		}		
	}
	agent.call(class_name,sf_func,cf_func,Form.serialize(fobj),str);
}
function reset_error_nodes(xmlData,div_class,div_id) {
	if ($(div_id)) {
		var a = $(div_id).getElementsByClassName(div_class);
		if (a) {
			for (var index = 0; index < a.length; ++index) {   
				if (!xmlData.getElementsByTagName(a[index].id).item(0))
					$(a[index]).removeClassName(div_class);   
			}
		}
	}
}
function refresh_form(xml) {
	if (xml.hasChildNodes()) {
		var xmlData = xml.documentElement;
		var error = xmlData.getElementsByTagName('error').item(0);
		var form_return = xmlData.getElementsByTagName('form_return').item(0);
		var popup_data = xmlData.getElementsByTagName('popup_controls').item(0);
		var submit_btn = xmlData.getElementsByTagName('submit_btn').item(0);
		if (submit_btn) {
			toggle_submit(submit_btn.childNodes[0].nodeValue);
		}
		if (popup_data && popup_data.getElementsByTagName('popup_id').item(0).childNodes[0]) 
			var popup_id = popup_data.getElementsByTagName('popup_id').item(0).childNodes[0].nodeValue;
		else
			var popup_id = '';

		if (form_return) {
			var feedback_msg = form_return.getElementsByTagName('feedback').item(0);
			var error_nodes = form_return.getElementsByTagName('err').item(0);
			//If we have multiple values under feedback msg, parse them out by line breaks
			if (feedback_msg.childNodes[0].childNodes.length > 0) {
				var feedback_txt = 'Please check your data, we found the following errors:<br /><br />';
				for (var i = 0; i < feedback_msg.childNodes.length; i++) {
					if (feedback_msg.childNodes[i].childNodes[0])
						feedback_txt += feedback_msg.childNodes[i].childNodes[0].nodeValue+'<br />';
				}
			} else 
				var feedback_txt = feedback_msg.childNodes[0].nodeValue;
		}
		//The submitted form contains errors
		if (error) {
			reset_error_nodes(xmlData,'error_class','main_table'+popup_id);
			reset_error_nodes(xmlData,'error_class_text','main_table'+popup_id);

			$('feedback_holder'+popup_id).style.display = 'block';
			$('feedback_message'+popup_id).innerHTML = feedback_txt;
			if (error_nodes) {
				for (var i = 0; i < error_nodes.childNodes.length; i++) {
					if ($(error_nodes.childNodes[i].nodeName).nodeType  == 'text')
						$(error_nodes.childNodes[i].nodeName).addClassName('error_class_text');
					else
						$(error_nodes.childNodes[i].nodeName).addClassName('error_class');
				}
			}
			if (xmlData.getElementsByTagName('jscript').item(0)) {
				for (var i = 0; i < xmlData.getElementsByTagName('jscript').item(0).childNodes.length; i++) {
					if (window.execScript)
						window.execScript(xmlData.getElementsByTagName('jscript').item(0).childNodes[i].nodeValue);
					else
						window.setTimeout(xmlData.getElementsByTagName('jscript').item(0).childNodes[i].nodeValue,0);
				}
			}
		//The submitted form is free of errors
		} else {
			if (xmlData.getElementsByTagName('page_feedback').item(0)) 
				show_page_feedback(xmlData.getElementsByTagName('page_feedback').item(0).childNodes[0].nodeValue);
			
			var action = xmlData.getElementsByTagName('action').item(0);
			if (!action)
				return alert('Action missing. Unable to refresh form output.');
			if (action.childNodes.item(0).nodeValue == 'close') {
				if (!popup_id)
					alert('Error 209?? Missing popup id');
					
				popup_windows[popup_id].hide();
				
				cf_loadcontent(xml);				
			} else if (action.childNodes.item(0).nodeValue == 'continue') 
				cf_loadcontent(xml);										
			
			if ($('feedback_holder'+popup_id) && $('main_table'+popup_id)) {
				$('feedback_holder'+popup_id).style.display = 'none';
				reset_error_nodes(xmlData,'error_class','main_table'+popup_id);
				reset_error_nodes(xmlData,'error_class_text','main_table'+popup_id);
			}

			if (xmlData.getElementsByTagName('jscript_action').item(0)) {
				if (window.execScript)
					window.execScript(xmlData.getElementsByTagName('jscript_action').item(0).childNodes[0].nodeValue);
				else
					window.setTimeout(xmlData.getElementsByTagName('jscript_action').item(0).childNodes[0].nodeValue,0);
			}
		}
	} 
}

function focus_to(el) {
	if ($(el))
		$(el).focus();
}

//A more concise version of refresh_form, but without all the functionality
function nothing(xml) {
	if (xml.hasChildNodes()) {
		var xmlData = xml.documentElement;
		if (xmlData.getElementsByTagName('jscript_action').item(0)) {
			if (window.execScript)
				window.execScript(xmlData.getElementsByTagName('jscript_action').item(0).nodeValue);
			else
				window.setTimeout(xmlData.getElementsByTagName('jscript_action').item(0).nodeValue,0);
		}
	}
	return;
}

function cf_loadcontent(xml) {
	var jscript_xml = '';
	if (xml.hasChildNodes()) {
		var xmlData = xml.documentElement;
		for (var i = 0; i < xmlData.childNodes.length; i++) {
			switch (xmlData.childNodes[i].nodeName) {
				case 'html':
				write_content(xmlData.childNodes[i],'html');
				break;

				case 'html_append':
				write_content(xmlData.childNodes[i],'html_append');
				break;

				case 'value':
				write_content(xmlData.childNodes[i],'value');
				break;
				
				case 'value_append':
				write_content(xmlData.childNodes[i],'value_append');
				break;
				
				case 'jscript':
				write_content(xmlData.childNodes[i],'jscript');
				break;
				
			}
		}
	} 
}

function show_popup_window(xml) {
	if (xml.hasChildNodes()) {
		cf_loadcontent(xml);
		var xmlData = xml.documentElement;
		for (var i = 0; i < xmlData.childNodes.length; i++) {
			switch (xmlData.childNodes[i].nodeName) {
				case 'class_name':
				if (xmlData.childNodes[i].firstChild)
					var class_name = xmlData.childNodes[i].firstChild.nodeValue;
				
				break;

				case 'row_lock':
				if (xmlData.childNodes[i].firstChild)
					var row_lock_id = xmlData.childNodes[i].firstChild.nodeValue;
				
				break;

				case 'focus':
				if (xmlData.childNodes[i].firstChild) 
					var focus_to_field = xmlData.childNodes[i].firstChild.nodeValue;
				
				break;

				case 'popup_controls':
				for (var j = 0; j < xmlData.childNodes[i].childNodes.length; j++) {
					switch (xmlData.childNodes[i].childNodes[j].nodeName) {
						case 'popup_id':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_id = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'popup_title':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_title = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'onclose':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var onclose_func = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'popup_width':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_width = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'popup_height':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_height = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'popup_resize':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_resize = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'popup_scrolling':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var popup_scrolling = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
						case 'cmdTable':
						if (xmlData.childNodes[i].childNodes[j].childNodes[0])
							var cmdTable = xmlData.childNodes[i].childNodes[j].childNodes[0].nodeValue;
						
						break;
					}
				}
				break;
			}
		}
		
		popup_windows[popup_id] = dhtmlwindow.open(popup_id,'inline',cmdTable,popup_title,'width='+(popup_width?popup_width:'750px')+(popup_height?',height='+popup_height:'')+',left=200px,top=150px,resize='+(popup_resize?popup_resize:'1')+',scrolling='+(popup_scrolling?popup_scrolling:'1')); 
		if ((class_name && row_lock_id) || onclose_func) 
			popup_windows[popup_id].onclose = function(){if(class_name && row_lock_id){unlock_row(class_name,row_lock_id,onclose_func)}if(onclose_func){eval(onclose_func)}}
		else {
			if (onclose_func)
				popup_windows[popup_id].onclose = function(){eval(onclose_func)}
			
		}
		if (focus_to_field) 
			focus_to(focus_to_field);
	}
}

function write_content(xml_content,type) {
	for (var i = 0; i < xml_content.childNodes.length; i++) {
		if (type == 'html' && $(xml_content.childNodes[i].nodeName)) {
			if (!xml_content.childNodes[i].childNodes[0])
				$(xml_content.childNodes[i].nodeName).innerHTML='';
			else
				$(xml_content.childNodes[i].nodeName).innerHTML=xml_content.childNodes[i].childNodes[0].nodeValue;
		} else if (type == 'html_append' && $(xml_content.childNodes[i].nodeName) && xml_content.childNodes[i].childNodes[0]) 
			$(xml_content.childNodes[i].nodeName).innerHTML+=xml_content.childNodes[i].childNodes[0].nodeValue;
		else if (type == 'value' && $(xml_content.childNodes[i].nodeName)) {
			if (!xml_content.childNodes[i].childNodes[0])
				$(xml_content.childNodes[i].nodeName).value='';
			else
				$(xml_content.childNodes[i].nodeName).value=xml_content.childNodes[i].childNodes[0].nodeValue;
		} else if (type == 'value_append' && $(xml_content.childNodes[i].nodeName) && xml_content.childNodes[i].childNodes[0])
			$(xml_content.childNodes[i].nodeName).value+=xml_content.childNodes[i].childNodes[0].nodeValue;
		else if (type == 'jscript') {
			if (xml_content.childNodes[i].nodeValue != null) {
				//alert(xml_content.childNodes[i].nodeValue);
				eval(xml_content.childNodes[i].nodeValue);
			} else {
				//alert('2: '+xml_content.childNodes[i].childNodes[0].nodeValue);
				eval(xml_content.childNodes[i].childNodes[0].nodeValue);
			}
		}
	}
}
