/*
data: object
	- item: array di percorsi delle immagini
	- base: percorso base
*/
function preloadImages(oData) {
	try {
		if (document.images && oData.list) {
			if (!document.MM_p) document.MM_p = new Array();
			var i, j = document.MM_p.length;
			for (i = 0; i < oData.list.length; i++) {
				if (oData.list[i].indexOf('#')!=0) {
					document.MM_p[j] = new Image;
					document.MM_p[j++].src = oData.base + oData.list[i];
				}
			}
		}
	} catch(e) {}
}

/*

*/
function validateElement(oElement, oOptions, bValue) {
	if (!oElement) return false;
	var test = new Array();
	switch (oElement.type) {
		case 'hidden':
		case 'text':
		case 'textarea':
		case 'password':
			test.push(oElement.value);
			break;
		case 'select-one':
			if (bValue) test.push(oElement.options[oElement.selectedIndex].text);
			else test.push(oElement.options[oElement.selectedIndex].value);
			break;
		case 'select-multiple':
			test = new Array();
			for (var i = 0; i < oElement.options.length; ++i) {
				if (oElement.options[i].selected) {
					if (bValue) test.push(oElement.options[i].text);
					else test.push(oElement.options[i].value);
				}
			}
			break;
		case 'checkbox':
		case 'radio':
			if (bValue) test.push(oElement.checked ? oElement.value : '');
			else test.push(oElement.checked);
		break;
			default:
			return 'unknown';
	}
	for (var i = 0; i < test.length; ++i) {
//		alert('test[' + i + ']=' + test[i]);
//		alert(test[i] == '');
		if (test[i] == '' && !oOptions.allowEmpty) {
			if (oElement.type == 'checkbox' || oElement.type == 'radio') return 'not_checked';
			else return 'empty';
		}
		if (oOptions.strictCompare == true) {
			if (oOptions.intMin && test[i] <= parseInt(oOptions.intMin)) return 'lower';
			if (oOptions.intMax && test[i] >= parseInt(oOptions.intMax)) return 'higher';
			if (oOptions.floatMin && test[i] <= parseFloat(oOptions.floatMin)) return 'lower';
			if (oOptions.floatMax && test[i] >= parseFloat(oOptions.floatMax)) return 'higher';
			if (oOptions.minLength >= String(test[i]).length) return 'longer';
			if (oOptions.maxLength <= String(test[i]).length) return 'shorter';
		} else {
			if (oOptions.intMin && test[i] < parseInt(oOptions.intMin)) return 'lower';
			if (oOptions.intMax && test[i] > parseInt(oOptions.intMax)) return 'higher';
			if (oOptions.floatMin && test[i] < parseFloat(oOptions.floatMin)) return 'lower';
			if (oOptions.floatMax && test[i] > parseFloat(oOptions.floatMax)) return 'higher';
			if (oOptions.minLength > String(test[i]).length) return 'longer';
			if (oOptions.maxLength < String(test[i]).length) return 'shorter';
		}
		if (oOptions.regexp) {
			try {
				if (!oOptions.regexp.exec(test[i])) return 'invalid';
			} catch(e) {
				return false;
			}
		}
		if (oOptions.compare) {
			if (test[i] != MMJ_getElementValue(oOptions.compare)) return 'mismatch';
		}
	}
	return true;
}

function getLabelFor(oElement) {
	var labels = oElement.ownerDocument.getElementsByTagName('label');
	var i = 0;
	try {
		do {
			if (labels[i].attributes.getNamedItem('for').value == oElement.id) return labels[i];
			i++;
		} while (i < labels.length);
	} catch(e) {}
	return null;
}


/**
*
*  Javascript sprintf
*  http://www.webtoolkit.info/
*
*
**/

