function Get_Cookie(name) {
	
   var start = document.cookie.indexOf(name+"=");
   var len = start+name.length+1;
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
   if (start == -1) return null;
   var end = document.cookie.indexOf(";",len);
   if (end == -1) end = document.cookie.length;
   return unescape(document.cookie.substring(len,end));
   
}

function Set_Cookie(name,value,expires,path,domain,secure) {
	
    var cookieString = name + "=" +escape(value) +
       ( (expires) ? ";expires=" + expires.toGMTString() : "") +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ( (secure) ? ";secure" : "");
    document.cookie = cookieString;
    
}

function Delete_Cookie(name,path,domain) {
	
   if (Get_Cookie(name)) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
      ( (domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
      
} 

function redirectpage(dest){
	
	if (window.location.replace)
		window.location.replace(dest)
	else
		window.location=dest
		
}

function Detect(){}

var uA=navigator.userAgent.toLowerCase();
var nA=navigator.appName.toLowerCase();
var majV=parseInt(navigator.appVersion);

Detect.prototype.IE=((uA.indexOf("msie")!=-1) && (uA.indexOf("opera")==-1));
Detect.prototype.IE4=(uA.indexOf("msie")!=-1);
Detect.prototype.IE5=((uA.indexOf("msie")!=-1) && ((majV="5")||(majV="5.5")||(majV="6")));
Detect.prototype.NS4=(nA.indexOf("netscape")!=-1);
Detect.prototype.isMac=(navigator.appVersion.indexOf("Mac")!=-1);
Detect.prototype.isWin=(navigator.appVersion.indexOf("Win")!=-1);

Detect.prototype.navigatorBrowserLanguage="false";
Detect.prototype.navigatorSystemLanguage="false";
Detect.prototype.navigatorUserLanguage="false";
Detect.prototype.navigatorLanguage="false";

var langcodes=new Array("GB","US", "BR", "PT", "DE","gb","us", "br", "pt", "de")

var langredirects=new Array("index.php?lang=english&url=index.php", "index.php?lang=english&url=index.php", "index.php?lang=portuguese&url=index.php", "index.php?lang=portuguese&url=index.php", "index.php?lang=german&url=index.php","index.php?lang=english&url=index.php", "index.php?lang=english&url=index.php", "index.php?lang=portuguese&url=index.php", "index.php?lang=portuguese&url=index.php", "index.php?lang=german&url=index.php");

var languageinfo=navigator.language? navigator.language : navigator.userLanguage

Detect.prototype.navigatorDetect=function() {
	
	for (i=0;i<langcodes.length;i++){
		
		if (languageinfo.substr(3,2)==langcodes[i]){
						
			Set_Cookie('LanguageWasAlreadySelected',Math.random()); 
			redirectpage(langredirects[i]);
			
		}
		
	}
	

}

if (!Get_Cookie('LanguageWasAlreadySelected')){
	Detect.prototype.navigatorDetect();
}
