var current_shown_day_stats = '';
var saved_show_id = '';

function regform_toogle_company() {
	if($$('regtype').selectedIndex == 1) {
		$$('data_corporate').style.display = 'none';
		$$('data_noncorporate').style.display = 'table';
	}else{
		$$('data_corporate').style.display = 'table';
		$$('data_noncorporate').style.display = 'none';
	}
}

function hideshow_hour_with_no_events() {
	if($$('dayview')) {
		day_nodes = getElementsByClassName('hour_with_no_events');

		for(var dn = 0;dn < day_nodes.length; dn++) {
			day_nodes[dn].style.display = day_nodes[dn].style.display == 'none' ? 'block' : 'none';
		}
	}

	if(day_nodes[dn-1].style.display == 'block') {
		$$('hideshow_link').innerHTML = 'скрии незаетите часове';
	}else{
		$$('hideshow_link').innerHTML = 'покажи незаетите часове';
	}
}

function show_day_stats(id) {
	if($(id).is(":hidden")) {
		if(current_shown_day_stats != '') $(current_shown_day_stats).hide();
		$(id).slideDown("normal");
		current_shown_day_stats = id;
	}else{
		$(id).hide();
		current_shown_day_stats = '';

	}
}

function load_small_cal(cal_date) {
	ajax_request("user.php?page=event_smallcalendar&month="+cal_date, 1,
			function () {
				try {
					if(ajax_object && ajax_object.readyState == 4 && ajax_object.status == 200) {
						html = ajax_object.responseText;
						cal = document.getElementById("calendar_holder");
						if(cal) cal.innerHTML = html;

					}
				}catch(e) {
				}
			}
	    );
	/*
	$.get("user.php", { page: "event_smallcalendar", month: cal_date },
			function (data) {
				$("#calendar").html(data);
			}
	     );
	     */
}

function open_addevent_box(cal_date, hour, date_formatted) {
	$("#time").attr("value", hour);
	//$("#event_box").title = __("Add event for ") + cal_date;
	$("#event_box_hidden_date").attr("value", cal_date);
	$("#event_box").data("title.dialog", __("Add event for ") + date_formatted);
	$("#event_box").dialog("open");
}

function open_editevent_box(name, cal_date, date_formatted, hour, smsgroupID, message, eventID) {
	$("#event_name").attr("value", name);
	//$("#event_box").title = __("Add event for ") + cal_date;
	$("#event_box").data("title.dialog", __("Edit event for ") + date_formatted);
	$("#event_box_hidden_date").attr("value", cal_date);
	$("#event_box_hidden_eventID").attr("value", eventID);
	$("#time").attr("value", hour);
	$("#message_text").val(message);
	$("#groupselect option").each(function(i){
		if(this.value == smsgroupID) this.selected = true;
		//if(this.value == smsgroupID) this.attr("selected", "selected");
	});
	$("#event_box").dialog("open");
}

function hide(hide_id) {
	var hide = document.getElementById(hide_id);

	if(hide) hide.style.display = 'none';
}

function show(show_id) {
	var show = document.getElementById(show_id);

	if(show) show.style.display = 'block';
}

function hide_show(hide_id, show_id) {
	hide(hide_id);
	show(show_id);
}

function $$(id) {
	var el = document.getElementById(id);

	if(el) return el;

	return false;
}

function scroll(id, pos) {
	$$(id).scrollTop = pos;
}

function check_saved_show_id() {
	var ret = false;
	if(saved_show_id.length>0) {
		show(saved_show_id);
		ret = true;
	}
	saved_show_id = '';

	return ret;
}

function show_sendsms_form(contact_id, contact_name) {
	$$('sendsms_form_contactID').value = contact_id;
	$$('sendsms_form_contact_name').innerHTML = contact_name;
	hide_tab(current_tab_id);
	show('smsgroup_sendsms_form');
}

