/*
	map.url.js
	
	The map.url code provides functions for generating urls for applicatiosn running in a number
    of server environment including OAS, Netweaver, ITS, and EHSweb. 
   
    The code works in conjunction with a set of JSON data files which describe the different environemnts, server types,
    SAP systems, and application runing in SAIS.
   
    The code is designed so that different servers can share a single set of data files
   
*/   

// DEFINE URL CLASS
urlClass = function() {}; // empty class constructor

// DATA TYPES
urlClass.prototype.naRegEx = /\#\w+/g; // "Named Anchor RegEx," strip local anchors, i.e. page.html#top


//*****************************************************************************
// BEGIN URL METHODS
//*****************************************************************************

 
//*****************************************************************************
// CHECKGETAPPURLDATA
//*****************************************************************************

/*
   This function checks for the existence of the data files required for getAppUrl:
   appData
   serverData
   sapSystemData

   It returns an error if any of these files is undefined
*/

urlClass.prototype.checkGetAppUrlData = function() {
  if (map.appData === undefined) {
    alert("appData is undefined, you may need to clear your browser cookies");
    return false;
  }
  else if (map.serverData === undefined) {
    alert("serverData is undefined");
    return false;
  }
  else if (map.sapSystemData === undefined){
    alert("sapSystemData is undefined");
    return false;
  }
  else {
    return true;
  }

}


// END CHECKGETAPPURLDATA
//*****************************************************************************


//*****************************************************************************
// CLASS APPENTRY
//*****************************************************************************

urlClass.prototype.AppEntry = function () {

	this.type = null;
	this.appName = null;
	this.appTitle = null;
	this.baseAppString = null;
	this.sapSystem = null;
	this.appGroup = null;
	this.testArg = null;
	this.defaultArg = null;
	this.prodOnly = null;
	this.maintenanceMode = null;
	this.maintenanceMessage = "This application is temporarily unavailable.";
	this.modifiedBy = null;
	this.dateModified = null;
}  
// END CLASS APPENTRY
//*****************************************************************************


//*****************************************************************************
// CLASS APPARGS
//*****************************************************************************

urlClass.prototype.AppArgs = function () {

  // specify the env value that will be returned by map.url.getEnvironment
  this.env = null;

  // optionally override the default server type of the application
  this.serverType = null;

  // specify the sapSystemId that would normally be derived from the environment
  this.sapSystemId = null;

  // application name or entry point to application
  this.appName = null;
         
  /* boolean value to indicate if additional args have been passed to the
     function, such as specific record id for po#, work ware, inspection, etc
  
     if additional args have not been passes, and default args exist, they will be used instead
  */
  this.additionalArgs = false;
  
  //array of arguments that will be used to build the argString
  this.argArray = new Array();

  // argument string parsed from getAppUrl that will get sent to the application   
  this.argString = "";

}

// END CLASS APPARGS
//*****************************************************************************

        
//*****************************************************************************
// GETAPPURL
//*****************************************************************************

urlClass.prototype.getAppUrl = function() {
         
  /* special variables that can be passed to getAppUrl */

  var appArgs = new map.url.AppArgs();
               
  /* getAppUrl depends on the existence of three data arrays, appData, serverData, and sapSystemData
     if any of these cannot be found, return an error         
  */
  if (!map.url.checkGetAppUrlData()) {
    return false;
  }

  var args = arguments; 
  appArgs = map.url.parseAppArgs(args);
            
  if (!appArgs.env) {
    appArgs.env = map.url.getEnvironment();
  }
   
  //DEBUG
  //alert("env = " + env + "  sapSystemId = " + sapSystemId + "  serverType = " + serverType + " appName = " + appName + "  argString = " + argString);

  return(map.url.getAppString(appArgs));

}
// END GETAPPURL
//*****************************************************************************

      
//*****************************************************************************
// PARSEAPPARGS
//*****************************************************************************      

