function formatdata(sarrayname, odataarray, ocaptionarray) {
    var lind1;
    var lind2;
    var oarray;


    for (lind1 = 0; lind1 <= odataarray.length - 1; lind1++) {
        adddata(sarrayname, ocaptionarray, odataarray[lind1].data)
    }
}
function seldata(sdata, sprice, surl, sku, varimage, sold_out, stock_text) {
    this.data = sdata
    this.price = sprice
    this.url = surl
    this.sku = sku
    this.varimage = varimage
    this.sold_out = sold_out
    this.stock_text = stock_text

}

function showattr(sform, sdataarray, sarrayname, scaptionarray, lcurselect, lselectcount, lmaxselect, acurrsel, linstanceindex) {
    var svalue;
    var sspace = '';
    var stxtprice = '';
    var ostock_text = new Object();
    copycurrsel(sform, lcurselect, lmaxselect, acurrsel, linstanceindex)
    setdefaultvalues(sdataarray, lselectcount, acurrsel, linstanceindex)
    selchange(sform, sarrayname, scaptionarray, lcurselect, lmaxselect, linstanceindex)
    restoreselection(sform, sarrayname, scaptionarray, lcurselect, lmaxselect, acurrsel, linstanceindex)
    svalue = addtobasket_getprice(1, sform, sdataarray, lselectcount, linstanceindex, ostock_text)


    svalue = svalue.toString();

    //stxtprice = "document.forms['" + sform + "'].txtprice" + linstanceindex + ".value = sspace + svalue"
    //eval(stxtprice)
    changedivtext('txtprice' + linstanceindex, svalue, sform)
    //changedivtext('divstock' + linstanceindex, ostock_text.value, sform)


    //if (eval("sparentguid" + linstanceindex) != '')
    //    calculate_total()
}

function adddata(sarrayname, ocaptionarray, vdata) {
    var lind1;
    var lind2;

    var otemp;
    var sarray = sarrayname;
    var bfound;
    var onode;
    var bnull;

    for (lind1 = 0; lind1 <= vdata.length - 1; lind1++) {
        bfound = false;
        bnull = false;
        stemp = sarray + '.children'
        otemp = eval(stemp)
        if (vdata[lind1] == null) { bnull = true; continue; }

        for (lind2 = 0; lind2 <= otemp.length - 1; lind2++) {
            if (vdata[lind1] == otemp[lind2].value) {
                sarray = sarray + ".children[" + lind2 + "]"
                bfound = true;
                break;
            }

        }
        if (!bfound && !bnull) {
            onode = new node(vdata[lind1], vdata[lind1], ocaptionarray[lind1])
            stemp = sarray // + '.children'
            otemp = eval(stemp)
            otemp.children[otemp.children.length] = onode
            sarray = sarray + ".children[" + (otemp.children.length - 1).toString() + "]"
        }
        else {
            //			alert('found')
        }

    }
}

function node(stext, svalue, schildlabel) {
    this.text = stext;
    this.value = svalue;
    this.childlabel = schildlabel;
    this.children = new Array()
}
function selchange(sform, sarrayname, scaptionarray, sselect, lmaxselect, linstanceindex) {
    var ocurr2;
    ocurrent = sselect;
    var scurobject = sarrayname;
    var scurobject2 = '';
    var osel;
    var oarrobj;
    var lselindex;
    var sdocform;

    var ocaptionarray = eval(scaptionarray);

    sdocform = "document.forms['" + sform + "']"
    for (index = 0; index <= sselect; index++) {
        ssel = sdocform + "." + "sel_" + linstanceindex + index;
        osel = eval(ssel)
        svalue = osel.options[osel.selectedIndex].value
        scurobject2 = scurobject + '.children'
        oarrobj = eval(scurobject2);
        for (lindex2 = 0; lindex2 <= oarrobj.length - 1; lindex2++) {
            if (svalue == oarrobj[lindex2].value) {
                scurobject = scurobject + '.children[' + lindex2 + ']'
                break;
            }
        }
    }
    lselindex = sselect;
    for (index = lselindex + 1; index <= lmaxselect - 1; index++) {
        ssel = sdocform + "." + "sel_" + linstanceindex + index;
        osel = eval(ssel);
        while (osel.options.length > 0)
            osel.options[osel.options.length - 1] = null;

        //eval(sdocform + "." + "txtsel_" + linstanceindex + index + ".value=''");
        changedivtext('txtsel_' + linstanceindex + index, '', sdocform)
    }
    while (true) {
        oarrobj = eval(scurobject + '.children')
        if (oarrobj.length <= 0) break;
        lselindex = lselindex + 1;
        ssel = sdocform + "." + "sel_" + linstanceindex + lselindex;
        osel = eval(ssel);
        for (index = 0; index <= oarrobj.length - 1; index++) {
            var oOption = new Option(oarrobj[index].text, oarrobj[index].value, false, false)
            osel.options.length = osel.options.length + 1
            osel.options[osel.options.length - 1] = oOption
        }
        if (typeof osel == 'object') {
            osel.selectedIndex = 0
            //eval(sdocform + "." + "txtsel_" + linstanceindex + lselindex + ".value='" + ocaptionarray[lselindex] + "'");
            changedivtext('txtsel_' + linstanceindex + lselindex, ocaptionarray[lselindex], sdocform)
        }
        scurobject = scurobject + '.children[0]'
    }
}


