/** Define global variable for those field they generated using javascript, used for manipulation */
var mkModel=new Array();
var subType=new Array();
var accA=new Array();
var selTown=new Array();
var milgeTo=yrTo=engTo='';


/** This function call from 'setDefReset' function to set the default value of given form field name
    var advFormField : this contain the HTMLobject i.e. document.formName.fieldname
*/
function setDefValue(advFormField)
{
    if(advFormField && advFormField.length > 0)
    {
        var defValArr=new Array();

        for(var i=0; i<advFormField.length; i++)
        {
            /** Assign value and text name seperated with ,to defValArr array of given field name */
            if(advFormField[i].value != "")
                defValArr[i]=advFormField[i].value+","+advFormField[i].text;
        }
        return defValArr;
    }
}
/** This function call from 'resetForm' function to get the default value of given form field name
    advFormField : 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 getDefValue(advFormField,defValArr,txt)
{
    if(advFormField)
    {
        /** First Remove all the value of given form field name */
        advFormField.options.length=0;
        selOpt=new Option(txt,'');
        eval("advFormField.options[0]=selOpt");

        if(defValArr.length > 0)
        {
            /** Set/add the default value of given form filed from defArray array */
            for(var i=0; i<defValArr.length; i++)
            {
                /** Split array split_defValArr[0] have value and split_defValArr[1] have text */
                split_defValArr=defValArr[i].split(/\,/);
                selOpt=new Option(split_defValArr[1],split_defValArr[0]);
                eval("advFormField.options[i]=selOpt");
            }
        }
    }
}
/** when page is loaded store default value of those field they generated using javaScript,
    this field value is not reset using reset function of javascript so, reset manually.
*/
function setDefReset(advForm)
{
    /** Set values for 'No upper limit' comboboxes */
    if(advForm.mileageTo.selectedIndex > 0)
        milgeTo=advForm.mileageTo.value;

    if(advForm.yearTo.selectedIndex > 0)
        yrTo=advForm.yearTo.value;

    if(advForm.engineTo.selectedIndex > 0)
        engTo=advForm.engineTo.value;

    /** Set for selected filed area  */
    if(advForm.id_sel_model.length > 0)
        mkModel=setDefValue(advForm.id_sel_model);

    if(advForm.id_sel_acc.length > 0)
        accA=setDefValue(advForm.id_sel_acc);

	if(advForm.id_sel_town.length > 0)
		selTown=setDefValue(advForm.id_sel_town);
}
//Function is to used not to allow the user to uncheck all of the checkboxes in group.
function updateChkGrp(chkGrp)
{
    var totEle=chkGrp.form.elements.length;

    //if the checkbox is unchecked.
    if(!chkGrp.checked)
    {
        for(var i=0;i < totEle;i++)
        {
            if(chkGrp.form.elements[i].name == chkGrp.name && chkGrp.form.elements[i].checked)
            {
            	callAjax(document.adv); // This is used for counter when price status is checked/unchecked
            	return;
            }
        }
        chkGrp.checked=true;
    }
    callAjax(document.adv); // This is used for counter when price status is checked/unchecked
}
//Function to select default option automatically when all of the other checkboxes are unchecked.
function changeChkOpt(chkGrp)
{
    var totEle=document.adv.elements.length;
    var chgFrmEle=0;

    //if the checkbox is unchecked.
    if(!chkGrp.checked)
    {
        //this variable holds the index of the "Any/default" in the form.
        var j=0;

        for(var i=0;i < totEle;i++)
        {
            if(chkGrp.form.elements[i].name == chkGrp.name && chkGrp.form.elements[i].checked)
                return;

            //Storet the index number of the default checkbox in the form.
            if(chkGrp.form.elements[i].name == chkGrp.name && !chgFrmEle)
            {
                j=i;
                chgFrmEle=1;
            }
        }
        //Display the default checkbox as selected, as user has not selected in checkbox other than default.
        chkGrp.form.elements[j].checked=true;
        return;
    }

    //if the 'Any' checkbox is selected, then uncheck other checkboxes with the same name.
    if(chkGrp.value == '' || chkGrp.value == 'on')
    {
        for(var i=0;i < totEle;i++)
        {
            if(chkGrp.form.elements[i].name == chkGrp.name && chkGrp.form.elements[i].value)
            {
                chkGrp.form.elements[i].checked=false;
            }
        }
    }
    else //if chekckbox other than 'Any' is selected, uncheck 'Any' checkbox.
    {
        for(var i=0;i < totEle;i++)
        {
            if(chkGrp.form.elements[i].name == chkGrp.name && (chkGrp.form.elements[i].value == '' || chkGrp.form.elements[i].value == 'on'))
                chkGrp.form.elements[i].checked=false;
        }
    }
}
function chkMandOutOfTwo(chk,chkOther)
{
    //if another checkbox is not selected/checked, keep the first one as selected regardless of
    //whether user has checked or unchecked.
    if(!chkOther.checked)
        chk.checked=true;

    return;
}
function blankFa()
{
    if(!checkCity(document.adv.findAgent.value))
    {
        alert(msgInvalidFa);
        document.adv.findAgent.focus();
        return false;
    }
}
function checkFa()
{
    var p='';
    var z='';
	var t='';

    if(document.adv.motMonth.selectedIndex > 0 && document.adv.motYear.selectedIndex <= 0)
    {
        alert(msgMotYearErr);
        document.adv.motYear.selectedIndex=0;
        document.adv.motYear.focus();
        return false;
    }
    else if(document.adv.motYear.selectedIndex > 0 && document.adv.motMonth.selectedIndex <= 0)
    {
        alert(msgMotMonthErr);
        document.adv.motMonth.selectedIndex=0;
        document.adv.motMonth.focus();
        return false;
    }
    if(document.adv.id_sel_model.length > 0)
    {
        for(a=0; a<document.adv.id_sel_model.length-1; a++)
        {
            if(document.adv.id_sel_model[a].value != "")
                p += "'"+document.adv.id_sel_model[a].value+"',";
        }
        if(document.adv.id_sel_model[a].value != "")
            p += "'"+document.adv.id_sel_model[a].value+"'";

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

        document.adv.zAcc.value=z;
    }
    if(document.adv.id_sel_town.length > 0)
    {
        for(a=0; a<document.adv.id_sel_town.length-1; a++)
        {
            if(document.adv.id_sel_town[a].value != "")
                t += document.adv.id_sel_town[a].value+",";
        }
        if(document.adv.id_sel_town[a].value != "")
            t += document.adv.id_sel_town[a].value;

        document.adv.selTown.value=t;
    }
    z='';

    return true;
}

