//This document contains reusable javascript functions
//Documentations standards are as follows:
//Each function will be preceeded by a short description of what it does
//A line with it's arguments with datatype, name and presumptions
//A line describing the return value, datatype and presumptions
//See the getURLValue function for an example.

//getURLValue
//Desc: Given the name, returns the value from a 'name=value construction in the QueryString.
//		If that construction is not found for the name passed, getURLValue returns the sDefault string.
//		The name must be preceeded by either an ampersand (ASCII 38) or a question mark (ASCII 63).
//		The value is terminated by either an ampersand (ASCII 38) or the end of the string to search.
//Args: sURL (string) the string to search for the name/value pair
//		sAttrib (string) the name of the name/value pair
// 		sDefault (string) the string to return if the name/value pair is not found
	 function getURLValue(sURL, sAttrib, sDefault)
	 {
	 	var bIgnoreCase = false;
	 	if(getURLValue.arguments.length > 3)
				{bIgnoreCase = getURLValue.arguments[3];}
		if(bIgnoreCase)
		{
			sURL = sURL.toLowerCase();
			sAttrib = sAttrib.toLowerCase();
		}

	 	sAttrib = sAttrib + "=";
		var iValStart = sURL.indexOf(sAttrib);
		if(iValStart == -1)
			{return(sDefault);}
		else
		{
			var sDelim = sURL.charAt(iValStart - 1);
			if((sDelim == "&") || (sDelim == "?"))
				{iValStart += sAttrib.length;}
			else
				{return(sDefault);}
		}
		var iValEnd = sURL.indexOf("&", iValStart);
		if(iValEnd == -1)
			{iValEnd = sURL.length;}
		return(sURL.substring(iValStart, iValEnd));
	 }

//repSubstring
//Desc: Returns a string with replacing the first sFind in sString with sReplace
//Args: sString (string) the string to find and replace the substring
//		sFind (string) the substring to find in sString
// 		sReplace (string) the string to substitue for sFind
	 function repSubstring(sString, sFind, sReplace)
	 {
	     var iStart = sString.indexOf(sFind);
	     if(iStart == -1)
	     {
	     	return(sString);
	     }
	     var iEnd = iStart + sFind.length;
	     var sRet = sString.substring(0, iStart) + sReplace + sString.substring(iEnd, sString.length);
	     return(sRet);
	 }//end repSubstring	
	 