function setdefaultvalues(sdataarray, lselectcount, acurrsel, linstanceindex) {
    var odataarray = eval(sdataarray);

    adefskuind = eval("ldefskuind" + linstanceindex)
    if (adefskuind[0] >= 0 && adefskuind[0] <= odataarray.length - 1) {
        adata = odataarray[adefskuind[0]].data
        for (lindex2 = 0; lindex2 <= lselectcount; lindex2++)
            acurrsel[lindex2] = adata[lindex2];
        adefskuind[0] = -1;
    }
}


function copycurrsel(sform, lselindex, lmaxselect, acurrsel, linstanceindex) {
    sdocform = "document.forms['" + sform + "']"

    for (i = 0; i <= lmaxselect - 1; i++)
        acurrsel[i] = '';

    for (i = lselindex + 1; i <= lmaxselect - 1; i++) {
        ssel = sdocform + "." + "sel_" + linstanceindex + i;
        osel = eval(ssel)
        if (osel.selectedIndex == -1) break;
        svalue = osel.options[osel.selectedIndex].value
        acurrsel[i] = svalue
    }
}


function restoreselection(sform, sarrayname, scaptionarray, lselindex, lmaxselect, acurrsel, linstanceindex) {
    var bfound = false;

    sdocform = "document.forms['" + sform + "']"
    for (i = lselindex + 1; i <= lmaxselect - 1; i++) {
        ssel = sdocform + "." + "sel_" + linstanceindex + i;
        osel = eval(ssel)
        if (osel.selectedIndex == -1) break;
        bfound = false;
        for (j = 0; j <= osel.options.length - 1; j++) {
            if (osel.options[j].value == acurrsel[i]) {
                osel.selectedIndex = j;
                selchange(sform, sarrayname, scaptionarray, i, lmaxselect, linstanceindex)
                bfound = true;
                break;
            }
        }
        if (!bfound) break;
    }
}


function showcolor(sform, lselectcount, lmaxselect, linstanceindex, lcolorindex, scolor) {
    sdocform = "document.forms['" + sform + "']"
    sdataarray = 'vdata' + linstanceindex
    sarrayname = 'amain' + linstanceindex
    scaptionarray = 'vcaption' + linstanceindex
    acurrsel = 'acurrsel' + linstanceindex
    lcolorindex = lcolorindex - 1
    lcurselect = lcolorindex

    ssel = sdocform + "." + "sel_" + linstanceindex + lcolorindex;
    osel = eval(ssel)
    if (osel.selectedIndex == -1) return;
    bfound = false;
    for (j = 0; j <= osel.options.length - 1; j++) {
        if (osel.options[j].value.toUpperCase() == scolor.toUpperCase()) {
            osel.selectedIndex = j;
            showattr(sform, sdataarray, sarrayname, scaptionarray, lcurselect, lselectcount, lmaxselect, acurrsel, linstanceindex)
            bfound = true;
            break;
        }
    }
    if (!bfound) alert('This option is not available')
}


