﻿/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
*       used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
*                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
*                             If set to null or omitted, the cookie will be a session cookie and will not be retained
*                             when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
*                        require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var pos = location.href.indexOf("name="), city;
if (pos == -1) {
    pos = location.href.indexOf("city=");
}
if (pos == -1) {
    city = "Toronto";
} else {
    var pos2 = location.href.indexOf("#");
    if (pos2 == -1) pos2 = location.href.length;
    city = decodeURIComponent(location.href.substring(pos + 5, pos2));
}
var ok = true;

if ($.cookie(city) != null)
    ok = false;
else
    $.cookie(city, "already", { expires: 1, path: '/', domain: '.dealsteam.com' });

if (/*ok && location.href.indexOf("#nosubscribe") == -1*/false) {
    try { var layer = new Layer($("body"), "subs-layer", { height: 130, width: 600 }); } catch(e) {  }
    layer.show();

    switch(city) {
        case "Toronto":
            $("div#subs-layer").append("<form method=post action='https://app.icontact.com/icp/signup.php' name='icpsignup' id='icpsignup311' accept-charset='UTF-8' onsubmit='return verifyRequired311();' >\
            <input type=hidden name=redirect value='" + location.href + "#nosubscribe' />\
            <input type=hidden name=errorredirect value='http://www.icontact.com/www/signup/error.html' />\
            <div id='SignUp'>\
            <table width='600' class='signupframe' border='0' cellspacing='0' cellpadding='5'>\
	            <tr>\
                  <td valign=top align=right>\
                    <span class='required'>*</span>&nbsp;Get Daily Deal email<br /><b>" + city + "</b>\
                  </td>\
                  <td align=left>\
                    <input id='fields_email' type='text' name = 'fields_email' title = 'your@email.com' value = 'your@email.com' class = 'backtext' style = 'width: 350px; font-size: 21px;' />\
                  </td>\
                </tr>\
                <input type=hidden name='listid' value='1411'>\
                <input type=hidden name='specialid:1411' value='UP36'>\
                <input type=hidden name='clientid' value='743613'>\
                <input type=hidden name='formid' value='311'>\
                <input type=hidden name='reallistid' value='1'>\
                <input type=hidden name='doubleopt' value='1'>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><span class='required'>*</span> = Required Field</td>\
                </tr>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><input type='submit' name='Submit' value='Submit' class='usub-buttons' style = 'font-size: 21px; margin-left: 15px;' ></td>\
                </tr>\
                </table>\
            </div>\
            </form>\
            <span class='link'><a href='http://www.icontact.com'>Email Marketing</a> You Can Trust</span>");
            break;
        case "Mississauga":
            $("div#subs-layer").append("<form method=post action='https://app.icontact.com/icp/signup.php' name='icpsignup' id='icpsignup311' accept-charset='UTF-8' onsubmit='return verifyRequired311();' >\
            <input type=hidden name=redirect value='" + location.href + "#nosubscribe' />\
            <input type=hidden name=errorredirect value='http://www.icontact.com/www/signup/error.html' />\
            <div id='SignUp'>\
            <table width='600' class='signupframe' border='0' cellspacing='0' cellpadding='5'>\
	            <tr>\
                  <td valign=top align=right>\
                    <span class='required'>*</span>&nbsp;Get Daily Deal email<br /><b>" + city + "</b>\
                  </td>\
                  <td align=left>\
                    <input id='fields_email' type='text' name = 'fields_email' title = 'your@email.com' value = 'your@email.com' class = 'backtext' style = 'width: 350px; font-size: 21px;' />\
                  </td>\
                </tr>\
                <input type=hidden name='listid' value='3016'>\
                <input type=hidden name='specialid:3016' value='RRYB'>\
                <input type=hidden name=clientid value='743613'>\
                <input type=hidden name=formid value='311'>\
                <input type=hidden name='reallistid' value='1'>\
                <input type=hidden name='doubleopt' value='1'>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><span class='required'>*</span> = Required Field</td>\
                </tr>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><input type='submit' name='Submit' value='Submit' class='usub-buttons' style = 'font-size: 21px; margin-left: 15px;' ></td>\
                </tr>\
                </table>\
            </div>\
            </form>\
            <span class='link'><a href='http://www.icontact.com'>Email Marketing</a> You Can Trust</span>");
            break;
        case "York Region":
            $("div#subs-layer").append("<form method=post action='https://app.icontact.com/icp/signup.php' name='icpsignup' id='icpsignup311' accept-charset='UTF-8' onsubmit='return verifyRequired311();' >\
            <input type=hidden name=redirect value='" + location.href + "#nosubscribe' />\
            <input type=hidden name=errorredirect value='http://www.icontact.com/www/signup/error.html' />\
            <div id='SignUp'>\
            <table width='600' class='signupframe' border='0' cellspacing='0' cellpadding='5'>\
	            <tr>\
                  <td valign=top align=right>\
                    <span class='required'>*</span>&nbsp;Get Daily Deal email<br /><b>" + city + "</b>\
                  </td>\
                  <td align=left>\
                    <input id='fields_email' type='text' name = 'fields_email' title = 'your@email.com' value = 'your@email.com' class = 'backtext' style = 'width: 350px; font-size: 21px;' />\
                  </td>\
                </tr>\
                <input type=hidden name='listid' value='3017'>\
                <input type=hidden name='specialid:3017' value='CZJK'>\
                <input type=hidden name=clientid value='743613'>\
                <input type=hidden name=formid value='311'>\
                <input type=hidden name='reallistid' value='1'>\
                <input type=hidden name='doubleopt' value='1'>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><span class='required'>*</span> = Required Field</td>\
                </tr>\
                <tr>\
                  <td>&nbsp;</td>\
                  <td><input type='submit' name='Submit' value='Submit' class='usub-buttons' style = 'font-size: 21px; margin-left: 15px;' ></td>\
                </tr>\
                </table>\
            </div>\
            </form>\
            <span class='link'><a href='http://www.icontact.com'>Email Marketing</a> You Can Trust</span>");
            break;    
        default:
            {
                $("div#subs-layer").append("<div style = 'width: 100%; height: 100%; text-align: center; padding: 20px; font-size: 21px;'>\
                    Get daily notification of " + city + "<p style = 'margin-top: 10px;'>\
                    <input id='fields_email' type='text' name = 'your@email.com' value = 'your@email.com' class = 'backtext' style = 'width: 350px; font-size: 21px;' />\
                    <input id='sub-button2' class='usub-buttons' type='button' value='Subscribe' style = 'font-size: 21px; margin-left: 15px;' />\
                </p></div>");
                break;
            }
    }
    var icpForm311 = document.getElementById('icpsignup311');

    if (document.location.protocol === "https:")

        icpForm311.action = "https://app.icontact.com/icp/signup.php";
    function verifyRequired311() {
        var em = $("input#fields_email");
        if (em.val() === "" || em.val() === "your@email.com") {
            em.focus();
            mb.show({ text: "The Get Daily Deal email field is required." });
            return false;
        }


        return true;
    }
    $("input#fields_email").focusin(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
        }
        $(this).removeClass("backtext").addClass("normtext");
    }).focusout(function() {
        if (!$(this).val().length)
            $(this).val($(this).attr("title")).removeClass("normtext").addClass("backtext");
    }).keypress(function() {
        $(this).removeClass("backtext").addClass("normtext");
    });

    $("input#sub-button2").live("click", function() {
        var email = $("input#fields_email").val();
        if (!email.length || email === "your@email.com") {
            mb.show({ text: "Please, in first input e-mail" });
            return false;
        }
        if (/^[\w\d\.\-\_]{2,}@[\w\d\.\-\_]{2,}[\w]{2,6}$/.test(email)) {
            $("<img />", { id: "sub-spinner", src: "/img/ajax-loader.gif" }).insertBefore("input#sub-button2");
            $("input.usub-buttons").hide();
            $.getJSON("/Subscriber.ashx?action=subscribe&email=" + email + "&city=" + city,
                    function(resp) {
                        $.cookie(city, "already", { expires: 365, path: '/', domain: '.dealsteam.com' });
                        $("div#subs-layer").find("img.layer-close").click();
                        mb.show({ text: resp.answer });
                    }
                );
        } else {
            mb.show({ text: "Incorrect e-mail!" });
        }
    });

    $("input#fields_email").live("keypress", function(e) {

        //if (e.keyCode == 13)
         //   return false;

    });
}
