// JavaScript Document


//formID is the form that we want to look at
//sleectedItems is an array that the items which are selected will be added to
//Selected items are identified by their checkbox being checked.
function getSelected(formID,selectedItems)
{
	if(document.forms[formID].elements.length == 0)
		return null;
	
	selectedItems.length = 0; //Clear the array
	
	for(i=0;i<document.forms[formID].elements.length;i++)
	{
		if(document.forms[formID].elements[i].type == "checkbox" && document.forms[formID].elements[i].getAttribute('name') == 'rowSelect')
			if(document.forms[formID].elements[i].checked)
			{
				selectedItems.push(document.forms[formID].elements[i].value);
			}
	}
	
	/*for(x=0;x<selectedItems.length;x++)
		alert(selectedItems[x]);*/
	return selectedItems;
}

//Turn all the check boxed to checked or unchecked. 
function setFormCheckState(formID,bool)
{
	if(document.forms[formID].elements.length == 0)
		return null;
	if(bool != true && bool != false)
	   	bool = false;
	
	for(i=0;i<document.forms[formID].elements.length;i++)
	{
		if(document.forms[formID].elements[i].type == "checkbox" && document.forms[formID].elements[i].getAttribute('name') == 'rowSelect')
			document.forms[formID].elements[i].checked = bool;
	}
	
	/*for(x=0;x<selectedItems.length;x++)
		alert(selectedItems[x]);*/
	return 0;
}

function unloadPopup(popupElement)
{
	var popup = document.getElementById(popupElement);
	//Like Uroboros, the snake that are its tail for eternity.
	if(popup)
		popup.parentNode.removeChild(popup);
	else 
		return false
		
	return true
}

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
}

//Sets an elements value is greater then capLimit it gets set to the cap limit. This is for capping the possigle values in input boxes.  
function cap(element,capLimit)
{
	if (element.value > capLimit)
		element.value = capLimit;
	
	return true;

}

//Debugging output (if firebug is not present then console.log causes a problem)
function debug(txt)
{
	if(console.log)
		console.log(txt);
}

//Gets the variables in the URL string (GET variables i.e. ?var=data)
function getUrlVars() 
{ 
	var map = {}; 
	var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, 
				function(m,key,value) { map[key] = (value === undefined) ? true : value.substring(1); }); 
	return map; 
}

//fancy functions for alternative drop down select boxes.
//These are all fairly simple function. They were taken from http://v2.easy-designs.net/articles/replaceSelect/
function selectReplacement(obj)
{
  // append a class to the select
  obj.className += ' replaced';
  // create list for styling
  var ul = document.createElement('ul');
  ul.className = 'selectReplacement';
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
	var selectedOpt;
	if (opts[i].selected) {
	  selectedOpt = i;
	  break;
	} else {
	  selectedOpt = 0;
	}
  }
  for (var i=0; i<opts.length; i++) {
	var li = document.createElement('li');
	var txt = document.createTextNode(opts[i].text);
	li.appendChild(txt);
	li.selIndex = opts[i].index;
	li.selectID = obj.id;
	li.onclick = function() {
	  selectMe(this);
	}
	if (i == selectedOpt) {
	  li.className = 'selected';
	  li.onclick = function() {
		this.parentNode.className += ' selectOpen';
		this.onclick = function() {
		  selectMe(this);
		}
	  }
	}
	if (window.attachEvent) {
	  li.onmouseover = function() {
		this.className += ' hover';
	  }
	  li.onmouseout = function() {
		this.className = 
		  this.className.replace(new RegExp(" hover\\b"), '');
	  }
	}
	ul.appendChild(li);
  }
  // add the input and the ul
  obj.parentNode.appendChild(ul);
}
function selectMe(obj) 
{
  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
	if (lis[i] != obj) { // not the selected list item
	  lis[i].className='';
	  lis[i].onclick = function() {
		selectMe(this);
	  }
   } else {
	  setVal(obj.selectID, obj.selIndex);
	  obj.className='selected'; 
	  obj.parentNode.className = 
		obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
	  obj.onclick = function() {
		obj.parentNode.className += ' selectOpen';
		this.onclick = function() {
		  selectMe(this);
		}
	  }
	}
  }
}
function setVal(objID, selIndex) 
{
  var obj = document.getElementById(objID);
  obj.selectedIndex = selIndex;
  obj.onchange();
}
function setForm() 
{
  var s = document.getElementsByTagName('select');
  for (var i=0; i<s.length; i++) {
	selectReplacement(s[i]);
  }
}
function closeSel(obj) 
{
  // close the ul
}

//Everything in the tags <blink> </blink> will, well, blink. 
//Blinking functions are from the web. These exist in many forms in many places. These are from the hideous www.draac.com. Offering all things degrading to the internet, like blinking text.
// I updated it to be more firendly with more browsers (using standard javascript rather then 'only IE' javascript.
function doBlink() {
	var blink = document.getElementsByTagName('blink')
	for (var i=0; i<blink.length; i++)
		blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : "" 
}

//Just call this with: window.onload = startBlink;
function startBlink() {
	
	var browserName = ""; 

	var ua = navigator.userAgent.toLowerCase(); 
	if ( ua.indexOf( "opera" ) != -1 ) 
	{ 
		browserName = "opera"; 
	}
	else if ( ua.indexOf( "msie" ) != -1 )
	{ 
		browserName = "msie"; 
	} 
	else if ( ua.indexOf( "safari" ) != -1 ) 
	{ 
		browserName = "safari"; 
	} 
	else if ( ua.indexOf( "mozilla" ) != -1 ) 
	{ 
		if ( ua.indexOf( "firefox" ) != -1 )
		{ 
			browserName = "firefox"; 
		} 
		else 
		{ 
			browserName = "mozilla"; 
		} 
	} 
	
	if(browserName  != 'firefox' && browserName  != 'mozilla') //Firefox supports the CSS blink directive
		setInterval("doBlink()",500)
}


