// Validate Fields for Contact Us page

var submitcount=0;

function highlightTextField(field) {
		field.style.borderColor = 'red';
		field.style.borderStyle = 'solid';
		field.style.borderWidth = '2px;';
}

function highlightSelectField(field) {
		field.style.backgroundColor = 'red';
		field.style.color = 'black';
}

function validateForm(form) {
	var ErrorMsg = '';
	var FirstErrorField = '';

	if(form.name.value == '') {
		ErrorMsg = ErrorMsg + '- Your Name\n';
		highlightTextField(form.name);
		if(FirstErrorField == '') {
			FirstErrorField = 'form.name';
		}
	}

	if(!isEmail(form.email.value)) {
		ErrorMsg = ErrorMsg + '- Email Address Invalid\n';
		highlightTextField(form.email);
		if(FirstErrorField == '') {
			FirstErrorField = 'form.email';
		}
	}
	
	if(form.comments.value == '') {
		ErrorMsg = ErrorMsg + '- Comments\n';
		highlightTextField(form.comments);
		if(FirstErrorField == '') {
			FirstErrorField = 'form.comments';
		}
	}	
	
	if(ErrorMsg != '') {
		eval(FirstErrorField + '.focus();');
		alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
		return false;
	} else {
		if (submitcount == 0)	{
			submitcount++;
			return true;
		}	else	{
			return false;
		}
	}
}

// validates email address
function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return false;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}


function new_freecap()
{
	// loads new freeCap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent browser/isp caching
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload the image\nSubmit the form and a new image will be loaded");
	}
}

