// Get path
function GetPath(type)
{
    if (type == 'static')  return document.all["static_path"].value;
    if (type == 'dynamic') return document.all["dynamic_path"].value;
}

function trim(str) {
    return str.replace(/^\s*|\s*$/g, "");
}

function SubmitSearchForm(form, method, url)
{
    //if (arguments[3] == null) document.all[form].target = '_self';
    //else document.all[form].target = arguments[3];
    if (document.all["sort_order"].value == "") document.all["sort_order"].value = "asc"
    if (document.all["sort_order"].value == "asc") document.all["sort_order"].value = "desc"
    if (document.all["sort_order"].value == "desc") document.all["sort_order"].value = "asc"

//  if (order == "asc") order = "desc"
    //  if (order == "desc") order = "asc"

    document.all[form].method = method;
//  document.all[form].action = url + "&order=" + document.all["sort_order"].value;
    document.all[form].action = url + "&order=" + order;
    document.all[form].submit();
}


function SubmitItemForm(form, method, url) {

    document.all[form].target = window.opener.name;
    document.all[form].method = method;
    document.all[form].action = url;
    document.all[form].submit();
    window.close();

}

function toggleDisplay(name) {
    var control = document.all.item(name);
    var controlImg = document.all.item(name + "_img");
    if (control == null) {
        window.alert(name + " not found!");
        return;
    }
    if (control.style.display == "none") {
        control.style.display = "block";
        controlImg.src = GetPath("static") + "/images/open.bmp";
    } else {
        control.style.display = "none";
        controlImg.src = GetPath("static") + "/images/closed.bmp";
    }
}

function toggleDisplayText(name) {
    var control = document.all.item(name);
    if (control == null) {
        window.alert(name + " not found!");
        return;
    }
    if (control.innerText == "[ + ]") {
        control.innerText == "[ - ]";
    } else {
        control.innerText == "[ + ]";
    }
}


function CheckPercent(fName)
{
    var sValue = document.all[fName].value
    if (sValue.length == 0) return "";

    //  If the number is not a valid integer, Return an error

    if (isNaN(sValue))
    {
        alert("Not a valid number");
        document.all[fName].value = "";
    }
    else if (!((sValue >= 0) && (sValue <= 100)))
    {
        alert("Not a valid percent");
        document.all[fName].value = "";
    }
    else
        return sValue;
}

function setDepartmentValue(fId, fName) {
    window.opener.document.all['department_id'].value = fId;
    window.opener.document.all['department_name'].innerHTML = fName;
    window.close()
}
/*
function CheckValidation(input){
    var qtyValue = document.all[input];
    if (isNaN(qtyValue.value) || (qtyValue.value=="")){
        alert("Not a valid number");
        qtyValue.value = "";
        setFocus(input);
        return false;
    }

    window.close();
    return true;
}
*/
function addNewItem(EAUrl, SQUrl, LFUrl) {
    var partNo = document.getElementById("part_no1");
//    alert(partNo.value);
    if ((partNo.value == "part") || (partNo.value == "allied")) partNo = document.getElementById("part_no2");
    for (var i = 0; i < partNo.options.length; i++) {
        if (partNo.options(i).selected) var unitCd = partNo.options(i).getAttribute("unit");
    }
//    alert(partNo.value);
//    alert(unitCd);
    if (unitCd == "EA") {
        SubmitForm('data_entry', 'post', EAUrl);
    }
    else if (unitCd == "SQ") {
        SubmitIntoPopup('data_entry', 'post',
                SQUrl + '&part_no=' + partNo.value,
                'height=100,width=400,status=no,toolbar=no,menubar=no,scrollbars=no')
    }
    else if (unitCd == "LF") {
        SubmitIntoPopup('data_entry', 'post',
                LFUrl + '&part_no=' + partNo.value,
                'height=50,width=400,status=no,toolbar=no,menubar=no,scrollbars=no')
    }
}
function setPartDropDownVisible() {
    var partNo1 = document.getElementById("part_no1");
    var partNo2 = document.getElementById("part_no2");
    if ((partNo1.value == "part") || (partNo1.value == "allied")) {
        partNo2.style.visibility = "visible";
    } else {
        partNo2.style.visibility = "hidden";
    }
}
function enablePartDropDown(form, partUrl, alliedUrl) {
    try {
        var partNo1 = document.getElementById("part_no1");
//        alert(partNo1.value);
        if (partNo1.value == "part") {
            SubmitForm(form, "post", partUrl);
        } else if (partNo1.value == "allied") {
            SubmitForm(form, "post", alliedUrl);
        }else {
            try {
                setPartDropDownVisible();
            } catch(ex) {
            }
        }
    } catch(ex) {
        alert(ex);
    }
}
function CheckSQValidation() {
    var tmp = "";
//    alert("[" + trim(document.all["item_width"].value) + "]");

    document.all["item_width"].value = trim(document.all["item_width"].value);
    document.all["item_height"].value = trim(document.all["item_height"].value);

    if (isNaN(document.all["item_width"].value)) tmp = "item_width";
    if (isNaN(document.all["item_height"].value)) tmp = "item_height";

    if (tmp != "") {
        alert("Not a valid number");
        document.all[tmp].value = "";
        setFocus(tmp);
        return false;
    }

    window.close();
    return true;
}
function CheckLFValidation() {
    var tmp = "";

    document.all["item_length"].value = trim(document.all["item_length"].value);

    if (isNaN(trim(document.all["item_length"].value))) tmp = "item_length";

    if (tmp != "") {
        alert("Not a valid number");
        document.all[tmp].value = "";
        setFocus(tmp);
        return false;
    }

    window.close();
    return true;
}
