/***************************/
/**********  FORM  *********/
/***************************/
/*max six element*/
function validateS( elem, max )
{
	var lastSel = -1;
	
	if( !elem.soList )
	elem.soList = [];
	
	for( var x = 0, count = 0, opts = elem.options, len = opts.length; x < len; x++ ){
		if( opts[ x ].selected ) {
			if( !elem.soList[ x ] )	{
				elem.soList[ x ] = true;
				lastSel = x;
			}	
			count++;
	
			if( count > max )	{
				elem.soList[ lastSel ] = false;
				opts[ lastSel ].selected = false;
				
				$(document).ready(function(){				
					$.colorbox({inline:true, href:"#FormPopUP",overlayClose: false});		
				});	
			
				if(global.lang == 'it') {
					document.getElementById('boxErrore').innerHTML = "<p>ATTENZIONE! Si possono selezionare al massimo "+ max +" elementi.</p>";			
				}
				else if(global.lang == 'en') {
					document.getElementById('boxErrore').innerHTML = "<p>ATTENZIONE! Si possono selezionare al massimo"+ max +"elementi.</p>";	
				}
				else if(global.lang == 'fr') {
					document.getElementById('boxErrore').innerHTML = "<p>ATTENZIONE! Si possono selezionare al massimo"+ max +"elementi.</p>";	
				}				
			} 
		}
	else
		elem.soList[ x ] = null;
	}
}

/*only number*/
function validate(evt) {  
	var theEvent = evt || window.event;
	var key = theEvent.keyCode || theEvent.which;
	key = String.fromCharCode( key );  
	var regex = /[0-9]|[\b]|[\t]|\+|\(|\)|\#|\-|\ |\./;  
	var isArrow;
	try
	{
		isArrow = evt.keyCode==37 || evt.keyCode==39;
	}
	catch(error)
	{
		isArrow = false;
	}
	if( !regex.test(key)/* && !isArrow*/) { //added arrow keys to the accepted ones
		theEvent.returnValue = false;
	 	theEvent.preventDefault();
	 }
}


/******* show single faq *******/
function showFaq(number) {					
	for (i=1; i<=$('div[id|="faq"]').length; i++) {	
		if(i==number) {			
			if ($("#faq-"+number).is(":hidden")) {				
				$("#faq-"+i).slideDown();						
			}			
		} else {		
			if ($("#faq-"+i).is(":visible")) {
			$("#faq-"+i).slideUp();					
			}
		}			
	}	
}

/******* gestione radio button e checkbox nel form *******/
//questa funzione è chiamata da custom-form-elements.js
function showHideCheckbox(element) {
	var comboBox = $('#comboBox'),
		boxCampionatura = $('#boxCampionatura'),
		checkboxCampionatura = $("input[name=campionatura]"),
		checkboxPalette = $("input[name=palette]");		
		campionaturaR = $("input[name=campionaturaR]");
		newsletter = $("input[name=newsletter]");
		from = $("input[name=from]");
		
	if(element.name == "palette") {
		if(element.checked == false){
			comboBox.show();
			if( checkboxCampionatura.attr('checked') == true ){
				boxCampionatura.hide();
				checkboxCampionatura.attr('checked', false);
			}				
		} else {
			comboBox.hide();				
		}
	}
	
	if (element.name == "campionatura")	{
		if(element.checked == false){
			boxCampionatura.show();
			campionaturaR.attr('checked', true);
			
			if( checkboxPalette.attr('checked') == true ){
				comboBox.hide();
				checkboxPalette.attr('checked', false);
			}
		} else {
			boxCampionatura.hide();
		}
	}
	
	if (element.name == "newsletter") {
		if(element.checked == true){
			from.attr('checked', false);
		}
	}	
	if (element.name == "from") {
		if(newsletter.attr('checked') == false){
			newsletter.attr('checked', true);
		}
	}		
}