urlClass.prototype.parseAppArgs = function(args) {
      		
  //create object ot hold parse arguments
  var appArgs = new map.url.AppArgs();

  // parse the arguments

  /*
    Arguments passed to getAppUrl are in the form of name=value pairs (p1=v1,p2=v2)  
    normally, the first argument is the appName
    It can be provided as 'myApp' or 'appName=myApp'
   If the appName is not provided as the first argument, then it must be entered as 'appName=myApp'
  */
   
  // iterate through the arguments
  for (var i = 0; i < args.length; i++) {
     argEntry = args[i].split("="); // argEntry[0] = name, argEntry[1] = value		  
     /* If a special argument is given, update the corresponding variable
        For example 'env=DEV' would set the env variable to "DEV";
        
        Else, add the entry to the argString 
     */
                  
     switch (argEntry[0]) {
  
       case "env":
         appArgs.env = argEntry[1];
       break;
  
       case "sapSystemId":
         appArgs.sapSystemId = argEntry[1];
       break;
    
       case "serverType":
         appArgs.serverType = argEntry[1];
       break;

       case "appName":
         if (appName == null) {
           appArgs.appName = argEntry[1];
         }
       break;

       default:
         // if only argEntry[0], treat as the appName
         if (!argEntry[1]) {
           appArgs.appName = argEntry[0];
         }
         else {
             appArgs.argArray.push(args[i]);
             appArgs.additionalArgs = true;
         }
       break;
     } // end switch
   } // end for loop

  return(appArgs);
}

// END PARSEAPPARGS
//*****************************************************************************      


//*****************************************************************************
// GETAPPENTRY
//*****************************************************************************        

/*
  uses the appName to return an appEntry object from the appData aray
*/
  
urlClass.prototype.getAppEntry = function(appName) {

  var appEntry = new map.url.AppEntry();
  for (var i = 0; i < map.appData.app.length; i++) {
    if (map.appData.app[i].appName == appName) {
	/*
  	  manually copies all fields from an app entry in the map.appData file to an appEntry object.
  	  This is done to prevent overwriting default values (such as maintenanceMessage) if these values
  	  are not defined in map.appData for a given entry
	*/
      tmp = map.appData.app[i];

      if (tmp.type != undefined) {
      	appEntry.type = tmp.type;
      } 	

      if (tmp.appName != undefined) {
      	appEntry.appName = tmp.appName;
      } 	

      if (tmp.appTitle != undefined) {
      	appEntry.appTitle = tmp.appTitle;
      } 	

      if (tmp.baseAppString != undefined) {
      	appEntry.baseAppString = tmp.baseAppString;
      } 	

      if (tmp.sapSystem != undefined) {
      	appEntry.sapSystem = tmp.sapSystem;
      } 	

      if (tmp.appGroup != undefined) {
      	appEntry.appGroup = tmp.appGroup;
      } 	

      if (tmp.testArg != undefined) {
      	appEntry.testArg = tmp.testArg;
      } 	

      if (tmp.defaultArg != undefined) {
      	appEntry.defaultArg = tmp.defaultArg;
      } 	

      if (tmp.prodOnly != undefined) {
      	appEntry.prodOnly = tmp.prodOnly;
      } 	

      if (tmp.maintenanceMode != undefined) {
      	appEntry.maintenanceMode = tmp.maintenanceMode;
      } 	

      if (tmp.maintenanceMessage != undefined) {
      	appEntry.maintenanceMessage = tmp.maintenanceMessage;
      } 	

      return(appEntry);
    }
  }
     
  return(appEntry);
}

// END GETAPPENTRY
//*****************************************************************************        
      
      
//*****************************************************************************
// GETAPPEXCEPTION
//*****************************************************************************        

/*
  uses the appName to search for a matching entry in the appException array
  appName properties defined in appException are used to override the standard values found in appData
*/
  
urlClass.prototype.getAppException = function(appName,appEntry) {

  var appException = new map.url.AppEntry();
  
  for (var i = 0; i < map.appException.app.length; i++) {
    if (map.appException.app[i].appName == appName) {
      appException = map.appException.app[i];

      // replace appEntry fields with values defined in appException

      if (appException.env) {
        appEntry.env = appException.env;
      }

      if (appException.type) {
        appEntry.type = appException.type;
      }
      
      if (appException.appName) {
        appEntry.appName = appException.appName;
      }

      if (appException.appTitle) {
        appEntry.appTitle = appException.appTitle;
      }

      if (appException.baseAppString) {
        appEntry.baseAppString = appException.baseAppString;
      }
 
      if (appException.sapSystem) {
        appEntry.sapSystem = appException.sapSystem;
      }

      if (appException.appGroup) {
        appEntry.appGroup = appException.appGroup;
      }

      if (appException.testArg) {
        appEntry.testArg = appException.testArg;
      }

      if (appException.defaultArg) {
        appEntry.defaultArg = appException.defaultArg;
      }

      if (appException.prodOnly) {
        appEntry.prodOnly = appException.prodOnly;
      }

      if (appException.maintenanceMode) {
        appEntry.maintenanceMode = appException.maintenanceMode;
      }

      if (appException.maintenanceMessage) {
        appEntry.maintenanceMessage = appException.maintenanceMessage;
      }

      return(appEntry);
    }
  }
     
  return(appEntry);
}

