﻿//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Copyright GlobalWeb Ltd 2009
//Author: Peter Foran
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// utility script

// clear default value of input box
// params: (object to test)
function clearDefault(el) { if (el.defaultValue == el.value) el.value = "" }

// restore default value of input box
// params: (object to test)
function restoreDefault(el) { if (el.value == "") el.value = el.defaultValue; }

// toggles visibility of an element based on state
// params: (id of element, true/false state)

function move(x) {
    //alert(position+"/"+x);
    if ((position == 0 && x == 77) || (position == -693 && x == -77)) {
        x = 0;
    }
    position = position + x;
    document.getElementById("thumbT").style.marginLeft = position + "px";
}

function go() {
    document.location = "Gallery.htm";
}

function toggleVis(id, state) {

    var obj = document.getElementById(id);

    if (obj) {
        if (!obj.isDisabled) {
            if (obj.style) {
                if (state == true) { obj.style.display = 'block'; } else { obj.style.display = 'none'; }
            } else {
                if (state == true) { obj.display = 'block'; } else { obj.display = 'none'; }
            }
        }
    }
}

// sets the value of a specified field (if it exists)
// params: (id of element, value)
function setFieldVal(id, val) {

    var obj = document.getElementById(id);

    if (obj) { if (!obj.isDisabled) { obj.value = val; } }
}

// adds values from input and sends to output
// params: (id of elements involved, output element)
function addFieldVals(id1, id2, id3, output) {

    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj3 = document.getElementById(id3);
    var total = 0;

    if (obj1) { if (!obj1.isDisabled) { total += parseInt(obj1.value); } }
    if (obj2) { if (!obj2.isDisabled) { total += parseInt(obj2.value); } }
    if (obj3) { if (!obj3.isDisabled) { total += parseInt(obj3.value); } }

    setFieldVal(output, total);
}

// takes values from input, multiplies them by respective values and sends to output
// params: (id of elements involved, mulitplier values, output element)
function addFieldVals_multi(id1, id2, id3, multi1, multi2, multi3, output) {

    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj3 = document.getElementById(id3);
    var total = 0;

    if (obj1) { if (!obj1.isDisabled) { total += parseInt(obj1.value) * multi1; } }
    if (obj2) { if (!obj2.isDisabled) { total += parseInt(obj2.value) * multi2; } }
    if (obj3) { if (!obj3.isDisabled) { total += parseInt(obj3.value) * multi3; } }

    setFieldVal(output, total);
}

// adds values from input and sends to output (MONEY-FORMAT VERSION)
// params: (id of elements involved, output element)
function addMoneyVals(id1, id2, id3, output) {

    var obj1 = document.getElementById(id1);
    var obj2 = document.getElementById(id2);
    var obj3 = document.getElementById(id3);
    var total = 0;

    if (obj1) { if (!obj1.isDisabled) { total += parseFloat(obj1.value.replace(/,/, '')); } }
    if (obj2) { if (!obj2.isDisabled) { total += parseFloat(obj2.value.replace(/,/, '')); } }
    if (obj3) { if (!obj3.isDisabled) { total += parseFloat(obj3.value.replace(/,/, '')); } }

    setFieldVal(output, formatMoney(total, 2, '.', ','));
}

formatMoney = function(n, c, d, t) {
    var c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

// converts sq. feet to sq. metres and vice versa (also hectares to acres)
// params: (value, type of conversion, output)
function convertSize(val, type, output) {

    var input = val.value.replace(/,/, '');
    var total = 0;

    if (type == 'tosqfeet') { if (isValidNumber(input)) { total = (input / (0.3048 * 0.3048)); } }
    if (type == 'tosqmetre') { if (isValidNumber(input)) { total = (input * (0.3048 * 0.3048)); } }
    if (type == 'tohectares') { if (isValidNumber(input)) { total = (input * 0.40468564224); } }
    if (type == 'toacres') { if (isValidNumber(input)) { total = (input / 0.40468564224); } }

    setFieldVal(output, formatMoney(total, 2, '.', ',').slice(0, -3)); //output value with "thousands" separator
}

function isValidNumber(inpString) {
    return /^[-+]?\d+(\.\d+)?$/.test(inpString);
}

// disables the output control if the primary element's value is 0. Used by BER input.
// params: (id of elements involved, output element)
function checkBER(id, output) {

    var obj = document.getElementById(id);
    var objoutput = document.getElementById(output);

    if (obj && objoutput) {
        if (!obj.isDisabled && !objoutput.isDisabled) {
            if (obj.value == '0') { objoutput.disabled = true; objoutput.style.background = '#cccccc'; }
            else { objoutput.disabled = false; objoutput.style.background = '#ffffff'; }
        }
    }
}

// select (or deselect) all checkboxes based on status of a parent checkbox
// params: (input checkbox)
function checkAll(chk) {

    for (var i = 0; i < document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].type == 'checkbox') {

            if (chk.checked == true) { document.forms[0].elements[i].checked = true; }
            else { document.forms[0].elements[i].checked = false; }
        }
    }
}


