// RICHIEDE crai_default.js (addSlashes)
// cerco tutte le classi form_crai, form_crai_small
// e le manipolo con il layout crai


// messaggio

function formCraiCreateMessage(id) {
	return '<span id="' + id + '_form_crai_message" class="form_crai_message" style="display: none;"><span id="' + id + '_form_crai_message_content" class="form_crai_message_content"></span></span>';
}

function formCraiShowMessage(id) {
	error = $('#' + id).attr('error');
	message = $('#' + id).attr('message');
	if (error == 'true') {
		$('#' + id + '_form_crai_message_content').html(message);
		$('#' + id + '_form_crai_message').fadeIn();
	}
}

function formCraiHideMessage(id) {
	$('#' + id + '_form_crai_message').fadeOut();
}

// text

function formCraiTextClick(id) {
	$('#' + id + '_form_crai_text_label').hide();
	$('#' + id).show();
	$('#' + id).focus();
}

function formCraiTextBlur(id) {
	if ($('#' + id).val() == '') {
		$('#' + id + '_form_crai_text_label').show();
		$('#' + id).hide();
	}
	formCraiHideMessage(id)
}

function formCraiTextUpdate(id) {
	error = $('#' + id).attr('error');
	if (error == 'true') {
		$('#' + id + '_form_crai_text_left').addClass('form_crai_text_left_image_error');
		$('#' + id + '_form_crai_text_left').removeClass('form_crai_text_left_image_ok');
		$('#' + id + '_form_crai_text_container').addClass('form_crai_text_container_image_error');
		$('#' + id + '_form_crai_text_container').removeClass('form_crai_text_container_image_ok');
	} else {
		$('#' + id + '_form_crai_text_left').addClass('form_crai_text_left_image_ok');
		$('#' + id + '_form_crai_text_left').removeClass('form_crai_text_left_image_error');
		$('#' + id + '_form_crai_text_container').addClass('form_crai_text_container_image_ok');
		$('#' + id + '_form_crai_text_container').removeClass('form_crai_text_container_image_error');
	}
}

// select

function formCraiSelectUpdate(id) {
	value = $('#' + id).val();
	//alert(value);
	$('#' + id).children().each(function() {
		if ($(this).attr('value') == value) {
			$('#' + id + '_form_crai_select_value').html($(this).html());
		}
	});
	$('#' + id).change();
	$('#' + id).css('display', 'none');
}

function formCraiSelectClickOption(id, value) {
	$('#' + id).val(value);
	formCraiSelectUpdate(id);
	formCraiSelectCloseOptions(id);
}

function formCraiSelectOpenOptions(id) {
	// chiudo tutte le eventuali altre select aperte, altrimenti rischio di aprirne 2 contemporaneamente e una sopra l'altra ...
	$('.form_crai select,.form_crai_small select').each(function() {formCraiSelectCloseOptions($(this).attr('id'))});
	options = '';
	$('#' + id).children().each(function() {
		options += '<span class="form_crai_select_option" onmouseover="$(this).css(\'background\', \'#eee\')" onmouseout="$(this).css(\'background\', \'#fff\')" onclick="formCraiSelectClickOption(\'' + id + '\', \'' + addSlashes($(this).attr('value')) + '\')">' + $(this).html() + '</span>';
	});
	$('#' + id + '_options').html(options);
	$('#' + id + '_options').slideDown()
	formCraiShowMessage(id);
}

function formCraiSelectCloseOptions(id) {
	$('#' + id + '_options').slideUp();
	formCraiHideMessage(id);
}

var formCraiSelectLockedOptions = false;
function formCraiLockSelectOptions(lock) {
	formCraiSelectLockedOptions = lock;
}

// checkbox
function formCraiCheckboxClick(id) {
	if ($('#' + id).is(':checked')) {
		$('#' + id).attr('checked', false);
		$('#' + id + '_container').removeClass('form_crai_checkbox_checked');
		$('#' + id + '_container').addClass('form_crai_checkbox_unchecked');
	} else {
		$('#' + id).attr('checked', true);
		$('#' + id + '_container').removeClass('form_crai_checkbox_unchecked');
		$('#' + id + '_container').addClass('form_crai_checkbox_checked');
	}
	
}

