/* version: 1.0.0.1
Author: Aries Huang
Email: widesky520@sina.com
*/
String.prototype.format = function(){ 
        var s = this; 
        for (var i=0,j=arguments.length; i<j; i++)
                s = s.replace("{" + (i) + "}", arguments[i]);
        return(s);
}

var avantalkCookie= {
        Set : function (){
                var name = arguments[0], value = escape(arguments[1]),
                days = (arguments.length > 2)?arguments[2]:90,
                path = (arguments.length > 3)?arguments[3]:"/";
                with(new Date()){
                        setDate(getDate()+days);
                        days=toUTCString();
                }
                document.cookie = "{0}={1};expires={2};path={3}".format(name, value, days, path);
        },
        Get : function (){
                var returnValue=document.cookie.match(new RegExp("[\b\^;]?" + arguments[0] + "=([^;]*)(?=;|\b|$)","i"));
                return returnValue?unescape(returnValue[1]):returnValue;
        },
        Delete : function (){
                var name=arguments[0];
                document.cookie = name + "=1 ; expires=Fri, 31 Dec 1900 23:59:59 GMT;";
        }
}

function avantalkFun_IncreaseStr(str,insertchr){
	var insertionPosition;
	var rtnValue;
	if(str==null || str=="") return "";
	insertionPosition = str.lastIndexOf('.php');
	rtnValue = str.substring(0,insertionPosition) + insertchr;
	rtnValue += str.substring(insertionPosition);	
	return (rtnValue);
}

function avantalkFun_goToUrl(){
    var url="";
	var fileName="";
	var cookieValue;
    url=window.location.href; //get current site url;
    var splitUrlToArray=url.split("/");
    fileName = splitUrlToArray[splitUrlToArray.length-1]; //get current page name;
    cookieValue = avantalkCookie.Get("multilanguage");
	
    //alert("cookie="+cookieValue+" - fileName="+fileName);
	if(cookieValue==null) return false;
	if(cookieValue=="_en"){
        if(fileName == "" || fileName.indexOf("index") != -1){
        	window.location.href="home.php";
		}
	}else{//case cookieValue==("_fr" || "_de" || "_es" || "_it")       
        if(fileName == "" || fileName.indexOf("index") != -1){
			window.location.href = avantalkFun_IncreaseStr('home.php',cookieValue);       
        }
	}
}
function multiGoes(url){
	var cookieValue;	
	cookieValue=avantalkCookie.Get("multilanguage");	
	if (cookieValue=="_en" || cookieValue==null){
		return true;
	}else{
		self.location.href=avantalkFun_IncreaseStr(url,cookieValue);return false;
	}
	
}
function avantalkFun_writeCookie(lang){
    avantalkCookie.Set("multilanguage",lang);
}
