function trim(s) {
	var res = s.replace(/^\s*(.*)/, "$1");
	res = res.replace(/(.*?)\s*$/, "$1");
	return res;
}

function isNumber(n) {
	var validChars = "0123456789.";
	var c;
	var res = true;
 
	if (n == '') {
		res = false;
	} 
 
	for (i = 0; i < n.length && res; i++) { 
		c = n.charAt(i); 
		if (validChars.indexOf(c) == -1) {
			res = false;
		}
	}
	return res;
}

function validateEmailField(emailElem) {
	var email = emailElem.value;
	var atIndex = email.indexOf("@");
	var afterAt = email.substring((atIndex + 1), email.length);
	// find a dot in the portion of the string after the ampersand only
	var dotIndex = afterAt.indexOf(".");
	// determine dot position in entire string (not just after amp portion)
	dotIndex = dotIndex + atIndex + 1;
	// afterAt will be portion of string from ampersand to dot
	afterAt = email.substring((atIndex + 1), dotIndex);
	// afterDot will be portion of string from dot to end of string
	var afterDot = email.substring((dotIndex + 1), email.length);
	var beforeAmp = email.substring(0,(atIndex));
	var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/ 
	// index of -1 means "not found"
	if ((email.indexOf("@") != "-1") && (email.length > 5) && (afterAt.length > 0) && (beforeAmp.length > 1) && (afterDot.length > 1) && (email_regex.test(email)) ) {
		return true;
	} else {
		return false;
	}
}


function openJSWindow(title, url, width, height) {
	var detect = navigator.userAgent;
	var tagname = detect.indexOf("MSIE")>=0 ? "object" : "embed";
	var contFlash = document.getElementsByTagName(tagname);
	for (var i = 0; i < contFlash.length; i++) {
        	$(contFlash[i]).hide();
       	}
	var w = width == undefined ? 400 : width;
	var h = height == undefined ? 500 : height;
	var win = new Window("servizi", {
		className: "veloprezzo", 
		title: title, 
		resizable: true, 
		minimizable: false,
		maximizable: false,
		width: w, 
		height: h, 
		showEffect: Element.show,
		url: url,
		onClose: function() {
			for (var i = 0; i < contFlash.length; i++) {
        			$(contFlash[i]).show();
       			}
		}
	});
	win.setDestroyOnClose();
	win.showCenter(true);
	//modifica x ie6
	$('servizi_content').src=$('servizi_content').src;
	//fine modifica
	
}



//<input onkeyup="return KeyUpImporto(this,event)" onkeypress="return KeyPressImporto(this,event)" maxlength="13" size="15" class="input_edit" value="" name="importo"/>

// permette inserimento di cifre da 0 a 9 e della virgola --------------
// viene impedito l'inserimeno di una seconda virgola ------------------
// viene impedito l'inserimento di pi˘ di due cifre dopo la virgola ----
function KeyPressImporto(importo,e) {
	var ie = (navigator.appName.indexOf('Microsoft Internet Explorer') > -1);
	var k;
	var valore = importo.value;
	if(ie) {
		k = e.keyCode;
	} else {
		k = e.which;
	}
	
	if( ((k > 47) && (k < 58)) || (k == 44) )
	{
		// Impediamo di digitare due volte la virgola
		if (k == 44) {
			if (importo.value.split(",").length == 2) return false;
		}
	
	}else {
		if((k != 8) && (k !=0)) return false;
	}
}
// toglie gli zeri iniziali e inserisce i punti delle migliaia ------------- 
function KeyUpImporto(textinput,e) {
	var ie = (navigator.appName.indexOf('Microsoft Internet Explorer') > -1);
	var k;
	if(ie) {
		k = e.keyCode;
	} else {
		k = e.which;
	}
	if (k !=110 & k !=188 & k != 37) {
		valore=textinput.value.replace(/\./g,"");
		textinput.value=formatValuta(valore);
	}
}
//----permette scrittura solo numeri da 0 a 9 -----------------------
function KeyPressOnlyNum(e) {
var ie = (navigator.appName.indexOf('Microsoft Internet Explorer') > -1);
var k;
if(ie) {
	k = e.keyCode;
} else {
	k = e.which;
}
if((k < 48) || (k > 57)) 
	{
		// Serve per abilitare le operazioni di modifica cancella	per browser diversi da IE
		if(k != 8 && k !=0) return false;				
	}
}

//----formatta il valore --------

function formatValuta (importo,complete){
	temp="";
	valore = importo+"";
	if (valore){
		// Conserva i centesimi
		centesimi="";
		if (valore.indexOf(",")>-1) {
			espr=valore.split(",");
			valtomod=valore.substring(0,valore.indexOf(","));
			centesimi=valore.substring(valore.indexOf(",")+1,valore.length+1);
			if (centesimi.length > 2){ centesimi = centesimi.substring(0,2); }
			else if (complete && centesimi.length == 1) {centesimi = centesimi+"0";}
			else if (complete && centesimi.length ==0){centesimi = "00";}
			valore=valtomod;
		}else
			centesimi = "00";
		// Toglie gli zeri iniziali
		if (valore.length !=1 && valore.substr(0,1)=='0') {
			valore=valore.substr(1,valore.length-1);
		}
		// Aggiungi i punti
		while (valore.length>0) {
			if (valore.length>3) {
				temp="."+valore.substr(valore.length-3,3)+temp;
				valore=valore.substr(0,valore.length-3);
			} else {
				temp=valore+temp;
				valore="";
			}
		}
		if (centesimi!="") {
			temp=temp+","+centesimi;
		}
	}else
		temp= "0,00";
	return (temp);
}

//Controllo CF
function controllaCF(cf){

    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return '';
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        return "La lunghezza del codice fiscale non è\n"
        +"corretta: il codice fiscale dovrebbe essere lungo\n"
        +"esattamente 16 caratteri.\n";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 )
            return "Il codice fiscale contiene un carattere non valido `" +
                cf.charAt(i) +
                "'.\nI caratteri validi sono le lettere e le cifre.\n";
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
        return "Il codice fiscale non è corretto:\n"+
            "il codice di controllo non corrisponde.\n";
    return "";
}

function validaNumeroTelefono (numero, minchar) {
	var test = numero.replace(/\D/g,"");
	var testLength = 7;
	if (minchar)
		testLength = minchar;
	
	if (test.length < testLength)
		return false;
	
	return true;
}


