/**
 * @author ma2k1
 */



// Funzione che controlla i campi di testo
function ctrl_text(form,tipo) {
			// controllo che i campi di testo non siano vuoti
			for (i=0; i<form.getElementsByTagName(tipo).length; i++) {
				this_input = form.getElementsByTagName(tipo)[i];
				
				//controllo solo i campi di testo e le textarea
				if ((tipo == 'input' && this_input.type == 'text') || tipo == 'textarea') {
					this_id =getLabelForId(this_input.id);
				
					if (!filled(this_input)) {
						alert('Il campo "'+ this_input.name+'" non è stato compilato');					
						this_id.className = 'problem';
						this_input.focus();
						return false;
					}
					else {
						this_id.className = 'completed';
					}
					
				}	
			}
			return true;
}

function ctrl_box(form,tipo) {

			// controllo che i campi di testo non siano vuoti
			for (i=0; i<form.getElementsByTagName(tipo).length; i++) {
				this_input = form.getElementsByTagName(tipo)[i];
				
				if (this_input.type == 'checkbox') {
				this_id = getLabelForId(this_input.id);		
					
					if (!check(this_input)) {
						alert('La casella '+ this_input.name+' deve essere spuntata');					
						this_id.className = 'problem';
						return false;
					}
					else {
						this_id.className = 'completed';
					}					
					
				}
								
			}	
			return true;
}

//associo ad un id la sua label
function getLabelForId(id) {
 var label, labels = document.getElementsByTagName('label');
 for (var i = 0; (label = labels[i]); i++) {
   if (label.htmlFor == id) {
     return label;
   }
 }
 return false;
}

//controllo se i campi sono vuoti o nulli
function filled(field) {
		if (field.value == "" || field.value == null) {
			return false;
		} else {
			return true;
		}
}
//controllo se le checkbox sono spuntate
function check(field) {
		if (field.checked == false) {
			return false;
		} else {
			return true;
		}
}
//cancello il value di un campo o textarea
function del(field) {
	field.value="";
}



//effettuo i controlli sul form e poi chiedo all'utente se vuole inviarlo o meno
function canSubmit(form, messaggio){
	
	//effettuo i vari controlli, se sono superati, chiedo all'utente se inviare o meno
	if (ctrl_text(form,'input') && ctrl_text(form,'textarea') && ctrl_box(form,'input')) {
		messaggio+='\n\nCliccare il tasto "OK" per confermare.';
		if (confirm(messaggio)) {
			return true;
		}
		else
			return false;	
	}
	else
		return false;
		
}

//focus sui campi del form
function evidenzia(inp){
		inp.style.border='1px solid blue';
		inp.style.background='white';	
}
function disevidenzia(inp){
		inp.style.border='1px solid #3a62b6';
		inp.style.background='#dce5e9';
}
//mouse sopra / out sui div news e faq
function effect_faq(inp,bkg,col){
	inp.style.background=bkg;
	inp.style.color=col;	
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//ritorna l'id del body
function idbody(){
 var div = document.getElementsByTagName("body")[0].getAttribute("id");
 return div;
}
//ritorna la classe associata al body
function classbody(){
 var el = document.getElementsByTagName("body")[0].className;
 return el;
}

//mostra o nasconde un div
function hiding(div){
	
	var mydiv= document.getElementById(div);
	if (mydiv.style.display == "none") {
		mydiv.style.display = "block";
	}
	else
		mydiv.style.display = "none";
}

//utilizzando la libreria behaviour.js di PPK e Simon Willis, separo
//il comportamento (Javascript) dalla struttura del documento Xhtml
//in questo modo javascript è NON intrusivo
var myrules = {

		'form.acform' : function(element) {
					element.onsubmit = function() {
							return canSubmit(this,'Si sta per inviare la mail alla NuovEnergie spa.');
					}
        },
		'form.acform input' : function(element) {
				if (element.type != "checkbox") {
						element.onfocus = function() {
								evidenzia(this);
						}
						element.onblur = function() {
								disevidenzia(this);
						}
				}						
        },
		'form.acform textarea' : function(element) {
					var i=0;
					element.onfocus = function() {
							evidenzia(this);
							//altrimento cancello il testo inserito dall'utente
							if (i==0) {
								del(this);
								i++;
							}
					}
					element.onblur = function() {
							disevidenzia(this);
					}					
        },
		'.risp' : function(element) {
					element.onmouseover = function() {
							effect_faq(this,'#dce5e9','black');
					}
					element.onmouseout = function() {
							effect_faq(this,'white','#333333');
					}					
        },
		'.news' : function(element) {
					element.onmouseover = function() {
							effect_faq(this,'#dce5e9','black');
					}
					element.onmouseout = function() {
							effect_faq(this,'white','#333333');
					}					
        }
}

Behaviour.register(myrules);

//gestisco l'onload!!!
// My window onload event
Behaviour.addLoadEvent(
	function() {
		if (classbody() == 'nf') {

			switch ( idbody() ){
				case "contatti" :
						opacity('esito',100,0,10000);
						setTimeout("hiding('esito');",10000);						
				  break;				  			  		  				  				  						
			}
		}
	}		
);