/******* controllo sui campi del form *******/
function checkRegistrazione(typeForm) {
	var f = "",
	myMailResp = "";	
	
	if(typeForm == "campionari")
	{
		f = document.formRichiestaCampionari;		
		myMailResp = f.mail.value;
		myObbField=['nome','cognome','qualifica','ragioneSociale','tipoAttivita','IVA','via','numero','CAP','comune','provincia','nazione','telefono','mail'];	
		myObbFieldStamp=[getLocalized('nome'),getLocalized('cognome'),getLocalized('qualifica'),getLocalized('ragione-sociale'),getLocalized('tipo'),getLocalized('IVA'),getLocalized('via'),getLocalized('n'),getLocalized('CAP'),getLocalized('comune'),getLocalized('provincia'),getLocalized('nazione'),getLocalized('telefono'),getLocalized('mail')];
	} else {
		f = document.formNewsletter;
		myMailResp = f.mail.value;
		myObbField=['nome','cognome','mail'];	
		myObbFieldStamp=[getLocalized('nome'),getLocalized('cognome'),getLocalized('mail')];	
	}
	
	
	
	for(var i=0;i<myObbField.length;i++)
	{
		var myCheck=f[myObbField[i]].value;
		if(myCheck.length<1)	{			
			$(document).ready(function(){				
				$.colorbox({inline:true, href:"#FormPopUP",overlayClose: false});		
			});	
		
			if(global.lang == 'it') {
				document.getElementById('boxErrore').innerHTML = "<p>ATTENZIONE! Il campo "+myObbFieldStamp[i].toUpperCase()+" è obbligatorio.</p>";			
			}
			else if(global.lang == 'en') {
				document.getElementById('boxErrore').innerHTML = "<p>Warning! The field "+myObbFieldStamp[i].toUpperCase()+" must be completed.</p>";
			}
			else if(global.lang == 'fr') {
				document.getElementById('boxErrore').innerHTML = "<p>Attention! "+myObbFieldStamp[i].toUpperCase()+" est requis.</p>";
			}
			return;
		}
	}
		
	if(typeForm == "campionari"){	
		if($("input[name=privacy]").attr('checked') == false) {
			$(document).ready(function(){				
				$.colorbox({inline:true, href:"#FormPopUP",overlayClose: false});		
			});				
			document.getElementById('boxErrore').innerHTML = "<p>"+getLocalized('acceptPrivacy');+"</p>";
			return;
		}
	} else {
		if($("input[name=privacy-n]").attr('checked') == false) {
			$(document).ready(function(){				
				$.colorbox({inline:true, href:"#FormPopUP",overlayClose: false});		
			});				
			document.getElementById('boxErrore').innerHTML = "<p>"+getLocalized('acceptPrivacy');+"</p>";
			return;
		}
	}
			
	//VERIFICO EMAIL vedi funzione sotto
	if(myMailResp != 0)	{
		if(isValidEmailAddress(myMailResp)) {	
		} else {			
			$(document).ready(function(){				
				$.colorbox({inline:true, href:"#FormPopUP",overlayClose: false});		
			});				
			document.getElementById('boxErrore').innerHTML = "<p>"+getLocalized('error1');+"</p>";				
			return;
		}
	}	
	
	//SUBMIT
	f.submit();
}

/******* validate email *******/
function isValidEmailAddress(myMailResp) {
	
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(myMailResp);
	
}

/***************************/
/******   COLLEZIONI  ******/
/***************************/
function onDocumentMouseMove (mouseX, mouseY) {
	var menuCollection = "",	
	 arrowCollection = "",
	 out = "";
	menuCollection = storedjQuery('#menuCollection');
	arrowCollection = storedjQuery('#arrowCollection');
	scrollUpBtn = storedjQuery('.scrollUpBtn');
	scrollDownBtn = storedjQuery('.scrollDownBtn');
	
	out = storedjQuery('#out');
	
	if(menuCollection.is(":visible") && menuCollection.position({left:"0"})) {	
		if(mouseIsOn(mouseX, mouseY, menuCollection) || mouseIsOn(mouseX, mouseY, scrollUpBtn) || mouseIsOn(mouseX, mouseY, scrollDownBtn)) {	
			out.unbind('click');		
		} else {			
				out.bind('click', function() {
				hideMenuCollection();				 
			});			
		}
	} else {
		out.unbind('click');
	}	
}


function showMenuCollection() {
	var menuCollection = "";	
	menuCollection = storedjQuery('#menuCollection');

	menuCollection.stop();		
	menuCollection.show().animate({left:'0',opacity:'1'}, 500, function(){});	
	global.iSawMenu = true;
	//mCustomScrollbars();	
	global.stopArrow = "yes"
	
	//creo cookie
	$.cookie("animationArrow","no");	
}

function hideMenuCollection(idCollection) {	
	var menuCollection = "";	
	menuCollection = storedjQuery('#menuCollection');
	
	menuCollection.stop();	
	menuCollection.animate({left:'-247px',opacity:'0'}, 700, function(){				
		menuCollection.hide();
		global.menuVisible = false;
		
		if(!idCollection)
		{
			
		} else
		{
			self.location='/'+global.lang+'/collezioni-estetiche/'+idCollection ;		
		}
		
	});
	//out.unbind('click');		
}					
					
					
/******* Move arrow *******/
function animateArrowForward (){
	var arrowCollection = "";	
	arrowCollection = storedjQuery('#arrowCollection');
	
	if($.cookie("animationArrow") == null){
		arrowCollection.animate({						
			left: '+=21'						
			}, 1000, function() {
				animateArrowBack();
		});
	}
		
}

