var submit_attempt = 0;

function popupnewsform(target) {
   window.open(target, 'EPIDEPTnews', 'resizable,scrollbars,menubar,width=780,height=540');
}

function checkfields() {
	if (document.contactus.name.value == 0) {
         alert("Please enter a valid name.")
     	   document.contactus.name.focus();
}else	if (document.contactus.email.value == 0) {
         alert("Please enter a valid email address.")
     	   document.contactus.email.focus();
}else	if (document.contactus.comments.value == 0) {
         alert("Please enter a valid comment.")
     	   document.contactus.comments.focus();
}else {
	   document.contactus.submit();
}
}

function checkfields_reg() {
	if (document.registrationform.name.value == 0) {
         alert("Please enter a valid name.")
     	   document.registrationform.name.focus();
}else	if (document.registrationform.email.value == 0) {
         alert("Please enter a valid email address.")
     	   document.registrationform.email.focus();
}else	if (document.registrationform.attending.value == 0) {
         alert("Please enter number attending.")
     	   document.registrationform.attending.focus();
}else {
	   document.registrationform.submit();
}
}

function checkfieldsml() {
	if (document.mailinglist.name.value == 0) {
         alert("Please enter a valid name.")
     	   document.mailinglist.name.focus();
}else	if (document.mailinglist.email.value == 0) {
         alert("Please enter a valid email address.")
     	   document.mailinglist.email.focus();
}else {
	   document.mailinglist.submit();
}
}

function newsentrycheck() {
	if (document.NewsEntry.Date.value == "") {
   		alert("DATE cannot be blank");
   		document.NewsEntry.Date.focus();
	} else if (document.NewsEntry.Headline.value == "") {
    		alert("HEADLINE cannot be blank");
    		document.NewsEntry.Headline.focus();
	} else if (document.NewsEntry.Author.value == "") {
   		 alert ("AUTHOR cannot be blank");
   		 document.NewsEntry.Author.focus();
	} else if (document.NewsEntry.Email.value == "") {
    		alert ("AUTHOR EMAIL cannot be blank");
    		document.NewsEntry.Email.focus();
	} else if (document.NewsEntry.Article.value == "") {
    		alert ("ARTICLE BODY cannot be blank");
    		document.NewsEntry.Article.focus();
      } else if (document.NewsEntry.Summary.value == "") {
    		alert("Article Caption cannot be blank");
    		document.NewsEntry.Summary.focus();
	} else {
            if(!(document.NewsEntry.Featured.checked)){
	         document.NewsEntry.Featured.checked = true
	         document.NewsEntry.Featured.value = "Off"
	         document.NewsEntry.Featured.style.visibility = "hidden";
            }
		if (submit_attempt == 0) {
			submit_attempt = submit_attempt + 1;
			document.NewsEntry.submit();
		}
	}
}

function groupcheck(myarray) {
	for (var i = 1; i < myarray.length; i++) {
		if(!(document.getElementById(myarray[i]).checked)){
	         document.getElementById(myarray[i]).checked = true
	         document.getElementById(myarray[i]).value = "Off"
	         document.getElementById(myarray[i]).style.visibility = "hidden";
            }
	}
}

