var proxy_url = "/proxy/main.php";

function AddEmailToMailing()
{
	var val = $('eta').getValue();
	var check = validateEmail(val);
	
	if(val != '' && check)
	{
		var params = new Object();
		params.act = 'addemailtomailing';
		params.e = val;
		new Ajax.Request(proxy_url,
		  {
		    method:'post',
		    parameters: params,
		    onSuccess: function(transport){
		      var data = new Array();
		      document.location.href="/iscrizioneeseguita";		      
		    },
		    onFailure: function(){ alert('Oooops, questo  imbarazzante...') }
		  });
		
		$('eta').setValue('');
	}else{
		alert("Per cortesia, specifica la tua email in modo corretto");
	}
}

function SearchProduct(q)
{
	var params = new Object();
	params.act = 'search';
	params.q = q;
	new Ajax.Request(proxy_url,
	  {
	    method:'post',
	    parameters: params,
	    onSuccess: function(transport){
	      var data = new Array();
	      alert(data);
	    },
	    onFailure: function(){ alert('Oooops, questo  imbarazzante...') }
	  });
}


function validateEmail(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
	   return false;
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false;
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	    return false;

	if (str.indexOf(at,(lat+1))!=-1)
	    return false;
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	    return false;
	
	if (str.indexOf(dot,(lat+2))==-1)
	    return false;
	
	if (str.indexOf(" ")!=-1)
	    return false;
	
	return true;			
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


function handleEnter(inField, e) {
    var charCode;
    
    if(e && e.which){
        charCode = e.which;
    }else if(window.event){
        e = window.event;
        charCode = e.keyCode;
    }

    if(charCode == 13) {
    	SearchProd();
    }
}

function SearchProd()
{
	var q = $('q').value;
	document.location.href="/prodotti/?q="+q;
}


function changeimg(file,id){
	var ihtml = '<img src="'+file+'" width="245" height="215" />';
	$('main-img_'+id).update(ihtml);
}

old=0;
function OpenProduct(id)
{
	if(old!=0 && old != id)
		Effect.BlindUp('scheda_prod_'+old);
		//$('scheda_prod_'+old).hide();
	
	old = id;
	//$('scheda_prod_'+id).show();
	Effect.BlindDown('scheda_prod_'+id);
}

function CloseProduct(id)
{
	old=0;
	Effect.BlindUp('scheda_prod_'+id);
}

function MoreInfoOpen(product_id)
{
	var tpl = TrimPath.parseDOMTemplate("tpl_main_information");
	var data = new Array();
	data.prod_id = product_id;
    var result  = tpl.process(data);
    
    $('lightbox2').update(result);
    Effect.Appear('lightbox1', { duration: 0.5 });
}

function MoreInfoSubmit()
{
	qfObj = new qForm("frminformation");
	
	qfObj.flname.required = true;
	qfObj.flname.requiredMessage = "Devi specificare Nome e Cognome";
	
	qfObj.email.required = true;
	qfObj.email.requiredMessage = "Devi specificare l'indirizzo e-mail.";
	qfObj.email.validateEmail("Indirizzo e-mail non valido.");

	if(qfObj.validate())
	{
		var params = new Object();
		params.act = 'addcontactrequest';

		params.product_id = qfObj.prod_id.getValue();
		params.flname = qfObj.flname.getValue();
		params.email = qfObj.email.getValue();
		params.telephone = qfObj.tel.getValue();
		params.contacttype = qfObj.contacttype.getValue();
		params.msg = qfObj.messaggio.getValue();
		
		new Ajax.Request(proxy_url,
		  {
		    method:'post',
		    parameters: params,
		    onSuccess: function(transport){
		      var data = new Array();
		      data = transport.responseText;
		      if(data=='true')
		      {
		    	  var tpl = TrimPath.parseDOMTemplate("tpl_main_information_ok");
		    	  var data = new Array();
		    	  var result  = tpl.process(data);
		    	  $('lightbox2').update(result);
		      }
		    },
		    onFailure: function(){ alert('Oooops, questo  imbarazzante...') }
		  });
	}
	
	return false;
}

function MoreInfoClose()
{
	Effect.Fade('lightbox1');
}