//repAllSubstrings
//Desc: Returns a string replacing all instances of sFind in sString with sReplace
//Args: sOrigString (string) the string to find and replace the substring
//	sFind (string) the substring to find in sString
// 	sReplace (string) the string to substitue for sFind

	 function repAllSubstrings(sOrigString, sFind, sReplace)
	 {
	 	//alert("Entering repAllSubstrings");
	 	
	 	var iStart = sOrigString.indexOf(sFind);
	 	var sRet = sOrigString;
	 	var i = 1;
	 	
	 	while (iStart != -1)
	 	{
	 		//alert("found instance " + i);
	 		
	 		var iEnd = iStart + sFind.length;
	 		
	 		var sFirst = sRet.substring(0, iStart);
	 		var sLast = sRet.substring(iEnd, sRet.length);
	 		
	 		//alert("first part is: " + sFirst);
	 		//alert("replace is: " + sReplace);
	 		//alert("last part is: " + sLast);
	 		
	 		var sRet = sFirst + sReplace + sLast;
	 		iStart = sRet.indexOf(sFind);
	 		i = i+ 1;
	 	}
	 	//alert("sRet is repAllSubstrings is: " + sRet);
	 	return sRet;
		    
	 }//end repAllSubstrings
	 
	 
	 
	 
	 
	 
	 
	 /**Function stripNonNumericChars
		strips any non-numeric characters and returns the new string
	 */
	 function stripNonNumericChars(s)
	 {
		var sNewString = "";
		var sNumbers = "0123456789";
		if (s == null  || s == "")
		{
			return s;
		}
		for (var i = 0; i < s.length; i++)
		{
			sChar = s.charAt(i);
			if (sNumbers.indexOf(sChar) != -1)
			{
				sNewString = sNewString + sChar;
			}
		}

		return sNewString;
	 }


	function repCharCode(sString, iFind, iReplace)
	 {
	 	var sNewString = "";	 	
	 	for (i = 0; i < sString.length; i++)
	 	{
	 		iCode = sString.charCodeAt(i);
	 		
	 		if (iCode == iFind)
	 		{
	 			sNewString = sNewString + String.fromCharCode(iReplace);
	 		}
	 		else
	 		{
	 			sNewString = sNewString + String.fromCharCode(iCode);
	 		}
	 	}
	 	
	 	return sNewString;
	 }
	 
	 function formatUSPhone(sPhone)
	 {
	 	
	 	if (sPhone == null || sPhone.length != 10)
		{
			return sPhone;
		}
		
		
		sArea = sPhone.substring(0,3);
		sPrefix = sPhone.substring(3,6);
		sSuffix = sPhone.substring(6,10);
		
		return "(" + sArea + ") " + sPrefix + "-" + sSuffix;
	}
	
	function launchStaticWindow(sURL, iWidth, iHeight)
	{  
	  //generate unique value to force reload of URL
	  dTemp = new Date();
  	  sUnique = dTemp.getTime();
  	  sURL += "&" + sUnique
  	  
  	  if (launchStaticWindow.arguments.length > 3) {
	  	sName = launchStaticWindow.arguments[3];
  	  }
  	  else {
  	  	sName = "PopUpWindow";
  	  }
  	  
	  sOptions = "toolbar=0,location=0,directories=0,menubar=0,scrollbars=0,resizable=0,width=" + iWidth + ",height=" + iHeight;
	  window.open(sURL, sName, sOptions); 
	  
	}
	
	function launchFullWindow(sURL, iWidth, iHeight, sName)
	{  
	  //generate unique value to force reload of URL
	  //dTemp = new Date();
	  //sUnique = dTemp.getTime();
	  //sURL += "&" + sUnique
	  
	  sOptions = "toolbar=1,location=1,directories=1,menubar=1,scrollbars=1,resizable=1,width=" + iWidth + ",height=" + iHeight;
	  window.open(sURL, sName, sOptions);
	}	
	
	function launchResizeWindow(sURL, iWidth, iHeight, sName)
	{  
	  //generate unique value to force reload of URL
	  //dTemp = new Date();
	  //sUnique = dTemp.getTime();
	  //sURL += "&" + sUnique
	
	 sOptions = "toolbar=0,location=0,directories=0,menubar=0,scrollbars=1,resizable=1,width=" + iWidth + ",height=" + iHeight;
	 window.open(sURL, sName, sOptions); 
	  
	}
	
	function launchNewFullWindow(sURL, iWidth, iHeight, sName)
	{  
	  //generate unique value to force reload of URL
	  dTemp = new Date();
	  sUnique = dTemp.getTime();
	  sURL += "&" + sUnique

	  sOptions = "toolbar=1,location=1,directories=1,menubar=1,scrollbars=1,resizable=1,width=" + iWidth + ",height=" + iHeight;
	  window.open(sURL, sName, sOptions);
	}
	
	function redirect (sHost, sPathname, bIncludeQS)
	{
		sCurrentHost = location.host;
		sCurrentPathname = location.pathname;
		sCurrentQS = location.search;
		sQS = "";
		
		if (sHost == null || sHost == "")
		{
			sHost = sCurrentHost;
		}
		if (sPathname == null || sPathname == "")
		{
			sPathname = sCurrentPathname;
		}
		if (bIncludeQS)
		{
			sQS = sCurrentQS;
		}
		
		location.replace("http://" + sHost + "/" + sPathname + "?" + sQS);
	
	}
	
	/**
	@Function String setFieldValue
		@Param String sName
		@Param String sValues
		@Param Form formPanel
		@Param String sType
		Sets the value(s) of a field and returns the former
		value(s) of that field.  If appropriate, splits multiple 
		new values by semi-colons and separates multiple old values 
		with semicolons,
	*/
	function setFieldValue (sName, sValues) 
	{
		//get the form argument
	  if (setFieldValue.arguments.length > 2) 
	  {
	    formPanel = setFieldValue.arguments[2];
	  }
	  else 
	  {
	    formPanel = document.forms[0];
	  }
	  
	  //get the type argument
	  if (setFieldValue.arguments.length > 3) 
	  {
	    sType = setFieldValue.arguments[3];
	  }
	  else 
	  {
	    sType = getFieldType(sName);
	  }
	  
	  var sOldVal = "";
	  
	  //just set the value attribute to the values seperated by semi-colons
	  if (sType == "text" || sType == "textarea" || sType == "hidden" || sType == "password") 
	  {
		  sOldVal = formPanel.elements[sName].value;		  
		  formPanel.elements[sName].value = sValues;
	  }
	  
	  //split the value on semi-colon for the select, checkbox and radio button options
	  var saValues = sValues.split(";");
	  
	  //de-select the currently selected item and select the item that matches the first value passed in
	  //note, ignoring any multiple values
	  if (sType == "select-one") 
	  {
	    var iSel = formPanel.elements[sName].selectedIndex;
	    if (iSel != -1) 
	    {
		  var eOpts = formPanel.elements[sName].options;
		  sOldVal = eOpts[iSel].value;
		  eOpts[iSel].selected = false;
		  //now find the item to select		  
		  for(var iOpts = 0; iOpts < eOpts.length; iOpts++)
		  {
			 if(eOpts[iOpts].value == saValues[0])
			 {
			   eOpts[iOpts].selected = true;			   
			   iOpts = eOpts.length;
			 }
		  }
	    }
	  }
	  
	  //de-select the currently selected items and select the items matching the values passed in
	  if (sType == "select-multiple") 
	  {
		eOpts = formPanel.elements[sName].options;
	    for(var iOptions = 0; iOptions < eOpts.length; iOptions++) 
	    {
		  //check to see if it's selected
	      if(eOpts[iOptions].selected)
	      {
	        sOldVal = sOldVal + eOpts[iOptions].value + ";";
	        eOpts[iOptions].selected = false;
	      }
	      //check to see if it should be selected
	      for(var iVals = 0; iVals < saValues.length; iVals++)
	      {
			  if(eOpts[iOptions].value == saValues[iVals])
			  {
				  eOpts[iOptions].selected = true;
			  }
		  }				  
	    }
	    sOldVal = sOldVal.substring (0, (sOldVal.length - 1));
	  }
	  
	  //uncheck currently checked items and check the items matching the values passed in
	  //Note: only selects the radio item that matches the first value
	  if (sType=="checkbox" || sType == "radio") 
	  {
	    var sCheckName;
	    for(var iNum = 0; iNum < formPanel.elements.length; iNum++) 
	    {
	      sCheckName = formPanel.elements[iNum].name;
	      
	      //check to see that the item name matches
	      while(sName == sCheckName) 
	      {
			//check if this item is checked
	        if(formPanel.elements[iNum].checked) 
	        {
	            sOldVal = sOldVal + formPanel.elements[iNum].value + ";";
	            formPanel.elements[iNum].checked = false;
	        }
	        
	        //check if it should be checked
	        for(var iVal = 0; iVal < saValues.length; iVal++)
	        {
			  if(formPanel.elements[iNum].value == saValues[iVal])
			  {
				  formPanel.elements[iNum].checked = true;
			  }
		    }
		    
	        //check the next item
	        iNum++;
	        if(iNum < formPanel.elements.length) 
	        {
	          sCheckName = formPanel.elements[iNum].name;
	        }
	        else 
	        {
	          sCheckName = "";
	        }
	      }
	    }
	    sOldVal = sOldVal.substring (0, (sOldVal.length - 1));
	  }
	  
	//return what we've gotten
	return sOldVal;
}


