domains = new Array("fileTransfer", "iPlanet", "iPlanetIntra", "iPlanetLogin", "msInter", "msIntra", "msInterLogin", "msIntraLogin", "oraInter", "oraIntra", "oraInterLogin", "oraIntraLogin");
envs = new Array("tech", "dev", "int", "impl", "train", "prod");

// Intended Public functions
function replaceDomain(docpath) {
  return replaceByRules(docpath, 'iPlanet') ;
}

function fileTransferInter(docpath) {
  return replaceByRules(docpath, 'fileTransfer') ;
}

function iPlanet(docpath) {
  return replaceByRules(docpath, 'iPlanet') ;
}

function iPlanetIntra(docpath) {
  return replaceByRules(docpath, 'iPlanetIntra') ;
}

function iPlanetLogin(docpath) {
  return replaceByRules(docpath, 'iPlanetLogin') ;
}

function msInter(docpath) {
  return replaceByRules(docpath, 'msInter') ;
}

function msIntra(docpath) {
  return replaceByRules(docpath, 'msIntra') ;
}

function msInterLogin(docpath) {
  return replaceByRules(docpath, 'msInterLogin') ;
}

function msIntraLogin(docpath) {
  return replaceByRules(docpath, 'msIntraLogin') ;
}

function oraInter(docpath) {
  return replaceByRules(docpath, 'oraInter') ;
}

function oraIntra(docpath) {
  return replaceByRules(docpath, 'oraIntra') ;
}

function oraInterLogin(docpath) {
  return replaceByRules(docpath, 'oraInterLogin') ;
}

function oraIntraLogin(docpath) {
  return replaceByRules(docpath, 'oraIntraLogin') ;
}

// This method will take a url, find its domain and prepend a "<env>." in 
// front of the domain.  e.g. http://fortisadvantage.com/a/sample/url.html
// becomes http://dev.fortisadvantage.com/a/sample/url.html in dev.
function prependEnv(docpath) {
  var env = identifyEnv(location.hostname);
  var hostname = identifyHostName(docpath);
  var newhostname;
  switch (env) {
    case "prod":
      newhostname = "www." + hostname;
      break;
    case "none":
      newhostname = "dev." + hostname;
      break;
    default:
      newhostname = env + "." + hostname;
  }
  var newdocpath = docpath.replace(new RegExp(hostname, "gi"), newhostname);
  return newdocpath;
}

// Intended non-public functions

function replaceByRules(docpath, target) {
  var newdocpath = docpath ;
  var env = identifyEnv(location.hostname) ;
  //if ( env != 'none' ) {
    var destinhostname = replaceName(env, target) ;
  //}
    newdocpath = newdocpath.replace(/fortisdomain/gi , destinhostname); 
  return newdocpath ;
}

function identifyHostName(url) {
  // From a url like "https://fortisdental.com/a/sample/url.html" it would return
  // "fortisdental.com".
  var s = null;
  s = url.match(/\/\/[\w.]*\/?/);
  if (s == -1) {
    alert("hostname not found.");
    return "";
  }
  // Strip off the leading // and the trailing /
  var str = s + "";  // This seems to be needed to make sure it is a String class.
  return str.substr(2, str.length - 3);
}

function identifyEnv(hostname)  {
  // look for a name that includes train., impl., int. or dev. in a format like
  //       xyz-dev.us.fortis.com        OR
  //       dev.anyname.org              OR
  //       dev.anyname.morename.org              OR
  //       anythingimpl.anyname.morename.edu              etc, etc
  // Basically anything with our env name immediately followed by a dot, but that must 
  // be followed by at least a two dot domain (e.g. .name.com, or .name.uk, or .us.fortis.com)

  var testdomain = null ;
  var compoundname = true ;
  var numericIP = false ;


  numericIP = hostname.search(/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/)  ;

  if ( numericIP != -1 ) {
    // use the numeric IP, its probably like 127.0.0.1 for local workstation
    return 'none' ;
  }
  else {
    compoundname = hostname.search(/\./)  ;
    if ( compoundname == -1 ) {
    // its a simple name, probably an internal like localhost without the domain name appended 
      return 'none' ;
    }
   	
    else {
      // Its a compound name, so look for our test env type names
      // e.g. train2.etdbw.com   int.us.fortis.com  dev.intra2.etdbw.com
      testdomain = hostname.match(/(.*)((train|impl|int|dev|tech)2?)\.(.+)\.([^\.]*$)/i)  ;
      if ( testdomain ) {
        // testdomain is non-null, so
        // Use the train, impl, int or dev that we found to construct the hostname
        // Return the plain env name, without any '2', etc
        return testdomain[3] ;
      }
      else {
		testdomain = hostname.search(/u1ecm72/);
		if (testdomain != -1)
			return 'dev';
		testdomain = hostname.search(/u1ecm69|u1ecm105/);
		if (testdomain != -1)
			return 'int';
		testdomain = hostname.search(/u1ecm73|u1ecm96|u1ecm97/);
		if (testdomain != -1)
			return 'impl';
		testdomain = hostname.search(/u1ecm68/);
		if (testdomain != -1)
			return 'train';
        return 'prod' ;
       }
      }
    }
}

