/* *********************************************************************************
  TemplateFilter provides a basic base function for creating a filter which redirects
  upon call to the handle function.
***********************************************************************************/
function TemplateFilter( formId, selectIdx, purl ){		
  this.formId = formId; //identifier of the filter form
  this.selectIdx = new Number(selectIdx);
  this.url = new TemplateURL(purl, true);

  if(typeof TemplateFilter._initialized == "undefined"){

	TemplateFilter.prototype.setParam = function( property, value ){	this.url.setParam(property, value); };	
	TemplateFilter.prototype.preHandle = function( sObj ){};	
	TemplateFilter.prototype.handle = function( sObj ) {		
	  this.url.setParam("page", "");
	  this.url.setParam("filter1Value", sObj.options[sObj.selectedIndex].value);
	  this.modifyHandle( sObj );
	  if( sObj.options[sObj.selectedIndex].value != ''){
	 	//alert( "reload to:"+this.url.getURL() );
		window.location.href=this.url.getURL();
	  }		
	};	
	TemplateFilter.prototype.removeOptions = function( elem ){
	   if(elem && elem.hasChildNodes()){
	     children = elem.childNodes;
	     idx = (new Number ( children.length )) -1;
	     var initialChild;
	     while(children.length > 0){
		 			var oldChild = elem.removeChild(children[idx]);
		 			if(oldChild.nodeType == 1 && oldChild.value==''){
		 				initialChild = oldChild;
		 			}
		 			idx--;
	     }
	     elem.options[0] = new Option(initialChild.text, initialChild.value);
	   }
	};	
	TemplateFilter.prototype.populate = function(value){};	
  }
  TemplateFilter._initialized="true";
};

/* *****************************************************************************
   MatrixTemplateFilter extends the TemplateFilter functionality to allow for the
   second filter option on the formId to be controlled by the value selected
   for the first option.
********************************************************************************/
function MatrixTemplateFilter( formId, url, allProductsMapped ){	
  this.optionmatrix = new Array(); 
  this.allProductsMapped = allProductsMapped;
  TemplateFilter.call(this, formId, 1, url );
  
  if(typeof MatrixTemplateFilter._initialized == "undefined"){
	/* Modifies the default url parameters in TemplateFilter with values specified */
    	MatrixTemplateFilter.prototype.modifyHandle = function( sObj ){
  		iObj = document.getElementById(this.formId).elements[0];
  		this.url.setParam("filter1Value", iObj.options[iObj.selectedIndex].value)
  		this.url.setParam("filter2Value", sObj.options[sObj.selectedIndex].value);
  		};

  	/* Locates the correct set of select options based on the id passed to the function */
      MatrixTemplateFilter.prototype.findOptions = function( matrixId ){
        for( var i=0; i<this.optionmatrix.length; i++ ){
          if( this.optionmatrix[i].matrixId == matrixId ){ 
          	return this.optionmatrix[i]; 
          }       
        }
      };     
   
      /* Adds a new select option value to the end of the list for the matrixId specified */
    	MatrixTemplateFilter.prototype.addMatrixOption = function( matrixId, optionValue, optionName ){     		
	  		var matrix = this.findOptions(matrixId);
	  		if( matrix ){ 
	  			matrix.addOption(optionValue, optionName); 
	  		}
	  		else{	  		   		 
	  		 matrix = new OptionMatrix(matrixId);
	  		 matrix.addOption(optionValue, optionName);
	  		 this.optionmatrix[this.optionmatrix.length] = matrix;	  		 
	  		}	  
	  		//alert("Add:"+matrixId+":"+this.optionmatrix.length);			  			
    	};

 		/* Calls the function that will modify the second select box options.  If that called function
 		   results in only one possible option for the select dropdown represented by selectIdx (i.e. size), 
 		   automatically submit the form via the handle function */
      MatrixTemplateFilter.prototype.initializeSelect = function( sObj ){
    	  this.loadSelect(sObj);
    	  var updateObj = document.getElementById(this.formId).elements[this.selectIdx];
    	  var optCount  = updateObj.options.length;
    	  if(optCount== 2){
    	  	//available options are: one selectable option (size) plus the instructional option (i.e. "Then select size") 
    	  	//The selectIdx (i.e. size) will default to the only selectable option available so reload the page with the filter results
    	  	this.handle(updateObj);
    	  }
      };

      /* Modifies the second select box options with the options for the id selected by the select
         box element passed to the function. */
      MatrixTemplateFilter.prototype.loadSelect = function( sObj ){
    	  var updateObj = document.getElementById(this.formId).elements[this.selectIdx];
    	  if(updateObj){
	    		this.removeOptions(updateObj);
	    		var mObj = this.findOptions(sObj.options[sObj.selectedIndex].value);
	    		if(mObj){
	    			var opts = mObj.matrixValues;	 
	    			for(i=0; i<opts.length; i++){
	    				updateObj.options[updateObj.options.length] = new Option(opts[i].text, opts[i].value);
	    				if(opts.length == 1 && i==0){
	    					//the remove options call replaces the first empty value so the selected
	    					//item will start at index 1.
	    				  updateObj.options[1].selected = true;
	    				}
	    			}
	    		}
    	  }
      };

      /* Loads the second select box with the values specified by the initial select boxes selected option*/
      MatrixTemplateFilter.prototype.populate = function(value){      
      	if(this.optionmatrix.length == 2){
      		if(this.allProductsMapped == true){      		
      		  /* All products are mapped and only one type/size combo is available - set default for dropdown boxes */
      		  document.getElementById(this.formId).elements[0].options[2].selected = true;      		
      	  }
      	}      	
        this.loadSelect(document.getElementById(this.formId).elements[0]);
        var opts = document.getElementById(this.formId).elements[1];
        for( var i=0; i<opts.length; i++){
        	if(value == opts[i].value){
        		opts[i].selected=true;
        	}
        }
      };
  }
  MatrixTemplateFilter._initialized=true;	
};
MatrixTemplateFilter.prototype = new TemplateFilter();

