//validate.js
//This script validates a form based on an array of required fields.


function requiredField(fieldName,type,question,funcName)
 {
  this.fieldName = fieldName; // Name of field from the HTML form
  this.type = type; // 1 = Text, Textarea, or Radio/Checklist if only 1 field ; 2 = Radio/Checklist if more than 1 field ; 3 = Select List ; 4 = Email ; 9 = Special function ;
  this.question = question; // Text question for the error report
  this.funcName = funcName; // Name of special function if needed 
 } 
 
function Email(field)
 {
  var emailAccount=new String(field.value);
  var filter=/^[a-z0-9]([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.)+[a-z]{2,4}$/i;
  if (!filter.test(emailAccount))
   {
	return false;
   }
  else 
   {
	return true;
   } 	
}
 
function textField(field)
 {
  if (field.value == "")
   {
    return false;
   }
  else
   {
    return true;
   }
 }

function radioCheck(rc1)
 {
  var valid = 0;
  for (i=0; i < rc1.length; i++)
   {
    if (rc1[i].checked) { valid++; }
   }
  if (valid == "0")
   {
    return false;
   }
  else
   {
    return true;
   }
 }

function selectList(listBox)
 {
  if (listBox.selectedIndex == "-1")
   {
    return false;
   }
  else
   {
    return true;
   }
 }

function reportErrors(list)
 {
  var report = "Some fields required by this form were not filled in correctly.\nFollowing is a list of these fields:\n\n";
  var alt = "Some fields required by this form were not filled in correctly.\n\nThere are too many to list.\n";
  var temp = "";
  var tempList = new Array(1);
  var c = 0;
  var t = 0;
  
  for ( i=0 ; i < list.length ; ++i )
   {
    if (list[i] == 1) { tempList[c] = i; c++; }
   }  
   
  if (tempList.length < 15)
   {
    for ( j=0 ; j<tempList.length ; ++j )
     {
	 temp = "";
	 t = tempList[j];
      temp = reqFields[t].question;
	 report = report + temp + "\n";
	}
    alert(report);
   }
  else
   {
    alert(alt);
   }    	  
 }
  
 function validateForm(form)
  {
   var totalReqs = reqFields.length;
   var okays = 0;
   var checkedFields = new Array(totalReqs);
   var i = 0;
   for (i=0;i<totalReqs;++i)
    {
     var tempField = eval("form." + reqFields[i].fieldName);
     var tempType = reqFields[i].type;
          
     if (tempType == 1)
	 { 
	  if (textField(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
	 }
	if (tempType == 2)
	 { 
	  if (radioCheck(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
	 } 
	if (tempType == 3)
	 { 
	  if (selectList(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
     }
	if (tempType == 4)
	 { 
	  if (Email(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
     }
    if (tempType == 9)	
     {
	  var tempCheck = eval(reqFields[i].funcName + "(form)");
	  if (tempCheck == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; }
	 } 	    

    }

   if (okays != totalReqs) { reportErrors(checkedFields); }
   else { form.submit(); }
   
  }
	
	function redirect() { setTimeout("go_now()",0); }
  //function go_now() { window.location.href="download.php?name=style.css"; }
  function go_now() { window.location.href="thankyou.html"; }
	
	function validateForm2(form)
  {
   var totalReqs = reqFields.length;
   var okays = 0;
   var checkedFields = new Array(totalReqs);
   var i = 0;
   for (i=0;i<totalReqs;++i)
    {
     var tempField = eval("form." + reqFields[i].fieldName);
     var tempType = reqFields[i].type;
          
     if (tempType == 1)
	 { 
	  if (textField(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
	 }
	if (tempType == 2)
	 { 
	  if (radioCheck(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
	 } 
	if (tempType == 3)
	 { 
	  if (selectList(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
     }
	if (tempType == 4)
	 { 
	  if (Email(tempField) == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; } 
     }
    if (tempType == 9)	
     {
	  var tempCheck = eval(reqFields[i].funcName + "(form)");
	  if (tempCheck == true) { checkedFields[i] = 0; ++okays; }
	  else { checkedFields[i] = 1; }
	 } 	    

    }

   if (okays != totalReqs) { reportErrors(checkedFields); }
   else { window.open('download.html','Visio',config='height=100,width=200'); form.submit(); }
   //else { window.open("download.html",'Visio Download',config='height=100,width=200,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0'); }//redirect(); }//form.submit(); }
   
  } 	



   	    