//
// this file contains Javascript written by the webapp developers,
//  as opposed to "scripts.js" which is written by the HTML folks downstairs
//

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

String.prototype.trim = trimString;

	
    // ============================================================================
	
	/*
	 * used on the Search landing page to set instructor pull-down menus to
	 * their default values when the page is reloaded or 'backed' up to
	 *
	 * mcurrey
	 */
	function searchLandingOnLoad() {
		document.getElementById("select5step").selectedIndex = 0;
		document.getElementById("select7cash").selectedIndex = 0;
	}
	
	// ============================================================================
	
	
	/*
	 * use this when you have more than one submit button and 
	 *  you want to make sure the right one is 'clicked' when
	 *  the user presses the 'enter' key
	 *
	 * add it to each field in the form like this:
	 *   onkeypress="redirectEnterKey( event, 'btnName' )"
	 *
	 * where "event" is presumably part of the page namespace, and
	 *  btnName is the ID of the button that should be pressed
	 *
	 *
	 * cf http://www.devx.com/vb2themax/Tip/18846
	 *    http://www.cs.tut.fi/~jkorpela/forms/enter.html
	 *
	 * rgitzel
	 */
    function redirectEnterKey( event, buttonToClick )
    {
    	var button ;
    	
        
        // process only the Enter key
        if( event.keyCode == 13 )
        {
            // cancel the default submit
            //alert(event.type+" , "+event.which+" , "+event.cancel);
            event.returnValue = false ;
            event.cancel = true ;
            
            // submit to the specified button            
            button = document.getElementById( buttonToClick ) ;

            if( button != null )
            {
                // submit the form by programmatically clicking the specified button
	            button.click() ;
                return false;
            }
        }
        
    }
    	
	function submitForm(name)
	{
		var form = document.forms[name];
		form.submit();
	}
		
	function submitPeriod(period)
	{
		var field = document.getElementById("cboAPeriod");
		field.value = period;
		submitForm('selectorForm');
	}
	
	function submitForSorting(formName, sortBy)
	{
		var form = document.getElementById(formName);
		var field = document.getElementById("sortOrder");
		field.value = sortBy;
		var fakeButton = document.getElementById("selectedButton");
		fakeButton.value = "reload";
		form.submit();
	}
	


	// =================================================================================

	// This function enables/disables the save button on the save 
	// search dialog boxes
	function ToggleSave() {
		if (document.getElementById("searchName").value == "") {
			document.getElementById("btnSaveOn").style.display = "none";
			document.getElementById("btnSaveOff").style.display = "inline";
		} else {
			document.getElementById("btnSaveOn").style.display = "inline";
			document.getElementById("btnSaveOff").style.display = "none";
		}
	}
	
	// This function clears the text from the name and description
	// fields on thes save search dialog boxes
	function clearSave() {
		document.getElementById("searchName").value = "";
		document.getElementById("searchDescription").value = "";
		ToggleSave();
	}
	
	// =================================================================================

	// This function enables/disables a button with a specified name 
	// based on the value of the specified textbox
	function ToggleButton(buttonName, textBoxName) 
	{
         
        if ( document.getElementById(textBoxName).value.trim().length <= 0) 
		{
            SetButtonStatus(buttonName, false);
		} else {
			SetButtonStatus(buttonName, true);
		}
	}

	
	// =================================================================================

	/*
	 * empty all the text entry boxes in the given form
	 *
	 * rgitzel, 12.13.05
	 */
	function clearForm( form )
	{
		var i, list, el ;
		
		
		if( form != null )
		{
			list = form.elements ;
			
			for( i = 0 ; i < list.length ; i++ )
			{
				el = list[i] ;
				
				if( el.type == "text" )
				{
					el.value = "" ;
				}
			}
		}
	}
	
	// =================================================================================
	/*
	 *set style for the stock rows in the list of
	 *stocks table
	 */
	function setStyle( letter )
	{
		var i, row ;
		var tableHandle = document.getElementById("stockList");
		var rowList = tableHandle.getElementsByTagName("TR");
		var rowName;

		for (i = 0; i < rowList.length; i++)
		{
			rowName = rowList[i].getAttribute("name");
			if (rowName.substr(0, 1).toLowerCase() == letter.toLowerCase())
			{
				rowList[i].style.display = "";
			}
			else
			{
				rowList[i].style.display = "none";
			}
		}
	}
	
	// =================================================================================


	// Right strip whitespaces
	// Variables: strMyString - String to strip white space from
	function RTrim(strMyString)
	{
	    return (strMyString.replace(/^\s*/,""));
	}
	
	// =================================================================================


	// Left strip whitespaces
	// Variables: strMyString - String to strip white space from
	function LTrim(strMyString)
	{
	    return (strMyString.replace(/\s*$/,""));
	}
	
	// =================================================================================


	// Left & Right strip whitespaces
	// Variables: strMyString - String to strip white space from
	function LRTrim(strMyString)
	{
	    return (RTrim(LTrim(strMyString)));
	}
	
	// =================================================================================

	
	function toggleAnalyzeCopyButton() 
	{
		var txtFeild = document.getElementById("txtSymbolA") ;
		var btnCopy = document.getElementById("btnAnalyzeCopy") ;
		//alert(txtFeild.symbol.value) ;
		
		if (txtFeild.symbol.value.length > 0)
		{
			dur.disabled = false ;
		}
		else
		{
			dur.disabled = true ;
		}
		
		btnCopy.disabled = true ;
	}
	
	// =================================================================================

	
	function toggleButtonOnTextChange(txtID, buttonID) 
	{
		var txt = document.getElementById(txtID) ;
		var btn = document.getElementById(buttonID) ;
		//alert(txtFeild.symbol.value) ;
		
		if (LRTrim(txt.value).length > 0)
		{
			SetButtonStatus( buttonID, true );
		}
		else
		{
			SetButtonStatus( buttonID, false );
		}
		
	}
    
    var noFocus = false;
    var focusHere=null;
	// ============================================================================
	
	// autoFocus is used to set the default focus to the 
	// symbol snapshot input box for each page.
	function autoFocus()
	{   
        if(noFocus) return;
        
        if (focusHere!=null){
	        var specificFocus = document.getElementById(focusHere);
	        if ( specificFocus != null && specificFocus != undefined ) {
	            specificFocus.focus();
	            specificFocus.select();
	            return;
	        }
	    }
        
        specificFocus = document.getElementById("focusHere");
		if ( specificFocus != null && specificFocus != undefined ) {
            specificFocus.focus();
            specificFocus.select();
            return;
        }
        
        quotePanelFocus = document.getElementById("pageSymbolInput");
		if ( quotePanelFocus != null && quotePanelFocus != undefined ) {
            quotePanelFocus.focus();
            quotePanelFocus.select();
            return;
        }
        
		var symbolSnapForm = document.getElementById("symbolSearch");
		
		if ( symbolSnapForm != null && symbolSnapForm != undefined ) {		
			var inputBox = symbolSnapForm.symbol;
			if ( inputBox != null && inputBox != undefined ) {
			
				inputBox.focus();
                inputBox.select();
            }
		}
	}

    // ============================================================================
	
	// autoSwipe is used remove the contents of the input text box that
	// gets passed in ONLY if the default text is present.  For any other
	// values, the text in the input box will be selected.
	function autoSwipe( textBox )
	{
		if ( textBox != null && textBox != undefined ) {
			
			if ( textBox.value == 'Enter Symbol' )
				textBox.value = ''; // delete this text
			else 
				textBox.select();
				
		}
	}
	

    function reload(){
        window.location.reload(true);
    }

    // Returns the document object of the passed iframe.
    function getIFrameDocument(oIframe) {
      var oDoc = (oIframe.contentWindow || oIframe.contentDocument);
      if (oDoc.document) oDoc = oDoc.document;
      return oDoc;
    }