function newxmlhttp () {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	return xmlhttp;
}

function form_to_params (form_id) {
	var params = new Array();
	
	var form = document.getElementById(form_id);
	if( form != null ){
		var elem = form.elements;
		for(var i = 0; i < elem.length; i++){
			//alert(elem[i].name + '(' + elem[i].type + ') = ' + elem[i].value);
			if( elem[i].type == 'radio' ){
				if( elem[i].checked == true ){
					params.push(elem[i].name + "=" + encodeURIComponent(elem[i].value));
				}
			}
			else if( elem[i].type == 'checkbox' ){
				if( elem[i].checked == true ){
					//alert(elem[i].name + " is checked");
					params.push(elem[i].name + "=1");
				}
				else {
					params.push(elem[i].name + "=0");
				}
			}
			else {
				params.push(elem[i].name + "=" + encodeURIComponent(elem[i].value));
			}
		}
	}
	else {
		throw('form_to_params: form not found');
	}
	return params.join("&");
}

function request_to_element (params, eid, focus_eid, callback) {
	params += '&__xmlhttp=element';
	var xmlhttp = newxmlhttp();
	xmlhttp.open("POST", "?", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200){
				var element = document.getElementById(eid);
				if( element != null ){
					element.innerHTML = xmlhttp.responseText;
					if( callback != null ){
						callback();
					}
					if( focus_eid != null ){
						set_focus_by_id(focus_eid);
					}
				}
				else {
					throw('request_to_element: element not found \'' + eid + '\'');
				}
			}
			else {
				alert('Something went wrong. Your recent actions may not have been saved. (HTTP status ' + xmlhttp.status + ')');
			}
		}
	}
	xmlhttp.send(params);
}

function request_to_eval (params) {
	params += '&__xmlhttp=eval';
	var xmlhttp = newxmlhttp();
	xmlhttp.open("POST", "?", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==200){
				try {
					eval(xmlhttp.responseText);
				} catch (e) {
					throw('request_to_eval: died: ' + xmlhttp.responseText + ' -- ' + e.description);
				}
			}
			else {
				alert('Something went wrong. Your recent actions may not have been saved. (HTTP status ' + xmlhttp.status + ')');
			}
		}
	}
	xmlhttp.send(params);
}

function form_to_element (form_id, eid, focus_eid, callback){
	request_to_element(form_to_params(form_id), eid, focus_eid, callback);
}

function form_to_eval (form_id, callback){
	request_to_eval(form_to_params(form_id));
}

function toggle_visibility (element_id){
	var element = document.getElementById(element_id);
	if( element != null ){
		if( element.style.display != 'block' ){
			element.style.display = 'block';
		}
		else {
			element.style.display = 'none';
		}
	}
}

function toggle_visibility_inline (element_id){
	var element = document.getElementById(element_id);
	if( element != null ){
		if( element.style.display != 'inline' ){
			element.style.display = 'inline';
		}
		else {
			element.style.display = 'none';
		}
	}
}

function set_listbox_selected_value(listbox, value){
	if( listbox != null ){
		for(i=0;i<listbox.length;i++){
			if(listbox.options[i].value==value){
				listbox.selectedIndex=i;
			}
		}
	}
}

function set_focus_by_id(eid){
	var element = document.getElementById(eid);
	if( element != null ){
		element.focus();
	}
}

function get_selected_text(){
	var txt = '';
	if (window.getSelection){
		txt = window.getSelection();
	}
	else if (document.getSelection){
		txt = document.getSelection();
	}
	else if (document.selection){
		txt = document.selection.createRange().text;
	}
	else {
		return null;
	}
	if( txt != null && txt != '' ){
		return txt;
	}
	else {
		return null;
	}
}

