﻿/*
add for all finder pages
depends on /hardware/_common/library/templates/parts/_part_buttons_find.xslt
*/

function closeWindow() {
    window.close();
}

function goBack(id, from) {
    if (onPopup)
        window.location.href = String.Format(QuestionPageOnPopup, id) + "&backfrom=" + from;
    else
        window.location.href = String.Format(QuestionPage, id) + "&backfrom=" + from;
}

function goNext() {
    var choose = document.getElementsByName("Choose");
    var chose = document.getElementsByName("cbChoose");

    if (choose && choose.length > 0) {
        selectSingle(choose);
    }
    else if (chose && chose.length > 0) {
        selectMulticheckbox(chose);
    }
}

// private methods
function selectSingle(choose) {
    var qid = "";
    for (var i = 0; i < choose.length; i++) {
        if (choose[i].checked) {
            qid = choose[i].value;
            break;
        }
    }
    if (qid.length == 0) {
        alert("Please choose an option");
    }
    else if (qid.indexOf("showProducts") >= 0) {
        next(qid.substr(qid.indexOf("_") + 1), true);
    }
    else {
        next(qid, false);
    }
}
function selectMulticheckbox(choose) {
    var qid = "";
    for (var i = 0; i < choose.length; i++) {
        if (choose[i].checked) {
            var temp = choose[i].value.split('_');
            if (qid.length == 0) {
                qid = temp[1] + "_" + temp[2];
            }
            else {
                qid += "_" + temp[2];
            }
        }
    }
    if (qid.length == 0) {
        alert("Please choose an option");
    }
    else {
        next(qid, true);
    }
}
function next(qid, toFinder) {
    if (toFinder) {
        var url = String.Format(FinderPage, qid);
        if (onPopup) {
            window.opener.location.href = url;
            window.close();
        }
        else
            window.location.href = url;
    }
    else {
        if (onPopup)
            window.location.href = String.Format(QuestionPageOnPopup, qid);
        else
            window.location.href = String.Format(QuestionPage, qid);
    }
}
String.Format = function() {
    if (arguments.length == 0)
        return "";
    if (arguments.length == 1)
        return arguments[0];
    var reg = /{(\d+)?}/g;
    var args = arguments;
    var result = arguments[0].replace(reg, function($0, $1) {
        return args[parseInt($1) + 1];
    })
    return result;
}