var mailRe = /\b(^(\S{1,}@).{3,}((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,4}))$)\b/gi;

function message(msg,error){
	if(error==true){
		jQuery.fancybox('<h2 style="color:#e2001a; font-weight:bold;">Errore</h2>'+msg,{hideOnOverlayClick:false,'autoDimensions':false,'width':350,'height':'auto','transitionIn':'none','transitionOut':'none'});
	} else {
		jQuery.fancybox('<h2 style="color:#1fa22e; font-weight:bold;">Messaggio</h2>'+msg,{hideOnOverlayClick:false,'autoDimensions':false,'width':350,'height':'auto','transitionIn':'none','transitionOut':'none'});
	}
}

function est(str){
	str = str.substr(str.lastIndexOf(".")+1,str.length);
	return (str=='gif' || str=="jpg" || str=="png" || str=='jpeg')?true:false;
}

function setStar(id){
	jQuery('#star1').attr('src',id>=1?"images/star.png":"images/emptystar.png");
	jQuery('#star2').attr('src',id>=2?"images/star.png":"images/emptystar.png");
	jQuery('#star3').attr('src',id>=3?"images/star.png":"images/emptystar.png");
	jQuery('#star4').attr('src',id>=4?"images/star.png":"images/emptystar.png");
	jQuery('#star5').attr('src',id==5?"images/star.png":"images/emptystar.png");
}

function resetStar(val){
	jQuery('#star1').attr('src',val>=1?"images/star.png":(val<0.5?"images/emptystar.png":"images/halfstar.png"));
	jQuery('#star2').attr('src',val>=2?"images/star.png":(val<1.5?"images/emptystar.png":"images/halfstar.png"));
	jQuery('#star3').attr('src',val>=3?"images/star.png":(val<2.5?"images/emptystar.png":"images/halfstar.png"));
	jQuery('#star4').attr('src',val>=4?"images/star.png":(val<3.5?"images/emptystar.png":"images/halfstar.png"));
	jQuery('#star5').attr('src',val==5?"images/star.png":(val<4.5?"images/emptystar.png":"images/halfstar.png"));
}

function addVote(i,v){
	src = document.location;
	jQuery.ajax({data:'azione=v&v='+v+'&i='+i,type:'POST',complete:function(xml,status){msg = jQuery.parseJSON(xml.responseText); jQuery('#'+msg.target).outerHTML(msg.html)}});
}

(function(jQuery){  
	jQuery.fn.extend({
		outerHTML : function( value ){
	
			// Replaces the content
			if( typeof value === "string" ){
				var jQuerythis = jQuery(this),
					jQueryparent = jQuerythis.parent();
					
				var replaceElements = function(){
					
					// For some reason sometimes images are not replaced properly so we get rid of them first
					var jQueryimg = jQuerythis.find("img");
					if( jQueryimg.length > 0 ){
						jQueryimg.remove();
					}
					
					var element;
					jQuery( value ).map(function(){
						element = jQuery(this);
						jQuerythis.replaceWith( element );
					})
					
					return element;
					
				}
				
				return replaceElements();
				
			// Returns the value
			}else{
				return jQuery("<div />").append(jQuery(this).clone()).html();
			}
	
		}
	});

})(jQuery);

function prenota1(){
	f = jQuery('#prenota');
	if(jQuery('#emailprenota').val()==''){ alert('Inserire un indirizzo email'); } 
	else { 
		if(!jQuery('#emailprenota').val().match(/\b(^(\S{1,}@).{3,}((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,4}))$)\b/gi)){ 
			alert('Indirizzo email non valido');
		} else {
			if(jQuery('#numbiglietti').val().match(/^[0-9]*$/gi) && parseInt(jQuery('#numbiglietti').val())>0){
				num = parseInt(jQuery('#numbiglietti').val());
				if(num>10){
					alert("Si possono prenotare al massimo 10 biglietti");
				} else {
					jQuery('#prenotatable').hide();
					/*jQuery('#prenota1').parent().parent().remove();
					jQuery('#numbiglietti').parent().parent().remove();*/
					t = '<strong>Inserire i nominativi per i biglietti richiesti.</strong><br /><table border="0" cellpadding="5">';
					for(i=0;i<num;i++){
						t += '<tr><td>Nominativo '+(i+1)+'</td><td><input class="nominativi" maxlength="26" type="text" name="n'+i+'" /></td></tr>';
					}
					
					t += '<tr><td>&nbsp;</td><td><a href="javascript:;" style="background: none repeat scroll 0pt 0pt rgb(250, 232, 0); font-weight: bold; padding: 5px 10px; text-transform: uppercase;" onclick="prenota2();">Richiedi i Biglietti &#9658;</a></td></tr></table>';
					f.append(t);
				}
			} else {
				alert("Inserire il numero di biglietti da prenotare");
			}
		} 
	}
}

function prenota2(){
	qs = '';
	jQuery('.nominativi').each(
		function(){
			if(jQuery(this).val()!='' && jQuery(this).val().length<27){
				qs += "&"+jQuery(this).attr('name')+"="+jQuery(this).val();
			} else if(jQuery(this).val()==''){
				alert("I Nominativi sono obbligatori");
				jQuery(this).focus();
				return false;
			} else {
				alert("I Nominativi possono contenere al massimo 26 caratteri");
				jQuery(this).focus();
				return false;
			}
	});
	jQuery.ajax({url:'prenota-biglietti.html',data:'a=prenota&m='+jQuery('#emailprenota').val()+qs,type:'POST',complete:function(xml,status){ msg = jQuery.parseJSON(xml.responseText); message(msg.msg,msg.error); if(typeof msg.target!='undefined' && typeof msg.html!='undefined'){ jQuery('#'+msg.target).html(msg.html); } }});
}
