
// JavaScript Document
YAHOO.namespace("example.container");
// global var to store number of recipes in the list 
var numofRec = 0;

// delay in ms after changes (we wait for more changes in this timeframe)
	var delay    = 1000;
	
	// id of an element we will use for results
	var results  = "results";
	
	/* Internal state */
	
	// a form we are going to monitor and submit
	var formNode = null;
	
	// timer id, when we wait for "delay"
	var timer    = -1;
	
	// are we accessing a server at this moment?
	var inflight = false;
	var prodinflight = false;
	
	// were any changes made while we were accessing a server?
	var resubmit = false;
	
	// intial page load
	var initialLoad = true;
	

// delay in ms after changes (we wait for more changes in this timeframe)
	var myProdFilter = {



	    // process changes to form's controls
	    onchange: function() {
	        if (!inflight | !prodinflight) {
	            // we are free to access a server
	            // let's clear any pending timers
	            if (timer != -1) clearTimeout(timer);
	            // set new timer, if this timer is uninterrupted, it will submit the form
	            //var _this = this;

	            if (!initialLoad) {
	                timer = setTimeout(function() { myProdFilter.mysubmit(); }, delay);
	                
	            }

	        } else {
	            // we are accessing a server at the moment => mark that changes were made
	            resubmit = true;
	        }
	    },

	    // submit the form
	    mysubmit: function() {
	        // reset all internal variables
	        timer = -1;
	        inflight = true;
	        resubmit = false;

	        // now we can call a server
	        //var _this = this;
	        removeElement('recipeList');

	        //RListObject.reLoadRequest("../xml/" + sFileName);
	        pListObject.reLoadRequest("xml/productlist.aspx");


	    },

	    // submit the form
	    myProdsubmit: function(id) {
	        // reset all internal variables
	        timer = -1;
	        prodinflight = true;
	        resubmit = false;
	        // now we can call a server
	        var _this = this;
	        removeProductElement();
	        productObject.startRequest("xml/product.aspx?id=" + id);

	    },

	    // we got results successfully
	    onload: function(data) {
	        // we are ready to call a server again
	        this.inflight = false;
	        if (this.resubmit) {
	            // if the form was modified, while we waited for results => resubmit it
	            this.resubmit = false;
	            this.onchange();
	        }
	    }
	}

function onCheckAll(id, checked){
	//alert(id);
	var elements = YAHOO.util.Dom.getElementsByClassName(id, 'input');
	//alert(id);
	//alert(elements.length);
	//if(elements.length > 0){
		if(checked){
			check(elements);
		} else {
			uncheck(elements);
		}
	//}
}

function check(field) {
	for (i = 0; i < field.length; i++){
		if (field[i].checked==false){
			field[i].checked = true;
		} 
	}
	myProdFilter.mysubmit();

}

function uncheck(field) {
	for (i = 0; i < field.length; i++){
		if (field[i].checked==true){
			field[i].checked = false;
		} 
	}
	myProdFilter.mysubmit();
	

}

function SubCheck(id, checked){
	myName = "cat_" + id;
	if(!checked){
		//alert(myName)
		thisBox = YAHOO.util.Dom.getElementsByClassName(myName, 'input');
		//alert(thisBox.length)
		thisBox[0].checked = false;
	} else {
		var elements = YAHOO.util.Dom.getElementsByClassName(id, 'input');
		if(!checkothers(elements)){
			thisBox = YAHOO.util.Dom.getElementsByClassName(myName, 'input');
			thisBox[0].checked = true;
		}
	};
	myProdFilter.mysubmit();


}


function checkothers(field) {
	myval = false;
	for (i = 0; i < field.length; i++){
		if (field[i].checked==false){
			myval = true;
		} 
	}
	return myval;

}
