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

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

Called By: advSearch.php

$Id: nv_advSearch.js,v 1.18 2010/12/15 04:54:13 nimisha Exp $
*****************************************************************************************************/
function changeAcc(v1,v2,curVal)
{
	if(v1.selectedIndex > 0)
		var boatid=v1.options[v1.selectedIndex].value;
	else
		var boatid='';

	if(boatid == '')
	{
		var strAcc=new String('');
		var j=0;

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

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

						strAcc = strAcc+"'"+ac[id]['v'][i]+"',";
						j++;
					}
				}
			}
		}
	}
	else
	{
		for(var i=0; i < ac[boatid]['v'].length; i++)
		{
			tempOpt=new Option(ac[boatid]['t'][i],
								ac[boatid]['v'][i]);
			eval("v2.options[i]=tempOpt");
		}
	}
}

/* Function that will be called when the boat type is changed in advance search. */
function advBoatTypeChange(blnChangeSelAcc)
{
	document.adv.id_acc.length=0;
	
	if(blnChangeSelAcc == 1)
		document.adv.id_sel_acc.length=0;

	changeAcc(document.adv.id_boat_type,document.adv.id_acc,''); 
    hideBoatPart(document.adv);
}

//Function to assign the selected values of the listboxes to the hidden variables. The values
//will be enclosed by ' and separated with ,.
function setZval(lst,zval,s1,s2)
{
var temp="";
zval.value="";

for(var i=0; i < lst.length; i++)
{
    if(lst.options[i].selected)
    {
        if(lst.options[i].value) // Check condition for ''.
            temp += s1+lst.options[i].value+s2;
    }
}
temp=temp.slice(0,-1);
zval.value=temp;
}


//This function is called when the advanced search form is submitted. It assigns the
//selected values from the listboxes to hidden variables using above function.
function checkFa(advForm)
{
	var z='';
	var sep1="'";
    var sep2="',";
    var temp='';

	if(document.adv.id_sel_acc.length > 0)
	{
		for(var 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;
	}
	z='';

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

		document.adv.zEngMake.value=z;
	}
	z='';

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

    setZval(document.adv.elements["id_sub_type[]"],document.adv.zSubType,sep1,sep2);
    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 != "")
                z += document.adv.id_sel_town[a].value+",";
        }
        if(document.adv.id_sel_town[a].value != "")
            z += document.adv.id_sel_town[a].value;

        document.adv.selTown.value=z;
    }
	return true;
}
//function to disable a group of checkbox.
//first argument is form itself. Second is checkbox name.
function disableElement(frmAdv,frmEle,enableDisable)
{
	var frmLen=frmAdv.elements.length;

	for(var i=0; i < frmLen; i++)
	{
		if(frmAdv.elements[i].name == frmEle)
		{
		    var element=frmAdv.elements[i];
		    var elementId=element.id;

		    element.disabled=enableDisable;

			if(enableDisable == 'disabled')
			    addClassName(elementId,'disable_frm_field');
			else
			    removeClassName(elementId,'disable_frm_field');
		}
	}
}

//Function to check whether passed string exist in array or not.
//It is used for subtype multiple values.
function chkSubType(string,array)
{ 
  var chk = 0;
  
  for (i = 0; i < array.length; i++)
  {
       if(array[i] == string)         
           chk =1;         
  }    
  if(chk==1)
      return true;
  else
      return false;
}

