function highlightCalendarCell(element) {
    $(element).style.border = '1px solid #999999';
}

function resetCalendarCell(element, color) {
    $(element).style.border = '1px solid #000000';
}

function startCalendar(month, year) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    } 
    new Ajax.Updater('calendarInternal', str + 'data/ajax/calendar.php', {method: 'post', postBody: 'action=startCalendar&month='+month+'&year='+year+''});
}

function startUpload(idnotizia) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    } 
    new Ajax.Updater('uploadInternal', str + 'data/ajax/upload.php', {method: 'post', postBody: 'action=startUpload&idnotizia='+idnotizia});
}

function startPoll(voto) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    } 
    new Ajax.Updater('pollInternal', str + 'data/ajax/poll.php', {method: 'post', postBody: 'action=startPoll&voto='+voto});
}

function checkUser(username) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    } 
    new Ajax.Updater('checkuser', str + 'data/ajax/checkuser.php', {method: 'post', postBody: 'action=checkUser&username='+username});
}

function String_trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function String_ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function String_rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function roundTo(value, decimalpositions)
{
    var i = value * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
} 

function CheckAlfanum(stringa) {
    var str = '';
    var espressione = /^[A-Za-z0-9]/;
    for( i=0; i < stringa.length; i++ ) {
        if( espressione.test(stringa.charAt(i)) || stringa.charAt(i)=='-' || stringa.charAt(i)=='_') {
            str += stringa.charAt(i);
        } 
    }                                       
    return str;        
}

function CheckAlfanumHTML(stringa) {
    var str = '';
	var chrprev = '';
    var espressione = /^[A-Za-z0-9]/;
    for( i=0; i < stringa.length; i++ ) {
        if( espressione.test(stringa.charAt(i)) || stringa.charAt(i)=='-' || stringa.charAt(i)=='_' || stringa.charAt(i)==' ' || stringa.charAt(i)=='\'') {
			if (stringa.charAt(i)==' ' || stringa.charAt(i)=='\'') {
				if (chrprev!='-') str += '-';
				chrprev='-';
			} else {
				str += stringa.charAt(i);
				chrprev=stringa.charAt(i);
			}
        } 
    }                                       
    return str;        
}

function CheckAlfanumHTMLdot(stringa) {
    var str = '';
	var chrprev = '';
    var espressione = /^[A-Za-z0-9]/;
    for( i=0; i < stringa.length; i++ ) {
        if( espressione.test(stringa.charAt(i)) || stringa.charAt(i)=='-' || stringa.charAt(i)=='_' || stringa.charAt(i)==' ' || stringa.charAt(i)=='\'' || stringa.charAt(i)=='.') {
			if (stringa.charAt(i)==' ' || stringa.charAt(i)=='\'') {
				if (chrprev!='-') str += '-';
				chrprev='-';
			} else {
				str += stringa.charAt(i);
				chrprev=stringa.charAt(i);
			}
        } 
    }                                       
    return str;        
}

function CheckNum(stringa) {
    var str = '';
    var espressione = /^[0-9]/;
    for( i=0; i < stringa.length; i++ ) {
        if( espressione.test(stringa.charAt(i)) ) {
            str += stringa.charAt(i);
        } 
    }                                       
    return str;        
}

function CheckFloat(stringa) {
    var str = '';
    var espressione = /^[0-9]/;
    for( i=0; i < stringa.length; i++ ) {
        if( espressione.test(stringa.charAt(i)) || stringa.charAt(i)==',') {
            if (stringa.charAt(i)=='.') {
                str += ',';
            } else {
                str += stringa.charAt(i);
            }
        } 
    }                                       
    return str;        
}

function ConvertStringFloat(stringa) {
    var str = '';
    stringa=stringa+'';
    for( i=0; i < stringa.length; i++ ) {
            if (stringa.charAt(i)=='.') {
                str += ',';
            } else {
                str += stringa.charAt(i);
            }         
    }                                       
    return str;        
}

function CheckMail(mail) {
    var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
    if (!email_reg_exp.test(mail) || mail == '' || mail == null) {
        return false;
    } else {
        return true;
    }
}

function startBuy(id, codice, qta, remove, opzione) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    } 
    new Ajax.Updater('buyInternal'+id, str + 'data/ajax/buy.php', {method: 'post', postBody: 'action=startBuy&id='+id+'&codice='+codice+'&qta='+qta+'&opzione='+opzione+'&remove='+remove});
}

function startInfo(email, nome, mobile, richiesta, id, invia) {
	stringa = location.href;
    str='';
    bs=0;
    i=0;
    while (bs<3) {
        str += stringa.charAt(i);
        if (stringa.charAt(i)=='/') bs++;
        i++;
    }
    new Ajax.Updater('infoInternal'+id, str + 'data/ajax/inforequest.php', {method: 'post', postBody: 'action=startInfo&email='+email+'&nome='+nome+'&mobile='+mobile+'&richiesta='+richiesta+'&id='+id+'&invia='+invia});
}

function _utf8_encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	}

function _utf8_decode(utftext) {
		var string = "";
		var i = 0;
		var c = 0;
		var c1 = 0
		var c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}