function addtobasket_getprice(smode, sform, sdataarray, lselectcount, linstanceindex, ostock_text) {
    var index;
    var osel;
    var svalue;
    var lselindex;
    var sdocform;
    var odocform;
    var selindex;
    var odataarray = eval(sdataarray);
    var surl;
    var sguid;
    var spf_id;

    sdocform = "document.forms['" + sform + "']"
    odocform = eval(sdocform)
    sguid = eval("sparentguid" + linstanceindex)
    spf_id = eval("spf_id" + linstanceindex)

    var i = 0;
    for (index = 0; index <= odataarray.length - 1; index++) {
        i = i + 1;
        bfound = false
        selindex = 0
        for (lindex2 = 0; lindex2 <= lselectcount; lindex2++) {
            adata = odataarray[index].data

            if (adata[lindex2] == null) continue;

            ssel = sdocform + "." + "sel_" + linstanceindex + selindex;
            osel = eval(ssel)

            //The following line is commented coz, in some situations, the value in the first option may be empty,
            //so we still loop thru the rest of the options to find the value
            //if (osel.options[osel.selectedIndex].text=='') break;

            svalue = osel.options[osel.selectedIndex].value

            if (adata[lindex2] == svalue) {
                bfound = true
            }
            else {
                bfound = false
                break;
            }
            selindex = selindex + 1
        }
        if (bfound) {
            if (smode == 1) {
                sskuctl = "skuctl_" + linstanceindex + ".value='" + sguid + "|" + spf_id + "|" + odataarray[index].sku + "|" + eval("isoptionalsku" + linstanceindex) + "'"
                eval(sskuctl)
                //if (eval('supsell' + linstanceindex) !=null && eval('supsell' + linstanceindex) == '') {
                    //if (odataarray[index].varimage != '') showvarimage('productImage', odataarray[index].varimage)
                    //}
                /*
                if (odataarray[index].sold_out == 1)
                { oimg = getMyHTMLElement('img_add_to_basket' + linstanceindex); oimg.src = '/images/buttons/sold_out.gif' }
                else
                { oimg = getMyHTMLElement('img_add_to_basket' + linstanceindex); oimg.src = '/images/buttons/addtobasket.gif' }
                */
                //ostock_text.value = odataarray[index].stock_text
/*
                if (typeof sJSProdURL == 'undefined') {
                    large_image_file_name = eval('default_sku_image' + linstanceindex)
                    large_image_file_name = replaceString(large_image_file_name.toLowerCase(), '_l.', '_xl.')
                } else {
                    large_image_file_name = odataarray[index].varimage
                    large_image_file_name = replaceString(large_image_file_name.toLowerCase(), '_l.', '_xl.')
                }
*/


                return (odataarray[index].price)
            }
            else if (smode == 2) {
                sqty = 1
                surl = eval("sddurl_data" + linstanceindex) + eval("sddurl_data_suffix" + linstanceindex)
                document.forms[sform].action = surl + 'sku=' + odataarray[index].url + '&quantity=' + sqty
                if (odataarray[index].sold_out == 1) { alert(eval('sold_out_msg' + linstanceindex)); break; }
                document.forms[sform].submit();
                break;
            }
            else if (smode == 3) {
                sqty = 1
                surl = eval("sddwishlisturl_data" + linstanceindex) + eval("sddurl_data_suffix" + linstanceindex)
                document.forms[sform].action = surl + 'sku=' + odataarray[index].url + '&quantity=' + sqty
                if (odataarray[index].sold_out == 1) { alert(eval('sold_out_msg' + linstanceindex)); break; }
                document.forms[sform].submit();
                break;
            }
        }
    }
}

function displaycaption_noskues(sform, lmaxselect, linstanceindex) {
    sdocform = "document.forms['" + sform + "']"
    for (index = 0; index <= lmaxselect - 1; index++) {
        //eval(sdocform + "." + "txtsel_" + linstanceindex + index + ".value='" + vcaption[index] + "'");
        changedivtext('txtsel_' + linstanceindex + index, vcaption[index], sdocform)
    }
}
function changedivtext(id, text, NS_form) {
    var s = '';
    if (document.all) {
        document.all(id).innerHTML = text;
    }
    else if (document.getElementById) {
        document.getElementById(id).innerHTML = text;
    }
    else if (document.layers) {
        s = "document.forms['" + NS_form + "']." + id + ".value = '" + text + "'"
        eval(s)
    }
}

function showvarimage(sid, simage) { oimg = getMyHTMLElement(sid); oimg.src = simage }
