//---------------------------------
//core iCohere javascript functions
//---------------------------------

/*
addToList(sList, sAddMe)            adds sAddMe to comma-delimited list sList (if not already present)
convertSpecialChars(sSource)		converts special HTML escape codes(e.g. &#1179;) embedded in a string to JavaScript escape codes
getElement(sName)                   returns the first DOM element with id or name of sName
getPageName(sURL)                   returns an all-lower-case pagename from a full URL
escapeSpecialChars(sSource)			escapes special characters into HTML escape codes
inList(sList, sItem)                determines if sItem is listed in sList
isBodyLoaded()                      determines whether or not the body in the current context is loaded
isChrome()							determines whether or not the current browser is Google Chrome
isDev()                             determines whether ot not iCohere is running in a development context (based on URL)
isIE()								determines whether or not the current browser is Internet Explorer
isIE7()								determines whether or not the current browser is Internet Explorer 7
isFirefox()							determines whether or not the current browser is Firefox
isMac()								determines whether or not the current browser is running on a Mac
isNetscape()                        determines whether or not the current browser is Netscape
isLoaded(sPageName)                 determines whether or not the given page is loaded ANYWHERE in the iCohere application
isOldSafari()						returns true if the user is running the old (buggy) version of Safari (85)
isPopUp(phPop)						determines whether or not the given window object points to an open popup	
isSafari()							determines whether or not the current browser is Safari
isTest()                            determines whether ot not iCohere is running in a test context (based on URL)
isToolbarLoaded()                   determines whether or not the toolbar in the current context is loaded
load()                              called by each page's onLoad event for page tracking purposes
MM_callJS()
MM_findObj()
MM_openBrWindow()
MM_preloadImages()
MM_swapImage()
MM_swapImgRestore()
stripFromList(sList, sStripMe)      removes sStripMe from comma-delimited list sList (if present)
unload()                            called by each page's onUnload event for page tracking purposes
getListItem(sList,i)   				returns item i from comma delimited list sList or an empty string if i > number of items
*/

/*
written:    9/21/01
purpose:    adds sAddMe to comma-delimited list sList (if not already present)
arguments:  sList   list to which sAddMe will be appended (if not already present)
            sAddMe  item to be added to sList
note:       this function relies on the inList() function also present in this file
*/
function addToList(sList, sAddMe) {
    //just in case
    sAddMe = sAddMe.toString();
    if (typeof(sList) == "undefined") sList = "";
    
    //if sAddMe is already in the list, leave the list alone
    if (inList(sList, sAddMe)) return sList;

    //is there anything in the list to begin with?
    if (sList.length > 0) {
        return sList + "," + sAddMe;
    }
    else {
        return sAddMe;
    }
}

/*
written: 3/9/07
purpose: converts special HTML escape codes(e.g. &#1179;) embedded in a string to JavaScript escape codes
arguments:	sHTML
*/
function convertSpecialChars(sSource) {
	var sDest = "";
	var iCode = 0;
	var re = new RegExp("&#\\d+;", "g");
	
	//if there are no codes, return sSource unchanged
	if (!re.test(sSource)) return sSource;

	//loop though each instance making replacements on the fly
	do {
		//shift lead-up characters into sDest
		if (sSource.search(re) > 0) {
			sDest = sDest + sSource.slice(0, sSource.search(re));
			sSource = sSource.slice(sSource.search(re));
		}
		
		//get the character code (just the hex digits as a string)
		//iCode = eval("0x" + sSource.slice(2, sSource.search(";")));
		iCode = sSource.slice(2, sSource.search(";"));
		
		//trim the code out of sSource
		if (sSource.length > sSource.indexOf(";") + 1) {
			sSource = sSource.slice(sSource.indexOf(";") + 1);
		}
		else {
			sSource = "";
		}
		
		//put the javascript equivalent into sDest
		sDest = sDest + String.fromCharCode(iCode);
	}
	while (sSource.search(re) > -1)

	//anything left in sSource?
	if (sSource.length > 0) sDest = sDest + sSource;
	
	//all done
	return sDest;
}