function load_contact(contact_id, contact_group_id, contact_name, contact_company, contact_phone, contact_notes) {
	var contact_id_input = document.getElementById('contact_id');
	var contact_name_input = document.getElementById('contact_name');
	var contact_company_input = document.getElementById('contact_company');
	var contact_phone_input = document.getElementById('contact_phone');
	var contact_notes_input = document.getElementById('contact_notes');
	var contact_title = document.getElementById('contact_title');
	var contact_group_select = document.getElementById('contact_group');

	if(contact_group_select) {
		var p = 0;
		while(p < contact_group_select.options.length) {
			if(contact_group_select.options[p].value == contact_group_id)
				contact_group_select.options.selectedIndex = p;
			p += 1;
		}
	}

	if(contact_id_input) contact_id_input.value = contact_id;
	if(contact_name_input) contact_name_input.value = contact_name;
	if(contact_company_input) contact_company_input.value = contact_company;

	if(contact_phone_input) contact_phone_input.value = contact_phone;
	if(contact_notes_input) contact_notes_input.value = contact_notes;

	if(contact_title) contact_title.innerHTML = '<img src="images/edit.png" style="margin-right:5px" align="absmiddle"/>Редактиране на контакт';

	hide_show('contact_list', 'contact_edit_form');
}

function smstext_counter(textarea, counter_id, smscounter_id) {
	var characters = textarea.value.length;
	var messages = 0;

	if (/^[\040-\176]*$/.test(textarea.value)) {
		// ASCII
		if (characters == 0)
			messages = 0;
		else if (characters <= 160)
			messages = 1;
		else
			messages = Math.ceil(characters / 153);
	} else {
		// UTF-8
		if (characters == 0)
			messages = 0;
		else if (characters <= 70)
			messages = 1;
		else
			messages = Math.ceil(characters / 66);
	}

	$$(counter_id).innerHTML = characters;
	$$(smscounter_id).innerHTML = messages;
}

//function smstext_counter(textarea, counter_id) {
	//var chars_left = 160 - textarea.value.length;

	//if(chars_left >= 0) {
		//$$(counter_id).innerHTML = chars_left;
	//}else{
		//textarea.value = textarea.value.substring(0, 160);
		//return false;
	//}
//}

function eventform_check_tofields(field) {
	if(field == 'contact') {
		if($$('eventform_contactID').selectedIndex != 0) {
			$$('eventform_smsgroupID').selectedIndex = 0;
			$$('eventform_smsgroupID').disabled = true;
			$$('eventform_tonum').value = '';
			$$('eventform_tonum').disabled = true;
		}else{
			$$('eventform_smsgroupID').disabled = false;
			$$('eventform_tonum').disabled = false;
		}
	}

	if(field == 'smsgroup') {
		if($$('eventform_smsgroupID').selectedIndex != 0) {
			$$('eventform_contactID').selectedIndex = 0;
			$$('eventform_contactID').disabled = true;
			$$('eventform_tonum').value = '';
			$$('eventform_tonum').disabled = true;
		}else{
			$$('eventform_contactID').disabled = false;
			$$('eventform_tonum').disabled = false;
		}
	}

	if(field == 'tonum') {
		if($$('eventform_tonum').value != '') {
			$$('eventform_smsgroupID').selectedIndex = 0;
			$$('eventform_smsgroupID').disabled = true;
			$$('eventform_contactID').selectedIndex = 0;
			$$('eventform_contactID').disabled = true;
		}else{
			$$('eventform_smsgroupID').disabled = false;
			$$('eventform_contactID').disabled = false;
		}
	}
}

function clear_event_userID(userID) {
	$$('eventform_userID').value = userID;
	clear_event();
}

function clear_event(date, hour) {
	$$('eventform_eventID').value = 0;
	$$('eventform_name').value = '';
	$$('eventform_date').value = date;

	//$$('eventform_hour').options.selectedIndex = 36;
	var p = 0;
	while(p < $$('eventform_hour').options.length) {
		if($$('eventform_hour').options[p].value == hour) {
			$$('eventform_hour').options.selectedIndex = p;
			break;
		}
		p += 1;
	}
	$$('eventform_contactID').options.selectedIndex = 0;
	$$('eventform_smsgroupID').options.selectedIndex = 0;
	$$('eventform_tonum').value = '';
	$$('eventform_message').innerHTML = '';

	$$('eventform_sendsms').checked = true;
	$$('eventform_title').innerHTML = '<img src="images/16x16/calendar.png" border="0" style="margin:5px" align="absmiddle"/>Добавяне на събитие';
	show('user_startpage_event_form');
}

