/**
 * @author studio
 */

function apri_chiudi(e){
	if ($(e).style.display == 'block') 
		$(e).style.display = 'none';
	else 
		$(e).style.display = 'block';
}

function checkEmail(valore)
{
  var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
  if (!espressione.test(valore))  return false;
  else  return true;
}
function messaggio(){
	
	// controlla campi
	if($('nome').value.length < 3){
		alert("Inserire almeno 3 caratteri.");
		$('nome').focus();
		return;
	}
	if(!checkEmail($('email').value)){
		alert("Inserire un indirizzo E-mail valido.");
		$('nome').focus();
		return;
	}
	var e = $('form-messaggio');
	var send_button = $('send-button');
	e.set('send',{
		url:'contatto.php',
		method:'post',
		onRequest: function(){
			send_button.disabled = true;
		},
		onComplete:  function(){
			send_button.disabled = false;
		},
		onFailure: function(){
			alert('Impossibile inviare il messaggio.\nControllare la connessione internet e riprovare.')
		},
		onSuccess: function(x){
			// alert(x)
			oJSON = JSON.decode(x);
			switch(oJSON.error){
				case '0':
					alert('Messaggio inviato correttamente.\nGrazie di averci contattato.');
					e.reset();
					break;
				case '1': // email
					alert('Inserire un indirizzo E-mail valido.');
					$('email').focus();
					break;
				case '2': // nome
					alert('Nome / Ragione Sociale deve avere almeno 3 caratteri.');
					$('nome').focus();
					break;
				case '3': // posibile mail injection
					alert('Campo nome errato.');
					$('nome').focus();
					break;
				default:
					alert('Errore nell\'invio del messaggio.\nIl server ha ritornato \''+x+'\'');
			}
		}	
	}).send();
}
function logout(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onSuccess: function(x){
			window.location.reload(true);
		}
	}).send('login=1&u=&p=');
}
function login(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onRequest: function(){
			$('frm-login-main').style.display = 'none';
			$('ajax-loader').style.display = 'block';
		},
		onComplete: function(){
			$('frm-login-main').style.display = 'block';
			$('ajax-loader').style.display = 'none';
		},
		onSuccess: function(x){
			var oJson = JSON.decode(x);
			var id = oJson.id || 0;
			if (id == 0) {
				alert('Username e Password non corrispondenti');
			}
			else {
				if (oJson.ref != '') 
					window.location.href = oJson.ref;
				else 
					window.location.href = "index.php";
			}	
		}
	}).send('login=1&u='+$('usr').value+'&p='+$('psw').value+'&redir='+$('redir').value);
}
function box_login(){
	var r = new Request({
		url: 'login.php',
		method: 'post',
		onSuccess: function(x){
			var oJson = JSON.decode(x);
			var id = oJson.id || 0;
			if(id == 0)
				alert('Username e Password non corrispondenti');
			else
				window.location.reload(true);
		}
	}).send('login=1&u='+$('username').value+'&p='+$('password').value);
}
function search(){
	alert($('search-text').value);
}

function applyShadow(targetElement, shadowColor, shadowOffset) {
  if (typeof(targetElement) != 'object') {
    targetElement = document.getElementById(targetElement);
  }
  var value = targetElement.firstChild.nodeValue;
  targetElement.style.position = 'relative';
  targetElement.style.zIndex = -1;
    
  var newEl = document.createElement('span');
  newEl.appendChild(document.createTextNode(value));
  newEl.className = 'shadowed';
  newEl.style.color = shadowColor;
  newEl.style.position = 'absolute';
  newEl.style.left = shadowOffset + 'px';
  newEl.style.top = shadowOffset + 'px';
  newEl.style.zIndex = -2;
  
  targetElement.appendChild(newEl);
}