var temp_menu;
var temp_sottomenu;
var temp_sotto_sottomenu;
document.observe('dom:loaded', function() {
	$$('.livello_2').each(Element.hide)
	$$('.livello_3').each(Element.hide);
	try {
		$$('.livello_2')[0].show();
	} catch (error) {}
	try {
		$$('li.menu_3.attivo1')[0].ancestors()[0].show();
		temp_sotto_sottomenu=$$('li.menu_3.attivo1')[0].ancestors()[0];
	} catch (error) {}
	try {
        $$('li.menu_2.attivo1')[0].ancestors()[0].show();
        if ($$('li.menu_2.attivo1')[0].ancestors()[0].id!='gruppo_0') temp_sotto_sottomenu=$$('li.menu_2.attivo1')[0].ancestors()[0];
    } catch (error) {}
	
	$$('.menu_2').each(function(item) {
		item.firstDescendant().observe('click', function(event) {
			if (temp_sotto_sottomenu!=Event.element(event).next()) {
				if ((temp_sotto_sottomenu!=null)) {
					Effect.BlindUp(temp_sotto_sottomenu);
				}
				try {
						Effect.BlindUp(temp_sottomenu);
					}catch (error) {	
				}
				Effect.BlindDown(Event.element(event).next());
				temp_sotto_sottomenu=Event.element(event).next();
				temp_sottomenu=null;
				if (Event.element(event).href.charAt(Event.element(event).href.length-1)=='#') return false;
				
			}
		});				
	});

	$$('.menu_1').each(function(item) {
		if(item!=$$('.menu_1')[0]) {
			item.firstDescendant().observe('click', function(event) {
				if (temp_sottomenu != Event.element(event).next()) {
					if ((temp_sottomenu != null) && (temp_sottomenu != $$('.livello_2')[0])) {
						Effect.BlindUp(temp_sottomenu);
					}
					try {
						Effect.BlindUp(temp_sotto_sottomenu);
					} 
					catch (error) {
					}
					Effect.BlindDown(Event.element(event).next());
					temp_sottomenu = Event.element(event).next();
					temp_sotto_sottomenu=null;
					if (Event.element(event).href.charAt(Event.element(event).href.length - 1) == '#') 
						return false;
				}
			});
		}			
	});	
	
	/*$$('.menu_3 a').each(function(item) {
		item.observe('mouseover', function(event) {
            Event.element(event).setStyle({'color':'#FBEC01',
			                               'text-shadow':'1px 1px 1px #999999'
										 });
			var current=Event.element(event).href;
			Event.element(event).ancestors()[1].childElements().each(function(item2) {
				if (item2.childElements()[0].href==current) {
					item2.childElements()[0].setStyle({'color':'#FBEC01',
                                           'text-shadow':'1px 1px 1px #999999'
                                         });
				}
			});
		});
		item.observe('mouseout', function(event) {
            Event.element(event).setStyle({'color':'',
                                           'text-shadow':''
                                         });
			var current=Event.element(event).href;
            Event.element(event).ancestors()[1].childElements().each(function(item2) {
                if (item2.childElements()[0].href==current) {
                    item2.childElements()[0].setStyle({'color':'',
                                           'text-shadow':''
                                         });
                }
            });
        });	
	});*/
	
	$$('.menu_2 a').each(function(item) {
        item.observe('mouseover', function(event) {
			var current=Event.element(event).href;
            if (current.charAt(current.length-1) != '#') {
				Event.element(event).setStyle({
					'color': '#FBEC01',
					'textShadow':'1px 1px 1px #333333',
					'filter:progid':'DXImageTransform.Microsoft.Shadow(color=#333333,direction=45)'
				});
			}
            Event.element(event).ancestors()[1].childElements().each(function(item2) {
                if ((current.charAt(current.length-1)!='#') && (item2.childElements()[0].href==current)) {
                    item2.childElements()[0].setStyle({'color':'#FBEC01',
                                           'textShadow':'1px 1px 1px #333333',
										   'filter':'progid:DXImageTransform.Microsoft.Shadow(color=#333333,direction=45,strength=1)'
                                         });
                }
            });
        });
        item.observe('mouseout', function(event) {
            Event.element(event).setStyle({'color':'',
                                           'text-shadow':'',
										   'filter':''
                                         });
            var current=Event.element(event).href;
            Event.element(event).ancestors()[1].childElements().each(function(item2) {
                if (item2.childElements()[0].href==current) {
                    item2.childElements()[0].setStyle({'color':'',
                                           'text-shadow':'',
                                           'filter':''										   
                                         });
                }
            });
        }); 
    });
	
	$$('table.prodotti-ico td table tr+tr a').each(function(item) {
		var temp_href=item.href;
		item.href='#';
		item.observe('click', function (event) {		
			url=temp_href.split('/');
			page_ref=url[url.length-1];
			if (page_ref!='') {
				var url ='http://'+location.host+'/data/ajax/page.php?open='+page_ref;
				new Ajax.Request(url, {
					method: 'get',
					onSuccess: function(response) {
      					$('popup_content').innerHTML=response.responseText;
  					}		
				});
			}
		},false);		
		new Popup('popup',item,{modal:true});
	});
	
});

