// Scott Berkley put this stuff together - berkleys //

function checkform() {
  var ftest = true;
  if (  checkitem(document.info_request.First_Name,"First Name") &&
	checkitem(document.info_request.Last_Name,"Last Name") &&
	checkitem(document.info_request.EMail,"Email") &&
	checkitem(document.info_request.EMail_Confirm,"Email Confirm") &&
	checkitem(document.info_request.Street_1,"Street") &&
	checkitem(document.info_request.City,"City") &&
	checkitem(document.info_request.State,"State/Province") &&
	checkitem(document.info_request.Postal_Code,"Postal Code") &&
	checkitem(document.info_request.Country,"country") &&
	checkradio(document.info_request.Degree_Program,"choose the Degree Program in which you are most interested.") &&
	checkradio(document.info_request.I_Heard_About_This_From,"tell us how you heard about the Ford School") &&
	checkemail(document.info_request.EMail) &&
	checkzipcode(document.info_request.Country, document.info_request.Postal_Code, "Please submit a valid USA zip code.") &&
	checkstate(document.info_request.Country, document.info_request.State,"Please submit a valid 2-letter state code abbreviation for the USA.") 
	)
     ftest = true;
  else {
    ftest = false;
  }
  return ftest;
 }

//check the text value//
function checkitem(item, fdesc) {
  var sendform = true; 
  if (item.value == "" || checkblanks(item.value) == true)  {
   if (fdesc != "") { alert("Please enter " + fdesc + "");  }
   item.focus();
   sendform = false;
   }
   return sendform;
 }


// following looks for all blanks in a string - typical of checking//
function checkblanks(item)  {
  var isblank = true;
  for (i = 0; i < item.length; i++) {
    if (item.charAt(i) != " ") {
      isblank = false;  }
  } 
  return isblank;
}


//following only checks for presence of @ and a period  //
//also eliminates bad characters   //
//routine could be adapted for finding anything in a string  //

function checkemail(item) {
  var goodmail = true;
  var addr = item.value;
  var invchar = " /:,;";
  for (i=0; i<invchar.length; i++)  {
	badchar = invchar.charAt(i);
	if (addr.indexOf(badchar,0) > -1)  {
   		goodmail = false;
	}
  }
  atpos = addr.indexOf("@",1);
  if (atpos == -1) {
	goodmail = false;
  }
  else  {
	perpos = addr.indexOf(".",atpos);
	if (perpos == -1)  {
	goodmail = false;
	}
	else  {
		if (perpos + 3 > addr.length)  {
			goodmail = false;
		}
	}
  }
  if (goodmail == false) {
    alert("Please enter a valid email address.");
    item.focus();
  }
  return goodmail;
}

function goodchar(string) {

var good="0123456789-";

var i = 0;

    for (i = 0; i < string.length; i++) {

       if (good.indexOf(string.charAt(i)) == -1) {
       		var badchar = true;
		}
	}    

	if (badchar == true){
	sendform = false;
	}
	else {
	sendform = true;
	}
return sendform;

} 

function checkzipcode (cntry, zip, fdesc) {  
	
	if (cntry.value == "USA") 
		{
	var zip = zip.value;
		

	if ((zip.length == 5 || zip.length == 10) && goodchar(zip)){
	sendform = true;
	}
	else {
	sendform = false;
	alert (fdesc);
	document.info_request.Postal_Code.focus();
	}
}

return sendform;
}

function checkstate (cntry, state, fdesc) {  

	if (cntry.value == "USA") 
	{
	var state = state.value;
		

	if (state.length > 2) {
	sendform = false;
	alert (fdesc);
	document.info_request.State.focus();
	}
	else {
	sendform = true;
		}
	}
return sendform;
}

//check the radio button//
function checkradio(item,fdesc) {
 var sendform;
	sendform = false;


	for (i = 0; i < item.length; i++) {
	  if (item[i].checked) {
     		sendform = true;
 	 	}
	}

	if (sendform == false)  {
	  alert("Please " + fdesc);
		
	}

  return sendform;
}