function replaceName(environ, target) {
   var ETDBW = '.familyside.ca' ;
   var FORTIS = '.us.fortis.com' ;
   var targethost ;

        switch ( environ ) {
          case "train" :
          case "impl" :
          case "int" :
          case "dev" :
          case "tech" :
            destinhostname = environ + ETDBW ;
            oraINTRAhostname = environ + '.intra2' + ETDBW ;
            msoftINTRAhostname = environ + '.intra2' + ETDBW ;
            iplanetINTRAhostname = environ + '.intra' + ETDBW ;
            oraINTERhostname = environ + '2' + ETDBW ;
            msoftINTERhostname = environ + '2' + ETDBW ;
            ftpINTERhostname = environ + '.ftp' + ETDBW ;

            destinloginname = environ + ETDBW ;
            oraINTRAloginname = environ + '.login.intra2' + ETDBW ;
            msoftINTRAloginname = environ + '.login.intra2' + ETDBW ;
            oraINTERloginname = environ + '2.login' + ETDBW ;
            msoftINTERloginname = environ + '2.login' + ETDBW ;
            break;

          case "train2" :
          case "impl2" :
          case "int2" :
          case "dev2" :
          case "tech2" :
            destinhostname = '' ;
            oraINTRAhostname = '' ;
            iplanetINTRAhostname = destinhostname ;
            msoftINTRAhostname = '' ;
            oraINTERhostname = environ + ETDBW ;
            msoftINTERhostname = environ + ETDBW ;
            ftpINTERhostname = environ + '.ftp' + ETDBW ;

            destinloginname = '' ;
            oraINTRAloginname = '' ;
            msoftINTRAloginname = '' ;
            oraINTERloginname = environ + '.login' + ETDBW ;
            msoftINTERloginname = environ + '.login' + ETDBW ;
            break;

        // it is a compound name but not one of Fortis' test/dev names
        // so it must be a production 'vanity' name,
        // so return the generic prod name
          case "prod" :
            destinhostname = 'www' + ETDBW ;
            oraINTRAhostname = 'intra2' + ETDBW ;
            msoftINTRAhostname = 'intra2' + ETDBW ;
            iplanetINTRAhostname = 'intra' + ETDBW ;
            oraINTERhostname = 'www2' + ETDBW ;
            msoftINTERhostname = 'www2' + ETDBW ;
            ftpINTERhostname = 'ftp' + ETDBW ;

            destinloginname = 'www' + ETDBW ;
            oraINTRAloginname = 'login.intra2' + ETDBW ;
            msoftINTRAloginname = 'login.intra2' + ETDBW ;
            oraINTERloginname = 'login' + ETDBW ;
            msoftINTERloginname = 'login' + ETDBW ;
            break;
            
        // This case is for when the env is null, meaning
        // its a simple name, probably an internal like localhost without the domain name appended, OR
        // the numeric IP, probably like 127.0.0.1 for local workstation
          case "none" :
          default :
            destinhostname = location.hostname ;
            oraINTRAhostname = location.hostname ;
            msoftINTRAhostname = location.hostname ;
            iplanetINTRAhostname = location.hostname ;
            oraINTERhostname = location.hostname ;
            msoftINTERhostname = location.hostname ;
            ftpINTERhostname = location.hostname ;

            destinloginname = location.hostname ;
            oraINTRAloginname = location.hostname ;
            msoftINTRAloginname = location.hostname ;
            oraINTERloginname = location.hostname ;
            msoftINTERloginname = location.hostname ;
        }

        switch (target) {
          case "iPlanet" :
            targethost = destinhostname ;
            break ;

          case "iPlanetLogin" :
            targethost = destinhostname ;
            break ;

          case "iPlanetIntra" :
             targethost = iplanetINTRAhostname ;
             break;

          case "oraInter" :
            targethost = oraINTERhostname ;
            break ;

          case "oraInterLogin" :
            targethost = oraINTERloginname ;
            break ;

          case "msInter" :
            targethost = msoftINTERhostname ;
            break ;

          case "msInterLogin" :
            targethost = msoftINTERloginname ;
            break ;

          case "oraIntra" :
            targethost = oraINTRAhostname ;
            break ;

          case "oraIntraLogin" :
            targethost = oraINTRAloginname ;
            break ;

          case "msIntra" :
            targethost = msoftINTRAhostname ;
            break ;

          case "msIntraLogin" :
            targethost = msoftINTRAloginname ;
            break ;

		  case "fileTransfer" :
			targethost = ftpINTERhostname ;
			break;
         }

  return targethost ;
}

function httpGet(docpath) {
window.location = docpath ;
}

function httpGetDomain(docpath) {
return httpGet(replaceDomain(docpath));
}

function testdump() {
	var dump = "";
	for (outer = 0; outer < domains.length; ++outer) {
		dump = dump + domains[outer] + " --------------\n";
		for (inner = 0; inner < envs.length; inner++) {
			dump = dump + " " + envs[inner] + ":\t" + replaceName(envs[inner], domains[outer]) + "\n";
		}
	}
	return dump;
}

