/***************************************************************************
 *   Copyright (C) 2008 by Alexey Denisov                                  *
 *   adenisov@fjproject.ru                                                 *
 ***************************************************************************/
/* $Id: serializer.js 11 2008-11-19 09:19:32Z stalkerxey $ */
function serializeFormObject(object)
{
	return $(object).map(function(){
			return jQuery.nodeName(this, "form") ?
				jQuery.makeArray(this.elements) : this;
		})
		.filter(function(){
			return this.name && !this.disabled &&
				(this.checked || /select|textarea/i.test(this.nodeName) ||
					/text|hidden|password/i.test(this.type));
		})
		.map(function(i, elem){
			var val = jQuery(this).val();
			return val == null ? null :
				val.constructor == Array ?
					jQuery.map( val, function(val, i){
						return {id: elem.id, value: val};
					}) :
					{id: elem.id, value: val};
		}).get();
}

function getObjectById(paramsObject, id)
{
	var newobj = null;
	$.each(paramsObject, function(i, obj){
		var oid = obj['id'];
		if (typeof oid != 'undefined' && oid.length > 0 && oid == id) {
			newobj = obj;
		}
	});
	return newobj;
}

function getObjectsByIds(paramsObject, idsArray)
{
	var array = {};
	$.each(idsArray, function(i, id){
		var obj = null;
		if (obj = getObjectById(paramsObject, id)) {
			array[obj.id] = obj;
		}
	})
	return array;
}

function setupObjects(paramsObject, idsArray)
{
	$.each(paramsObject, function(k, obj){
		$.each(idsArray, function(i, id){
			var oid = obj['id'];
			if (typeof oid != 'undefined' && oid.length > 0 && oid == id) {
				var val = obj['value'];
				setupFormObject(oid, val);
			}
		});
	});
	return;
}

function setupObjectsByIdPart(paramsObject, reg)
{
	$.each(paramsObject, function(i, obj){
		var id = obj['id'];
		var val = obj['value'];

		if (typeof id != 'undefined' && id.length > 0 && reg.test(id)) {
			setupFormObject(id, val);
		}
	});
	return;
}

function setupFormObject(id, val)
{
	var obj = $('#' + id);
	if (obj.length == 0)
		return;
	if (typeof val == 'undefined') {
		obj.val('');
	} else if (obj.attr('type') == 'checkbox') {
		obj.val(val).attr('checked', true);
	} else {
		obj.val(val);
	}
	return;
}