function addDefaultOpt(v1,txt)
{
    if(v1.options.length == 0)
    {
        selOpt=new Option(txt,'');
        eval('v1.options[0]=selOpt');
    }
}
function remDefaultAccOpt(v1)
{
    for(var i=1; i < v1.length; i++)
    {
        v1.options[i-1].text=v1.options[i].text;
        v1.options[i-1].value=v1.options[i].value;
    }
    v1.length=v1.length-1;
}
function remDefaultSubOpt(v1)
{
    for(var i=1; i < v1.length; i++)
    {
        v1.options[i-1].text=v1.options[i].text;
        v1.options[i-1].value=v1.options[i].value;
    }
    v1.length=v1.length-1;
}


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

        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;
                alert(msgAccDupSelected);
            }
        }
        if(dup != 1)
        {
            selOpt=new Option(v1.options[v1.selectedIndex].text,v1.options[v1.selectedIndex].value);
            eval('v2.options[tot]=selOpt');
        }
    }
}

//Function to remove the selected option from the listbox.
//Used for accessory and subtype.
function remSelectedOpt(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 showHideRentUnit()
{
	if(document.adv.ad_type[1].checked)
		document.getElementById('adv_rent_price').style.display="block";
	else
		document.getElementById('adv_rent_price').style.display="none";
}

function loadDef(v1)
{
    if(!v1.value)
    {
        if(v1.name=='priceFrom')
            v1.value=lvalue;
        else
            v1.value=uvalue;
    }
	showHideRentUnit();
}
/** Change of Vehicle type to get make in Advanced Search */

var textureName='';
var optChar='';
var optGroup='';
var textureGroup='';
var make_opt='';

function advVehicleTypeChange(v1,selVehType,v2,curval,cond,v3)
{
    var vehicleid=selVehType;
    var bflag=1;

    // If browser is IE but version is not 6.0 then flag will be false
    if(navigator.appVersion.indexOf('MSIE') != -1 && navigator.appVersion.indexOf('MSIE 6.0') == -1)
        bflag=0;
    if(navigator.appName == 'Netscape' && !(parseFloat(navigator.appVersion) >= 4.8))
        bflag=0;

    if(isNaN(curval))
        var id_make='';
    else
        var id_make=curval;

    textureName='';

    if(vehicleid != '')     // Vehicle Type is selected
    {
       var j=1;

        for(i=0; i < m1.length; i++)
        {
            goFlag=0;
            arrMakeInfo=(m1[i]).split('#');
            arrMakeTypeInfo=(arrMakeInfo[3]).split(',');

            for(k=0; k < arrMakeTypeInfo.length; k++)
            {
                if(arrMakeTypeInfo[k] == vehicleid)
                    goFlag=1;
            }
            if(goFlag == 0)
                continue;

            var make_value=arrMakeInfo[0];
            var make_text=arrMakeInfo[1];

            // Changes has been made for displaying makes in option group wise.
            // i.e. Starting with char A will listed below A Group, same as for B to Z.
            if(make_text.match(/^[A-Za-z]{1}/) && (!(navigator.userAgent.match(/Opera/))) && bflag == 1)
            {
                optChar='';
                optChar=make_text.substr(0,1);
                optGroup='T';

                if(textureName != optChar)
                {
                    textureName=optChar;
                    textureGroup=document.createElement('optgroup');
                    textureGroup.label=textureName;
                    v2.appendChild(textureGroup);
                }
            }
            else
                optGroup='F';

            if(optGroup != 'F' && textureName != '')
            {
                make_opt=document.createElement('option');
                make_opt.value=make_value;

                if(navigator.userAgent.match(/IE/))
                    make_opt.innerText=make_text;
                else
                    make_opt.text=make_text;

                textureGroup.appendChild(make_opt);
            }
            else
            {
                makeOpt=new Option(make_text,make_value);
                eval('v2.options[j]=makeOpt');
            }

            // Option group part ends here
            if(id_make == make_value)
                v2.selectedIndex=j;

            j++;
        }
        if(v2.selectedIndex == ''  || v2.selectedIndex < 1)
            v2.selectedIndex=0;
    }
}