// END GETAPPEXCEPTION
//*****************************************************************************        


//*****************************************************************************
// GETAPPSTRING
//*****************************************************************************        

/*
  uses the appName and env values to get the application string 
*/

urlClass.prototype.getAppString = function(appArgs) {
  
  var appEntry = null;
  var appString = "";
  var sapSystemId = "";
  var wasHostAndWasSystemId = "";
  var env = appArgs.env;

  sapSystemId = appArgs.sapSystemId;
  
  if (!sapSystemId) {
    sapSystemId = map.url.getSapSystemId(env);
  }
       
  // get the app values from appData for the matching appName
  var appEntry = map.url.getAppEntry(appArgs.appName);
  if (appEntry === undefined) {
    alert("'" + appName + "' not found");
    return(false);
  } 

 
  // If the app has default arguments, and no additional arguments have been passed, add them to the argArray
  if (appEntry.defaultArg && (!appArgs.additionalArgs) ) {
    defaultArray = appEntry.defaultArg.split(",");
    for (var i = 0; i < defaultArray.length; i++) {
      appArgs.argArray.push(defaultArray[i]);
    }
  }
  
  // Get the server entry based on the environment and server type
  var serverEntry = map.url.getServer(appEntry.type,env);
  if (!serverEntry) {
    alert("server entry not found for type = " + appEntry.type + " env = " + env);
  	return(false);
  }
  
  // If the app requires a sapSystemId, generate the name value pair and add it to the argArray
  // Also add the wasHost and wasSystemId for DEV + WEBAS systems 
  if (appEntry.sapSystem == "true") {
  	if (sapSystemId != null) {
  		sapSystemString = "sapSystemId=" + sapSystemId;
  		appArgs.argArray.push(sapSystemString);
  	
  	}
        // Add the wasHost and wasSystemId for DEV + WEBAS systems 
        if ( (env == "DEV") && (serverEntry.type == "WEBAS") ){
        	wasSystemIdString = "wasSystemId=" + serverEntry.wasSystemId;
        	appArgs.argArray.push(wasSystemIdString);
        	wasHostString = "wasHost=" + serverEntry.wasHost;
        	appArgs.argArray.push(wasHostString);
        }
  }
 

  // Build the argString from the contents of argArray
  for (var i = 0; i < appArgs.argArray.length; i++) {
    if ( (appArgs.argString.length > 0) || (appEntry.type == "ITS") ) {
      appArgs.argString = appArgs.argString + "&";
    }
    appArgs.argString = appArgs.argString + appArgs.argArray[i];
  }
  
  /* Add a "?" to the beginning of the argString if it has args and is not an ITS app (ITS already has "?"
    as part of the baseAppString)
  */
    
  if ( (appArgs.argString.length > 1) && (appEntry.type != "ITS") ) {
    appArgs.argString = "?" + appArgs.argString;
  }

  
  // build the url from all components  
  appString = serverEntry.baseServerString + appEntry.baseAppString + appArgs.argString;
 
       
  return (appString);
       
}
// END GETAPPSTRING
//*****************************************************************************     


//*****************************************************************************
// GETSERVER
//*****************************************************************************      

/*
   this function loops through the server entries and returns a serverEntry object
   matching the specified serverType and env values
*/

urlClass.prototype.getServer = function(serverType,env) {
      
  var serverEntry;
  	  
  for (i = 0; i < map.serverData.server.length; i++) {             
    if ( (map.serverData.server[i].type == serverType) && (map.serverData.server[i].env == env) ) {
      serverEntry = map.serverData.server[i];
    }
  } // end for   

  return (serverEntry);      

}
// END GETSERVER
//*****************************************************************************     

   	  
//*****************************************************************************
// GETSAPSYSTEM
//*****************************************************************************      

