var DEBUG = false;
//////////////////////// HMC FUNCTIONS ////////////////////////////

function updateProducts(){
	var myProd = new Array();
	for(var i=0;i<products.length;i++){
		myProd.push(i);
	}

	//check only those products which satisfy Built-in/FS criteria

	//myProd = getLookforProds(myProd);
	//if(DEBUG)alert("getLookforProds returned products "+myProd.length);

	//check only those products which satisfy Built-in/FS & Colours criteria
	myProd = getPriceProds(myProd);
	if(DEBUG)alert("getPriceProds returned products "+myProd.length);
	
	myProd = getCapacityProds(myProd);
	if(DEBUG)alert("getCapacityProds returned products "+myProd.length);

	//check only those products which satisfy Built-in/FS, Colours, Price & USP criteria
	myProd = getWidthProds(myProd);
	if(DEBUG)alert("getWidthProds returned products "+myProd.length);
	
	myProd = getWhirlpoolDiffProds(myProd);
	if(DEBUG)alert("getWhirlpoolDiffProds returned products "+myProd.length);
	//myProd = getSurfaceProds(myProd);
	//if(DEBUG)alert("getSurfaceProds returned products "+myProd.length);

	//myProd = getWhirlpoolDiffProds(myProd);
	//if(DEBUG)alert("getWhirlpoolDiffProds returned products "+myProd.length);

	//check only those products which satisfy all above criteria
	//myProd = getTypeProds(myProd);

	//var prodStr = " model(s)";
	if(myProd != null && myProd != "" && myProd.length > 0){
		prodStr = myProd.length;
	}else{
		prodStr = 0;
	}
	dd.elements.matchedProds.write(prodStr);
	var img= "";

	if (myProd != null && myProd != "" && myProd.length > 1) {
		img="<input type=\"image\" src=\"../images/products/pdt_cmpre_but_txt.gif\"  border=\"0\">";
	} else if (myProd != null && myProd != "" && myProd.length == 1) {
		img="<input type=\"image\" src=\"../images/products/pdt_view_but_txt.gif\"  border=\"0\">";
	}
	dd.elements.sub_but.write(img);

	if(DEBUG)alert("Total products qualifying all criteria are "+myProd.length);
}

/*
function getTypeProds(myProd){

	if(document.hmc.type.value == "" || document.hmc.type.value == "-1"){
		if(DEBUG)alert("No selection made for Type");
		return myProd;
	}

	var found = false;
	var qualifyingProducts = new Array();
	var count = 0;
	var sid = 91;
	prod:for(var i=0;i<myProd.length;i++){
		for(var j=0;j<products[myProd[i]].classifications.length;j++){
			for(var k=0;k<products[myProd[i]].classifications[j].specs.length;k++){
				var s = products[myProd[i]].classifications[j].specs[k];
				if(s.specid == sid && (document.hmc.type.options[document.hmc.type.options.selectedIndex].value).toUpperCase() == (s.value).toUpperCase()){
					qualifyingProducts.push(myProd[i]);
					count++;
					continue prod;
				}
			}
		}

	}

	if(DEBUG)alert("total products found matching Type "+count);
	return qualifyingProducts;
}
*/

