/*<![CDATA[*/
<!--
var maxSaveResults = 25;
function toggleAllItems(obj)
{
	var checkboxstate=obj.src.split("/");
	var checkboxparts=checkboxstate.length;
	var checkimage=checkboxstate[checkboxparts-1];
	switch (checkimage)
	{
		case "checkboxtrue.gif":  unselectAllItems(); setMasterField(-1); break;
		case "checkboxfalse.gif": selectAllItems();  setMasterField(1);  break;
		case "checkboxmixed.gif": selectAllItems();  setMasterField(1);  break;
		default: alert('search.js - error: unhandled state "'+checkimage+'"');
	}
}


function selectAllItems() {
	var checkedfields = 0;
	var fieldCollection = document.getElementsByName("r");
  
	for (x=0;x<fieldCollection.length;x++)
	{
		if (fieldCollection[x].value!="action")
		{
			fieldCollection[x].checked=true;
			checkedfields++
		}
	}
}

function unselectAllItems()
{
	var fieldCollection=document.getElementsByName("r");
	for (x=0;x<fieldCollection.length;x++)
	{
		if (fieldCollection[x].value!="action")
		{
			fieldCollection[x].checked=false;
		}
	}
}

// This method is for the results page. 
// To change the top level check box image
function toggleMasterItem(boxObj)
{
    var checkedfields = getSelectedCheckBoxCount();
	
	if (checkedfields < 1){
		setMasterField(-1);
	}
	if (checkedfields > 0){
		setMasterField(0);
	}

	if (checkedfields == document.getElementsByName("r").length){
		setMasterField(1)
	}
}

// This method is for the saved results page. 
// To change the top level check box image
function toggleSavedPageItem(boxObj) {
    var checkedfields = getSelectedCheckBoxCount();

	if (checkedfields < 1){
		setMasterField(-1);
	}
	if (checkedfields > 0){
		setMasterField(0);
	}

	if (checkedfields == document.getElementsByName("r").length){
		setMasterField(1)
	}
}

function alertMaxReached() {
	var savedResults = getSavedResultsCount();
	var allowResults = maxSaveResults - savedResults;
	if (allowResults == 0) {
		alert('Sorry, you have reached the maximum number of 25 results that can be saved in Scirus, if you want to save any results then you have delete some of the already saved results.');
	} else {
		alert('You have exceeded maximum total number of results ' + maxSaveResults + '. \nRight now you have ' + savedResults + ' result(s) saved. At most you can select '+ allowResults + ' result(s) for this selection.\n\nPlease uncheck some of the results to limit your total number of saved results to ' + maxSaveResults + '.');
	}	
}

function setMasterField(value){
	var masterCheckbox = document.getElementById("masterCheck");
	var masterCheckboxZ = document.getElementById("masterCheckZ");

	switch(value){
		case -1:  masterCheckbox.src="images/checkboxfalse.gif"; masterCheckbox.title = 'Click checkbox to select all results on this page'; masterCheckbox.alt = 'Click checkbox to select all results on this page'; break;
		case 0: masterCheckbox.src="images/checkboxmixed.gif"; masterCheckbox.title = 'Click checkbox to select all results on this page';masterCheckbox.alt = 'Click checkbox to select all results on this page '; break;
		case 1:  masterCheckbox.src="images/checkboxtrue.gif"; masterCheckbox.title = 'Click checkbox to deselect all results on this page'; masterCheckbox.alt = 'Click checkbox to deselect all results on this page'; break;
	}

	switch(value){
		case -1:  masterCheckboxZ.src="images/checkboxfalse.gif"; masterCheckboxZ.title = 'Click checkbox to select all results on this page'; masterCheckboxZ.alt = 'Click checkbox to select all results on this page'; break;
		case 0:  masterCheckboxZ.src="images/checkboxmixed.gif"; masterCheckboxZ.title = 'Click checkbox to select all results on this page'; masterCheckboxZ.alt = 'Click checkbox to select all results on this page'; break;
		case 1:  masterCheckboxZ.src="images/checkboxtrue.gif"; masterCheckboxZ.title = 'Click checkbox to deselect all results on this page'; masterCheckboxZ.alt = 'Click checkbox to deselect all results on this page'; break;
	}
}

// This method is to read the Saved Results cookie 
// and get the eid's count
function getSavedResultsCount(){

	var cookies = document.cookie;
	var savedResultCookie = "ScirusSavedResultsv2";
	var eidCount = 0;
	
	if (cookies.indexOf(savedResultCookie) != -1) {   
		var results = cookies.match( savedResultCookie + '=(.*?)(;|$)' );
		var items = results[1].split("+");
		eidCount = items.length - 1;
	}	
	
	return eidCount;
}

// This method is to return the count of the checked check boxes 
function getSelectedCheckBoxCount() { 
    var checkedfields = 0;
    var fieldCollection = document.getElementsByName("r");

	for (x = 0;x < fieldCollection.length;x++) {
		if (fieldCollection[x].value != "action") {
			if (fieldCollection[x].checked) {
				checkedfields++;
			}
		}
	}
	return checkedfields;
}

// This is method for the save link in the results page 
// Check the max save count and submit to the save page
function SaveResults(){
    var checkedfields = 0;
	checkedfields = getSelectedCheckBoxCount() + getSavedResultsCount();
	var object = document.forms["results"];
	object.save.value = "saved";
   
   	if (checkedfields > maxSaveResults){
    	alertMaxReached();
	}else{
	   object.submit();	
	}
}
