/* $Id: nv_boatReviewSearch.js,v 1.16 2012/01/03 06:15:05 bakul 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';
    R[10]='ilmatyynyalus';

    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;

    if(jQuery('select[name=boat_type]').val() == '')
        return false;

    opt=new Option(capSubtype,'');
    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 addSubType(v1,v2)
{
    var dup = 0;
    var addSubtype=true;
    var valDropDown = '';
    var boatTypeVal = jQuery('select[name=boat_type] option:selected').val();

    if ((v1.sub_type.selectedIndex < 0))
        return;

        var tmpValue = v1.sub_type[v1.sub_type.selectedIndex].value;
        jQuery('div[id^=id_sel_subtype_]').each(function() {
            var getBoatTypeSubTypeArr= jQuery(this).attr('id').split('id_sel_subtype_');
            var tmpValue2 = getBoatTypeSubTypeArr[1];

            if(tmpValue2 == '')
            {
                addSubtype=false;
                return false;
            }
            if(tmpValue == tmpValue2)
            {
                addSubtype=false;
                return false;
            }


            if(jQuery('select[name=sub_type] option:selected').val() == '')
            {
                jQuery('div[id^=id_sel_subtype_]').remove();
            }

        });
        if (addSubtype)
        {
            str1 = jQuery('select[name=boat_type] option:selected').text();
            if ((v1.sub_type[v1.sub_type.selectedIndex].value).match(/^([0-9]{1,})/i))
            {
                //valDropDown = str1+', '+v1.sub_type[v1.sub_type.selectedIndex].text+' '+'<span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_subtype_close_'+boatTypeVal+'_'+v1.sub_type[v1.sub_type.selectedIndex].value+'" onclick="javascript:removeBrDiv(this.id,\'subtype\');"></span>';
                valDropDown = v1.sub_type[v1.sub_type.selectedIndex].text+' '+'<span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_subtype_close_'+v1.sub_type[v1.sub_type.selectedIndex].value+'" onclick="javascript:removeBrDiv(this.id,\'subtype\');"></span>';
            }
            else
            {
                //valDropDown = str1 +' : '+ anySubtype + ' <span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_subtype_close_'+boatTypeVal+'_'+v1.sub_type[v1.sub_type.selectedIndex].value+'" onclick="javascript:removeBrDiv(this.id,\'subtype\');"></span>';
                valDropDown = modelDefVal + ' <span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_subtype_close_'+v1.sub_type[v1.sub_type.selectedIndex].value+'" onclick="javascript:removeBrDiv(this.id,\'subtype\');"></span>';
            }
            jQuery('#id_sel_subtype').append('<div id="id_sel_subtype_'+v1.sub_type[v1.sub_type.selectedIndex].value+'">'+valDropDown+"</div>");
            callAjax(document.br_search);
        }
}

function addSelMake(frm)
{
    var addMake = true;

    var makeVal = jQuery('#id_make option:selected').val();
    var makeTxt = jQuery('#id_make option:selected').text();

    jQuery('div[id^=id_sel_make_]').each(function() {
        var getMakeArr= jQuery(this).attr('id').split('id_sel_make_');

        if(makeVal == getMakeArr[1])
        {
            addMake=false;
            return false;
        }
    });

    if(addMake)
    {
        if(makeVal != '')
        {
            valDropDown = makeTxt + ' <span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_make_close_'+makeVal+'" onclick="javascript:removeBrDiv(this.id,\'make\');"></span>';
            jQuery('#id_sel_make').append('<div id="id_sel_make_'+makeVal+'">'+valDropDown+"</div>");
        }
        else
        {
            jQuery('div[id^=id_sel_make_]').remove();
        }
        callAjax(document.br_search);
    }
}

function addSelMotorModel(frm)
{
    var addMotor = true;

    var motorVal = jQuery('#id_motor_model option:selected').val();
    var motorTxt = jQuery('#id_motor_model option:selected').text();

    jQuery('div[id^=id_sel_motor_]').each(function() {
        var getMotorArr= jQuery(this).attr('id').split('id_sel_motor_');

        if(motorVal == getMotorArr[1])
        {
            addMotor=false;
            return false;
        }

    });

    if(addMotor)
    {
        if(motorVal != '')
        {
            valDropDown = motorTxt + ' <span title="'+capResetSearch+'" alt="'+capResetSearch+'" class="grey_reset" id="id_sel_motor_close_'+motorVal+'" onclick="javascript:removeBrDiv(this.id,\'motor\');"></span>';
            jQuery('#id_sel_motor').append('<div id="id_sel_motor_'+motorVal+'">'+valDropDown+"</div>");
        }
        else
        {
            jQuery('div[id^=id_sel_motor_]').remove();
        }
        callAjax(document.br_search);
    }
}
function removeBrDiv(id,section)
{
   removeEntireDiv(id,section);
   callAjax(document.br_search);
}
function removeEntireDiv(id,val)
{
    getNwMdlArr = id.split('id_sel_'+val+'_close_');
    jQuery('#'+id).remove();
    if(/\s/g.test(getNwMdlArr[1]))
        jQuery('div[id*=id_sel_'+val+'_'+getNwMdlArr[1]+']').remove();
    else
        jQuery('div[id=id_sel_'+val+'_'+getNwMdlArr[1]+']').remove();
}
function checkBrFa()
{
    var z='';
    if(jQuery('#id_sel_subtype').length > 0)
    {
        jQuery('div[id^=id_sel_subtype_]').each( function() {
            var getSubTypeArr = jQuery(this).attr('id').split('id_sel_subtype_');
            if(getSubTypeArr[1]>0)
                z += getSubTypeArr[1]+',';
        });
        z = z.substring(0,z.length-1);
        document.br_search.zSubType.value = z;
    }

    var z='';
    if(jQuery('#id_sel_make').length > 0)
    {
        jQuery('div[id^=id_sel_make_]').each( function() {
            var getMakeArr = jQuery(this).attr('id').split('id_sel_make_');
             z += getMakeArr[1]+',';
        });
        z = z.substring(0,z.length-1);
        document.br_search.zMake.value = z;
    }

    var z='';
    if(jQuery('#id_sel_make').length > 0)
    {
        jQuery('div[id^=id_sel_make_]').each( function() {
            var getMakeArr = jQuery(this).attr('id').split('id_sel_make_');
            if(getMakeArr[1] != '' && getMakeArr[1] != 'undefined')
                z += getMakeArr[1]+',';
        });
        z = z.substring(0,z.length-1);
        document.br_search.zMake.value = z;
    }

    var z='';
    if(jQuery('#id_sel_motor').length > 0)
    {
        jQuery('div[id^=id_sel_motor_]').each( function() {
            var getMotorArr = jQuery(this).attr('id').split('id_sel_motor_');
            if(getMotorArr[1] != '' && getMotorArr[1] != 'undefined')
             z += getMotorArr[1]+',';
        });
        z = z.substring(0,z.length-1);
        document.br_search.zMotor.value = z;
    }

    var z='';
    jQuery('input[name^=chkAcc]:checked').each(function(){
        z += this.value+",";
    });
    z = z.substring(0,z.length-1);
    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 loadDefBr(v1)
{
    if(!v1.value)
    {
        if(v1.name=='boat_priceFrom')
        {
            v1.value=lvalue;
            v1.style.color = '#666666';
        }
        else if(v1.name=='boat_priceTo')
        {
            v1.value=uvalue;
            v1.style.color = '#666666';
        }
        else if(v1.name=='boat_model')
        {
            v1.value=ffSrchModel;
            v1.style.color = '#666666';
        }
        else if(v1.name=='srch')
        {
            v1.value=srchDealerNm;
            v1.style.color = '#666666';
        }
    }
}

function loadDefBrReset(v1)
{
    if(v1.name=='boat_priceFrom')
    {
        v1.value=lvalue;
        v1.style.color = '#666666';
    }
    else if(v1.name=='boat_priceTo')
    {
        v1.value=uvalue;
        v1.style.color = '#666666';
    }
    else if(v1.name=='boat_model')
    {
        v1.value=ffSrchModel;
        v1.style.color = '#666666';
    }
    else if(v1.name=='srch')
    {
        v1.value=srchDealerNm;
        v1.style.color = '#666666';
    }
}

/** 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");
    }
}

/** Reset the all form field when reset button click */
function resetBrForm(brForm)
{
    brForm.reset();

    jQuery('div[id^=id_sel_make_]').remove();
    jQuery('div[id^=id_sel_motor_]').remove();
    jQuery('div[id^=id_sel_subtype_]').remove();

    if(V_AD_TYPE == 'B' || V_AD_TYPE == '')
    {
        brSubTypeChange('',document.br_search.sub_type,document.br_search.zSubType.value);
        brGetDefValue(document.br_search.id_sel_acc,"------------- "+modelDefVal+" -------------");
    }

    callAjax(document.br_search); // This is used for default counter when page load
    jQuery('input[type=text]').each(function(){
        loadDefBrReset(this);
    });
    return true;
}

