// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// for Netscape 6/Mozilla by Thor Larholm thor@jscript.dk
// Usage: include this code segment at the beginning of your document
// before any other Javascript contents.

if (typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where){
		case 'beforeBegin':
			this.parentNode.insertBefore(parsedNode,this)
			break;
		case 'afterBegin':
			this.insertBefore(parsedNode,this.firstChild);
			break;
		case 'beforeEnd':
			this.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (this.nextSibling)
this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	}

	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	}
}

// Get any document from the page in any common browser
function getElemById(id)
{
	if (document.all!=null)
	{return eval('document.all.' + id);}
	else{return document.getElementById(id);}
}

//check if something is null
function elemNotNull(id)
{return (getElemById(id)!=null);}

// set the value of an element
function setElemValue(id,sValue)
{
	if(elemNotNull(id)){getElemById(id).value=sValue;}
}

// set the innerHTML of an element
function setElemInnerHTML(id,sValue)
{
	if(elemNotNull(id))
	{
	// Security : check for inner html validity
		if(getElemById(id).tagName=='INPUT')
		{
			raiseJavascriptError('element ' + id + ' is not a text string');
		}
		else
		{
			getElemById(id).innerHTML=sValue;
		}
	}
}

// rewrite an HTML element
function setElemOuterHTML(id,sValue)
{
	if(elemNotNull(id))
	{
		// Security : check for inner html validity
		if(getElemById(id).tagName=='INPUT'){raiseJavascriptError('element ' + id + ' is not a text string');}
		else{getElemById(id).outerHTML=sValue;}
	}
}

// return the innerHTML of an element
function getElemInnerHTML(id)
{
	if(elemNotNull(id)){return getElemById(id).innerHTML;}
}

// show an error message to the screen
function raiseJavascriptError(sError)
{
	top.status='Error: ' + sError;
}


// This function investigates an input and returns an int value, or 0 if there is no data to retrieve
function getIntValFromInput(sInputName)
{
	var nTotalTemp=0;
	var nTemp=parseInt(getValFromInput(sInputName).replace(',',''),10);
	if(!isNaN(nTemp)){nTotalTemp = nTemp;}
	return nTotalTemp;
}


// This function investigates an input and returns an int value, or 0 if there is no data to retrieve
function getValFromDropDown(sInputName, bAllowSelectFirstElement)
{
	sTemp='';
	if (bAllowSelectFirstElement == null){bAllowSelectFirstElement = false;}
	if (bAllowSelectFirstElement == 0)   {bAllowSelectFirstElement = false;}
	if (bAllowSelectFirstElement == 'no'){bAllowSelectFirstElement = false;}
	if (bAllowSelectFirstElement == '')  {bAllowSelectFirstElement = false;}

	if(elemNotNull(sInputName) && getElemById(sInputName).options!=null )
	{
		nSelectedIndex = getSelectedIndexFromDropDown(sInputName);
		if(nSelectedIndex>0
		|| (bAllowSelectFirstElement && nSelectedIndex==0))
		{
			sTemp=getElemById(sInputName).options[getSelectedIndexFromDropDown(sInputName)].value;
		}
		else
		{
			sTemp = '';
		}
	}
	return sTemp;
}

// This function investigates an input and returns an int value, or 0 if there is no data to retrieve
function getSelectedIndexFromDropDown(sInputName)
{
	var nTemp='';
	if(elemNotNull(sInputName) && getElemById(sInputName).options!=null )
	{
		nTemp=getElemById(sInputName).selectedIndex;
	}
	return nTemp;
}

// This sets the selected index of a dropdown to be a particular amount.
// Useful to use both methods
function setSelectedIndex(sInputName,nIndex)
{
	if(elemNotNull(sInputName) && getElemById(sInputName).options!=null )
	{
		getElemById(sInputName).selectedIndex = nIndex;
		getElemById(sInputName).options[nIndex].selected=true;
	}

}

// This function investigates an input and returns an int value, or 0 if there is no data to retrieve
function getIntValFromDropDown(sInputName)
{
	var nTotalTemp=0;
	var nTemp=parseInt(getValFromDropDown(sInputName),10);
	if(!isNaN(nTemp)){nTotalTemp = nTemp;}
	return nTotalTemp;
}

// This function investigates an input and returns an int value, or 0 if there is no data to retrieve
function getFloatValFromDropDown(sInputName)
{
	var fTotalTemp=0;
	var fTemp=parseFloat(getValFromDropDown(sInputName));
	if(!isNaN(fTemp)){fTotalTemp = fTemp;}
	return fTotalTemp;
}

