var badTimeFlag = true;

function mySubmit(form) {
	setDataFields(form);
	form.action = "preview.asp";
	if ( Validator(form) ) {
		form.submit();
	}
}

function chkEtv(form){
	if (form.etvDesc.value=="") {
		form.etvDesc.value = form.Title.value;
	}
}

function Validator(theForm) {
  
  if (theForm.Title.value == "") {
    alert("Please enter a value for the \"Title\" field.");
    theForm.Title.focus();
    return (false);
  }

  if ( theForm.bMonth.selectedIndex == 0 ) {
	alert("Please enter begin Month.");
    	theForm.bMonth.focus();
    	return (false);
  }
  if ( theForm.bDay.selectedIndex == 0 ) {
	alert("Please enter begin day.");
    	theForm.bDay.focus();
    	return (false);
  }
  if ( compareToday(theForm.bDate.value)<0 ) {			
  		alert("You must enter a date bigger than " + theForm.bDate.value)
  		return false;			
  } 
  if ((new Date(theForm.eDate.value))<(new Date(theForm.bDate.value))) {
		alert("You must enter an end date bigger than " + theForm.bDate.value + " or leave it blank.")
  		return false;
  }  
  if ( theForm.StartHour.selectedIndex == 0  && theForm.cTime.checked ) {
	alert("Please specify beginning time.");
    	theForm.StartHour.focus();
    	return (false);
  }
  	
  if (badTimeFlag && !confirm("Are you sure that you want the ending time to be less than the starting time?")) {
	theForm.StartHour.focus();
    	return false;
  }

  if ( theForm.Campus.selectedIndex == 0 ) {
		alert("Please select a  campus.");
    	theForm.Campus.focus();
    	return (false);
  }
	
  if ( theForm.School.selectedIndex == 0 ) {
		alert("Please select a  School/Unit.");
    	theForm.School.focus();
    	return (false);
  }

  if ( theForm.BuildingOther.value.length<2 ) {
	alert("Please select or enter a building name.");
    	theForm.BuildingOther.focus();
    	return (false);
  }

  var s1 = theForm.School.options[theForm.School.selectedIndex].value;
  if ( s1.indexOf("VCU Reg") == 0 && theForm.SponsorOther.value == "" ) {
	alert("Please enter a organization name.");
    	theForm.SponsorOther.focus();
    	return (false);
  }
  if ( theForm.SponsorOther.value == "" ) {			//SponsorOther
	alert("Please select or enter a Sponsor name.");
    	theForm.School.focus();
    	return (false);
  }
  if (theForm.SponsorOther.value.length > 250) {
    alert("Please enter at most 250 characters in the \"SponsorOther\" field.");
    	theForm.SponsorOther.focus();
    	return (false);
  }
  if ( theForm.Description.value.length < 4 ) {
	alert("Please enter a description.");
    	theForm.Description.focus();
    	return (false);
  }

  if (theForm.SubmitterName.value == "") {
    alert("Please enter a value for the \"SubmitterName\" field.");
    theForm.SubmitterName.focus();
    return (false);
  }

  if (theForm.SubmitterPhone.value == "") {
    alert("Please enter a value for the \"SubmitterPhone\" field.");
    theForm.SubmitterPhone.focus();
    return (false);
  }

  if ( theForm.SubmitterEmail.value != "" ) {
     if ( notValidEMail( theForm.SubmitterEmail.value ) ) {        // email address checking
		alert("Not a valid email address.");
		theForm.SubmitterEmail.focus();
		return (false);
     }
  } else {
    alert("Please enter a value for the \"SubmitterEmail\"  field.");
    theForm.SubmitterEmail.focus();
    return (false);
  }

  if (theForm.AccomodationsName.value == "") {
    alert("Please enter a value for the \"AccomodationsName\" field.");
    theForm.AccomodationsName.focus();
    return (false);
  }

  if (theForm.AccomodationsPhone.value == "") {
    alert("Please enter a value for the \"AccomodationsPhone\" field.");
    theForm.AccomodationsPhone.focus();
    return (false);
  }

  if ( theForm.AccomodationsEmail.value != "" ) {
     if ( notValidEMail( theForm.AccomodationsEmail.value ) ) {        // email address checking
	alert("Not a valid email address.");
	theForm.AccomodationsEmail.focus();
	return (false);
     }
  }

  if ( theForm.Category.selectedIndex == 0 ) {
		alert("Please select a  Category.");
    	theForm.Category.focus();
    	return (false);
  }
  
  return true;
}

function compareToday(s1) {
	var toDay = new Date();
	var theDay = new Date(s1);
	var d0 = toDay.getDate();
	var m0 = toDay.getMonth();
	var y0 = toDay.getYear();
	var d1 = theDay.getDate();
	var m1 = theDay.getMonth();
	var y1 = theDay.getYear();
	if ( ( y1<y0 ) || ( y1==y0 && m1<m0 ) || ( y1==y0 && m1==m0 && d1<d0 ) ) {			
  		return -1;			
	}
	if ( y1==y0 && m1==m0 && d1==d0  ) {			
  		return 0;			
	}
	return 1;
}