urlClass.prototype.getSapSystem = function(env) {
      
  var sapSystemEntry;
  	  
  for (i = 0; i < map.sapSystemData.sapSystem.length; i++) {             
    if (map.sapSystemData.sapSystem[i].env == env) {
      sapSystemEntry = map.sapSystemData.sapSystem[i];
    }
  } // end for   

  return (sapSystemEntry);      

}
// END GETSAPSYSTEM
//*****************************************************************************     


//*****************************************************************************
// GETENVIRONMENT
//*****************************************************************************      

urlClass.prototype.getEnvironment = function() {

/*
  ENVIRONMENTS
  -------------------------- 
  DEV = development, SF2
  TEST = test, SF5
  PROD = production, PS1
  TRAIN = training, SF7
  SH1
  SH2
  SH3
  SF6
  SF8	
*/

/*
  1. If map.url.getSystemFromHomeUrl returns null (no homeUrl cookie defined, get the environment rom the hostname           
*/
       
  var myEnv;
  var system = map.url.getSystemFromHomeUrl();
   
  if (system) {
    switch (system) {
      case "SF2":
        myEnv = "DEV";
      break;
         
      case "SF5":
        myEnv = "TEST";
      break;

      case "SF6":
        myEnv = "SF6";
      break;

      case "SF7":
        myEnv = "TRAIN";
      break;

      case "SF8":
        myEnv = "SF8";
      break;

      case "PS1":
        myEnv = "PROD";
      break;

      case "SH1":
        myEnv = "SH1";
      break;

      case "SH2":
        myEnv = "SH2";
      break;

      case "SH3":
        myEnv = "SH3";
      break;

      case "insidemit-dev":
        myEnv = "DEV";
      break;
      
      case "insidemit-test":
          myEnv = "TEST";
        break;
        
      case "insidemit.mit.edu":
          myEnv = "PROD";
      break;
      
      case "insidemit-portal-dev":
          myEnv = "DEV";
	  break;
        
      case "insidemit-portal-test":
          myEnv = "TEST";
	  break;
 
      case "insidemit-portal.mit.edu":
        myEnv = "PROD";
      break;

    } // end switch
  
  }
  else {
    myEnv = map.url.getEnvironmentFromHost();
  }

  return(myEnv);
   
}
// END GETENVIRONMENT
//*****************************************************************************      


//*****************************************************************************
// GETSYSTEMFROMHOMEURL
//*****************************************************************************

/* getSystemFromHomeUrl
* returns a systemName string derived from the homeURL cookie 
*/

urlClass.prototype.getSystemFromHomeUrl = function() {
 
  var system = null;
  var homeURL = map.cookie.getCookieValue("homeURL");

  if (homeURL) {
    var SystemList = new Array();
    SystemList.push("SF2");
    SystemList.push("SF5");
	SystemList.push("SF2");	
    SystemList.push("SF5");
	SystemList.push("SF6");
	SystemList.push("SF7");
	SystemList.push("SF8");
	SystemList.push("SH1");
	SystemList.push("SH2");		
    SystemList.push("PS1");
    SystemList.push("PS1");
    SystemList.push("insidemit-dev");
    SystemList.push("insidemit-test");
    SystemList.push("insidemit.mit.edu");
    SystemList.push("insidemit-portal-dev");
    SystemList.push("insidemit-portal-test");
    SystemList.push("insidemit-portal.mit.edu");
    for (var i=0; i<SystemList.length; i++) {
      if (homeURL.indexOf(SystemList[i]) != -1) {
   	    system = SystemList[i];
      }
    }
  }
 
  return system;
}
// END FUNCTION GETSYSTEMFROMHOMEURL
//*****************************************************************************


//*****************************************************************************
// GETENVIRONMENTFROMHOST
//*****************************************************************************

/* getEnvironentFromHost
 * returns a environment string derived from the host of the current URL
 */