function newsentrycheck2() {
	if (document.NewsEntry.Date.value == "") {
   		alert("DATE cannot be blank");
   		document.NewsEntry.Date.focus();
	} else if (document.NewsEntry.Headline.value == "") {
    		alert("HEADLINE cannot be blank");
    		document.NewsEntry.Headline.focus();
	} else if (document.NewsEntry.When.value == "") {
    		alert("WHEN cannot be blank");
    		document.NewsEntry.When.focus();
	} else if (document.NewsEntry.TimeFinish.value == '' && document.NewsEntry.TimeStart.value != '') {
   		alert("End Time required to complement Start Time");
   		document.NewsEntry.TimeFinish.focus();
	} else if (document.NewsEntry.TimeFinish.value != '' && document.NewsEntry.TimeStart.value == '') {
    		alert("Start Time required to complement End Time");
    		document.NewsEntry.TimeStart.focus();
	} else if (document.NewsEntry.Where.value == "") {
    		alert("WHERE cannot be blank");
    		document.NewsEntry.Where.focus();
	} else if (document.NewsEntry.Author.value == "") {
   		 alert ("AUTHOR cannot be blank");
   		 document.NewsEntry.Author.focus();
	} else if (document.NewsEntry.Email.value == "") {
    		alert ("AUTHOR EMAIL cannot be blank");
    		document.NewsEntry.Email.focus();
	} else if (document.NewsEntry.Article.value == "") {
    		alert ("ARTICLE BODY cannot be blank");
    		document.NewsEntry.Article.focus();
      } else if (document.NewsEntry.Summary.value == "") {
    		alert("Article Caption cannot be blank");
    		document.NewsEntry.Summary.focus();
	} else {
            if(!(document.NewsEntry.Featured.checked)){
	         document.NewsEntry.Featured.checked = true
	         document.NewsEntry.Featured.value = "Off"
	         document.NewsEntry.Featured.style.visibility = "hidden";
            }
		if (submit_attempt == 0) {
			submit_attempt = submit_attempt + 1;
			document.NewsEntry.submit();
		}
	}
}

function newsentrycheckSS() {
	if (document.NewsEntry.Date.value == "") {
   		alert("DATE cannot be blank");
   		document.NewsEntry.Date.focus();
	} else if (document.NewsEntry.Headline.value == "") {
    		alert("HEADLINE cannot be blank");
    		document.NewsEntry.Headline.focus();
	} else if (document.NewsEntry.Speaker.value == "") {
    		alert("SPEAKER cannot be blank");
    		document.NewsEntry.Speaker.focus();
	} else if (document.NewsEntry.When.value == "") {
    		alert("WHEN cannot be blank");
    		document.NewsEntry.When.focus();
	} else if (document.NewsEntry.TimeFinish.value == '' && document.NewsEntry.TimeStart.value != '') {
   		alert("End Time required to complement Start Time");
   		document.NewsEntry.TimeFinish.focus();
	} else if (document.NewsEntry.TimeFinish.value != '' && document.NewsEntry.TimeStart.value == '') {
    		alert("Start Time required to complement End Time");
    		document.NewsEntry.TimeStart.focus();
	} else if (document.NewsEntry.Where.value == "") {
    		alert("WHERE cannot be blank");
    		document.NewsEntry.Where.focus();
	} else if (document.NewsEntry.Author.value == "") {
   		 alert ("AUTHOR cannot be blank");
   		 document.NewsEntry.Author.focus();
	} else if (document.NewsEntry.Email.value == "") {
    		alert ("AUTHOR EMAIL cannot be blank");
    		document.NewsEntry.Email.focus();
	} else if (document.NewsEntry.Article.value == "") {
    		alert ("ARTICLE BODY cannot be blank");
    		document.NewsEntry.Article.focus();
      } else if (document.NewsEntry.Summary.value == "") {
    		alert("Article Caption cannot be blank");
    		document.NewsEntry.Summary.focus();
	} else {
            if(!(document.NewsEntry.Featured.checked)){
	         document.NewsEntry.Featured.checked = true
	         document.NewsEntry.Featured.value = "Off"
	         document.NewsEntry.Featured.style.visibility = "hidden";
            }
		if (submit_attempt == 0) {
			submit_attempt = submit_attempt + 1;
			document.NewsEntry.submit();
		}
	}
}


function setdefaultdisplayday() {
	document.NewsEntry.DDay.value = catday[document.NewsEntry.CatID.selectedIndex];
}


function setdefaultheadlinecolor() {
	for (var i = 0; i < document.NewsEntry.HColor.length ;i++) {
		if (document.NewsEntry.HColor.options[i].value.toUpperCase() == catcolor[document.NewsEntry.CatID.selectedIndex].toUpperCase())  {
			document.NewsEntry.HColor.options[i].selected = true;
			return;	
		}
	}
}


