// JavaScript Document

//Sort select lists
function sortList(lb) {
	arrTexts = new Array();
	arrVals = new Array();

	for(i=0; i<lb.length; i++)  {
		var thisText = lb.options[i].text
		arrTexts[i] = lb.options[i].text;
		arrVals[thisText] = lb.options[i].value;
	}

	arrTexts.sort();

	for(i=0; i<lb.length; i++)  {
		var thisText = arrTexts[i];
		lb.options[i].text = arrTexts[i];
		lb.options[i].value = arrVals[thisText];
	}
}
// End of sortList()



// Dual list move function
function moveDualList( srcList, destList, moveAll ) {
	// Do nothing if nothing is selected
	if ((srcList.selectedIndex == -1) && (moveAll == false)) {
		return;
	}

	newDestList = new Array( destList.options.length );

	var len = 0;

	for(len = 0; len < destList.options.length; len++) {
		if (destList.options[ len ] != null) {
			newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected );
		}
	}

	for(var i = 0; i < srcList.options.length; i++) { 
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
			// Statements to perform if option is selected

			// Incorporate into new list
			newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected);
			len++;
		}
	}

	// Populate the destination with the items from the new array
	for (var j = 0; j < newDestList.length; j++) {
		if (newDestList[ j ] != null) {
			destList.options[ j ] = newDestList[ j ];
		}
	}

	// Erase source list selected elements
	for(var i = srcList.options.length - 1; i >= 0; i--) { 
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
			// Erase Source
			srcList.options[i] = null;
		}
	}

	sortList(destList);
	sortList(srcList);

} 
// End of moveDualList()

function selectAll(SelectBox) {
     for(var i=0; i < SelectBox.length; i++) {
     	SelectBox[i].selected = true;
     }
}

// Resizes images with a certain alt tag
function ResizeThem()
{
  var maxheight = 180;
  var maxwidth = 120;
  var imgs = document.getElementsByTagName("img");
  for ( var p = 0; p < imgs.length; p++ )
  {
    if ( imgs[p].getAttribute("alt") == "reviewImage" )
    {
      var w = parseInt( imgs[p].width );
      var h = parseInt( imgs[p].height );
      if ( w > maxwidth )
      {
        imgs[p].style.cursor = "pointer";
        h = ( maxwidth / w ) * h;
        w = maxwidth;
        imgs[p].height = h;
        imgs[p].width = w;
      }
      if ( h > maxheight )
      {
        imgs[p].style.cursor="pointer";
        imgs[p].width = ( maxheight / h ) * w;
        imgs[p].height = maxheight;
      }
    }
  }
}


// Changes the sort order of the page

function changeSort(selectField) {
	var redirect = selectField.options[selectField.selectedIndex].value;
	if(redirect == "") {
		return false;
	}
	else {
		window.location = redirect;
	}
}

// Does an Ajax search to display results

function ajaxSearch() {
	
/*
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
		{
		alert ("Browser does not support HTTP Request")
		return
		} 
		
	ajaxString = "ajaxSearch.php?" + ajaxString;
	xmlHttp.onreadystatechange=stateChanged ;
	xmlHttp.open("GET",ajaxString,true);
	xmlHttp.send(null);*/
	
}


function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
	 alert(xmlHttp.responseText); 
 } 
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

// Reveals/Hides a section by ID when a checkbox is ticked
function revealSection(element, revealId) {
	if(element.checked == true) {
		document.getElementById(revealId).style.display = 'block';
	}
	else {
		document.getElementById(revealId).style.display = 'none';
	}
}