/* ****************************************************************
SizeTemplateFilter is a simple MatrixTemplateFilter extension
******************************************************************/
function SizeTemplateFilter(formId, url){ 
	MatrixTemplateFilter.call(this, formId, url); 
};
SizeTemplateFilter.prototype = new MatrixTemplateFilter();

/* *******************************************************************
OptionMatrix contains an array of select options for the id specified.
**********************************************************************/
function OptionMatrix( matrixId ){
  this.matrixId = matrixId;
  this.matrixValues = new Array();
  
  if(typeof OptionMatrix._initialized == "undefined"){
		OptionMatrix.prototype.addOption = function( optionValue, optionName ){
			if(this.matrixValues){
			 idx = new Number( this.matrixValues.length );
       this.matrixValues[ idx ] = new Option(optionName, optionValue); }
      }
    OptionMatrix.prototype.output = function(){
    	if(this.matrixValues){
    		var options = "";
    		for(var i=0; i<this.matrixValues.length; i++){
    			options+=this.matrixValues[i].value+":";
    		}
    	}
    }
  }
  OptionMatrix._initialized=true;
}

/* ********************************************************************
TemplateURL provides a utilitity to add parameters to a root url for 
redirection.
***********************************************************************/
function TemplateURL( root, includeNull ){
			
	this.includeNull = includeNull;
	this.root = root;
	this.pKey = new Array();
	this.pVal = new Array();
	
	if(typeof TemplateURL._initialized == "undefined"){		
		TemplateURL.prototype._getIdx = function( id ){
			idx = -1;
			for( i=0; i<this.pKey.length; i++ ){  if(this.pKey[i] == id){ idx = i; break; }  }
			return idx;
		};
		TemplateURL.prototype.setParam = function( prop, val ){		
			if(val == "null" || val=="undefined"){ val=''; }
			var idx = this._getIdx(prop);			
			if(idx != -1){ this.pVal[idx] = val;}
			else{ this.pKey[this.pKey.length]=prop; this.pVal[this.pVal.length]=val; }			
			//alert("Setting property url parameter "+prop+" to "+val);					
		};
		TemplateURL.prototype.getURL = function(){
			urlStr = this.root;
			var init = false;
			for( i=0; i<this.pKey.length; i++ ){				
				if( this.pVal[i] == '' && !this.includeNull ){}
				else{
				  if(!init){ tStr = "?"; init = true; } else { tStr = "&" }		   
				  urlStr = urlStr + tStr + this.pKey[i]+"="+this.pVal[i];			   
			  }								
			}
			return urlStr;
		};	      	
  }	
	TemplateURL._initialized="true";
}