function animateArrowBack (){
	var arrowCollection = "";	
	arrowCollection = storedjQuery('#arrowCollection');
	
	if($.cookie("animationArrow")== null){
		arrowCollection.animate({						
			left: '-=21'					
			}, 1000, function() {					
		});
	}
	
}		

function stopArrowAnimation(){
	clearInterval(global.timer);
}	

function startArrowAnimation(){
	if(global.iSawMenu == false) {
		global.timer = setInterval(animateArrowForward, 2000);
	}
}

if($.cookie("animationArrow")== null){
	startArrowAnimation();
	window.setInterval(stopArrowAnimation, 10000);
}
else{
	stopArrowAnimation();	
}


//setto l'altezza del contenitore per posizionare il footer a causa delle colonne con position absolute
function setHeight () {		
	var square988 = "",
	columnRightHeight = "";
	
	square988 = $('.square-988');
	columnRightHeight = $('.column-right').height() + 30;
	
	square988.height(columnRightHeight);
}

/******* Swap Image *******/
function swapImageBg(imgPath,imgAlt,number) {
			
 $('#imageBg').attr('src', imgPath).attr('alt', imgAlt);

 for (i=0; i<$('a[id|="numberpag"]').length; i++) {	
		if(i==number) {	
			$('#boxNumerazione-'+i).css({'background-color':'#e8e8e8'});				
		} else {		
			$('#boxNumerazione-'+i).css({'background-color':'#ffffff'});
		}			
	}	
 
}


/***************************/
/*** OPEN POP UP PRIVACY ***/
/***************************/
function openPopUpPrivacy() {
	$(document).ready(function(){				
		$.colorbox({width:'500px',inline:true, href:"#privacy",overlayClose: false});		
	});	
}


/***************************/
/****** NEWS - OUTLET  *****/
/***************************/
function createNewsHtml (i) {

	titoloN = global.news[i].titolo;
	testoN = global.news[i].testo;
	idN = global.news[i].id;
	dataN = global.news[i].data;
	
	var html = '';
	if(global.selectedNews == idN) {
		html +=	'<p class="link-news">. <i>'+dataN+'</i> .<b>'+titoloN+'<br /></b> '+testoN+'</p>';
		html +=	'<div class="clear"><!-- --></div>';	
	} else if (global.selectedNews == "0" && i == 0){
		html +=	'<p class="link-news">. <i>'+dataN+'</i> .<b>'+titoloN+'<br /></b> '+testoN+'</p>';
		html +=	'<div class="clear"><!-- --></div>';
	} else {
		//href="<?echo getURL($lang,'news')."/".$IDItem?>	
		html +=	'<p class="link-news"><a href="/'+global.lang+'/'+global.namePage+'/'+idN+'">. <i>'+dataN+'</i> .<b>'+titoloN+'<br /></b> '+testoN+'</a></p>';
		html +=	'<div class="clear"><!-- --></div>';
	}

	return html;
}

//Creazione box news					
function createNews(pag) {
	var allNewsHtml = "", 
		newsHtml = "", 
		from = 0,
		to = 0,
		i = 0;
		boxNews = null;
	
	boxNews = storedjQuery('#box-news');
	global.newsPage = pag;	

	from = (pag - 1) * global.newsForPage;
	to = pag * global.newsForPage;
		
	for(i = from; i < to && i < global.news.length; i++){
		newsHtml = createNewsHtml(i);
		allNewsHtml += newsHtml;
	}
	allNewsHtml += newsPagination(pag);
	boxNews.html(allNewsHtml);
}

//Paginazione
function newsPagination(pag) {
	
	var pageNumber = 0
	next = 0,
	prev = 0;
	
	pageNumber = Math.ceil(global.news.length / global.newsForPage);
	
	next = parseInt(pag)+1;
	prev = parseInt(pag) -1;
	
	if(next > pageNumber) {
		next = pageNumber;
	}
	
	if(prev < 1) {
		prev = 1;
	} 
	
	var html = '';
	html += '<div class="box-paginazione">';
	html += '<p>Paginazione</p>';	
	html +=	'<p>';
	for(i = 1; i < (pageNumber+1); i++) {			
		if(i == pag) {
			 html +='<a href="javascript:void(0);" style="color:#ffffff;"> '+i+'.&nbsp;</a>';	
		} else {	
			html +='<a href="javascript:void(0);" onclick="javascript:createNews(\''+i+'\');">&nbsp;'+i+' .&nbsp;</a>';	
		}			 
	}			
	html += '</p>';		
	html += '</div>';
	
	return html;
}

