var DEBUG = false;
//////////////////////// HMC FUNCTIONS ////////////////////////////

function updateProducts(){

	var myProd = new Array();

	for(var i=0;i<products.length;i++){
		myProd.push(i);
	}

	// check all products
	myProd = getBIProds(myProd);

	if(DEBUG)alert("getBIProds 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 = getTypeProds(myProd);
	if(DEBUG)alert("getTypeProds returned products "+myProd.length);

    myProd = getCompartmentsProds(myProd);
	if(DEBUG)alert("getCompartmentsProds returned products "+myProd.length);

    myProd = getWhirlpoolDiffProds(myProd);
    if(DEBUG)alert("getWhirlpoolDiffProds returned products "+myProd.length);

    myProd = getCapacityProds(myProd);
    if(DEBUG)alert("getCapacityProds returned products "+myProd.length);

	//var prodStr = " product(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 getBIProds(myProd){

	if(document.hmc.state[0].checked || document.hmc.state[1].checked){
		var count = 0;
		var bi;
		var qualifyingProducts = new Array();
		if(document.hmc.state[0].checked) bi = false;
		if(document.hmc.state[1].checked) bi = true;

		for(var i=0;i<myProd.length;i++){
			if(products[myProd[i]].isBuiltIn == bi){
				count++;
				qualifyingProducts.push(myProd[i]);
			}
		}
		if(DEBUG)alert("total products fround matching BI "+count);
		return qualifyingProducts;
	}else{
		if(DEBUG)alert("No Selection criteria for BI");
		return myProd;
	}
}


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 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 = 1063;
	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() == (products[myProd[i]].classifications[j].specs[k].value).toUpperCase()){
					qualifyingProducts.push(myProd[i]);
					count++;
					continue prod;
				}
			}
		}

	}

	if(DEBUG)alert("total products found matching Type "+count);
	return qualifyingProducts;
}

function getCompartmentsProds(myProd){

    if(document.hmc.compartments.value == "" || document.hmc.compartments.value == "-1"){
        if(DEBUG)alert("No selection made for Compartment OR Compartment is not searching criteria");
        return myProd;
    }
    var cid = 1;
    var sid = 1060;
    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];
                if(s.specid == sid && />=\s+5/.test(document.hmc.compartments.value) && s.value >= 5){
                    qualifyingProducts.push(myProd[i]);
                    count++;
                    continue prod;
                }else if(s.specid == sid && /<\s+5/.test(document.hmc.compartments.value) && s.value < 5){
                    qualifyingProducts.push(myProd[i]);
                    count++;
                    continue prod;
                }
            }
        }

}
if(DEBUG)alert("total products found matching Compartment "+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 qualifyingProducts = new Array();
	var arr = 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 && 1062 == s.specid && 'A' == (s.value).substring(0,1).toUpperCase()){
							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 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 sid = 1059;
	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+10/.test(document.hmc.capacity.value) && s.value > 10){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}else if(s.specid == sid && /<=\s+10/.test(document.hmc.capacity.value) && s.value <= 10){
						qualifyingProducts.push(myProd[i]);
						count++;
						continue prod;
					}
				}
			//}
		}
	}
	if(DEBUG)alert("total products found matching Compartment "+count);
	return qualifyingProducts;
}



function fillKeys(){
	if(document.hmc.state[0].checked){
		document.hmc.KEY_state.value="Standgerät";
	}else if(document.hmc.state[1].checked){
		document.hmc.KEY_state.value="Einbaugerät";
	}

	if(DEBUG)alert("document.hmc.KEY_state.value "+document.hmc.KEY_state.value);

	var keys = new Array();
	keys.push("Nicht wichtig für mich");
	keys.push("Gefriergerät größer 85 cm");
	keys.push("Intigrierbar/Unterbau Gefriergerät");
	keys.push("Gefriertruhe");

	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("5 oder mehr");
 	keys.push("4 oder weniger");
 	document.hmc.KEY_compartments.value=document.hmc.KEY_compartments.value+keys[document.hmc.compartments.options.selectedIndex];
 	if(DEBUG)alert("document.hmc.KEY_compartments.value "+document.hmc.KEY_compartments.value);

 	keys = new Array();
 	keys.push("Nicht wichtig für mich");
 	keys.push("über 10 kg");
 	keys.push("10 kg und weniger");
 	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("Energieklasse A oder besser");
 	/*for(var i=0;i<keys.length;i++){
 		if(document.hmc.WhirlpoolDiff[i].checked){
 			document.hmc.KEY_WhirlpoolDiff.value=document.hmc.KEY_WhirlpoolDiff.value+keys[i]+",";
 		}
 	}*/
 	if(document.hmc.WhirlpoolDiff.checked){
 	    document.hmc.KEY_WhirlpoolDiff.value = document.hmc.KEY_WhirlpoolDiff.value+keys[0];
 	}
 	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 = dd.elements.thumbl.x;
var maxX = minX+tWidth+70;

//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();