// JavaScript Document


//--------------------------------INSERT DATA------------------------------------------

function insertData(more) {
		ajax_json_call('/filebin/ajax/php/renewal_data.php', 'GET', "id="+more);
}


//------------------------------------------------JSON-------------------------------------------------------

function ajax_json_call(callUrl, callType, callVars, onCompleteFunc) {
	try {
		$.ajax({
			type: callType,
			url: callUrl,
			data: callVars,
			dataType: "json",
			success: function(data) {
				if (data) {
					process_success_ajax_json_call(data);
				} else {
					//error
				}
		 	},
			complete: function() {
				//if we need to run something discard if we get data or not
				if (eval("typeof " + onCompleteFunc + " == 'function'")) {
					eval(onCompleteFunc+'()');
				}
			}
		});
	} catch (e) {
		//nothing for now
	}
}
function process_success_ajax_json_call(data) {
	//var formElements = {formType:['input', 'type', 'radio', 'password', 'textarea', 'checkbox', 'select']}
	if (data) {
		var alert_content="";
		$.each(data, function(id, dataHolder) {
			if ($("#"+id) && id!="alert_msg") {
				//dataHolder is array (jAction,contentReturn,insertId)
				if (dataHolder.jAction.indexOf('insert')>-1) {
					//insertAfter, insertBefore
					$(dataHolder.jContentReturn)[dataHolder.jAction]('#'+dataHolder.jHelpId);
				} else {
					//replace/set new value
					$("#"+id)[dataHolder.jAction](dataHolder.jContentReturn);
				}
			}
			//display js alert
			if (id=="alert_msg") alert_content += content+"\n";
		});
		if (alert_content != "") alert(alert_content);
	}
	else {
		//process error here...
	}
}

