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);

	
	//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 = getCapacityProds(myProd);
	if(DEBUG)alert("getCapacityProds 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 = getWhirlpoolDiffProds(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_submit_but_txt.gif\"  border=\"0\">";
	} else if (myProd != null && myProd != "" && myProd.length == 1) {
		img="<input type=\"image\" src=\"../images/products/pdt_submit_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 qualifyingProducts = new Array();
	for(var i=0;i<myProd.length;i++){
		found = true;
		for(var j=0;found && j<document.hmc.colours.length;j++){
			if(document.hmc.colours[j].checked){
				found = false;
				for(var k=0;k<products[myProd[i]].colours.length;k++){
					if(document.hmc.colours[j].value == products[myProd[i]].colours[k]){
						found = true;
						break;
					}
				}
			}
		}
		if(found && j == document.hmc.colours.length && j > 0){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching colours "+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 getCapacityProds(myProd){
	if(document.hmc.capacity.value == "" || document.hmc.capacity.value == "-1"){
		if(DEBUG)alert("No selection made for capacity OR capacity is not searching criteria");
		return myProd;
	}

	var cid = 1;
	var sid = 1123;
	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];
					var val = document.hmc.capacity.value;
					val = val.replace(/>=\s+(\d+)/,"$1");
					if(s.specid == sid && />=\s+\d+/.test(document.hmc.capacity.value) && s.value >= val){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}
				}
			}
		}
	}
	if(DEBUG)alert("total products found matching capacity "+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 qualifyingProducts = new Array();
	var count = 0;
	var sid = 1126;
	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 getWhirlpoolDiffProds(myProd){
	var found = false;
	if(document.hmc.WhirlpoolDiff.checked){
			found = true;
		
		}
	if(!found){
		if(DEBUG)alert("No selection made for WhirlpoolDiff");
		return myProd;
	}
	var arr = new Array();
	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<1;j++){
			if(document.hmc.WhirlpoolDiff.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 param = document.hmc.WhirlpoolDiff.value;
						 arr = param.split("||");
						 if(arr[0] == s.specid && /[JAja]+/.test(s.value)){
							found = true;
							break cla;
						}
					}
				}
			}
		}
		if(found && j == 1 && j > 0){
			qualifyingProducts.push(myProd[i]);
			count++;
		}
	}
	if(DEBUG)alert("total products found matching WhirlpoolDiff "+count);
	return qualifyingProducts;
}

function fillKeys(){

	var pattern = /,$/;

	var keys = new Array();
	keys.push("Edelstahl");
	keys.push("Titan");
	keys.push("Weiss");
	keys.push("Schwarz");
	keys.push("Braun");
	keys.push("Aluminium");
	keys.push("Silbergrau");
	

	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("Nicht wichtig für mich");
	keys.push("Solo-Mikrowelle");
	keys.push("Kombinationsmikrowelle");
	keys.push("Mikrowelle mit Dunstabzugshaube");
	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ür mich");
	keys.push("40 Liter und mehr");
	keys.push("17 Liter und mehr");
	document.hmc.KEY_capacity.value=document.hmc.KEY_capacity.value+keys[document.hmc.capacity.options.selectedIndex];
	if(DEBUG)alert("document.hmc.KEY_capacity.value "+document.hmc.KEY_capacity.value);	

	keys = new Array();
	keys.push("Crisp");
	
			if(document.hmc.WhirlpoolDiff.checked){
			document.hmc.KEY_WhirlpoolDiff.value=document.hmc.KEY_WhirlpoolDiff.value+keys[0];
		}

	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);

}


////////////////////////////// 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;
       if ((finalMinVal+minVal) > showMinPrice)
       {
         showmin =  Math.floor((finalMinVal+minVal)/10);
         showmin =  (showmin * 10) - showMinPrice;
       }
        var showmax =  Math.floor((maxVal-finalMaxVal)/10);
    	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 = 132;
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();