function setdefaultnewstype() {
	document.NewsEntry.TypeID.length = 0;
	for (var i = 0; i < newstypeid.length ;i++) {
		var newopt = new Option();
		newopt.value = newstypeid[i];
		newopt.text = newstypedisp[i];
		if (!(cattype[document.NewsEntry.CatID.selectedIndex].toUpperCase() != 'UPLOAD' && newstypeid[i].toUpperCase() == 'UPLOAD')) {
			document.NewsEntry.TypeID.options[document.NewsEntry.TypeID.length] = newopt;
		}
	}
	for (var i = 0; i < document.NewsEntry.TypeID.length ;i++) {
		if (document.NewsEntry.TypeID.options[i].value.toUpperCase() == cattype[document.NewsEntry.CatID.selectedIndex].toUpperCase())  {
			document.NewsEntry.TypeID.options[i].selected = true;
			return;	
		}
	}
}


function dispdaychk(ddayfield) {
	ddayfield.value = ddayfield.value.replace(/ /g, '');
	if (isNaN(ddayfield.value) || Number(ddayfield.value) <= 0) {
		alert("Please enter a positive integer number for Display Day(s).");
		ddayfield.focus();
		ddayfield.select();
	}
	else  {
		ddayfield.value = Number(ddayfield.value);
		for (var i = 0; i+1 <= ddayfield.value.length; i++) {
			if (ddayfield.value.charAt(i) == ".")  {
				alert("Please enter a positive integer number for Display Day(s).");
				ddayfield.focus();
				ddayfield.select();
			}
		}		
	}	
}


function isInteger(intstr) {   
	var tf = 1;
	if (intstr.length == 0) {
	   tf = 0;
	} else if ( (intstr.length == 1 && intstr.charAt(0) == "-" ) ||
	       (intstr.length == 2 && intstr.charAt(0) == "-" &&
	       intstr.charAt(1) == "0") ) {
	   tf = 0;
	} else {
	  for (var i = 0; i+1 <= intstr.length; i++) {
		var intval = parseFloat(intstr.charAt(i));
		if ( !(intstr.charAt(i) == "-" && i == 0)
			&& (isNaN(intval)) ) {
		     tf = 0;
		}
	  }
	}
	if (tf == 0) {
	   return(false);
	} else {
	   return(true);
	}
}

function isDate(datestr) {
	var locsep = 0;
	var iserr = false;
	var tmon;
	var tday;
	var tyear;
	var today;
	var currentyear;
	var sep = "/";
	var sep1;
	var sep2;

	  sep1 = datestr.indexOf(sep,locsep);


	var verifydate = true;
 
	while ( verifydate == true ) {
	  if (sep1 == datestr.lastIndexOf(sep) ||
		(sep1 == 0) || (sep1 == -1)) {
	      iserr=true; break;
	  }

	  locsep = sep1+1

	  sep2 = datestr.indexOf(sep,locsep);
	  if (!(sep2 == datestr.lastIndexOf(sep)) ||
		(sep2 == sep1+1) || (sep2 == -1)) {
	      iserr=true; break;
	  }

	  
	  tmon = datestr.substring(0,sep1);

	  var inttmp = parseFloat(tmon);

	  
	  if (tmon.length == 1) {
		tmon = "0"+tmon;
	  }


	  if (!(isInteger(tmon))) {
		iserr=true; break;
	  } else if (!(1 <= inttmp && inttmp <= 12)) {
		iserr=true; break;
	  }
	

	  tday = datestr.substring(sep1+1,sep2);

	  var inttmp = parseFloat(tday);


	  if (tday.length == 1) {
		tday = "0"+tday;
	  }
	  if (!(isInteger(tday))) {
		iserr=true; break;
	  } else if (!(1 <= inttmp && inttmp <= 31)) {
		iserr=true; break;
	  } else if ( (tmon == 4 || tmon == 6 || tmon == 9 || tmon == 11) && tday == 31 ) {
		iserr=true; break;
	    } else if ( tmon == 2 && tday >= 30 ) {
		iserr=true; break;
	  }


	  tyear = datestr.substring(sep2+1,datestr.length);

	  if (tyear.length == 2) {
		today = new Date();
		currentyear = today.getYear();
		if ( 0 <= currentyear && currentyear <= 99 ) {
			currentyear = 19
		} else if (100 <= currentyear <= 199) {
		     currentyear = 20;
		} else {
			currentyear = "" + currentyear
			currentyear = currentyear.substring(0,2)
		}

//		if ( currentyear < 0) {
//		     currentyear = 18;
//		} else if (0 <= currentyear <= 99 ) {
//		     currentyear = 19;
//		} else if (100 <= currentyear <= 199) {
//		     currentyear = 20;
//		} else if (200 <= currentyear <= 299) {
//		     currentyear = 30;
//		}
   
		tyear = currentyear+tyear;
	  }


	  var inttmp = parseFloat(tyear);


	  if (!(isInteger(tyear))) {
		iserr=true; break;
	  } else if (!(1000 <= inttmp && inttmp <= 2099)) {
		iserr=true; break;
	  } else if ( (tyear.substring(2,4) == "00") && 
		 (tmon == 2 && tday == 29 && ! (tyear/400 == parseInt(tyear/400))) ) {
		iserr=true; break;
	  } else if ( tmon == 2 && tday == 29 && !( tyear/4 == parseInt(tyear/4)) ) {
		iserr=true; break;
	  }

	  verifydate = false
  }

    if (iserr) {
       return(null);
    } else {
       this.datevalue = (tmon+"/"+tday+"/"+tyear);
       return(this.datevalue);
    }
} 


