//	NOTE : "darty.bewan" is used for development testing purpose
function IgnoredVar(inStr)
{
	var result = false;
	if (inStr.length >= 2) result = ((inStr.charAt(0) == '<') && (inStr.charAt(1) == '!'));
	return result;
}
function NeedRedirect(hostname,pathname,sLocalIP,sPublicIP)
//	Returns
//	0 for IAD access (through hostname, local IP, remote IP or proxy)
//	1 for NAS access (through hostname in local mode)
//	2 for local Black Hole DNS or remote dynamic DNS access
{
	var iadaccess = false;
	
	if (hostname == "partagedartybox") return 1;
	if (hostname == "http://dartybox-news.fr/ftp/dbv2") iadaccess = true;
	else if (hostname.indexOf("darty.bewan") != -1) iadaccess = true;
	else if ((!IgnoredVar(sLocalIP)) && (hostname == sLocalIP)) iadaccess = true;
	else if ((!IgnoredVar(sPublicIP)) && (sPublicIP != "") && (hostname == sPublicIP)) iadaccess = true;
	if (!iadaccess) iadaccess = (pathname.indexOf("\/dartybox") != -1);
	if ((!iadaccess) && (!IgnoredVar(sLocalIP))) iadaccess = (pathname.indexOf("\/" + sLocalIP) != -1);
	if ((!iadaccess) && (!IgnoredVar(sPublicIP)) && (sPublicIP != "")) iadaccess = (pathname.indexOf("\/" + sPublicIP) != -1);
	return (iadaccess) ? 0 : 2;
}
function ProxyHRef(url,sLocalIP,sPublicIP)
//	Must be called when NeedRedirect returns 0
//	Returns the path of proxy extracted form host and path
{
	var hostname = url.hostname.toLowerCase();
	var pathname = url.pathname;
	var port = url.port;
	
	if (hostname == "dartybox") return hostname + ((port == "") ? "" : (":" + port));
	if (hostname.indexOf("darty.bewan") != -1) return hostname + ((port == "") ? "" : (":" + port));
	if ((!IgnoredVar(sLocalIP)) && (hostname == sLocalIP)) return hostname + ((port == "") ? "" : (":" + port));
	if ((!IgnoredVar(sPublicIP)) && (sPublicIP != "") && (hostname == sPublicIP)) return hostname + ((port == "") ? "" : (":" + port));
	
	var index = -1;
	var found = "";
	
	if ((index = pathname.indexOf("\/dartybox")) != -1) found = "\/dartybox";
	else if ((!IgnoredVar(sLocalIP)) && ((index = pathname.indexOf("\/" + sLocalIP)) != -1)) found = "\/" + sLocalIP;
	else if ((!IgnoredVar(sPublicIP)) && (sPublicIP != "") && ((index = pathname.indexOf("\/" + sPublicIP)) != -1)) found = "\/" + sPublicIP;
	if ((index != -1) && (found != "")) return hostname + pathName.substr(index + strlen(found));
	return hostname;
}
function GoNAS(url,iHttpPort)
{
	var newurl = "";

	newurl = url.protocol + "\/\/partagedartybox";
	newurl += ((iHttpPort == "") || (isNaN(iHttpPort)) || (eval(iHttpPort) == 80)) ? "" : (":" + iHttpPort);
	newurl += (url.pathname == "") ? "\/" : url.pathname;
	newurl += (url.hash == "") ? ("") : ("#" + url.hash);
	document.location.replace(newurl);
}
function GoDepannage()
{
	document.location.replace("http:\/\/dartybox\/basic\/fr\/depannage.htm");
}
function NASRedirect(iHttpEnabled,iHttpPort,sLocalIP,sPublicIP)
{
	var url = document.location;
	var redirection = NeedRedirect(url.hostname.toLowerCase(),url.pathname,sLocalIP,sPublicIP);
	
	switch (redirection) {
		case 0:
			//	User access IAD through domain name, local IP
			//	or public IP => it's OK.
			return false;
		case 1:
			//	User want to access NAS service but use the wrong port
			//	=> redirect it on the appropriate port.
			if ((iHttpEnabled != "") && (!isNaN(iHttpEnabled)) && (eval(iHttpEnabled) == 1)) GoNAS(url,iHttpPort);
			else GoDepannage();
			return true;
		default:
			//	User reach here because requires host from local and
			//	DSL and/or PPP is down.
			//	Note : For remote access, at this point, the user
			//	access IAD via Dynamic DNS. As remote access is
			//	restricted by MAC address, this case is ignored.
			//	(Support must not use the dynamic DNS access)
			GoDepannage();
			return true;
	}
}
DNS access)
			GoDepannage();
			return true;
	}
}