/**
	@Function boolean isMac
	Returns true if the client browser platform is a Macintosh	
*/
function isMac()
{
	if ((navigator.platform).indexOf("Mac") != -1)
	{
		//alert("is Mac");
		return true;
	}
	return false;
}

function hasAcrobatPlugin()
{
	//alert(navigator.plugins.length);
	for (var i = 0; i < navigator.plugins.length; i++)
	{
		if (navigator.plugins[i].name == "PDFViewer")
		{
			return true;
		}
	}
	return false;
}

/**
	@Function boolean isMacOSX
	Returns true if the client browser platform is a Macintosh	
*/
function isMacOSX()
{
	//alert("start isOSX");
	
	if ((navigator.platform).indexOf("Mac") != -1 && (navigator.userAgent).indexOf(5.2) != -1)
	{
		//alert("is OS X");
		return true;
	}
	return false;
}

/**
	@Function boolean isIE55
	Returns true if the client browser platform is a IE 5.5
*/
function isIE55()
{
	if ((navigator.appVersion).indexOf("MSIE 5.5") != -1)
	{
		//alert("it is IE 5.5");
		return true;
	}
	return false;
}

/**
	@Function boolean isIE6
	Returns true if the client browser platform is a IE 6.x
*/
function isIE6()
{
	if ((navigator.appVersion).indexOf("MSIE 6.") != -1)
	{
		//alert("it is IE 6");
		return true;
	}
	return false;
}


