/*==========================================================================#
# * Function for adding a Filter to an Input Field                          #
# * @param  : [filterType  ] Type of filter 0=>Alpha, 1=>Num, 2=>AlphaNum   #
# * @param  : [evt         ] The Event Object                               #
# * @param  : [allowDecimal] To allow Decimal Point set this to true        #
# * @param  : [allowCustom ] Custom Characters that are to be allowed       #
#==========================================================================*/
function filterInput(filterType, evt, allowDecimal, allowCustom){
    var keyCode, Char, inputField, filter = '';
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var num   = '0123456789';
    // Get the Key Code of the Key pressed if possible else - allow
    if(window.event){
        keyCode = window.event.keyCode;
        evt = window.event;
    }else if (evt)keyCode = evt.which;
    else return true;
    // Setup the allowed Character Set
    if(filterType == 0) filter = alpha;
    else if(filterType == 1) filter = num;
    else if(filterType == 2) filter = alpha + num;
    if(allowCustom)filter += allowCustom;
    if(filter == '')return true;
    // Get the Element that triggered the Event
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true;
    // Get the Pressed Character
    Char = String.fromCharCode(keyCode);
    // If the Character is a number - allow
    if((filter.indexOf(Char) > -1)) return true;
    // Else if Decimal Point is allowed and the Character is '.' - allow
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true;
    else return false; 
}

function roundTo(base, precision){
	var m = Math.pow(10,precision);
	var a = Math.round(base*m) / m;
	return a;
}

function calcolaInteressi(capitale, giorni, tipo){
	var factor = 0.0347; //fattore per interessi netti
	if (tipo == "l"){factor = 0.0475;}
	var cap = parseFloat(capitale);
	var gg = parseInt(giorni);
	
	//var interessi = (cap*gg)*(Math.pow(1+(factor/4),4)-1)/366;
	var interessi = (cap*gg)*(factor)/366;
	
	if (isNaN(interessi))
		interessi = '';			
	else{	
		interessi = 'euro ' + roundTo(interessi,2);
		interessi = interessi.replace(".",",");
	}
		
	return interessi;
}

function calcolaMontante(capitale, giorni){
	var cap = parseFloat(capitale);
	var gg = parseInt(giorni);
	//var interessi_netti = (cap*gg)*(Math.pow(1+(0.0347/4),4)-1)/366;
	var interessi_netti = (cap*gg)*(0.0347)/366;

	var montante = cap + interessi_netti;
	if (isNaN(montante)){montante = "";}
	else{
		montante = 'euro ' +  roundTo(montante,2);
		montante = montante.replace(".",",");
	}
	return montante;
}

//formatta un campo di input per l'inserimento di valuta
function KeyUpFormatCurrency(textinput,e) {
 	var k;
	if(jQuery.browser['msie']){
		k = e.keyCode;
	} else {
		k = e.which;
	}

	if (k !=110 & k !=188 & k != 37) {
		valore=textinput.val().replace(/\./g,"");
		// 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); } 
			valore=valtomod;
		}
		// Toglie gli zeri iniziali
		if (valore.length !=1 && valore.substr(0,1)=='0') {
			valore=valore.substr(1,valore.length-1);
		}
		temp="";

		// 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;		
		}
		textinput.val(temp);
	}
}

// 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 KeyPressFormatCurrency(importo,e) {
	var k;
	if(jQuery.browser['msie']){
		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.val().split(",").length == 2) return false;
		}
	}else {
		if((k != 8) && (k !=0)) return false;
	}
}

function swapImg(){
	
	var img_src_1 = $(this).attr('src');
	var img_src_2 = $(this).parent('a').attr('rel');
	$(this).attr('src',img_src_2);	
	$(this).parent('a').attr('rel',img_src_1);	
}

