// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function toggle_producttypes(e) {
    var c = $$('div#producttypes input');
        for (var i = 0; i < c.length; i++) {
            c[i].checked = e.checked
        }
}

function toggle_companies(e) {
 if (e.value != "") {
    $('companies').hide();
 } else {
    $('companies').show();
 }
}


/* This part was migrated from the old website */
function calculate (pulldown1, pulldown2, pulldown3) {
	widthsel = get_select_value(pulldown1);
	lengthsel = get_select_value(pulldown2);
	depthsel = get_select_value(pulldown3);

	checkNum(document.form1.width.value);
	checkNum(document.form1.length.value);
	checkNum(document.form1.depth.value);


	wid = document.form1.width.value;
	cwid = convert(widthsel, wid);
	len = document.form1.length.value;
	clen = convert(lengthsel, len);
	dep = document.form1.depth.value;
	cdep = convert(depthsel, dep);
	
	cumeters = cwid * clen * cdep;
	document.form1.metric.value = cumeters.toFixed(2);
	document.form1.imperial.value = (cumeters * 1.3079506).toFixed(2);
        

}

function convert (pulldown, val) {
	var answer;

	if (pulldown == "feet") {
		answer = val * 0.3048;
	}	

	else if (pulldown == "yards") {
		answer = val * 0.9144;
	}

	else if (pulldown == "inches") {
		answer = val * 0.0254;
	}
	else if (pulldown == "meters") {
		answer = val;
	}
	else if (pulldown == "centimeters") { 
		answer = val * 0.01;
	}
			

	return answer;

}

// elem takes the input of the select "name" field
function get_select_value(elem){

        //  return the "value" of the selected option in a "select" 
        //  (does not work with "multiple")
        j  = elem.length;
        k  = -1;
        var opts = elem.options;
        for(i = 0; i < j; i++){
            if(opts[i].selected){
                return opts[i].value;
            } 
			else if(opts[i].defaultSelected){
                k = i;
            }
        }
        return (k >= 0) ?  opts[k].value : null;
}

function checkNum(str) {
        for (var i=0; i<str.length; i++) {
        var ch = str.substring(i, i + 1)
        if (ch!="." && ch!="+" && ch!="-" && ch!="e" && ch!="E" && (ch < "0" || ch > "9")) {
                alert('"' + str + '"' + " is not a valid number.");
                return false
                }
        }
        return true
}


function Clear(curForm) {
	curForm.width.value="";
	curForm.length.value="";
	curForm.depth.value="";
	curForm.metric.value="";
	curForm.imperial.value="";
	curForm.widthselect.selectedIndex=0
	curForm.lengthselect.selectedIndex=0
	curForm.depthselect.selectedIndex=0

}
