
function LanguageBar(formId, selectId, appRoot, currLang, postBack, defLng, mode)
{
    this.FormID = formId;
    this.SelectID = selectId;
    this.AppRoot = appRoot;
    this.CurrLang = currLang;
    this.PostBack = postBack;
    this.DefLng = defLng;
    this.Mode = mode;
    this.List = null;
    if (this.SelectID != null)
    {
        this.List = document.getElementById(this.SelectID);
        var instance = this;
		instance.ChangeHandler = function(e) {instance.Change(e);};
		if (document.all && this.List.attachEvent) 
		{	
			this.List.attachEvent("onchange", this.ChangeHandler);
		} 
		else if (this.List.addEventListener)
		{
            this.List.style.cursor = "";
            this.List.addEventListener("change", this.ChangeHandler, false);
		}
    }
}

LanguageBar.Create = function (formId, selectId, appRoot, currLang, postBack, defLng, mode)
{
    if (typeof(languageBars) == "undefined")
    {
        languageBars = new Array();
    }
    languageBars[selectId] = new LanguageBar(formId, selectId, appRoot, currLang, postBack, defLng, mode);
}

LanguageBar.prototype.Submit = function (lng)
{
    var newLocation;
    var form = document.getElementById(this.FormID);
    if (this.Mode == "PathPrefix")
    {
        newLocation = this.GetPathPrefix(form, lng);
    }
    else if (this.Mode == "QueryString")
    {
        newLocation = this.GetQueryString(form, lng);
    }
    else
    {
        newLocation = form.action;
    }

    //Need to override the personalized prefered language.
    if (newLocation.indexOf('sfopl') < 0)
    {
        newLocation += (newLocation.indexOf('?') > -1) ? '&sfopl=true' : '?sfopl=true';
    }

    if (this.PostBack != "")
    {
        form.action = newLocation;
        eval(this.PostBack);
    }
    else
    {
        document.location.href = newLocation;
    }
    return false;
}

LanguageBar.prototype.GetPathPrefix = function (form, lng)
{
    if (this.CurrLang == '')
    {
        if (this.DefLng == lng)
        {
            return form.action;
        }
        else
        {
            var idx = form.ownerDocument.location.pathname.toLowerCase().indexOf(this.AppRoot.toLowerCase()) + this.AppRoot.length - 1;
            return this.AppRoot + lng + form.ownerDocument.location.pathname.substring(idx) + form.ownerDocument.location.search;
        }
    }
    else
    {
        var rep = '/';
        if (lng != this.DefLng)
        {
            rep += lng + '/';
        }
        var regEx = new RegExp('/' + this.CurrLang + '/', 'gi') ;
        return form.ownerDocument.location.pathname.replace(regEx, rep) + form.ownerDocument.location.search;
    }
}

LanguageBar.prototype.GetQueryString = function (form, lng)
{
    if (this.CurrLang == '')
    {
        if (lng == this.DefLng)
        {
            return form.ownerDocument.location.href;
        }
        else
        {
            var key = (form.action.indexOf('?') > -1) ? '&sflang=' : '?sflang=';
            return form.ownerDocument.location.href + key + lng;
        }
    }
    else
    {
        var idx = form.ownerDocument.location.href.indexOf('?sflang=');
        var key = (idx > -1) ? '?sflang=' : '&sflang=';
        var match = (idx > -1) ? '\\?sflang=' : '&sflang=';
        var rep = (lng != this.DefLng) ? key + lng : '';
        var regEx = new RegExp(match + this.CurrLang, 'gi') ;
        return form.ownerDocument.location.href.replace(regEx, rep);
    }
}

LanguageBar.prototype.Change = function(e)
{
    var opt = this.List.options[this.List.selectedIndex];
	this.Submit(opt.value);
}