/* $Id: nv_boatValidation.js,v 1.44 2011/11/07 05:46:13 bakul Exp $ */

function checkAllVehicle(theForm,isAdmin)
{
    if(theForm.id_boat_type.value == 14 && theForm.id_sub_type.selectedIndex == 0)
    {
        document.getElementById('id_sub_type').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capSubtype);
        theForm.id_sub_type.focus();
        return false;
    }
    if(theForm.id_make.selectedIndex == 0)
    {
        document.getElementById('id_make').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capMk);
        theForm.id_make.focus();
        return false;
    }
    if(theForm.id_boat_type.value == 14 && theForm.mfg_year.selectedIndex == 0)
    {
        document.getElementById('mfg_year').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capYModel);
        theForm.mfg_year.focus();
        return false;
    }
    if(!isblank(theForm.model.value) && !theForm.model.value.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´\/_\s-,\.\+\*\&\(\)]{1,30}$/))
    {
        document.getElementById('model').className='highlight';
        alert(msgModel);
        theForm.model.select();
        theForm.model.focus();
        return false;
    }
    if(!isblank(theForm.price_ask.value))
    {
        if(theForm.bestsold.checked)
        {
            document.getElementById('price_ask').className='highlight';
            alert(msgInvalidEntry);
            theForm.price_ask.focus();
            return false;
        }
        else
        {
            if(!(theForm.price_ask.value.match(/^[0-9]{1,10}$/)) || parseInt(theForm.price_ask.value) < 1)
            {
                document.getElementById('price_ask').className='highlight';
                alert(msgInvalidPrice);
                theForm.price_ask.select();
                theForm.price_ask.focus();
                return false;
            }
            if(theForm.id_boat_type.value != 14 && (parseInt(theForm.price_ask.value) >= 1) && (parseInt(theForm.price_ask.value) <= 50) && theForm.ad_type.value != "R")
            {
                document.getElementById('price_ask').className='highlight';
                alert(msgPrice);
                theForm.price_ask.select();
                theForm.price_ask.focus();
                return false;
            }
            if(theForm.id_boat_type.value == 14 && (parseInt(theForm.price_ask.value) >= 1) && (parseInt(theForm.price_ask.value) < 20) && theForm.ad_type.value != "R")
            {
                document.getElementById('price_ask').className='highlight';
                alert(msgPrice);
                theForm.price_ask.select();
                theForm.price_ask.focus();
                return false;
            }
        }
    }
    else
    {
        if(!theForm.bestsold.checked)
        {
            document.getElementById('price_ask').className='highlight';
            if(theForm.ad_type.value == "R")
                alert(msgInvalidRentPrice);
            else
                alert(msgInvalidEntry);
            theForm.price_ask.focus();
            return false;
        }
    }
    if(theForm.nett_price)
    {
        if(!isblank(theForm.nett_price.value))    /* If nett price is not blank */
        {
            if(!(theForm.nett_price.value.match(/^[0-9]{1,10}$/)) || parseInt(theForm.nett_price.value) < 1)
            {
                document.getElementById('nett_price').className='highlight';
                alert(msgInvalidPrice);
                theForm.nett_price.focus();

                try { pageTracker._trackEvent('Virheet','Rekisteröinti','nett_price');} catch(err) {}

                return false;
            }
        }
    }

    if(theForm.warranty_mon && theForm.warranty_mon.value != '')
    {
        if( isNaN(theForm.warranty_mon.value) || isblank(theForm.warranty_mon.value) )
        {
            document.getElementById('warranty').className='highlight';
            alert(msgInvalidWarranty);
            theForm.warranty_mon.focus();
            return false;
        }

        if (parseInt(theForm.warranty_mon.value) < 1)
        {
            document.getElementById('warranty').className='highlight';
            alert(msgInvalidWarranty);
            theForm.warranty_mon.focus();
            return false;
        }
    }
    if(theForm.warranty_km && theForm.warranty_km.value != '')
    {
        if( isNaN(theForm.warranty_km.value) || isblank(theForm.warranty_km.value) )
        {
            document.getElementById('warranty').className='highlight';
            alert(msgInvalidWarranty);
            theForm.warranty_km.focus();
            return false;
        }

        if (parseInt(theForm.warranty_km.value) < 1)
        {
            document.getElementById('warranty').className='highlight';
            alert(msgInvalidWarranty);
            theForm.warranty_km.focus();
            return false;
        }
    }
    if(theForm.length.value && theForm.length.value != '')
    {
        var jsLength=theForm.length.value.replace(",",".");

        if(isNaN(jsLength) || Math.ceil(jsLength) < 1)
        {
            document.getElementById('length_type').className='highlight';
            alert(msgInvalidLength);
            theForm.length.select();
            theForm.length.focus();
            return false;
        }
        if(jsLength > 99)
        {
            document.getElementById('length_type').className='highlight';
            alert(msgOverLength);
            theForm.length.select();
            theForm.length.focus();
            return false;
        }
    }
    if(theForm.height && theForm.height.value != '')
    {
        var jsHeight=theForm.height.value.replace(",",".");

        if(isNaN(jsHeight) || Math.ceil(jsHeight) < 1)
        {
            document.getElementById('height').className='highlight';
            alert(msgInvalidHeight);
            theForm.height.select();
            theForm.height.focus();
            return false;
        }
        if(jsHeight > 50)
        {
            document.getElementById('height').className='highlight';
            alert(msgOverHeight);
            theForm.height.select();
            theForm.height.focus();
            return false;
        }
    }
    if(theForm.weight && theForm.weight.value != '')
    {
        if(!(theForm.weight.value.match(/^[0-9]{1,10}$/)) || (theForm.weight.value < 1))
        {
            document.getElementById('weight').className='highlight';
            alert(msgInvalidWeight);
            theForm.weight.select();
            theForm.weight.focus();
            return false;
        }
    }
    if(theForm.width && theForm.width.value != '')
    {

        var jsWidth=theForm.width.value.replace(",",".");

        if(isNaN(jsWidth) || Math.ceil(jsWidth) < 1)
        {
            document.getElementById('width').className='highlight';
            alert(msgInvalidWidth);
            theForm.width.select();
            theForm.width.focus();
            return false;
        }
        if(jsWidth > 50)
        {
            document.getElementById('width').className='highlight';
            alert(msgOverWidth);
            theForm.width.select();
            theForm.width.focus();
            return false;
        }
    }
    if(theForm.draft && theForm.draft.value != '')
    {
        var jsDraft=theForm.draft.value.replace(",",".");

        if(isNaN(jsDraft) || Math.ceil(jsDraft) < 1)
        {
            document.getElementById('draft').className='highlight';
            alert(msgInvalidDraft);
            theForm.draft.select();
            theForm.draft.focus();
            return false;
        }
        if(jsDraft > 50)
        {
            document.getElementById('draft').className='highlight';
            alert(msgOverDraft);
            theForm.draft.select();
            theForm.draft.focus();
            return false;
        }
    }
    if(theForm.storage && !isLong(theForm.storage.value,' ',40))
    {
        document.getElementById('storage').className='highlight';
        alert(msgStorage+' '+msgTooLongWord);
        theForm.storage.select();
        return false;
    }
    if(theForm.note.value.length > 2000)
    {
        document.getElementById('note').className='highlight';
        alert(msgNoteLen);
        theForm.note.select();
        return false;
    }

    if(isAdmin != 'Y')
    {
        if(isMaxWord(theForm.note.value," ",80) == 0)
        {
            document.getElementById('note').className='highlight';
            alert(msgLngDescWord);
            theForm.note.select();
            return false;
        }
    }

    if(theForm.sail_note && theForm.sail_note.value.length > 2000)
    {
        document.getElementById('sail_note').className='highlight';
        alert(msgSailNoteLen);
        theForm.sail_note.select();
        return false;
    }
    if(theForm.userType.value != 'D')
    {
        if(theForm.id_domicile.selectedIndex == 0)
        {
            document.getElementById('domicile').className='highlight';
            alert(capPlz+' '+capSel.toLowerCase()+' '+capDom);
            theForm.id_domicile.focus();
            return false;
        }
    }
    if(theForm.id_domicile_ele.selectedIndex != 0 && theForm.id_town.selectedIndex == 0)
    {
        document.getElementById('town').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capTown);
        theForm.id_town.focus();
        return false;
    }
    if(theForm.userType.value == 'D' && theForm.delivery_cost)
    {
        if(!isblank(theForm.delivery_cost.value))
         {
             if(!(theForm.delivery_cost.value.match(/^[0-9]{1,10}$/)) || parseInt(theForm.delivery_cost.value) < 1)
             {
                 document.getElementById('delivery_cost').className='highlight';
                 alert(msgInvalidDelCost);
                 theForm.delivery_cost.select();
                 theForm.delivery_cost.focus();
                 return false;
             }
         }
         else
         {
             if(!theForm.no_delivery_cost.checked)
             {
                 document.getElementById('delivery_cost').className='highlight';
                 alert(msgInvalidDelCostEntry);
                 theForm.delivery_cost.select();
                 theForm.delivery_cost.focus();
                 return false;
             }
         }
    }
    if((document.getElementById('enginechecky') && document.getElementById('enginechecky').checked) || document.getElementById('id_boat_type_ele').value == 8)
    {
        if(theForm.id_eng_make && theForm.id_eng_make.selectedIndex == 0)
        {
            document.getElementById('id_eng_make').className='highlight';
            theForm.id_eng_make.focus();
            alert(capPlz+' '+capSel.toLowerCase()+' '+capEng+' '+capMk);
            return false;
        }
        if(theForm.id_eng_type2.selectedIndex == 0)
        {
            document.getElementById('id_eng_type').className='highlight';
            alert(msgEngineType);
            theForm.id_eng_type2.focus();
            return false;;
        }
        if(theForm.id_eng_type.selectedIndex == 0 && theForm.id_eng_type2.value != "3")
        {
            document.getElementById('id_eng_type').className='highlight';
            alert(msgEngineType);
            theForm.id_eng_type.focus();
            return false;;
        }
        if(theForm.id_eng_fuel_type.selectedIndex == 0 && theForm.id_eng_type2.value != "3")
        {
            document.getElementById('id_eng_fuel_type').className='highlight';
            alert(msgFuelType);
            theForm.id_eng_fuel_type.focus();
            return false;;
        }

        if(theForm.power && theForm.power.value != '')
        {
            if(!(theForm.power.value.match(/^[0-9 x X]{1,10}$/)) || (theForm.power.value < 1))
            {
                document.getElementById('power').className='highlight';
                alert(msgInvalidPower);
                theForm.power.select();
                theForm.power.focus();
                return false;
            }
        }
        if(theForm.hours && theForm.hours.value != '')
        {
            if(!(theForm.hours.value.match(/^[0-9]{1,10}$/)) || (theForm.hours.value < 1))
            {
                document.getElementById('hours').className='highlight';
                alert(msgInvalidHours);
                theForm.hours.select();
                theForm.hours.focus();
                return false;
            }
        }
        if(theForm.eng_model_info && !isLong(theForm.eng_model_info.value,' ',40))
        {
            document.getElementById('eng_model_info').className='highlight';
            alert(msgEngMdlInfo+' '+msgTooLongWord);
            theForm.eng_model_info.select();
            theForm.eng_model_info.focus();
            return false;
        }
        if(theForm.eng_note && theForm.eng_note.value.length > 2000)
        {
            document.getElementById('eng_note').className='highlight';
            alert(msgEngNoteLen);
            theForm.eng_note.select();
            return false;
        }
    }

    if(theForm.heat_model_info && !isLong(theForm.heat_model_info.value,' ',40))
    {
        document.getElementById('heat_model_info').className='highlight';
        alert(msgHeatMdlInfo+' '+msgTooLongWord);
        theForm.heat_model_info.select();
        theForm.heat_model_info.focus();
        return false;
    }
    if(theForm.heat_note && theForm.heat_note.value.length > 2000)
    {
        document.getElementById('heat_note').className='highlight';
        alert(msgHeatNoteLen);
        theForm.heat_note.select();
        return false;
    }
    return true;
}

