

// HTTP

function http_load(url) {
	var http;
	// Mozilla
	if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
		http.open("GET", url, false);
		http.send(null);
	}
	// IE
	else if (window.ActiveXObject) {
		http = new ActiveXObject("Microsoft.XMLHTTP");
		http.open("GET", url, false);
		http.send();
	}
	if (http.status == 200) {
		return http.responseText;
	}
	else {
		return '';
	}
}


// Date editor

function pad0(s, n) {
	s = s.toString();
	while (s.length < n)
		s = '0' + s;
	return s;
}

function is_decimal(n) {
	n = n.toString();
	for (var i = 0; i < n.length; i++) {
		var c = n.charCodeAt(i);
		if (c < 48 || c > 57)
			return false;
	}
	return true;
}

function is_date(s) {
	var t = s.split('-');
	return t.length == 3 && is_decimal(t[0]) && is_decimal(t[1]) && is_decimal(t[2])
		&& t[0] > 1900 && t[1] >= 1 && t[1] <= 12 && t[2] >= 1 && t[2] <= 31;
}

function str_to_date(s) {
	var t = s.split('-');
	return new Date(t[0], t[1] - 1, t[2]);
}

function date_to_str(d) {
	return pad0(d.getFullYear(), 4) + '-' + pad0(d.getMonth() + 1, 2) + '-' + pad0(d.getDate(), 2);
}

function date_obj_shift(d, days)
{
	if (days != 0)
		d = new Date(d.getFullYear(), d.getMonth(), d.getDate() + days);
	return date_to_str(d);
}

function day_shift(s, days) {
	var d;
	if (s == '') {
		d = new Date();
		days = 0;
	} else {
		if (!is_date(s))
			return '';
		d = str_to_date(s);
	}
	return date_obj_shift(d, days);
}

function date_edit_change(o)
{
	if (!is_date(o.value))
		o.style.color = '#000000';
	else {
		var d = str_to_date(o.value);
		var dow = d.getDay();
		o.style.color = (dow == 0 || dow == 6) ? '#DD0000' : '#000000';
	}
}

function shift_date_field(id, days) {
	var o = find_obj(id);
	var s = day_shift(o.value, days);
	if (s == '')
		alert('Invalid date');
	else {
		o.value = s;
		date_edit_change(o);
	}
}

function clear_date_field(id) {
	var o = find_obj(id);
	o.value = '';
	date_edit_change(o);
}

function wdays_shift(d, days) {
	return http_load('/system/calendar-txt.php?fn=add_working_days&date=' + date_to_str(d) + '&add=' + days);
}

function date_edit_now_plus(value, date_editor) {
	var date_obj = find_obj(date_editor);
	var s = '';
	if (value != '')
		s = wdays_shift(new Date(), parseInt(value));
	date_obj.value = s;
	date_edit_change(date_obj);
}

function show_date_edit(id, value, table_start, table_end) {
	var d = document;
	value = value.substr(0, 10);
	if (value == '0000-00-00' || value == '0' || !is_date(value))
		value = '';
	if (table_start)
		d.write('<table border=0 cellspacing=0 cellpadding=0 align=left><tr>');
	d.write('<td><input type=text name=' + id + ' value="' + value + '" size=10 maxlength=10 onChange="date_edit_change(this)"></td>');
	d.write('<td nowrap>&nbsp;<a href="javascript:shift_date_field(\'' + id + '\',-1);"><img src="/img/i-decrease.gif" class=icon></a></td>');
	d.write('<td nowrap>&nbsp;<a href="javascript:shift_date_field(\'' + id + '\',1);"><img src="/img/i-increase.gif" class=icon></a></td>');
	d.write('<td nowrap>&nbsp;<a href="javascript:clear_date_field(\'' + id + '\');"><img src="/img/i-clear.gif" class=icon></a></td>');
	if (table_end)
		d.write('</tr></table>');
	date_edit_change(find_obj(id));
}


// Misc. editors (role, user, contact)

function role_toggle_layer(id, builtin) {
	toggle_layer2(id + '.frm', '/system/roles-mru-frm.php?ret_obj=' + id + '&builtin=' + builtin);
}

function role_click(ret_obj, role_id, doc) {
	if (doc == null)
		doc = parent.document;
	find_obj(ret_obj, doc).value = (role_id == 0 ? '' : role_id);
	find_obj(ret_obj + '.info', doc).innerHTML = http_load('/system/role-info-html.php?id=' + role_id);
}


var _role_multi_editors = new Array();

function add_role_editor(id, init_num, builtin)
{
	var o = find_obj(id + '.edit');
	if (_role_multi_editors[id] == null)
		_role_multi_editors[id] = init_num - 1;
	var n = ++_role_multi_editors[id];
	if (n >= 100)
		alert('Too many selectors');
	else
		o.innerHTML += http_load('/system/role-edit-html.php?name=' + id + '_' + n + '_&builtin=' + builtin);
}

function user_toggle_layer(id) {
	toggle_layer2(id + '.frm', '/system/users-search-frm.php?ret_obj=' + id);
}

function user_click(ret_obj, user_id, doc) {
	if (doc == null)
		doc = parent.document;
	find_obj(ret_obj, doc).value = (user_id == 0 ? '' : user_id);
	find_obj(ret_obj + '.info', doc).innerHTML = http_load('/system/user-info-html.php?id=' + user_id);
}

var _user_multi_editors = new Array();

