// JavaScript Document for 
//*confirming two fields of e-mail addresses are the same
//*ensuring proper e-mail address format in both
//*requiring certain fields

function CheckRequiredFields() {
	
//ensures that certain fields are required
	var errormessage = new String();
	//error messages are generated if the following if statements are true
	if(WithoutContent(document.askaqform.name.value)) 
		{ errormessage += "\n\nPlease enter your name in the \"Name\" box."; }
	if(WithoutContent(document.askaqform.from.value)) 
		{ errormessage += "\n\nPlease enter your email in the \"E-mail\" box."; }
	if(WithoutContent(document.askaqform.confirm.value)) 
		{ errormessage += "\n\nPlease enter your email again in the \"Confirm e-mail\" box."; }

	if(document.askaqform.Area.value == "(None Selected)")
		{ errormessage += "\n\nPlease choose a subject from the \"Subject\" drop down menu."; }
	
	if(WithoutContent(document.askaqform.Reason.value))
		{ errormessage += "\n\nPlease fill in the \"How will you use this information?\" box."; }

	if(WithoutContent(document.askaqform.Question.value))
		{ errormessage += "\n\nPlease type your question in the \"Your question\" box."; }
	
//comparing the two e-mail addresses
	em1 = document.askaqform.from.value;	
	em2 = document.askaqform.confirm.value;
	
	if (em1 != em2) 
		{ errormessage += "\n\nThe e-mail addresses you entered do not match. Please check them."; }

  if (
      (em1.match("ipl.org")) || 
      (em2.match("ipl.org")) || 
      (em1.match("fluggly")) || 
      (em2.match("fluggly"))
     )
		{ errormessage += "\n\n"+em1+" is not a valid email address to enter into the form."; }
  
//making sure that the e-mail address submitted has a proper format.
	AtPos = em1.indexOf("@") // check for ampersand (@)
	StopPos = em1.lastIndexOf(".") // check for dot (.)
	
	if ((AtPos == -1 || StopPos == -1) && (em1.length > 0))
		{	errormessage += "\n\nYour e-mail address needs an ampersand (@) and/or a period."; 
			} 
	if (StopPos < AtPos) 
		{	errormessage += "\n\nYour e-mail address needs a period or it has to be moved after the ampersand (@)."; 
			} 
	if (StopPos - AtPos == 1)	
		{	errormessage += "\n\nYour e-mail address needs at least 1 character between the period and the ampersand (@)."; 
			} 
	if (AtPos == 0)	 
		{	errormessage += "\n\nYou can't have the ampersand (@) start your e-mail."; 
			} 
	if ((StopPos - em1.length == -1) && (em1.length > 0))
		{	errormessage += "\n\nYou can't have the period at the end of your e-mail."; 
			} 


// Put field checks above this point.
	if(errormessage.length > 2) { //won't allow form to be submited if there's errormessage has a value
		alert('We still need some more information before you submit your question.' + errormessage);
		return false;
		}
	return true;
} // end of function CheckRequiredFields()


function WithoutContent(ss) {
//checks if text input fields and text areas are empty
	if(ss.length > 0) { return false; } //no value if argument exists
	return true;
}

function NoneWithCheck(ss) {
//checks if radio buttons are checked or not
	for(var i = 0; i < ss.length; i++) 
		{
		if(ss[i].checked) { return false; } //no value if argument is checked
		}
	return true;
	}
	
function WithoutSelectionValue(ss) {
//checks if drop down menus have been selected
	for(var i = 0; i < ss.length; i++) 
		{
		if(ss[i].selected)
		{
			if(ss[i].value.length) { return false; }
		}
	}
	return true;
}