/*
written: 4/13/09
purpose: escapes special characters into HTML escape codes
*/
function escapeSpecialChars(sSource) {
	var sResult = '';
	
	//loop through each character
	for (var iCurChar = 0; iCurChar < sSource.length; iCurChar++) {
		//if the character is higher than 127, escape it
		if(sSource.charCodeAt(iCurChar) > 127) {
			sResult = sResult + '&#' + sSource.charCodeAt(iCurChar) + ';'; 
		}
		else {
			sResult = sResult + sSource.charAt(iCurChar);
		}
	}
	
	return sResult; 
}



/*
written:    ?
purpose:    returns the first DOM element with id or name of sName
arguments:  sName   the id or name of the desired element
ret value:  the desired element
*/
function getElement(sName) {
	var sVersion = navigator.appVersion.toLowerCase()

    //try to get the element by ID        
    if (typeof(document.getElementById(sName)) != "undefined") {
        return document.getElementById(sName);
    }
    
    //if that failed, try to get the element by name
    if (document.getElementsByName(sName).length > 0) {
        return document.getElementsByName(sName)[0];
    }

    //as a last resort, simply try it as a direct reference (will probably work for IE)
    try {return eval(sName);} catch(err){};
}


/*
written:    2/24/02
purpose:    returns an all-lower-case pagename from a full URL
arguments:  sURL
ret value:  just the page name (w/ no path or URL arguments)
*/
function getPageName(sURL) {
    //drop url to lower case
    sURL = sURL.toLowerCase();
    
    //strip off any url arguments
    if (sURL.indexOf("?") > -1) sURL = sURL.substr(0, sURL.indexOf("?"));
    
    //strip off all leading path info
    do {sURL = sURL.substr(sURL.indexOf("/") + 1)} while (sURL.indexOf("/") > -1 && sURL.length > sURL.indexOf("/") + 1);
    
    //return sURL
    return sURL;
}


/*
written:    9/24/01
purpose:    determines if sItem is listed in sList
arguments:  sItem   the item that may or may not be in sList
            sList   the comma delimited list that may or may not contain sItem
*/
function inList(sList, sItem) {
	//explicitly convert sItem to a string; I learned the hard way that this sometimes makes a difference
	sItem = sItem.toString();
	
    //is it there at all?
    if (sList.indexOf(sItem) < 0) return false;
    
    //exhaust all possibilities
    if (sList.indexOf("," + sItem + ",") > -1 || 
        sList.indexOf(sItem + ",") == 0 || 
        (sList.indexOf("," + sItem) > -1 && sList.indexOf("," + sItem) == sList.length - sItem.length - 1) || 
        sList == sItem) {
        return true;
    }
    else {
        return false;
    }
}

/* returns item i in comma delimited list sList */
function getListItem(sList, i)
{
	if (i==0) return sList.substring(0,sList.indexOf(","));
													 
	var k=0;
	stemplist = sList.substring(sList.indexOf(",")+1,sList.length);
	while (k!=i)
	{
		k++
		if (stemplist.indexOf(",") == -1)
		{
			if (k==i)
			{
				return stemplist
			}
			else {
				return ""
			}
		}
		else if (k==i) 
		{
			return stemplist.substring(0,stemplist.indexOf(","));
		}
		else
		{
			stemplist = stemplist.substring(stemplist.indexOf(",")+1,stemplist.length);	
		}
	}
}

/*
written:    2/24/01
purpose:    determines whether or not the body in the current context is loaded
ret value:  true if loaded; otherwise false
*/
function isBodyLoaded() {
    if (typeof(top.pbBodyLoaded) == "undefined") return false;
    if (top.pbBodyLoaded) {
        return true;
    }
    else {
        return false;
    }
}


/*
adapted:    4/4/11
purpose:    determines whether or not the current browser is Google Chrome
ret value:  true or false
*/
function isChrome() {
    var sNav = navigator.appVersion.toLowerCase();
    
    if (sNav.indexOf("chrome") > -1) {
        return true;
    }
    else {
        return false;
    }
}


