﻿// teamsave common.js
// (c) 2010

function asyncJS( urlprefix, sslprefix, scriptsrc ) {
    var aJS = document.createElement('script'); aJS.type = 'text/javascript'; aJS.async = true;
    aJS.src = ('https:' == document.location.protocol ? 'https://' + sslprefix : 'http://' + urlprefix) + scriptsrc;
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(aJS);
}

function scrollMeWhere(locationid) {
    $.scrollTo(locationid, 800);
}

function showCities() {
    $("a.morecities").attr("href", "javascript:void(0)");
    $("a.morecities").click(function() {
        var targetcity = $(this).attr("rel");
        if ($(targetcity).hasClass("cityexpanded")) {
            $(targetcity).slideUp("normal", function() { }).removeClass("cityexpanded");
            $(this).addClass("citynonactivelink").removeClass("cityactivelink");
        } else {
            $(targetcity).slideDown("normal", function() { }).addClass("cityexpanded");
            $(this).addClass("cityactivelink").removeClass("citynonactivelink");
        } return false;
    });
}

function changeCity() {
    $("a.changecity").attr("href", "javascript:void(0)");
    $("a.changecity").click(function () {
        var targetcity = $(this).attr("rel");
        if ($(targetcity).hasClass("cityexpanded")) {
            $(targetcity).slideUp("normal", function () { }).removeClass("cityexpanded");
        } else {
            $(targetcity).slideDown("normal", function () { }).addClass("cityexpanded");
        } return false;
    });
}

function changeLang(locale) {
	setCookie("TS", "Lang", locale, true);
	
	//var langurl = locale.substring(0,2) + "/";
	//if (langurl == "en/") langurl = "";

	//window.location.href = "/" + langurl + "deals";
	window.location.href = "/deals";
}

function signMeUpShow() {
    /*$("a.signuplink").attr("href","javascript:void(0)");*/
    $("a.signuplink").click(function() {
        var targetsignup = $(this).attr("rel");
        if ($(targetsignup).hasClass("cityexpanded")) {
            $(targetsignup).slideUp("normal", function() { }).removeClass("cityexpanded");
            $(this).addClass("signupnonactivelink").removeClass("signupactivelink");
        } else {
            $(targetsignup).slideDown("normal", function() { }).addClass("cityexpanded");
            $(this).addClass("signupactivelink").removeClass("signupnonactivelink ");
        } return false;
    });
}

function validateEmail(form) {
    if (form.email && (!isEmail(form.email.value) || form.email.value == "you@domain.com" || form.email.value == "vous@domaine.com")) {
        alert("The email address appears to be invalid, please try again.");
        form.email.focus();
        return false;
    }
    else
        return true;
}

function isEmail(argvalue) {
    if (argvalue == null || argvalue == "")
        return false;

    if (argvalue.indexOf(" ") != -1)
        return false;
    else if (argvalue.indexOf("@") == -1)
        return false;
    else if (argvalue.indexOf("@") == 0)
        return false;
    else if (argvalue.indexOf("@") == (argvalue.length - 1))
        return false;

    arrayString = argvalue.split("@");

    if (arrayString[1].indexOf(".") == -1)
        return false;
    else if (arrayString[1].indexOf(".") == 0)
        return false;
    else if (arrayString[1].charAt(arrayString[1].length - 1) == ".") {
        return false;
    }
    return true;
}

function getMarket() {
    var market = getCookie("TS", "Market");
    if (market == null || market == "") market = "toronto";
    return market;
}

function getMarketName() {
    var market = getCookie("TS", "MarketName");
    if (market == null || market == "") market = "Toronto";
    return market;
}

function getFacebookPage() {
    var market = getCookie("TS", "FacebookPage");
    if (market == null || market == "") market = "";
    return market;
}

function getTwitterPage() {
    var market = getCookie("TS", "TwitterPage");
    if (market == null || market == "") market = "";
    return market;
}

function getCookie(cookie, name) {
    ccookie = cookie.toLowerCase() + '=';
    dcookie = document.cookie;
    cpos = dcookie.toLowerCase().indexOf(ccookie);
    if (cpos != -1) {
        cstart = cpos + ccookie.length;
        cend = dcookie.indexOf(";", cstart);

        if (cend == -1)
            cend = dcookie.length;

        if (name == null) {
            value = dcookie.substring(cstart, cend);
            value = decodeURI(value);
            return value;
        }
        cookievalue = '&' + dcookie.substring(cstart, cend);
        cname = '&' + name.toLowerCase() + '=';
        cpos = cookievalue.toLowerCase().indexOf(cname);

        if (cpos != -1) {
            cstart = cpos + cname.length;
            cend = cookievalue.indexOf('&', cstart);

            if (cend == -1)
                cend = cookievalue.length;

            value = cookievalue.substring(cstart, cend);
            value = decodeURI(value);
            return value;
        }

        return null;
    }

    return null;
}