urlClass.prototype.getEnvironmentFromHost = function() {

  // get the host name an port #
  var hostString = location.host;

  // get the host name without the port #
  var hostStringArray = hostString.split(":");
  var host = hostStringArray[0]; 

  //alert(host);
  //alert(location.pathname);
  
  var myEnv;

  switch(host) {
  
    /* Development */
    case "mahogany.mit.edu" :
      myEnv = "DEV";
    break;
    
    case "its-dev0.mit.edu" :
      myEnv = "DEV";
    break;    
    
    case "sf2its.mit.edu.mit.edu" :
      myEnv = "DEV";
    break;

    case "insidemit-dev.mit.edu" :
      myEnv = "DEV";
    break;
    
    case "insidemit-portal-dev.mit.edu" :
        myEnv = "DEV";
    break;
    
    case "insidemit-apps-dev.mit.edu" :
      myEnv = "DEV";
    break;

    case "ehsweb-dev.mit.edu" :
      myEnv = "DEV";
    break;
    
    /* Test */
    case "mimosa.mit.edu" :
      myEnv = "TEST";
    break;

    case "its-tst0.mit.edu" :
      myEnv = "TEST";
    break;

    case "sf5its.mit.edu.mit.edu" :
      myEnv = "TEST";
    break;

    case "insidemit-test.mit.edu" :
      myEnv = "TEST";
    break;
    
    case "insidemit-portal-test.mit.edu" :
        myEnv = "TEST";
    break;

    case "insidemit-apps-test.mit.edu" :
      myEnv = "TEST";
    break;

    case "ehsweb-test.mit.edu" :
      myEnv = "TEST";
    break;

    /* Production */
    case "sapndi-prd0.mit.edu" :
      myEnv = "PROD";
    break;
    
    case "ps1its.mit.edu" :
      myEnv = "PROD";
    break;
    
    case "its.mit.edu" :
      myEnv = "PROD";
    break;    

    case "insidemit.mit.edu" :
      myEnv = "PROD";
    break;    

    case "insidemit-portal.mit.edu" :
        myEnv = "PROD";
    break;    
    
    case "insidemit-apps.mit.edu" :
      myEnv = "PROD";
    break;

    case "ehsweb.mit.edu" :
      myEnv = "PROD";
    break;
    
    /* Athena uses a single server for various environments, determined by folder name
       http://web.mit.edu/sapweb/SF2 indicates that links will go to the DEV environment
     */

    case "web.mit.edu" :
      var pathArray = location.pathname.split("/");
      var envFolder = pathArray[2]; /*  ex: "/sapweb/SF2" */
      switch (envFolder) {
        case "SF2":
          myEnv = "DEV";
        break;

        case "SF5":
          myEnv = "TEST";
        break;
        
        case "SF6":
          myEnv = "SF6";
        break;

        case "SF7":
          myEnv = "TRAIN";
        break;

        case "SF8":
          myEnv = "SF8";
        break;

        case "PS1":
          myEnv = "PROD";
        break;
        
        case "SH1":
          myEnv = "SH1";
        break;

        case "SH2":
          myEnv = "SH2";
        break;

        case "SH3":
          myEnv = "SH3";
        break;
        
        default:
          myEnv = "DEV";
        break;
      }
    break;
     
    // web is a shortened link for web.mit.edu
    case "web" :
      var pathArray = location.pathname.split("/");
      var envFolder = pathArray[2]; /*  ex: "/sapweb/SF2" */
      switch (envFolder) {
        case "SF2":
          myEnv = "DEV";
        break;

        case "SF5":
          myEnv = "TEST";
        break;
        
        case "SF6":
          myEnv = "TEST";
        break;

        case "SF7":
          myEnv = "TRAIN";
        break;

        case "SF8":
          myEnv = "SF8";
        break;

        case "PS1":
          myEnv = "PROD";
        break;
        
        case "SH1":
          myEnv = "SH1";
        break;

        case "SH2":
          myEnv = "SH2";
        break;

        case "SH3":
          myEnv = "SH3";
        break;
        
        default:
          myEnv = "DEV";
        break;
      }
    break;

    default: 
      myEnv = "DEV";
    break; 
  }
  
  return myEnv;

}
// END FUNCTION GETENVIRONMENTFROMHOST
//*****************************************************************************


//*****************************************************************************
// GETSAPSYSTEMID
//*****************************************************************************