function getPriceProds(myProd){

	var count = 0;
	var qualifyingProducts = new Array();
	for(var i=0;i<myProd.length;i++){
		if(parseInt(products[myProd[i]].price) >= parseInt(document.hmc.minPrice.value) && parseInt(document.hmc.maxPrice.value) >= parseInt(products[myProd[i]].price)){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching price "+count);
	return qualifyingProducts;
}
function getWidthProds(myProd){
	var found = false;
	for(var j=0;j<document.hmc.width.length;j++){
			if(document.hmc.width[j].checked){
			found = true;
			break;
		}
	}
	
	if(!found){
		if(DEBUG)alert("No selection made for width");
		return myProd;
	}

	var cid = 1;
	var sid = 20734;
	var count = 0;
	var qualifyingProducts = new Array();
	prod:for(var i=0;i<myProd.length;i++){
		for(var j=0;j<products[myProd[i]].classifications.length;j++){
			if(products[myProd[i]].classifications[j].classid = cid){
				for(var k=0;k<products[myProd[i]].classifications[j].specs.length;k++){
					var s = products[myProd[i]].classifications[j].specs[k];
					for(var x=0; x<document.hmc.width.length;x++){
						if(document.hmc.width[x].checked){
							if(s.specid == sid && /<=\s+600/.test(document.hmc.width[x].value) && s.value <= 600){
								qualifyingProducts.push(myProd[i]);
								count++;
								continue prod;
							}else if(s.specid == sid && />\s+600/.test(document.hmc.width[x].value) && s.value > 600){
							qualifyingProducts.push(myProd[i]);
							count++;
							continue prod;
					}
					}
					}
				}
			}
		}
	}
	if(DEBUG)alert("total products found matching width "+count);
	return qualifyingProducts;
}
/*
function getSurfaceProds(myProd){

	if(document.hmc.surface.value == "" || document.hmc.surface.value == "-1"){
		if(DEBUG)alert("No selection made for Surface");
		return myProd;
	}

	var found = false;
	var qualifyingProducts = new Array();
	var count = 0;
	var sid = 92;
	prod:for(var i=0;i<myProd.length;i++){
		for(var j=0;j<products[myProd[i]].classifications.length;j++){
			for(var k=0;k<products[myProd[i]].classifications[j].specs.length;k++){
				var s = products[myProd[i]].classifications[j].specs[k];
				if(s.specid == sid && (document.hmc.surface.options[document.hmc.surface.options.selectedIndex].value).toUpperCase() == (s.value).toUpperCase()){
					qualifyingProducts.push(myProd[i]);
					count++;
					continue prod;
				}
			}
		}

	}

	if(DEBUG)alert("total products found matching Type "+count);
	return qualifyingProducts;

}
*/
/*function getWhirlpoolDiffProds(myProd){

	var found = false;
	for(var j=0;j<document.hmc.WhirlpoolDiff.length;j++){
		if(document.hmc.WhirlpoolDiff[j].checked){
			found = true;
			break;
		}
	}

	if(!found){
		if(DEBUG)alert("No selection made for WhirlpoolDiff");
		return myProd;
	}

	var qualifyingProducts = new Array();
	var count = 0;
	for(var i=0;i<myProd.length;i++){
		found = true;
		var j=0;
		for(j=0; found && j<document.hmc.WhirlpoolDiff.length;j++){
			if(document.hmc.WhirlpoolDiff[j].checked){
				found = false;
				cla:for(var k=0;k<products[myProd[i]].classifications.length;k++){
					for(var l=0;l<products[myProd[i]].classifications[k].specs.length;l++){
						var s = products[myProd[i]].classifications[k].specs[l];
						var arr = (document.hmc.WhirlpoolDiff[j].value).split("||");
						if(arr[0] == s.specid && /[JAja]+/.test(s.value)){
							found = true;
							break cla;
						}
					}
				}
			}
		}
		if(found && j == document.hmc.WhirlpoolDiff.length && j > 0){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching WhirlpoolDiff "+count);
	return qualifyingProducts;
}
*/
/*function getLookforProds(myProd){

	var found = false;
	for(var j=0;j<document.hmc.lookfor.length;j++){
		if(document.hmc.lookfor[j].checked){
			found = true;
			break;
		}
	}

	if(!found){
		if(DEBUG)alert("No selection made for lookfor");
		return myProd;
	}

	var qualifyingProducts = new Array();
	var count = 0;
	for(var i=0;i<myProd.length;i++){
		found = true;
		var j=0;
		for(j=0; found && j<document.hmc.lookfor.length;j++){
			if(document.hmc.lookfor[j].checked){
				found = false;
				cla:for(var k=0;k<products[myProd[i]].classifications.length;k++){
					for(var l=0;l<products[myProd[i]].classifications[k].specs.length;l++){
						var s = products[myProd[i]].classifications[k].specs[l];
						var arr = (document.hmc.lookfor[j].value).split("||");
						if(arr[0] == s.specid && /[JAja]+/.test(s.value)){
							found = true;
							break cla;
						}
					}
				}
			}
		}
		if(found && j == document.hmc.lookfor.length && j > 0){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching lookfor "+count);
	return qualifyingProducts;
}
*/
function getCapacityProds(myProd){

	
	var found = false;
	for(var j=0;j<document.hmc.capacity.length;j++){
			if(document.hmc.capacity[j].checked){
			found = true;
			break;
		}
	}
	
	if(!found){
		if(DEBUG)alert("No selection made for Type");
		return myProd;
	}

	var cid = 1;
	var sid = 91;
	var count = 0;
	var qualifyingProducts = new Array();
	prod:for(var i=0;i<myProd.length;i++){
		for(var j=0;j<products[myProd[i]].classifications.length;j++){
			if(products[myProd[i]].classifications[j].classid = cid){
				for(var k=0;k<products[myProd[i]].classifications[j].specs.length;k++){
					var s = products[myProd[i]].classifications[j].specs[k];
					for(var x=0; x<document.hmc.capacity.length;x++){
							if(document.hmc.capacity[x].checked){
								var val = document.hmc.capacity[x].value;
								
									
								if(s.specid == sid && s.value == val){
									qualifyingProducts.push(myProd[i]);
									count++;
									continue prod;
								}
							}
					}
				}
			}
		}
	}
	
	
	if(DEBUG)alert("total products found matching capacity "+count);
	return qualifyingProducts;
}
function getWhirlpoolDiffProds(myProd){

	var found = false;
	for(var j=0;j<document.hmc.WhirlpoolDiff.length;j++){
		if(document.hmc.WhirlpoolDiff[j].checked){
			found = true;
			break;
		}
	}
	
	if(!found){
		if(DEBUG)alert("No selection made for WhirlpoolDiff");
		return myProd;
	}

	var qualifyingProducts = new Array();

	var count = 0;
	for(var i=0;i<myProd.length;i++){
		found = true;
		var j=0;
		for(j=0; found && j<document.hmc.WhirlpoolDiff.length;j++){
			if(document.hmc.WhirlpoolDiff[j].checked){
				found = false;
				cla:for(var k=0;k<products[myProd[i]].classifications.length;k++){
					for(var l=0;l<products[myProd[i]].classifications[k].specs.length;l++){
						var arr = (document.hmc.WhirlpoolDiff[j].value).split("||");
						var s = products[myProd[i]].classifications[k].specs[l];
						if(arr[0] == s.specid && 25236 == s.specid && 'JA' == (s.value).toUpperCase()){
							found = true;
							break cla;
						}
						else if(arr[0] == s.specid && 25242 == s.specid && 'JA' == (s.value).toUpperCase()){
							found = true;
							break cla;
						}
					}
				}
			}
		}
		if(found && j == document.hmc.WhirlpoolDiff.length && j > 0){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching WhirlpoolDiff "+count);
	return qualifyingProducts;
}
function fillKeys(){

	var keys = new Array();
	var pattern = /,$/;
/*
	keys = new Array();
	keys.push("Niet van belang");
	keys.push("Gas");
	//keys.push("Elektrische platen");
	keys.push("Elektrische glaskeramische");
	keys.push("Elektrische glaskeramische - inductie");

	document.hmc.KEY_type.value = document.hmc.KEY_type.value+keys[document.hmc.type.options.selectedIndex];
	if(DEBUG)alert("document.hmc.KEY_type.value "+document.hmc.KEY_type.value);
*/
   /*
	keys = new Array();
	keys.push("Nicht wichtig f&uuml;r mich");
	keys.push("Edelstahl");
	keys.push("Glaskeramik");
	document.hmc.KEY_surface.value=document.hmc.KEY_surface.value+keys[document.hmc.surface.options.selectedIndex];
	if(DEBUG)alert("document.hmc.KEY_surface.value "+document.hmc.KEY_surface.value);
     */

	keys = new Array();
	//keys.push("Niet van belang");
	keys.push("Standaard (60 cm breed)");
	keys.push("Andere afmetingen");
	//keys.push("60 cm");
	for(var i=0;i<document.hmc.width.length;i++){
		if(document.hmc.width[i].checked){
			document.hmc.KEY_width.value=document.hmc.KEY_width.value+keys[i]+",";
		}
	}
	document.hmc.KEY_width.value= (document.hmc.KEY_width.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_width.value "+document.hmc.KEY_width.value);
	
	keys = new Array();
	keys.push("Gas");
	keys.push("Vitrokeramisch");
	keys.push("Inductie");
	
	for(var i=0;i<document.hmc.capacity.length;i++){
		if(document.hmc.capacity[i].checked){
			document.hmc.KEY_capacity.value=document.hmc.KEY_capacity.value+keys[i]+",";
		}
	}
	document.hmc.KEY_capacity.value = (document.hmc.KEY_capacity.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_capacity.value "+document.hmc.KEY_capacity.value);
	
	keys = new Array();
	keys.push("Wokbrander (gaskookplaten)");
	keys.push("Direct Touch (vitrokeramische kookplaten)");
	for(var i=0;i<document.hmc.WhirlpoolDiff.length;i++){
		if(document.hmc.WhirlpoolDiff[i].checked){
			document.hmc.KEY_WhirlpoolDiff.value=document.hmc.KEY_WhirlpoolDiff.value+keys[i]+",";
		}
	}
	document.hmc.KEY_WhirlpoolDiff.value = (document.hmc.KEY_WhirlpoolDiff.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_WhirlpoolDiff.value "+document.hmc.KEY_WhirlpoolDiff.value);

/*
	keys = new Array();
	keys.push("Touch-Control");
	keys.push("Induktion");
	for(var i=0;i<document.hmc.WhirlpoolDiff.length;i++){
		if(document.hmc.WhirlpoolDiff[i].checked){
			document.hmc.KEY_WhirlpoolDiff.value=document.hmc.KEY_WhirlpoolDiff.value+keys[i]+",";
		}
	}
	document.hmc.KEY_WhirlpoolDiff.value = (document.hmc.KEY_WhirlpoolDiff.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_WhirlpoolDiff.value "+document.hmc.KEY_WhirlpoolDiff.value);

	keys = new Array();
	keys.push("Kocheflächen nur in Verbindung mit einem Bauknecht Herd");
	keys.push("Eigenständige Kochfelder");
	for(var i=0;i<document.hmc.WhirlpoolDiff.length;i++){
		if(document.hmc.lookfor[i].checked){
			document.hmc.KEY_lookfor.value=document.hmc.KEY_lookfor.value+keys[i]+",";
		}
	}
	document.hmc.KEY_lookfor.value = (document.hmc.KEY_lookfor.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_WhirlpoolDiff.value "+document.hmc.KEY_lookfor.value);
*/

}


////////////////////////////// END HMC FUNCTIONS ///////////////////////

function my_DragFunc(){
    if (dd.obj.name.indexOf('thumb') >= 0){
		setMoveLimits();
		calculate();
    }
}

function my_DropFunc(){
    if (dd.obj.name.indexOf('thumb') >= 0){
		updateProducts();
    }
}

function setMoveLimits(){
	dd.elements.thumbr.maxoffl = tWidth - getDisplacement(dd.elements.thumbl);
	dd.elements.thumbl.maxoffr = tWidth - getDisplacement(dd.elements.thumbr);
}

function getDisplacement(d_o){
	return Math.abs(d_o.defx-d_o.x);
}

function calculate(){

	var multiplier = (maxVal - minVal)/tWidth;
	var LfractionMove = dd.elements.thumbl.x-dd.elements.thumbl.defx;
	var RfractionMove = dd.elements.thumbr.defx-dd.elements.thumbr.x;
	finalMinVal = (LfractionMove*multiplier);
	finalMaxVal = (RfractionMove*multiplier);

       var showmin = 0;
	   var showMinPrice = 0;
	   
      if ((finalMinVal+minVal) > showMinPrice)
      {
        showmin =  Math.floor((finalMinVal+minVal)/10);
        showmin =  (showmin * 10) - showMinPrice;
      }
       var showmax =  Math.floor((maxVal-finalMaxVal)/10);
	   var showMaxPrice = 0;
    showmax =  (showmax * 10) +showMaxPrice;


	var divStart = '<p class="awht8n">';
	currency = '&euro;'
	var divEnd = ' </p>';

	//dd.elements.thumbl.write(divStart+Math.ceil(finalMinVal+minVal)+currency+divEnd);
	//dd.elements.thumbr.write(divStart+Math.ceil(maxVal-finalMaxVal)+currency+divEnd);

    dd.elements.thumbl.write(divStart+Math.ceil(showmin)+currency+divEnd);
   	dd.elements.thumbr.write(divStart+Math.ceil(showmax)+currency+divEnd);

	document.hmc.minPrice.value = Math.ceil(finalMinVal+minVal);
	document.hmc.maxPrice.value = Math.ceil(maxVal-finalMaxVal);
}

//initialization setting
SET_DHTML("thumbl"+MAXOFFLEFT+0+MAXOFFRIGHT+tWidth+HORIZONTAL,"thumbr"+MAXOFFLEFT+tWidth+MAXOFFRIGHT+0+HORIZONTAL,"track"+NO_DRAG);
dd.elements.thumbl.setZ(dd.elements.track.z+1);
dd.elements.thumbr.setZ(dd.elements.track.z+1);
dd.elements.track.addChild("thumbl");
dd.elements.track.addChild("thumbr");


var minX = 136;
var maxX = minX+tWidth+71;

//set initial positions of markers..
dd.elements.thumbl.defx = minX;
dd.elements.thumbr.defx = maxX;

// move to initial position
if(minValDiff == 0 && maxValDiff == 0){
	dd.elements.thumbl.moveTo(minX, tY);
	dd.elements.thumbr.moveTo(maxX, tY);
}else{
	var pos = ((tWidth*minValDiff)/(maxVal - minVal))+minX;
	dd.elements.thumbl.moveTo(pos, tY);
	pos = maxX-((tWidth*maxValDiff)/(maxVal - minVal));
	dd.elements.thumbr.moveTo(pos, tY);
}

// For Updating No. Of Products...
ADD_DHTML("matchedProds");
ADD_DHTML("sub_but");

calculate();
updateProducts();
