function validateRegEx(regex, theStr, theAlert, theMessage) {
	if (!regex.test(theStr)) {
		if (theAlert != null)
			theAlert.innerHTML = theMessage;
		return false;
	} else {
		if (theAlert != null)
			theAlert.innerHTML = "";
		return true;
	}
}

function validateEmpty(inputElement, theAlert) {
	if (inputElement.value.length == 0) {
		if (theAlert != null) 
			theAlert.innerHTML = "Cannot be empty.";
		return false;
	} else {
		if (theAlert != null) 
			theAlert.innerHTML = "";
		return true;
	}
}

function validateEmail(inputElement, theAlert) {
	if(!validateEmpty(inputElement, theAlert))
	return false;
	
	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, inputElement.value, theAlert, "Enter a valid Email address.");
}

function validatePhone(inputElement, theAlert) {
	if(inputElement.value.length == 0) {
		if (theAlert != null)
			theAlert.innerHTML = "";
		return true;
	} else {
	return validateRegEx(/^\d{3}-\d{3}|\d{4}-\d{4}$/, inputElement.value, theAlert, "Valid format: 123-456(7)-8901");
	}
}

function finalValidate(form) {
	if (validateEmpty(form["name"], form["name_alert"]) &&
	    validateEmail(form["email"], form["email_alert"]) &&
		validateEmpty(form["inquiries"], form["inquiries_alert"])) {
	
		form.submit();
		alert("Thank you, your inquiries has been sent to Oaxaca Studio");
		window.location = "index.html";
	    } else {
	    alert("Either you have not entered the valid information or you have left input fields empty.");  
	    }
}



