// Preload images
var empty = new Image(); empty.src = "fieldempty.gif";
var email = new Image(); email.src = "emailerror.gif";
var zipcd = new Image(); zipcd.src = "ziperror.gif";
var phone = new Image(); phone.src = "phoneerror.gif";


function showImage(imagename, imageurl) { 
	document[imagename].src = imageurl;
}


function validateForm(f) {
        if (1 > f.firstname.value.length){
                showImage("firstnameerror", "http://www.emrvacationrentals.com/images/fieldempty.gif");
                errorFirstName = true;
                }
        else {
                showImage("firstnameerror", "http://www.emrvacationrentals.com/images/blankimage.gif");
                errorFirstName = false;
                }


        if (1 > f.lastname.value.length){
                showImage("lastnameerror", "http://www.emrvacationrentals.com/images/fieldempty.gif");
                errorLastName = true;
                }
        else {
                showImage("lastnameerror", "http://www.emrvacationrentals.com/images/blankimage.gif");
                errorLastName = false;
                }


        if (8 > f.homephone.value.length){
                showImage("phoneerror1", "http://www.emrvacationrentals.com/images/phoneerror.gif");
                errorHomePhone = true;
                }
        else {
                showImage("phoneerror1", "http://www.emrvacationrentals.com/images/blankimage.gif");
                errorHomePhone = false;
                }

		if (8 > f.workphone.value.length){
                showImage("phoneerror2", "http://www.emrvacationrentals.com/images/phoneerror.gif");
                errorWorkPhone = true;
                }
        else {
                showImage("phoneerror2", "http://www.emrvacationrentals.com/images/blankimage.gif");
                errorWorkPhone = false;
                }

        if (f.email.value.search("@") == -1 || f.email.value.search("[.*]") == -1 || 6 > f.workphone.value.length) { 
                showImage("emailerror", "http://www.emrvacationrentals.com/images/emailerror.gif")
                errorEmail = true;
                }
        else {
                showImage("emailerror", "http://www.emrvacationrentals.com/images/blankimage.gif");
                errorEmail = false;
                }


// --- The error detection logic ---


        haveErrors = errorFirstName || errorLastName || errorEmail || (errorHomePhone && errorWorkPhone);


        // Ie: throw an error if...
        //      ONLY First name is missing OR
        //      ONLY Last name is missing OR
        //      BOTH of phone AND email is missing


        if (haveErrors == true){
                showImage("submiterror", "http://www.emrvacationrentals.com/images/submiterror.gif");
                }
        else {
                showImage("submiterror", "http://www.emrvacationrentals.com/images/blankimage.gif");
                }



        return (!haveErrors);   // Return false if we have errors, ie: NOT haveErrors
                                        // Notice that the form can submit even if EITHER the email OR phone
                                        // fields show the "missing information" gif.
}
