/****************************************************************************************************
Script: nv_advSearch.js

Description: The script contains the function used in advSearch.php

Called By: advSearch.php

$Id: nv_boatReviewSearch.js,v 1.5 2010/08/02 07:13:50 arti Exp $
****************************************************************************************************/
//Function to change the subtype corresponding to boat type
function brSubTypeChange(v1,v2,curval)
{
    // sail boat type array for boat reivew
    var S=new Array();
    var M=new Array();
    var R=new Array();

    S[1]='Matkapursi';
    S[2]='Kilpapursi';
    S[3]='Monitoimivene';
    S[4]='Moottoripursi';
    S[5]='Retkipursi';
    S[6]='Kevytpursi';
    S[7]='Purjelauta';
    S[8]='matka/ kilpapursi';

    M[1]='Matkavene';
    M[2]='Urheiluvene';
    M[3]='Kalastusvene';
    M[4]='Retkivene(HT)';
    M[5]='Työvene';
    M[6]='Avovene';
    M[7]='Ohjauspulpetillinen avovene';

    R[1]='soutuvene';
    R[2]='jolla';
    R[3]='kajakki';
    R[4]='kanootti';
    R[5]='kumivene';
    R[6]='rib';
    R[7]='vesijetti';
    R[8]='puolivalmiste';
    R[9]='muu';

    var boatType=new Array(S,M,R);

    //holds the seleted boat type.
    var typeId='';

    if(v1.selectedIndex >= 0)
        var typeId=new String(v1.options[v1.selectedIndex].value);

    if(typeId=='S')
        typeId=S;
    else if(typeId=='M')
        typeId=M;
    else if(typeId=='R')
        typeId=R;

    curval=curval.replace(/'/g,"");
    var curvalArr=new Array();
    curvalArr=curval.split(",");

    //If the selected value is not passed in curval, then check
    //whether any subtype is selected and if yes, store it as curval.
    if(curval == '' && v2.selectedIndex > 0)
        curval=v2.options[v2.selectedIndex].value;

    var opt;
    v2.length=0;
    var j=0;

    opt=new Option(modelDefVal,'');
    eval("v2.options[j]=opt");
    j++;

    if(typeId != '') //If the boat type is selected
    {
        for(var k=0; k < curvalArr.length; k++)
        {
            for(var i=1; i < typeId.length; i++)
            {
                opt=new Option(typeId[i],i);
                eval("v2.options[i]=opt");

                if(typeId[i]==typeId[curvalArr[k]])
                {
                    v2.options[i].selected=true;
                }

                j++;
            }
        }
    }
    else    //If the boat type is not selectd(simple search and advacned search)
    {
        var idBoatType;
        var strIdSubType=new String('');

        //Loop through all of the boat types.
        for(var k=0; k < v1.length; k++)
        {
            var idBoatType=v1.options[k].value;

            if(idBoatType > 0)
            {
                for(var i=1;i < typeId[idBoatType].length; i++)
                {
                    //Check whether the subtype has been already added in the listbox as one subtype
                    // can belong to more than one boat type.
                    if(strIdSubType.search("'"+typeId[idBoatType][i]+"'") == -1)
                    {
                        opt=new Option(typeId[idBoatType][i],typeId[idBoatType][i]);
                        eval("v2.options[j]=opt");

                        if(curval.search("'"+typeId[idBoatType][i]+"'") != -1)
                            v2.options[j].selected=true;

                        strIdSubType=strIdSubType+"'"+typeId[idBoatType][i]+"',";
                        j++;
                    }
                }
            }
        }
    }
}

function checkBrFa()
{
    var z='';
    if(document.br_search.sub_type && document.br_search.sub_type.length > 0)
    {
        for(var a=0; a < document.br_search.sub_type.length; a++)
        {
            if(document.br_search.sub_type[a].selected && document.br_search.sub_type[a].value != '')
                z += "'"+document.br_search.sub_type[a].value+"',";
        }
        document.br_search.zSubType.value=z.slice(0,-1);
    }

    var z='';
    if(document.br_search.id_sel_acc && document.br_search.id_sel_acc.length > 0)
    {
        for(var a=0; a < document.br_search.id_sel_acc.length-1; a++)
        {
            if(document.br_search.id_sel_acc[a].value != '')
                z += document.br_search.id_sel_acc[a].value+",";
        }
        if(document.br_search.id_sel_acc[a].value != '')
            z += document.br_search.id_sel_acc[a].value;

        document.br_search.zAcc.value=z;
    }
}

//Function to add the selected option from one listbox into another listbox.
//Used for accessory and subtype.
function addSelectedOptBr(v1,v2)
{
    if(v1.options[v1.selectedIndex].value != '')
    {
        if(v2.length == 1 && v2.options[0].value == '')
            v2.length--;

        var tot=v2.length;
        var dup=0;

        for(o=0; o < tot; o++)
        {
            if(v2.options[o].value == v1.options[v1.selectedIndex].value)
            {
                dup=1;
                switch (v2.id)
                {
                    case 'id_sel_acc':
                        alert(msgAccDupSelected);
                        break;
                    default:
                        alert(msgDupSelected);
                        break;
                }
            }
        }
        if(dup != 1)
        {
            selOpt=new Option(v1.options[v1.selectedIndex].text,v1.options[v1.selectedIndex].value);
            eval('v2.options[tot]=selOpt');
        }
    }
}

function remSelectedOptBr(v1)
{
    if(!(v1.selectedIndex >= 0))
        return;

    var p=v1.selectedIndex;
    var tot=v1.length;

    if(tot > p)
    {
        for(var i=p+1; i < tot; i++)
        {
            v1.options[i-1].text=v1.options[i].text;
            v1.options[i-1].value=v1.options[i].value;
        }
    }
    v1.length=tot-1;

    // If no option is there then add "any" as default
    if(v1.length == 0)
    {
        selOpt=new Option('---------------- '+modelDefVal+' -----------------','');
        eval('v1.options[0]=selOpt');
    }
    v1.selectedIndex=-1;
}

function loadDef(v1)
{
    if(!v1.value)
    {
        if(v1.name=='boat_priceFrom' || v1.name=='motor_forceFrom')
        v1.value=lvalue;
        else
            v1.value=uvalue;
    }
}

/** This function call from 'resetForm' function to get the default value of given form field name
    brFormField : This contain the HTMLObject i.e. this.form.fielname
    defValArr : This contain the Global array that set for default value
    txt : optional if no any selected option then display the caption value like ------ Any ------
*/
function brGetDefValue(brFormField,txt)
{
    if(brFormField)
    {
        /** First Remove all the value of given form field name */
        brFormField.options.length=0;
        selOpt=new Option(txt,'');
        eval("brFormField.options[0]=selOpt");
    }
}


