// ======== lLt (fixes for lLib) ===============================================

// ==== instance :

function lLt () {
	return this;
}
lLt.instance = null;
lLt.getInstance = function () {
	if(!lLt.instance) {
		lLt.instance = new lLt();
	}
	return lLt.instance;
};

// ==== execRemoteJs, lLib.makeRequest fix :

lLt.prototype.requestStack = new Array();

lLt.prototype.execRemoteJs = function (url, handler, s_onRequestComplete, s_params) {
	if (typeof(s_onRequestComplete) != 'string') var s_onRequestComplete = "";
	if (typeof(s_params) != 'string') var s_params = "";

	var requestId = this.requestStack.length;
	this.requestStack[requestId] = handler;
	
	var s_delim = "?";
	if (url.indexOf('?') !== -1) s_delim = '&';

	url += s_delim + "requestId=" + requestId + "&rnd=" + Math.random();

	var v_ = $j.ajax({
		type: "GET",
		url: url,
		data: null,
		dataType: 'script',
		complete: function(xhr, status) {
			if (s_onRequestComplete && s_onRequestComplete.length) {
				eval(s_onRequestComplete+"('"+status+"'"+(s_params.length ? (", "+s_params) : "")+")");
			}
		}
	});
	if (v_ === undefined && s_onRequestComplete && s_onRequestComplete.length) {
		eval(s_onRequestComplete+"('"+'undefined'+"'"+(s_params.length ? (", "+s_params) : "")+")");
	}

	return requestId;
};

lLt.prototype.makeResponse = function (response) {
	if(typeof response != "object") {
		return false;
	}

	var requestHandler;
	var requestId = response.id;

	if(typeof this.requestStack[requestId] == "undefined") {
		return false;
	} else {
		requestHandler = this.requestStack[requestId];
		requestHandler(response);
		this.requestStack[requestId] = undefined;
		return true;
	}
};

// ==

lLib.prototype.makeRequest = function (url, handler) {
	return lLt.execRemoteJs(url, handler, "", "");
}
lLib.prototype.makeResponse = function (response) {
	return lLt.getInstance().makeResponse(response);
}

// ==== ajaxForm

lLt.prototype.ajaxForm = function (v_form, s_onRequestComplete, s_params) {
	if (typeof(v_form) == 'string') {
		v_form = document.forms[v_form];
	}
	if (typeof(s_onRequestComplete) != 'string') var s_onRequestComplete = "";
	if (typeof(s_params) != 'string') var s_params = "";

	var s_url = v_form.action;
	var s_method = v_form.method;
	var arr_els = v_form.elements;
	if (s_url.length && s_method.length) {
		var s_data = "";
		var i = 0;
		for (i=0; i<arr_els.length; i++) {
			var el = arr_els[i];
			var s_name = el.name;
			if (!s_name) s_name = el.id;
			if (s_name) {
				var s_tag = el.tagName;
				if (s_tag.length) s_tag = s_tag.toUpperCase();
				var s_type = el.type;
				if (s_type.length) s_type = s_type.toUpperCase();

				s_name = escape(s_name);

				if (s_tag == 'SELECT' && el.options && el.options.length) {
					var arr_options = el.options;
					if (el.multiple) {
						var j = 0;
						for (j = 0; j < arr_options.length; j++) {
							var the_option = arr_options[j];
							if (the_option.selected) {
								s_data += ((s_data.length ? '&' : '') + s_name + '=' + escape(the_option.value ? the_option.value : the_option.innerHTML));
							}
						}
					} else if (el.selectedIndex > -1 && el.selectedIndex < arr_options.length) {
						var the_option = arr_options[el.selectedIndex];
						s_data += ((s_data.length ? '&' : '') + s_name + '=' + escape(the_option.value ? the_option.value : the_option.innerHTML));
					}
				} else if (s_type == 'CHECKBOX' && el.checked) {
					s_data += ((s_data.length ? '&' : '') + s_name + '=' + escape(el.value ? el.value : "ON"));
				} else if (s_type == 'RADIO' && el.checked) {
					s_data += ((s_data.length ? '&' : '') + s_name + '=' + escape(el.value));
				} else if (s_type == 'TEXT' || s_type == 'HIDDEN' || s_type == 'BUTTON' || s_tag == 'TEXTAREA') {
					s_data += ((s_data.length ? '&' : '') + s_name + '=' + escape(el.value));
				}
			}
		}
		//
		var v_ = $j.ajax({
			type: s_method,
			url: s_url,
			data: (s_data.length ? s_data : null),
			dataType: 'xml',
			complete: function(xhr, status) {
				if (s_onRequestComplete && s_onRequestComplete.length) {
					eval(s_onRequestComplete+"(xhr, '"+status+"'"+(s_params.length ? (", "+s_params) : "")+")");
				}
			}
		});
		if (v_ === undefined && s_onRequestComplete && s_onRequestComplete.length) {
			eval(s_onRequestComplete+"('"+'undefined'+"'"+(s_params.length ? (", "+s_params) : "")+")");
		}
	} else {
		if (s_onRequestComplete && s_onRequestComplete.length) {
			eval(s_onRequestComplete+"('"+'invalidform'+"'"+(s_params.length ? (", "+s_params) : "")+")");
		}
	}
}