// JavaScript Document
// Discount Canadian Drugs JavaScript Document

function validateForm(){

	emailExp = new RegExp(".+[@].+");
	phoneFaxExp = /^[0-9]{3}[-]?[0-9]{3}[-]?[0-9]{4}$/;

	//Creates prompt
	if(document.form2.fullName.value == ""){
		nameVal = prompt("You have not entered a name on the form", "Please enter a name here and then click ok");
		document.form2.fullName.value = nameVal;
	}

	if(document.form2.contactBy[0].checked == "1" && document.form2.email.value == ""){
		emailVal = prompt("You have chosen to be contacted by email.", "Please enter an email address here and then click ok");
		document.form2.email.value = emailVal;
		while(!emailExp.test(emailVal)){
			emailVal = prompt("You did not enter a valid email address. Please check your entry and try again.", "Please enter a valid email address here then click ok");
			document.form2.email.value = emailVal;
			if(document.form2.email.value == "null"){
				document.form2.email.value = "";
				break;
			}
		}
	}
	if(document.form2.contactBy[1].checked == "1" && document.form2.phone.value == ""){
		phoneVal = prompt("You have chosen to be contacted by phone.", "Please enter a valid phone number.");
		document.form2.phone.value = phoneVal;
		while(!phoneFaxExp.test(phoneVal)){
			phoneVal = prompt("Please enter a valid phone number (e.i. 204-312-1298)", "xxx-xxx-xxxx");
			document.form2.phone.value = phoneVal;
			if(document.form2.phone.value == "null"){
				document.form2.fax.value = "";
				break;
			}
		}
	}
	if(document.form2.contactBy[2].checked == "1" && document.form2.fax.value == ""){
		faxVal = prompt("You have chosen to be contacted by fax.", "Please enter a valid fax number.");
		document.form2.fax.value = faxVal;
		while(!phoneFaxExp.test(faxVal)){
			faxVal = prompt("Please enter a valid fax number (e.i. 204-587-2873)", "xxx-xxx-xxxx");
			document.form2.fax.value = faxVal;
			if(document.form2.fax.value == "null"){
				document.form2.fax.value = "";
				break;
			}
		}
	}
	
	// takes care of null values
	for(i=0; i<4; i++){
		if(document.form2[i].value == "null" || document.form2[i].value == "Please enter a name here and then click ok"){
			document.form2[i].value = "";
		}
	}
	
	if(!document.form2.contactBy[0].checked && document.form2.email.value != ""){
		while(!emailExp.test(document.form2.email.value)){
			emailVal = prompt("You did not enter a valid email address. Please check your entry and try again.", "Please enter a valid email address here then click ok");
			document.form2.email.value = emailVal;
			if(document.form2.email.value == "null"){
				document.form2.email.value = "";
				break;
			}
		}
	}
	if(!document.form2.contactBy[1].checked && document.form2.phone.value != ""){
		while(!phoneFaxExp.test(document.form2.phone.value)){
			phoneVal = prompt("Please enter a valid phone number (e.i. 204-312-1298)", "xxx-xxx-xxxx");
			document.form2.phone.value = phoneVal;
			if(document.form2.phone.value == "null"){
				document.form2.phone.value = "";
				break;
			}
		}
	}
	if(!document.form2.contactBy[2].checked && document.form2.fax.value != ""){
		while(!phoneFaxExp.test(document.form2.fax.value)){
			faxVal = prompt("Please enter a valid fax number (e.i. 204-312-1298)", "xxx-xxx-xxxx");
			document.form2.fax.value = faxVal;
			if(document.form2.fax.value == "null"){
				document.form2.fax.value = "";
				break;
			}
		}
	}
	if(document.form2.comment.value == ""){
		alert("The comments/questions field is empty. Please type in your comment or question.");
	}
	if(document.form2.fullName.value != "" && document.form2.comment.value != ""){
		if(document.form2.contactBy[0].checked && document.form2.email.value != ""){
			if(document.form2.phone.value == ""){
				document.form2.phone.value = "Not given";
			}
			if(document.form2.fax.value == ""){
				document.form2.fax.value = "Not given";
			}
			document.form2.submit();
		}
		else if(document.form2.contactBy[1].checked && document.form2.phone.value != ""){
			if(document.form2.email.value == ""){
				document.form2.email.value = "Not given";
			}
			if(document.form2.fax.value == ""){
				document.form2.fax.value = "Not given";
			}
			document.form2.submit();
		}
		else if(document.form2.contactBy[2].checked && document.form2.fax.value != ""){
			if(document.form2.email.value == ""){
				document.form2.email.value = "Not given";
			}
			if(document.form2.phone.value == ""){
				document.form2.phone.value = "Not given";
			}

		document.form2.submit();
		}
	}
}

function validateEmail(){
	emailExp = new RegExp(".+[@].+");
	friendEmail = document.form2.to_email.value;
	yourEmail = document.form2.your_email.value;
	if(!emailExp.test(friendEmail)){
		friendEmail = prompt("You did not enter a valid email address. Please check your entry and try again.", "Please enter a valid email address here then click ok");
		document.form2.to_email.value = friendEmail;
	}
	if(!emailExp.test(yourEmail)){
		yourEmail = prompt("You did not enter a valid email address. Please check your entry and try again.", "Please enter a valid email address here then click ok");
		document.form2.your_email.value = yourEmail;
	}
	if(emailExp.test(friendEmail) && emailExp.test(yourEmail)){
		document.form2.submit();
	}
}