function subscribe(formObject) {
    var email = formObject.email.value;

    if (!isValidEmail(email)) {
        return false;
    }

    $.ajax({
        url: "/subscribe.html?email=" + email,
        cache: false,
        success: function(html) {
            if (html && html.indexOf("success") != -1) {
                formObject.email.value = '';
                openModalDiv(530, 425, 'gateway_model_response');
            }
        }
    });
    return false;
}

function refresh() {
    closeModal();
    document.location.reload;
}

function gatewaySubscribe(email) {
    if (!email) {
        var today = new Date();
        var expires_date = new Date(today.getTime() + (30 * 1000 * 60 * 60 * 24) );
        document.cookie = "gw=1" + ";expires=" + expires_date.toGMTString() + ";path=/";
        closeModal();
        return false;
    }
    if (!isValidEmail(email)) {
        return false;
    }
    $.ajax({
        url: "/subscribe.html?email=" + email,
        cache: false,
        success: function(html) {
            if (html && html.indexOf("success") != -1) {
                openModalDiv(530, 425, 'gateway_model_response');
                _gaq.push(['_trackPageview', '/goal/signup']);
            }
        }
    });
    return false;
}


function isValidEmail(email) {
    return (email.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}


var currentTime = 0;

function setCurrentTime(ctime) {
    currentTime = ctime;
}

function setTimeRemaining() {
    var days = (currentTime - currentTime % (3600 * 24)) / (3600 * 24);

    var hour = (currentTime - (days * 3600 * 24) - currentTime % 3600) / 3600;
    var min = (currentTime - (days * 3600 * 24) - (hour * 3600) - currentTime % 60) / 60;

    var sec = currentTime % 60;
    if (sec < 0) {
        document.getElementById("timeremaining").innerHTML = "Expired";
    }
    else {
        if (sec < 10) {
            sec = "0" + sec;
        }
        if (min < 10) {
            min = "0" + min;
        }
        if(days > 0){
            hour += days * 24;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        document.getElementById("timeremaining").innerHTML = hour + " hr " + min + " min " + sec + " sec";
    }
    currentTime = currentTime - 1;
}

function retailerSearch(form) {
    var code = form.code.value;
    if (code) {
        var value = form.retailer.value;
        value = value.replace(/ /g, '-');
        value = value.replace(/\./g, '');
        value = value.replace(/\#/g, '');
        value = value.replace(/\//g, '');
        value = value.replace(/\?/g, '');
        document.location.href = "/retailer/" + code + "/" + value + ".html";
    }
    return false;
}

function openInstructions() {
    instruction = window.open('/deal-instruction.htm', 'instruction', 'height=400,width=518');
    instruction.document.write('<link type="text/css" href="/includes/css/screen.css" charset="utf-8" rel="Stylesheet" media="screen, projection"/>');
    instruction.document.write('<style type="text/css"> html{ margin:0; padding:0;}</style>');
    instruction.document.write(document.getElementById('coupon-detail').innerHTML);
    instruction.focus();

    return true;
}