urlClass.prototype.getSapSystemId = function(env) {
  
  var mySapSystemId; 
  
  // get the sapSystemId from sapSystemData using the env value if sapSystemId is not defined
  if (mySapSystemId == null) {
    for (i = 0; i < map.sapSystemData.sapSystem.length; i++) {
      if (map.sapSystemData.sapSystem[i].env == env) {
        mySapSystemId = map.sapSystemData.sapSystem[i].sapSystemId;
      }
    }
  }
    
  return mySapSystemId;

}  
// END GETSAPSYSTEMID
//*****************************************************************************


//*****************************************************************************
// CLASS LINKENTRY
//*****************************************************************************

urlClass.prototype.LinkEntry = function() {
  this.appName = "";
  this.env = "";
  this.functionName = "";
  this.url = "";
}

// END CLASS LINKENTRY
//*****************************************************************************


//*****************************************************************************
// GENERATEGETAPPURLTESTLINKS
//*****************************************************************************

/*
   This function returns and array of test links objects
   used to test the getAppUrl functions and related data in each environment

   The linkEntry object has the following fields
   
   env - environent
   serverType - Server Type; OAS, WEBAS, ITS, etc
   appName - application name or entry point
   url - resultant url from getAppUrl
*/

urlClass.prototype.generateGetAppUrlTestLinks = function() {
  
  var appEntry;
  var serverEntry;
  var envArray = new Array("DEV","TEST","PROD");
  var envString = "";
  var testArgString = ""; // optional test arguments defined in appData
  var functionString = "";
  var linkArray = new Array();
  var a;
  var e;
  var testString = "";

  // check for required data files 
  if (!map.url.checkGetAppUrlData()) {
    return false;
  }

  // iterate through all apps in appData
  for (a = 0; a < map.appData.app.length; a++) {
    appEntry = map.appData.app[a];
    if (appEntry.testArg.length > 0 ) {
      testArgString = "," + appEntry.testArg;
    }
    else {
      testArgString = "";
    }
    
    for (e = 0; e < map.envData.env.length; e++) {
      linkEntry = new map.url.LinkEntry();
      linkEntry.appName = appEntry.appName;
      //alert("link entry " + a + " " + e + " = " + linkEntry.appName); 
      linkEntry.env = map.envData.env[e].env;
      envString = "'env=" + map.envData.env[e].env + "'";
      linkEntry.functionString = "map.url.getAppUrl('"  + appEntry.appName + "'," + envString + testArgString + ")";      
      linkEntry.url = map.url.getAppUrl(linkEntry.appName,"env=" + linkEntry.env);
      linkArray.push(linkEntry);
    }  
  }
      
  return(linkArray);   
}  
// END GENERATEGETAPPURLTESTLINKS
//*****************************************************************************


//*****************************************************************************
// INCLUDEGETAPPURLDATA
//*****************************************************************************
  
/*
  This function is designed for generating getAppUrl test links.
  It dynamically created javascript include statements to read JSON
  data files from the insidemit-apps server for the current environment. 
  This is so JSON data files can be tested before moving to production.
  
  Normally, these includes will be hard coded in the html to point the insidemit-apps prodctuin server
 
*/

urlClass.prototype.includeGetAppUrlData = function() {

  document.write( "<!-- Include JSON Data -->");
     
  var env = map.url.getEnvironment();
      
  var dataPath = ""; // server and base directory where JSON data files are stored
     
  switch(env) {
    case "DEV":
      serverPath = "https://insidemit-apps-dev.mit.edu/dhtml/js/map/";
    break;
       
    case "TEST":
      serverPath = "https://insidemit-apps-test.mit.edu/dhtml/js/map/";
    break;
       
    case "PROD":
      serverPath = "https://insidemit-apps.mit.edu/dhtml/js/map/";
    break;

    default:
      serverPath = "https://insidemit-apps-dev.mit.edu/dhtml/js/map/";
    break;
  }


  var dataArray = new Array("map.envData.js","map.serverData.js","map.appData.js","map.sapSystemData.js");

  for (var s = 0; s < dataArray.length; s++) {
    document.write( "\<script language=\"javascript\" " );
    dataPath =  serverPath + dataArray[s];
    document.write( "   src='" + dataPath + "'" );
    document.write( "\>" );
    document.write( "\</script\>" );
  }

}
// END INCLUDEGETURLDATA
//*****************************************************************************


