var ajax_handler_url = "http://www.lagrangeaux3v.com/site/ajax.php";
//var ajax_handler_url = "http://localhost/forge/www.lagrangeaux3v.com/site/ajax.php";

/* general */
// -- dom
function $ (id) {
	return document.getElementById(id);
}

function $value(id) {
	return document.getElementById(id).value;
}

function $visible(id) {
	return ($(id).style.display == "none") ? false : true;
}

function toggle (id) {
	if ($(id).style.display == "none") {
		$(id).style.display = "block";
	} else {
		$(id).style.display = "none";
	}
}
// -- string utils
function is_alphanum (str) {
	var myRegxp = /^([a-zA-Z0-9_-]+)$/;
	return myRegxp.test(str);
}
function is_email (str) {
	var myRegxp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9-]{2,}[.][a-zA-Z]{2,3}$/;
	return myRegxp.test(str);

}
// -- ajax

function ajax()
{
   //---------------------
   // Private Declarations
   //---------------------
   var _request = null;
   var _this = null;

   //--------------------
   // Public Declarations
   //--------------------
   this.GetResponseXML = function()
   {
      return (_request) ? _request.responseXML : null;
   }

   this.GetResponseText = function()
   {
      return (_request) ? _request.responseText : null;
   }

   this.GetRequestObject = function()
   {
      return _request;
   }

   this.InitializeRequest = function(Method, Uri)
   {
      _InitializeRequest();
      _this = this;

      switch (arguments.length)
      {
         case 2:
            _request.open(Method, Uri);
            break;

         case 3:
            _request.open(Method, Uri, arguments[2]);
            break;
      }

      if (arguments.length >= 4) _request.open(Method, Uri, arguments[2], arguments[3]);
      this.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
   }

   this.SetRequestHeader = function(Field, Value)
   {
      if (_request) _request.setRequestHeader(Field, Value);
   }

   this.Commit = function(Data)
   {
      if (_request) _request.send(Data);
   }

   this.Close = function()
   {
      if (_request) _request.abort();
   }

   //---------------------------
   // Public Event Declarations.
   //---------------------------
   this.OnUninitialize = function() { };
   this.OnLoading = function() { };
   this.OnLoaded = function() { };
   this.OnInteractive = function() { };
   this.OnSuccess = function() { };
   this.OnFailure = function() { };

   //---------------------------
   // Private Event Declarations
   //---------------------------
   function _OnUninitialize() { _this.OnUninitialize(); };
   function _OnLoading() { _this.OnLoading(); };
   function _OnLoaded() { _this.OnLoaded(); };
   function _OnInteractive() { _this.OnInteractive(); };
   function _OnSuccess() { _this.OnSuccess(); };
   function _OnFailure() { _this.OnFailure(); };

   //------------------
   // Private Functions
   //------------------
   function _InitializeRequest()
   {
      _request = _GetRequest();
      _request.onreadystatechange = _StateHandler;
   }

   function _StateHandler()
   {
      switch (_request.readyState)
      {
         case 0:
            window.setTimeout("void(0)", 100);
            _OnUninitialize();
            break;

         case 1:
            window.setTimeout("void(0)", 100);
            _OnLoading();
            break;

         case 2:
            window.setTimeout("void(0)", 100);
            _OnLoaded();
            break;

         case 3:
            window.setTimeout("void(0)", 100);
            _OnInteractive();
            break;

         case 4:
            if (_request.status == 200)
               _OnSuccess();
            else
               _OnFailure();

            return;
            break;
      }
   }

   function _GetRequest()
   {
      var obj;

      try
      {
         obj = new XMLHttpRequest();
      }
      catch (error)
      {
         try
         {
            obj = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (error)
         {
            return null;
         }
      }

      return obj;
   }
}

var ajaxCall = function(_callback)   {
	this._datas = new Array();
	this.cb = _callback;
	this.addVar = function (varname, varvalue) {
		this._datas[varname] = escape(varvalue);
	}
	this.getVars = function() {
		var str = "";
		for (var name in this._datas) {
			str += "&" + name + "=" + this._datas[name];
		}
		return str;
	}
    this.OnSuccess = function() {
        //alert(this.GetResponseText());
		this.cb(this.GetResponseText());
    }
    this.GetData = function(file) {
       this.InitializeRequest('POST', file);
       this.Commit(this.getVars());
     }
}
ajaxCall.prototype = new ajax();


/* local */

// -- mentions légales
var win;

function popup (url, id, w, h) {
	win = window.open(url, id, "width=" + w + ",height=" + h + "status=yes,toolbar=no,menubar=no,location=no");
	win.focus();
	
}

// -- formulaire de contact
function contact() {
	if (validContactForm()) {
		var ident = $value("usermail").split("@");		
		this.onMailSent = function (response) {
			if (response=="1") {
				//$("msg_contact").innerHTML = onSentMessage;
				$("contact_form").style.display = "none";
				$("contact_msf").style.display = "none";
				$("contact_ok").style.display = "block";
			} else {
				$("msg_contact").innerHTML = "<font color=\"red\">Une erreur est survenue, veuillez v&eacute;rifier que votre adresse email est correctement renseign&eacute;e.</font>" + response;
			}
		}
		$("envoyer").disabled = true;
		var ajaxRequest = new ajaxCall(this.onMailSent);
		ajaxRequest.addVar("tokken", "submitcontactform");		
		
   		ajaxRequest.addVar("usermail", $("usermail").value);
		ajaxRequest.addVar("usermailname", ident[0]);
		
  		ajaxRequest.addVar("userobject", $("sujet").value);
		ajaxRequest.addVar("usermsg", $("message").value);
  		ajaxRequest.GetData( ajax_handler_url);
		
		
		
						 
	} else {
		return;
	}
	
	
}

function validContactForm() {
	var err = false;
	var firstWrongId = "";
		// -- user mail
	if ($("usermail").value == "" || ! is_email($("usermail").value)) {
		$("usermail").className = "error";
		firstWrongId = "usermail";
		err = true;
	} else {
		$("usermail").className = "";
	}
	if ($("sujet").value == "") {
		$("sujet").className = "error";
		err = true;
		if (firstWrongId == "") {	firstWrongId="sujet";	}
	} else {
		$("sujet").className = "";
	}		
	if ($("message").value  == "") {
		$("message").className = "error";
		err = true;
		if (firstWrongId == "") {	firstWrongId="message";	}
	} else {
		$("message").className = "";
	}		
	
	if (!err) {
		return true;
	} else {
		$(firstWrongId).focus();
		return false;
		
	}
	
	
}

