// Javascript routines for Product List page

var selectedModelGroup = "";
var threeSelectionMessage = false;
var groupSelectionMessage = false;
var selectedTally  = 0;

function SelectModel(modelId, modelGroup) {
    var thisModelCheckbox;

    if(selectedModelGroup != "" && modelGroup != selectedModelGroup) {
        if(groupSelectionMessage == false) {
            alert("You can only compare products with like fuel types.  Please uncheck the products you currently have and make another selection.");
            //groupSelectionMessage = true;
        }
        return false;
    } else {
	selectedTally++;
        selectedModelGroup = modelGroup;
        if(selectedTally > 3) {
            alert("You may only select up to three models at a time to compare.");
            selectedTally--;
            return false;
        }
        return true;
    }
}

function UnselectModel(modelId) {
	selectedTally--;
	if (selectedTally < 1) {
		selectedTally = 0;
		selectedModelGroup = "";
	}
}

onLoadCount = function() {
	// Check to see if any of the checkboxen are checked
	// We have to do this because when someone goes to the compare page, then clicks the
	// back button in their browser, the Javascript variables are reset. So we have to
	// set them back to where they were.
	var fieldName;
	var formFields = document.getElementsByName('compare');	
	for(var i = 0; i < formFields.length; i++) {	
		if (formFields[i].checked == true) {
			selectedTally++;
		}
		formFields[i].disabled = false;
	}
}

function ValidateSelected(modelGroup) {
	if ( (selectedTally == 0 || selectedTally < 2) || (selectedModelGroup != "" && selectedModelGroup != modelGroup ) ) { 
		alert("You must choose at least 2 products with like fuel types to compare."); 
		return false; 
	} else if( selectedTally > 3 ) { 
		alert("You may only select up to three models with like fuel types at a time to compare.");
		return false; 
	} else return true;
}

windowOnLoad(onLoadCount);
