﻿function GetQueryStringValue(name)
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

function JSONEncode(value)
{
    //check for double quotes
    value = value.replace(/\"/g, "\\\"")
    
    return value;
}

function redirectHttpToHttps() 
{
    var httpURL = window.location.hostname;
    var httpsURL = "https://" + httpURL;
    window.location = httpsURL;
}


function CheckLanguage(scwsname)
{
    //en is the standard language for Sitecore.....NOT en-CA
    var cookieVal = 'en';
    
    // default sitecore website name is "website"
    if (scwsname == null || scwsname == "")
    {
        scwsname = "website";
    }

    if (jQuery.cookie(scwsname + '#lang') != null)
    {
        cookieVal = jQuery.cookie(scwsname + '#lang');
    }

    return cookieVal;
}

function ChangeLanguage(scwsname)
{
    // default sitecore website name is "website"
    if (scwsname == null || scwsname == "")
    {
        scwsname = "website";
    }    
    
    var cookieVal = CheckLanguage(scwsname);
    
    if (cookieVal == 'en')
    {   
        cookieVal = 'fr-CA';
    }
    else
    {
        cookieVal = 'en';
    }
               
    //set the cookie to appropriate language
    jQuery.cookie(scwsname + '#lang', cookieVal, { path: '/'});

    //make sure to set the coveo lcid querystring parameter
    var lcid = (CheckLanguage(scwsname).substring(0, 2) == 'en') ? 'en' : 'fr';
    var scLang = (CheckLanguage(scwsname).substring(0, 2) == 'en') ? "en" : "fr-CA";

    var redirectPath = window.location.pathname + jQuery.query.set('lcid', lcid).set('sc_lang', scLang);

    //special logic for blogs
    if (redirectPath.indexOf("blogs") > 0) {
        redirectPath = "/blogs.aspx" + jQuery.query.set('lcid', lcid).set('sc_lang', scLang);
    }
    
    window.location = redirectPath;

}


function Setanguage(scwsname) {
    // default sitecore website name is "website"
    if (scwsname == null || scwsname == "") {
        scwsname = "website";
    }

    var cookieVal = CheckLanguage(scwsname);

       //set the cookie to appropriate language
    jQuery.cookie(scwsname + '#lang', cookieVal, { path: '/' });

    //make sure to set the coveo lcid querystring parameter
    var lcid = (CheckLanguage(scwsname).substring(0, 2) == 'en') ? 'en' : 'fr';
    var scLang = (CheckLanguage(scwsname).substring(0, 2) == 'en') ? "en" : "fr-CA";

    var redirectPath = window.location.pathname + jQuery.query.set('lcid', lcid).set('sc_lang', scLang);

    //special logic for blogs
    if (redirectPath.indexOf("blogs") > 0) {
        redirectPath = "/blogs.aspx" + jQuery.query.set('lcid', lcid).set('sc_lang', scLang);
    }

    window.location = redirectPath;

}