function notValidEMail(aString) {
        var i = aString.indexOf("@");           // ([i|j]<1) for first or none @
        var j = aString.lastIndexOf(".");       // (i>j-2) for '@.'
        var k = aString.length;                 // (i|j vs k) for ending with @.
        if ( ( i < 1 ) || ( j < 1 ) || ( i > k-3 ) || ( i > j-2 ) || ( j > k-2 ) ) {
                return true;
        }
        return false;
}


function setSameData( form1, v1 ) {
	with ( form1 ) {
		if ( v1 == 1 ) {
			AccomodationsEmail.value = SubmitterEmail.value;
			AccomodationsName.value = SubmitterName.value;
			AccomodationsPhone.value = SubmitterPhone.value;
		} else {
			AccomodationsEmail.value = "";
			AccomodationsName.value = "";
			AccomodationsPhone.value = "";	
		}    
	}
}



function setDataFields(form1) {
	var str1 = "";
	var str2 = "";
	var tmp1 = "";
	var tmp2 = "";
	var tmp = 0;
	with (form1) {
		if ( StartHour.selectedIndex != 0 ) {
			tmp1 = StartHour.options[StartHour.selectedIndex].text;
			tmp2 = StartMin.options[StartMin.selectedIndex].text;
			tmp = tmp1.length - 3;
			str1 = tmp1.substr(0, tmp) + ":" + tmp2 + tmp1.substr(tmp)
			bTime.value = str1;

			str1 = checkNoon(str1); 	// for theTime string
		} else {
			str1 = "08:00 AM";
			bTime.value = str1;
		}
		if ( EndHour.selectedIndex != 0 ) {
			tmp1 = EndHour.options[EndHour.selectedIndex].text;
			tmp2 = EndMin.options[EndMin.selectedIndex].text;
			tmp = tmp1.length - 3;
			str2 = tmp1.substr(0, tmp) + ":" + tmp2 + tmp1.substr(tmp)
			eTime.value = str2;

			str2 = checkNoon(str2);		// for theTime string
			str1 = str1 + " - " + str2;	// for theTime string
			if ( ( EndHour.selectedIndex > StartHour.selectedIndex ) ||
				( ( EndHour.selectedIndex == StartHour.selectedIndex ) &&
				( EndMin.selectedIndex > StartMin.selectedIndex ) ) ) {
				badTimeFlag = false;
			}
		} else if ( cTime.checked ) {
			str1 = str1 + " (All day event)";
			badTimeFlag = false;
		}		
		
		theTime.value = str1;			// for theTime string
		if (( bMonth.selectedIndex != 0 )  && (bDay.selectedIndex != 0 ) ) {
		   bDate.value = bMonth.options[bMonth.selectedIndex].value + "/" + bDay.options[bDay.selectedIndex].text + "/" + bYear.options[bYear.selectedIndex].text;
		} else {	bDate.value = ""; }
		if ( (eMonth.selectedIndex != 0 ) && (eDay.selectedIndex != 0 ) ) {
		   eDate.value = eMonth.options[eMonth.selectedIndex].value + "/" + eDay.options[eDay.selectedIndex].text + "/" + eYear.options[eYear.selectedIndex].text;
		} else {	eDate.value = ""; }
		
		if ( Buildings.selectedIndex != 0 ) {
			BuildingOther.value = Buildings.options[Buildings.selectedIndex].text
		}

		if ( School.selectedIndex>0) {
			str1 = School.options[School.selectedIndex].value;
			if ( Dept.selectedIndex > 0 ) {
				str1 = str1 + ":" + Dept.options[Dept.selectedIndex].value;
			}
			SponsorOther.value = str1;
		}
		
		OrganizationURL.value = OrganizationURL.value.replace("http://", "");
		EventURL.value = EventURL.value.replace("http://", "");
	}
	
	return;
}

function checkNoon (str) {
	if ( str.indexOf("12:00")==0 ) {
		if (str.indexOf(" pm")>0 ) {
			str = "Noon";
		} else {
			str = "Midnight";
		}
	}
	return str;
}
var cbFlag = 1;
function cckBoxAll(form1) {
	var i;
	cbFlag = 0;
	if ( form1.Audience[0].checked ) {
		for(i=1; i<form1.Audience.length; i++ ) {
			if ( form1.Audience[i].checked ) {
				form1.Audience[i].checked = false;				
			}
		}
		form1.Audience[0].checked = true;
	}
	cbFlag = 1;
}
function cckBox(form1) {
	if ( form1.Audience[0].checked ) {
		form1.Audience[0].checked = false;
	} 
}
function checkHead(obj) {
	if (obj.value.length>8) {
		var theHead = obj.value.substr(0,4).toUpperCase();
		if ( theHead != "HTTP" ) {
			obj.value = "http://" + obj.value;
		}
	} else {
		obj.value = "";
	}
}

