/* $Id: nv_common_75.js,v 1.2 2009/12/01 09:35:57 kiran Exp $ */

var _su='http://'+location.hostname;

function createXHR()
{
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   return null; // no XHR support
}

function AjaxUpdater(updateDiv,url,extraAction)
{
    var xhr = createXHR();
    xhr.open("GET", url, true)
    xhr.setRequestHeader("Content-Type", "text/html;charset=UTF-8");

    xhr.onreadystatechange=function()
    {
        if(xhr.readyState == 4)
        {
            if(updateDiv)
                idiv(updateDiv).innerHTML=xhr.responseText;
            if(extraAction)
                eval(extraAction);
        }
    }
    xhr.send(null);
    return false;
}

function idiv(i)
{
    return document.getElementById(i);
}

function check_search()
{
    if(isblank(document.search.veh_id.value))
    {
        alert(msgBlankSearchBoat);
        document.search.veh_id.focus();
        return false;
    }
    else
    {
        //If boat id is there
        if(document.search.veh_id.value)
        {
            if(document.search.veh_id.value.match(/^[0-9]{1,}$/))
            {
                //Strips blank spaces from left and right
                var lre=/^\s*/;
                var rre=/\s*$/;

                document.search.veh_id.value=document.search.veh_id.value.replace(lre,"");
                document.search.veh_id.value=document.search.veh_id.value.replace(rre,"");

                self.location.href='viewBoat.php?id_boat='+document.search.veh_id.value;
            }
            else
            {
                alert(msgNumeric);
                document.search.veh_id.focus();
                return false;
            }
        }
    }
    return false;
}