//Function to dipslay and hide sail, engine and heating
//part according to boat type in advanced search.
function hideBoatPart(advForm)
{
	if(advForm.id_boat_type.selectedIndex > 0)
		var id_boat=advForm.id_boat_type.options[advForm.id_boat_type.selectedIndex].value;
	else
		var id_boat='';

	 if(advForm.elements["id_sub_type[]"].selectedIndex > 0)
	    {  
	        var id_subType =new Array();
	        lst = advForm.elements["id_sub_type[]"];
	        
	        for(var i=0; i < lst.length; i++)
	        {
	            if(lst.options[i].selected)
	            {              
	                id_subType += lst.options[i].value+",";
	            }            
	        }
	        id_subType=id_subType.substr(0,id_subType.length-1);
	    }
	    else
	    {
	        var id_subType='';
	    }

	//Display sailing criteria, if the boat type is 'Sailing boat','Surf board','Motorsailor'
    //other wise disable those options.
	if(id_boat == 3 || id_boat == 4 || id_boat == 11 || id_boat == '')
	{
		document.getElementById('id_sail_type').disabled='';
		document.getElementById('id_sail_make').disabled='';
		document.getElementById('id_sail_material').disabled='';
	}
	else
	{
		document.getElementById('id_sail_type').disabled='disabled';
		document.getElementById('id_sail_make').disabled='disabled';
		document.getElementById('id_sail_material').disabled='disabled';
	}
    //Display heating criteria, if the boat type is 'Motor boat','Sailing boat' other wise disable those options.
    if(id_boat == 1 || id_boat == 3 || id_boat == '')
    {
        disableElement(advForm,'id_heat_make[]','');
        disableElement(advForm,'id_heat_fuel_type[]','');
    }
    else
    {
        disableElement(advForm,'id_heat_make[]','disabled');
        disableElement(advForm,'id_heat_fuel_type[]','disabled');
    }
	//Display engine criteria, if the boat type is 'Motor boat','Sailing boat','Rubber boat','Hovercraft',
    //'Motorsailor','Boat accessories and parts(having sub type is "Motors and motor acccessories, propellers")'
    //other wise disable those options.
    if(id_boat == 8)
    {
        document.getElementById('adv_id_sub_type').disabled=true;
        document.getElementById('lengthFrom').disabled=true;
        document.getElementById('lengthTo').disabled=true;
        document.getElementById('heightFrom').disabled=true;
        document.getElementById('heightTo').disabled=true;
        document.getElementById('widthFrom').disabled=true;
        document.getElementById('widthTo').disabled=true;
        document.getElementById('weightFrom').disabled=true;
        document.getElementById('weightTo').disabled=true;
        document.getElementById('accCond1').disabled=true;
        document.getElementById('accCond2').disabled=true;
        document.getElementById('id_acc').disabled=true;
        document.getElementById('id_sel_acc').disabled=true;
        document.getElementById('id_body_material').disabled=true;
        document.getElementById('id_eng_make').disabled=true;
        document.getElementById('id_sel_eng_make').disabled=true;
        document.getElementById('engine_rig').style.display='block';
        document.getElementById('powerFrom').disabled='';
        document.getElementById('powerTo').disabled='';
        document.getElementById('id_eng_type').disabled='';

        disableElement(advForm,'id_cooling_type[]','');
        disableElement(advForm,'id_eng_fuel_type[]','');

        addClassName('adv_id_sub_type','disable_frm_field');
        addClassName('lengthFrom','disable_frm_field');
        addClassName('lengthTo','disable_frm_field');
        addClassName('heightFrom','disable_frm_field');
        addClassName('heightTo','disable_frm_field');
        addClassName('widthFrom','disable_frm_field');
        addClassName('widthTo','disable_frm_field');
        addClassName('weightFrom','disable_frm_field');
        addClassName('weightTo','disable_frm_field');
        addClassName('accCond1','disable_frm_field');
        addClassName('accCond2','disable_frm_field');
        addClassName('id_acc','disable_frm_field');
        addClassName('id_sel_acc','disable_frm_field');
        addClassName('id_body_material','disable_frm_field');
        addClassName('id_eng_make','disable_frm_field');
        addClassName('id_sel_eng_make','disable_frm_field');
        addClassName('id_sail_type','disable_frm_field');
        addClassName('id_sail_make','disable_frm_field');
        addClassName('id_sail_material','disable_frm_field');
    }
    else if((id_boat == 14 && chkSubType(9,id_subType)) || id_boat == 1 || id_boat == 3 || id_boat == 9
        || id_boat == 10 || id_boat == 11 || id_boat == 15 || id_boat == '')
	{
        // In boat_type 14 and sub_type=9 not display only engine_make area.
        if(chkSubType(9,id_subType)==false)
        {
            document.getElementById('id_eng_make').disabled='';
            document.getElementById('id_sel_eng_make').disabled='';
        }

        disableElement(advForm,'id_cooling_type[]','');
		disableElement(advForm,'id_eng_fuel_type[]','');
		document.getElementById('powerFrom').disabled='';
		document.getElementById('powerTo').disabled='';
		document.getElementById('id_eng_type').disabled='';
	}
	else
	{
		disableElement(advForm,'id_cooling_type[]','disabled');
		disableElement(advForm,'id_eng_fuel_type[]','disabled');
        document.getElementById('id_eng_make').disabled='disabled';
        document.getElementById('id_sel_eng_make').disabled='disabled';
		document.getElementById('powerFrom').disabled='disabled';
		document.getElementById('powerTo').disabled='disabled';
		document.getElementById('id_eng_type').disabled='disabled';
	}

    if(id_boat != 8)
    {
        document.getElementById('adv_id_sub_type').disabled=false;
        document.getElementById('lengthFrom').disabled=false;
        document.getElementById('lengthTo').disabled=false;
        document.getElementById('heightFrom').disabled=false;
        document.getElementById('heightTo').disabled=false;
        document.getElementById('widthFrom').disabled=false;
        document.getElementById('widthTo').disabled=false;
        document.getElementById('weightFrom').disabled=false;
        document.getElementById('weightTo').disabled=false;
        document.getElementById('accCond1').disabled=false;
        document.getElementById('accCond2').disabled=false;
        document.getElementById('id_acc').disabled=false;
        document.getElementById('id_sel_acc').disabled=false;
        document.getElementById('id_body_material').disabled=false;
        document.getElementById('engine_rig').style.display='none';

        removeClassName('adv_id_sub_type','disable_frm_field');
        removeClassName('lengthFrom','disable_frm_field');
        removeClassName('lengthTo','disable_frm_field');
        removeClassName('heightFrom','disable_frm_field');
        removeClassName('heightTo','disable_frm_field');
        removeClassName('widthFrom','disable_frm_field');
        removeClassName('widthTo','disable_frm_field');
        removeClassName('weightFrom','disable_frm_field');
        removeClassName('weightTo','disable_frm_field');
        removeClassName('accCond1','disable_frm_field');
        removeClassName('accCond2','disable_frm_field');
        removeClassName('id_acc','disable_frm_field');
        removeClassName('id_sel_acc','disable_frm_field');
        removeClassName('id_body_material','disable_frm_field');
        removeClassName('id_eng_make','disable_frm_field');
        removeClassName('id_sel_eng_make','disable_frm_field');
        removeClassName('id_sail_type','disable_frm_field');
        removeClassName('id_sail_make','disable_frm_field');
        removeClassName('id_sail_material','disable_frm_field');
    }
}