function set_date_today(field_name){
	var now = new Date();
	
	var day_field = field_name + '__day'; var day = document.getElementById(day_field); if( day != null ){ set_listbox_selected_value(day, now.getDate()); }
	var month_field = field_name + '__month'; var month = document.getElementById(month_field); if( month != null ){ month.selectedIndex = now.getMonth() + 1; }
	var year_field = field_name + '__year'; var year = document.getElementById(year_field); if( year != null ){ set_listbox_selected_value(year, now.getFullYear()); }
	var hour_field = field_name + '__hour'; var hour = document.getElementById(hour_field); if( hour != null ){ set_listbox_selected_value(hour, now.getHours()); }
	var minute_field = field_name + '__minute'; var minute = document.getElementById(minute_field); if( minute != null ){ set_listbox_selected_value(minute, now.getMinutes()); }
	var second_field = field_name + '__second'; var second = document.getElementById(second_field); if( second != null ){ set_listbox_selected_value(second, '00'); }
}

function set_date_clear(field_name){
	var day_field = field_name + '__day'; var day = document.getElementById(day_field); if( day != null ){ day.selectedIndex = 0; }
	var month_field = field_name + '__month'; var month = document.getElementById(month_field); if( month != null ){ month.selectedIndex = 0; }
	var year_field = field_name + '__year'; var year = document.getElementById(year_field); if( year != null ){ year.selectedIndex = 0; }
	var hour_field = field_name + '__hour'; var hour = document.getElementById(hour_field); if( hour != null ){ hour.selectedIndex = 0; }
	var minute_field = field_name + '__minute'; var minute = document.getElementById(minute_field); if( minute != null ){ minute.selectedIndex = 0; }
	var second_field = field_name + '__second'; var second = document.getElementById(second_field); if( second != null ){ second.selectedIndex = 0; }
}

function single_select_changed(select){
	var hidden_other_id = 'formfield_' + select.name + '_other';
	var hidden_other_element = document.getElementById(hidden_other_id);
	if( hidden_other_element != null ){
		if( select.value == 'SELECTED_OTHER' ){
			hidden_other_element.style.display = 'block';
			var textbox = hidden_other_element.getElementsByTagName('input');
			if( textbox != null ){
				textbox[0].focus();
			}
		}
		else {
			hidden_other_element.style.display = 'none';
		}
	}
}

function input_hint (input_id, hint){
	var input_jq = jQuery('#' + input_id);
	if( input_jq.val() == '' || input_jq.val() == hint ){
		_input_hint_set(input_jq, hint);
	}
	input_jq.focus(function(){
		if( input_jq.val() == hint ){
			_input_hint_set(input_jq);
		}
	});
	input_jq.blur(function(){
		if( input_jq.val() == '' ){
			_input_hint_set(input_jq, hint);
		}
	});
}

function _input_hint_set (input_jq, hint){
	if( hint != null ){
		input_jq.val(hint);
		if( input_jq.prop('type') == 'password' ){
			input_jq.prop('type', 'text');
			input_jq.addClass('_input-hint-is-password');
		}
		input_jq.addClass('input-hint');
	}
	else {
		input_jq.val('');
		if( input_jq.hasClass('_input-hint-is-password') ){
			input_jq.prop('type', 'password');
		}
		input_jq.removeClass('input-hint');
	}
}

(function(jQuery) {
	jQuery.fn.goTo = function() {
		var offset = jQuery(this).offset();
		if( offset != null ){
			jQuery('html, body').animate({
				scrollTop: (offset.top - 300) + 'px'
			}, 0);
		}
		return this; // for chaining...
	}
})(jQuery);

function form_trigger (element){
	jQuery('<input>').attr('type','hidden').attr('name', '_prelim-submit').attr('value', 1).appendTo(element.form);
	jQuery('<input>').attr('type','hidden').attr('name', '_prelim-trigger-field').attr('value', element.name).appendTo(element.form);
	jQuery('<input>').attr('type','hidden').attr('name', '_prelim-scrolltop').attr('value', jQuery(window).scrollTop()).appendTo(element.form);
	disable_form_input();
	element.form.submit();
}

function disable_form_input(tagname){
	if( tagname != null ){
		var elements = document.getElementsByTagName(tagname);
		for(var i=0;i<elements.length;i++){
			// setting 'disabled' seems to mess up the post data..
			elements[i].setAttribute('readonly',true);
		}
	}
	else {
		disable_form_input('input');
		//disable_form_input('select');
		//disable_form_input('textarea');
	}
}