function add_user_editor(id, init_num)
{
	var o = find_obj(id + '.edit');
	if (_user_multi_editors[id] == null)
		_user_multi_editors[id] = init_num - 1;
	var n = ++_user_multi_editors[id];
	if (n >= 30)
		alert('Too many selectors');
	else
		o.innerHTML += http_load('/system/user-edit-html.php?name=' + id + '_' + n + '_');
}

function group_click(ret_obj, group_id, group_descr, doc) {
	if (doc == null)
		doc = parent.document;
	find_obj(ret_obj, doc).value = (group_id == 0 ? '' : group_id);
	find_obj(ret_obj + '.info', doc).innerHTML = group_descr;
}

function contact_click(ret_obj, contact_id, contact_descr, sender_code, doc) {
	if (doc == null)
		doc = parent.document;
	find_obj(ret_obj, doc).value = contact_id;
	find_obj(ret_obj + '.info', doc).innerHTML = contact_descr;
	var o = find_obj('sender_code', doc);
	if (o != null)
		o.value = sender_code;
}

var _contact_multi_editors = new Array();

function add_contact_editor(id, init_num, ddl)
{
	var o = find_obj(id + '.edit');
	if (_contact_multi_editors[id] == null)
		_contact_multi_editors[id] = init_num - 1;
	var n = ++_contact_multi_editors[id];
	if (n >= 100)
		alert('Too many selectors');
	else
		o.innerHTML += http_load('/system/contact-edit-html.php?name=' + id + '_' + n + '_' + '&ddl=' + ddl);
}

function suggest_reg_number(id, out)
{
	var o = find_obj(id);
	if (o != null)
		o.value = http_load('/system/suggest-reg-number.php?out=' + out);
}

function generate_rand_number(id)
{
	var o = find_obj(id);
	if (o != null)
		o.value = http_load('/system/generate-rand-number.php?');
}

function doc_click(ret_obj, id, value, doc)
{
	// WARNING: id is eaither of: in_docs.id or in_out_relations.id
	if (doc == null)
		doc = parent.document;
	find_obj(ret_obj, doc).value = id;
	find_obj(ret_obj + '.info', doc).innerHTML = value;
}

var _doc_multi_editors = new Array();

function add_doc_editor(id, init_num, out)
{
	var o = find_obj(id + '.edit');
	if (_doc_multi_editors[id] == null)
		_doc_multi_editors[id] = init_num - 1;
	var n = ++_doc_multi_editors[id];
	if (n >= 30)
		alert('Too many selectors');
	else
		o.innerHTML += http_load('/system/doc-edit-html.php?out=' + out + '&name=' + id + '_' + n + '_');
}


// Report forms

var _rep_multi_editors = new Array;

function inc_id_number(id) {
	if (_rep_multi_editors[id] == null)
		_rep_multi_editors[id] = -1;
	return id + '_' + (++_rep_multi_editors[id]) + '_';
}

function rep_add_text_field(id, max_length, value) { // WARNING: value should be htmlspecialchars()'ed
	var o = find_obj(id + '.edit');
	var length = max_length >= 16 ? 16 : 10; // max(10, min(15, max_length));
	if (!value) value = '';
	if (o.innerHTML != '') o.innerHTML += ' -or- ';
	o.innerHTML += '<input type=text name=' + inc_id_number(id) + ' size=' + length + ' maxlength=' + max_length + ' value="' + value + '">';
}

function menu_from_array(name, values, sel) {
	var s = '<select name=' + name + '>';
	for(var i=0; i < values.length; i++) {
		if (!values[i] && i > 0) continue;
		s += '<option value=' + i + (i == sel ? ' selected' : '') + '>' + values[i] + '</option>';
	}
	s += '</select>';
	return s;
}

function rep_add_menu_field(id, values, sel) {
	var o = find_obj(id + '.edit');
	if (o.innerHTML != '') o.innerHTML += ' -or- ';
	o.innerHTML += menu_from_array(inc_id_number(id), values, sel);
}

function check_all(prefix, flag) {
	var i;
	for (i = 0; i < document.forms.length; i++) {
		var j, f = document.forms[i];
		for (j = 0; j < f.length; j++) {
			var o = f.elements[j];
			if (o.name.substr(0, prefix.length) == prefix)
				o.checked = flag;
		}
	}
}

function uinfo(uid) {
	var w=window.open('/system/uinfo.php?id=' + uid ,'','width=550,height=320,scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,location=no,status=no');
}

function load_custom_select(table_name, select_name, default_value) {
	var div = find_obj(select_name + '.div');
	if (table_name == '')
		div.innerHTML = '<select name=' + select_name + '><option></option></select>';
	else {
		div.innerHTML = '<span class=hint>Loading...</span>';
		div.innerHTML = http_load('/system/custom-value-list-html.php?select_name=' + escape(select_name)
			+ '&table_name=' + escape(table_name) + '&selected=' + escape(default_value));
	}
}

function open_search_form(out,saved) {
	window.name = 'mulberry_search_result_html';
	var w=window.open('/docs/search-form.php?out='+out+'&saved='+saved,'mulberry_search','width=750,height=520,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}

/*
function open_fulltext_search_form(out) {
	window.name = 'mulberry_fulltext_search_result_html';
	var w=window.open('/docs/fulltext-search-form.php?out='+out,'mulberry_fulltext_search','width=750,height=520,scrollbars=yes,menubar=no,resizable=yes,toolbar=no,location=no,status=no');
}
*/

function open_docket_window(doc_id, out) {
	var w=window.open('/tmpl/docket.php?id=' + doc_id + '&out=' + out,'','scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,location=no,status=no');
}