// above code based on code below take from this site Winter 2007:
//http://hyperdisc.unitec.ac.nz/materials/javascript/top10/simpleformvalidation2.htm
//<script type="text/javascript" language="JavaScript">
//
//function CheckRequiredFields() {
//var errormessage = new String();
//// Put field checks below this point.
//
//if(NoneWithCheck(document.exampleform.radioOne))
//	{ errormessage += "\n\nPlease click one radio button of the set of three."; }
//if(WithoutCheck(document.exampleform.radioLoner))
//	{ errormessage += "\n\nThe \"Loner\" radio button must be clicked."; }
//if(NoneWithCheck(document.exampleform.checkOne))
//	{ errormessage += "\n\nPlease check one or more check boxes of the set of three."; }
//if(WithoutCheck(document.exampleform.checkLoner))
//	{ errormessage += "\n\nThe \"Loner\" check box must be checked."; }
//if(WithoutContent(document.exampleform.sometext.value))
//	{ errormessage += "\n\nPlease type something in the \"Some text\" text field."; }
//if(NoneWithContent(document.exampleform.oneOrTheOther))
//	{ errormessage += "\n\nSomething must be typed in one or both of the set of form text fields."; }
//if(WithoutContent(document.exampleform.areaName.value))
//	{ errormessage += "\n\nSomething must be typed in the textarea box."; }
//if(WithoutContent(document.exampleform.FileGet.value))
//	{ errormessage += "\n\nA file name must be provided for uploading."; }
//if(WithoutSelectionValue(document.exampleform.dropname))
//	{ errormessage += "\n\nPlease select something from the dropdown list."; }
//
//// Put field checks above this point.
//if(errormessage.length > 2) {
//	alert('NOTE:' + errormessage);
//	return false;
//	}
//return true;
//} // end of function CheckRequiredFields()
//
//
//function WithoutContent(ss) {
//if(ss.length > 0) { return false; }
//return true;
//}
//
//function NoneWithContent(ss) {
//for(var i = 0; i < ss.length; i++) {
//	if(ss[i].value.length > 0) { return false; }
//	}
//return true;
//}
//
//function NoneWithCheck(ss) {
//for(var i = 0; i < ss.length; i++) {
//	if(ss[i].checked) { return false; }
//	}
//return true;
//}
//
//function WithoutCheck(ss) {
//if(ss.checked) { return false; }
//return true;
//}
//
//function WithoutSelectionValue(ss) {
//for(var i = 0; i < ss.length; i++) {
//	if(ss[i].selected) {
//		if(ss[i].value.length) { return false; }
//		}
//	}
//return true;
//}
//
////-->
//</script>
//
//<form name="exampleform" onSubmit="return CheckRequiredFields()">
//
//<table border="1" cellpadding="9" cellspacing="0"><tr><td>
//
//<input type="radio" name="radioOne" value="one">One<br>
//<input type="radio" name="radioOne" value="two">Two<br>
//
//<input type="radio" name="radioOne" value="three">Three
//<hr width="33%">
//<input type="radio" name="radioLoner" value="RRRR">Loner :)
//
//</td><td>
//
//<input type="checkbox" name="checkOne" value="one">checkOne one<br>
//<input type="checkbox" name="checkOne" value="two">checkOne two<br>
//<input type="checkbox" name="checkOne" value="three">checkOne three
//<hr width="33%">
//<input type="checkbox" name="checkLoner" value="CCCC">Loner :)
//
//</td></tr><tr><td>
//
//Some text:<br>
//<input type="text" name="sometext">

//<hr width="33%">
//Some text in either or both:<br>
//<input type="text" name="oneOrTheOther"><br>
//<input type="text" name="oneOrTheOther">
//
//</td><td>
//
//Type something:<br>
//<textarea cols="22" rows="5" name="areaName"></textarea>
//
//</td></tr><tr><td>
//
//<input type="file" name="FileGet" size="22">
//
//</td><td>
//
//<select name="dropname">
//<option value="">-- Select --</option>
//<option value="one">one</option>
//<option value="two">two</option>
//<option value="three">three</option>
//</select>
//
//</td></tr><tr><td colspan="2" align="center">
//
//<input type="submit">
//
//</td></tr></table>
//
//</form>




