
//Mailing list
function sendemail(instance_number) {
    var contact_form = document.getElementById('mailing-list-form' + instance_number);
    if (emailcheck(contact_form.email.value)) {        
        $.post("/send_email.php", {
            email: contact_form.email.value
        }, function(){});
        alert ("Thank you for adding your e-mail to our mailing list");
        contact_form.reset();
    } else {
        alert ("Please complete with a valid e-mail");
    }
}

//Contact page form
function send_contact_email() {
    if (checkfields('contact-form')) {
        var contact_form = document.getElementById('contact-form');
        //alert("name: " + contact_form.name.value + " - email: " + contact_form.email.value + " - phone" + contact_form.phone.value + " - description: " + contact_form.message.value);
        $.post("/send_contact_email.php", {
            name: contact_form.name.value,
            email: contact_form.email.value,
            phone: contact_form.phone.value,
            message: contact_form.message.value
        }, function(a){});
        alert ("Thank you for contacting us");
        contact_form.reset();
    } else {
        alert ("Please complete the whole form and make sure to use a valid e-mail");
    }
}

function checkfields(form_to_check) {
    var form = document.getElementById(form_to_check);
    var name = form.name.value;
    var email = form.email.value;
    var phone = form.phone.value;
    var message = form.message.value;
    if ( !emailcheck(email) || name == '' || phone == '' || message == '' ) {
        return false;
    } else {
        return true;
    }
}

function emailcheck(str) {

    var at="@";
    var dot=".";
    var lat=str.indexOf(at);
    var lstr=str.length;

    //no @, @ at the beggining or @ at the end
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false;
    }
    //no ., . at the beggining or . at the end
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false;
    }
    //just one @
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
    }
    //. just before @ or ????
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
    }
    //dot after the @
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
    }
    //no spaces
    if (str.indexOf(" ")!=-1){
        return false;
    }
    return true;
}