function formCraiCheckboxCheck(id, checked) {
	if (!checked) {
		$('#' + id).attr('checked', false);
		$('#' + id + '_container').removeClass('form_crai_checkbox_checked');
		$('#' + id + '_container').addClass('form_crai_checkbox_unchecked');
	} else {
		$('#' + id).attr('checked', true);
		$('#' + id + '_container').removeClass('form_crai_checkbox_unchecked');
		$('#' + id + '_container').addClass('form_crai_checkbox_checked');
	}
}

$(document).ready( function() {

	var counter = 0; // contatore usato per differenziare gli id su elementi con lo stesso nome

	// text (attributi: length, error = true|false, message)
	$('.form_crai input[type="text"],.form_crai_small input[type="text"],.form_crai input[type="password"],.form_crai_small input[type="password"]').each(function() {
		counter++;
		
		var id = $(this).attr('id');
		var name = $(this).attr('name');
		var value = $(this).attr('value');
		var type = $(this).attr('type');
		
		var len = $(this).attr('len');
		var error = $(this).attr('error');
		var message = $(this).attr('message');
		var fieldLabel = $(this).attr('field-label');
		
		if (id == null || id == '') {
			id = name + '_' + counter
		}
		if (len == null || len == 0) {
			len = 150;
		}
		if (message == null) {
			message = '';
		}
		if (fieldLabel == null) {
			fieldLabel = '';
		}

		$(this).replaceWith(function() {
			text = '';
			text += '<span class="form_crai_container">';
				text += formCraiCreateMessage(id);
				text += '<span id="' + id + '_form_crai_text_left" class="form_crai_text_left">&nbsp;</span>';
				text += '<span id="' + id + '_form_crai_text_container" style="width:' + len +'px;" class="form_crai_text_container" onclick="formCraiTextClick(\'' + id + '\')">';
				text += '<span id="' + id + '_form_crai_text_label" style="display: ' + (value==''?'block':'none') + ';" class="form_crai_text_label">' + fieldLabel + '</span>'
				text += '<input style="display:' + (value!=''?'inline':'none') + ';" type="' + type + '" id="' + id + '" name="' + name + '" value="' + value + '" error="' + error + '" message="' + message + '" onfocus="formCraiShowMessage(\'' + id + '\')" onblur="formCraiTextBlur(\'' + id + '\')" />';
				text += '</span>';
			text += '</span>';
			return text;
		});
		formCraiTextUpdate(id);
	});

	// textarea
	$('.form_crai textarea,.form_crai_small textarea').each(function() {
		counter++;
		
		var id = $(this).attr('id');
		var name = $(this).attr('name');
				
		var fieldWidth = $(this).attr('field-width');
		var fieldHeight = $(this).attr('field-height');
		var error = $(this).attr('error');
		var message = $(this).attr('message');
		
		if (id == null || id == '') {
			id = name + '_' + counter
		}
		if (fieldWidth == null || fieldWidth == 0) {
			fieldWidth = 200;
		}
		if (fieldHeight == null || fieldHeight == 0) {
			fieldHeight = 100;
		}
		if (message == null) {
			message = '';
		}
		$(this).replaceWith(function() {
			text = '';
			text += '<span class="form_crai_container">';
				text += formCraiCreateMessage(id);
				text += '<span id="' + id + '_form_crai_textarea_top_left" class="form_crai_textarea_top_left">&nbsp;</span>';
				text += '<span id="' + id + '_form_crai_textarea_top" class="form_crai_textarea_top" style="width:' + fieldWidth + 'px;">&nbsp;</span>';
				text += '<span id="' + id + '_form_crai_textarea_top_right" class="form_crai_textarea_top_right">&nbsp;</span>';
				
				text += '<span id="' + id + '_form_crai_textarea_left" class="form_crai_textarea_left" style="height:' + fieldHeight + 'px;">&nbsp;</span>';
				text += '<span style="display: block; float: left;width:' + fieldWidth + 'px;"><textarea id="' + id + '" name="' + name + '" style="resize: none;width:' + (fieldWidth-4) + 'px;height:' + (fieldHeight-4) + 'px;" error="' + error + '" message="' + message + '" onfocus="formCraiShowMessage(\'' + id + '\')" onblur="formCraiHideMessage(\'' + id + '\')">' + $(this).html() + '</textarea></span>'
				text += '<span id="' + id + '_form_crai_textarea_right" class="form_crai_textarea_right" style="height:' + fieldHeight + 'px;">&nbsp;</span>';
				
				text += '<span id="' + id + '_form_crai_textarea_bottom_left" class="form_crai_textarea_bottom_left"></span>';
				text += '<span id="' + id + '_form_crai_textarea_bottom" class="form_crai_textarea_bottom" style="width:' + fieldWidth + 'px;"></span>';
				text += '<span id="' + id + '_form_crai_textarea_bottom_right" class="form_crai_textarea_bottom_right"></span>';
				
			text += '</span>';
			return text;
		});
		
		//formCraiTextUpdate(id);
	});
	
	// select (attributi: length, error, message)
	$('.form_crai select,.form_crai_small select').each(function() {
		counter++;
		
		var id = $(this).attr('id');
		var name = $(this).attr('name');
		
		var len = $(this).attr('len');
		var error = $(this).attr('error');
		var message = $(this).attr('message');
		
		var elementValue = $(this).attr('element-value');
		
		var eventChange = $(this).attr('event-change');
		
		if (id == null || id == '') {
			id = name + '_' + counter;
		}
		if (len == null || len == 0) {
			len = 150;
		}
		if (message == null) {
			message = '';
		}

		if (elementValue == null) {
			elementValue = '';
		}
		
		if (eventChange == undefined) {
			eventChange = '';
		}

		$(this).replaceWith(function() {
			select = '';
			select += '<span class="form_crai_container" onmouseover="formCraiLockSelectOptions(true);" onmouseout="formCraiLockSelectOptions(false);">';
				select += formCraiCreateMessage(id);
				select += '<span class="form_crai_select_left">&nbsp;</span>';
				select += '<span class="form_crai_select_container" style="width: ' + len + 'px;">'
					select += '<select style="display: none;" id="' + id + '" name="' + name + '" error="' + error + '" message="' + message + '" element-value="' + elementValue + '" onchange="' + eventChange + '">' + $(this).html() + '</select>';
					select += '<span id="' + id + '_form_crai_select_value" class="form_crai_select_value" onclick="formCraiSelectOpenOptions(\'' + id + '\')"></span>';
					select += '<span id="' + id + '_options" class="form_crai_select_options" style="display: none;"></span>';
				select += '</span>';
			select += '</span>';
			return select;
		});

		$('#' + id).val(elementValue);
		
		formCraiSelectUpdate(id);
		
		
	});
	
	// checkbox
	$('.form_crai input[type="checkbox"],.form_crai_small input[type="checkbox"]').each(function() {
	
		counter++;
		var id = $(this).attr('id');
		var name = $(this).attr('name');
		var value = $(this).attr('value');
		var checked = $(this).attr('checked');
		
		var eventClick = $(this).attr('event-click');
		
		if (id == null || id == '') {
			id = name + '_' + counter;
		}
		
		if (eventClick == undefined) {
			eventClick = '';
		}
		
		$(this).replaceWith(function() {
			out = '';
			out += '<span class="form_crai_container">';
				out += formCraiCreateMessage(id);
				out += '<span id="' + id + '_container" class="form_crai_checkbox_container ' + (checked?'form_crai_checkbox_checked':'form_crai_checkbox_unchecked') + '" onclick="formCraiCheckboxClick(\'' + id + '\');' + eventClick + '">';
				out += '<input style="display: none;" type="checkbox" id="' + id + '" name="' + name + '" value="' + value + '" ' + (checked?'checked="checked"':'') + ' />';
				out += '</span>';
			out += '</span>';
			return out;
		});
		
	});
	
	// button (attributi: color, )
	$('.form_crai input[type="button"],.form_crai_small input[type="button"],.form_crai input[type="submit"],.form_crai_small input[type="submit"]').replaceWith(function() {
		var name = $(this).attr('name');
		var value = $(this).attr('value');
		var type = $(this).attr('type');
		var fieldColor = $(this).attr('field-color');
		if (fieldColor != 'red' && fieldColor != 'green') {
			fieldColor = 'red';
		}
		out = '';
		out += '<span class="form_crai_container">';
		out += '<span class="form_crai_button_left form_crai_button_left_' + fieldColor + '">&nbsp;</span>';
		out += '<span class="form_crai_button_container form_crai_button_container_' + fieldColor + '"><input type="' + type + '" name="' + name + '" value="' + value + '" /></span>';
		out += '</span>';
		return out;
	});
	

	
	// body on click chiudo options della select
	$('body').click(function() {
		if (!formCraiSelectLockedOptions) {
			$('.form_crai select,.form_crai_small select').each(function() {formCraiSelectCloseOptions($(this).attr('id'))});
		}
	});
	
});