function getPage(){
  url    = window.location.href;
  page   = window.location.href.toString().match(/\/([^\/]+)(#)?$/)[1];
  page   = page.replace(/\W/g,"");
  return page;
}

function maintainScrollUnload(e){
  page = getPage();
  var date = new Date()
  var date = new Date()
  var expdate = date.getTime()
  expdate += 5*60*1000; //expires in 5 minutes(milliseconds)
  date.setTime(expdate);

  if(page.match(/^(simpleSearch|listBoat|listAdvSearchFindAgent|listCategory)/)){
    document.cookie = 'scroll=' +page+ document.documentElement.scrollTop
                    + "; expires="+ date.toGMTString();
  }
  if(page.match(/^viewBoat/)){
    document.cookie = "prevpage="+page + "; expires="+ date.toGMTString();
  }
}

function maintainScrollLoad()
{
  var page=getPage();
  if( document.cookie.match(/prevpage=viewBoatphp/ ) )
    {
        var reg = new RegExp( "scroll="+page + "(\\d+)" );
        if( scroll = document.cookie.match(reg) )
        {
            document.documentElement.scrollTop=scroll[1]-50;
        }
    }
}

if(!window.location.href.toString().match(/\/$/) && navigator.userAgent.indexOf('Firefox') > 0){
    if (window.addEventListener) {
      window.addEventListener("load", maintainScrollLoad, false);
      window.addEventListener("beforeunload", maintainScrollUnload, false);
    }
}

function car_quiz()
{
    window.open('carQuiz.php','ap','scrollbars=auto,resizable=yes,width=555,height=700');
    AjaxUpdater('car_quiz',_su+'/updateQuizCount.php');
}
/** Function to increase the hit counter of navigation_ad table */
function navigationHitCounter(id,urlNo)
{
    AjaxUpdater('',_su+'/navigationHitCounter.php?id='+id+'&urlNo='+urlNo);
}

//This method is Used in Simple Search and Boat Post forms.
var textureName='';
var optChar='';
var optGroup='';
var textureGroup='';
var make_opt='';

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 to change the subtype corresponding to boat type
function subTypeChange(v1,v2,curval,cond)
{

    //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 for changes the make value as per change the boat sub type
    v1=contain the id_boat_type
    v2=contain the id_sub_type
    v3=contain the id_make
    curval=contain the selected/current value of id_make
    cond=wether call from post/editing(p) OR search(s) page.
*/
function makeChange(v1,v2,v3,curval,cond)
{
    if(v1.options[v1.selectedIndex].value == 14)
    {
        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 != '')
        {
            v3.length=1;
            var j=1;

            if(v2 && v2.selectedIndex >= 0)
            {
                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;
                                v3.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('v3.options[j]=makeOpt');
                        }
                        /** OPTION GROUP PART ENDS HERE */
                        if(id_make.search("'"+make_value+"'") != -1)
                            v3.options[j].selected=true;

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

                        j++;
                    }
                }
                if(v3.selectedIndex == '' || v3.selectedIndex < 1)
                    v3.selectedIndex=0;
            }
            else
            {
                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];

                    /** This is only for 'other make'(make_id=1) for boat sub type */
                    if(make_value == 1)
                    {
                        /** 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;
                                v3.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('v3.options[j]=makeOpt');
                        }
                        /** OPTION GROUP PART ENDS HERE */
                        if(id_make.search("'"+make_value+"'") != -1)
                            v3.options[j].selected=true;

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

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

//Function to change year list box options
function yearChange(v1,v2,curval)
{
    var tot=v1.selectedIndex+1;

    if(tot == 1)
        tot=v1.options.length-1;

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

    v2.length=1;

    for(i=1,j=1; i < tot; j++,i++)
    {
        temp=new Option(v1.options[i].value,v1.options[i].value);
        eval("v2.options[j]=temp");

        if(curval == v1.options[i].value)
            v2.selectedIndex=j;
    }
}

//Function to change the Length,Power and Price list box options
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 RemoveGroup(groupLabel,selectId)
{
    try
    {
        var selectList=document.getElementById(selectId);
        var eachGroup=selectList.firstChild;

        while (groupLabel != eachGroup.label)
            eachGroup=eachGroup.nextSibling;

        selectList.removeChild(eachGroup);
    }
    catch(er){}
}

//Function to open new pop up window with variable width and height
function openImg(img_nm,w,h,nm)
{
    var myfeatures="directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",left=0,top=0";

    if(nm == '')
        nm='new';

    var newImg=open(img_nm,nm,myfeatures);
    newImg.focus();
}

//Function opens window of desired size by giving proper alert window
function doLogin(msgVar,width,height)
{
    openImg("msgWin.php?opt="+msgVar,width,height,'newuser');
}

// Function to submit favorite page by adding/removing boats
function submitFav(thisV)
{
    frmObj = document.fav;
    var i,flag='N',checked_value,img_id='';

    for(i=1; i < document.fav.length; i++)
    {
        if(document.fav.elements[i].type == 'checkbox')
        {
            if(document.fav.elements[i].checked)
            {
                flag='Y';
                continue;
            }
        }
    }
    if(flag != 'Y')
    {
        alert(msgSelectAtLeastOne);
        return false;
    }

    if(thisV == 'remove')
        document.fav.opt.value='Rem';
    else
        document.fav.opt.value='Fav';

    /*  AJAX code starts    */

    if(thisV != 'remove')
    {
        idiv('loadingImg').style.display = 'inline';
        idiv('loadingImg2').style.display = 'inline';

        var pars = Form.serialize($(frmObj));
        var sub_url = document.fav.action;
        var xmlHttpReq=createXHR();

        xmlHttpReq.open('POST',sub_url,true);
        xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttpReq.setRequestHeader("Content-length", pars.length);
        xmlHttpReq.setRequestHeader("Connection", "close");
        xmlHttpReq.onreadystatechange=function() {
            if(xmlHttpReq.readyState == 4) {
                var str = xmlHttpReq.responseText;
                arr = str.split("#");

                if(arr[0] != '0')
                {
                    idiv('favorite_cnt').innerHTML='(' + arr[0] + ')';
                    idiv('favorite_cnt_end').innerHTML = '(' + arr[0] + ')';
                }
                if(arr[1] != '0')
                {
                    idiv('common_messageX').innerHTML = arr[1] + ' ' + msgAddFavBig;
                    idiv('Add_To_Fav_End').innerHTML  = arr[1] + ' ' + msgAddFavBig;
                }
                else
                {
                    idiv('common_messageX').innerHTML = msgRptAddFav;
                    idiv('Add_To_Fav_End').innerHTML  = msgRptAddFav;
                }
                idiv('common_messageX').style.display = 'block';
                idiv('loadingImg').style.display='none';
                idiv('Add_To_Fav_End').style.display  = 'block';
                idiv('loadingImg2').style.display = 'none';
            }
        }
		xmlHttpReq.send(pars);

        //  Remove checked items
        for(i=1; i < document.fav.length; i++)
        {
            if(document.fav.elements[i].type == 'checkbox')
            {
                if(document.fav.elements[i].checked)
                {
                    document.fav.elements[i].checked=false;
                    if(thisV != 'remove' && document.fav.elements[i].alt != "compVehicle")
                    {
                        checked_value=document.fav.elements[i].value;
                        changeClass(checked_value,'listing');
                    }
                    continue;
                }
            }
        }
        return false;
    }
    else
    {
        document.fav.submit();
    }
}

//Argument 'label' can be the entity name you want to delete,
//for exa., user,dealer,boat etc. and 'nm' can be the user name,
//dealer name or boat id etc.
function newConfirmDel(ref,label,nm)
{
    if(confirm(msgNewDeleteConfirm+" "+label+", '" + nm + "'?"))
        window.location.replace(ref);
}

//To check login sesion of user in VIF while sumbitting
//question or giving answer, if user is not logged in
//a popup window opens where he can log in
function checkQue(file,from)
{
    if(file.id_user.value == '')
    {
        if(from == 'O')
            openImg('../../popUpLogin.php?M=OnlyForRegisteredUser',350,320,'na_login');
        else
            openImg('popUpLogin.php?M=OnlyForRegisteredUser',350,320,'na_login');

        return false;
    }
    var flag='N';

    for(var q=0; q < file.elements.length; q++)
    {
        if(file.elements[q].type == 'text')
        {
            if(!isblank(file.elements[q].value))
            {
                if(!isLong(file.elements[q].value,' ',40))
                {
                    alert(msgTooLongWord);
                    file.elements[q].select();
                    file.elements[q].focus();
                    return false;
                }
                flag='Y';
            }
        }
    }
    if(flag == 'N')
    {
        alert(msgBlankForm);
        return false;
    }
    return true;
}

//Function to return nothing, this is used sometimes
//to display tool tip on plain text by making trick
function returnVoid() {}

function confirmReset(thisForm)
{
    if(confirm(msgConfirmReset))
        return true;

    return false;
}

// function to change town list according the selected domicile.
function partTownChange(ptype,town,curval,page)
{
    if(ptype)
    {
        if(ptype.length)
        {
            if (page == 'SELECTPART')
            {
                if(ptype.selectedIndex > -1 && ptype.selectedIndex > 0)
                        idPart=new String(ptype.options[ptype.selectedIndex].value);
                else
                        idPart='';
            }
            else
            {
                if(ptype.selectedIndex > -1)
                        idPart=new String(ptype.options[ptype.selectedIndex].value);
                else
                        idPart='';
            }
        }
        else
            partid=ptype;
    }
    if(page == 'simp')
        townlist=curval.split(',');

    if(page == 'POST')
    {
        town.length=1;
        j=1;
    }
    else if(page == 'ADV')
    {
        town.length=0;
        j=0;
    }
    else if (page == 'listService')
    {
        town.length=0;
        j=0;
    }
    else
    {
        town.length=1;
        j=1;
    }
    if(idPart != '')
    {
        if(tId[idPart])
        {
            for(i=0; i < tId[idPart].length; i++)
            {
                cvalue=new String(tId[idPart][i]);
                ctext=new String(tName[idPart][i]);

                cOpt=new Option(ctext,cvalue);
                eval("town.options[j]=cOpt");

                if(page == 'simp')
                {
                    for(k=0; k < townlist.length; k++)
                    {
                        if(cvalue == townlist[k])
                            town.options[j].selected=true;
                    }
                }
                else
                {
                    if(tId[idPart][i] == curval)
                        town.selectedIndex=j;
                }
                j++;
            }
        }
    }
    else
    {
        j=1;
        town.length=1;
    }
}

function categoryChange(category,subcategory,curval)
{
    if(category)
    {
        if(category.length)
        {
            if(category.selectedIndex > -1)
            {
                idCat=new String(category.options[category.selectedIndex].value);
            }
            else
                idCat='';
        }
        else
            idCat=category;
    }

    subcategory.length=0;
    j=0;

    if(idCat != '')
    {
        if(sId[idCat])
        {
            for(i=0; i < sId[idCat].length; i++)
            {
                if(sCnt[idCat][i] > 0)
                {
                    if(sAvail[idCat][i].match(/NV/))
                    {
                        cvalue=new String(sId[idCat][i]);
                        ctext=new String(sName[idCat][i]);

                        cOpt=new Option(ctext,cvalue);
                        eval("subcategory.options[j]=cOpt");

                        if(sId[idCat][i] == curval)
                            subcategory.selectedIndex=j;
                        j++;
                    }
                }
            }
        }
    }
    else
    {
        j=0;
        subcategory.length=0;
    }
}

function checkPage(theForm,pageVal)
{
    if(theForm.gotopage.value.match(/^[0-9]{1,3}$/))
    {
        if(theForm.gotopage.value != 0)
        {
            if(theForm.gotopage.value > pageVal)
            {
                alert(msgPageNotAvailable);
                return false;
            }
            else
                return true;
        }
        else
        {
            alert(notValidPageNumber);
            return false;
        }
    }
    else
    {
        alert(notPageVal);
        return false;
    }
}

//  Functions for changing background color of updated row(s) when posting question or answers for Boats.
function whiteBG()
{
    idiv('que_modal').value = '';
    var elements = document.getElementsByTagName("tr");
    for(var i = 0;i < elements.length;i++)
    {
        if(elements[i].id == "TR_BG" && elements[i].className == "bgyellow" )
        {
            elements[i].className = "bgwhite";
        }
    }
    setTimeout("yellowBG();",500);
}

function yellowBG()
{
    var elements = document.getElementsByTagName("tr");
    for(var i = 0;i < elements.length;i++)
    {
        if(elements[i].id == "TR_BG" && elements[i].className == "bgwhite")
        {
            elements[i].className = "bgyellow";
        }
    }
    setTimeout("oddevenBG();",500);
}

// Function for doing normal background.
function oddevenBG()
{
    var elements = document.getElementsByTagName("tr");
    for(var i = 0;i < elements.length;i++)
    {
        if(elements[i].id == "TR_BG")
        {
            if(i % 2 == 0)
                elements[i].className = "even";
            else
                elements[i].className = "odd";
        }
    }
}

//      Fn. for Login modal
function loginmodal(opt,rPage)
{
    //  D => Simple Dealer , N => From View Page , P => Post Ad, RP => any given redirect page rPage
    idiv('lightbox').style.left='40%';
    if(opt == 'D')
    {
        pageURL='../modalLogin.php?mainOnly=1&sd=1';
        idiv('lightbox').style.left = '65%';
    }
    else if(opt == 'N')
        pageURL='modalLogin.php?mainOnly=1&sd=0';
    else if(opt == 'P')
        pageURL='modalLogin.php?mainOnly=1&postad=1';
    else if(opt == 'RP')
    {
        pageURL='modalLogin.php?mainOnly=1&redirectPage='+rPage;
        idiv('lightbox').style.left = '40%';
    }
    else if(opt != 'P' && location.href.search('advSearch') == -1)
        document.getElementById('que_modal').blur();

    lightbox.prototype.content = pageURL;
    lightbox.prototype.activate();
    idiv('lightbox').style.top='60%';
    idiv('lightbox').style.width='295px';
}

function sd_ajaxQue(formObj,var_O)
{
    if(checkQue(formObj,var_O) != false )
    {
        var id_bike = formObj.id_boat.value;
        var pars = Form.serialize($(formObj));
        if(formObj.replyToQue.value == 0)
        {
            formObj.que.disabled=true;
            formObj.exchangeid.disabled=true;
            formObj.save.disabled=true;
            document.getElementById('loaderimg').style.display='inline';
        }
        /* replacing € with &euro; */
        pars = pars.replace(/%E2%82%AC/g,'%26euro%3B');

        var xmlHttpReq=createXHR();
        xmlHttpReq.open('GET',_su+'/simple_dealer/sd_questionAnswer.php?ajxQueAns=1&'+pars,true);
        xmlHttpReq.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        xmlHttpReq.onreadystatechange=function() {
            if(xmlHttpReq.readyState == 4) {
                var str = xmlHttpReq.responseText;
                idiv('submitXQA').innerHTML = str;
                setTimeout("whiteBG();",1000);
            }
        }
        xmlHttpReq.send(null);
        return false;
    }
    else
        return false;
}

function sd_ajaxLoadImage(id)
{
    var qa_id = id;
    if(document.getElementById('nv_id_user').value == "")
        return false;


    if(checkQue(document.frmqueans) != false )
    {
        var answer = document.frmqueans.replyToQue.value;
        var pars = Form.serialize($(document.frmqueans));

        if(answer == 1)
        {
            var loader_img_id='loaderimg'+qa_id;
            var ans_id='ans'+qa_id;
            var anssave_id='anssave'+qa_id;

            document.getElementById(ans_id).disabled=true;
            document.getElementById(anssave_id).disabled=true;
            document.getElementById(loader_img_id).style.display='inline';
        }

        /* replacing € with &euro; */
        pars = pars.replace(/%E2%82%AC/g,'%26euro%3B');

        var xmlHttpReq=createXHR();
        xmlHttpReq.open('GET',_su+'/simple_dealer/sd_questionAnswer.php?ajxQueAns=1&'+pars,true);
        xmlHttpReq.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        xmlHttpReq.onreadystatechange=function() {
            if(xmlHttpReq.readyState == 4) {
                var str = xmlHttpReq.responseText;
                idiv('submitXQA').innerHTML = str;
                setTimeout("whiteBG();",1000);
            }
        }
        xmlHttpReq.send(null);
        return false;
    }
    else
        return false;
}

//      Check blank form from modal
function modalForm(frmObj)
{
    var flag='N';
    var page_from = frmObj.page_from.value;
    var login_page;

    if(frmObj.postad.value == 1)
        var returnURL="postAd.php";
    else if(frmObj.redirectPage && frmObj.redirectPage.value != '')
        var returnURL=frmObj.redirectPage.value;
    else
        var returnURL=location.href;

    if(page_from.search('simple_dealer') == -1)
        login_page = 'login.php?ajxLogin=1'
    else
        login_page = '../login.php?ajxLogin=1'

    for(i=0; i < frmObj.elements.length;i++)
    {
        if(frmObj.elements[i].type == 'text')
        {
            if(!isblank(frmObj.elements[i].value))
                flag='Y';
        }
    }
    if(frmObj.passwd.value.split(" ").join("").length == 0)
        flag = 'N';

    if(flag == 'N')
    {
        alert(msgMandet);
        return false;
    }

    if(flag == 'Y')
    {
        var pars = Form.serialize($(frmObj));
        var xmlHttpReq=createXHR();

        xmlHttpReq.open('GET',login_page + ((login_page.indexOf('?') > 0) ? '&' : '?') + pars,true);
        xmlHttpReq.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
        xmlHttpReq.onreadystatechange=function() {
            if(xmlHttpReq.readyState == 4) {
                if(xmlHttpReq.responseText == "success")
                    location.href = returnURL;
                else if(xmlHttpReq.responseText == "toHomePage")
                    location.href="home.php";
                else
                    idiv('msgLoginError').innerHTML = '<table width="100%" border="0" cellspacing="3" cellpadding="0"><tr><td height="27" class="message">' + xmlHttpReq.responseText + '</td></tr></table>';
            }
        }
        xmlHttpReq.send(null);
    }
    return false;
}

/** Function to change the video status. */
function changeVideoStatus(idAd,videoStatus,idReporter)
{
    var reporterSuffix;

    if(confirm(msgChangeStatus))
    {
        if(idReporter == '')
        {
            reporterSuffix='';
            idReporter='';
        }
        else
            reporterSuffix='_'+idReporter;

        var pars='id_boat='+idAd+'&video_status='+videoStatus+'&id_reporter='+idReporter+'&ajxifrm=A';
        AjaxUpdater('vStatus'+idAd+reporterSuffix,_su+'/extra/admin/changeVideoStatus.php?'+pars);
    }
    else
        return;
}

/** Function for confirmation when video ad is delete. */
function confirmVideoDelete(ref)
{
    if(confirm(msgDeleteVideoConfirm))
        window.location.replace(ref);
}

/**
Function to play the video at given id of element.
id_element=>id of element where to display the video.
videoUrl=>viewVideo.php file.
*/
function playVideo(id_element,videoUrl)
{
    AjaxUpdater(id_element,videoUrl+((videoUrl.indexOf('?') > 0) ? '&' : '?')+'ajxifrm=A');
}

//      Check send link form
function checkFL()
{
    if(isblank(document.sendlink.name.value))
    {
        alert(msgFullFLname);
        document.sendlink.name.focus();
        return false;
    }
    if(!document.sendlink.name.value.match(/^[a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´_\s-]{1,50}$/))
    {
        alert(msgFullFLname);
        document.sendlink.name.focus();
        document.sendlink.name.select();
        return false;
    }
    if(!emailInvalid(document.sendlink.userEmail.value))
    {
        alert(msgInvalidEmail);
        document.sendlink.userEmail.focus();
        return false;
    }
    if(isblank(document.sendlink.email.value))
    {
        alert(msgInvalidEmail);
        document.sendlink.email.focus();
        return false;
    }
    s = document.sendlink.email.value;
    removeSpace = s.replace(/ /g,"");
    str1=removeSpace.split(",");

    if(str1.length > 10)
    {
        alert(msgMoreThan10Mail);
        document.sendlink.email.focus();
        return false;
    }
    else
    {
        for(var m=0; m<str1.length; m++)
        {
            if(!emailInvalid(str1[m]))
            {
                alert(msgInvalidEmail);
                document.sendlink.email.focus();
                return false;
            }
        }
    }
    return true;
}

function checkedAll(val, frm, nm)
{
    var len=frm.elements.length;
    for(i=0; i<len; i++)
    {
        if(frm.elements[i].type == 'checkbox' && frm.elements[i].name==nm)
        {
            frm.elements[i].checked = val;
        }
    }
}

/**
Function to change the model as per make and engine_model as per model using ajax.
1.  id_make         =form object of make.
2.  id_sub_type     =boat subtype
3.  curval_make     =Current value of make
4.  curval_make     =Current value of subtype
5.  chgFrom         =change make and/or model from posing/simple search/adv search(POST/SIMP/ADV/N/OM)
6.  chgFor          =change for type/make/model.('TP'/'MK'/'MD')
7.  admin           =Function calling from admin or client side if admin pass '1'.
8.  exOpt           =Pass the extra option.
                    1. 'nxSrc'=> Calling from Nettix search.
                    2. 'carawall'=>Calling from carawall search
9.  id_boat_type    =form object of boat class.
10. vehicleStatus   =boat Status
*/
function ajaxMakeModelChange(id_make,id_sub_type,curval_make,curval_sub_type,chgFrom,chgFor,admin,exOpt,id_boat_type,vehicleStatus)
{
    var idSubType='';

    if(!vehicleStatus)
        var vehicleStatus='';

    if(id_sub_type)
        idSubType=id_sub_type.value;

    /** Parse the required variables those are used in ajaxMakeModelChange.php file. */
    var ajaxPars='idMake='+id_make.value+'&idSubType='+idSubType;
        ajaxPars +='&curvalMake='+curval_make+'&curvalSubType='+curval_sub_type;
        ajaxPars +='&chgFrom='+chgFrom+'&chgFor='+chgFor+'&admin='+admin+'&exOpt='+exOpt;
        ajaxPars +='&idBoatType='+id_boat_type.value+'&vehicleStatus='+vehicleStatus;

    var ajaxFname='ajaxMakeSubTypeChange.php?ajxifrm=A';

    /** Display image loader. */
    if(chgFrom == 'SIMP')
    {
        if(chgFor == 'TP')
            idiv('loadingImg_src_make').style.display='inline';
    }

    var xmlHttpReq=createXHR();

    xmlHttpReq.open('GET',_su+'/'+ajaxFname+'&'+ajaxPars);
    xmlHttpReq.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
    xmlHttpReq.onreadystatechange=function()
    {
        if(xmlHttpReq.readyState == 4)
        {
            showResponse(xmlHttpReq.responseXML);

            /** Hide image loader. */
            if(chgFrom == 'SIMP')
            {
                if(chgFor == 'TP')
                    idiv('loadingImg_src_make').style.display='none';
            }
        }
    }
    xmlHttpReq.send(null);
    return false;
}

/** This function is return whole url with append of selected search criteria*/
function cleanFrmUrl(qs)
{
    var l =qs.elements.length;
    var u='';

    u=u.replace(/\s/g,'-').toLowerCase();

    for(var i = 0; i < l; i++)
    {
        var e=qs.elements[i];

        if(e.name == 'search' || e.name == 'qsm')
           continue;

        if(e.type == 'select-multiple')
        {
            var si=new Array();
            var count=0;
            var k;
            while (e.selectedIndex != -1)
            {
                if (e.selectedIndex != 0)
                {
                    u=u+e.name+'='+e.options[e.selectedIndex].value+'&';
                    si[count++]=e.selectedIndex;
                }
                e.options[e.selectedIndex].selected=false;
            }

            for(k=0; k<si.length; k++)
                e.options[si[k]].selected=true;
        }
        else if(((e.type == 'checkbox' || e.type == 'radio') && e.checked && e.value))
        {
            u=u+e.name+'='+e.value+'&';
        }
        else if(e.value && !isblank(e.value) && e.type != 'checkbox' && e.type != 'radio')
        {
            if(e.value.indexOf('#'))
                u=u+e.name+'='+escape(e.value)+'&';
            else
                u=u+e.name+'='+e.value+'&';
        }
    }

    return u.substr(0,u.length-1);
}

/** Function to check whether given string is really empty or not */
function isblank(s)
{
    for(var i=0; i < s.length; i++)
    {
        var c=s.charAt(i);

        if((c != ' ') && (c != "\n") && (c != "\t"))
            return false;
    }
    return true;
}

function callAjax(frmObj)
{
    var ajaxFName;
    /** 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(frmObj.name == 'adv')
    {
        checkFa();
        return ajaxCounterChng(ajaxFName+'&frm=ADV&'+ cleanFrmUrl(frmObj),frmObj.name);
    }
    else if(frmObj.name == 'br_search')
    {
        checkBrFa();
        return ajaxCounterChng(ajaxFName+'&frm=BR&'+ cleanFrmUrl(frmObj),frmObj.name);
    }
    else if(frmObj.name == 'srch' || frmObj.name == 'srch_4x4car')
    {
        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' || frmName == 'br_search')
        loadingImg='loadingImg_src_chngCntAdv';
    else if(frmName == 'srch')
        loadingImg='loadingImg_src_chngCnt';
    else if (frmName == 'srch_4x4car' )
        loadingImg='loadingImg_car';
    idiv(loadingImg).style.display='inline';

    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 to change part type and part category and vehicle type and related make model using Ajax.
*/
function ajaxCarMkMdlChange(id_carMake,id_carModel,chgFor,curVal)
{
    var divName;

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

    /** Parse the required variables those are used in ajaxPartSearch.php file. */
    var ajaxPars='id_carMake='+id_carMake.value+'&id_carModel='+id_carModel.value;
        ajaxPars +='&chgFor='+chgFor+'&curSelVal='+curVal;

    if(chgFor == 'MK')
        divName='carModel';

    AjaxUpdater(divName,_su+'/ajaxCarSearch.php?ajxifrm=A&'+ajaxPars,'idiv("loadingImg_car").style.display="none";');
}
function yamahaCallAjax(boatType,subType,idMake,dealer_id)
{
    var ad_condition='';

    if (subType != '')
        ad_condition='&subType='+subType.value;
    if (idMake != '')
        ad_condition=ad_condition+'&idMake='+idMake.value;

    var ajaxFName=_su + '/ajaxClass.php?boatType='+boatType.value+'&dealer_id='+dealer_id+ad_condition;
    return ajaxClassChng(ajaxFName,'srch');
}

function ajaxClassChng (searchUrl,frmName)
{
    var ajaxPars='';
    var ajaxFname=searchUrl.replace('/#/ig','@');
    new Ajax.Request(ajaxFname,{ parameters:ajaxPars,
                                method: 'get',
                                contentType: 'text/html',
                                onSuccess: function(transport)
                                    {
                                        showTextResponse(transport.responseText);
                                    }
                                }
                    );
    return false;
}
