var isMozilla = (navigator.userAgent.toLowerCase().indexOf('gecko')!=-1) ? true : false;
var regexp = new RegExp("[\r]","gi");

function prendiElementoDaId(id_elemento) {
	var elemento;
	if(document.getElementById)
	elemento = document.getElementById(id_elemento);
	else
	elemento = document.all[id_elemento];
	return elemento;
};

function preload() {
	if (!document.images) return;
	var wl = new Array();
	var arguments = preload.arguments;
	for (var i = 0; i < arguments.length; i++) {
		wl[i] = new Image();
		wl[i].src = arguments[i];
	}
}
function ControllaMail(EmailAddr){
	Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (Filtro.test(EmailAddr))
	return true;
	else
	{
		return false;
	}
}

function pulisci(valore,name){
	Obj = prendiElementoDaId(name)
	Obj.innerHTML =valore
}
function Trim(valore) {
	return valore.replace(/\s+$|^\s+/g,"");
}



function storeCaret(selec,id)
{
	if (isMozilla)
	{
	
      
	  
		oField = prendiElementoDaId(id);

		objectValue = oField.value;
		

		deb = oField.selectionStart;
		fin = oField.selectionEnd;

		objectValueDeb = objectValue.substring( 0 , oField.selectionStart );
		
		objectValueFin = objectValue.substring( oField.selectionEnd , oField.textLength );
		objectSelected = objectValue.substring( oField.selectionStart ,oField.selectionEnd );

		
		oField.value = objectValueDeb + "[" + selec + "]" + objectSelected + "[/" + selec + "]" + objectValueFin;
		
		oField.selectionStart = objectValueDeb.length;
		oField.selectionEnd = (objectValueDeb + "[" + selec + "]" + objectSelected + "[/" + selec + "]").length;
		oField.focus();
		oField.setSelectionRange(
		objectValueDeb.length + selec.length + 2,
		objectValueDeb.length + selec.length + 2);
	}
	else
	{
		// Si on est sur IE

		//oField = document.forms['news'].elements['newst']
		oField = prendiElementoDaId(id);

		var str = document.selection.createRange().text;

		if (str.length>0)
		{
			// Si on a selectionné du texte
			var sel = document.selection.createRange();
			sel.text = "[" + selec + "]" + str + "[/" + selec + "]";
			sel.collapse();
			sel.select();
		}
		else
		{
			oField.focus(oField.caretPos);
			oField.focus(oField.value.length);
			oField.caretPos = document.selection.createRange().duplicate();

			var bidon = "%~%";
			var orig = oField.value;
			oField.caretPos.text = bidon;
			var i = oField.value.search(bidon);
			oField.value = orig.substr(0,i) + "[" + selec + "][/" + selec + "]" + orig.substr(i, oField.value.length);
			var r = 0;
			for(n = 0; n < i; n++)
			{if(regexp.test(oField.value.substr(n,2)) == true){r++;}};
			pos = i + 2 + selec.length - r;
			//placer(document.forms['news'].elements['newst'], pos);
			var r = oField.createTextRange();
			r.moveStart('character', pos);
			r.collapse();
			r.select();

		}
	}
}


function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "") {
			for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
				if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		} // Keep on going until we build an "inbetween" string that doesn't exist
		// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		// Next, replace the "inbetween" string with the "toString"
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} // Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function IsNum( numstr ) {
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")
	return false;
	var isValid = true;
	var decCount = 0;
	numstr += "";
	for (i = 0; i < numstr.length; i++) {
		if (numstr.charAt(i) == ".")
		decCount++;
		if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
			isValid = false;
			break;
		} else if ((numstr.charAt(i) == "-" && i != 0) || (numstr.charAt(i) == "." && numstr.length == 1) || (numstr.charAt(i) == "." && decCount > 1)) {
			isValid = false;
			break;
		}
	}

	return isValid;
}


function showToolTip(e,id_obj){
	if(document.all)e = event;
	var obj = document.getElementById(id_obj);
	obj.style.display = 'block';	
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);

	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0;
	var leftPos = e.clientX + 80;
	if(leftPos<0)leftPos = 0;

	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight +120 + st + 'px';
}

function hideToolTip(id_obj){
	document.getElementById(id_obj).style.display = 'none';
}



function nuovo_messaggio(){
			w = 450;
			h = 80;
			LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
			obj = prendiElementoDaId('pop_up');
			obj.style.width= w+'px';
			obj.style.left= LeftPosition+'px';
			obj.style.top= TopPosition+'px';
			obj.style.display='block';
			opacity('pop_up',0,100,300);
}
function hide(obj_id){
			obj = prendiElementoDaId(obj_id);						
			obj.style.display='none';
			//frame
			objframe = prendiElementoDaId(obj_id+'_frame');
			if(objframe != null){
				objframe.src= '';
			}
}



//fade
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++;
        }
    }
}

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 + ")";
} ;

/*DACANCELLARE [START]*/
function popup_center(w,h,id_obj,vel,url){
			LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
			TopPosition = TopPosition-90;
			obj = prendiElementoDaId(id_obj);
			obj.style.width= w+'px';
			obj.style.height= h+'px';
			
			//frame
			objframe = prendiElementoDaId(id_obj+'_frame');
			if(objframe != null){
				objframe.src= url;
				objframe.style.width = w+'px';
				objframe.style.height = h+'px';	
			}
			obj.style.left= LeftPosition+'px';
			obj.style.top= TopPosition+'px';
			obj.style.display='block';									
}
/*DACANCELLARE [END]*/

function popup_general(w,h,id_obj,url){
			LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
			TopPosition = TopPosition-90;
			obj = prendiElementoDaId(id_obj);
			obj.style.width= w+'px';
			obj.style.height= h+'px';
			
			//frame se presente
			objframe = prendiElementoDaId(id_obj+'_frame');
			if(objframe != null){
				objframe.src= url;
				objframe.style.width = w+'px';
				objframe.style.height = h+'px';	
			}
			obj.style.left= LeftPosition+'px';
			obj.style.top= TopPosition+'px';
			obj.style.display='block';									
}