//Function to add the selected option from one listbox into another listbox.
//Used for accessory and subtype.
function addSelectedOpt(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;
                    case 'id_sel_eng_make':
                        alert(msgDupEngModelSelected);
                        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 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;
}
/** Define global variable for those field they generated using javascript, used for manipulation */
var lenTo=htTo=wtTo=wgtTo=powTo=yrTo='';
var engMake=new Array();
var accA=new Array();
var selTown=new Array();

/** 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;
    }
}
/** 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.lengthTo.selectedIndex > 0)
        lenTo=advForm.lengthTo.value;

    if(advForm.heightTo.selectedIndex > 0)
        htTo=advForm.heightTo.value;

    if(advForm.widthTo.selectedIndex > 0)
        wtTo=advForm.widthTo.value;

    if(advForm.weightTo.selectedIndex > 0)
        wgtTo=advForm.weightTo.value;

    if(advForm.powerTo.selectedIndex > 0)
        powTo=advForm.powerTo.value;

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

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

    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 blankFa()
{
    if(isblank(document.adv.findAgent.value) || !checkCity(document.adv.findAgent.value))
    {
        alert(msgInvalidFa);
        document.adv.findAgent.focus();
        return false;
    }
}
//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;

        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 > 0)
            {
                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 addDefaultOpt(v1,txt)
{
    if(v1.options.length == 0)
    {
        selOpt=new Option(txt,'');
        eval('v1.options[0]=selOpt');
    }
}
function updateNumField(o)
{
    if (frmFixUIntField(o))
    o.focus();
}

var frmD=new Object(), frmO, frmE;

function frmCheckUInt(o)
{
    for (var i=0;i < o.length;i++)
    {
        var c=o.charAt(i);
        if (c < '0' || c >'9')
        return false;
    }
    return true;
}

function frmFixUIntField(o)
{
    var t=frmCheckUInt(o.value);
    if (t)
    {
        frmD[o.name]=o.value;
    }
    else
    {
        o.value=(frmD[o.name]!=null?frmD[o.name]:'');
    }
    return t;
}

function showHideRentUnit()
{
	if(document.adv.status[1].checked)
		document.getElementById('adv_rent_price').style.display="block";
	else
		document.getElementById('adv_rent_price').style.display="none";
}

function loadDefSearch(v1)
{
    if(!v1.value)
    {
        if(v1.name=='priceFrom' || v1.name=='prFrom')
        	v1.value=lvalue;
        else if(v1.name=='priceTo' || v1.name=='prTo')
            v1.value=uvalue;
    }
	showHideRentUnit();
}

function addClassName(element, className)
{
    if (!(element = idiv(element)))
    {
        if(!element.className)
            return;
    }
    element.className += (element.className ? ' ' : '') + className;
    return element;
}

function removeClassName(element, className)
{
    if (!(element = idiv(element)))
    {
        if(!element.className)
            return;
    }

    element.className = element.className.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), ' ').strip();
    return element;
}

function boatTypeChange(v1,v2,curval,cond)
{
    var boatid='';
    var bflag=1;

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

    if(cond == 'Y' || cond == 'POST')
        var id_make="'" + curval + "'";
    else if(cond == 'ADV')
        var id_make=curval;
    else
        var id_make='';

    if(v1)
    {
        if(v1.length)
        {
            if(v1.selectedIndex > -1)
                var boatid=new String(v1.options[v1.selectedIndex].value);
            else
                var boatid='';
        }
        else
            var boatid=v1;
    }
    textureName='';
    if(boatid != '')    //Boat Type is selected
    {
        v2.length=1;
        var j=1;
        for(i=0; i < m.length; i++)
        {
            goFlag=0;
            arrMakeInfo=(m[i]).split('#');
            arrMakeTypeInfo=(arrMakeInfo[3]).split(',');

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

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

            if(cond == 'POST' || cond == 'ADV' || arrMakeInfo[2] > 0)   //Posting boat && Simple Search
            {
                /** OPTION GROUP PART STARTS HERE */
                /* Changes has been made for displaying makes in alphabetical group. */
                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 */

                //Display the selected values.
                if(id_make.search("'"+make_value+"'") != -1)
                    v2.options[j].selected=true;

                if(id_make == make_value)
                    v2.options[j].selected=true;

                j++;
            }
        }
    }
    else
    {
        //When Boat Type is not selected (Simple Search)
        v2.length=1;
        var j=1;

        for(i=0; i < m.length; i++)
        {
            arrMakeInfo=(m[i]).split('#');

            if(arrMakeInfo[2] > 0 || cond == 'ADV')
            {
                var make_value=arrMakeInfo[0];
                var make_text=arrMakeInfo[1];

                /** OPTION GROUP PART STARTS HERE */
                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 */

                //Display the selected values.
                if(id_make.search("'"+make_value+"'") != -1)
                    v2.options[j].selected=true;

                if(id_make == make_value)
                    v2.options[j].selected=true;

                j++;
            }
        }
    }
}
function subTypeChange(v1,v2,curval,cond)
{
    if(cond == 's' && idiv('sid_sub_type'))
    {
        if(v1 && v1.options[v1.selectedIndex].value == 8)
        {
            jQuery('#sid_sub_type').hide();
            jQuery('#sid_length').hide();
        }
        else
        {
            jQuery('#sid_sub_type').css('display','inline');
            jQuery('#sid_length').css('display','inline');
        }
    }

    //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;

    //'s' -> simple search, 'p'-> boat post/edit, 'ADV'->advamce Search
    if(cond == 's' || cond == 'p' || cond == 'ADV')
        curval="'"+curval+"'";

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

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

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

    if(cond == 's') //For simplate search display the 'Boat subtype' as default option.
    {
        opt=new Option(subtype,'');
        eval("v2.options[j]=opt");
        j++;
    }
    else if(cond == 'p')    //For boat post/edit, display the blank value as default option.
    {
        opt=new Option(capSel+' '+subtype,'');
        eval("v2.options[j]=opt");
        j++;
    }
    else if(cond == 'ADV') //For advance search display the 'Boat subtype' as default option.
    {
        opt=new Option(modelDefVal,'');
        eval("v2.options[j]=opt");
        j++;
    }
        if(typeId != '') //If the boat type is selected(boat post/edit, simple search and advanced search)
    {
        for(var i=0; i < r[typeId].length; i++)
        {
            opt=new Option(s[typeId][i],r[typeId][i]);
            eval("v2.options[j]=opt");

            if(curval.search("'"+r[typeId][i]+"'") != -1)
                v2.options[j].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=0;i < r[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("'"+r[idBoatType][i]+"'") == -1)
                    {
                        opt=new Option(s[idBoatType][i],r[idBoatType][i]);
                        eval("v2.options[j]=opt");

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

                        strIdSubType=strIdSubType+"'"+r[idBoatType][i]+"',";
                        j++;
                    }
                }
            }
        }
    }
}
function listboxChange(v1,v2,curval,sign)
{
    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

    if(curval == '' && v2.selectedIndex > 0)
        curval=v2.options[v2.selectedIndex].value;

    var tot=v1.length;
    v2.length=1;

    for(i=st,j=1; i < tot; j++,i++)
    {
        if (parseInt(v1.options[i].value) >= 1000 && parseInt(v1.options[i].value) < 1000000)
        {
            var len=v1.options[i].value.length;
            var disp=v1.options[i].value;
            var disp1=disp.substr(0,len-3);
            var disp2=disp.substr(len-3,len);
            disp=disp1+' '+disp2;
        }
        else if (parseInt(v1.options[i].value) >= 1000000)
        {
            var len=v1.options[i].value.length;
            var disp=v1.options[i].value;
            var disp1=disp.substr(0,len-6);
            var disp2=disp.substr(len-6,len-4);
            var disp3=disp.substr(len-3,len);
            disp=disp1+' '+disp2+' '+disp3;
        }
        else
            var disp=v1.options[i].value;

        if(sign != '')
            disp=disp+' '+sign;

        temp=new Option(disp,v1.options[i].value);
        eval("v2.options[j]=temp");

        if(curval == v1.options[i].value)
            v2.selectedIndex=j;
    }
}
function RemoveGroups(selectId,v2,make_txt)
{
    var lChars=new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','Ä','Ö');

    for(var chr=0; chr < lChars.length; chr++)
        RemoveGroup(lChars[chr],selectId);
}
function callAjax(frmObj)
{
    var ajaxFName;var loadingImg='';
    var frmName = frmObj.name;
    /** for write hidden parameter selected model & selected acc and validate inspected motmonth & motyear */
    if(frmObj.name == 'srch_4x4car')
        ajaxFName=_su + '/ajaxSearchCounter.php?ajxifrm=A&site=NA';
    else
        ajaxFName=_su + '/ajaxSearchCounter.php?ajxifrm=A&site=NV';

    if(frmName == 'adv')
    {
        checkFa();
        value = '&frm=ADV&';       
    }
    else if(frmName == 'br_search')
    {
        checkBrFa();
        value = '&frm=BR&';        
    }
    else if(frmName == 'srch' || frmName == 'srch_4x4car')    
        value = '&frm=SIMP&';            
    
    var searchUrl = ajaxFName+value+ cleanFrmUrl(frmObj);   
    var ajaxFname=searchUrl.replace('/#/ig','@');

    if(frmName == 'adv' || frmName == 'br_search')
        loadingImg='loadingImg_src_chngCntAdv';
    else if(frmName == 'srch')
        loadingImg='loadingImg_src_chngCnt';
    else if (frmName == 'srch_4x4car' )
        loadingImg='loadingImg_car';
    
    var xmlHttpReq=createXHR();

    xmlHttpReq.open('GET',ajaxFname);
    xmlHttpReq.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
    xmlHttpReq.onreadystatechange=function()
    {
        if(xmlHttpReq.readyState == 4)
        {
            showResponse(xmlHttpReq.responseXML);
            idiv(loadingImg).style.display='none';
        }
    };
    xmlHttpReq.send(null);
    return false;     
}
function getDomTown(domVal,curval,page)
{
    var townStr='';
    var townUrl='http://'+location.hostname+'/ajaxGetDomicileTown.php?ajxifrm=A&frm='+page+'&site=NV&';

    if(curval) townStr='&sel_id_town='+curval;

    if(page == 'ADV')
        AjaxUpdater('adv_id_town',townUrl+'id_domicile='+domVal+townStr,"callAjax(document.adv);");
    else if(page == 'SIMP')
        AjaxUpdater('srch_id_town',townUrl+'id_domicile='+domVal+townStr,"callAjax(document.srch);");
    else if(page == 'POST')
        AjaxUpdater('post_id_town',townUrl+'id_domicile='+domVal+townStr);
    else if(page == 'listService')
        AjaxUpdater('service_id_town',townUrl+'id_domicile='+domVal+townStr);
}

function ajaxListChange(frmObj,chgFor,efs)
{  
    var efs,pars;
    var xhr=createXHR();

    pars=Form.serialize($(frmObj));
    if(typeof efs == 'undefined') efs='';

    pars +='&site=NV&chgFor=' + chgFor + '&ajxr=A' + efs + '&frm=' + frmObj.name;
   
    xhr.open('POST',_su+'/ajaxChangeEntity.php');
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", pars.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.onreadystatechange=function()
    {
        if(xhr.readyState == 4)
        {
            showTextResponse(xhr.responseText);

            if(frmObj.name == 'srch' || frmObj.name == 'adv')
            {                
                callAjax(frmObj);
            }
        }
    }
    xhr.send(pars);
    return false;
}