// This function investigates an input and returns a float value, or 0 if there is no data to retrieve
function getFloatValFromInput(sInputName)
{
	fTotalTemp=0;
	if(elemNotNull(sInputName))
	{
		// incomplete
		sValue = getValFromInput(sInputName).replace('£','').replace('$','').replace('€','');
		var safetyvalve=0;
		if(sValue!=null)
		{
			while (sValue.indexOf(',')>-1 && safetyvalve<1000)
			{
				sValue=sValue.replace(',','');
				safetyvalve++;
			}
			if(!isNaN(parseFloat(sValue)))
			{
				fTotalTemp = parseFloat(sValue);
			}
		}
	}
	return fTotalTemp;
}

// This function investigates an input and returns a float value, or 0 if there is no data to retrieve
function getValFromInput(sInputName)
{
	fTotalTemp='';
	if(elemNotNull(sInputName))
	{
		fTemp=getElemById(sInputName).value;
		fTotalTemp += fTemp;
	}
	return fTotalTemp;
}

function jsFirstOrSecond(oFirst,oSecond)
{
	if(oFirst!=''){return oFirst;}
	else{return oSecond;}
}
function jsNumFirstOrSecond(oFirst,oSecond)
{
	if(oFirst!='' && oFirst!=0){return oFirst;}
	else{return oSecond;}
}// function to transfer the content of a form input into a string



function safeIsVisible(sElementID)
{
	var oItem = getElemById(sElementID);
	return safeIsObjectVisible(oItem);
}

// Takes an object and checks if it's visible
function safeIsObjectVisible(oItem)
{
	if(oItem!=null){return (oItem.style.display=='' || oItem.style.display=='block' || oItem.style.display=='inline');}
	else{raiseJavascriptError('Error checking item visibility: Item did not exist');}
	return false;
}


// turn something that's visible invisible and vice versa
function safeToggleVisibility(sElementID)
{
	if(elemNotNull(sElementID))
	{
		var oItem = getElemById(sElementID);
		if(oItem.style.display=='none'){makeVisibleSafe(sElementID,1);}
		else{makeVisibleSafe(sElementID,0);}
	}
	else
	{
		raiseJavascriptError('Error toggling item visibility: Item did not exist');
	}
}

// Safe way to make an item visible or not
function makeVisibleSafe(sElementID,bMakeVis)
{
	if(elemNotNull(sElementID))
	{
		var oItem = getElemById(sElementID);
		if(bMakeVis==1){oItem.style.display='';}
		else{oItem.style.display='none';}
	}
	else{
		raiseJavascriptError('Error visibling item visibility(' + sElementID + ', ' + bMakeVis + '): Item did not exist');
	}
	return false;
}

function isNull(field) {
	ofield = getElemById(field);
	return ofield!=null && ofield.value=='';
}

function strReplace(sWhatToFind,sWhatToPutInstead, sWhatToFindItIn)
{
	if(sWhatToFindItIn!=null && sWhatToFindItIn !='')
	{
		var sTempToFind = sWhatToFind;
		if(sTempToFind==''){sTempToFind = '"';}
		var safetyvalve = 0;
		var sTemp = sWhatToFindItIn;
		while ((sTemp.indexOf(sTempToFind)>-1) && safetyvalve<1000)
		{
			sTemp = sTemp.replace(RegExp(sTempToFind), sWhatToPutInstead);
			safetyvalve++;
		}
		return sTemp;
	}
	return '';
}

// check to see if something is an array
function is_array(oThingy)
{
  return (oThingy!=null && oThingy.length!=null);
}


function removeFromDropdown(oSelector, sValToRemove)
{
	var bFoundIt=false;

	if (oSelector != null
    	&& oSelector.options != null)
  {
		if(sValToRemove!=null)
		{
			for(var y=0;y<oSelector.options.length;y++)
			{
				if(oSelector.options[y].value == sValToRemove)
				{
					bFoundIt = true;
					// end this loop
					var nLocation = y;
//					oSelector.options[y] = null;
				}
			}
			for (var z=nLocation; z<oSelector.options.length-1; z++)
			{
				oSelector.options[z].text = oSelector.options[z+1].text
				oSelector.options[z].value = oSelector.options[z+1].value
			}
			oSelector.options[z] = null;
		}
	}
	return bFoundIt;
}