//Function used when inserting sold price at marking boat sold
function soldBoat(theForm)
{
    s=theForm.sold_price.value;

    if(theForm.finn_boat_dealer.value == "Y" && s == "")
    {
        alert(finnBoatPrice);
        theForm.sold_price.focus();
        return false;
    }
    else if(s != "")
    {
        if(!(s.match(/^[0-9]{1,10}$/)))
        {
            alert(msgInvalidPrice);
            theForm.sold_price.focus();
            return false;
        }
        if (parseInt(s) < 1 )
        {
            alert(msgLessThan1Price);
            theForm.sold_price.focus();
            return false;
        }
    }
    return true;
}

// Function to add the sail information in listbox
function addSailDetails(type,make,material,mfg_year,sail_options,sail_options_val,sail_options_txt)
{
    if(type.selectedIndex == 0 && make.selectedIndex == 0 && material.selectedIndex == 0 && mfg_year.selectedIndex == 0)
    {
        alert(msgSelectSail);
        return;
    }
    var val=type.options[type.selectedIndex].value+","+
            make.options[make.selectedIndex].value+","+
            material.options[material.selectedIndex].value+","+
            mfg_year.options[mfg_year.selectedIndex].value;

    var txt='';

    if(type.selectedIndex > 0)
        txt +=type.options[type.selectedIndex].text+", ";

    if(make.selectedIndex > 0)
        txt +=make.options[make.selectedIndex].text+", ";

    if(material.selectedIndex > 0)
        txt +=material.options[material.selectedIndex].text+", ";

    if(mfg_year.selectedIndex > 0)
        txt +=mfg_year.options[mfg_year.selectedIndex].text+', ';

    var txt=txt.slice(0,-2);
    var tot=sail_options.length;
    var selOpt = new Option(txt,val);
    eval("sail_options.options[tot]=selOpt");

    sail_options_val.value += sail_options.options[tot].value+"#";
    sail_options_txt.value += txt+"#";
}

