
function openMenu(idmenu) {
	for (i=1;i<=4;i++) {
		if (i != idmenu) { ypSlideOutMenu.hideMenu('menu'+i); }
	}
	ypSlideOutMenu.showMenu('menu'+idmenu);
}

function include_page(url,pars,elem) {
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                document.getElementById(elem).innerHTML = request.responseText;
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }
}

function include_js(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

function my_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){xScroll = document.body.scrollWidth;yScroll = document.body.scrollHeight;} else {xScroll = document.body.offsetWidth;yScroll = document.body.offsetHeight;}arrayPageSize = new Array(xScroll,yScroll);return arrayPageSize;};

function remove(){$('.MYW_overlay').remove();$('.MYW_window').remove();}

function my_window(url,w){arrsize = my_getPageSize();if(!w) w=800;var top = document.documentElement.scrollTop || window.pageYOffset || 0;if($('.MYW_overlay')) remove();d = document.createElement('div');document.body.appendChild(d);d.className = 'MYW_overlay';$('.MYW_overlay').css('height',arrsize[1]);d.innerHTML = "<!--[if lte IE 6.5]><iframe></iframe><![endif]-->";d.onclick=remove;d2 = document.createElement('div');document.body.appendChild(d2);d2.className = 'MYW_window';$(".MYW_window").css({width:w+"px",top:(top+20)+"px",left:((arrsize[0]/2)-(w/2))+"px"});d3 = document.createElement('div');d3.className='MYW_content';d2.appendChild(d3); include_page(url,'',d3);}

function OpenWin(strLink){
         window.open(strLink,'action','toolbar=no,width=400,height=218,scrollbars=yes,status=no')
}
function OpenReg(strLink){
         window.open(strLink,'action','toolbar=yes,width=760,height=500,scrollbars=yes,status=no')
}

function CloseWin() {
        window.open('','_parent','');
        window.close();
}

function bookmarksite(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function getCookie(check_name) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}				
				
function setCookie(name, value, expires) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ";path=/" ) + ( ";domain=.vistoepreso.it" ); 

}

function showBox(element) {
        if (document.getElementById(element).style.display == 'none') {
                document.getElementById(element).style.display = 'block';
        } else {
                document.getElementById(element).style.display = 'none';
        }
}

function verifyEmail(s) {
        var chrs = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-@';
        var sLen = s.length; var i=0, c=0, cCnt=0, step=0;
        if (sLen < 6) return false;
        if (s.indexOf('@.')>=0) return false;
        if (s.indexOf('.@')>=0) return false;
        while (i < sLen){
                c=s.charAt(i);
                if (!(chrs.indexOf(c)>=0 || (c=='_' && step<1))) return false;
                if (c=='.') { if (cCnt<1) return false; cCnt=0; }
                if (c=='@') { if (step>0) return false; if (cCnt<1) return false; step++; cCnt=0; }
                cCnt=cCnt+1; i++;
        }
        if (cCnt < 3 || cCnt > 5 || step==0 || (s.indexOf(".")<0) ) return false;
        return true;
}

function verifyNumber(s) {
        var chrs = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-@';
        var sLen = s.length; var i=0, c=0;
        if (sLen < 1) return false;
        if (s.indexOf('.@')>=0) return false;
        while (i < sLen){
                c=s.charAt(i);
                if (chrs.indexOf(c)>=0) return false;
                i++;
        }
        return true;
}

function appendOption(id,string,val) {

	var elOptNew = document.createElement('option');
	elOptNew.text = string;
	elOptNew.value = val;
	var elSel = document.getElementById(id);

	try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	}
	catch(ex) {
		elSel.add(elOptNew); // IE only
	}

}

function removeOptions(id) {

	var elSel = document.getElementById(id);
	while (elSel.length > 0) {
		elSel.remove(elSel.length - 1);
	}

}

function Access(form) {

	document.getElementById('loading').style.display = '';
        email = document.getElementById(form).email.value;
        pwd = document.getElementById(form).password.value;
        rememberme = document.getElementById(form).rememberme.value;
	var url = '/bin/users/login.php';	
        var pars = 'email='+email+'&password='+pwd+'&rememberme='+rememberme;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                Login(request.responseText);
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }
}

function CheckAccess(email,pwd) {

	document.getElementById('errorelogin').style.display = 'none';
	var url = '/bin/users/login.php';	
        var pars = 'email='+email+'&password='+pwd;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
		if (request.responseText == 0) {
			document.getElementById('errorelogin').innerHTML = '<strong>I dati inseriti non corrispondono ad alcun utente registrato</strong>';
			document.getElementById('errorelogin').style.display = '';
		}
       	}
       	function reportError(request)
       	{
           	alert('Sorry. There was an error.');
       	}
}

