/****************************************************************************************************
Script: nk_advSearch.js

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

Called By: advSearch.php

$Id: nk_advSearch.js,v 1.7 2010/12/14 11:12:53 hetal Exp $
*****************************************************************************************************/

/** Define global variable for those field they generated using javascript, used for manipulation */
var milgeTo=h_milgeTo=engTo=wgtTo=yrTo='';
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 != "" && advFormField[i].value != null)
				defValArr[i]=advFormField[i].value+","+advFormField[i].text;
		}
		return defValArr;
	}
}
function setDefReset(advForm)
{
    /** Set values for 'No upper limit' comboboxes */
    if(advForm.mileageTo.selectedIndex > 0)
        milgeTo=advForm.mileageTo.value;

    if(advForm.h_mileageTo.selectedIndex > 0)
        h_milgeTo=advForm.h_mileageTo.value;

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

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

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

    /** Set for selected filed area  */
    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 unchecked
            	return;
            }
        }
        chkGrp.checked=true;
    }
    callAjax(document.adv); // This is used for counter when price status is 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,i;

        for(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(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(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;
    }
    return true;
}
function checkFa()
{
    var z=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_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;
    }
	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;
    }

    var sep1="'";
    var sep2="',";
    var temp='';

    setZval(document.adv.id_make,document.adv.zMake,sep1,sep2);
    setZval(document.adv.elements["id_sub_type[]"],document.adv.zSubType,sep1,sep2);
    setZval(document.adv.id_cat_sub_type,document.adv.zCatSubType,sep1,sep2);

    return true;
}
//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;
}
function addDefaultOpt(v1,txt)
{
    if(v1.options.length == 0)
    {
        var selOpt=new Option(txt,'');
        eval('v1.options[0]=selOpt');
    }
}
//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=v2.length - 1;

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

        for(var 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)
    {
        var selOpt=new Option("---------------- "+modelDefVal+" -----------------",'');
        eval('v1.options[0]=selOpt');
    }
    v1.selectedIndex=-1;
}
function changeMachineType()
{   
    accChange(document.adv.id_machine_type,document.adv.id_acc,document.adv.zAcc.value,document.adv.id_sel_acc);
}
//Function to change the accessory corresponding to machine type.
function accChange(v1,v2,curval,v3)
{
    //holds the selected machine type.
    var i,n,k,opt,selOpt,j=0,typeId='';

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

    v2.length=0;

    if(typeId != '') //If the machine type is selected
    {
        for(i=0,n=0; i < ar[typeId].length; i++)
        {
            opt=new Option(acv[typeId][i],ar[typeId][i]);
            eval('v2.options[j]=opt');

            if(curval.search("'"+ar[typeId][i]+"'") != -1)
            {
                selOpt=new Option(acv[typeId][i],ar[typeId][i]);
                eval('v3.options[n++]=selOpt');
            }

            j++;
        }
    }
    else    //If the machine type is not selected.
    {
        for(k=1,n=0; k < 7; k++)
        {
            for(i=0; i < ar[k].length; i++)
            {
                opt=new Option(acv[k][i],ar[k][i]);
                eval('v2.options[j]=opt');

                if(curval.search("'"+ar[k][i]+"'") != -1)
                {
                    selOpt=new Option(acv[k][i],ar[k][i]);
                    eval('v3.options[n++]=selOpt');
                    curval=curval.replace("'"+ar[k][i]+"'",''); // curval set '' after it found from array.
                }
                j++;
            }
        }
    }
}
//Function to change the Length,Power and Price list box options
function listboxChange(v1,v2,curval,sign)
{
    var st,len,disp,disp1,disp2,i,j,tot,temp;
    if(v1.selectedIndex < 1)
        st=1;
    else
        st=v1.selectedIndex;

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

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

    for(i=st,j=1; i < tot;i++,j++)
    {
        if (parseInt(v1.options[i].value) >= 1000)
        {
            len=v1.options[i].value.length;
            disp=v1.options[i].value;
            disp1=disp.substr(0,len-3);
            disp2=disp.substr(len-3,len);
            disp=disp1+' '+disp2;
        }
        else
            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 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 loadDef(v1)
{
    if(!v1.value)
    {
        if(v1.name=='priceFrom')
        v1.value=lvalue;
        else
            v1.value=uvalue;
    }
	showHideRentUnit();
}
function ajaxListChange(frmObj,chgFor,efs)
{ 
    var efs,pars;
    var xhr=createXHR();

    pars=Form.serialize($(frmObj));
    if(typeof efs == 'undefined') efs='';
    
    pars +='&site=NK&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;
}
function getDomTown(domVal,curval,page)
{
    var townStr='';
    var townUrl='http://'+location.hostname+'/ajaxGetDomicileTown.php?ajxifrm=A&frm='+page+'&site=NK&';

    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);
}
/** call Ajax function for search result counter on change elements value. */
function callAjax(frmObj)
{
    /** for write hidden parameter selected model & selected acc and validate inspected motmonth & motyear */
    var ajaxFName=_su + '/ajaxSearchCounter.php?ajxifrm=A&site=NK';
    if(frmObj.name == 'adv')
    {
        checkFa();
        return ajaxCounterChng(ajaxFName+'&frm=ADV&'+ cleanFrmUrl(frmObj),frmObj.name);
    }
    else if(frmObj.name == 'srch')
    {
        return ajaxCounterChng(ajaxFName+'&frm=SIMP&'+ cleanFrmUrl(frmObj),frmObj.name);
    }
}
/** Ajax Function for display total counter of vehicle which are belong to search criteria */
function ajaxCounterChng(searchUrl,frmName)
{
    var loadingImg='';
    var ajaxFname=searchUrl.replace('/#/ig','@');

    if(frmName == 'adv')
        loadingImg='loadingImg_src_chngCntAdv';
    else if(frmName == 'srch')
        loadingImg='loadingImg_src_chngCnt';

    idiv(loadingImg).style.display='inline';

    var xmlHttpReq=createXHR();
    xmlHttpReq.open('GET',ajaxFname);
    xmlHttpReq.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
    xmlHttpReq.onreadystatechange=function()
    {
        if(xmlHttpReq.readyState == 4)
        {
            showResponse(xmlHttpReq.responseXML);
            idiv(loadingImg).style.display='none';
        }
    };
    xmlHttpReq.send(null);
    return false;
}
