
/***********************************************************************************
 * Section 1
 * This function has no link with cookies, but need to be inserted in all files
 * All code relative to cookies is in section 2, below
 **********************************************************************************/

/**
 * Function used to open a popup window (also works with navigator > 4)
 * param Name must be composed only of alphabetic and underscores characters
 * @return
 */
function popup(theURL, Name, popW, popH, scroll) {
    var winleft = (screen.width - popW) / 2;
    var winUp = (screen.height - popH) / 2;
    winProp = 'toolbar=no,location=no,status=no,menubar=no,resizable=no,scrollbars='+scroll
        + ',width='+popW+',height='+popH+',left='+winleft+',top='+winUp;
    aWin = window.open(theURL, Name, winProp);
    if (parseInt(navigator.appVersion) >= 4) { aWin.window.focus(); }
}

/**
 * Email to a friend.
 * This will open the popup window in the appropriate language
 * @sLocale [RECOMMANDED] The locale for displaying and processing the information
 * (one of the 4 following : en_CA_qc, en_CA_on, fr_CA_qc, fr_CA_on)
 * Otherwise, the locale is taken from the cookie
 */
function email_friend(sLocale) {

    var sURL = "/email_friend/init.do?"
    if (sLocale != null)
        sURL = sURL + "locale=" + sLocale + "&";

    sURL += "title=" + document.title + "&link=" + encodeURI(document.URL)

    var popW = 344;
    var popH = 450;

    popup(sURL, 'EmailFriend', popW, popH, 'no');
}

/**
 * Email Me News.
 * This will open the popup window in the appropriate language
 * @sLocale [RECOMMANDED] The locale for displaying and processing the information
 * (one of the 4 following : en_CA_qc, en_CA_on, fr_CA_qc, fr_CA_on)
 * Otherwise, the locale is taken from the cookie
 */
function email_news(sLocale) {

       var sURL = "/email_news/showform.do?"
    if (sLocale != null)
        sURL = sURL + "locale=" + sLocale + "&";

    //sURL += "title=" + document.title + "&link=" + encodeURI(document.URL)
    var popW = 450;
    var popH = 450;

    popup(sURL, 'EmailNews', popW, popH, 'no');
}

/**
 * Check Availability
 * Open the check availability window
 * @sOfferType [MANDATORY] one of the 3 possible offer types : Analog, Digital, HSI
 * @sLocale [RECOMMANDED] The locale for displaying and processing the information
 * (one of the 4 following : en_CA_qc, en_CA_on, fr_CA_qc, fr_CA_on)
 */
function check_availability(sOfferType, sLocale) {
    var popW = 344;
    var popH = 350;

    var sURL = "/check_availability/input.do?offerType=" + sOfferType;

    if (sLocale != null)
        sURL = sURL + "&locale=" + sLocale;

    popup(sURL, 'CheckAvailability', popW, popH, 'no');
}






/***********************************************************************************
 * Section 2
 * Everything relative to cookies
 **********************************************************************************/

/* These constants are also defined in
 *	 - com.cogeco.wse.presentation.Constants.java
 *	Please KEEP IN SYNC if you decide to modify this
 */
var daysForCookie = 180;          // 6 months
var localeCookieName = "COGECO_LOCALE";
var keepLocaleCookieName = "COGECO_LOCALE_KEEP";

/***********************************************************************************
 * This function changes the cookie language
 * It doesn't change the link itself. To be used in conjunction with "href"
 * If "remember preferences was check", reset the cookie for another N days
 * (where N equals the value for the variable "daysForCookie" at the beginning of this file)
 * Usage :  <a href="/en/pageABC.html" onclick="switchLanguageCookie()">English</a>
 **********************************************************************************/
function switchLanguageCookie() {

    var numDays = -1;  // Number of days to store the cookie

    // Read the cookie saying if we should "remember the preferences"
    var rp = readCookie(keepLocaleCookieName);
    if (rp == "true") {
        numDays = daysForCookie;
        setCookie(keepLocaleCookieName, "true", numDays);
    }

    // Read the cookie and redirect to the right page
    var c = readCookie(localeCookieName);
    if (c == "")   				   return;
    else if (c == "en_CA_on")      setCookie(localeCookieName, "fr_CA_on", numDays);
    else if (c == "fr_CA_on")	   setCookie(localeCookieName, "en_CA_on", numDays);
    else if (c == "en_CA_qc")	   setCookie(localeCookieName, "fr_CA_qc", numDays);
    else if (c == "fr_CA_qc")	   setCookie(localeCookieName, "en_CA_qc", numDays);

    return true;
}


/***********************************************************************************
 * This function reads the cookie with cookie name (reusable)
 **********************************************************************************/
function readCookie(cookieName) {
    var cookiestring=""+document.cookie;
    var index1=cookiestring.indexOf(cookieName);
    if (index1==-1 || cookieName=="") return "";
    var index2=cookiestring.indexOf(';',index1);
    if (index2==-1) index2=cookiestring.length;
    return unescape(cookiestring.substring(index1+cookieName.length+1,index2));
}


/**********************************************************************************
 * The name of the cookie, the value to be stored,
 * and finally the number of days until the cookie expires.
 * The first lines in the function convert
 * the number of days to a valid date.  (reusable)
 * *** To set a session cookie, set numDays to a negative value ***
 **********************************************************************************/
function setCookie(cookieName, value, numDays){

    var ExpireDate = new Date ();
    ExpireDate.setTime(ExpireDate.getTime() + (numDays * 24 * 3600 * 1000));

    // The next line stores the cookie, simply by assigning the values to the "document.cookie" object.
    // Note the date is converted to Greenwich Mean time using the "toGMTstring()" function.
    var sNumDays = (numDays < 0) ? "" : "; expires=" + ExpireDate.toGMTString();
    var sCookie = cookieName + "=" + escape(value) + "; path=/" + sNumDays;
    document.cookie = sCookie;
}