function changeBRYear(val)
{
    if(jQuery('input[name=br_ad_type]').val() == 'B' || jQuery('input[name=br_ad_type]').val() == '' )
        callAjax(document.br_search);
    else
    {
        if(getQuerystring('pub_year') == '')
            window.location.href = document.location.href+'?pub_year='+val;
        else
            window.location.href = replaceQueryString(document.location.href,'pub_year',val)
    }
}

function showHideBrEle(adType)
{
    if(adType == 'B')
    {
        jQuery('#id_boat_type_src').show();
        jQuery('#id_sub_type_src').show();
        jQuery('#id_make_src').show();
        jQuery('#id_price_src').show();
        jQuery('#id_engine_src').show();
        jQuery('#id_length_src').show();

        jQuery('#id_acc_group_src').hide();
        jQuery('#id_service_group_src').hide();
        jQuery('#id_company_name_src').hide();
    }
    else if(adType == 'C')
    {
        jQuery('#id_acc_group_src').show();

        jQuery('#id_boat_type_src').hide();
        jQuery('#id_sub_type_src').hide();
        jQuery('#id_make_src').hide();
        jQuery('#id_price_src').hide();
        jQuery('#id_engine_src').hide();
        jQuery('#id_length_src').hide();
        jQuery('#id_service_group_src').hide();
        jQuery('#id_company_name_src').hide();
    }
    else if(adType == 'S')
    {
        jQuery('#id_service_group_src').show();

        jQuery('#id_boat_type_src').hide();
        jQuery('#id_sub_type_src').hide();
        jQuery('#id_make_src').hide();
        jQuery('#id_price_src').hide();
        jQuery('#id_engine_src').hide();
        jQuery('#id_length_src').hide();
        jQuery('#id_acc_group_src').hide();
        jQuery('#id_company_name_src').hide();
    }
    else if(adType == 'A')
    {
        jQuery('#id_company_name_src').show();

        jQuery('#id_boat_type_src').hide();
        jQuery('#id_sub_type_src').hide();
        jQuery('#id_make_src').hide();
        jQuery('#id_price_src').hide();
        jQuery('#id_engine_src').hide();
        jQuery('#id_length_src').hide();
        jQuery('#id_service_group_src').hide();
        jQuery('#id_acc_group_src').hide();
    }
}

