// ############# - GETELEMENTBYID - #############
function getObj(itemId) {
	return document.getElementById(itemId);
}
// ################### - FINE - ###################

var effettua_login = false;

// ################### - FINE - ###################

// ############# - BROWSER DETECT - #############
// BROWSER TYPE		= BrowserDetect.browser
// BROWSER VERSION	= BrowserDetect.version
// OPERATVE SYSTEM	= BrowserDetect.OS
// ################################################
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

if (BrowserDetect.browser == 'Explorer')  {
	if (BrowserDetect.version == 7) {
		var BrowserType = 'msie7';
	} else {
		var BrowserType = 'msie';
	}
} else if (BrowserDetect.browser == 'Firefox') {
	var BrowserType = 'firefox';
} else {
	var BrowserType = '';
}
// ################### - FINE - ###################



function goTo( url ) {
	window.location.href = url;
}

function divAlertAbsolute() { 
	var itemAlertDivObj = document.getElementById('div_alert_absolute');
	itemAlertDivObj.style.display = '';
	var	div_width = itemAlertDivObj.clientWidth;
	itemAlertDivObj.style.display = 'none';
	
	
	if (div_width < document.body.clientWidth) {
	} else {
		if (userBrowser == 'ie') {
			itemAlertDivObj.style.left = '0px';
		}
	}
}



function debug(str)
{
	if (document.getElementById('debug'))
	{
		document.getElementById('debug').innerHTML += '<br />' + str;
	}
}

function doLogin(theForm)
{
	if (theForm.login.value == '' || theForm.cpwd.value == '')
	{
		alert('Inserire email e password.');
		return false;
	}
	
	pwdhash = hex_md5(theForm.cpwd.value);
	theForm.cpwd.value = '';
	theForm.hpwd.value = pwdhash;
	return true;
}

function MultiDimensionalArray(iRows,iCols)
{
	var i;
	var j;
	var a = new Array(iRows);
	for (i=0; i < iRows; i++)
	{
		a[i] = new Array(iCols);
		for (j=0; j < iCols; j++)
		{
		   a[i][j] = "0";
		}
	}
	return(a);
}

function printArray( myArray )
{
	var str = '';
	for ( i=0; i<myArray.length; i++)
	{
		for ( j=0; j < myArray[i].length; j++)
		{
			str += '[' + myArray[i][j] + ']';
		}
		str += '<br />';
	}
	debug(str);
}

function bookmarksite(title, url){
	if (document.all) {
		window.external.AddFavorite(url, title);
	} else if (window.sidebar) { 
		window.sidebar.addPanel(title, url, "")
	}
}

function clearSelect( oSelect, disable ) {
	if ( oSelect ) {
		n_options = oSelect.options.length;
		for(var i=n_options-1; i>=0; i--) {
			oSelect.options[i] = null;
		}
	
		oSelect.disabled = disable;
	}
}

function fillSelect( oSelect, oList, insert_default_option, first_option_label, first_option_value ) {
	/*
	if ( oSelect && oList ) {
		
		if ( insert_default_option ) {
			oSelect.options[0] = new Option(first_option_label, first_option_value);
		}
		
		for(var i=0; i<oList.length; i++) {
			var value = oList[i].getElementsByTagName('id').item(0).firstChild.data;
			var label = oList[i].getElementsByTagName('nome').item(0).firstChild.data;
			//alert('value = ' + value + ' - label = ' + label);
			oSelect.options[oSelect.options.length] = new Option(label, value);
		}
	}
	*/
	fillSelectCustom( oSelect, oList, 'id', 'nome', insert_default_option, first_option_label, first_option_value );
	
}

function fillSelectCustom( oSelect, oList, field_value, field_label, insert_default_option, first_option_label, first_option_value ) {
	
	if ( oSelect && oList ) {
		
		if ( insert_default_option ) {
			oSelect.options[0] = new Option(first_option_label, first_option_value);
		}
		
		for(var i=0; i<oList.length; i++) {
			var value = oList[i].getElementsByTagName(field_value).item(0).firstChild.data;
			var label = oList[i].getElementsByTagName(field_label).item(0).firstChild.data;
			//alert('value = ' + value + ' - label = ' + label);
			oSelect.options[oSelect.options.length] = new Option(label, value);
		}
	}
	
}