//*****************************************************************************
// FILENAME
//*****************************************************************************

 /*
 * Gets the filename out of a passed URL.
 * If no URL is passed, uses the current doc location.
 */

urlClass.prototype.fileName = function(docLocHref) {
	if (!docLocHref) {
		docLocHref = document.location.href;
	}
	var fileName = docLocHref.substr(docLocHref.lastIndexOf("\/") + 1).split("?")[0].replace(this.naRegEx,"");
	return(fileName);
}
// END FILENAME
//*****************************************************************************


//*****************************************************************************
// GETQUERY
//*****************************************************************************

/* 
 * Searches the doc location and returns an array of field-value pairs from
 * the query string, if there is one, delimited by a "=". You can pass a url
 * for evaluation, or by default it processes the location of the current doc.
 * If the string does not have a query or not the value you're looking for,
 * you'll get a false.
 */

urlClass.prototype.getQuery = function(strUrl) {
	if (!strUrl) { // If there's no arg use the document location.
		strUrl = document.location.toString();
	}
	if ( strUrl.indexOf("?") != -1 ) { // If there's a ? there's a query string.
		var strQuery = strUrl.split("?")[1];
	}
	else {
		return(false);
	}
	if ( strQuery != "" || strQuery.indexOf("&") != -1 ) {
		var arrQuery = strQuery.split("&");
	}
	else {
		var arrQuery = false;
	}
	return(arrQuery);
}
// END GETQUERY
//*****************************************************************************


//*****************************************************************************
// GETQUERYVALUE
//*****************************************************************************
	
urlClass.prototype.getQueryValue = function(strVal, strUrl) {
	var q = map.url.getQuery(strUrl);
	for ( var i=0; i<q.length; i++ ) {
		if ( q[i].indexOf(strVal) != -1 ) {
			return(q[i].split("=")[1]);
		}
	}
	return(false);
}

// END GETQUERYVALUE
//*****************************************************************************


//*****************************************************************************
// SYSTEMNAME
//*****************************************************************************

urlClass.prototype.systemName = function() {
  var strSub, arrArgs, i;
  strSub = document.domain;
  arrArgs = map.url.systemName.arguments;

  for (i=0; i<arrArgs.length; i++)
  {
    if (document.location.href.indexOf(arrArgs[i]) != -1)
    {
      strSub = arrArgs[i];
      break;
    }
  }
  return strSub;
}
// END SYSTEMNAME
//*****************************************************************************


//*****************************************************************************
// FUNCTION POINTTO
//*****************************************************************************

urlClass.prototype.pointTo = function(appName) {

  /*
     This function launches the the application matching the supplied "appName" argument
     It will launch the application in a new window for ITS apps
     It will open the application in the exisiting window for non-ITS apps
  */
  
  // Define the default screen height and width for opening a new application window
  w         = screen.availWidth * 0.95;
  h         = screen.availHeight * 0.85;

   // placeholders for function literals implemented later
  var funcAlert, funcBuildUrlCookie, wasURL;

  funcAlert = function(strDest)  // Function literal for alerting when there's no test system
  {                                // for the selected transaction.
    var strAlert = "You are on a test system, but " + strDest + " does not have a test site.";
    alert(strAlert);
  }

  //function literal for new insideMIT apps, old one above is deprecated and only used for edacca and po
  funcBuildRestartCookie = function (appTitle, restartURL)
  {
	 document.cookie = "appTitle=" + appTitle +";path=/;domain=mit.edu";
	 document.cookie = "restartURL=" + restartURL +";path=/;domain=mit.edu";
  }


  // Build the url from all components. getPointToUrl returns an array, with the url
  // string as the first element, and an appEntry object as the second. If there was
  // a problem constructing the url, the url string returned will be empty.	
  var urlArr = map.url.getPointToUrl(appName);

  url = urlArr[0];
  var appEntry = urlArr[1];
  var env = urlArr[2];
  
  // Check for any error conditions from getPointToUrl:

  // Display an error if the appEntry is still undefined after the exception list
  if (appEntry.appName == null) {
    alert("'" + appName + "' not found");
    return;
  } 

  // Display an error if the user tries to open a production link on a non production system
  if ( (appEntry.prodOnly == "true") && (env != "PROD") ){
    funcAlert(appEntry.appTitle); 
    return;
  }

  // Display an error if the user tries to open an app set to maintenanceMode
  if (appEntry.maintenanceMode == "true"){
    alert(appEntry.maintenanceMessage); 
    return;
  }

  // launch the app in a new window for ITS apps
  if ( (appEntry.type == "ITS") || (appEntry.type == "SAPWEB") || (appEntry.type == "SAPWEBSS") ){
  	map.window.appWindow(url); 
  }
  // open the app in the current window and create restart cookies for insideMIT apps
  else {
  	funcBuildRestartCookie(appEntry.appTitle, url);
	location.href = url;
  }


}
// END FUNCTION POINTTO
//*****************************************************************************

