/*----------------------------------------------------------------------
 File Name		: GlobalFunctions.js
 Description	: Holds constants, variables and global functions 
 Date Created	: 11/27/2001
---------------------------------------------------------------------- */

//Browser Values
var NS4 = (document.layers);
var IE4 = (document.all);
var VER4 = (NS4 || IE4);

//Top Menu functions
if (VER4)
{
	secondIm = "<IMG SRC=\"<%=G_URL_ROOT%>images/Headerover.gif\" width=\"560\" height=\"30\" border=\"0\" alt=\"top menu\" usemap=\"#Htopmenu\">";
	arPopups = new Array();
}

//Currently selected top menu
var mnuSelected = '';

// whitespace characters
var whitespace = " \t\n\r";



function setBeginEnd(which, from, to)
{
	arPopups[which] = new Array();
	arPopups[which][0] = from;
	arPopups[which][1] = to;
}

if (VER4)
{
	setBeginEnd(1,199,253);
	setBeginEnd(2,256,350);
	setBeginEnd(3,358,480);
	setBeginEnd(4,486,610);
	setBeginEnd(5,614,685);
	setBeginEnd(6,690,754);
}

function mapOver(which,on)
{
	clTop = 1;
	clBottom = 30;
	
	if (!VER4) {return;}
	
	if (IE4)
	{
		whichE1 = document.all.topMenuOver.style;
	}
	else
	{
		whichE1 = document.topMenu.document.topMenuOver;
	}
	
	if (!on) {whichE1.visibility = "hidden"; return};
	
	clLeft = arPopups[which][0];
	clRight = arPopups[which][1];
	
	if (NS4)
	{
		whichE1.clip.left = clLeft;
		whichE1.clip.right = clRight;
	}
	else
	{
		whichE1.clip = "rect(" + clTop + " " + clRight + " " + clBottom + " " + clLeft + ")";
	}
	
	whichE1.visibility = "visible";
}

function topMenuDropDown(menu)
{
	//If the same menu is clicked again, hide it
	if (mnuSelected == menu)
	{
		docjslib_setVisibility(menu, 0);
		mnuSelected = '';
	}
	else
	{
		if (mnuSelected != '')
		{
			docjslib_setVisibility(mnuSelected, 0);
		}			
		docjslib_setVisibility(menu, 1);			
		mnuSelected = menu;		
	}		
}

/* Global Functions */

function OpenEnlargedImageWindow(sURL,nHeight,nWidth,nLeft,nTop)
{
	var strWindowFeatures;
	
	if (NS4)
	{
		strWindowFeatures = "width="+nWidth+",height="+nHeight+",screenY="+nTop+",screenX="+nLeft+",dependent=yes,resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes";
	}
	else
	{
		strWindowFeatures = "width="+nWidth+",height="+nHeight+",top="+nTop+",left="+nLeft+",resizable=yes,menubar=no,status=no,toolbar=no,scrollbars=yes";
	}
	
	window.open(sURL,'EnlargedImage',strWindowFeatures);
}

function ChangeCursor(oElement, CursorStyle)
{
	oElement.style.cursor = CursorStyle;
}

function docjslib_setVisibility(id, flag) {
	if (NS4) {
	  var str = (flag) ? 'show' : 'hide';
	  eval("document." + id).visibility = str;
	}
	else {
	  var str = (flag) ? 'visible' : 'hidden';
	  eval("document.all." + id).style.visibility = str;
	}
}

function OpenNewWindow(sURL)
{
	var strWindowFeatures;
	
	if (NS4)
	{
		strWindowFeatures = "width=600,height=400,screenY=0,screenX=0,dependent=yes,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes,scrollbars=yes";
	}
	else
	{
		strWindowFeatures = "width=600,height=400,top=0,left=0,resizable=yes,location=yes,menubar=yes,status=yes,toolbar=yes,scrollbars=yes";
	}
	
	window.open(sURL,'',strWindowFeatures);
}

