﻿var setCoupon = function(code, price) {
    $("#coupon-container-code").html(code);
    $("#coupon-container-price").html('$ ' + price);
    $("#coupon-container").show();
    $("#refer-us").hide();
};

var formEmailProviderDefs = function() {
    $('#mail').focus(function() {
        if (this.value == '')
            this.value = 'E-Mail';
        else if (this.value == 'E-Mail')
            this.value = '';
    });
    $('#mail').blur(function() {
        if (this.value == 'E-Mail')
            this.value = '';
        else if (this.value == '')
            this.value = 'E-Mail';
    });
    $('#pass').focus(function() {
        if (this.value == '')
            this.value = 'password';
        else if (this.value == 'password')
            this.value = '';
    });
    $('#pass').blur(function() {
        if (this.value == 'password')
            this.value = '';
        else if (this.value == '')
            this.value = 'password';
    });

    $('#submitBtn').click(function() {
        if ($('#e-mail-provider').val() == '-') {
            alert('Please select your E-Mail Provider');
            return;
        } else if ($('#mail').val() == 'E-mail' || $('#mail').val() == '') {
            alert('Please insert your E-mail');
            return;
        } else if ($('#pass').val() == 'password' || $('#pass').val() == '') {
            alert('Please insert your Password');
            return;
        }

        $('#refer-us-loading').show();
        $('#mailData').submit();
    });

    $('#pass').val('password');
};

var formEmailProviderResponse = function(responseText, statusText, xhr, $form) {
    $('#refer-us').html(responseText);
    $('#refer-us-close').click(function() {
        $('#refer-us').fadeOut('slow');
    });
    $('#fullName').focus(function() {
        if (this.value == 'Full Name')
            this.value = '';
    });
    $('#fullName').blur(function() {
        if (this.value == '')
            this.value = 'Full Name';
    });

    $('#refer_btn').mouseover(function() {
        $(this).attr('src', 'images/coupon-images/refer_btn_over.png');
    });
    $('#refer_btn').mouseout(function() {
        $(this).attr('src', 'images/coupon-images/refer_btn.png');
    });

};

var formSendMailResponse = function (responseText, statusText, xhr, $form) {
    $('#refer-us').html(responseText);
    setCoupon($('#couponCodeRes').html(), $('#couponValueRes').html());
};

var reloadDoc = function() {
    window.location.reload();
};

var sendCoupon = function() {
    if ($('#fullName').val() == 'Full Name') {
        alert("Please enter your full name.");
        $('#fullName').focus();
    } else {
        $('#refer-us-loading').show();
        $('#contacs').submit();
    }
};

var setCouponPos = function() {
    try {
        window.document.getElementById("coupon-container").style.left = ($('#link-1').offset().left - 120) + 'px';
        window.document.getElementById("coupon-container").style.top = $('#link-1').offset().top + 'px';
    } catch (e) { /*Nothing to do*/ }
};

var referalLogic = function () {
    $('#mailData').ajaxForm({ target: '', success: formEmailProviderResponse });
    $('#e-mail-provider').customStyle();
    $('#refer-us-close').click(function () {
        $('#refer-us').fadeOut('slow');
    });
    formEmailProviderDefs();
    setCouponPos();

    $(window).resize(setCouponPos);
};