//*****************************************************************************
//FUNCTION GETPOINTTOURL
//*****************************************************************************

urlClass.prototype.getPointToUrl = function(appName) {

	/*
This function determines the URL that maps to the supplied "appName" argument.
There shouldn't be any UI controls in here (e.g. alerts), as this function is
not necessarily being called from a web browser - it could be called from 
a java console app.
	 */
	  //set a homeURL cookie in all cases equal to the location of the opener, make it a global cookie
  	document.cookie = "homeURL=" + document.location.href +";path=/;domain=mit.edu";

	// Get the environment, DEV, TEST, PROD, etc
	var env = map.url.getEnvironment();

	var sapSystemId =  map.url.getSapSystemId(env,null);

	// Get metadata for the app
	var appEntry = map.url.getAppEntry(appName);  

	// Get exceptions for the app
	try {
		var appEntry = map.url.getAppException(appName,appEntry);
	} catch	(e) {}

	// array of additonal url parameters
	var argArray = new Array();

	// string of "&" separated additional url parameters
	var argString = "";

	// Return empty string if the appEntry is still undefined after the exception list
	// or if the user tries to open a production link on a non production system
	// or if the user tries to open an app set to maintenanceMode.
	// We defer the actual processing of the error to the caller (usually pointTo).
	if ( (appEntry.appName == null) ||
			((appEntry.prodOnly == "true") && (env != "PROD")) ||
			(appEntry.maintenanceMode == "true" ) ) {
		return ["", appEntry];
	}

	// If the app has default arguments, add them to the argArray
	if (appEntry.defaultArg) {
		defaultArray = appEntry.defaultArg.split(",");
		for (var i = 0; i < defaultArray.length; i++) {
			argArray.push(defaultArray[i]);
		}
	}

	// Get the server entry based on the environment and server type
	var serverEntry = map.url.getServer(appEntry.type,env);

	// If the app requires a sapSystemId, generate the name value pair and add it to the argArray
	// Also add the wasHost and wasSystemId for DEV + WEBAS systems 
	if (appEntry.sapSystem == "true") {
		if (sapSystemId != null) {
			sapSystemString = "sapSystemId=" + sapSystemId;
			argArray.push(sapSystemString);

		}

		if ( (env == "DEV") && (serverEntry.type == "WEBAS") ){
			var sapSystemEntry = map.url.getSapSystem(env);
			wasSystemIdString = "wasSystemId=" + sapSystemEntry.wasSystemId;
			argArray.push(wasSystemIdString);
			wasHostString = "wasHost=" + sapSystemEntry.wasHost;
			argArray.push(wasHostString);
		}
	}

	// Build the argString from the contents of argArray
	for (var i = 0; i < argArray.length; i++) {
		if ( (argString.length > 0) || (appEntry.type == "ITS") ) {
			argString = argString + "&";
		}
		argString = argString + argArray[i];
	}
	if ( (argString.length > 1) && (appEntry.type != "ITS") ) {
		argString = "?" + argString;
	}

	// build the url from all components  
	url = serverEntry.baseServerString + appEntry.baseAppString + argString;

	return [url, appEntry, env];

}
//END FUNCTION GETPOINTTOURL
//*****************************************************************************


// END URL METHODS
//*****************************************************************************


//*****************************************************************************
// ADD URL CLASS TO MAPCLASS
//*****************************************************************************
mapClass.prototype.url = new urlClass();

