/*************************************
* Forms                              *
**************************************
*                                    *
* Author: Tim Jans                   *
*                                    *
**************************************/

$(document).ready(function() {
	
	prepare_form_inputs();

	

});

function prepare_form_inputs() {
	/* on hover & on blur class change */

	if ($('.styledForm')) {
		
		$('.styledForm .txtInput').focus(function() { $(this).addClass('high'); });
		$('.styledForm .txtInput').blur(function() { $(this).removeClass('high'); });

	}

}

function prepare_form_tooltip() {

	var t = -5;
	var l = 10;

	$("#fauxTT").each(function() {$(this).remove();});
	
	$('.hint .formHintIcon').tooltip({
		bodyHandler: function() { 
	        return $($(this).next()).html(); 
	    }, 
	    showURL: false,
		opacity: 0.85,
		extraClass: "ttHint",
		top: t,
		left: l
	});
	
	$('.err .formHintIcon').tooltip({
		bodyHandler: function() { 
	        return $($(this).next()).html(); 
	    }, 
	    showURL: false,
		opacity: 0.85,
		extraClass: "ttErr",
		top: t,
		left: l
	});

	if($('li.err').length > 0) {

		//pos = $('li.err:first .formHintIcon').position();
		pos = $('li.err:first .formHintIcon').offset();
		h = $('li.err:first .formHintIcon').height();
		w = $('li.err:first .formHintIcon').width();
	
		fauxTT = $('<div id="fauxTT" class="ttErr"><div class="body">' + $('li.err:first .formHint').html() + '</div></div>')
		//.css({top: (pos.top + 58), left: (pos.left + l + w + 130)})
		.css({top: (pos.top), left: (pos.left + l + w )})
		.appendTo(document.body)
		.show();
		
		$(".formHintIcon").hover(function(){
			$('#fauxTT').remove();
		},function(){
			//$('#fauxTT').show();
		});
	}

}



/************************

Select box manipulation

*************************/

function prepare_null_option_selects() {
	
	$(".removeNullOption").each(function(i) {
		remove_null_option($(this));
		$(this).blur(function() {remove_null_option($(this))});
	});
	
	
	
}

function remove_null_option(obj) {
	
	var value = '';

	

	if (obj.val() != 0) {
	
		obj.children().each(function(i) {
			//alert('remove null');
				
			value = value + $(this).val() + ' --- ';
			if ($(this).val() == 0) {
				$(this).remove();
			}
			if ($(this).val() == "0") {
				$(this).remove();
			}
			
			
			
		});	
	}
	
	//	alert(value);
}

function clear_options(obj, defaultText, defaultValue) {
	
	if (typeof(defaultValue) == "undefined") {		
		defaultValue = 0;		
	} 
	
	var elm = obj[0];
	var len = elm.options.length;
	
	for (var i = 0; i < len; ++i) {
   		elm.remove(1);
	}
	
	elm.options[0] = new Option(defaultText, defaultValue, false);
	
	//obj.append('<option label="' +  defaulttext + '" value="' + defaultvalue + '">' + defaulttext + '</option>');

}

function add_option(obj, label, value) {

	obj.append('<option label="' +  label + '" value="' + value + '">' + label + '</option>');
}

function set_options_from_xml(objSelect, data, defaultText, defaultValue) {

//	objSelect.css('background', '#f00');


	var elm = objSelect[0];
	var len = elm.options.length;
	var countNew = $("count", data).text();
	var counter = 0;
	var jData = $( data );
	
	
	
	
	
	
	for (var i = 0; i < len; ++i) {
   		elm.remove(1);
	}
				
	if (countNew > 1) {
		elm.options[0] = new Option(defaultText, defaultValue);
		counter++;
	} 
	
	jData.find('options').children().each(function(i) {
		
		selected = ($(this).children('selected').text() == 'true' ? true : false);
		
		if (countNew == 1) {
			selected = true;
		}
		
		elm.options[counter] = new Option(($(this).children('label').text()), ($(this).children('value').text()) ,selected, selected);
		counter++;
	});
	
	if (countNew == 1) {
		objSelect.change();
		
		
	}
	
	//objSelect.css('background', '#000');
} 


