/* $Id: nm_common.js,v 1.6 2010/02/02 10:04:09 vijay Exp $ */

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

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

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

    if(v1 && v1.options[v1.selectedIndex].value == 22)
    {
        v1.selectedIndex=0;

        boatUrl3=boatUrl.replace(/&amp;/g,'&');
        boatUrl=boatUrl3;

        if(cond != 'POST')
        {
            if(boatUrl.match(/login\.php/))
                boatUrl2=boatUrl.replace(/\/login(.*)/,'/listBoat.php');
            else
                boatUrl2=boatUrl+'/listBoat.php';

            window.open(boatUrl2);
        }
        else
            window.open(boatUrl+'&back=addEditBoatImage.php');

        return;
    }
    else if(v1 && v1.options[v1.selectedIndex].value == 20)
    {
        v1.selectedIndex=0;

        varaosaUrl3=varaosaUrl.replace(/&amp;/g,'&');
        varaosaUrl=varaosaUrl3;

        if(cond != 'POST')
        {
            if(varaosaUrl.match(/login\.php/))
                varaosaUrl2=varaosaUrl.replace(/\/login(.*)/,'/listPart.php');
            else
                varaosaUrl2=varaosaUrl+"/listPart.php";

            window.open(varaosaUrl2);
        }
        else
            window.open(varaosaUrl+'&back=addEditPartImage.php');

        return;
    }
    else if(v1 && v1.options[v1.selectedIndex].value == 21)
    {
        v1.selectedIndex=0;

        autoUrl3=autoUrl.replace(/&amp;/g,'&');
        autoUrl=autoUrl3;

        if(cond != 'POST')
        {
            if(autoUrl.match(/login\.php/))
                autoUrl2=autoUrl.replace(/\/login(.*)/,'/listVehicle.php');
            else
                autoUrl2=autoUrl+'/listVehicle.php';

            window.open(autoUrl2);
        }
        else
            window.open(autoUrl+'&back=addEditVehicleImage.php');

        return;
    }

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

    // While posting, if vehicle type is changed change Make and Model as well
    if(cond == 'POST' && v3.length > 0)
        v3.length=1;
    else if(cond == 'Y') // Simple Search
        v3.length=1;

    if(v1)
    {
        if(v1.length)
        {
            if(v1.selectedIndex > -1)
                vehicleid=new String(v1.options[v1.selectedIndex].value);
            else
                vehicleid='';
        }
        else
            vehicleid=v1;
    }
    textureName='';

    if(vehicleid != '')     // Vehicle Type is selected
    {
        if(cond == 'Y' || (v2.length > 0 && v2.options[0].value == ''))
        {
            v2.length=1;
            var j=1;
        }
        else                 // Posting Vehicle
        {
            v2.length=0;
            var j=0;
        }
        if(v2.length != 0)
            v2.selectedIndex=0;

        // for(i=0; i < e[vehicleid].length; i++)
        for(i=0; i < m1.length; i++)
        {
            // var make_value=new String(e[vehicleid][i]);
            // var make_text=new String(f[vehicleid][i]);
            goFlag=0;
            arrMakeInfo=(m1[i]).split('#');
            arrMakeTypeInfo=(arrMakeInfo[3]).split(',');

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

            // if(cond == 'POST' || a[make_value] > 0)     // Posting Vehicle && Simple Search
            var make_value=arrMakeInfo[0];
            var make_text=arrMakeInfo[1];

            if(cond == 'POST' || arrMakeInfo[2] > 0)     // Posting Vehicle && Simple Search
            {
                /** OPTION GROUP PART STARTS HERE */

                /* changes has been made for displaying makes in option group wise.
                    i.e. Starting with char A will listed below A Group, same as for B to Z.*/
                if(make_text.match(/^[A-Za-zАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя]{1}/) && (!(navigator.userAgent.match(/Opera/))) && bflag == 1)
                {
                    optChar='';
                    optChar=make_text.substr(0,1);
                    optGroup='T';
                    if (textureName != optChar)
                    {
                        textureName = optChar;
                        textureGroup = document.createElement ('optgroup');
                        textureGroup.label = textureName;
                        v2.appendChild(textureGroup);
                    }
                }
                else
                {
                    optGroup='F';
                }

                if (optGroup != 'F' && textureName != '')
                {
                    make_opt = document.createElement('option');
                    make_opt.value = make_value;
                    if (navigator.userAgent.match(/IE/))
                        make_opt.innerText=make_text;
                    else
                        make_opt.text = make_text;
                    textureGroup.appendChild(make_opt);
                }
                else
                {
                    makeOpt=new Option(make_text,make_value,false,false);
                    eval("v2.options[j]=makeOpt");
                }

                /** OPTION GROUP PART ENDS HERE */

                if(id_make == make_value)
                    v2.selectedIndex=j;

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

        if(cond == 'POST' && v2.length > 0) // Posting Vehicle
            makeChange(v2,v3,'','',v1);
    }
    else
    {
        // When Vehicle Type is not selected (Simple Search)
        v2.length=1;
        var j=1;

        for(i=0; i < m1.length; i++)
        {
            // if(a[u[z]] > 0 || cond == 'ADV')
            arrMakeInfo=(m1[i]).split('#');

            if(arrMakeInfo[2] > 0 || cond == 'ADV')
            {
                /** OPTION GROUP PART STARTS HERE */
                var make_value=arrMakeInfo[0];
                var make_text=arrMakeInfo[1];

                /* changes has been made for displaying makes in option group wise.
                    i.e. Starting with char A will listed below A Group, same as for B to Z.*/
                if(make_text.match(/^[A-Za-zАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя]{1}/) && (!(navigator.userAgent.match(/Opera/))) && bflag == 1)
                {
                    optChar='';
                    optChar=make_text.substr(0,1);
                    optGroup='T';
                    if (textureName != optChar)
                    {
                        textureName = optChar;
                        textureGroup = document.createElement ('optgroup');
                        textureGroup.label = textureName;
                        v2.appendChild(textureGroup);
                    }
                }
                else
                {
                    optGroup='F';
                }

                if (optGroup != 'F' && textureName != '')
                {
                    make_opt = document.createElement('option');
                    make_opt.value = make_value;
                    if (navigator.userAgent.match(/IE/))
                        make_opt.innerText=make_text;
                    else
                        make_opt.text = make_text;
                    textureGroup.appendChild(make_opt);
                }
                else
                {
                    makeOpt=new Option(make_text,make_value,false,false);
                    eval("v2.options[j]=makeOpt");
                }
                /** OPTION GROUP PART ENDS HERE */

                // if(id_make == u[z])
                if(id_make == make_value)
                    v2.selectedIndex=j;

                j++;
            }
        }
    }
}

// Used in Simple Search, Advance Search and Vehicle Post forms.
function makeChange(v1,v2,curval,cond,v3)
{
    var vehicleid='';

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

    if(v1.selectedIndex > -1)
        var makeid=new String(v1.options[v1.selectedIndex].value)
    else
        var makeid='';

    if(v3)
    {
        if(v3.length)
        {
            if(v3.selectedIndex > -1)
                vehicleid=new String(v3.options[v3.selectedIndex].value)
            else
                vehicleid='';
        }
        else
            vehicleid=v3;
    }
    if(makeid != '')
    {
        if(cond == 'Y' || (v2.length > 0 && v2.options[0].value == ''))
        {
            v2.length=1;
            var j=1;
        }
        else    // Posting Vehicle
        {
            v2.length=1;
            var j=1;
        }
        if(cond == 'ADV')
        {
            // Remove first element which is just blank
            v2.length=0;
            var j=0;
        }
        if(v2.length != 0)
            v2.selectedIndex=0;

        if(cond == 'ADV')
        {
            if(m[makeid].length > 0)
            {
                var make_value=v1.options[v1.selectedIndex].value+"#"+v1.options[v1.selectedIndex].text;
                var make_text=v1.options[v1.selectedIndex].text+": "+anyModel;

                modelOpt=new Option(make_text,make_value,false,false);
                eval("v2.options[j++]=modelOpt");
            }
        }
        // for(i=0; i < m[makeid].length; i++)
        arrModel=(m[makeid]).split(',');

        for(i=0; i < arrModel.length; i++)
        {
            // var model_value=new String(m[makeid][i]);
            // var model_text=d[model_value];
            var model_id=arrModel[i];
            arrModelInformation=(c[model_id]).split('#');
            var model_value=model_id;
            var model_text=arrModelInformation[0];
            var goFlag=0;

            // Simple Search
            if(cond == 'Y')
            {
                // When Vehicle Type is also selected then get models for that Vehicle Type only
                if(vehicleid != '')
                {
                    // if(c[model_value][vehicleid] > 0)
                    // goFlag=1;
                    arrVehicleTypeCounter=arrModelInformation[2].split(/[@,]/);

                    for(k=0; k < arrVehicleTypeCounter.length; k++,k++)
                    {
                        if(arrVehicleTypeCounter[k]==vehicleid && arrVehicleTypeCounter[k+1] > 0)
                        {
                            goFlag=1;
                            break;
                        }
                    }
                }
                else
                {
                    // if(c[model_value][0] > 0)
                    if(arrModelInformation[1] > 0)
                        goFlag=1;
                }
            }
            else
            {
                if(cond == 'ADV')
                    goFlag=1;
                // {
                // // if(v[model_value] == document.adv.id_bike_type.value)
                // goFlag=1;
                // }
                if(vehicleid != '')
                {
                    // Vehicle Posting
                    // if (c[model_value][vehicleid] >= 0)
                    // goFlag=1;
                    arrVehicleTypeCounter=arrModelInformation[2].split(/[@,]/);

                    for(k=0; k < arrVehicleTypeCounter.length; k++,k++)
                    {
                        if(arrVehicleTypeCounter[k]==vehicleid && arrVehicleTypeCounter[k+1] >= 0)
                        {
                            goFlag=1;
                            break;
                        }
                    }
                }
                else    // Admin -> Normal Vehicle Search (Without Vehicle Type)
                    goFlag=1;
            }
            if(goFlag == 1)
            {
                modelOpt=new Option(model_text,model_value,false,false);
                eval("v2.options[j]=modelOpt");

                if(id_model == model_value)
                    v2.selectedIndex=j;

                j++;
            }
        }
        if(cond != 'ADV')
        {
            if(v2.selectedIndex == ''  || v2.selectedIndex < 1)
                v2.selectedIndex=0;
        }
    }
    else
    {
        v2.length=1;
        var j=1;

        if(cond == 'ADV')
        {
            selOpt=new Option('                                          ','',false,false);
            eval("v2.options[0]=selOpt");
        }
    }
}

// Change subType according to Vehicle Type
function subTypeChange(vtype,subtype,curval,page)
{
    if(vtype)
    {
        if(vtype.length)
        {
            if(vtype.selectedIndex > -1)
                var vehicleid=new String(vtype.options[vtype.selectedIndex].value);
            else
                var vehicleid='';
        }
        else
            var vehicleid=vtype;
    }
    if(page == 'POST')
    {
        subtype.length=1;
        j=1;
    }
    else
    {
        subtype.length=1;
        j=1;
    }
    if(vehicleid != '')
    {
        if(sid[vehicleid])
        {
            for(i=0; i < sid[vehicleid].length; i++)
            {
                var svalue=new String(sid[vehicleid][i]);
                var stext=new String(stype[vehicleid][i]);

                sOpt=new Option(stext,svalue,false,false);
                eval("subtype.options[j]=sOpt");

                if(sid[vehicleid][i] == curval)
                    subtype.selectedIndex=j;

                j++;
            }
        }
    }
    else
    {
        // When Vehicle Type is not selected (Simple Search)
        for(z=0; z < vtype.length; z++)
        {
            if(sid[vtype.options[z].value])
            {
                for(i=0; i < sid[vtype.options[z].value].length; i++)
                {
                    var svalue=new String(sid[vtype.options[z].value][i]);
                    var stext=new String(stype[vtype.options[z].value][i]);

                    sOpt=new Option(stext,svalue,false,false);
                    eval("subtype.options[j]=sOpt");

                    if(curval == svalue)
                        subtype.selectedIndex=j;

                    j++;
                }
            }
        }
    }
}

// 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,false,false);
        eval("v2.options[j]=temp");

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

// Function to change price list box options
function priceChange(v1,v2,curval,sign)
{
    if(sign)
        sign=sign;
    else
        sign="$";

    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

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

    for(i=st,j=1; i < tot; j++,i++)
    {
        if (parseInt(v1.options[i].value,0) >= 1000)
        {
            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+" "+sign;
        }
        else
            var disp=v1.options[i].value+" "+sign;

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

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

// Function to change mileage list box options
function mileageChange(v1,v2,curval)
{
    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

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

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

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

// Function to change engine size list box options
function engineChange(v1,v2,curval)
{
    if(v1.selectedIndex < 1)
        var st=1;
    else
        var st=v1.selectedIndex;

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

    for(i=st,j=1; i < tot; j++,i++)
    {
        temp=new Option(v1.options[i].value,v1.options[i].value,false,false);
        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,0) >= 1000)
        {
            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
            var disp=v1.options[i].value;

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

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

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

// Function which checks whether given email is valid or not
function emailInvalid(s)
{
    if(s.match(/^\w+([\.\+]?[-]{0,2}\w+)*[\.]?@\w+([\.-]?\w+)*(\.\w{2,4})+$/i))
        return true;
    else
        return false;
}

// Function which checks whether given url address is valid or not
function urlInvalid(s)
{
    if(!(s.match(/^http:\/\/[a-zA-Z0-9]+([_|\.-][a-zA-Z0-9~-]{1,})*\.([a-z]{2,4})(\/[a-zA-Z0-9~-]{1,}[_|\.]{0,1}[a-zA-Z0-9~]{1,}\/?)*([_|\.][a-zA-Z0-9~-]{1,})*$/i) || s.match(/^ftp:\/\/[a-zA-Z0-9]+([_|\.][a-zA-Z0-9~-]{1,})*\.([a-z]{2,4})(\/[a-zA-Z0-9~-]{1,}[_|\.]{0,1}[a-zA-Z0-9~]{1,}\/?)*([_|\.][a-zA-Z0-9~]{1,})*$/i)))
        return false;
    else
        return true;
}

// Function to open new pop up window with fixed size
function openWin(win_nm,nm)
{
    var h=screen.height;
    h=h-80;

    var myfeatures="directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width=730,height="+h+",left=0,top=0";

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

    var newImg=window.open(win_nm,nm,myfeatures);
    newImg.focus();
}
function openNewWin(win_nm,w,h,nm)
{
    var myfeatures='directories=no,location=no,menubar=no,status=yes,titlebar=no,toolbar=no,resizable=yes,scrollbars=no,width='+w+',height='+h+',left=0,top=0';

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

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

// 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=yes,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 navigation
function submitNav(pno,sort,ono)
{
    document.nav.page.value=pno;
    document.nav.sortCol.value=sort;
    document.nav.ord.value=ono;
    document.nav.submit();
}

// This function is used to switch between imaeg and text version
// It used toggling method, hence if Image is set then set Text and vice versa
function switchVersion()
{
    if(Get_Cookie('imgCook') == 'T')
        Set_Cookie('imgCook','','','','','');
    else
    {
        var now = new Date();
        now.setDate(now.getDate() + 1);// Extend time for 1 day

        Set_Cookie('imgCook','T',now.toGMTString(),'','','');
    }

    window.location.reload(true);
}

// Function to suhmit favorite page by adding/removing vehicles
function submitFav(thisV)
{
    var flag='N';

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

    document.fav.submit();
}

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

// Sort displaying vehicle list page according to specified arguments.
function sortForm(col,ord)
{
    document.sortl.sortCol.value=col;
    document.sortl.ord.value=ord;
    document.sortl.submit();
}

// To popup message while removing single records
function confirmDel(ref)
{
    if(confirm(msgDeleteConfirm))
        window.location.replace(ref);
}

// To popup message while removing multiple records
function confirmMultiDel(act,frm)
{
    if(confirm(msgDeleteConfirm))
    {
        if (act != '')
            eval("document."+frm+".action="+'act');

        eval("document."+frm+".submit()");
    }
}

// Script area for dynamic banner display
var zg=0;
var banid=new Array();
var links=new Array();
var alt=new Array();
var duration=new Array();
var target=new Array();

// Part of rotating banner module. This is main function
// which rotates banners by certain intevral as specified
// in duration array.
function showBanner(whatToDo,whereFrom)
{
    if(whatToDo == 'show')
    {
        var whenToCall=1000;

        // Run this script only when there exists local image
        if(document.getElementById('netBanner'))
        {
            // Get random banner ID, but when there is default banner, use only that
            if(banid.length > 1)
            {
                do
                {
                    zg=Math.floor(Math.random() * banid.length)
                }
                while(zg < 1)

                whenToCall=duration[zg];

                // When VIF is viewed from ODS / TDS do not show commercial banners
                // Hence when url link matches 'netti' only then show that banner.
                if(whereFrom == 'odstds')
                {
                    if(links[zg].match(/http:\/\/(www.)?(netti[a-zA-Z]{1,15})\.([a-z]{2,4})/i))
                        document.netBanner.src=eval('netBanner'+zg+'.src');
                    else
                        whenToCall=1;
                }
                else
                    document.netBanner.src=eval('netBanner'+zg+'.src');

                window.setTimeout('showBanner(\'show\',\''+whereFrom+'\');',whenToCall);
            }
            else
                zg=0;
        }
    }
    if(whatToDo == 'golink')
    {
        if(links[zg])
        {
            str='bannerCounter.php?id='+banid[zg]+'&url='+links[zg];

            if(target[zg] == 'B')
                window.open(str);
            else
                window.location.replace(str);
        }
    }
    if(whatToDo == 'showinfo')
    {
        if(alt[zg])
        {
            window.status=links[zg];
            document.netBanner.title=alt[zg];
        }
    }
    if(whatToDo == 'removeinfo')
        window.status='';
}

// Part of rotating banner module, to open url in new or same
// window as specified in links array.
function clickLink()
{
    if(links[zg])
    {
        str="bannerCounter.php?id="+banid[zg]+"&url="+links[zg];

        if (target[zg] == 'B')
            window.open(str);
        else
            self.location.replace(str);
    }
}

// Part of rotating banner module, to show alter text on windows's status bar
function descript()
{
    if(alt[zg])
    {
        window.status=alt[zg];

        if ((navigator.appName == 'Netscape') && (parseFloat(navigator.appVersion.substr(0,3)) < 5.0))
        {
            document.netBan.visibility='show';
            text='';
            text=alt[zg];

            document.netBan.document.write('<layer id="id1" top="20" left="450" bgcolor="#f7f7d3" style="border:0px solid white; font-family: verdana; font-size:11px; color:black;">'+text+'</layer>');
        }
        else
            document.banner.title=alt[zg];
    }
}

// Part of rotating banner module, to remove alter text from windows's status bar
function remove()
{
    window.status='';

    if ((navigator.appName == 'Netscape') && (parseFloat(navigator.appVersion.substr(0, 3)) < 5.0))
        document.netBan.visibility='hide';
}

// Function to add selected values from 'Model' fields
// into 'Selected models' field, used in Advanced search page
function addModel(v1,v2)
{
    if(!(v1.id_model.selectedIndex >= 0))
        return;

    if(v1.id_model[v1.id_model.selectedIndex].value != '')
    {
        if(v2.options[0].value == '')
            document.adv.id_sel_model.length--;

        if(v2.selectedIndex == 1)
            v2.selectedIndex=0;

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

        for(o=0; o<tot; o++)
        {
            var tmpValue2=v2.options[o].value;
            var tmpValue=v1.id_model[v1.id_model.selectedIndex].value;

            if(tmpValue == tmpValue2)
            {
                dup=1;
                alert(msgDupSelected);
                break;
            }
            else if(tmpValue.match(/^([0-9]{1,})$/i) && tmpValue2.match(/^([0-9]{1,})#/i))
            {
                var makeid=tmpValue2.substr(0,tmpValue2.indexOf('#'));
                var make=tmpValue2.substr(tmpValue2.indexOf('#')+1,tmpValue2.length);

                arrModel=(m[makeid]).split(',');

                // for(i=0; i < m[makeid].length; i++)
                for(i=0; i < arrModel.length; i++)
                {
                    // var model_value=new String(m[makeid][i]);
                    // // if(tmpValue == model_value)
                    if(tmpValue == arrModel[i])
                    {
                        dup=1;
                        alert(makeAllSelected);
                        break;
                    }
                }
            }
            else if(tmpValue.match(/^([0-9]{1,})#/i))
            {
                var makeid=tmpValue.substr(0,tmpValue.indexOf('#'));
                var make=tmpValue.substr(tmpValue.indexOf('#')+1,tmpValue.length);

                // for(i=0; i < m[makeid].length; i++)
                for(i=0; i < arrModel.length; i++)
                {
                    // var model_value=new String(m[makeid][i]);
                    var model_value=new String(arrModel[i]);

                    for(h=0; h<tot; h++)
                    {
                        var v2Value=v2.options[h].value;

                        if(v2Value == model_value)
                        {
                            var j=0;

                            for(var r=h; r<tot;r++)
                            {
                                j=r+1;

                                if(j > (tot-1))
                                    j=tot-1;

                                var disp=v2.options[j].text;
                                var val=v2.options[j].value;

                                selOpt=new Option(disp,val,false,false);
                                eval("v2.options[r]=selOpt");
                            }
                            v2.length=tot-1;
                            tot=v2.length;
                        }
                    }
                }
            }
        }
        if(dup != 1)
        {
            if((v1.id_model[v1.id_model.selectedIndex].value).match(/^([0-9]{1,})#/i) )
                selOpt=new Option(v1.id_model[v1.id_model.selectedIndex].text,v1.id_model[v1.id_model.selectedIndex].value,false,false);
            else
            {
                str1=v1.id_model[0].text;
                selOpt=new Option(str1.slice(0,str1.indexOf(":"))+"-"+v1.id_model[v1.id_model.selectedIndex].text,v1.id_model[v1.id_model.selectedIndex].value,false,false);
            }

            eval("v2.options[tot]=selOpt");
        }
    }
}

// Function to remove selected values from 'Selected Models'
// fields used in Advanced search page
function removeModel(v1)
{
    var p=v1.id_sel_model.selectedIndex;
    var j=0;
    var tot=v1.id_sel_model.options.length;

    if(tot > p)
    {
        for(i=p; i<=tot; i++)
        {
            j=i+1;

            if(j > (tot-1))
                j=tot-1;

            var disp=v1.id_sel_model[j].text;
            var val=v1.id_sel_model[j].value;

            selOpt=new Option(disp,val,false,false);
            eval("v1.id_sel_model.options[i]=selOpt");
        }
    }
    v1.id_sel_model.length=tot-1;

    // If no option is there then add "any" as default
    if(v1.id_sel_model.options.length == 0)
    {
        selOpt=new Option('                                                      ','',false,false);
        eval("v1.id_sel_model.options[0]=selOpt");
    }
}

// This function check only alfa numeric value not allowed special characters
function checkAlfaNumeric(s)
{
    if(!isblank(s))
    {
        if(!s.match(/^[a-zA-Z0-9АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя]*$/))
            return false;
        else
            return true;
    }
}

// Function to swap image (used in Map etc.)
function swapImage(imgname,path,carid,imageid)
{
    carid.value=imageid;
    imgname.src=path;
}

// Function to change full image of VIF etc.
function loadImage(newimg)
{
    document.getElementById('fullimg').src=newimg;
}

// 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)
{
    /* Check. User is registered or not */
    if(file.id_user.value == '')
    {
        if(from == 'O')
            openImg('../../popUpLogin.php?M=OnlyForRegisteredUser',350,320,'nm_login');
        else
            openImg('popUpLogin.php?M=OnlyForRegisteredUser',350,320,'nm_login');

        return false;
    }
    var flag='N';

    for(i=0; i < file.elements.length;i++)
    {
        if(file.elements[i].type == 'text')
        {
            if(!isblank(file.elements[i].value))
                flag='Y';
        }
    }
    if(flag == 'N')
    {
        alert(msgBlankForm);
        return false;
    }
}

// Function to check phone,fax,mobile for finland country
function check(s)
{
    if(!s.match(/^[0-9\s\+\(\)-]{5,25}$/) )
        return false;
    else
        return true;
}

// Function to check loginid string, various rules are followed
function checkLogin(s)
{
    if(!(s.match(/^[a-zA-Z0-9АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_]{4,12}$/)))
        return msgUserid;
    else
    {
        s=s.toLowerCase();

        if(s == 'admin' || s == 'webmaster' || s == 'help' || s == 'helpdesk' || s == 'nettiauto' || s == 'user' || s == 'dealer' || s == 'dummy')
            return msgResdId;
    }
    return 1;
}

// Function to check Password string, various rules are followed
function checkPasswd(s)
{
    if(s.match(/^[a-zA-Z0-9АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_.-]{4,12}$/))
        return true;

    return false;
}

// Function to check characters length of word
function isLong(txtVal,sep,len)
{
    if (txtVal == '')
        return true;
    else
    {
        if(sep ==  ' ')
            tempValue=txtVal.split(/\s+/);
        else
            tempValue=txtVal.split(sep);

        for(var i=0; i < tempValue.length; i++)
        {
            if(tempValue[i].length > len)
            {
                return 0;
                break;
            }
        }
    }
}

// Function to check SSN number validation in Finland country
function checkSsn(p)
{
    lChar=new Array('A','B','C','D','E','F','H','J','K','L','M','N','P','R','S','T','U','V','W','X','Y','Z');

    if(!(p.match(/^[0-9]{6}-[0-9]{3}[0-9a-zA-Z]{1}$/)))
        return false;
    else
    {
        ssn=p.replace('-','');
        first=ssn.substr(0,9);
        start1=ssn.substr(0,6);
        middle=ssn.substr(6,3);
        last=ssn.substr(9,1);

        remin=(first % 31);

        if(remin >= 0 && remin <= 9)
            rightChar=remin;
        else
            rightChar=lChar[remin-10];

        rightSSN=start1+"-"+middle+rightChar;

        if(rightSSN == p)
            return true;
        else
            return false;
    }
}

// Function to get stored cookie
function Get_Cookie(name)
{
    var start=document.cookie.indexOf(name+"=");
    var len=start+name.length+1;

    if ((!start) && (name != document.cookie.substring(0,name.length)))
        return null;

    if (start == -1)
        return null;

    var end=document.cookie.indexOf(';',len);

    if (end == -1)
        end=document.cookie.length;

    return unescape(document.cookie.substring(len,end));
}

// Function to set new cookie
function Set_Cookie(name,value,expires,path,domain,secure)
{
    document.cookie=name+"="+escape(value)+((expires)?";expires="+expires:"")+((path)?";path="+path:"")+((domain)?";domain="+domain:"")+((secure)?";secure":"");
}

// Function to delete cookie
function Delete_Cookie(name,path,domain)
{
    if(Get_Cookie(name))
        document.cookie=name+"="+((path)?";path="+path:"")+((domain)?";domain="+domain:"")+";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

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

// Function to check the alphanumeric characters.
function checkAlphaNumeric(s)
{
    if(!s.match(/^[0-9a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя\s-]{1,100}$/))
        return false;
    else
        return true;
}

// Function to check First-Last name values when it is entered as full name
function checkFullName(s)
{
    if(!s.match(/^[a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_\s-]{1,50}$/))
        return false;
    else
        return true;
}

// Function to check First-Last name values
function checkName(s)
{
    if(!s.match(/^[a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя-]{1,30}$/))
        return false;
    else
        return true;
}

// Function to check hobby/reference values
function checkHobbyRef(s)
{
    if(!s.match(/^[0-9a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_:;@()\s\+\,'.-]{1,250}$/))
        return false;
    else
        return true;
}

// Function to check address/contact time values
function checkAddContTime(s)
{
    if(!s.match(/^[0-9a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_:()\s\,'.!-]{1,250}$/))
        return false;
    else
        return true;
}

// Function to check city values
function checkCity(s)
{
    if(!s.match(/^[0-9a-zA-ZАаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя_:\s\,-]{1,250}$/))
        return false;
    else
        return true;
}

// Function to check zip values
// function checkZip(s)
// {
// if(!s.match(/^[0-9a-zA-Z]{1,250}$/))
// return false;
// else
// return true;
// }

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 confirmReset(thisForm)
{
    if(confirm(msgConfirmReset))
        return true;

    return false;
}

// function to change town list according the selected domicile.
function townChange(ptype,town,curval,page) // partTownChange
{
    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 == '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,false,false);
                eval("town.options[j]=cOpt");

                if(tId[idPart][i] == curval)
                    town.selectedIndex=j;
                j++;
            }
        }
    }
    else
    {
        j=1;
        town.length=1;
    }
}

function changeLanguage(frm)
{
    if(frm.sitelang.value == 1)
        frm.sitelang.value=2;
    else
        frm.sitelang.value=1;

    frm.submit();
    return false;
}

// Function to suhmit favorite page to compare vehicles.
function compareVehicle()
{
    var cnt=flag=0;

    for(var i=1; i < document.fav.length; i++)
    {
        if(document.fav.elements[i].type == 'checkbox' && document.fav.elements[i].checked)
        {
            cnt++;
            continue;
        }
    }
    if(cnt < 2 || cnt > 4)
    {
        alert(msgSelMinTwoVeh);
        return false;
    }
    else
    {
        document.fav.action='compareVehicles.php';
        document.fav.submit();
    }
}

function changeClass(id,cls)
{
    if(document.getElementById(id).className=='fieldheader')
        document.getElementById(id).className=cls;
    else
        document.getElementById(id).className='fieldheader';
}

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(/NM/))
                    {
                        cvalue=new String(sId[idCat][i]);
                        ctext=new String(sName[idCat][i]);

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

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