/*******************************************************
HOW TO USE JSON

function updatediv(){
var obj = makePostJson("controller.php","action=prova");

var myOptions = {

  "Value 1" : obj[0],
  "Value 2" : "Text 1"
}
jQuery("#people").addOption(myOptions, false);

}
**************************************************/

function makePostUpd(controller,param,div){

var cont = jQuery.ajax({
           type: "POST",
           url: controller,
           data: param,
           async: false
           }).responseText;

     jQuery(div).html(cont);
}

function makePostAdv(controller,param){

var cont = jQuery.ajax({
           type: "POST",
           url: controller,
           data: param,
           async: false
           }).responseText;
     return cont;
}




function makePostNoUpd(controller,param){

jQuery.ajax({
           type: "POST",
           url: controller,
           data: param,
           async: true
});

}

function makePostRedir(controller,param){

jQuery.ajax({
           type: "POST",
           url: controller,
           data: param,
           success: function(msg){

                      var result= new Array();
                      result=msg.split('|');
                      if(result[0]=='ok'){

                      window.location.href= result[1];
                      }

                    }
});

}

function makePostJson(controller,param){

var json=jQuery.ajax({
           type: "POST",
           dataType: "json",
           url: controller,
           async: false,
           data: param



});
var json = eval('(' + json.responseText + ')');

return json;

}

function makePostJsonAsync(controller,param){

var json=jQuery.ajax({
           type: "POST",
           dataType: "json",
           url: controller,
           async: true,
           data: param



});
var json = eval('(' + json.responseText + ')');

return json;

}

function doLoad(div){
cont="<center><img src='img/ajax_loader2.gif'></center>";
jQuery(div).html(cont);
}


/*
*FDesc:popola una select con delle opzioni ottenute da un array JSON encodato
*/
function populateSelect(selectid,controller,param){
//selectid is select#pippo or select[@name='tizio']
var obj =makePostJson(controller,param);

var options ='<option></option>';

for (i = 0; i < obj.length; i++) {

options = options + '<option value="' + obj[i].value + '" >' + (obj[i]).text + '</option>';
}

jQuery(selectid).html(options);

jQuery(selectid+" option:eq(0)").attr("selected","selected");




if(jQuery(selectid+" option:eq(1)").val()=='noroutes'){

//jQuery('a#a_reload').fadeIn();

//alert(jQuery('a#a_reload').attr('href'));

/*window.top.location='*/
var x=jQuery('a#a_reload').attr('href');


window.top.location=x;

}else{
return 1;
}



}



/*
FDesc:disabilita o abilita tutti gli input in un contenitore
*/
function disableInputs(containername,option){
/*containername es:span#pippo*/
jQuery(':input',containername).each(function() {
		if(option){jQuery(this).attr("disabled","disabled")}
		else{jQuery(this).attr("disabled","")};
 	});
}


/*
FDesc:svuota tutti gli input in un contenitore
*/
function emptyInputs(containername){
/*containername es:span#pippo*/
jQuery(':input',containername).each(function() {
		jQuery(this).val('')

 	});
}


/*Fdesc:Valida la form passata per nome

formid="form#formid"
param="action=xxx"
errdiv="div#errors"
*/
function formvalidate(formid,param,errdiv,successfunc,loadingdiv){

       var v = jQuery(formid).validate({

       errorLabelContainer: jQuery(errdiv),

       submitHandler: function(form) {

       if(loadingdiv.length!=0)
       doLoad(loadingdiv);

       jQuery(form).ajaxSubmit({

       url: 'controller.php?'+param,

 	   success: function(responseText) {

 	                                var v= eval(successfunc);

									v(responseText);

								}

       	 				});

				}
		});
}



/*Fdesc:fa il redirect a partire da una risposta
answrtype= 'ok!page.php'
*/
function redirAns(msg){

	var result= new Array();
	result=msg.split('|');
	if(result[0]=='ok'){
	window.location.href= result[1];
	}else{
	alert(msg);
	}
	//potrei mettere anche un alert con jqmodal

}