// Function to remove the sail information from listbox
function removeSailDetails(sail_options,sail_options_val,sail_options_txt)
{
    if(sail_options.length == 0 || sail_options.selectedIndex < 0)
        return;

    var selIndex=sail_options.selectedIndex;
    var tot=sail_options.length;
    var txt,val;

    if(selIndex >= tot)
        return;

    for(var i=selIndex + 1; i < sail_options.length; i++)
    {
        sail_options.options[i - 1].value=sail_options.options[i].value;
        sail_options.options[i - 1].text=sail_options.options[i].text;
    }
    sail_options.length=tot - 1;

    sail_options_txt.value='';
    sail_options_val.value='';

    for(var i=0; i < sail_options.length; i++)
    {
        sail_options_txt.value += sail_options.options[i].text+"#";
        sail_options_val.value += sail_options.options[i].value+"#";
    }
    if(sail_options.length > 0)
        sail_options.selectedIndex=-1;
}

//Function to display towns of the selected domicile
function domicileChange(v1,v2,curval)
{
    //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;

    //holds the selected domicile.
    var domId='';

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

    v2.length=1;

    //If the domicile is selected
    if(domId != '')
    {
        var opt,townLen=t[domId].length;

        for(i=0, j=1; i < townLen; i++)
        {
            opt=new Option(n[domId][i],t[domId][i]);
            eval("v2.options[j]=opt");

            if(curval.search(t[domId][i]) != -1)
                v2.options[j].selected=true;
            j++;
        }
    }
}
function checkPurchaseDetail(theForm)
{
     if(theForm.id_boat_type.selectedIndex < 0)
     {
        document.getElementById('id_boat_type').className='highlight';
         alert(capPlz+' '+capSel.toLowerCase()+' '+capMk);
         theForm.id_boat_type.focus();
         return false;
     }

    if(theForm.id_make.selectedIndex == 0)
    {
        document.getElementById('id_make').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capMk);
        theForm.id_make.focus();
        return false;
    }

    if(!(theForm.price_ask.value.match(/^[0-9]{1,10}$/)) || theForm.price_ask.value < 1)
    {
        document.getElementById('price_ask').className='highlight';
        alert(msgInvalidPrice);
        theForm.price_ask.select();
        theForm.price_ask.focus();
        return false;
    }

    if(isblank(theForm.boat_year_from.value))
    {
        document.getElementById('mfg_year').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capYModelFrm);
        theForm.boat_year_from.focus();
        return false;
    }

    if(isblank(theForm.boat_year_to.value))
    {
        document.getElementById('mfg_year').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capYModelTo);
        theForm.boat_year_to.focus();
        return false;
    }

    if(theForm.id_domicile.selectedIndex < 1 )
    {
        document.getElementById('domicile').className='highlight';
        alert(capPlz+' '+capSel.toLowerCase()+' '+capDom);
        theForm.id_domicile.focus();
        return false;
    }

    if(theForm.note.value.length > 2000)
    {
        document.getElementById('note').className='highlight';
        alert(msgNoteLen);
        theForm.note.select();
        return false;
    }
    if(isMaxWord(theForm.note.value," ",80) == 0)
    {
        document.getElementById('note').className='highlight';
        alert(msgLngDescWord);
        theForm.note.select();
        return false;
    }

    return true;
}

