function firstUp(txt) {
    return txt.substr(0,1).toUpperCase() + txt.substr(1);
}



//hotspots
//    if p,c,s hit a hotspot, then adjust hotspot dropdown
function ajustHotSpot() {
    var pid = document.getElementById('provinceId').value;
    var cid = document.getElementById('cityId').value;
    var sid = document.getElementById('sectorId').value;
    for (i = 0; i < hotspots.length; i++) {
        if ((hotspots[i].p == pid) && (hotspots[i].c == cid) && ((hotspots[i].s == sid) || (hotspots[i].s == ''))) {
            document.getElementById('hotspotId').value = hotspots[i].id;
            return;
        }
    }
    document.getElementById('hotspotId').value = '';
}

function alignToHotSpot() {
    var hid = document.getElementById('hotspotId').value;
    for (i = 0; i < hotspots.length; i++) {
        if (hotspots[i].id == hid) {
            var hotspot = hotspots[i];
            if (hotspot.p != '') {
                document.getElementById('provinceId').value = hotspot.p;
                setCityOptions();
                if (hotspot.c != '') {
                    document.getElementById('cityId').value = hotspot.c;
                    setSectorOptions();
                }
                if (hotspot.s != '') document.getElementById('sectorId').value = hotspot.s;
            }
            return;
        }
    }
}

// validation
function limitText(limitField, textId) {
    if (limitField.value.length > 1000) {
        limitField.value = limitField.value.substring(0, 1000);
    } else {
        document.getElementById(textId).innerHTML = 1000 - limitField.value.length;
    }
}
function isFieldEmpty(field) {
    var val = trim(field.value);
    switch(field.type) {
        case 'text':
        case 'textarea':
            return (val==null) || (val=='');
        break
        case 'select-one':
            return (val==null) || (val=='') || (val=='0');
        break
        default:
        return false;
    }
}
function messageText(msg, p0, p1, p2) {
    msg = msg.replace('{0}', p0);
    msg = msg.replace('{1}', p1);
    msg = msg.replace('{2}', p2);
    return msg;
}
function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}
function isEmail(field) {
    var val = trim(field.value);
    var emailRE = /^[A-Za-z0-9._%-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/
    if (!emailRE.test(val)) {
        return false;
    }
    return true;
}
function isPhone(field) {
    var val = trim(field.value);
    var phoneRE = /^((\+?\d{1,3}(-|\s)?\(?\d\)?(-|\s)?\d{1,5})|(\(?\d{2,6}\)?))(-|\s)?(\d{3,4})(-|\s)?(\d{4})((\sx|\sext)(-|\s)?\d{1,5}){0,1}$/
    if (!phoneRE.test(val)) {
        return false;
    }
    return true;
}
function validateField(field, required, validation, min, max) {
    var empty = isFieldEmpty(field);
    // check required
    if (empty && required) {
        switch(field.type) {
            case 'text':
            case 'textarea':
                alert(messageText(phrase['valMsg_pleaseEnter'], phrase[field.name]));
            break
            case 'select-one':
                alert(messageText(phrase['valMsg_pleaseChoose'], phrase[field.name]));
            break
            default:
        }
        field.focus();
        return false;
    }
    // validate data
    if (!empty) {
        switch(validation) {
            case 'email':
                if(!isEmail(field)) {
                    alert(messageText(phrase['valMsg_email'], phrase[field.name]));
                    field.select();
                    field.focus();
                    return false;
                }
            break
            case 'phone':
                if(!isPhone(field)) {
                    alert(messageText(phrase['valMsg_phone'], phrase[field.name]));
                    field.select();
                    field.focus();
                    return false;
                }
            break
            default:
        }
    }
    return true;
}

