function change_img(clicked_img) {
    var main_src = document.querySelector("#main_img").src;
    document.querySelector("#main_img").src = clicked_img.src;
    clicked_img.src = main_src;
}

function email_availability(product_id, email) {
    if($("#avail_email").attr("type")=="hidden"){
        $("#avail_email").attr("type", "email");
        $( "#email_btn" ).removeClass( "btn-warning" ).addClass( "btn-success" );
        return $("#email_btn").text("Submit");
    }
    if (!email.length) {
        email = $("#avail_email").val();
        var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var valid = regex.test(email);
        if (!email.length || !valid) return alert("please make sure your email is correct");
    }
    fetch("/add/email_product/" + product_id, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ email }),
    }).then(response => response.json())
        .then(data => {
            if (data.message == "success") $("#email_positive_notif").show("slow");
            else if (data.desc == "already exists") {
                $("#email_positive_notif").text("already saved");
                $("#email_positive_notif").show("slow");
            }
        });
}