/*
 * Gian Matteo Sau - JavaScript Tools
 * jQuery Based
 *    http://jquery.com/
 *
 * Copyright (c) 2010 Gian Matteo Sau
 *    http://gianmatteosau.net/
 */

function windowInfo(){
	$('#debugger').css({ 'display' : 'block' });
	$('#debugger').html('<div id="info">Larghezza BODY: '+document.body.clientWidth+'<br />Larghezza WRAPPER: '+$('#wrapper').width()+'</div>');
}

function destroy() {
	open('./inc/process/logout.php')
}

function new_title(titolo){
	document.title = document.title + " | " + titolo;
}

function new_navigation(stringa){
	$('#dove_sei').html(stringa);
}

function delete_content(){
	$('#response').html('');
}

function del_timer(){
	var t = setTimeout("delete_content()", 5000);
}

function anyCheck(value) {
	document.getElementById('identificazione').value = value;
}

function pwdFocus() {
	$('#fakepassword').hide();
	$('#login_password').show();
	$('#login_password').focus();

}

function pwdBlur() {
	if($('#login_password').attr('value') == ''){
		$('#login_password').hide();
		$('#fakepassword').show();
	}
}

function barraColore(colore, barra) {
	$('#'+barra+'*').css({'background-color' : colore});
}

function cambiaColore(colore, livello) {
	$('#'+livello).css({'color' : colore});
}


function checksSelected(mode, modulo, page){
	var valore = document.getElementById('identificazione');

	if(valore.value == ""){
		alert("Selezionare un elemento");
	} else {
		//str = valore.id;
		if(mode == "delete"){
			if(!page){
				if(confirm('Stai per eliminare una voce.\nContinuare?')) window.location = './moduli/'+modulo+'/processi/del.php?id='+valore.value;
			} else {
				window.location = './moduli/'+modulo+'/processi/'+page+'.php?voce='+valore.value;
			}
		} else if(mode == "add" || mode == "duplica"){
			document.lista.action  = './?mod='+modulo+'&file='+page;
			document.lista.submit();
		} else if(mode == "print"){
			if(!page){
				window.location = './pdf/create.php?id='+valore.value;
			} else {
				window.location = './pdf/'+page+'.php?id='+valore.value;
			}
		} else if(mode == "send"){
			window.location = './moduli/'+modulo+'/processi/send_email.php?id='+valore.value;
		} else if(mode == "none"){
			alert('Funzione non attiva per questa Categoria');
		} else {
			document.lista.submit();
		}
	}
}

function controlloForm(nomeForm, campi, alerting){
	var x=document.forms[nomeForm];
	var messaggio = "Compilare i seguenti campi obbligatori: ";
	
	for (var i=0;i<x.length;i++){
		if(campi.search(x.elements[i].name) >= 0){
			//alert(x.elements[i].name + ': ' + x.elements[i].value + ' (controllato)\n');
			//alert(x.elements[i].name+', '+x.elements[i].type+', '+x.elements[i].value);
			var str = x.elements[i].name;
			
			if(x.elements[i].type == "checkbox"){
				if(x.elements[i].checked == false){
					messaggio = messaggio + "\n-> " + str;
						
					x.elements[i].style.border = "1px red solid";
					x.elements[i].style.color = "#990000";
					x.elements[i].style.background = "#F9E8E8";
				} else {
					x.elements[i].style.border = "1px #999 solid";
					x.elements[i].style.color = "#333";
					x.elements[i].style.background = "#f4f4f4";
				}
			} else {
				if(x.elements[i].value == ""){
					messaggio = messaggio + "\n-> " + str;
						
					x.elements[i].style.border = "1px red solid";
					x.elements[i].style.color = "#990000";
					x.elements[i].style.background = "#F9E8E8";
				} else {
					x.elements[i].style.border = "1px #999 solid";
					x.elements[i].style.color = "#333";
					x.elements[i].style.background = "#f4f4f4";
				}
			}
		}
	}
	
	if (messaggio == "Compilare i seguenti campi obbligatori: ") {
		return true
	} else {
		if(alerting) alert(messaggio);
		return false;
	}
	
}

function cambiaFlag(valore){
    $("#flag").attr("src","./img/icone/flags/"+valore);
}

function allestimenti(oggetto){
	if($('#'+oggetto).val() == 0){
		$('#'+oggetto).val($('#'+oggetto).attr('id'))
	} else {
		$('#'+oggetto).val(0)
	}	
}

function sendForm(scheda){
	//alert(scheda);
	document.forms[scheda].submit();
}

function formToString(form){
	var str			= "";
	var elements	= form.elements;
	for(var i=0;i<elements.length;i++){
		var el = form.elements[i]; 
		if(el.tagName=="INPUT"){
			if(el.type.toUpperCase()=="RADIO"){
				if(!el.checked)
					continue;
			}
		}
		if(!el.name)
			continue;
		if(str!="")
			str += "&";
		str += el.name + "=" + form.elements[i].value;
	}
	return str;
}

