function preloadImages() {
	var d=document;
	if(d.images){
		if(!d.MM_p) d.MM_p=new Array();
		var i, j=d.MM_p.length, a=preloadImages.arguments;
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0){
				d.MM_p[j]=new Image;
				d.MM_p[j++].src=a[i];
			}
	}
}

function getCurLocationWithoutPageName(){
	var curPage = getCurPageName();
	return location.href.replace(curPage, '');
}

function getCurPageName(){
	var arrLoc = location.href.split('/');
	return arrLoc[arrLoc.length-1];
}

function getFileNameWithoutExt(file){
	file = file.replace('.htm', '')
	file = file.replace('.html', '')
	file = file.replace('.php', '')
	file = file.replace('.asp', '')
	file = file.replace('.aspx', '')

	return file;
}

function getOffsetTop(ctr){
	var px = 0;

	while(ctr){
		px += ctr.offsetTop;
		ctr = ctr.offsetParent;
	}

	return px;
}

function getOffsetLeft(ctr){
	var px = 0;

	while(ctr){
		px += ctr.offsetLeft;
		ctr = ctr.offsetParent;
	}

	return px;
}

function isInt(arg){
	breturn = true;
	if (arg != ""){
		arg = arg.replace(",", ".");
		test = parseInt(arg, 10);
		if (isNaN(test)){
			breturn = false;
		}
		else{
			testdbl = parseFloat(arg);
			if (test != testdbl) breturn = false;
		}
	}
	return breturn;
}


function testInt(id, val, msg){
	bok = true;
	if (! isInt(val)){
		bok = false;
		alert(msg);
		document.getElementById(id).focus();
	}
	return bok;
}

function testDate(id, val, msg, culture){
	bok = true;

	if (val != ""){
		var data = getDate(val, culture);
		if (data == null) bok = false;
		if (! bok){
			alert(msg);
			document.getElementById(id).focus();
		}
		else{
			str = (data.getDate().toString().length == 1)? "0" + data.getDate().toString(): data.getDate().toString();
			str += "/";
			str += ((data.getMonth()+1).toString().length == 1)? "0" + (data.getMonth()+1).toString(): (data.getMonth()+1).toString();
			str += "/";
			str += data.getFullYear().toString();
			document.getElementById(id).value = str;
		}
	}
	return bok;
}

function testPastDate(id, val, msg1, msg2, culture){
	bok = true;

	if (val != ""){
		var data = getDate(val, culture);
		if (data == null) bok = false;

		if (testDate(id, val, msg1, culture)){
			var oggi = new Date();
			var diff = Date.parse(data) - Date.parse(oggi);
			if (diff < -86400000){
				alert(msg2);
				document.getElementById(id).focus();
			}
		}
	}
}

function getDate(val, culture){
	var data = null;
	if (val != ""){
		var arDate = val.split("/");
		if (arDate.length == 3){
			if (isInt(arDate[0]) && isInt(arDate[1]) && isInt(arDate[2])){
				switch(culture){
				case "it-IT":
					data = new Date(arDate[2], arDate[1]-1, arDate[0]);
					break;
				case "en-US":
					data = new Date(arDate[2], arDate[0]-1, arDate[1]);
					break;
				}
			}
		}
	}
	return data;
}

function testTime(id, val, msg){
	bok = true;

	if (val != ""){
		var ora = getTime(val);
		if (ora == null) bok = false;
		if (! bok){
			alert(msg);
			document.getElementById(id).focus();
		}
		else{
			str = (ora.getHours().toString().length == 1)? "0" + ora.getHours().toString(): ora.getHours().toString();
			str += ".";
			str += (ora.getMinutes().toString().length == 1)? "0" + ora.getMinutes().toString(): ora.getMinutes().toString();
			document.getElementById(id).value = str;
		}
	}
}

function getTime(val){
	var ora = null;
	var oggi = new Date;
	if (val != ""){
		var arTime = val.split(".");
		if (arTime.length == 2){
			if (isInt(arTime[0]) && isInt(arTime[1])){
				ora = new Date(oggi.getFullYear(), oggi.getMonth(), oggi.getDate(), arTime[0], arTime[1]);
			}
		}
	}
	return ora;
}

function testLength(id, val, length, msg){
	bok = true;
	if (val.length != length){
		bok = false;
		alert(msg);
		document.getElementById(id).focus();
	}
	return bok;
}

function leaveTextBoxAfterDigit(ctr, length, idnext, msg){
	var keycode = window.event.keyCode;
	if (keycode != 9 && keycode != 16 && keycode != 37 && keycode != 38 && keycode != 39 && keycode != 40){
		if(ctr.value.length == length){
			var next = document.getElementById(idnext);
			if (next) next.focus();
		}
	}
}