// JavaScript Document
function validateCheckBoxes() {
  var errors = '';
  var RequiredResponsesCheckbox = window.document.COI_Form.Entered_Required_Responses;
 
	if (RequiredResponsesCheckbox == null || !RequiredResponsesCheckbox.checked) {
		errors += '- Entered Required Responses is not checked.\n';
	}

	return errors;
}


 function validateRadios() {
 var errors = '';
 var count = -1;
   for (i = window.document.COI_Form.Affiliated_With_Business.length-1; i > -1; i--) {
     if (window.document.COI_Form.Affiliated_With_Business[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You have not answered Question 1.\n';   
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Affiliated_With_Business[0].checked) {     
      if (window.document.COI_Form.Affiliated_With_Business_Details.value == "") {
      errors += '- You did not enter explanation to Question 1\n';
      }
    }
   }
   
   
   count = -1;
   for (i = window.document.COI_Form.Direct_Interest_With_Business.length-1; i > -1; i--) {
     if (window.document.COI_Form.Direct_Interest_With_Business[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You have not answered Question 2.\n';
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Direct_Interest_With_Business[0].checked) {     
      if (window.document.COI_Form.Direct_Interest_With_Business_Details.value == "") {
      errors += '- You did not enter explanation to Question 2\n';
      }
    }
   }   
   
   count = -1;
   for (i = window.document.COI_Form.Business_Decisions_12_Months.length-1; i > -1; i--) {
     if (window.document.COI_Form.Business_Decisions_12_Months[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You have not answered Question 3.\n';
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Business_Decisions_12_Months[0].checked) {     
      if (window.document.COI_Form.Business_Decisions_12_Months_Details.value == "") {
      errors += '- You did not enter explanation to Question 3\n';
      }
    }
   }   
   
   count = -1;
   for (i = window.document.COI_Form.Compensation_Supplier.length-1; i > -1; i--) {
     if (window.document.COI_Form.Compensation_Supplier[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You have not answered Question 4.\n';
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Compensation_Supplier[0].checked) {     
      if (window.document.COI_Form.Compensation_Supplier_Details.value == "") {
      errors += '- You did not enter explanation to Question 4\n';
      }
    }
   }
      
   count = -1;
   for (i = window.document.COI_Form.Business_Not_Covered.length-1; i > -1; i--) {
     if (window.document.COI_Form.Business_Not_Covered[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You have not answered Question 5.\n';
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Business_Not_Covered[0].checked) {     
      if (window.document.COI_Form.Business_Not_Covered_Details.value == "") {
      errors += '- You did not enter explanation to Question 5\n';
      }
    }
   }   
   
      count = -1;
   for (i = window.document.COI_Form.Statement_of_Exceptions_Entered.length-1; i > -1; i--) {
     if (window.document.COI_Form.Statement_of_Exceptions_Entered[i].checked) {
       count = i; i = -1;
     }    
   }
   if (count == -1)  {
    errors += '- You did not specify Statement of Exceptions.\n';
   }
   else {
   //begin check of area text
    if (window.document.COI_Form.Statement_of_Exceptions_Entered[0].checked) {     
      if (window.document.COI_Form.Statement_of_Exceptions.value == "") {
      errors += '- You did not enter Statement of Exceptions.\n';
      }
    }
   }   
     
   return errors;
 }

function validateEthics() {
  var errors = '';
  if (!window.document.COI_Form.Employee_Volunteer[0].checked && !window.document.COI_Form.Employee_Volunteer[1].checked) {
  errors += '- You did not specify EmployeeVolunteer.\n';
  }
  if (window.document.COI_Form.Employee_Volunteer[1].checked) {     
      if (window.document.COI_Form.Understand_Code_of_Ethics == null || !window.document.COI_Form.Understand_Code_of_Ethics.checked) {
		  errors += '- Agree to IEEE Code of Ethics is not checked.\n';
		  }
    }
  return errors;

}


function validateDropDowns() {
  var errors = '';
  var selectedElement = window.document.COI_Form.Coordinator_Email.options[window.document.COI_Form.Coordinator_Email.selectedIndex].value;
 
	if (selectedElement == "") {
		errors += '- You have not selected a Departmental Coordinator Name.\n';
	}

	return errors;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 

dropDownErrors = validateDropDowns();
if (dropDownErrors != null && dropDownErrors != '') {
	errors += dropDownErrors;
}

  radioErrors = validateRadios();
  if (radioErrors != null && radioErrors != '')  {
    errors += radioErrors;
  }

  checkboxErrors = validateCheckBoxes();
  if (checkboxErrors != null && checkboxErrors != '') {
    errors += checkboxErrors;
  }

  ethicsError = validateEthics(); 
  if (ethicsError != null && ethicsError != '') {
     errors += ethicsError;
  }  

  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
  
// set email to selected recipient as well as alternate static address

window.document.COI_Form.submit_to.value = window.document.COI_Form.Coordinator_Email.value + "," + window.document.COI_Form.submit_to_alt.value + "," + window.document.COI_Form.submit_by.value;

// set email cc field to submitter

// window.document.COI_Form.cc_to.value = window.document.COI_Form.submit_by.value;

// set coordinator's name from listing

window.document.COI_Form.Coordinator_Name.value = window.document.COI_Form.Coordinator_Email.options[window.document.COI_Form.Coordinator_Email.selectedIndex].text;
}