function contentDisp(div, page){
	$('#'+div).html('<img src="./img/icone/loading.gif" />');
	$('#'+div).css({ 'display' : 'none' }).load(page).fadeIn('slow');
}

function contentDispParams(div, page, arrayParametri){
	$('#'+div).html('<img src="./img/icone/loading.gif" />');
 	$('#'+div).load(page, { 'parametri[]': arrayParametri }).fadeIn(3000);
}

function contentDispParamsDonated(div, page, testo, arrayParametri){
	$('#'+div).html(testo+'<img src="./img/icone/loading.gif" />');
 	$('#'+div).load(page, { 'parametri[]': arrayParametri }).fadeIn(3000);
}


function loadingInDiv(div, page){
	$('#'+div).css({ 'display' : 'none' }).load(page).fadeIn('slow');
}

function openDivByClass(div){
	$('.'+div).fadeIn('slow');
}

function checkUtenti(div, page, valore){
	if(valore == 2){
		$('#'+div).css({ 'display' : 'none' }).load(page).fadeIn('slow');
	} else {
		$('#'+div).html('');
	}
}

function quoteDonazione(messaggio, messaggioMin, maxValore){
	if($('#quotaDonata').val() > maxValore){
		alert(messaggio+" "+maxValore+" Euro")
		return false;
	} else if($('#quotaDonata').val() < 1 && maxValore > 50){
		alert(messaggioMin+" 1 Euro")
		return false;
	} else {
		return true;
	}
}

function apriDiv(div, titolo){
	if ($("#"+div).is(":hidden")) {
		//$("#"+titolo).removeClass().addClass("faq_titolo");
		$("#"+div).show("slow", function() {
			//$("#"+titolo).addClass("faq_titolo_stand");
			//$(this).css({"margin-top":"5px", "display":"block"});
		});
		//$("#"+div).show("slow").css({"display":"block","margin-top":"5px"});
	} else {
		$("#"+div).hide("slow", function() {
			//$("#"+titolo).removeClass().addClass("faq_titolo");
			//$(this).css({"display":"none","margin-top":"0"});
		});
		//$("#"+div).hide("slow").css({"display":"none","margin-top":"0"});
	}
}

function checkFileType(form, checkDiv){
	var fileName = new String(document.forms[form].allegato.value);
	if(fileName.lastIndexOf(".zip") == -1 && fileName.lastIndexOf(".pdf") == -1) {
		//alert("Sono ammessi solo file ZIP");
		$('#'+checkDiv).html('<font color="red">Sono ammessi solo file <b>.zip</b> o <b>.pdf</b><br />Qualsiasi altro tipo di file non verr&agrave; allegato.</font>');
		$('#allegato').val('');
		//document.forms[form].allegato.value = '';
		//alert(fileName.lastIndexOf(".zip"));
	} else {
		$('#checkType').html('');
	}
}

function appendContenuto(div, pagina, id){
	$('#'+div).html('<center><img src="./img/icone/loading.gif" /></center>');
	$('#'+div).load("page/inc/"+pagina+".php?id="+id);
}

function cambiaPagina(pulsante, page, arrayParametri){
	//alert("");
	$("div.paginazione_stand").each(function (i) {
		$(this).removeClass().addClass("paginazione");
	});
	
	$('div#'+pulsante).removeClass().addClass("paginazione_stand");
	
 	$('div#change').load(page, { 'parametri[]': arrayParametri });
	scrollaSu();
}

function scrollaSu(){
	$('html,body').animate({
	scrollTop: $("div.titolo_pagina").offset().top
	}, 400);
}

function str_replace (search, replace, subject, count) {
	j = 0,
	temp = '',
	repl = '',
	sl = 0,
	fl = 0,
	f = [].concat(search),
	r = [].concat(replace),
	s = subject,
	ra = Object.prototype.toString.call(r) === '[object Array]',
	sa = Object.prototype.toString.call(s) === '[object Array]';
    s = [].concat(s);

    if (count) {
        this.window[count] = 0;
    } 

    for (i = 0, sl = s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
		
		for (j = 0, fl = f.length; j < fl; j++) {
            temp = s[i] + '';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
				this.window[count] += (temp.length - s[i].length) / f[j].length;
            }
        }
    }
    return sa ? s : s[0];
}

// Numeric only control handler
jQuery.fn.ForceNumericOnly =
	function()
	{
		return this.each(function()
		{
			$(this).keydown(function(e)
			{
				var key = e.charCode || e.keyCode || 0;
				return (
					key == 8 || 
					key == 9 ||
					key == 46 ||
					(key >= 37 && key <= 40) ||
					(key >= 48 && key <= 57) ||
					(key >= 96 && key <= 105));
			})
		})
	};
