function getSelectedIndex(selectionID)
{
	var selection = document.getElementById(selectionID);
 	for (var index = 0; index < selection.options.length; index++) 
 		if (selection.options[ index ].selected) 
 			return index;
 	return -1;
}
function getSelectedValue(selectionID)
{
	var index = getSelectedIndex(selectionID);
	if(index>=0)
	{
		var selection = document.getElementById(selectionID);
		return selection.options[index].value;
	}else
	{
		return null;
	}
}
function getAJAXObject()
{
	var AJAX = null; 	//	Initialize the AJAX variable.
	if (window.XMLHttpRequest) 
	{ 	//	Are we working with mozilla?
		AJAX=new XMLHttpRequest(); 	//Yes -- this is mozilla.
	} else 
	{ 	//Not Mozilla, must be IE
		AJAX=new ActiveXObject("Microsoft.XMLHTTP"); //	Wheee, ActiveX, how do we format c: again?
	} //End setup Ajax.
	if (AJAX==null) 
	{ //If we couldn't initialize Ajax...
		alert("Your browser doesn't support AJAX."); //Sorry msg.return false //
	}
	return AJAX;
}
function ajaxLoadPage(pageURL,outputID,params)
{
	var xmlhttp =  getAJAXObject();
	xmlhttp.open('POST', pageURL, true);
	xmlhttp.onreadystatechange = function() {
    	if (xmlhttp.readyState == 4) {

    		 if (xmlhttp.status == 200)
    		 {
	        	var output = document.getElementById(outputID);
	        	if(output != null)
		      		output.innerHTML = xmlhttp.responseText;
	      	 }else
	      	 {
	        	var output = document.getElementById(outputID);
	      	 	if(output != null)
		      	 	output.innerHTML = "Error with error code:"+xmlhttp.status;
	      	 }
	    }else
	    {
        	var output = document.getElementById(outputID);
	    	if(output!=null)
		    	output.innerHTML = 'Loading...';
	    }
	}
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(params);
}
function popup(url,width,height)
{
	if(width==null)width=200;
	if(height==null)height=400;
	var newWindow=window.open(url,'name','height='+height+',width='+width);
	if (window.focus) {newWindow.focus()}
	return newWindow;
}

function hideAllIds(prefixId,maxNum)
{
	for(i=1;i<=maxNum;i++)
	{
		var eachObj = document.getElementById(prefixId+i);
		if(eachObj!=null)
		{
			eachObj.style.display='none';
		}		
	}	
	return false;
}
function setAllClassName(prefixId,newClassName,maxNum)
{
	for(i=1;i<=maxNum;i++)
	{
		var eachObj = document.getElementById(prefixId+i);
		if(eachObj!=null)
		{
			eachObj.className=newClassName;
		}
		
	}
	return false;
}
function setClassName(id,newClassName)
{
		var eachObj = document.getElementById(id);
		if(eachObj!=null)
		{
			eachObj.className=newClassName;
		}
}
function clientSideInclude(id, url) {
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  } else if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  }else {alert('Unknwon Browser Type');}
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
  return;
}

function doubleSize(imageId)
{
	var img = document.getElementById(imageId);
	img.width=img.width*2;
	img.height=img.height*2;
	return false;
}
function halfSize(imageId)
{
	var img = document.getElementById(imageId);
	if(img !=null)
	{
	img.width=img.width/2;
	img.height=img.height/2;
	}
	return false;
}
function hide(objectId,prefix,tagName)
{
	var obj = document.getElementById(objectId);
	if(prefix==null)
	{
		if(obj!=null)
		{
			obj.style.display='none';
		}
	}else
	{
		if(tagName!=null)
		{
			var children = obj.getElementsByTagName(tagName);
			var childIndex =0;
			for(childIndex = 0;childIndex<children.length;childIndex++)
			{
				var eachChild =  children[childIndex];
				var eachChildId = eachChild.id;
				if(eachChildId.indexOf(prefix)==0)
				{
					eachChild.style.display='none';
				}
			}
		}
	}
	return false;
}
function show(objectId)
{
	
	var obj = document.getElementById(objectId);
	if(obj!=null)
	{
	obj.style.display='block';
	}
	return false;
}