function appendOptionSelect( oSelect, option_label, option_value ) {
	
	if ( oSelect ) {
		var objOption = new Option(option_label, option_value);
		oSelect.options.add(objOption);
		
		return objOption;
	}
	
	return false;
}

/**************************************
Controllo del Codice Fiscale
Linguaggio: JavaScript
***************************************/
function controllaCF(cf) {
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' ) {
		return false;
	} 
	cf = cf.toUpperCase();
	if( cf.length != 16 && cf.length != 11 ) {
		return false;
	}
	
	return true;
	
	/*
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ) {
		if( validi.indexOf( cf.charAt(i) ) == -1 ) {
			return false;
		}
	} 
	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 false;
	}
	
	return true;  
	*/
}


function swapImage(id_img, new_img_url) {
	
	document.getElementById(id_img).src = new_img_url;
	
}


if (document.all) {
	var userBrowser = 'ie';
} else {
	var userBrowser = 'moz';
}

function openContattaInserz(idItem) {
	//itemObj = document.getElementById(idItem);
	//frameObj = document.getElementById('frame_id_form');
	
	window.scrollTo(1024, 0)
	
	document.getElementById('frame_id_form').style.display = '';
	window.open('/contatta_inserzionista/?ida='+ idItem ,'frame_name_form');
}

// presa da http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_number_format/
function number_format( number, decimals, dec_point, thousands_sep ) {
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: number_format(1234.5678, 2, '.', '');
    // *     returns 1: 1234.57
 
    var i, j, kw, kd, km;
 
    // input sanitation & defaults
    if( isNaN(decimals = Math.abs(decimals)) ){
        decimals = 2;
    }
    if( dec_point == undefined ){
        dec_point = ",";
    }
    if( thousands_sep == undefined ){
        thousands_sep = ".";
    }
 
    i = parseInt(number = (+number || 0).toFixed(decimals)) + "";
 
    if( (j = i.length) > 3 ){
        j = j % 3;
    } else{
        j = 0;
    }
 
    km = (j ? i.substr(0, j) + thousands_sep : "");
    kw = i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep);
    kd = (decimals ? dec_point + Math.abs(number - i).toFixed(decimals).slice(2) : "");
 
 
    return km + kw + kd;
}

function openNewWindow(URL) {
	newWin = window.open(URL ,'sitoAgenzia', 'width=800, height=600, scrollbars=yes, resizable=yes, status=no, location=no, toolbar=no');
	newWin.focus();
}

function searchCodeFocus(itemObj) {
	var defaultValue = 'codice annuncio';
	if ( (itemObj.value == '') || (itemObj.value == defaultValue) ) {
		itemObj.value = '';
	}
	if (itemObj.style.backgroundColor != '') {
		itemObj.style.backgroundColor = '';
	}
}

function searchCodeBlur(itemObj) {
	var defaultValue = 'codice annuncio';
	if ( (itemObj.value == '') || (itemObj.value == defaultValue) ) {
		itemObj.value = defaultValue;
	}
}
function seachCode(formObj) {
		
	var re = /CU[0-9]{6}/;
	
	if ( !formObj.code.value.match(re) ) {
		formObj.code.style.backgroundColor = '#ffff00';
		alert("Codice Annuncio non valido");
		return false;
	} else {
		formObj.code.style.backgroundColor = '';
		return true;
	}
	
}
function searchRiferimentoFocus(itemObj) {
	var defaultValue = 'riferimento annuncio';
	if ( (itemObj.value == '') || (itemObj.value == defaultValue) ) {
		itemObj.value = '';
	}
	if (itemObj.style.backgroundColor != '') {
		itemObj.style.backgroundColor = '';
	}
}

function searchRiferimentoBlur(itemObj) {
	var defaultValue = 'riferimento annuncio';
	if ( (itemObj.value == '') || (itemObj.value == defaultValue) ) {
		itemObj.value = defaultValue;
	}
}
/*
function seachRiferimento(formObj) {
	if (formObj.riferimento.value == 'riferimento annuncio') {
		formObj.riferimento.style.backgroundColor = '#ffff00';
		alert("Riferimento annuncio non valido");
		return false;
	} else {
		formObj.riferimento.style.backgroundColor = '';
		return true;
	}
}
*/