
function ratioChange(dimD, dimL, dimR, dimDP, dsID, dsIDCpy, dtID, dtID2, sID1, tID1, sID2, tID2, sID3, tID3, bPreFill) {
	
	if (bPreFill) { preFillSizes(sID1,sID2,sID3); }
	
	var d = dimD.split(",");
	var l = dimL.split(",");
	var r = dimR.split(",");
	var dp = dimDP.split(",");

	//if (dsIDCpy != "") { $get(dsIDCpy).innerText = $get(dsID).children[$get(dsID).selectedIndex].text; }

	for (var i = 0; i < d.length; i++) {
		if (d[i] == $get(dsID).selectedIndex) {
			// we have a match
			$get(dtID).innerText = l[i];
			//if (dtID2 != "") { $get(dtID2).innerText = l[i]; }
			if (tID1 != "") $get(tID1).innerText = roundNumber($get(sID1).value * r[i], dp[i]);
			if (tID2 != "") $get(tID2).innerText = roundNumber($get(sID2).value * r[i], dp[i]);
			if (tID3 != "") $get(tID3).innerText = roundNumber($get(sID3).value * r[i], dp[i]);
			break;
		}
	}

}

function preFillSizes(minSize, maxSize, totalSize) {

    var iMaxSize = parseFloat($get(maxSize).value);

    var iTotalSize = iMaxSize;
    if (totalSize != '')
        parseFloat($get(totalSize).value);

    var iMinSize = iMaxSize;
	if (minSize != '')
	    iMinSize = parseFloat($get(minSize).value);

	if (iMaxSize == 0 && iTotalSize == 0) {
        $get(maxSize).value = iMinSize;
        if (totalSize != '')
            $get(totalSize).value = iMinSize;
	} else if (iMaxSize != iTotalSize && iMaxSize > iTotalSize) {
        if (totalSize != '')
            $get(totalSize).value = iMaxSize;
	} else if (iTotalSize > iMaxSize) {
		//do nothing
	}
}

function roundNumber(x, dp) {
	return Math.round(x*Math.pow(10,dp))/Math.pow(10,dp);
}

function selectDropList(searchTxt, cmb) {

    var id = $get(cmb);

    var bDistrict = (id.id.indexOf("cmbIntTown") == -1);
    var searchTxt = searchTxt.toLowerCase();

    if (bDistrict && id.selectedIndex > 1)
        return;

    if (searchTxt == "" || searchTxt == undefined)
        return;

    for (var i = 0; i < id.length; i++) {

        var sOption = id.options[i].text.toLowerCase();

        if (sOption == searchTxt) {
            id.options[i].selected = true;
            break;
        } else {

            if (bDistrict) {
                var iIndex = sOption.indexOf("-");

                if (id.options[i].text.toLowerCase() == searchTxt) {
                    id.options[i].selected = true;
                    break;
                }

                if (iIndex > -1) {
                    sOption = sOption.substring(iIndex + 1, sOption.length);
                    sOption = trim(sOption);
                    if (sOption == searchTxt) {
                        id.options[i].selected = true;
                        if (searchTxt.length == sOption.length) { break; }
                    }
                } else {
                    return;
                }
            }
            else {
                var iIndex = sOption.indexOf(" ");

                // check for the word in the name with a space around it
                if (iIndex > -1) {
                    if (sOption.indexOf(" " + searchTxt + " ") >= 0 ||
				        sOption.indexOf(searchTxt + " ") == 0 ||
				        (sOption.indexOf(" " + searchTxt) > -1 && sOption.indexOf(" " + searchTxt) == (sOption.length - searchTxt.length - 1))
				        ) {
                        id.options[i].selected = true;
                    }
                }

                // check for the word in the name with a dash around it
                if (iIndex > -1) {
                    if (sOption.indexOf("-" + searchTxt + "-") >= 0 ||
				            sOption.indexOf(searchTxt + "-") == 0 ||
				            (sOption.indexOf("-" + searchTxt) > -1 && sOption.indexOf("-" + searchTxt) == (sOption.length - searchTxt.length - 1))
				            ) {
                            id.options[i].selected = true;
                    }
                }
            }
        }
    }

}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

//***************** TEST AREA *****************//

function calcRate(MaxSize, DimensionText, MaxSizeClientID, DimensionClientID, RateClientID, OutputClientID) {
	
	var rate;

	var MainRate = $get(RateClientID).value;
	do {
		MainRate = MainRate.replace(",", "");
	} while (MainRate.indexOf(",") >= 0)

	if (MaxSizeClientID == "") {
	    rate = (parseFloat(MainRate) / MaxSize)
	    rate = rate.toFixed(2);
	    $get(OutputClientID).innerHTML = "&#163;" + rate + " per " + DimensionText;
	} else {
	    rate = parseFloat(parseFloat(MainRate) / parseFloat($get(MaxSizeClientID).value));
	    rate = rate.toFixed(2);
	    var dimSelection = $get(DimensionClientID);
	    var index = dimSelection.selectedIndex
	    var dimText = dimSelection.options[index].text;
	    $get(OutputClientID).innerHTML = "&#163;" + rate + " per " + dimText;
	}

}

function setBackColour(cont, btnUndo) {

    $get(btnUndo).disabled = false;

    if (cont.style.backgroundColor == "#FFFFFF" || cont.style.backgroundColor == "" || cont.style.backgroundColor == "white") {
        cont.style.backgroundColor = "#FFFFBB";
    }

}