function cleanBrUrl(qs)
{
    var boatType = qs.elements.boat_type_src.options[qs.elements.boat_type_src.selectedIndex];
    var mk = qs.elements.make_src.options[qs.elements.make_src.selectedIndex];

    if (boatType.value && boatType.value != '0')
        document.new_boat_srch.zBoatType_src.value = boatType.value;
    if (mk.value && mk.value != '0')
        document.new_boat_srch.zMake_src.value = mk.value;

    var u = _su + '/uudet-veneet?';
    var l = qs.elements.length;

    u = u.replace(/\s/g, '-').toLowerCase();
    boatType.value = mk.value = '';
    for ( var i = 0; i < l; i++)
    {
        var e = qs.elements[i];
        if(e.value && e.value != '0' && (e.type == 'hidden' || e.type == 'select-one' || e.name == 'qs_search'))
            u = u + e.name + '=' + e.value + '&';
    }
    u = u.replace(/[()]+/g,'');
    window.location=u.substr(0, u.length - 1);
    return false;
}

function ajaxBrChange(frmObj, chgfor)
{
    var pars;

    pars = jQuery(frmObj).serialize();

    pars += '&ajxOpt='+chgfor;
    pars=pars.replace(/([a-zA-Z_]+=&)*/gi,'');

    jQuery.post(_su+'/ajaxChangeBrEntity.php',pars,function(data){
        arr = data.split("#@#@#");

        if(arr[0])
            jQuery('#boat_type_src').html(arr[0]);
        if(arr[1])
            jQuery('#sub_type_src').html(arr[1]);
        if(arr[2])
            jQuery('#make_src').html(arr[2]);

        callAjax(frmObj);
    });

    return false;
}