function hideAll(parentPrefix,formId)
{
	var form = document.getElementById(formId);
	var inputs = form.getElementsByTagName('i');
	var inputIndex =0;
	var values = new Array();
	for(inputIndex = 0;inputIndex<inputs.length;inputIndex++)
	{
		var eachInput =  inputs[inputIndex];
		var eachInputId = eachInput.id;
		if(eachInputId.indexOf(fieldsPrefix)==0)
		{
			var value = parseFloat(eachInput.value);
			if(!isNaN(value))
			{
				values[values.length] =  value;
			}else
			{
				values[values.length] =  0.0;
			}
		}
	}
	return values;
}



function getKeyCode(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	return keynum;
}

function isDownArrow(e)
{
	var keyCode = getKeyCode(e);
	return (keyCode== 40);
}
function isUpArrow(e)
{
	var keyCode = getKeyCode(e);
	return (keyCode== 38);	
}
function setFocus(elemId)
{
	var obj = document.getElementById(elemId);
	obj.focus();
}

function moveFocus(elemId,dif,number_delimit)
{
	if(number_delimit==null || number_delimit=='')
	{
		number_delimit = '_';
	}
	var rowIdNumberIndex = elemId.lastIndexOf(number_delimit);
	var rowIndex = parseInt(elemId.substring(rowIdNumberIndex+1,elemId.length));
	var newId = elemId.substring(0,rowIdNumberIndex)+number_delimit+(rowIndex+dif);	
	setFocus(newId);
	
}
function setSpan(spanId,text)
{
	var span = document.getElementById(spanId);
	if(span!=null)span.innerHTML=text;
}

function cloneRow(rowId,number_delimit)
{
	var row = document.getElementById(rowId);
	var tab = row.parentNode.parentNode;
	var rows = tab.getElementsByTagName('tr');
	var root=rows[rows.length-1].parentNode;//the TBODY
	var clone=row.cloneNode(true);//the clone of the first row
	clone.id = 'newClone';
	root.appendChild(clone);//appends the clone	
	return clone;
}
function addRow(rowId,number_delimit)
{
	var clone = cloneRow(rowId,number_delimit);
	var tab = clone.parentNode.parentNode;
	resetAllIds(tab,number_delimit);
	resetRowInputValue(clone);	
}
function addRows(rowId,number_delimit,number_of_rows)
{
	var clone = null;
	if(number_of_rows == null || number_of_rows<1)number_of_rows = 1;
	for(var index = 0;index<number_of_rows;index++)
	{
		clone = cloneRow(rowId,number_delimit);
		resetRowInputValue(clone);	
	}
	if(clone != null)
	{
		var tab = clone.parentNode.parentNode;
		resetAllIds(tab,number_delimit);
	}
}
function getLastRow(tabId)
{
	var tab = document.getElementById(tabId);
	var rows=tab.getElementsByTagName('tr');
	return rows[rows.length-1];
}

function resetRowInputValue(row)
{
	var inputs = row.getElementsByTagName('input');
	
	for(var inputIndex = 0;inputIndex<inputs.length;inputIndex++)
	{
		
		var eachInput = inputs[inputIndex];
		if(eachInput.type == 'text' || eachInput.type == 'textarea')
		{
			eachInput.value = '';
		}
	}
}
function resetAllIds(tab,number_delimit)
{
	// reset all id and value
	if(number_delimit==null || number_delimit=='')
	{
		number_delimit = '_';
	}
	var rows = tab.getElementsByTagName('tr');
	for(var rowIndex = 0;rowIndex<rows.length;rowIndex++)
	{
		var eachRow = rows[rowIndex];
		
		var rowIdNumberIndex = eachRow.id.lastIndexOf(number_delimit);
		//if(rowIdNumberIndex<0)continue;
		eachRow.id = eachRow.id.substring(0,rowIdNumberIndex)+number_delimit+rowIndex;
		
		var inputs = eachRow.getElementsByTagName('input');
	
		for(var inputIndex = 0;inputIndex<inputs.length;inputIndex++)
		{
			
			var eachInput = inputs[inputIndex];
			var eachInputId = eachInput.id;
			
			var inputIdNumberIndex = eachInputId.lastIndexOf(number_delimit);
			if(inputIdNumberIndex<0)continue;
			eachInput.id = eachInputId.substring(0,inputIdNumberIndex);
			eachInput.id = eachInput.id+number_delimit+rowIndex;
			eachInput.name = eachInput.id;
			
		}
	}	
	
}