function setCookie(cookie, name, value, sticky, days, nodomain) {
    expire = "";
    domain = "";
    path = "/";
    cookievalue = value;

    var thedate = new Date();

    if (sticky && days == null)
        expire = "; expires=" + (new Date(thedate.setDate(thedate.getDate() + 14))).toUTCString();
    else if (sticky && !isNaN(days))
        expire = "; expires=" + (new Date(thedate.setDate(thedate.getDate() + days))).toUTCString();

    if (name != null && name != "") {
        dcookie = getCookie(cookie);
        if (dcookie != null) {
            dcookie = '&' + dcookie;

            cname = '&' + name.toLowerCase() + '=';
            cpos = dcookie.toLowerCase().indexOf(cname);

            if (cpos != -1) {
                cstart = cpos + cname.length;
                cend = dcookie.indexOf('&', cstart);

                if (cend == -1)
                    cend = dcookie.length;

                cookievalue = dcookie.substring(0, cpos) + '&' + name + "=" + value + dcookie.substring(cend, dcookie.length);
                cookievalue = cookievalue.substring(1);
            }
            else {
                cookievalue = dcookie.substring(1) + '&' + name + "=" + value;
            }
        }
        else {
            cookievalue = name + "=" + value;
        }
    }

    if (nodomain == null)
        domain = "; domain=" + location.host.substring(location.host.indexOf('.'));

    cookiestring = cookie + "=" + encodeURI(cookievalue) + "; path=" + path + expire + domain + ';';

    document.cookie = cookiestring;
}


function getQS(name) {
    if (document.location.search != null && document.location.search != "") {
        var qsname = '&' + name.toLowerCase() + '=';

        var qs = '&' + document.location.search.substring(1);
        var qspos = qs.toLowerCase().indexOf(qsname);

        if (qspos != -1) {
            qsstart = qspos + qsname.length;
            qsend = qs.indexOf("&", qsstart);

            if (qsend == -1)
                qsend = qs.length;

            return unescape(qs.substring(qsstart, qsend));
        }
    }

    return null;
}

function captureQSCookies() {
    var promo = getQS("promo");
    if (promo != null && promo != "")
    {
    	if (promo.substring(0,1) == "/")
    		promo = promo.substring(1);
        setCookie("TS", "promocode", escape(promo), true);
    }

    var invite = getQS("invite");
    if (invite != null && invite != "")
        setCookie("TS", "invitedby", escape(invite), true);
}

function showNotice(msg) {

    var fadeColor = "#29dc20";

    if (msg.substring(0, 1) == "!") {
        msg = msg.substring(1);
        $("#noticeicon").attr("src", "/images/icons/Cancel-(24x24).gif");
        fadeColor = "#d21e00";
    }
    else
        $("#noticeicon").attr("src", "/images/icons/Checkmark-(24x24).gif");

    $("#noticemsg").text(msg);

    $("#noticebar").fadeIn(1200);
    $("#noticemsg").animate({ color: "#686868" }, 1500);
    $("#noticemsg").animate({ color: fadeColor }, 'fast');
    $("#noticemsg").animate({ color: "#686868" }, 1500);
}

function hideNotice() {
    $("#noticebar").fadeOut("slow");
}

function captureNotice() {
    if (document.location.hash != null && document.location.hash.indexOf("msg.") != -1) {
        showNotice(unescape(document.location.hash.substring(5)));
    }
}

function dump(arr, level) {
    var dumped_text = "";
    if (!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for (var j = 0; j < level + 1; j++) level_padding += "  ";

    if (typeof (arr) == 'object') { //Array/Hashes/Objects
        for (var item in arr) {
            var value = arr[item];

            if (typeof (value) == 'object') { //If it is an array,
                dumped_text += level_padding + "\"" + item + "\" =>\n";
                dumped_text += dump(value, level + 1);
            } else {
                dumped_text += level_padding + "\"" + item + "\": \"" + value + "\"\n";
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>" + arr + "<===(" + typeof (arr) + ")";
    }
    return dumped_text;
}