function convertDropdownToList(sSelectBox, sSeparator)
{
	var oDropDown = getElemById(sSelectBox);
	var sResult = '';
	if (sSeparator == null){sSeparator = ':::';}
	for (var x=0; x <oDropDown.options.length; x++)
	{
	  sResult += oDropDown.options[x].value + sSeparator;
	}
	return sResult;
}
//return true of something exists in a dropdown
function existsInDropdown(oSelector, sValToFind)
{
	var bFoundIt=false;
	var nNumber = null;
	for(var y=0;y<oSelector.options.length;y++)
	{
		if(oSelector.options[y].value == sValToFind)
		{
			bFoundIt = true;
			nNumber = y;
			// end this loop
			y = oSelector.options.length;
		}
	}
	return nNumber;
}

function clearDropdown(oDropDown, sExcept)
{
	if (oDropDown != null)
	{
	  if (oDropDown.options != null)
		{
			for(var nIndex=oDropDown.options.length-1;nIndex >= 0;nIndex--)
			{
				if(oDropDown.options[nIndex].value!=sExcept)
				{
					oDropDown.options[nIndex] = null;
				}
			}
		}
	}
}

function add_option(oDropDown, sText, sValue)
{
	if (oDropDown != null)
	{
	  oDropDown.options[oDropDown.options.length] = new Option(sText, sValue);
	}
}

/*alphabetically aware adding option code*/
function add_option_alpha(oDropDown, sText, sValue)
{

	var bSucceeded = false;
	if (oDropDown != null)
	{
	  // if the first item is a "please select"
		if (oDropDown.options       != null
		    && oDropDown.options[0] != null
				&& oDropDown.options.length > 0
				&&
				  (oDropDown.options[0].value    == '0'
					 || oDropDown.options[0].value == ''
					 || oDropDown.options[0].text  == 'Please Select...')
			)
		{
		  var first_elem = 1;
		}
		else
		{
		  var first_elem=0;
		}
		// ensure that if all the elements are null that we add in the correct place
		if (oDropDown.options.length == 0
		   || oDropDown.options[first_elem] == null)
		{
		  oDropDown.options[oDropDown.options.length] = new Option(sText, sValue);
		  bSucceeded=true;
		}
		else if(sText.toLowerCase() < oDropDown.options[first_elem].text.toLowerCase())
		{
			// create a new option
			oDropDown.options[oDropDown.options.length] = new Option();
			// length-1 because we just added one
			for (var y=oDropDown.options.length-1; y> first_elem; y--)
		  {
		    oDropDown.options[y].text = oDropDown.options[y-1].text;
		    oDropDown.options[y].value = oDropDown.options[y-1].value;
		  }

		  oDropDown.options[first_elem].text = sText;
	    oDropDown.options[first_elem].value = sValue;
		  bSucceeded=true;
		}
		else if (!bSucceeded && (sText.toLowerCase() > oDropDown.options[oDropDown.options.length-1].text.toLowerCase()))
		{
			oDropDown.options[oDropDown.options.length] = new Option();
		  oDropDown.options[oDropDown.options.length-1].text = sText;
	    oDropDown.options[oDropDown.options.length-1].value = sValue;
		  bSucceeded=true;
		}
		else
		{
			for (var x=oDropDown.options.length-1; x>=first_elem; x--)
			{
			  if (!bSucceeded)
			  {
			    // If we find it then we need to shift everything up
					if (sText.toLowerCase() <= oDropDown.options[x].text.toLowerCase()
							 &&( sText.toLowerCase() >= oDropDown.options[x-1].text.toLowerCase())
									// First one? just put it in
							/*&& ((x <oDropDown.options.length-1))*/
							)
					{
						// create a new option
						oDropDown.options[oDropDown.options.length] = new Option();
						// length-1 because .length is 1 based
						for (var y=oDropDown.options.length-1; y> x; y--)
					  {
					    oDropDown.options[y].text = oDropDown.options[y-1].text;
					    oDropDown.options[y].value = oDropDown.options[y-1].value;
					  }
					  oDropDown.options[x].text = sText;
				    oDropDown.options[x].value = sValue;

						// This will mean we don't do this again
						bSucceeded = true;
					}
			  }
		  }
		}
	  //
	  if (!bSucceeded)
	  {
			// create a new option
			oDropDown.options[oDropDown.options.length] = new Option();
			// length-1 because we just added one
			for (var y=oDropDown.options.length-1; y> first_elem; y--)
		  {
		    oDropDown.options[y].text = oDropDown.options[y-1].text;
		    oDropDown.options[y].value = oDropDown.options[y-1].value;
		  }

		  oDropDown.options[0].text = sText;
	    oDropDown.options[0].value = sValue;
		}
	}
}



