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 = getColourProds(myProd);

	//if(DEBUG)alert("getColourProds returned products "+myProd.length);
	
	myProd = getWhirlpoolDiffProds(myProd);
	if(DEBUG)alert("getWhirlpoolDiffProds 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);

	//check only those products which satisfy Built-in/FS, Colours, Price & USP criteria
	myProd = getWidthProds(myProd);
	if(DEBUG)alert("getWidthProds returned products "+myProd.length);

	//check only those products which satisfy all above criteria
	//myProd = getTypeProds(myProd);
	//if(DEBUG)alert("getTypeProds returned products "+myProd.length);
	
	myProd = getBuiltInProds(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 getColourProds(myProd){

	var found = false;
	for(var j=0;j<document.hmc.colours.length;j++){
		if(document.hmc.colours[j].checked){
			found = true;
			break;
		}
	}

	if(!found){
		if(DEBUG)alert("No selection made for colours");
		return myProd;
	}

	var count = 0;
	var isFound = false;
	var qualifyingProducts = new Array();
	for(var i=0;i<myProd.length;i++){
		isFound = false;
        for(var j=0;j<document.hmc.colours.length;j++){
			if(document.hmc.colours[j].checked){
             for(var k=0;k<products[myProd[i]].colours.length;k++){
              if(document.hmc.colours[j].value == products[myProd[i]].colours[k]){
						isFound = true;
						break;
					}
				}
			}
		}
		if(isFound){
			qualifyingProducts.push(myProd[i]);
			count++;

		}
	}
	if(DEBUG)alert("total products found matching colours "+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 && 25316 == s.specid && 'JA' == (s.value).toUpperCase()){
							found = true;
							break cla;
						}
						else if(arr[0] == s.specid && 25321 == s.specid && 'JA' == (s.value).toUpperCase()){
							found = true;
							break cla;
						}
						else if(arr[0] == s.specid && 25315 == 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 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 sid = 133;
	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++){
			for(var k=0;k<products[myProd[i]].classifications[j].specs.length;k++){
				var s = products[myProd[i]].classifications[j].specs[k];
				var va = (s.value).toUpperCase();
				var dva = (document.hmc.type.value).toUpperCase();
				if(s.specid == sid && va == dva){
					qualifyingProducts.push(myProd[i]);
					count++;
					continue prod;
				}
			}
		}
	}
	if(DEBUG)alert("total products found matching Type "+count);
	return qualifyingProducts;
}
*/

function getBuiltInProds(myProd){
	
	var found = false;
	for(var j=0;j<document.hmc.builtin.length;j++){
			if(document.hmc.builtin[j].checked){
			found = true;
			break;
		}
	}
	
	if(!found){
		if(DEBUG)alert("No selection made for width");
		return myProd;
	}
	
	var qualifyingProducts = new Array();
	var count = 0;
	var sid = 133;
	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];
				for(var x=0; x<document.hmc.builtin.length;x++){
					if(document.hmc.builtin[x].checked){
						var val = document.hmc.builtin[x].value;			
									
								if(s.specid == sid && s.value == val){
									qualifyingProducts.push(myProd[i]);
									count++;
									continue prod;
				}
				}
				}
			}
		}

	}

	if(DEBUG)alert("total products found matching builtin "+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){
	if(document.hmc.width.value == "" || document.hmc.width.value == "-1"){
		if(DEBUG)alert("No selection made for width OR width is not searching criteria");
		return myProd;
	}

	var cid = 2;
	var sid = 1934;
	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];
					
					if(s.specid == sid && />=\s+90/.test(document.hmc.width.value) && s.value >= 90){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}else if(s.specid == sid && /=\s+60/.test(document.hmc.width.value) && s.value == 60){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}
					
					/*var val = document.hmc.width.value;
					val = val.replace(/=\s+(\d+)/,"$1");
					if(s.specid == sid && /=\s+\d+/.test(document.hmc.width.value) && s.value == val){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}*/
					
				//}
			//}
		//}
	//}
/*	if(DEBUG)alert("total products found matching width "+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 = 1934;
	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+90/.test(document.hmc.width[x].value) && s.value > 90){
								qualifyingProducts.push(myProd[i]);
								count++;
								continue prod;
							}else if(s.specid == sid && /=\s+90/.test(document.hmc.width[x].value) && s.value == 90){
							qualifyingProducts.push(myProd[i]);
							count++;
							continue prod;
							}else if(s.specid == sid && /<\s+90/.test(document.hmc.width[x].value) && s.value < 90){
							qualifyingProducts.push(myProd[i]);
							count++;
							continue prod;
						}
					}
					}
				}
			}
		}
	}
	if(DEBUG)alert("total products found matching width "+count);
	return qualifyingProducts;
}

function fillKeys(){

	var keys = new Array();
	var pattern = /,$/;


	/*keys.push("Inox");
	keys.push("Titanium");
	keys.push("Silbergrau");
	keys.push("White");
	//keys.push("Brown");
	keys.push("Aluminium");
	for(var i=0;i<document.hmc.colours.length;i++){
		if(document.hmc.colours[i].checked){
			document.hmc.KEY_colours.value = document.hmc.KEY_colours.value+keys[i]+",";
		}
	}
	document.hmc.KEY_colours.value = (document.hmc.KEY_colours.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_colours.value "+document.hmc.KEY_colours.value);*/
	
	keys = new Array();
	keys.push("Randafzuiging");
	keys.push("Pro Touch, anti-fingerprint");
	keys.push("Deep Silence, zeer stil");
	
	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("Niet van belang");
	keys.push("Wandschouw");
	keys.push("Eiland");
	keys.push("Vlakscherm");
	keys.push("Inbouwunit");
	keys.push("Geintegreerde");
	keys.push("Onderbouw");
	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("Wandschouw");
	keys.push("Eiland");
	keys.push("Vlakscherm");
	keys.push("Inbouwunit");
	keys.push("Geïntegreerde");
	keys.push("Onderbouw");
	
	for(var i=0;i<document.hmc.builtin.length;i++){
		if(document.hmc.builtin[i].checked){
			document.hmc.KEY_builtin.value=document.hmc.KEY_builtin.value+keys[i]+",";
		}
	}
	
	document.hmc.KEY_builtin.value = (document.hmc.KEY_builtin.value).replace(pattern,"");
	if(DEBUG)alert("document.hmc.KEY_builtin.value "+document.hmc.KEY_builtin.value);
	
/*
	keys = new Array();
	keys.push("Niet van belang");
	//keys.push("50 cm");
	keys.push("60 cm");
	//keys.push("80 cm");
	keys.push("90 cm of meer");
	//keys.push("100 cm");
	document.hmc.KEY_width.value=document.hmc.KEY_width.value+keys[document.hmc.width.options.selectedIndex];
	if(DEBUG)alert("document.hmc.KEY_width.value "+document.hmc.KEY_width.value);*/
	
	keys = new Array();
	keys.push("Extra breed (meer dan 90 cm)");
	//keys.push("über 60 cm");
	keys.push("Breed (90 cm)");
	keys.push("Standaard (50-70 cm)");
	//keys.push("kleiner 55 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);
	
}


////////////////////////////// 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 = 133;
var maxX = minX+tWidth+72;

//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();

