	/* ********************************************************************
    
    NOTE: This script is designed to be included before any other script,
    due to the fact it handles browser and document.all issues! dw 8/7/01
	
	This is the Utility.js client side page, which contains commonly 
	used JavaScript functions required by the RQQ Replacement web site
	It consists of the following functions:
	1)	browserSniffer
			This code should be run as soon as the Utility.js page is loaded.  
			It detects what browser configuration is being used.
	2)	searchorama()
			This function is passed a search string, and will search all the 
			producers in the Producer List box.  If a match is found, the first
			line which matches will be selected.  If a match is not found,
			the user will be alerted with a message to that effect.
	3)  getProducer(strAction)
			Identify the subproducer/producer selected for identity change
			Submit if a subproducer
	4)	submitProducer() 
			This routine is invoked on double-click of the Producer Select tag.  
			The requested producer will be returned to DBA.
	5)  openSizedWindow()
			This routines opens a new window of a specified size and focuses 
			on it.
			rg - adapted from fmc function on QuoteList.asp
	6)	getObjById(objectID,withStyle)
			This function retrieves an object reference for a page element
			or style, referenced correctly for each browser version.  
			It requires the string variable for browser version, automatically 
			defined below, which is returned by browserSniffer();
	7)	writeToDiv(divId, strMessage, strMessageClass)
			This function writes a message to a specified <DIV> HTML element.
	8)	useStandardStylesheet()
			This function writes a stylesheet link based on the browser in use.
			This is necessary to adjust for the differences in CSS support.
	9)  fakeDisableCheckbox(obj)
			simulates disabled checkbox in ns4
	10)	fakeDisableRadio(oRadio)
			simulates disabled radio in ns4
	11)	2 lines of executable code to establish the browser variable and 
			the docAll variable for the page.
	12)	changeVisibility(objname,visibility,...)
			change the visibility of the given object in the arguments.
	
	 ********************************************************************* */

	/**********************************************************************/
	//
	//  Executable code below:
	// 
	/**********************************************************************/
    var strBrowser = browserSniffer();
    // search variables required by searchorama()
    var lastSearchString = "";
    var lastSearchIndex = 0;

	
    /**********************************************************************
	//
	// browserSniffer()
	//
	// Purpose:  this function checks the client browser type and returns
	//           the browser type as a string code
	//
	// Arguments:  - none -
	//
	// Returns:  the following are the possible return values:
	//                'ie4' -- Internet Explorer ver. 4.x
	//                'ie5' -- Internet Explorer ver. 5.x
	//                'ns6' -- Netscape Navigator ver. 6.x
	//                'unsupported' -- anything not in the above categories
	//
	**********************************************************************/
	function browserSniffer() {
		var browsertype = "unsupported";
		var ie4andUp = (document.all) ? true : false;
		var ie4 = false;
		var ie5 = false;
		var ns62andup = false;
		var ns4 = (document.layers) ? true : false;
		var ns6 = (document.getElementById && !document.all) ? true : false;
		
		
		if (ie4andUp) {
			var appVer = navigator.appVersion.toLowerCase();
			var iePos  = appVer.indexOf('msie');
			if (iePos != -1) {
				var verMinor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
   				var verMajor = parseInt(verMinor);
				if (verMajor >= 5) {
					ie5 = true;
				}
			}
		}
		if (ns6) {
			var nsPos  = navigator.userAgent.indexOf('Netscape6/6.1');
			if (nsPos == -1) {
				var ns62andup = true;
			}
		}

		if (ie5) browsertype = "ie5";
		else if (ns62andup) browsertype = "ns6";
		
		return browsertype;
	}

	
	/**********************************************************************/
    //
	//    searchCode(strChar)
	//	This function is passed a search string, and will search all the 
	//	producers in the Producer List box.  If a match is found, the first
	//	line which matches will be selected.  If a match is not found,
	//	the user will be alerted with a message to that effect.
	// 
	/**********************************************************************/
	function searchCode()
	{
		var tempString = document.frmSearch["searchstring"].value;
		var sLength = tempString.length;
		var oControl = document.frmSearch["producerlist"];
		var isMatch = false;
		
		for (i = 0; i < oControl.length; i++)
		{
			var tempVal = oControl.options[i].text;
			tempVal = tempVal.substring(0, sLength);
			if (tempVal.toUpperCase() == tempString.toUpperCase())
			{
				oControl.options[i].selected = true;
				isMatch = true;
				break;
			}
		}
		if (isMatch)
		{
			// it's good
			//getProducer("producer");
		}else{
			alert("No match was found for your search.");
		}
	}
	
	/**********************************************************************/
    //
	//  searchorama()
	//	This function will search all the 
	//	producers in the Producer List box.  If a match is found, the first
	//	line which matches will be selected.  If a match is not found,
	//	the user will be alerted with a message to that effect.
	//  DW 8/15/01
	// 
	/**********************************************************************/
    function searchorama(){
      //var searchString = document.frmSearchEnter["searchstring"].value; 
			var searchString = document.frmSearchEnter.searchstring.value; 
      var testval = 1;
      //var oControl = document.frmSearch["producerlist"];
			var oControl = document.frmSearch.producerlist;
      var searchMax = oControl.length;
      var searchCounter = 0;
      var blnSearchAgain = false;

      // re-search functionality variables
      if (searchString == lastSearchString){
      	searchCounter = lastSearchIndex;
      	blnSearchAgain = true;	      		
      } 
      
      
      searcherString = ("^" + searchString ); //to search beginning of a word
      re = new RegExp();
      re.compile(searcherString, "i"); // compile regex to be case-insensitive
      
      /* Loop thru each option until a match or the options are exhaustes */
      while (testval) {
        
        	/* Test each option in order to see if there's a match */
         if(re.test(oControl.options[searchCounter].text)){

              oControl.selectedIndex = searchCounter;         
              testval = 0;  //so we exit the loop
              if ((searchCounter +1)< searchMax){
                lastSearchIndex = (searchCounter + 1); //update the global
              } else
                lastSearchIndex = 0;
              lastSearchString = searchString; // for the next search
       	 }
				 
          /* Test to see if the search is exhausted */
          if (testval != 0){
          		searchCounter+=1;
          		if (searchCounter >= searchMax){
              		if (blnSearchAgain == false) {
              			testval = 0;
              			alert("Search did not find a match for " + searchString);
              			lastSearchIndex = 0;
              			lastSearchString = "";
              		} else {
              			// since we didn't find any more matches...
              			// reset to the beginning as if you were 
              			searchCounter = 0;
              			blnSearchAgain = false;
              		}
          		}
          }
      }
      return (false); //so we don't actually try to post something
    }
	
	/**********************************************************************/
    //
	//  getProducer(strAction)
	//	Identify the subproducer/producer selected for identity change
	//	Submit if a subproducer
	// 
	/**********************************************************************/
	function getProducer(strAction)
	{
		var sValue;
		// Get selected value
		if (strAction == "producer")
		{		
			// Set the selected value in the main form - have not double clicked,
			// have just searched for a producer
			sValue = document.frmSearch.producerlist.value;
			document.frmProducerInfo.TASK.value = "producer";
			document.frmProducerInfo.PROD.value = sValue;
		}else{
			//submit this request to ProducerInfo.asp - this is
			//a request for the data for a subproducer and their data.
			// Quoting identity will be the subproducer selected.
			document.frmProducerInfo.TASK.value = "producerinfo";
			//sValue = document.frmProducerInfo["alphanum-0-subproducercode"].value;
			var oForm = document.forms["frmProducerInfo"];
			for (var i=0;i<=oForm.length;i++){
				var oControl = oForm.elements[i];
				if (oControl.name == "alphanum-0-subproducercode") {		
					sValue = oControl.value
					if (sValue != null) {
						sValue = oControl.value;
					}else{
						sValue = oControl.options[oControl.selectedIndex].value;
					}
					break;
				}
			}
			if ((sValue == "") || (sValue == null))
			{
				// Submit request is from the QuoteList page for all subproducers. get the list again. No Inspire
				document.frmQuoteList.SUBPROD.value = "";
				document.frmQuoteList.submit();
			}else{
				document.frmProducerInfo.submit()
			}
		}		
	}
	
	
	/**********************************************************************/
    //
	//  submitProducer() 
	//	on doubleclick this routine is invoked.  The requested producer
	//	will be returned to DBA
	// 
	/**********************************************************************/
	function submitProducer() 
	{
		var oControl = document.frmSearch["producerlist"]; 	// Set select control holder
		var iIndex = oControl.selectedIndex;		// retrieve selected index
		var sValue = oControl.options[iIndex].value	// retrieve value at selected index
		// set hidden variables to capture chosen value
		document.frmProducerInfo.TASK.value = "producer";		 
		document.frmProducerInfo.PROD.value = sValue;
		// hacky - the "All" value is only available from QuoteList, so is SUBPROD...
		if ((sValue == "") || (sValue == null))
		{
			// Submit request from the QuoteList page for all producers. get the list again. No Inspire
			document.frmQuoteList.PROD.value = "";
			document.frmQuoteList.SUBPROD.value = "";

			document.frmQuoteList.submit();
		}else{
			document.frmProducerInfo.submit();
		}
	}
	
	
    /**********************************************************************/
    //
	//  openSizedWindow(pagename, title, itop, ileft, iwidth, iheight)
	//	opens a new window of a specified size and focuses on it
	//  rg - adapted from fmc function on QuoteList.asp
	// 
	/**********************************************************************/
	function openSizedWindow(pagename, title, itop, ileft, iwidth, iheight)
	{
		var openedPage;
		// Do the page opening
		openedPage = window.open(pagename, "popupWindow", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=" + iwidth + ",height=" + iheight + ",top=" + itop + ",left=" + ileft);
		openedPage.focus();
	}
    
	
	/**********************************************************************/
    //
	//	getObjById(objectID,withStyle)
	//	This function retrieves an object reference for a page element
	//	or style, referenced correctly for each browser version.  
	//	It requires the string variable for browser version, automatically 
	//	defined below, which is returned by browserSniffer();
	// 
	/**********************************************************************/
	function getObjById(objectID,withStyle) {
		if (withStyle == 1) {
			if ((strBrowser == "ie5") || (strBrowser == "ns6")) { 
				return (document.getElementById(objectID).style) ; 
			}
			else { 
				if (strBrowser == "ie4") { 
					return (document.all[objectID].style); 
				}
				else {
					if (strBrowser == "ns4") { 
						return (document.layers[objectID]); 
					}
				};
			}
		}
		else {
			if (strBrowser == "ns6") { 
				return (document.getElementById(objectID)) ; 
			}
			else { 
				if ((strBrowser == "ie5") || (strBrowser == "ie4")) { 
					return (document.all[objectID]); 
				}
				else {
					if (strBrowser == "ns4") { 
						return (document.layers[objectID]); 
					}
				};
			}
		}
	}
	
	
	/**********************************************************************/
    //
	//	writeToDiv(divId, strMessage, strMessageClass)
	//	This function writes a message to a specified <DIV> HTML element.
	//  Note that there are two key dependencies here:
	//		1) the "strBrowser" variable defined below, 
	//		2) the "getObjById" function defined above, and 
	//		3) absolute positioning of the <DIV> tags in the stylesheet.
	// 
	//	Item 3 is necessary for NS4-complience.
	//	E.G.	#test {
	//				position: absolute;
	//				top: 400px;
	//				left: 80%;
	//				width: 15%;
	//				padding: 0px;
	//			}
	/**********************************************************************/
	function writeToDiv(divId, strMessage, strMessageClass) {
		var oDiv;
		if ((typeof divId) == 'object') {
			oDiv = divId;
		}
		else {
			oDiv = getObjById(divId, 0);
		}
		var strHtmlMessage = '<p class="' + strMessageClass + '">' + strMessage + '</p>';
		
		if ((strBrowser == "ie5") ||
		    (strBrowser == "ns6")  || 
		    (strBrowser == "ie4")
		   ) {
			oDiv.innerHTML = strHtmlMessage;
		}
		//else if (strBrowser == "ns4")
	//	{
	//		oDiv.document.open();
	//		oDiv.document.write(strHtmlMessage);
	//		oDiv.document.close();
	//	}
		else {
			// Incompatible browser - display in an alert
			alert(strMessage);
		}
	}
	
	
	/**********************************************************************/
    //
	//	useStandardStylesheet()
	//	This function writes a stylesheet link based on the browser in use.
	//	This is necessary to adjust for the differences in CSS support.
	//
	/**********************************************************************/
	function useStandardStylesheet() {
		switch (strBrowser) {
			case 'ns4' :
				document.write('<link rel="stylesheet" type="text/css" href="ns4.css">'); 
				break;
			case 'ie4' :
			case 'ie5' :
			case 'ns6' :
				document.write('<link rel="stylesheet" type="text/css" href="ie5.css">'); 
				break;
			default :
				document.write('<link rel="stylesheet" type="text/css" href="ie5.css">'); 
				break;
		} 
	}
	
			
	/**********************************************************************/
	//
	//  Function to disable check boxes in ns4
	// 
	/**********************************************************************/

		function fakeDisableCheckbox(obj){
	    var blnReverse = !obj.checked;
	    obj.onMouseUp=(obj.checked=blnReverse);
		}   
		

	/**********************************************************************/
	//
	//  Function to disable radio buttons in ns4
	// 
	/**********************************************************************/

	//	function fakeDisableRadio(obj){
	//    var iCheckedIndex = 0;
	//    for (var i=0;i < document.radios.radio.length; i++){
	//        if (document.radios.radio[i].checked){
	//            iCheckedIndex = i;
	//            document.radios.radio[iCheckedIndex].checked = true;
	//            alert("value is currently uneditable");
	 //           break;  //to get around radio behavior
	 //       }
	 //   }
	//}  
	
			function fakeDisableRadio(oRadio){
		// !!! short term hack- but is essentially performing the right action...
		alert("value is currently uneditable");		
				// This function is not functional, but if it goes away, the hack does not work...								
		   	for (var i=0;i < oRadio.length; i++){
	       	if (oRadio[i].checked){
	            iCheckedIndex = i;
	            oRadio[iCheckedIndex].checked = true;
	          	alert("value is currently uneditable");
	            break;  //to get around radio behavior
	       	}
		    }
			}

	/***********************************************************************/
	//
	//  Function to change visibility of a object
	//
	/***********************************************************************/
	
		function changeVisibility() {
			var inc, endInc=arguments.length;
			// run through the args (objects) and set the visibility of each
			for (inc=0; inc<endInc; inc+=2) {
				// get a good object reference
				var daObj = getObjById(arguments[inc],1);
				if (arguments[inc+1] == hidden) {
					// hide the object
					daObj.visibility = hidden;
				} else if (arguments[inc+1] == visible) {
					// show the object
					daObj.visibility = visible;
				} else if (arguments[inc+1] == toggle) {
					// toggle the object's visibility
					if (daObj.visibility == visible) {
						daObj.visibility = hidden;
					} else if (daObj.visibility == hidden) {
						daObj.visibility = visible;
					}
				}
			}
		}
		
		function changeWidth() {
			var inc, endInc=arguments.length;
			// run through the args (objects) and set the visibility of each
			for (inc=0; inc<endInc; inc+=2) {
				// get a good object reference
				var daObj = getObjById(arguments[inc],1);
				daObj.height = arguments[inc+1];
			}
		}
		
// This function is used to define if the browser supports the needed
// features
function hasSupport() {

	if (typeof hasSupport.support != "undefined")
		return hasSupport.support;
	
	var ie55 = /msie 5\.[56789]/i.test( navigator.userAgent );
	
	hasSupport.support = ( typeof document.implementation != "undefined" &&
			document.implementation.hasFeature( "html", "1.0" ) || ie55 )
			
	// IE55 has a serious DOM1 bug... Patch it!
	if ( ie55 ) {
		document._getElementsByTagName = document.getElementsByTagName;
		document.getElementsByTagName = function ( sTagName ) {
			if ( sTagName == "*" )
				return document.all;
			else
				return document._getElementsByTagName( sTagName );
		};
	}

	return hasSupport.support;
}

