
var ContactForm = {

	initSubsidiary : function() {
            var baseurl = $('baseurl').value;

            new Ajax.Request(baseurl + '/contact/subsidiary', {
                method: 'get',

                onSuccess: function(response) {
                    var parts = response.responseText.split(/;/);

                    if(parts.length > 0)
                    {
                        // only show entry if there's more than the main pharmacy
                        if(parts.length > 1)
                        {
                            $('row_subsidiary').show();
                        }

                        for(i = 0; i < parts.length; i++)
                        {
                            var subsidiary = parts[i].split(/#/);

                            var opt = document.createElement('option');
                            opt.value = subsidiary[0];
                            opt.text = subsidiary[1];
                            $('s_subsidiary').options.add(opt);
                        }
                    }
                },
                onException:  function(response) {
                    alert("AJAX Fehler: " + response.responseText);
                },
                onFailure:  function(response) {
                    alert("AJAX Fehler: " + response.responseText);
                }
            });
        },
	onClickSubmit : function() {

		// check for missing fields
		var errors = [];

		if ($("s_lastname").value == "") errors[errors.length] = "- Bitte geben Sie Ihren Nachnamen an. ";
		if ($("s_email").value == "") errors[errors.length] = "- Bitte geben Sie Ihre E-Mail Adresse an. ";
		if ($("s_captcha").value == "") errors[errors.length] = "- Bitte geben Sie den Sicherheitscode ein. ";


		// got errors?
        if (errors.length != 0) {
            var msg = "Folgende Fehler sind aufgetreten:" +
                      "\n" +
                      errors.join("\n");

            alert(msg);
            return;

        } else { // all ok

			// s_firstname=vorname&s_lastname=nachname&s_street=stra%C3%9Fe&s_zipcode=plz&s_city=ort&s_phone=telefon&s_email=email&s_comment=bemerkung&send=Senden
			var formData = Form.serialize("contactform");

            var baseurl = $('baseurl').value;
			new Ajax.Request(baseurl + '/contact/index', {
                  parameters: formData,
                  method: 'post',
                  evalScripts: true,

                  onSuccess: function(response) {

                    var resps = response.responseText.split(/,/);

                    // validation error on server side
                    if (resps[0] == 'ERR') {

                        if ('CAPTCHA' == resps[1])  alert("Der eingegebene Sicherheitscode ist falsch");
                        else if ('YOUR_NAME' == resps[1]) alert('- Bitte geben Sie ihren Namen an');
                        else if ('YOUR_EMAIL' == resps[1]) alert('- Bitte geben Sie Ihre E-Mail Adresse an');
                        else
                            alert("Unbekannter Fehler "+ resps[1]);

                    } else if (resps[0] == "OK") {
                        // all ok

                        alert("Vielen Dank für Ihre Nachricht.\nWir werden uns schnellstmöglichst mit Ihnen in Verbindung setzen.");
                        //$("recommendationdiv").className = "hidden";

                    } else {
                        alert("Unbekannte Serverantwort: " + response.responseText);
                    }

                  },

                  onException:  function(response) {
                       alert("AJAX Fehler: " + response.responseText);
                  },
                  onFailure:  function(response) {
                    alert("AJAX Fehler: " + response.responseText);
                  }
            });
		
		}
	}


};