/*
written:    7/31/02
purpose:    determines whether or not iCohere is running in a development context (based on url)
ret value:  true or false
*/
function isDev() {
    var sURL = window.location.href;
    sURL = sURL.toLowerCase();
    if (sURL.indexOf("icoheredev/") > -1) {	//note that Firefox changes the underscore to %5F
        return true;
    }
    else {
        return false;
    }
}


/*
written:    7/17/03
purpose:    determines whether or not the current browser is Internet Explorer
ret value:  true or false
*/
function isIE() {
    var sNav = navigator.appName.toLowerCase();
    
    if (sNav.indexOf("microsoft") > -1 || sNav.indexOf("msie") > -1 || sNav.indexOf("internet explorer") > -1) {
        return true;
    }
    else {
        return false;
    }
}


function isIE6() {
    var sNav = navigator.userAgent.toLowerCase();
    
    if (sNav.indexOf("msie 6") > -1) {
        return true;
    }
    else {
        return false;
    }
}

/*
written:    4/23/08
purpose:    determines whether or not the current browser is Internet Explorer 7
ret value:  true or false
*/
function isIE7() {
    var sNav = navigator.userAgent.toLowerCase();
    
    if (sNav.indexOf("msie 7") > -1) {
        return true;
    }
    else {
        return false;
    }
}

/*
written:    4/23/08
purpose:    determines whether or not the current browser is Internet Explorer 7
ret value:  true or false
*/
function isIE8() {
    var sNav = navigator.userAgent.toLowerCase();
    
    if (sNav.indexOf("msie 8") > -1) {
        return true;
    }
    else {
        return false;
    }
}

/* 11/6/07 

	Macintosh leopard findings
	Firefox        Mozilla/5.0+(000000000;+0;+00000+000+00+0;+00000;+0000000000)+00000000000000+000000000000000
	Sarfari        Mozilla/5.0+(000000000;+0;+00000+000+00+0;+00000)+00000000000000000000+0000000+0000+000000+0000000000000+0000000000000
	Netscape (7.2) Mozilla/5.0+(000000000;+0;+000+000+00+0+000000;+00000;+00000000)+00000000000000+000000000000
	Netscape (9.0) Mozilla/5.0+(000000000;+0;+00000+000+00+0;+00000;+0000000000000)+00000000000000+000000000000000+00000000000000000
*/



/*
written:    8/1/06
purpose:    determines whether or not the current browser is Firefox
ret value:  true or false
*/
function isFirefox() {
    var sNav = navigator.userAgent.toLowerCase();
    
    if (sNav.indexOf("firefox") > -1) {
        return true;
    }
    else {
		if (sNav.indexOf("mozilla/5.0 (000000000; 0; 00000 000 00 0; 00000; 0000000000) 00000000000000 000000000000000") > -1)
		{
				return true;
		}
        return false;
    }
}


/*
written:    3/27/06
purpose:    determines whether or not the current browser is running on a Mac
ret value:  true or false
*/
function isMac() {
    var sNav = navigator.userAgent.toLowerCase();
    if (sNav.indexOf("macintosh") > -1) {
        return true;
    }
    else {
		if (sNav.indexOf("mozilla/5.0 (000000000; 0; 000") > -1)
		{
			return true;
		}
        return false;
    }
}




/*
written:    ?
purpose:    determines whether or not the current browser is Netscape
ret value:  true or false
*/
function isNetscape() {
    var sNav = navigator.appName.toLowerCase();
    if (sNav.indexOf("netscape") > -1) {
        return true;
    }
    else {
		if (sNav.indexOf("mozilla/5.0 (000000000; 0; 000 000 00 0 000000; 00000; 00000000) 00000000000000 000000000000") > - 1 || 
			sNav.indexOf("mozilla/5.0 (000000000; 0; 00000 000 00 0; 00000; 0000000000000) 00000000000000 000000000000000 00000000000000000") > -1)
		{
				return true;
		}
        return false;
    }
}