function Login(userid) {

	document.getElementById('loading').style.display = 'none';
	if (userid == 0) {
		document.getElementById('errorelogin').innerHTML = '<strong>Dati inseriti errati</strong>';
		document.getElementById('errorelogin').style.display = '';
	} else {
		document.location.href = '/bin/users/main.php?id_user='+userid;
	}

}

function ComboItem(ID, ParentID, Value, Sel) {

        this.ID = ID
        this.Value = Value
        this.ParentID = ParentID
        this.Sel = Sel
}

function FillCombo(cmbArray, cmbObj, cmbKey, ind) {

	if (ind) { document.getElementById(ind).style.display = ''; }
        cmbObj.options.length = 0;
	q = 1;
        for (var i = 0; i < cmbArray.length; i++) {
                if (cmbArray[i].ParentID == cmbKey) {
                        cmbObj.options[q] = new Option(cmbArray[i].Value, cmbArray[i].ID, cmbArray[i].Sel);
			q++;
                }
        }
	if (ind) { document.getElementById(ind).style.display = 'none'; }
}

function ShowCities(strarray) {

        removeOptions('id_city');
        var num = 0;
        var cities = strarray.split(",");
        while (num < cities.length - 1) {
                var elem = cities[num].split("=");
                appendOption('id_city',elem[0],elem[1]);
                num += 1;
        }

}

function ChangeCities(prov,ind) {

	document.getElementById(ind).style.display = '';
        var url = '/bin/city.php';
        var pars = 'id_prov='+prov;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                ShowCities(request.responseText);
		document.getElementById(ind).style.display = 'none';
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }

}

function ShowCategories(strarray) {

        removeOptions('id_category');
        var num = 0;
        var categories = strarray.split(",");
        while (num < categories.length - 1) {
                var elem = categories[num].split("=");
                appendOption('id_category',elem[0],elem[1]);
                num += 1;
        }

}

function ChangeCategories(macro,ind) {

	document.getElementById(ind).style.display = '';
        var url = '/bin/categories.php';
        var pars = 'id_macro='+macro;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
		document.getElementById('category').style.display = '';
                ShowCategories(request.responseText);
		document.getElementById(ind).style.display = 'none';
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }

}

function ShowModels(strarray) {

        removeOptions('id_model');
        var num = 0;
        var models = strarray.split(",");
        while (num < models.length - 1) {
                var elem = models[num].split("=");
                appendOption('id_model',elem[0],elem[1]);
                num += 1;
        }

}

function ChangeModels(brand,ind) {

	document.getElementById(ind).style.display = '';
        var url = '/bin/models.php';
        var pars = 'id_brand='+brand;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                ShowModels(request.responseText);
		document.getElementById(ind).style.display = 'none';
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }

}

function ShowZones(strarray) {

        removeOptions('id_zone');
        var num = 0;
        var zones = strarray.split(",");
        while (num < zones.length - 1) {
                var elem = zones[num].split("=");
                appendOption('id_zone',elem[0].replace('(',' ('),elem[1]);
                num += 1;
        }

}

function ChangeZones(city) {

        var url = '/bin/zone.php';
        var pars = 'id_city='+city;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                ShowZones(request.responseText);
        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }

}

function numbersonly(myfield, e, dec) {

var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;

}

function decimalonly(myfield, e) {

var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789,").indexOf(keychar) > -1))
   return true;

else
   return false;

}

function miniPanel() {

        cityname = document.getElementById('city_name').innerHTML;
        if (cityname.length - 44 > 15) {
                document.getElementById('city_name').className = 'citysmall';
        }

	panel = '<div class="minipanel">';
	bookmark = getCookie('VEP_BOOKMARK');
	if (bookmark) {
		bm_num = bookmark.split(',');
		bmn = bm_num.length - 1;
		panel += '<div class="icolisting papreferiti"><a href="javascript:Modalbox.show(\'/bin/bookmark_list.php?list='+bookmark+'\', {title: \'I miei preferiti\', width: 870});">I miei preferiti ('+bmn+')</a></div>';
	} else {
		panel += '<div class="icolisting papreferiti"><a href="javascript:void(0);">I miei preferiti (0)</a></div>';
	}
	comp = getCookie('VEP_COMP');
	if (comp) {
		comp_num = comp.split(',');
		con = comp_num.length - 1;
		panel += '<div class="icolisting palista"><a href="javascript:Modalbox.show(\'/bin/compare_list.php?list='+comp+'\', {title: \'Lista confronto\', width: 970});">Lista confronto ('+con+')</a></div>';
	} else {
		panel += '<div class="icolisting palista"><a href="javascript:void(0);">Lista confronto (0)</a></div>';
	}

	var url = '/bin/checkuser.php';	
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: '', onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
		if (request.responseText == 0) {
			document.getElementById('minipanel').innerHTML = panel+'<div class="icolisting palogin"><a href="javascript:Modalbox.show(\'/login.html\', {title: \'Login\', width: 700});">Login</a></div></div>';
		} else {
			document.getElementById('minipanel').innerHTML = panel+'<div class="icolisting palogin"><a href="/bin/users/main.php?id_user='+request.responseText+'"><strong>Amministrazione</strong></a></div><div  class="logout"><a href="/bin/users/logout.php">Logout</a></div></div>';
		}
       	}
       	function reportError(request)
       	{
           	alert('Sorry. There was an error.');
       	}
}

