var d=document
function init_form_validation(f)
{
	f.Make.numeric = false;
	f.Model.optional = false;
	f.EngineCapacity.numeric = true;
	f.EngineCapacity.optional = true;
	f.Mileage.numeric = true;
	f.Mileage.optional = true;
    f.BodyStyle.optional = true;
    f.Transmission.optional = true;
    f.Fuel.optional = false;
    f.Condition.optional = false;
    f.Doors.optional = true;
	f.Registration.numeric = true;
	f.Registration.optional = true;
    f.Registration.integer = true;
    f.Registration.min = 1940
	f.Colour.optional = false;
	f.Price.numeric = true;
	f.Price.min = 1;
	f.Price.max = 50000;
	f.Comments.optional = true;
	f.Class.optional = false;
    f.picSize.numeric = true;
    f.picSize.min = -1;
    f.picSize.max = 100000;
}

function validate_form(f)
{
var msg;
var empty_fields = "";
var errors = "";
init_form_validation(f);

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

	if ((e.type.indexOf('select')==0))
    {
    	if (!(e.optional))
    	{
			if (e.options.length == 0)
            {
				errors += e.name+" selection has not been set up\n";
			}
            if (e.value==null||(e.value=="")||(isBlank(e.value)))
            {
            	empty_fields += "\n         " + e.name;
            }
		}
    }

	if (((e.type == "text")|| (e.type == "textarea")) && !e.optional && (!e.numeric)){
		if ((e.value==null)||(e.value=="")||(isBlank(e.value))){
			empty_fields += "\n         " + e.name;
			continue;
		}
	}
		if ((e.numeric || (e.min !=null) ||(e.max !=null))&& (e.type.indexOf('select')==-1)){
			var v = parseFloat(e.value);
			if ((isNaN(v) &&(!e.optional||(e.optional && !isBlank(e.value)))) ||
				((e.min != null) && (v<e.min)) ||
				((e.max != null) && (v>e.max))|| (e.integer && (!isInteger(v)&& !isBlank(e.value)))) {
				errors += "- The field "+ e.name + " must be a number ";
				if (e.min != null)
					errors += " that is greater than "+e.min;
				if (e.max != null && e.min != null)
					errors += " and less than "+ e.max;
				else if (e.max !=null)
					errors += " that is less than " + e.max;
				errors += ".";
                if (e.integer && !isInteger(v))
                {
                	errors += "and must be a whole number.\n";
                }
                else
                {
                	errors +="\n";
                }
			}
			if (!isNaN(v)){e.value = v}

		}

}

if (!empty_fields && !errors) return true;
msg =  "_____________________________________________________________\n\n";
msg += "The form was not submitted because of the following error(s).\n";
msg += "Please correct these error(s) and  re-submit.\n";
msg +=  "_____________________________________________________________\n\n";

if (empty_fields) {
msg +="- The following required fields are empty:" + empty_fields + "\n";
if (errors) msg += "\n";
}
msg += errors;
alert(msg);
return false;
}

function isBlank(strItem)
{
	return(strItem.replace(/\s*/gi,"")=="")
}

function isInteger(numItem)
{
	return(Math.floor(numItem)==Math.ceil(numItem))
}