/*
written:    10/7/04
purpose:    returns true if the current browser is the old (buggy) version of Safari
ret value:  true or false
*/
function isOldSafari() {
    var sNav = navigator.userAgent.toLowerCase();
    if (sNav.indexOf("safari/85") > -1) {
        return true;
    }
    else {
        return false;
    }
}


/*
written:	8/5/09
purpose:	determines whether or not the given window object points to an open popup
ret value: 	true or false
*/
function isPopUp(phPop) {
	if (typeof(phPop) == 'undefined') return false;
	if (phPop.closed) return false;
	return true;
}


/*
written:    9/27/04
purpose:    returns true if the current browser is Safari
ret value:  true or false
*/
function isSafari() {
    var sNav = navigator.userAgent.toLowerCase();
    if (sNav.indexOf("safari") > -1) {
        return true;
    }
    else {
		if (sNav.indexOf("mozilla/5.0 (000000000; 0; 00000 000 00 0; 00000) 00000000000000000000 0000000 0000 000000 0000000000000 0000000000000") > -1)
		{
				return true;
		}
        return false;
    }
}


/*
written:    2/25/02
purpose:    determines whether or not the given page is loaded ANYWHERE in the iCohere application
arguments:  sPageName   the name of the page in question INCLUDING EXTENSION
ret value:  true or false
*/
function isLoaded(sPageName) {
    //convert sPageName to lower case
    sPageName = sPageName.toLowerCase();
    
    //concatenate lists of all loaded toolbar and body pages from topmost framer
    var sFullList = phTopFrame.psToolbarLoaded + ',' + phTopFrame.psBodyLoaded;

    //is sPageName in the list?
    if (phTopFrame.inList(sFullList, sPageName)) {
        return true;
    }
    else {
        return false;
    }
}


/*
written:    8/1/02
purpose:    determines whether or not iCohere is running in a test context (based on url)
ret value:  true or false
*/
function isTest() {
    var sURL = window.location.href;
    sURL = sURL.toLowerCase();
    if (sURL.indexOf("icoheretest/") > -1) {	//note that Firefox changes the underscore to %5F
        return true;
    }
    else {
        return false;
    }
}


/*
written:    2/24/01
purpose:    determines whether or not the toolbar in the current context is loaded
ret value:  true if loaded; otherwise false
*/
function isToolbarLoaded() {
    if (typeof(top.pbToolbarLoaded) == "undefined") return false;
    if (top.pbToolbarLoaded) {
        return true;
    }
    else {
        return false;
    }
}


/*
written:    11/20/01
purpose:    called by each page's onLoad event for page tracking purposes
*/
function load() {
    //in some situations (when iCohere is unloading phTopFrame will not be available and the function should exit
    try {
        var sPage = phTopFrame.getPageName(location.href);
    }
    catch (err) {
        return;
    }
    
    try {
        //which frame are we in?
        var sFrame = window.name;
        sFrame = sFrame.toLowerCase();

        //are we in the toolbar or body frame?
        if (sFrame.indexOf('toolbar') != -1) {
            //set convenience variables in top frame (wherever that is)
            top.pbToolbarLoaded = true;
            
            //set variables in phTopFrame
            phTopFrame.psToolbarLoaded = phTopFrame.addToList(phTopFrame.psToolbarLoaded, sPage);
        }
        else {
            //set convenience variables in top frame (wherever that is)
            top.pbBodyLoaded = true;

            //set variables in phTopFrame
            phTopFrame.psBodyLoaded = phTopFrame.addToList(phTopFrame.psBodyLoaded, sPage);

            //find me
            //alert("currently loaded toolbars: " + phTopFrame.psToolbarLoaded + "\n\ncurrently loaded bodies: " + phTopFrame.psBodyLoaded);
        }
    }
    catch (err) {
        alert("error in load() function: " + err.description);
    }

    //return piPopupLevel as potentially useful information
    return piPopupLevel;
}
    

//-------------------------------
//dreamweaver generated functions
//-------------------------------

function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

