//DD_belatedPNG.fix('#header, #content, #footer');

function substr( f_string, f_start, f_length ) {
    // Returns part of a string  
    // 
    // version: 810.1317
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
    f_string += '';

    if(f_start < 0) {
        f_start += f_string.length;
    }

    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if(f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

// Acquire GET data
function acquireGETdata()
{
	var querystring=location.search.substr(1); // Toglie il ? del location.search
	var dati = new Array(); // Crea un nuovo array dove spezzettare illocation.search
	dati = querystring.split("&"); // Assegna all'array dati tutte le variabili prese dall'url
	for (n=0; n<dati.length; n++) {
		var_name = dati[n].substr(0,dati[n].indexOf("=")) ;
		var_content = dati[n].substr(dati[n].indexOf("=")+1) ;
		var_set = "GET_"+var_name + " = '" + var_content + "';" ;
		eval(var_set);
	}
}

// Controlla la lunghezza massima di un campo di testo
function checkMaxLength(val,max,divtarget) {
	var currentLength = $(val).val().length;
	if (currentLength > max)
	{
		$(val).text().slice(0,255);		
		$('#'+divtarget).html("<b>"+(currentLength-1)+"/"+max+" <span style='color: #900;'>Max caratteri raggiunto, il testo è stato tagliato.</span></b>");
	} else {
		$('#'+divtarget).html("<b>"+currentLength+"/"+max+"</b>");
	}
}

// array prototype
Array.prototype.sum = function(){
	for (var i=0, sum=0; i < this.length; sum += this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}
Array.prototype.remove=function(s){
	for (i=0; i < this.length; i++){
		if (s == this[i]) this.splice(i, 1);
	}
}


/*function checkMaxLength(val,max,divtarget) {
	var maxLength = max;
	var currentLength = val.value.length;
	var text = val.value.slice(0,255);
	if (currentLength > maxLength)
	{
		val.value = text;
		getByID(divtarget).innerHTML = "<b>"+(currentLength-1)+"/"+max+" <span style='color: #900;'>Max caratteri raggiunto, il testo è stato tagliato.</span></b>";
	} else {
		getByID(divtarget).innerHTML = "<b>"+currentLength+"/"+max+"</b>";
	}
}*/

// Crea una stringa con tutti i valori del form nella pagina
function preparaDatiUpdate(){
	var stringa = "";
  	var form = document.forms[0];
  	var numeroElementi = form.elements.length;
 
  	for(var i = 0; i < numeroElementi; i++){
		if(i < numeroElementi-1){
	   		if (form.elements[i].type == "checkbox"){
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].checked)+"&";
			} else {
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
			}
		} else {
			if (form.elements[i].type == "checkbox"){
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].checked);
			} else {
				stringa += form.elements[i].name+"="+encodeURIComponent(form.elements[i].value);
			}
    	}
  	}
  	
  	return stringa;
}

// Redirect
function cancelValue(id) { getByID(id).value = ""; }

function MM_goToURL(){ //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

// funzione per ricavare il riferimento in base all'id
function getByID(id_elemento) {
	var elemento;
	if(document.getElementById)
		elemento = document.getElementById(id_elemento);
	else
		elemento = document.all[id_elemento];
	return elemento;
};

// Funzione per ricavare il valore selezionato in un radio button
function getFromRadio(nome_form,id_elemento) {
	var indice = 0;
	for (i = 0; i < document.forms[nome_form].elements[id_elemento].length; i++) {
		if (document.forms[nome_form].elements[id_elemento][i].checked) indice = i;
	}
	var res = document.forms[nome_form].elements[id_elemento][indice];
	if (res == undefined) { res = document.forms[nome_form].elements[id_elemento]; }
	return res;
}

// Apri popup
function popUpMe(location,width,height,resize) {
	width = width == "" ? width = "600" : width;
	height = height == "" ? height = "400" : height;
	//resize = resize == "" ? "no" : "yes";
	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
    window.open(location,"_blank",'width='+width+', height='+height+', top='+TopPosition+', left='+LeftPosition+', toolbar=no, location=no, directories=no, menubar=no, status=no, resizable=no, scrollbars=yes');
}

//Controllo Email
function checkEmail(email) {
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(email) || (email == "") || (email == "undefined")) {
	   alert("Inserire un indirizzo Email corretto.");
	   getByID("email").focus();
	   return false;
	} else {
		return true;
	}
}
