var reCred = /\(C\)$/, reNonc = /\(NC\)$/, reBoth = /\(C\/NC\)$/, reTermWinter = /^Winter/;
var credEle=createSelect("shortSubject");
var noncEle=createSelect("shortSubject");
var bothEle=createSelect("shortSubject");

// Initialize subject select option lists
function iniPage() {
	var sRef=document.getElementById("shortSubject");
	for(var i=1;i<sRef.options.length;i++) {
		ckTxt = sRef.options[i].text;
		ckVal = sRef.options[i].value;
		if(reCred.test(ckTxt)||reBoth.test(ckTxt)) { credEle.appendChild(createOpt(ckTxt,ckVal)); }
		if(reNonc.test(ckTxt)||reBoth.test(ckTxt)) { noncEle.appendChild(createOpt(ckTxt,ckVal)); }
		bothEle.appendChild(createOpt(ckTxt,ckVal));
		}
	onInit();	// Initialize employee ajax lookup
	document.getElementById("subBtn").disabled=false;
}

// Create and return a select element and append an empty option
function createSelect(n) {	
	var newSelect=document.createElement("select");
	newSelect.setAttribute("name",n,false);
	newSelect.setAttribute("id",n,false);
	newSelect.appendChild(createOpt("",""));
	return newSelect;
}
	
// Create and return a option element
function createOpt(t,v) { 	
	var newOpt=document.createElement("option");
	newOpt.appendChild(document.createTextNode(t));
	newOpt.setAttribute("value",v,false);
	return newOpt;
}

// Didn't use a var to hold ref for what ele because would have created a new select and decreased performamnce
function changeSubject(w) {
	var sRef=document.getElementById("shortSubject");
	switch(w) { 
		case 0: sRef.parentNode.replaceChild(noncEle,sRef); break;
		case 1: sRef.parentNode.replaceChild(credEle,sRef); break;
		case 2: sRef.parentNode.replaceChild(bothEle,sRef); break;
		default: sRef.parentNode.replaceChild(bothEle,sRef); break;
	}
	document.getElementById("shortSubject").style.width = "17.5em";
}

var reAny3Letters = /[a-zA-Z]{3}/;
var re1to3DigitNum = /^\d{1,3}$/;
var reWinter = /^Winter/i;
var typeStatus = new Array(1,1,1,1,1,1,1); // accelerated, hybrid, interactive, online, telecourse, traditional, weekend college

function ckForm() {
	var ck = true;
	var fRef = document.getElementById("f_CS");
	
	ck=ckType();

	// determine if special course is selected, if so, no required fields for searching
	if ((fRef.seniorCourses.checked == true) || (fRef.kidsCourses.checked == true) || ((fRef.creditBox.checked == false) && (fRef.noncreditBox.checked == true))) {
		// one of the fields is on, go with no requirements
	} else {
		// neither field is set, require fields
		if(ck==true&&(typeStatus[0]==1||typeStatus[5]==1)) {	// accelerated or traditional is selected
			//if(fRef.termId.value==0) { ck=false; alert("A term is required to search."); }	// term is required
			if(ck==true&&!reWinter.test(fRef.termId.options[fRef.termId.selectedIndex].text)&&fRef.shortSubject.value==""&&fRef.courseNum.value==""&fRef.courseKey.value==""&&fRef.instructor.value=="") { 
				ck=false; alert("A subject, course number, course keyword, or instructor is required to search.");
			}
		}
		else if(ck==true&&typeSum>1) {	// more than one other type is selected, if 1 then good
			if(fRef.shortSubject.value==""&&fRef.courseNum.value==""&fRef.courseKey.value==""&&fRef.instructor.value=="") {
				ck=false; alert("A subject, course number, course keyword, or instructor is required to search.");
			}	
		}
	}
	
	if(ck==true&&fRef.courseNum.value!=""&&re1to3DigitNum.test(fRef.courseNum.value)==false) { ck=false; alert("A course number must be a 1 to 3 digit number.  Ex: 123"); }
	if(ck==true&&fRef.courseKey.value.length>0&&reAny3Letters.test(fRef.courseKey.value)==false) { ck=false; alert("Course keyword must contain at least 3 adjacent letters.  Ex: mat"); }
	if(ck==true&&fRef.instructor.value.length>0&&reAny3Letters.test(fRef.instructor.value)==false) { ck=false; alert("Instructor's name must contain at least 3 adjacent letters.  Ex: Doe"); }
	
	if(ck==false) { fRef.didTriggerValidate.value=1; }	// set that they did hit validation
	return ck;
}

// checks the box associated with the id submitted
function ckBox(boxId) {
	if(document.getElementById(boxId).checked==true) { 
		document.getElementById(boxId).checked=false;
		switchType(boxId);
	} else { 
		document.getElementById(boxId).checked=true; 
		switchType(boxId);
	}
}

// switches the credit / non-credit type based on the selected boxes in the course type section
function switchType() {
	if(document.getElementById('creditBox').checked==true) {
		if(document.getElementById('noncreditBox').checked==true) {
			// credit and noncredit checked
			changeSubject(2);
		} else {
			// only credit checked
			changeSubject(1);
		}
	} else {
		if(document.getElementById('noncreditBox').checked==true) {
			// only noncredit checked
			changeSubject(0);
		}
	}
}

function checkType(typeBox) {
	if ((document.getElementById('creditBox').checked==false) && (document.getElementById('noncreditBox').checked==false)) {
		if (typeBox == 'creditBox') {
			document.getElementById('noncreditBox').checked=true;
			changeSubject(0);
		} else {
			document.getElementById('creditBox').checked=true;				
			changeSubject(1);
		}
	}
}

function ckType(typeSelected) {
	var typeSum = 0;
	var ck=true;
	
	for(var i=0;i<typeStatus.length;i++) { 
		if(document.getElementById("typeId"+(i+1)).checked==true) { typeStatus[i]=1; } else { typeStatus[i]=0; }
		typeSum = typeSum + typeStatus[i]; // count number of ways selected
		}		
	if(typeSum==0) { 
		// 1 type is required
		ck=false; alert("At least one way to take a class is required to search."); 
		document.getElementById(typeSelected).checked=true;
	}
	
	return ck;
}

// used for showing previous terms
function insertOptionBefore(longTerm,termYear,termId) {
	var formSelect = document.getElementById('termId');
	
	if (formSelect.selectedIndex >= 0) {
		var elOptNew = document.createElement('option');
		elOptNew.text =  longTerm + ' ' + termYear;
		elOptNew.value = termId;
		var elOptOld = formSelect.options[1];  
		try {
			formSelect.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			formSelect.add(elOptNew, 1); // IE only
		}
	}
}

// used for adding locations to Location field
function appendOptionLast(optionText, optionValue)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = optionText;
  elOptNew.value = optionValue;
  var elSel = document.getElementById('campusId');

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

// used to remove locations from Location field
function removeOptionLast()
{
  var elSel = document.getElementById('campusId');
  if (elSel.length > 0)
  {
    elSel.remove(elSel.length - 1);
  }
}