d_url = "http://www.silgest.com/";

String.prototype.trim = function() {
  return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function empty( mixed_var ) {
  return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  mixed_var === undefined || mixed_var.length === 0  );
}

function avvisa_login() {
  alert('\u00C8 necessario essere registrati al sito per effettuare\r\nil download del file desiderato.');
}

jQuery(document).ready(function(){
  jQuery("#uslog").click(function(){
    var valid = "";
    var name = jQuery("#unome").val();
    var mail = jQuery("#amail").val();
    var tphone = jQuery("#utel").val();
    var gname = jQuery("#ucogn").val();
    var sect = jQuery("#selAct").val();
    var visual = jQuery("#ubot").val();
    if ( empty ( name ) ) { valid += "<br />Nome richiesto."; }
    if ( !mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i) ) { valid += "<br />Indirizzo E-mail richiesto."; }
    if ( empty (gname) || gname.length < 3 ) { valid += "<br />Cognome richiesto."; }
    if ( empty (tphone) || tphone.length < 3 ) { valid += "<br />Telefono richiesto."; }
    if ( !empty ( visual ) ) { valid = "Bot Action"; }
    if ( !empty ( valid ) ) {
      jQuery("#fields").fadeOut("fast");
      jQuery("#respo").fadeIn("slow");
      jQuery("#respo").html( "<div style='background:#FFF; color:#FF0000; padding: 3px;'>Sono stati riscontrati i seguenti errori:" + valid + "</div>" );
      setTimeout('jQuery("#respo").fadeOut("slow"); jQuery( "#fields" ).fadeIn("fast");', 3000 );
    } else {
      var datastr = 'nome=' + Url.encode ( name ) + '&cogn=' + Url.encode ( gname ) + '&mail=' + mail + '&tell=' + tphone + '&pcont=' + visual + '&sector=' + sect;
      jQuery("#respo").html("<div style='background:#FFF; color:#000; padding: 3px; font-weight: bold; text-align: center;'>Elaborazione in corso ...</div>");
      jQuery("#fields").fadeOut("fast");
      jQuery("#respo").fadeIn("slow");
      setTimeout("sendl('" + datastr + "')", 2000 );
    }
    return false;
  });
  
  jQuery("#logu").click(function(){
    var valid = "";
    var usr = jQuery("#user_data").val();
    var psw = jQuery("#pass_data").val();
    if ( empty ( usr ) || usr.length < 3 ) { valid += "\r\nNome Utente richiesto."; }
    if ( empty ( psw ) || psw.length < 3 ) { valid += "\r\nPassword richiesta."; }
    if ( !empty ( valid ) ) {
      alert("Sono stati riscontrati i seguenti errori:" + valid);
    } else {
      var datastr = 'usr_name=' + Url.encode ( usr ) + '&pas_name=' + Url.encode ( psw ) + '&locot=' + Url.encode ( document.location.href );
      setTimeout("log_s('" + datastr + "')", 2000 );
    }
    return false;
  });

  jQuery("#forc").click(function(){
    var valid = ""; var scleto = false;
    var name = jQuery("#anag").val();
    var mail = jQuery("#mail").val();
    var tphone = jQuery("#telphone").val();
    var momex = jQuery("#note").val();
    var visual = jQuery("#bot-contact").val();
    var mogg = jQuery("#ogg").val();
    var captc = jQuery("#capt").val();
    if ( document.frmContacts.scelta ) {
      for ( var i = 0; i < document.frmContacts.scelta.length; i++ ) {
        if ( document.frmContacts.scelta[i].checked ) {
          if ( document.frmContacts.scelta[i].value == "no" ) { valid += "<br />E' Obbligatorio Leggere e Accettare l'Informativa sulla Privacy."; }
          else if ( document.frmContacts.scelta[i].value == "si" ) { scleto = true; }
        }
      }
    }
    if ( !scleto ) { valid += "<br />E' Obbligatorio Leggere e Accettare l'Informativa sulla Privacy."; }
    if ( empty ( name ) ) { valid += "<br />Nome richiesto."; }
    if ( !mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i) ) { valid += "<br />Indirizzo E-mail richiesto."; }
    if ( empty ( momex ) || momex.length < 3 ) { valid += "<br />Messaggio richiesto."; }
    if ( empty ( captc ) || captc.length < 6 ) { valid += "<br />Codice di Controllo richiesto."; }
    if ( !empty ( visual ) ) { valid = "Bot Action"; }
    if ( !empty ( valid ) ) {
      jQuery("#fields").fadeOut("fast");
      jQuery("#respo").fadeIn("slow");
      jQuery("#respo").html( "<div style='background:#FFF; color:#FF0000; padding: 3px;'>Sono stati riscontrati i seguenti errori:" + valid + "</div>" );
      setTimeout('jQuery("#respo").fadeOut("slow"); jQuery( "#fields" ).fadeIn("fast");', 3000 );
    } else {
      var datastr = 'nome=' + Url.encode ( name ) + '&mail=' + mail + '&tcell=' + tphone + '&pcont=' + visual + "&omex=" + Url.encode ( momex ) + "&dogg=" + Url.encode ( mogg ) + "&obsa=" + Url.encode ( captc );
      jQuery("#respo").html("<div style='background:#FFF; color:#000; padding: 3px; font-weight: bold; text-align: center;'>Elaborazione in corso ...</div>");
      jQuery("#fields").fadeOut("fast");
      jQuery("#respo").fadeIn("slow");
      setTimeout("send('" + datastr + "')", 2000 );
    }
    return false;
  });

});

function sendl(datastr){
  jQuery.ajax({
    type: "POST",
    url: d_url + "pages/utenti/act.php",
    data: datastr,
    cache: false,
    success: function(html){
      jQuery("#respo").fadeIn("slow");
      jQuery("#respo").html("<div style='background:#FFF; color:#000; padding: 3px; text-align: center;'>" + html + "</div>");
      setTimeout('jQuery("#respo").fadeOut("slow"); jQuery("#fields").fadeIn("fast");', 3000 );
      document.frmIscrivi.reset();
    }
  });
}

function go_away ( resp, url ) {
  switch ( resp ) {
    case '0': alert ( "Login Errato!" ); break;
    case '1': alert( "Accesso eseguito con successo!" ); document.location.href = url; break;
  }
}

function log_s(datastr){
  jQuery.ajax({
    type: "POST",
    url: d_url + "pages/utenti/lock.php",
    data: datastr,
    cache: false,
    success: function(html){
      //alert(html);
      var bud = html.split ( "|" );
      go_away ( bud[0], bud[1] );
      document.frmUlogin.reset();
    }
  });
}

function send(datastr){
  jQuery.ajax({
    type: "POST",
    url: d_url + "pages/contatti/to_mail.php",
    data: datastr,
    cache: false,
    success: function(html){
      jQuery("#respo").fadeIn("slow");
      jQuery("#respo").html("<div style='background:#FFF; color:#000; padding: 3px; text-align: center;'>" + html + "</div>");
      setTimeout('jQuery("#respo").fadeOut("slow"); jQuery("#fields").fadeIn("fast");', 3000 );
      //document.frmContacts.reset();
      setTimeout('document.location.href = document.location.href', 3000 );
    }
  });
}

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (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;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = 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;
	}
}