function removeRow(row,number_delimit,min_num_rows){
	if(min_num_rows==null||min_num_rows<1)min_num_rows=2;
	var tab = row.parentNode.parentNode;
	var rows = tab.getElementsByTagName('tr');
	if(rows.length<=min_num_rows)
	{
		resetRowInputValue(row);
	}else
	{
		tab.deleteRow(row.rowIndex);
		resetAllIds(tab,number_delimit);
	}
	
}


function delListItem(Objectname,ItemIndex){
/*remove an item from a listbox
Objectname is formname.listname format
ItemIndex is item number (list is 0 based first item = 0 second item = 1 and so on)
Usage DelListItem(form1.list1,1) this will remove the second item from list1 in form1
*/
	var listcount = Objectname.length-1; //get the count of list items
	
	if(listcount < ItemIndex){ //if you asked for a nonexisting item
		var DelListItem = 'false'; //set the response to false
		return DelListItem; //return OOPS
	}
	
	else{ //the item is valid remove it
		Objectname.options[ItemIndex] = null; //set the item display text and value to nothing
		var DelListItem = 'true'; //set the response to true
		return DelListItem; //return OK
	}
}

function delListSelectedItem(Objectname)
{ 
 	for (var index = 0; index < Objectname.options.length; index++) 
 		if (Objectname.options[ index ].selected) 
 			DelListItem(Objectname,index);
}

function addList(Objectname,ItemValue,DisplayText){
/*insert a new item into a listbox
Objectname is formname.listname format
ItemValue is value passed on submit
DisplayText is text shown in listbox
Usage AddList(form1.list1,'new text','5') this will add a new option to list1 in form1 it will display 'new text' and pass a value of 5 on submit
*/
	var listcount = Objectname.length; //listcount will give you the next open listindex number
	
	Objectname.options[Number(listcount)] = new Option(DisplayText,ItemValue); //new option will let you insert the (display text,list value)
	var AddList = 'true'; //set the response to true
	return AddList; //return OK
}


function clearList(Objectname){
/*clears ALL items form listbox
Objectname is formname.listname format
Usage ClearList(form1.list1) this will remove ALL items from list1 in form1 the list is now blank
*/
	Objectname.options.length = 0; //set ALL item display text and values to nothing
	var AddList = 'true'; //set the response to true
	return AddList; //return OK
}

function submitSubAction(form,subAction,index)
{
 form.subAction.value = subAction;
 form.subActionIndex.value = index;
 form.action='../exam/Question.do?function=view&action=${param.action}';
 form.submit();
}
function selectAll(lst)
{
	var index;
	if(lst==null)
		return;
	for (var i=0;i<lst.options.length;i++)
	{
		lst.options[i].selected  = true;
	}
}


function toggleVisibility(toggleObjectId)
{
	 var obj = document.getElementById(toggleObjectId);
	 if(obj==null)return false;
     if (obj.style.display == 'none')
     {
          obj.style.display = 'block';
     }else if (obj.style.display == 'block')
	 {
		 obj.style.display = 'none';
	 }else
	 {
		 obj.style.display = 'none';
	 }	
	 return false;
}

function setVisibility(visible)
{
	if(visible=='true')
	{
     if (document.getElementById(theTable).style.display == 'none')
     {
          document.getElementById(theTable).style.display = 'block';
     }
    }else
    {
     if (document.getElementById(theTable).style.display == 'none')
     {
          document.getElementById(theTable).style.display = 'none';
     }
     else
     {
          document.getElementById(theTable).style.display = 'none';
     }
    }
}
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
function playSound(name)
{
	var flashMovie=getFlashMovieObject('playSound');
	flashMovie.SetVariable("/:soundFilename",name);
	flashMovie.Play();
}
function disable(id)
{
	var obj = document.getElementById(id);
	obj.disabled=true;
}
function enable(id)
{
	var obj = document.getElementById(id);
	obj.disabled=false;
}