function gotoURL(url) {
    if (url == 'GFA BLOG'){
        document.getElementById('redirect_form').submit();
    }
    else if(url == 'GFA LEAVE ONLINE'){
        window.open("http://leave.gfacorp.com:81/webperson/default1.aspx");
    }
    else{
        location.href = url;
    }    
}

function gotoURL_parent(url) {
    
	parent.document.location.href = url;
}

function autoFocus() {
    if (document.forms) {
        var found = false;
        for (var i=0; i<document.forms.length; ++i) {
            var elements = document.forms[i].elements;
            if (elements) {
                for (var j=0; j<elements.length; ++j) {
                    if (elements[j].type == 'text' ||
                        elements[j].type == 'textarea' ||
                        elements[j].type == 'password' ||
                        elements[j].type == 'button') {
                        if (elements[j].readOnly) {
                            continue;
                        }
                        else {
                            try {
                                elements[j].focus();
                                found = true;
                                break;
                            } catch (e) {
                            }
                        }
                    }
                }
                if (found)
                    break;
            }
        }
    }
}

function clearValue(ids) {
    if (ids) {
        for (var i=0; i<ids.length; ++i) {
            var control = document.getElementById(ids[i]);
            if (control) {
                if (control.type == "file" || control.type == "hidden" ||
                    control.type == "password" || control.type == "text" ||
                    control.type == "textarea") {
                    control.value = "";
                } else if (control.type == "checkbox" || control.type == "radio") {
                    control.checked = false;
                } else if (control.type == "select-one") {
                    if (control.options.length > 0)
                        control.selectedIndex = 0;
                } else if (control.type == "select-multiple") {
                    if (control.options.length > 0) {
                        var opts = control.options;
                        for (var j=0; j<opts.length; ++j) {
                            opts[j].selected = false;
                        }
                    }
                }
            }
        }
    }
}

function setControlEnable(id, enabled) {
    var control = document.getElementById(id);
    if (control) {        
        if (control.type == "file" || control.type == "checkbox" ||
            control.type == "password" || control.type == "text" ||
            control.type == "textarea" || control.type == "radio" ||
            control.type == "select-one" || control.type == "select-multiple") {
            control.disabled = !enabled;
        }
    }
}

function extractValue(id, isValue) {
    var control = document.getElementById(id);
    if (control) {
        if (control.type == "file" || control.type == "hidden" ||
            control.type == "password" || control.type == "text" ||
            control.type == "textarea") {
            return control.value;
        } else if (control.type == "checkbox" || control.type == "radio") {
            if (isValue)
                return control.value;
            else
                if (control.checked)
                    return true;
                else
                    return false;
        } else if (control.type == "select-one") {
            if (control.options.length > 0 && !control.disabled) {
                if (isValue)
                    return control.options[control.selectedIndex].value; 
                else
                    return control.options[control.selectedIndex].text; 
            } else {
                return null;
            }
        } else if (control.type == "select-multiple") {
            if (control.options.length > 0) {
                var opts = control.options;
                var arr = "[";
                for (var i=0; i<opts.length; ++i) {
                    if (isValue)
                        arr += "\"" + opts[i].value + "\"";
                    else
                        arr += "\"" + opts[i].text + "\"";
                    if (i < opts.length - 1) {
                        arr += ", ";
                    }
                }
                arr = "]";
                return arr;
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}

function isValidControls(ids) {
    if (ids) {
        for (var i=0; i<ids.length; ++i) {
            var control = document.getElementById(ids[i]);
            if (!control) {
                return false;
            }
        }
        return true;
    }
    return false;
}

function nullValue(val) {
    if (val == null)
        return "";
    else
        return val;
}

function showValues(ids, vals) {
    if (ids && vals && ids.length == vals.length) {
        for (var i=0; i<ids.length; ++i) {
            var control = document.getElementById(ids[i]);
            if (control) {
                if (control.type == "file" || control.type == "hidden" ||
                    control.type == "password" || control.type == "text" ||
                    control.type == "textarea") {
                    control.value = nullValue(vals[i]);
                } else if (control.type == "checkbox" || control.type == "radio") {
                    control.checked = (vals[i] == "true" || vals[i] == true);
                } else if (control.type == "select-one") {
                    if (control.options.length > 0) {
                        var opts = control.options;
                        for (var j=0; j<opts.length; ++j) {
                            if (opts[j].value == nullValue(vals[i]))
                                opts[j].selected = true;
                            else
                                opts[j].selected = false;
                        }
                    }
                } else if (control.type == "select-multiple") {
                    if (control.options.length > 0) {
                        var opts = control.options;
                        var arr = vals[i];
                        var found = false;
                        for (var j=0; j<opts.length; ++j) {
                            for (var k=0; k<arr.length; ++k) {
                                if (opts[j].value == arr[k]) {
                                    found = true;
                                    break;    
                                }
                            }
                            opts[j].selected = found;
                        }
                    }
                }
            }
        }
    }
}

function submitFormWithConfirm(frmName, action, confirmMessage) {
    var frm = document.getElementById(frmName);
    frm.action = action;
    if (confirmMessage && confirmMessage.length > 0) {
        if (confirm(confirmMessage)) {
            frm.submit();
        }
    } else {
        frm.submit();
    }
}

function submitFormWithConfirmAndSelectedItem(frmName, action, confirmMessage, chkPrefix, errorMessage) {
    var frm = document.getElementById(frmName);
    var hasChecked = false;
    if (frm) {
        var elms = frm.elements;
        for (var i=0; i<elms.length; ++i) {
            if (elms[i].type == "checkbox" && 
                elms[i].name.indexOf(chkPrefix) == 0) {
                if (elms[i].checked) {
                    hasChecked = true;
                    break;
                }
            }
        }
        if (hasChecked)
            submitForm(frmName, action, confirmMessage);
        else
            alert(errorMessage);
    }
}

function submitForm(frm, action) {
    frm.action = action;
    frm.submit();    
}