function Compare(id_ad,id_macro) {

        comp = getCookie('VEP_COMP');
        if (!comp) { comp = ''; }
	num_comp = comp.split('-'+id_macro+',').length - 1;
	if (num_comp >= 4) {
		alert('Il numero massimo di annunci da confrontare per questa categoria e\' stato raggiunto!');
	} else {
        	if (comp.indexOf(id_ad+'-'+id_macro) < 0) {
                	setCookie('VEP_COMP',comp+id_ad+'-'+id_macro+',',24);
			if (document.getElementById('ad'+id_ad)) {
			moveFX(document.getElementById('ad'+id_ad),document.getElementById('minipanel'));
			}
        	}
        	document.getElementById('compare'+id_ad).className = 'confrontaok';
        	miniPanel();
	}

}

function delCompare(id_ad,id_macro) {

        comp = getCookie('VEP_COMP');
        if (comp.indexOf(id_ad+'-'+id_macro) >= 0) {
                setCookie('VEP_COMP',comp.replace(id_ad+'-'+id_macro+',',''),24);
        	document.getElementById('ad'+id_ad).style.display = 'none';
		if (document.getElementById('compare'+id_ad)) {
        		document.getElementById('compare'+id_ad).className = 'confrontaok';
		}
        }
	num_comp = comp.split('-'+id_macro+',').length - 2;
	if (num_comp <= 1) {
		if (document.getElementById('comp'+id_macro)) {
        		document.getElementById('comp'+id_macro).style.display = 'none';
		}
	}
	if (num_comp == 0) {
        	document.getElementById('cat'+id_macro).style.display = 'none';
		Modalbox.resize(0,-82);
	}
        miniPanel();

}

function clearCompare(id_macro) {

        comp = getCookie('VEP_COMP');
	comp_list = comp.split(',');
	i = 0;
	newcomp = '';
	while (i < comp_list.length - 1) {
        	if (comp_list[i].indexOf('-'+id_macro) < 0) {
			newcomp += comp_list[i]+',';
		}
		i++;
        }
        setCookie('VEP_COMP',newcomp,24);
        document.getElementById('cat'+id_macro).style.display = 'none';
	Modalbox.resize(0,-82);
        miniPanel();

}

function Bookmark(id_ad) {

        bm = getCookie('VEP_BOOKMARK');
        if (!bm) { bm = ''; }
        if (bm.indexOf(id_ad) < 0) {
                setCookie('VEP_BOOKMARK',bm+id_ad+',',720);
                document.getElementById('bookmark'+id_ad).className = 'preferitiok';
        } else {
                setCookie('VEP_BOOKMARK',bm.replace(id_ad+',',''),720);
                document.getElementById('bookmark'+id_ad).className = 'preferiti';
        }
        miniPanel();
}

function delBookmark(id_ad) {

        bm = getCookie('VEP_BOOKMARK');
        if (!bm) { bm = ''; }
        if (bm.indexOf(id_ad) >= 0) {
                setCookie('VEP_BOOKMARK',bm.replace(id_ad+',',''),720);
		if (document.getElementById('bookmark'+id_ad)) {
                	document.getElementById('bookmark'+id_ad).className = 'preferiti';
		}
        	document.getElementById('ad'+id_ad).style.display = 'none';
        }
        miniPanel();
}

function showProducts(id_company) {

        var url = '/bin/users/products.php';
        var pars = 'id_company='+id_company;
        var myAjax = new Ajax.Request(url, {method: 'post', parameters: pars, onFailure: reportError,  onComplete: showResponse });
        function showResponse(request) {
             //alert(request.responseText);
                var prod = request.responseText.split(",");
                num = 0;
                while (num < prod.length) {
                        if (prod[num] == 2) { document.getElementById('banner_text').style.display = ''; }
                        if (prod[num] == 3) { document.getElementById('banner_graph').style.display = ''; }
                        if (prod[num] == 16) { document.getElementById('minisite').style.display = ''; }
                        if (prod[num] == 'print') {
                                document.getElementById('import').style.display = '';
                                document.getElementById('export').style.display = '';
                        }
                        num++;
                }

        }
        function reportError(request)
        {
            alert('Sorry. There was an error.');
        }

}

function disclaimer(url) {

        disc = getCookie('VEP_DISCLAIM');
        if (disc == 'incontri') {
		document.location.href = url;
	} else {
        	window.open('/disclaimer.php?url='+url,'Disclaimer','toolbar=no,width=600,height=500,scrollbars=no,status=no')
	}

}