/*Fdesc: cancella una form
answrtype= cancella la form
*/
function clearForm(form) {
 // iterate over all of the inputs for the form
 // element that was passed in
jQuery(':input', form).each(function() {
var type = this.type;
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = -1;
});
};

/*Fdesc: resetta i campi di unput in un contenitore
answrtype= no answer
*/
function clearFields(container) {
 // iterate over all of the inputs for the form
 // element that was passed in
jQuery(':input', container).each(function() {
var type = this.type;
var tag = this.tagName.toLowerCase();
// it's ok to reset the value attr of text inputs,
// password inputs, and textareas
if (type == 'text' || type == 'password' || tag == 'textarea')
this.value = "";
else if (type == 'checkbox' || type == 'radio')
this.checked = false;
// select elements need to have their 'selectedIndex' property set to -1
// (this works for both single and multiple select elements)
else if (tag == 'select')
this.selectedIndex = 0;
});
};


/*Fdesc: copia il contenuto dei campi input text select di un contenitore  in un altro corrispondente
answrtype= cancella la form(da rivedere)
*/
function copyFields(containerfrom,containerto) {
 // iterate over all of the inputs for the form
 // element that was passed in
	jQuery(':input', containerfrom).each(function() {
	var name = jQuery(this).attr('name');
	var value = jQuery(this).val();
	var type = this.type;
	var tag = this.tagName.toLowerCase();
	// it's ok to reset the value attr of text inputs,
	// password inputs, and textareas
	if (type == 'text' || type == 'password' || tag == 'textarea'){
	jQuery(containerto+' input[@name='+name+']').val(value);
	}
	else if(type == 'checkbox' || type == 'radio') {
		if(jQuery(this).is(':selected')){
		jQuery(containerto+' input[@name='+name+']').attr('selected','selected');
		}else{
		jQuery(containerto+' input[@name='+name+']').attr('selected','');
		}
	}
	// select elements need to have their 'selectedIndex' property set to -1
	// (this works for both single and multiple select elements)
	else if (tag == 'select'){
	jQuery(containerto+' select[@name='+name+'] option[@value='+value+']').attr('selected','selected');
	}
	});
};


/*Fdesc: copia il contenuto dei campi input text select di un contenitore  in un altro corrispondente
answrtype= cancella la form(splitta il nome all_ e ricerca la secoda parte nel nome
*/

function copyFieldsEreg(containerfrom,containerto) {
 // iterate over all of the inputs for the form
 // element that was passed in
	jQuery(':input', containerfrom).each(function() {
	var name = jQuery(this).attr('name');
	var temp ='';
	var temp = name.split('_');
	name= temp[1];
	var value = jQuery(this).val();
	var type = this.type;
	var tag = this.tagName.toLowerCase();
	var disabled=0;

	if(jQuery(this).is(':disabled')){
	disabled=1;
	}

	// it's ok to reset the value attr of text inputs,
	// password inputs, and textareas
	if (type == 'text' || type == 'password' || tag == 'textarea'){

	if(disabled == 1){jQuery(containerto+' input[@name$='+name+']').attr('disabled','disabled');}
	else{jQuery(containerto+' input[@name$='+name+']').attr('disabled','');}

	jQuery(containerto+' input[@name$='+name+']').val(value);
	}
	else if(type == 'checkbox' || type == 'radio') {
	    if(disabled == 1){jQuery(containerto+' input[@name$='+name+']').attr('disabled','disabled');}
		else{jQuery(containerto+' input[@name$='+name+']').attr('disabled','');}

		if(jQuery(this).is(':checked')){
		jQuery(containerto+' input[@name$='+name+'][@value='+value+']').attr('checked','checked');
		}else{
		jQuery(containerto+' input[@name$='+name+'][@value='+value+']').attr('checked','');
		}
	}

	// select elements need to have their 'selectedIndex' property set to -1
	// (this works for both single and multiple select elements)
	else if (tag == 'select'){
	if(disabled == 1){jQuery(containerto+' select[@name$='+name+']').attr('disabled','disabled');}
	else{jQuery(containerto+' select[@name$='+name+']').attr('disabled','');}

	jQuery(containerto+' select[@name$='+name+'] option[@value='+value+']').attr('selected','selected');
	}
	});
};