sprintfWrapper = {

	init : function () {

		if (typeof arguments == "undefined") { return null; }
		if (arguments.length  <  1) { return null; }
		if (typeof arguments[0] != "string") { return null; }
		if (typeof RegExp == "undefined") { return null; }

		var string = arguments[0];
		var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosuxX])))/g);
		var matches = new Array();
		var strings = new Array();
		var convCount = 0;
		var stringPosStart = 0;
		var stringPosEnd = 0;
		var matchPosEnd = 0;
		var newString = '';
		var match = null;

		while (match = exp.exec(string)) {
			if (match[9]) { convCount += 1; }

			stringPosStart = matchPosEnd;
			stringPosEnd = exp.lastIndex - match[0].length;
			strings[strings.length] = string.substring(stringPosStart, stringPosEnd);

			matchPosEnd = exp.lastIndex;
			matches[matches.length] = {
				match: match[0],
				left: match[3] ? true : false,
				sign: match[4] || '',
				pad: match[5] || ' ',
				min: match[6] || 0,
				precision: match[8],
				code: match[9] || '%',
				negative: parseInt(arguments[convCount])  <  0 ? true : false,
				argument: String(arguments[convCount])
			};
		}
		strings[strings.length] = string.substring(matchPosEnd);

		if (matches.length == 0) { return string; }
		if ((arguments.length - 1)	<  convCount) { return null; }

		var code = null;
		var match = null;
		var i = null;

		for (i=0; i < matches.length; i++) {

			switch (matches[i].code) {
				case '%':
					substitution = '%';
					break;
				case 'b':
					matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
					substitution = sprintfWrapper.convert(matches[i], true);
					break;
				case 'c':
					matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
					substitution = sprintfWrapper.convert(matches[i], true);
					break;
				case 'd':
					matches[i].argument = String(parseInt(matches[i].argument));
					substitution = sprintfWrapper.convert(matches[i]);
					break;
//				case 'e':
//					matches[i].argument = String(parseFloat(matches[i].argument));
//					substitution = sprintfWrapper.convert(matches[i]);
//					break;
				case 'f':
					matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
					substitution = sprintfWrapper.convert(matches[i]);
					break;
				case 'o':
					matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
					substitution = sprintfWrapper.convert(matches[i]);
					break;
				case 's':
					matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
					substitution = sprintfWrapper.convert(matches[i], true);
					break;
				case 'u':
					matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
					substitution = sprintfWrapper.convert(matches[i]);
					break;
				case 'x':
					matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
					substitution = sprintfWrapper.convert(matches[i]);
					break;
				case 'X':
					matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
					substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
					break;
				default:
					substitution = matches[i].match;
			}

			newString += strings[i];
			newString += substitution;

		}
		newString += strings[i];

		return newString;

	},

	convert : function(match, nosign){
		if (nosign) {
			match.sign = '';
		} else {
			match.sign = match.negative ? '-' : match.sign;
		}
		var l = match.min - match.argument.length + 1 - match.sign.length;
		var pad = new Array(l  <  0 ? 0 : l).join(match.pad);
		if (!match.left) {
			if (match.pad == "0" || nosign) {
				return match.sign + pad + match.argument;
			} else {
				return pad + match.sign + match.argument;
			}
		} else {
			if (match.pad == "0" || nosign) {
				return match.sign + match.argument + pad.replace(/0/g, ' ');
			} else {
				return match.sign + match.argument + pad;
			}
		}
	}
}

sprintf = sprintfWrapper.init;

/**
*/
function padLeft(s, c, n) {
	if (! s || ! c || s.length >= n) {
		return s;
	}
	var max = (n - s.length)/c.length;
	for (var i = 0; i < max; i++) {
		s = c + s;
	}
	return s;
}

function padRight(s, c, n) {
	if (! s || ! c || s.length >= n) {
		return s;
	}
	var max = (n - s.length)/c.length;
	for (var i = 0; i < max; i++) {
		s += c;
	}
	return s;
}

function getPageSize() {

	 var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight) { // all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}

	return [pageWidth,pageHeight];
}

function debug(o, autoshow) {
	var s = new String('');
	for (var prop in o) {
		s += prop + ':' + o[prop] + '\n';
	}
	if (autoshow || autoshow == undefined) alert(s);
	return s;
}