function MM_openBrWindow(theURL,winName,features,w,h) { //v2.0
  if (MM_openBrWindow.arguments.length == 3)
  {			
  	window.open(theURL,winName,features);
	return;
  }	
  var winl = (screen.width - w)/2;
  var wint = (screen.height - h)/2;	
  window.open(theURL,winName,features + ',top='+wint+',left='+ winl)
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;
  for(i=0;i<a.length;i++) {
  	x = a[i];
	x.src = x.oSrc;
  }
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;
  if(!d) d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  x=d[n];
  if(!x && d.all) {
  	x=d.all[n];
  }
  if (!x) {
  	for (i=0;i<d.forms.length;i++) x=d.forms[i][n];
  }
  if (!x && d.layers) {
  	for(i=0;i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  }
  if(!x && d.getElementById) x=d.getElementById(n);
  return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments;
  document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3) {
  	x=document.images[a[i]];
  	if (x!=null){
		document.MM_sr[j++]=x;
		if(!x.oSrc) x.oSrc=x.src;
		x.src=a[i+2];
	}
  }
}

/* NOTE: this function has had line breaks added to it because older versions of Safari can't parse it in its original form */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;
  for(i=0;i<a.length;i++) {
  	x = a[i];
	x.src = x.oSrc;
  }
}

/*
written:    9/21/01
purpose:    removes sStripMe from comma-delimited list sList (if present)
arguments:  sList       comma-delimited list from which sStripMe is to be removed
            sStripMe    item to be removed from list
*/
function stripFromList(sList, sStripMe) {
    var iIndex = 0;
    
    //make sure sList is not empty
    if (typeof(sList) == "undefined") return "";
    if (sList == "") return "";
    
    //just in case: convert sStripMe to a string   
    sStripMe = sStripMe.toString();
   
    //simplest case: sStrip me is in the list w/ commas on both sides
    iIndex = sList.indexOf("," + sStripMe + ",");
    if (iIndex > -1) {
        return sList.substr(0, iIndex + 1) + sList.substr(iIndex + sStripMe.length + 2);
    }
    
    iIndex = sList.indexOf(sStripMe);
    if (iIndex == -1) {
        //sStripMe is not in the list
        return sList;
    }
    else {
        //sStripMe is all that's in the list
        if (iIndex == 0 && sStripMe.length == sList.length) {
            return "";
        }

        //sStripMe is the 1st in the list
        if (iIndex == 0 && sList.charAt(sStripMe.length) == ",") {
            return sList.substr(sStripMe.length + 1);
        }
        
        //sStripMe is the last in the list
        if (sList.indexOf("," + sStripMe) == sList.length - sStripMe.length - 1) {
            return sList.substr(0, sList.length - sStripMe.length - 1);
        }
    }
    
    alert("stripFromList function failed to strip " + sStripMe + " from " + sList);
}


/*
written:    11/20/01
purpose:    called by each page's onUnload event for page tracking purposes
*/
function unload() {
    //in some situations (when iCohere is unloading phTopFrame will not be available and the function should exit
    try {
        var sPage = phTopFrame.getPageName(location.href);
    }
    catch (err) {
        return;
    }
    
    try {
        //which frame are we in?
        var sFrame = window.name;
        sFrame = sFrame.toLowerCase();

        //are we in the toolbar or body frame?
        if (sFrame.indexOf('toolbar') != -1) {
            //set convenience variables in top frame (wherever that is)
            top.pbToolbarLoaded = false;
            
            //set variables in phTopFrame
            phTopFrame.psToolbarLoaded = phTopFrame.stripFromList(phTopFrame.psToolbarLoaded, sPage);
        }
        else {
            //set convenience variables in top frame (wherever that is)
            top.pbBodyLoaded = false;
            
            //set variables in phTopFrame
            phTopFrame.psBodyLoaded = phTopFrame.stripFromList(phTopFrame.psBodyLoaded, sPage);
        }
    }
    catch (err) {
        alert("error in load() function: " + err.description);
    }
    
    //return piPopupLevel as potentially useful information
    return piPopupLevel;
}