function load_event(event_id, event_name, event_date, event_hour, event_contact_id, event_smsgroup_id,
		event_tonum, event_message, event_sendsms) {
	$$('eventform_eventID').value = event_id;
	$$('eventform_name').value = event_name;
	$$('eventform_date').value = event_date;

	if($$('eventform_hour')) {
		var p = 0;
		while(p < $$('eventform_hour').options.length) {
			if($$('eventform_hour').options[p].value == event_hour) {
				$$('eventform_hour').options.selectedIndex = p;
				break;
			}
			p += 1;
		}
	}

	if($$('eventform_contactID')) {
		var p = 0;
		while(p < $$('eventform_contactID').options.length) {
			if($$('eventform_contactID').options[p].value == event_contact_id) {
				$$('eventform_contactID').options.selectedIndex = p;
				break;
			}
			p += 1;
		}
	}

	if($$('eventform_smsgroupID')) {
		var p = 0;
		while(p < $$('eventform_smsgroupID').options.length) {
			if($$('eventform_smsgroupID').options[p].value == event_smsgroup_id) {
				$$('eventform_smsgroupID').options.selectedIndex = p;
				break;
			}
			p += 1;
		}
	}

	// hour
	// event_contact_id
	// event_smsgroup_id
	$$('eventform_tonum').value = event_tonum;
	$$('eventform_message').innerHTML = event_message;

	if(event_sendsms == 1) {
		$$('eventform_sendsms').checked = true;
	}else{
		$$('eventform_sendsms').checked = false;
	}

	$$('eventform_title').innerHTML = '<img src="images/16x16/calendar.png" border="0" style="margin:5px" align="absmiddle"/>Редактиране на събитие';

	show('user_startpage_event_form');
}

function smsgroup_new_contact() {
	$$('contact_id').value = 0;
	$$('contact_name').value = '';
	$$('contact_phone').value = '';
	$$('contact_company').value = '';
	$$('contact_notes').value = '';
	$$('contact_title').innerHTML = '<img src="images/addgroup.png" style="margin-right:5px" align="absmiddle"/>Добавяне на контакт към тази група';
	hide_show('contact_list', 'smsgroup_contact_form');
}

function smsgroup_edit_contact(contact_id, contact_name, contact_company, contact_phone, contact_notes) {
	$$('contact_id').value = contact_id;
	$$('contact_name').value = decodeURI(contact_name);
	$$('contact_phone').value = contact_phone;
	$$('contact_company').value = contact_company;
	$$('contact_notes').value = contact_notes;
	$$('contact_title').innerHTML = '<img src="images/edit.png" style="margin-right:5px" align="absmiddle"/>Редактиране на контакт';

	hide_show('contact_list', 'smsgroup_contact_form');
}

function smsgroup_sendsms() {
	hide('contact_list');
	hide('contact_add');
	hide('smsgroup_sendsms_form_contact');
	show('smsgroup_sendsms_form');
}

function smsgroup_sendsms_contact(contactID, name) {
	hide('contact_list');
	hide('contact_add');
	hide('smsgroup_sendsms_form_contact');

	$$('smsgroup_sendsms_form_contact_id').value = contactID;
	$$('smsgroup_sendsms_form_contact_title').innerHTML = name;
	show('smsgroup_sendsms_form_contact');
}

function startpage_sendsms_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_sendsms_form');
}

function startpage_sendsms_group_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_sendsms_group_form');
}

function startpage_sendsms_contact_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_sendsms_contact_form');
}

function startpage_event_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_event_form');
}

function startpage_event_import_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_event_import_form');
}

function startpage_addcontact_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_addcontact_form');
}

function startpage_searchcontact_box() {
	hide('user_startpage_sendsms_form');
	hide('user_startpage_sendsms_contact_form');
	hide('user_startpage_sendsms_group_form');
	hide('user_startpage_event_form');
	hide('user_startpage_event_import_form');
	hide('user_startpage_addcontact_form');
	hide('user_startpage_searchcontact_form');

	show('user_startpage_searchcontact_form');
}

