/*
 * Funzioni jquery per la classe: CZ_forms
 *
 */
$(document).ready(CZ_forms);

function CZ_forms() {

	/*
	 * Messagi di errore
	 *
	 */
	//$("div.error").effect("shake");
	
	$("input.cz_checkall").click(function() {

			var stato = this.checked;

			$(":checkbox").each(function() {

				if(stato==true) {
					this.checked = true;  
				} else {
					this.checked = false; 
				}

			});

		});
	

	$("form.czform").each(function() { 


		var formsid	 =	'#'+this.id;
		var formspath =	'#'+this.id;
		var changeform = false;


		/*
		 * imposta il form come modificato
		 *
		 */
		function setChange() {

			changeform = true;

			$("a").each(function() {

				//$(this).click(confirmLinkOut);

			});
		}

		/*
		 * richiesta di conferma se si esce dal form senza salvare
		 *
		 */
		function confirmLinkOut(e) {
			
			if(changeform==true) {
				var result = confirm("ATTENZIONE\n\n Il form e' stato modificato.\n\n Uscire senza salvare?");
				if (result) {
					return true;
				} else {
					return false;
				}
			}

		}
		

		/*
		 * colora gli elementi sul focus
		 *
		 */
		$(formspath).find("input, select, textarea").each(function() {

			$(this).focus(function() {
				$(this).addClass("inputon");
			});
			$(this).blur(function() { 
				$(this).removeClass("inputon");
			});

			$(this).change(setChange);

		});


		/*
		 * pulsanti per il translate
		 *
		 */
		$(formspath + " button[name='translate']").click(function() { 

			$("#dialog").html( '<iframe src="translate.php?module=home&controller=translate" width="100%" height="100%" frameborder="0"></iframe>' );

			$("#dialog").dialog({
				bgiframe: true,
				height: 600,
				width: 800,
				modal: true
			});

			$('#dialog').dialog('open');
			
		});


		/*
		 * pulsanti per i datepicker
		 *
		 */

		$(formspath + " button[name='calendar']").live('click', function() {
			var id = '#'+$(this).attr('rel');

			$(id).datepicker({yearRange: '1900:2050', firstDay: 1, changeMonth: true, changeYear: true});  
			$(id).datepicker('show');	
		});

		/*
		 * pulsanti per pulire i campi di testo degli idPopup
		 *
		 */
		$(formspath + " button[name='clean']").live('click', function() {

			var rel = $(this).attr('rel');
			var myarray = rel.split("|"); 
			
			for(var n = 0; el = myarray[n]; n++) { 
				var id = '#'+el;
				$(id).val('');
			}


		});


		/*
		 * pulsanti per i popoup modali
		 *
		 */
		$(formspath + " button[name='popup']").click(function() { 

			var rel = $(this).attr('rel');
			var myarray = rel.split("|"); 
			var id = '#'+myarray[0];
			var url = myarray[1];

			//alert(url);
			
			var myheight = 600;
			var mywidth =  800;

			if(url.indexOf("filesystem")!=-1) {
				myheight = screen.height-((screen.height/100)*40);
				mywidth =  screen.width-((screen.width/100)*10);
			}

			$("#dialog").html( '<iframe src="' + url + '" width="100%" height="100%" frameborder="0"></iframe>' );

			$("#dialog").dialog({
				title: 'CMZ - Seleziona un valore',
				bgiframe: true,
				height: myheight,
				width: mywidth,
				modal: true
			});

			$('#dialog').dialog('open');
			
		});


		/*
		 * funzioni per le dynamiclist
		 *
		 */
		$(formspath + " div.dynamiclist ul").sortable({placeholder: 'ui-state-highlight'});

		$(formspath + " div.dynamiclist ul").change(function () {

			var idelement = $(this).attr('id');
			idelement = idelement.replace("_ul", ""); 
			
			var hidden = "";

			if($('#'+idelement+'_ul').html() == "") { 
				
				$('#'+idelement+'_hidden').html('');
				$('#'+idelement+'_ul').append('<li class="ui-state-default ui-state-disabled empty">Elenco vuoto</li>');

			} else {

				$('#'+idelement+'_ul li').each(function() { 
					var temp = $(this).attr('id');
					var value = temp.split("__")[1];
					hidden += "<input type=\"hidden\" name=\"" + idelement + "[]\" id=\"" + idelement + "\" value=\"" + value + "\" />";
				})
				
				$('#'+idelement+'_hidden').html(hidden);
			}

		});
		

	});

}

/*
 * funzioni per far tornare valori dalle popup modali
 *
 */
function CZ_forms_returnValueText(idelement, valore, testo) {
	
	idelement = '#'+idelement;
	
	parent.$(idelement).val(valore);
	parent.$(idelement+'_text').val(testo);

	parent.$('#dialog').dialog('close');
}

function CZ_forms_returnValueList(idelement, valore, testo) {

	parent.$('#'+idelement+' li.empty').remove();

	var idLi = idelement + '__' + valore;

	var element = '';
	element += '<li class="ui-state-default" id="' + idLi + '">';
	element += testo;
	element += '<a href="javascript:void(false);" onclick="$(\'#'+idLi+'\').remove(); $(\'#'+idelement+'\').trigger(\'change\');" class="delete" title="Elimina dalla lista">Elimina</a>';
	element += '</li>';

	idelement = '#'+idelement; 
	parent.$(idelement).append( element );

	//parent.$('#dialog').dialog('close');
	parent.$(idelement).trigger('change');


	$("#dialog").html( 'Il valore selezionato &egrave; stato inserito sul form della pagina principale. Puoi chiudere la finestra o selezionare altri elementi da aggiungere.\n\nChiudere la finestra?' );

	$("#dialog").dialog({
		title: 'CMZ - Valore inserito',
		bgiframe: true,
		resizable: false,
		modal: true,
		buttons: {
			'Si, chiudi la finestra': function() {
				parent.$('#dialog').dialog('close');
			},
			'No': function() {
				$(this).dialog('close');
			}
		}
	});
	$('#dialog').dialog('open');

}

function CZ_forms_returnMultiValueList(input, idelement) {


	parent.$('#'+idelement+' li.empty').remove();

	$("input[name='" + input + "[]']").each(function(i) {
		if(this.checked)
		{
			var idLi = idelement + '__' + this.value.split("|")[0];
			
			var element = '';
			element += '<li class="ui-state-default" id="' + idLi + '">';
			element += this.value.split("|")[1];
			element += '<a href="javascript:void(false);" onclick="$(\'#'+idLi+'\').remove(); $(\'#'+idelement+'\').trigger(\'change\');" class="delete" title="Elimina dalla lista">Elimina</a>';
			element += '</li>'; 
			
			parent.$("#" + idelement).append( element );
			parent.$("#" + idelement).trigger('change');

		}
	});

	parent.$('#dialog').dialog('close');

}

function getComuni(el, id)
{
	var myhref = 'cmz/plugin/ajax.php?plugin=users&bot=registration&ajaxaction=comuni&id_element=' + id + '&i_provincia_id=' + el.value;
	$.get(myhref, function(html) { 
		$("#" + id).replaceWith( html );
	});
}

function getTendina(el, id, colid, colidparent, coltext, dbtable)
{
	var myhref = 'cmz/plugin/ajax.php?plugin=curriculumvitae&bot=cv&ajaxaction=tendina&id_element=' + id + '&';
	myhref += 'value=' + el.value + '&colid=' + colid + '&colidparent=' + colidparent + '&coltext=' + coltext + '&dbtable=' + dbtable;
	$.get(myhref, function(html) { 
		$("#" + id).replaceWith( html );
	});
}