function displayEngineInfo()
{
    if(document.getElementById('enginechecky').checked)
            document.getElementById('engine').style.display = "";
    else if(document.getElementById('enginecheckn').checked)
        document.getElementById('engine').style.display = "none";
}
function backToAttachImg(link)
{
    document.boatpost.action=link;
    document.boatpost.submit();
    return false;
}
//if 'sold at best price' is selected, remove the 'price'
function removePrice()
{
    if(document.boatpost.bestsold.checked)
        document.boatpost.price_ask.value='';
}
//if 'no delivery cost' is selected, remove the 'delivery cost'
function removeDeliveryCost()
{
    if (document.boatpost.elements['no_delivery_cost'].checked)
        document.boatpost.elements['delivery_cost'].value = "";
}
//if 'price' is entered, uncheck the 'sold at best price'
function removeCheckbox(el)
{
    switch(el)
    {
        case 'price':
            if(document.boatpost.price_ask.value != '')
                document.boatpost.bestsold.checked=false;
        case 'delCost':
            if (document.boatpost.elements['delivery_cost'])
                document.boatpost.elements['no_delivery_cost'].checked = false;
    }
}
function backToAttachImg(link)
{
    document.boatpost.action=link;
    document.boatpost.submit();
    return false;
}
/** Add remove the star in field name */
function addRemoveMand(v)
{
    if(v.value == 14)
    {
        jQuery('#id_st_star').css('display','inline');
        jQuery('#id_mfg_star').css('display','inline');
    }
}
function backToAttachImg(link)
{
    document.boatpost.action=link;
    document.boatpost.submit();
    return false;
}
function saveCurVal(curVal)
{
    oldValue='';
    oldValue = curVal.selectedIndex;
}
function loadDefBlur(v1)
{
    if(v1.name=='additional_info')
    {
        if(!v1.value)
        {
            v1.value=defAdditionalInfo;
            v1.style.color = '#666666';
        }
    }
}
function loadDefClick(v1)
{
    if(v1.name=='additional_info')
    {
        if(v1.value == defAdditionalInfo)
        {
            v1.value='';
            v1.style.color = '#000000';
        }
    }
}
jQuery(document).ready(function(){
    jQuery(window).scrollTop(jQuery('input[name=hideScrollTop]').val());
    jQuery(window).scroll(getScrollPosition);
});