function datechk(nam,val,low,high,nv,mv,req,vnam,lname) {
	  var dateval = isDate(val);
	  if (dateval != null) {
	     nam.value = dateval;

	     var tmon = parseFloat(dateval.substring(0,2));
	     var tday = parseFloat(dateval.substring(3,5));
	     var tyear = parseFloat(dateval.substring(6,11));

	     var lmon = parseFloat(low.substring(0,2));
	     var lday = parseFloat(low.substring(3,5));
	     var lyear = parseFloat(low.substring(6,11));

	     var hmon = parseFloat(high.substring(0,2));
	     var hday = parseFloat(high.substring(3,5));
	     var hyear = parseFloat(high.substring(6,11));

	     var mmon = parseFloat(mv.substring(0,2));
	     var mday = parseFloat(mv.substring(3,5));
	     var myear = parseFloat(mv.substring(6,11));

	     var nmon = parseFloat(nv.substring(0,2));
	     var nday = parseFloat(nv.substring(3,5));
	     var nyear = parseFloat(nv.substring(6,11));

	     var dval = (tmon*30.4375)+(tday)+(tyear*365.25);
	     var lval = (lmon*30.4375)+(lday)+(lyear*365.25);
	     var hval = (hmon*30.4375)+(hday)+(hyear*365.25);
	     var mval = (mmon*30.4375)+(mday)+(myear*365.25);
	     var nval = (nmon*30.4375)+(nday)+(nyear*365.25);

	     today = new Date();
//	     var tval = ( (today.getMonth()+1)*30.4375 )+( today.getDate() )
//		+( (today.getYear()+1900)*365.25 );


		if ( 0 <= today.getYear() && today.getYear() <= 199 ) {
	     		var tval = ( (today.getMonth()+1)*30.4375 )+( today.getDate() )
				+( (today.getYear()+1900)*365.25 );
		} else {
		 	var tval = ( (today.getMonth()+1)*30.4375 )+( today.getDate() )
				+( (today.getYear())*365.25 );
		}

	     if (! (( lval <= dval && dval <= hval ) || dval == nval || dval == mval)) {
		 alert(lname+" value " + dateval + " is out of range [" + low + ":" + high + "]");
		 fail = vnam;
		 nam.focus();
		     nam.select();
//	      }  else if ( dval > tval && dval != nval && dval != mval ) {
//		alert(lname+" value " + dateval + " is not prior to today. ");
//		 fail = vnam;
//		 nam.focus();
//		     nam.select();
	      } else if ( req == "yes" && ( val == mv) ) {
		alert(lname+" is a REQUIRED field.");
		fail = vnam;
		nam.focus();
		nam.select();
	      } else {    
	      fail = "no"; 
		 }
	  } else if (val == "") {
	      if ( req == "yes" ) {
		alert(lname+" is a REQUIRED field.");
		fail = vnam;
		nam.focus();
		nam.select();
	      } else {
		nam.value = mv;
		  fail = "no";
	      }
	  } else  {
	      alert(lname+" value is not a valid DATE value.");
	      fail = vnam;
	      nam.focus();
		  nam.select();
	  }
}