/**
	@Function void printPage
	Prints the document in the current window
*/
function printPage()
{  
	//mac's don't print
	if (isMac())
	{
		return false;
	}
	
	if (window.print) 
	{
		window.print() ;  
	} 
	else
	{
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
	}
}


HashHolder = new Array();
iNumEntries = 0;

function Hashtable(sKey, sValue)
{	
	this.key = sKey;
  	this.val = sValue;
}

function putInHash(sKey, sVal)
{	
  	//check to see whether this field is already in the field info array
  	bIsNew = true;
  	for (i = 0; i < HashHolder.length; i++)
  	{
  		if (HashHolder[i].key == sKey)
  		{
  			HashHolder[i] = new Hashtable(sKey, sVal);
  			i = HashHolder.length;
  			bIsNew = false;
  		}
  	}
  	
  	if (bIsNew)
  	{  	
		//add the fieldinfo to the field information array
		HashHolder[iNumEntries] = new Hashtable(sKey, sVal);
		iNumEntries++;
	}
  	
}

function getFromHash(sKey)
{
	for (i = 0; i < HashHolder.length; i++)
	{
		if (HashHolder[i].key == sKey)
		{
			return HashHolder[i].val;
		}
  	}
  	
  	return null;
}

function getStyleWidth (obj)
{
	var sWidth = obj.style.width;
	return parseInt(sWidth);


}

function formatNumber(objNum, iDecPlaces)
{
	var sVal = "" + Math.round (eval(objNum) * Math.pow(10, iDecPlaces));
	while (sVal.length <= iDecPlaces)
	{
		sVal = "0" + sVal;
	}
	var iDecPoint = sVal.length - iDecPlaces;
	return sVal.substring(0, iDecPoint) + "." + sVal.substring(iDecPoint, sVal.length);

}

/**
* boolean contains(array, value)
* returns true if array is an array and one of it's elements equals value
* returns false otherwise
*/
function contains(array, value) {
	if(array != null && array.length != null) {
		for(var a=0; a<array.length; a++) {
			if(array[a] == value) {
				return(true)
			}
		}
	}
	return(false);
}

/**
* boolean countElements(array, value)
* if array is an array, returns the number of elements in array that equal value
* if array is not an array, returns 0
*/
function countElements(array, value) {
	var ret = 0;
	if(array.length != null) {
		for(var a=0; a<array.length; a++) {
			if(array[a] == value) {
				ret++;
			}
		}
	}
	return(ret);
}

/**
* String setReportParams(sQry, sDelim, sMultiValueSep)
* loops through all fields on form replacing any instance
* of sDelim + field.name + sDelim with the value of field.
*/
function setReportParams(sQry, sDelim, sMultiValueSep) {
	var ret = sQry;
	var val = "";
	var lastName = "";
	var thisVal, fld, name;
	var formPanel = document.forms[0];
	
	if(sMultiValueSep == null) {
		sMultiValueSep = ";";
	}
	
    for(var iNum = 0; iNum < formPanel.elements.length; iNum++) 
    {
      fld = formPanel.elements[iNum];
      name = fld.name;
      //alert("Getting value of " + name);
      thisVal = getFieldValue(name);
      //alert("Value = " + thisVal);
      
      if(name == lastName) {
      	//alert("Appending value...");
      	val += sMultiValueSep + thisVal;
      } else {
      	//alert("Setting value in Qry: " + sDelim + lastName + sDelim + " - " + val);
      	//if it's a new field, set the last field...
      	ret = repAllSubstrings(ret, sDelim + lastName + sDelim, val);
      	//and store this value
      	val = thisVal;
      }
      
      lastName = name;
      
    }//end element for loop
    
    //catch the last field...
	ret = repAllSubstrings(ret, sDelim + lastName + sDelim, val);
	
    //alert("returning " + ret);
	return(ret);
}