////////////////////////////////////////////////////////////////////////
// School option and building option
////////////////////////////////////////////////////////////////////////
//Print out options 
function prtOption(aryObjVal, aryObjTxt) {
	var i =0;
	var intCount = aryObjVal.length
	for (i=0; i<intCount; i++) {
		document.writeln("<option value='" + aryObjVal[i] + "'>" + aryObjTxt[i] + "</option>");
	}
	return;
}
function prtOptionSearch(aryObjVal, aryObjTxt, firstShow) {
	var i =1;
	var intCount = aryObjVal.length
	document.writeln("<option value=''>" + firstShow + "</option>");
	for (i=1; i<intCount; i++) {
		document.writeln("<option value='" + aryObjVal[i] + "'>" + aryObjTxt[i] + "</option>");
	}
	return;
}

//Print out options, set selected by matching a string
function prtOption2(aryObjVal, aryObjTxt, strMatch) {
	var i =0;
	var intCount = aryObjVal.length
	for (i=0; i<intCount; i++) {
		if ( aryObjVal[i].indexOf(strMatch) == 0 ) {
			document.writeln("<option value='" + aryObjVal[i] + "' selected>" + aryObjTxt[i] + "</option>");
		} else {
			document.writeln("<option value='" + aryObjVal[i] + "'>" + aryObjTxt[i] + "</option>");
		}
	}
	return;
}
function prtEmptyOptionList(strFirst) {
	var i;
	var space1 = " &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;";
	document.writeln("<option value=''>" + strFirst + "</option>");
	for (i=0; i<intArySize; i++) {
		document.writeln("<option value=''>"+space1+"</option>");
	}
}

function ClearList(OptionList, TitleName) {	
    	OptionList.length = 0;
  	OptionList.options[0] = new Option(TitleName, '', true, true);
	return;
}

function LoadList(Index, obj1, strAry) {
	if (strAry[Index-1]==null) {
		return;
	} 
    	if (Index > 0) {
  	  	var CurrentOption = strAry[Index-1].split('|');
  		for (var i=0; i<CurrentOption.length; i++) {
  			obj1.options[i+1] = new Option(CurrentOption[i], CurrentOption[i], false, false);
  			//next line added by JLK to set "Model" to selected state until chosen
  			obj1.options[0].selected = true;
  	  	}
  	} else {
		obj1.options[1] = new Option("", "", false, false);
	}
	return;
}

function ChangeList(CalledFromOnLoad, obj, obj1, aStrAry) {	
    	//alert("obj="+obj.name+"\nobj1="+obj1.name);
    	// Clear both of the lists first then add the options if valid Make has been selected. -GDC 
	var CurrItem = obj1.selectedIndex;
	ClearList(obj1, '\(Select a Item\)');
	if (CalledFromOnLoad == 0) {
		LoadList(obj.selectedIndex, obj1,aStrAry);
	} else {
		LoadList(1, obj1,aStrAry);
		// if ChangeList was called when loading the page then restore the choice that was already here. -GDC 
  	  	obj1.selectedIndex = CurrItem;
  	}			
}

var emptyStrAry = new Array();
emptyStrAry[0] = "";
function initList(theForm) {
	ChangeList(1, theForm.Campus, theForm.Buildings, emptyStrAry);
	//ChangeList(0, theForm.Campus, theForm.Buildings, buildings) 	//20060125 had a bug when back from preview
	ChangeList(1, theForm.Campus, theForm.Buildings, buildings)
	ChangeList(1, theForm.School, theForm.Dept, emptyStrAry);
	//ChangeList(0, theForm.School, theForm.Dept, depts) 			//20060125 had a bug when back from preview
	ChangeList(1, theForm.School, theForm.Dept, depts)
	return;
}  

function reSelectDept(theForm, strMatch) {
	var dCount = theForm.Dept.options.length;
	for (var i=0; i<dCount; i++) {
		if ( theForm.Dept.options[i].value.indexOf(strMatch) == 0 ) {
			theForm.Dept.selectedIndex = i;
			break;
		}
  	}
	return;
}  

function filterIt(obj,s1,s2) {
	var str1 = obj.value;
	str1 = filterStr(str1,s1,s2)
	str1 = filterStr(str1,"--","- -");
	obj.value = str1;
}


function chkTitle(obj){
	var str0 = obj.value;
	str0 = filterStr(str0,"<b>","");
	str0 = filterStr(str0,"</b>","");
	str0 = filterStr(str0,"<B>","");
	str0 = filterStr(str0,"</B>","");
	str0 = filterStr(str0,"<i>","");
	str0 = filterStr(str0,"</i>","");
	str0 = filterStr(str0,"<I>","");
	str0 = filterStr(str0,"</I>","");
	str0 = filterStr(str0,"<strong>","");
	str0 = filterStr(str0,"</strong>","");
	str0 = filterStr(str0,"<STRONG>","");
	str0 = filterStr(str0,"</STRONG>","");
	obj.value = str0;
}
function filterStr(str0,s1,s2) {
	while ( str0.indexOf(s1)>=0 ) {
		str0 = str0.replace(s1,s2);
	}
	return str0;
}

