﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() { 
    return this.replace(/^\s+/,""); 
} 
String.prototype.rtrim = function() { 
    return this.replace(/\s+$/,""); 
} 

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
var strTmp = '';
var cLang = '';
var href = '';
var lIdx1 = 0;
var lIdx2 = 0;
var lIdx3 = 0;
var lIdx4 = 0;
$(document).ready(function() {
    $("#btnLang1").click(function() 
    {
        if ($(this).attr('name') != t3SearchP.CurrentLang) //de current->click en
        {
            SaveSwitchLanguageStats();
            lIdx1 = t3SearchP.RawUrl.indexOf('lang-');
            lIdx2 = t3SearchP.RawUrl.indexOf('lang=');
            lIdx3 = t3SearchP.RawUrl.indexOf('?');
            lIdx4 = t3SearchP.RawUrl.indexOf('&');
            if ((lIdx1 != -1) || (lIdx2 != -1)){
                if (lIdx1 != -1)
                {
                    cLang = t3SearchP.RawUrl.substr(lIdx1, 7);
                    strTmp = t3SearchP.RawUrl.substr(1).replace(cLang, "lang-"+$(this).attr('name'));
                }
                if (lIdx2 != -1)
                {
                    cLang = t3SearchP.RawUrl.substr(lIdx2, 7);
                    strTmp = t3SearchP.RawUrl.substr(1).replace(cLang, "lang="+$(this).attr('name'));
                }
            }else{
                //var params = { lang: $(this).attr('name')};
                //var str = jQuery.param(params);
                if ((lIdx3 != -1) && (lIdx4 != -1)){
                    strTmp = t3SearchP.RawUrl + "&lang="+$(this).attr('name');
                }
                if ((lIdx3 != -1) && (lIdx4 == -1)){
                    strTmp = t3SearchP.RawUrl + "&lang="+$(this).attr('name');
                }
                if ((lIdx3 == -1) && (lIdx4 == -1)){
                    strTmp = t3SearchP.RawUrl + "?lang="+$(this).attr('name');
                }
            }
            //strTmp = t3SearchP.URLPath + "?lang="+$(this).attr('name');
            if (t3SearchP.CurrentLang=="de")
            {
                strTmp = strTmp.replace(t3ResText.str_from_de,t3ResText.str_from_en);
                strTmp = strTmp.replace(t3ResText.str_to_de,t3ResText.str_to_en);
                strTmp = strTmp.replace(t3ResText.str_in_de,t3ResText.str_in_en);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_de,t3ResText.str_cheapflight_en);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_de,t3ResText.str_cheaphotel_en);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_de,t3ResText.str_cheapcar_en);
            }
            if (t3SearchP.CurrentLang=="fr")
            {
                strTmp = strTmp.replace(t3ResText.str_from_fr,t3ResText.str_from_en);
                strTmp = strTmp.replace(t3ResText.str_to_fr,t3ResText.str_to_en);
                strTmp = strTmp.replace(t3ResText.str_in_fr,t3ResText.str_in_en);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_fr,t3ResText.str_cheapflight_en);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_fr,t3ResText.str_cheaphotel_en);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_fr,t3ResText.str_cheapcar_en);
            }
            strTmp = t3SearchP.BaseURL+strTmp;
            strTmp = strTmp.replace("//","/");
            //window.location = t3SearchP.BaseURL+strTmp;
            window.location.replace(strTmp);
            return false;
        }
        //alert("strTmp = "+strTmp);
        //window.location.href=t3SearchP.BaseURL+strTmp;
    });
    
    $("#btnLang2").click(function() 
    {
        if ($(this).attr('name') != t3SearchP.CurrentLang) //en current->click fr
        {
            SaveSwitchLanguageStats();
            lIdx1 = t3SearchP.RawUrl.indexOf('lang-');
            lIdx2 = t3SearchP.RawUrl.indexOf('lang=');
            lIdx3 = t3SearchP.RawUrl.indexOf('?');
            lIdx4 = t3SearchP.RawUrl.indexOf('&');
            if ((lIdx1 != -1) || (lIdx2 != -1)){
                if (lIdx1 != -1)
                {
                    cLang = t3SearchP.RawUrl.substr(lIdx1, 7);
                    strTmp = t3SearchP.RawUrl.substr(1).replace(cLang, "lang-"+$(this).attr('name'));
                }
                if (lIdx2 != -1)
                {
                    cLang = t3SearchP.RawUrl.substr(lIdx2, 7);
                    strTmp = t3SearchP.RawUrl.substr(1).replace(cLang, "lang="+$(this).attr('name'));
                }
            }else{
                //var params = { lang: $(this).attr('name')};
                //var str = jQuery.param(params);
                if ((lIdx3 != -1) && (lIdx4 != -1)){
                    strTmp = t3SearchP.RawUrl + "&lang="+$(this).attr('name');
                }
                if ((lIdx3 != -1) && (lIdx4 == -1)){
                    strTmp = t3SearchP.RawUrl + "&lang="+$(this).attr('name');
                }
                if ((lIdx3 == -1) && (lIdx4 == -1)){
                    strTmp = t3SearchP.RawUrl + "?lang="+$(this).attr('name');
                }
            }
            if (t3SearchP.CurrentLang=="de")
            {
                strTmp = strTmp.replace(t3ResText.str_from_en,t3ResText.str_from_de);
                strTmp = strTmp.replace(t3ResText.str_to_en,t3ResText.str_to_de);
                strTmp = strTmp.replace(t3ResText.str_in_en,t3ResText.str_in_de);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_en,t3ResText.str_cheapflight_de);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_en,t3ResText.str_cheaphotel_de);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_en,t3ResText.str_cheapcar_de);
            }
            if (t3SearchP.CurrentLang=="fr")
            {
                strTmp = strTmp.replace(t3ResText.str_from_en,t3ResText.str_from_fr);
                strTmp = strTmp.replace(t3ResText.str_to_en,t3ResText.str_to_fr);
                strTmp = strTmp.replace(t3ResText.str_in_en,t3ResText.str_in_fr);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_en,t3ResText.str_cheapflight_fr);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_en,t3ResText.str_cheaphotel_fr);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_en,t3ResText.str_cheapcar_fr);
            }
            if ($(this).attr('name')=="fr" && t3SearchP.CurrentLang=="en")
            {
                strTmp = strTmp.replace(t3ResText.str_from_en,t3ResText.str_from_fr);
                strTmp = strTmp.replace(t3ResText.str_to_en,t3ResText.str_to_fr);
                strTmp = strTmp.replace(t3ResText.str_in_en,t3ResText.str_in_fr);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_en,t3ResText.str_cheapflight_fr);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_en,t3ResText.str_cheaphotel_fr);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_en,t3ResText.str_cheapcar_fr);
            }
            if ($(this).attr('name')=="de" && t3SearchP.CurrentLang=="en")
            {
                strTmp = strTmp.replace(t3ResText.str_from_en,t3ResText.str_from_de);
                strTmp = strTmp.replace(t3ResText.str_to_en,t3ResText.str_to_de);
                strTmp = strTmp.replace(t3ResText.str_in_en,t3ResText.str_in_de);
                strTmp = strTmp.replace(t3ResText.str_cheapflight_en,t3ResText.str_cheapflight_de);
                strTmp = strTmp.replace(t3ResText.str_cheaphotel_en,t3ResText.str_cheaphotel_de);
                strTmp = strTmp.replace(t3ResText.str_cheapcar_en,t3ResText.str_cheapcar_de);
            }
            //alert("strTmp = "+strTmp);
            strTmp = t3SearchP.BaseURL+strTmp;
            strTmp = strTmp.replace("//","/");
            //window.location = t3SearchP.BaseURL+strTmp;
            window.location.replace(strTmp);
            return false;
        }
    });
});