function CheckRequired(aInputObj)
{

	var bResult = true;
	var bFocusSet = false;
	var bErrorFound = false;
	var ErrorColor = "#FF8080";
	var CorrectColor = "";
	var nIndex, nCount;
	var elementType;
	
	nCount = aInputObj.length;
	for (nIndex = 0; nIndex < nCount; nIndex++)
	{
	
		//initialize error flag
		bErrorFound = false;
		
		//get the form elements and do the not null validation
		oInput = aInputObj[nIndex];
		elementType = oInput.type;
		
		if (oInput.type == "text")
		{
			if ( (oInput.value == "") || (oInput.value.length == 0) )
			{
				if (!bFocusSet)
				{
					oInput.focus();
					bFocusSet = true;
				}
				
				bErrorFound = true;
				bResult = false;
			}
		}
		

		if (oInput.type == "textarea")
		{
			if ( (oInput.value == "") || (oInput.value.length == 0) )
			{
				if (!bFocusSet)
				{
					oInput.focus();
					bFocusSet = true;
				}
				
				bErrorFound = true;
				bResult = false;
			}
		}			
		
		if (oInput.type == "password")
		{
			if ( (oInput.value == "") || (oInput.value.length == 0) )
			{
				if (!bFocusSet)
				{
					oInput.focus();
					bFocusSet = true;
				}
				
				bErrorFound = true;
				bResult = false;
			}
		}

		if ( (oInput.type == "select-one") || (oInput.type == "select-multiple") )
		{
			if ( (oInput.value == "") || (oInput.value.length == 0) )
			{
				if (!bFocusSet)
				{
					oInput.focus();
					bFocusSet = true;
				}
				
				bErrorFound = true;
				bResult = false;
			}
		}			

		//Highlight cell if input error found
		if (bErrorFound)
		{
			SetInputColor(oInput,ErrorColor);
		}		
		else
		{
			SetInputColor(oInput, CorrectColor);
		}
	}			
	
	return bResult;
}		

function SetInputColor(oInput, strColor)
{
	if (NS4)
	{
		oInput.backgroundColor = strColor;
	}
	else
	{
		if (oInput.style)
		{
			oInput.style.backgroundColor = strColor;
		}
	}
}

function TrimSpaces(oForm)
{
	var nIdx, nElements, nTrimIndex, nStringLen;
	var StringText;
	var oInput;
	
	nElements = oForm.elements.length;
	for(nIdx = 0; nIdx < nElements; nIdx++)
	{
		oInput = oForm.elements[nIdx];
		if ( (oInput.type == "text") || (oInput.type == "textarea") )
		{
			StringTest = oInput.value;
			nStringLen = StringTest.length;
			
			if (nStringLen > 0)
			{
				nTrimIndex = 0;
				
				// check each character until non-blank character found
				while ( nTrimIndex < nStringLen)
				{
					if (StringTest.charAt(nTrimIndex) == ' ')
						nTrimIndex++;
					else
						break;						
				}
				
				//User entered all blanks
				if (nTrimIndex == nStringLen)
					oInput.value = '';
				else
				{
					//trim blank spaces at the beginning
					if (nTrimIndex > 0)
						oInput.value = StringTest.substr(nTrimIndex-1);					
				}						
			}
		}
	}
}

//Used to add to * to the search text
function AddWildCard(oInput,oSource)
{
	var c = oSource.value;
	
	c = stripInitialWhitespace(c);

	if (isOneWord(c))
	{
		//If the word entered is a word for which a special page has been created
		//don't add *
		if (isSearchWord(c.toUpperCase()))
		{
			oInput.value = c;
		}
		else
		{
			//If the word entered is a word for which a special search header has been
			//created, don't add *
			if (isSearchHeader(c.toUpperCase()))
			{
				oInput.value = c;
			}
			else
			{
				oInput.value = c  + '*';
			}				
		}
	}
	else
	{
		oInput.value = c;	
	}		
		//alert(oInput.value);
}

var USSearchWordDelimiter = "|";
var USSearchWords = "INTEGRIN|INTEGRINS|INTERGIN|INTERGINS|MMP|MMPS|MATRIX METALLOPROTEINASE|MATRIX METALLOPROTEINASES|ANTIBODY|ANTIBODIES"

function isSearchWord(s)
{   
    return ( (USSearchWords.indexOf(s) != -1) &&
             (s.indexOf(USSearchWordDelimiter) == -1) )
}

var USSearchHeaderDelimiter = "|";
var USSearchHeaders = "PHOSPHO|PHOSPHO-SPECIFIC|PHOSPHOSPECIFIC|PHOSPHO SPECIFIC"

function isSearchHeader(s)
{   
    return ( (USSearchHeaders.indexOf(s) != -1) &&
             (s.indexOf(USSearchHeaderDelimiter) == -1) )
}


// Removes initial (leading) whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.

function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && (whitespace.indexOf(s.charAt(i)) != -1))
       i++;
    
    return s.substring (i, s.length);
}

//Returns true if s contains only one word
function isOneWord (s)
{
   var i;

    // Search through string's characters one by one
    // until we find a whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) != -1) return false;
    }

    // one word
    return true;

}