/* $Id: nm_vehicleValidation.js,v 1.37 2011/10/04 05:25:27 hetal Exp $ */

function checkAllVehicle(theForm,isAdmin)
{
	var regno=theForm.regno.value;
	var shwReg=theForm.show_reg.checked;
	var notReg=theForm.no_reg.checked;
	var idBikeType=theForm.bike_type.value;
	removeHighlightClass();

	if(isblank(idBikeType))
    {
        document.getElementById('bike_type').className='highlight';
        alert(msgMandet);
        theForm.bike_type.focus();
        return false;
    }
    /** If Bike_type(Vehicle calss) have type then should be mandatory to select. */
    if(idBikeType == 2 || idBikeType == 5 || idBikeType == 6 || idBikeType == 16 || idBikeType == 31)
    {
        if(isblank(theForm.sub_type.value))
        {
            document.getElementById('sub_type').className='highlight';
            alert(msgMandet);
            theForm.sub_type.focus();
            return false;
        }
    }

	if(isblank(theForm.car_make.value))
	{
		document.getElementById('car_make').className='highlight';
		alert(msgMandet);
		theForm.car_make.focus();
		return false;
	}

    if(idBikeType != 31) // Check when vehicle type not Trailer and sleighs
	{
		if(isblank(theForm.car_model.value))
		{
			document.getElementById('bike_model').className='highlight';
			alert(msgMandet);
			theForm.car_model.focus();
			return false;
		}
	    if(theForm.production_number && theForm.production_number.value != '')
	    {
	        if(!theForm.production_number.value.match(/^[0-9a-zA-ZåÅäÄöÖÁÀÉÈÒÓÙÚáàèéòóùúÝìíýÌÍ´\s]{1,30}$/))
	        {
	            document.getElementById('production_number').className='highlight';
	            alert(msgProductionNo);
	            theForm.production_number.focus();
	            return false;
	        }
	    }

	    //If any word in engine model field is longer than specified width characters long
		if(isLong(theForm.engine_model.value," ",21) == 0)
		{
			document.getElementById('engine_model').className='highlight';
			alert(msgEngModel);
			theForm.engine_model.focus();
			return false;
		}
		if((isblank(regno)) && (shwReg == true) && (notReg == false) && theForm.ad_type.value == "F")
		{
			document.getElementById('regno').className='highlight';
			alert(msgBlankRegNo);
			theForm.regno.select();
			theForm.regno.focus();
			return false;
		}
		if((isblank(regno)) && (notReg == false) && theForm.ad_type.value == "F")
		{
			document.getElementById('regno').className='highlight';
			alert(msgAboutRegNo);
			theForm.regno.select();
			theForm.regno.focus();
			return false;
		}/*
		else if(!(isblank(regno)) && !(isblank(notReg)))
		{
			document.getElementById('regno').className='highlight';
			document.getElementById('regnoimg').className='hlightimage';
			alert(msgAboutRegNo);
			theForm.regno.select();
			theForm.regno.focus();
			return false;

		}	*/
		else if(!(isblank(regno)) && theForm.ad_type.value == "F")
		{
			if( !(regno.match(/^([a-zA-ZäÄÖöÅå]{1,3})([0-9]{1,3})$/)) && !(regno.match(/^([a-zA-ZäÄÖöÅå]{1,3})-([0-9]{1,3})$/)) && !regno.match(/^([0-9]{1,3})([a-zA-ZZäÄÖöÅå]{1,3})$/) && !regno.match(/^([0-9]{1,3})-([a-zA-ZZäÄÖöÅå]{1,3})$/) && theForm.adminUser.value == "dummy")
			{
				document.getElementById('regno').className='highlight';
				alert(msgInvalidRegNo);
				theForm.regno.select();
				theForm.regno.focus();
				return false;
			}
		}
	}

	if(!isblank(theForm.car_price.value))
	{
		if((theForm.bestsold.checked))
		{
			document.getElementById('car_price').className='highlight';
			alert(msgInvalidEntry);
			theForm.car_price.select();
			theForm.car_price.focus();
			return false;
		}
		else
		{
			if(!(theForm.car_price.value.match(/^[0-9]{1,10}$/)) || parseInt(theForm.car_price.value) < 1)
			{
				document.getElementById('car_price').className='highlight';
				alert(msgInvalidPrice);
				theForm.car_price.select();
				theForm.car_price.focus();
				return false;
			}
			if((parseInt(theForm.car_price.value) >= 1) && (parseInt(theForm.car_price.value) <= 50) && theForm.ad_type.value != "R")
			{
				document.getElementById('car_price').className='highlight';
				alert(msgPrice);
				theForm.car_price.select();
				theForm.car_price.focus();
				return false;
			}
		}
	}
	else
	{
		if(!theForm.bestsold.checked)
		{
			document.getElementById('car_price').className='highlight';
			if(theForm.ad_type.value == "R")
				alert(msgInvalidRentPrice);
			else
				alert(msgInvalidEntry);
			theForm.car_price.select();
			theForm.car_price.focus();
			return false;
		}
	}
	if(theForm.userCat.value == 'D' && theForm.delivery_cost && idBikeType != 31) // Check when vehicle type not Trailer and sleighs
	{
		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(isblank(theForm.bike_year.value))
	{
		document.getElementById('bike_year').className='highlight';
		alert(msgMandet);
		theForm.bike_year.focus();
		return false;
	}

	if(idBikeType != 31) // Check when vehicle type not Trailer and sleighs
	{
		if(isblank(theForm.car_color.value))
		{
			document.getElementById('car_color').className='highlight';
			alert(msgMandet);
			theForm.car_color.focus();
			return false;
		}

		if(isblank(theForm.car_color_type.value))
		{
			document.getElementById('car_color_type').className='highlight';
			alert(msgMandet);
			theForm.car_color_type.focus();
			return false;
		}

		if(isblank(theForm.eng_type.value))
		{
			document.getElementById('eng_type').className='highlight';
			alert(msgMandet);
			theForm.eng_type.focus();
			return false;
		}

		if(theForm.eng_type.value != 4)
		{
			if(isblank(theForm.car_engine.value))
			{
				document.getElementById('car_engine').className='highlight';
				alert(msgMandet);
				theForm.car_engine.focus();
				return false;
			}
		}


		if(theForm.bike_mileage.value != '')
		{
			if( isNaN(theForm.bike_mileage.value) || isblank(theForm.bike_mileage.value) )
			{
				document.getElementById('bike_mileage').className='highlight';
				alert(msgInvalidMileage);
				theForm.bike_mileage.select();
				theForm.bike_mileage.focus();
				return false;
			}

			if (parseInt(theForm.bike_mileage.value) < 1)
			{
				document.getElementById('bike_mileage').className='highlight';
				alert(msgInvalidMileage);
				theForm.bike_mileage.select();
				theForm.bike_mileage.focus();
				return false;
			}
		}

		if(isblank(theForm.car_drive_type.value))
		{
			document.getElementById('car_drive_type').className='highlight';
			alert(msgMandet);
			theForm.car_drive_type.focus();
			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.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 any word in vehicle remark field is longer than specified word lenght,
	//it will give a alert message.
	if(isAdmin != 'Y')
	{
		if(isMaxWord(theForm.car_remark.value," ",40) == 0)
		{
			document.getElementById('car_remark').className='highlight';
			alert(msgLngRmrkWord);
			theForm.car_remark.focus();
			return false;
		}
	}

	if (theForm.userCat.value != 'D')
	{
		//Check for blank domicile field
		if(isblank(theForm.car_domicile.value))
		{
			document.getElementById('domicile').className='highlight';
			alert(msgMandet);//msgBlankDom
			theForm.car_domicile.focus();
			return false;
		}

		if(isblank(theForm.bike_town.value))
		{
			if(theForm.car_domicile.value != 15 && theForm.car_domicile.value != 25)
			{
				document.getElementById('town').className='highlight';
				alert(msgMandet);
				theForm.bike_town.focus();
				return false;
			}
		}
	}
	if(theForm.snowCheck.checked)
	{
		if(!(document.getElementById('snowl').checked) && !(document.getElementById('snowf').checked))
		{
			document.getElementById('snowMobile').className='highlight';
			document.getElementById('snowimg').className='hlightimage';
			alert(msgCoolingType);
			document.getElementById('snowMobile').focus();
			return false;
		}
	}

	return true;
}

function checkPurchaseDetail(theForm)
{
    var idBikeType=theForm.bike_type.value;

    /** If Bike_type(Vehicle calss) have type then should be mandatory to select. */
    if(idBikeType == 2 || idBikeType == 5 || idBikeType == 6 || idBikeType == 16 || idBikeType == 31)
    {
        if(isblank(theForm.sub_type.value))
        {
            document.getElementById('sub_type').className='highlight';
            alert(msgMandet);
            theForm.sub_type.focus();
            return false;
        }
    }
	if(isblank(theForm.car_make.value))
	{
        document.getElementById('car_make').className='highlight';
		alert(msgMandet);
		theForm.car_make.focus();
		return false;
	}

	if(idBikeType != 31)
	{
		if(isblank(theForm.car_model.value))
		{
	        document.getElementById('bike_model').className='highlight';
			alert(msgMandet);
			theForm.car_model.focus();
			return false;
		}

		//If any word in engine model field is longer than specified width characters long
		if(isLong(theForm.engine_model.value," ",21) == 0)
		{
	        document.getElementById('engine_model').className='highlight';
			alert(msgEngModel);
			theForm.engine_model.focus();
			return false;
		}
	}

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

	if((isblank(theForm.bike_year_from.value)) || (isblank(theForm.bike_year_to.value)))
	{
        document.getElementById('idBikeYear').className='highlight';
		alert(msgMandet);
		theForm.bike_year_from.focus();
		return false;
	}

	//If any word in vehicle remark field is longer than specified word lenght,
	//it will give a alert message.
	if(isMaxWord(theForm.car_remark.value," ",40) == 0)
	{
        document.getElementById('car_remark').className='highlight';
		alert(msgLngRmrkWord);
		theForm.car_remark.focus();
		return false;
	}

	if(idBikeType != 31)
	{
		if(theForm.snowCheck.checked)
		{
			if(!(document.getElementById('snowl').checked) && !(document.getElementById('snowf').checked))
			{
				document.getElementById('snowMobile').className='highlight';
				alert(msgCoolingType);
				document.getElementById('snowMobile').focus();
				return false;
			}
		}

		if(isblank(theForm.eng_type.value))
	    {
	        document.getElementById('eng_type').className='highlight';
	        alert(msgMandet);
	        theForm.eng_type.focus();
	        return false;
	    }

		if(theForm.eng_type.value!=4)
		{
			if(isblank(theForm.car_engine.value))
		    {
		        document.getElementById('car_engine').className='highlight';
		        alert(msgMandet);
		        theForm.car_engine.focus();
		        return false;
		    }
		}
	}
	return true;
}

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

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

//This function check for word length in the string entered in the given textbox(input field)
function txtValid(theTxtbox,maxWordLen)
{
	//If any word in engine model field is longer than specified width characters long
	if(isLong(theTxtbox.value," ",maxWordLen) == 0)
	{
		alert(msgLngWordFPart + maxWordLen + msgLngWordSPart);
		theTxtbox.focus();
		return false;
	}
	else
		return true;
}

//This function works like the function 'txtValid', but used
//when there is a array of textBox(input field) elements
function txtErrayValid(theFrm,txtName,txtId,maxWordLen)
{
	for(i=0; i<theFrm.elements.length; i++)
	{
		var eleName=theFrm.elements[i].name;

		if(eleName.match(txtName) != null)
		{
			if(eleName.match(txtId) != null)
			{
				if(isLong(theFrm.elements[i].value," ",maxWordLen) == 0)
				{
					alert(msgLngWordFPart + maxWordLen + msgLngWordSPart);
					theFrm.elements[i].focus();
					return false;
					break;
				}
				else
				{
					return true;
					break;
				}
			}
		}
	}
	return false;
}

/** Function to check Road permit for moped */
function checkMopedRoadPermit()
{
    var bikeType = document.bikepost.bike_type.value;
    var bikeSubType = document.bikepost.sub_type.value;
    var engSize = document.bikepost.car_engine.value;

    if(engSize > 50 && bikeType == 5 && bikeSubType == 45)
    {
        document.bikepost.sub_type.value = '46';
        alert(msgNoRoadPermit);
    }
}
