function init( )
{
    Element.hide($('contact'));
    Element.hide($('about'));
    Element.hide($('imprint'));
    show('about');
}

/*
function showLogo () {
    Element.setStyle($('logo'),{'visibility' : 'visible'});
    window.setTimeout(function () { showLogo(); }, 100 );
    Effect.Appear($('logo'), { duration: 0.8 });
}
*/


function showAbout()
{
    hideAll();
    show('about');
}

function showContact()
{
    hideAll();
    show('contact');
}

function showImprint()
{
   	hideAll();
    show('imprint');
}

function hideAll()
{
	hide('about');
    hide('contact_response');
    hide('contact');
    hide('imprint');	
}

function showResponse(string) 
{
    hide('contact');
    var response = $('contact_response');
    response.innerHTML = string;
    show('contact_response');
}

function show(id)
{
    Element.setStyle($(id),{'visibility' : 'visible'});
    Effect.Appear($(id), {duration: 0.8, from: 0, to: 1.0});
}

function hide(id)
{
    Element.setStyle($(id),{'visibility' : 'hidden'});
    Effect.Appear($(id), {duration: 0.8, from: 1.0, to: 0});
}

// Event.observe($('contact'), 'click', showContact);
// Event.observe('about', 'click', showAbout);




function activateContact()
{
    if ($('contactform')) {
        $('contactform').onsubmit = function() {
            return false;
        };
    }

    if ($('submit_contact')) {
        $('submit_contact').onclick = function() {
             sendContactForm();
        };
    }

    if ($('name')) {
        $('name').onclick = function() {
             clearInput(this);
        };
    }

    if ($('email')) {
        $('email').onclick = function() {
             clearInput(this);
        };
    }

    if ($('message')) {
        $('message').onclick = function() {
             clearInput(this);
        };
    }
}



function sendContactForm()
{
    url = "http://www.sociomantic.com/contact";

    if (checkForm()) {
        var f = $('contactform');
        var params = Form.serialize(f);
        var a = { 'function': 'contact_sociomantic' };
        var h = $H(a);

        var params = params + '&' + h.toQueryString();

        	var myAjax = new Ajax.Request(
    	       url, {method: 'get', parameters: params,
    	       onComplete: showConfirmation,
    	       onFailure: function(req) {alert(req.responseText)},
    	       onException: function(t,e) {alert(e);}
        } );
    }
    else {
        return false;
    }
}



function checkForm()
{
    var email_normal = /^[a-zA-Z0-9_\+-]+(\.[a-zA-Z0-9_\+-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.([a-zA-Z]{2,4})$/;

    if ( $F('name') == "" ) {
        $('name').value = language['error_name'];
        $('name').style.color = "#d00";

        return false;
    }

    if ( $F('email') == "" || !email_normal.test($F('email')) ) {
        $('email').value = language['error_email'];
        $('email').style.color = "#d00";

        return false;
    }

    if ( $F('message') == "" ){
        $('message').value = language['error_message'];
        $('message').style.color = "#d00";

        return false;
    }

    return true;
}


function clearInput(element)
{
    if (element.value == language['error_' + element.name]) {
        if ( element.type == 'textarea' ){
            element.innerHTML = '';
            element.value = '';
        }
        else {
            element.value = '';
        }

        element.style.color = '#666';
    }
}


function showConfirmation(originalRequest, json)
{
    if (json) {
        if (json.send == true) {
            showResponse(language['thankyou']);
        }
        if (json.send == false) {
            showResponse(language['error_send_contact']);
        }
    }
}


Event.observe(window, 'load', init);
Event.observe(window, 'load', activateContact);