// šđčćž

// parameters
var a_config = Array();

var handleReturn = {
	success: handleSuccess,
	failure: handleFailure
};

function getNodeValue(node) {
	if(node.childNodes.length > 0) {
		sibl = node.childNodes.item(0);
		value = sibl.data;
	} else {
		value = '';
	}
	return value;
}

function parseXML(xmlObj, a_conf) {
	outputObj = get_element_by_id(a_conf['output_id']);
	i = 0;
	j = 0;
	var xmlRoot = xmlObj.childNodes.item(0);
	for(i = 0; i < xmlRoot.childNodes.length; i++) {
		xmlNode = xmlRoot.childNodes.item(i);
		/*
			xml must return data in this order:
				- 0: id
				- 1: name
				- 2: selected - optional, default 0
		*/
		idValue = getNodeValue(xmlNode.childNodes.item(0));
		valValue = getNodeValue(xmlNode.childNodes.item(1));
		if(xmlNode.childNodes.length > 2) {
			selValue = getNodeValue(xmlNode.childNodes.item(2));
		} else {
			selValue = 0;
		}
		outputObj.options[i] = new Option(valValue, idValue);
		if(selValue == 1) {
			j = i;
		}
	}
	outputObj.length = i;
	outputObj.selectedIndex = j;
}

var handleSuccess = function(o) {
	if(a_config[o.argument[0]]['debug']) {
		alert(o.responseText);
	}
	if(a_config[o.argument[0]]['loading_id']) {
		var loadingObj = get_element_by_id(a_config[o.argument[0]]['loading_id']);
		loadingObj.style.display = 'none';
	}
	if(a_config[o.argument[0]]['output_id']) {
		var outputObj = get_element_by_id(a_config[o.argument[0]]['output_id']);
		if(a_config[o.argument[0]]['show_output'] == 1) {
			outputObj.style.display = 'block';
		}
		switch(a_config[o.argument[0]]['return_type']) {
			case 1: // select (xml data)
				if(a_config[o.argument[0]]['parseXML']) {
					a_config[o.argument[0]]['parseXML'](o.responseXML, a_config[o.argument[0]]);
				} else {
					parseXML(o.responseXML, a_config[o.argument[0]]);
				}
				break;
			default: // HTML
				if(a_config[o.argument[0]]['append'] == 1) {
					outputObj.innerHTML = o.responseText + outputObj.innerHTML;
				} else if(a_config[o.argument[0]]['append'] == 2) {
					outputObj.innerHTML+= o.responseText;
				} else {
					outputObj.innerHTML = o.responseText;
				}
				break;
		}
	}
	if(a_config[o.argument[0]]['call_function_on_success']) {
		a_config[o.argument[0]]['call_function_on_success'](o);
	}
}

var handleFailure = function(o) {
	if(a_config[o.argument[0]]['error_message']) {
		alert(a_config[o.argument[0]]['error_message']);
	} else {
		alert(o.statusText);
	}
}

var handleUpload = function(o) {
	handleSuccess(o);
}

/**
	 * call script trough ajax
	 *
	 * @param string config_id: under what id will config be saved in an array a_config
	 * @param array config: current config for calling script
	 * 
	 * config description
	 * output_id: object id that receives result
	 * loading_id: loading id to display when waiting for data
	 * script_url: url of the calling script
	 * conn_type: get or post
	 * post_data: what to post to script - applies to post transaction
	 * return_type: define return type: html(0), xml(1)
	 * append: how to display data: replace(0), append at the beginning (1), append at the end (2) - only for html responce
	 * error_message: what to display if error occures
	 * call_function_on_success: js function to call on success
	 * parseXML: js function to call if parsing xml data
	 * debug: set to true to display return data from script as alert
	 */
function callScript(config_id, config) {
	a_config[config_id] = Array();
	a_config[config_id]['output_id'] = config['output_id'];
	a_config[config_id]['loading_id'] = config['loading_id'];
	a_config[config_id]['script_url'] = config['script_url'];
	a_config[config_id]['return_type'] = config['return_type'];
	a_config[config_id]['append'] = config['append'];
	a_config[config_id]['error_message'] = config['error_message'];
	a_config[config_id]['call_function_on_success'] = config['call_function_on_success'];
	a_config[config_id]['parseXML'] = config['parseXML'];
	a_config[config_id]['debug'] = config['debug'];
	a_config[config_id]['show_output'] = config['show_output'];
	a_config[config_id]['form_id'] = config['form_id'];
	if(!a_config[config_id]['show_output']) {
		a_config[config_id]['show_output'] = 1;
	}
	var handleReturn = {
		success: handleSuccess,
		failure: handleFailure,
		upload: handleUpload,
		argument: [config_id]
	}
	if(config['loading_id']) {
		var loadingObj = get_element_by_id(config['loading_id']);
		loadingObj.style.display = 'block';
	}
	if(config['script_url'] && config['conn_type']) {
		if(config['form_id']) {
			var form_obj = get_element_by_id(config['form_id']);
			YAHOO.util.Connect.setForm(form_obj);
		}
		var request = YAHOO.util.Connect.asyncRequest(config['conn_type'], config['script_url'], handleReturn, config['post_data']);
	} else {
		alert("No script url or connection type defined!");
	}
}