/*******************************************************************************/
/*                           Miscellaneous Functions                           */
/*******************************************************************************/

// This function checks to see if a selection was made in a checkbox group
function cbxSelectionMade(cbx){
	var isGood=false;
	if(cbx.checked){
		isGood=true;
	}
	for(i=0;i<cbx.length && !isGood;i++){
		if(cbx[i].checked){
			isGood=true;
		}
	}
	return isGood;
}

// This function checks to see if a selection was made in a dropdown box
function cboSelectionMade(cbo){
	var isGood=false;
	
	if(cbo.selectedIndex>0&&cbo.options[cbo.selectedIndex].value!="")
		isGood=true;
		
	return isGood;
}

// This function ensures the remove checkbox is unchecked if the keep box is checked
// on the newComments.cfm form
function keepIt(idx){
	var thisForm=document.fComment;
	if(thisForm.keep.checked){
		thisForm.remove.checked=false;
		return 0;
	}
	if(thisForm.keep[idx].checked)
		thisForm.remove[idx].checked=false;
}

// This function ensures the keep checkbox is unchecked if the remove box is checked
// on the newComments.cfm form
function dumpIt(idx){
	var thisForm=document.fComment;
	if(thisForm.remove.checked){
		thisForm.keep.checked=false;
		return 0;
	}
	if(thisForm.remove[idx].checked)
		thisForm.keep[idx].checked=false;
}

//call a calendar
var calendar;
function ShowCalendar(formname,boxname,boxdate){
	calendar=window.open('show_calendar.cfm?boxname='+boxname+'&boxdate='+boxdate+'&formname='+formname,'calendar','scrollbars=0,width=230,height=190, top=40,left=40');
} 

/*******************************************************************************/
/*                             Validation Functions                            */
/*******************************************************************************/

// This function is used to validate email entries
function isValidEmail(thisEmail){
	var emailexp = /.*\@.*\..*/;
	if (!emailexp.test(thisEmail)) {
		return false;
	} 
	return true;
}

// function checks a date value to make sure its valid
function isValidDate(aDate) {
	var dateexp = /^(\d{1,2}\/\d{1,2}\/\d{4})$/;
    if (!dateexp.test(aDate)) { return false; } 	
	var	temp = new String(aDate), m = 0, d = 0;
	
	for (i=0; i < temp.length; i++){ 
		var str = temp.charAt(i); 
		if (str == "/") { if (m == 0) { m = i; } else { d = i; break; }}			
	}
	
	var month = parseInt(temp.substring(0, m), 10);
	var day = parseInt(temp.substring(m + 1, d), 10); 
	var year = parseInt(temp.substring(d + 1, temp.length), 10);								
					
	switch (month) {
		case 1:	case 3:	case 5:	case 7:	case 8:	case 10: case 12:					
			if ((day < 1) || (day > 31)) {  return false;	} break;
		case 2:		
			if ((year % 400 == 0) || (year % 4 == 0)) {	if ((day < 1) || (day > 29)) {  return false;	} break}
			if ((day < 1) || (day > 28)) {  return false; } break;
		case 4:	case 6:	case 9: case 11:
			if ((day < 1) || (day > 30)) {	 return false; } break;
		default: return false;			
	}		
	return true;		
}

// This function is for validating intranet user submissions
function validateIUser(thisForm,mode){
	if(thisForm.userFName.value==""){
		alert("You must provide a first name for the user!");
		thisForm.userFName.focus()
		return false
	}
	if(thisForm.userLName.value==""){
		alert("You must provide a last name for the user!");
		thisForm.userLName.focus()
		return false
	}
	if(thisForm.userEmail.value==""||!isValidEmail(thisForm.userEmail.value)){
		alert("You must provide a valid email address!");
		thisForm.userEmail.focus()
		return false
	}
	
	return true;
}

// This function is for validating my profile submissions
function validateMyProfile(thisForm){
	if(thisForm.userFName.value==""){
		alert("You must provide a first name for the user!");
		thisForm.userFName.focus()
		return false
	}
	if(thisForm.userLName.value==""){
		alert("You must provide a last name for the user!");
		thisForm.userLName.focus()
		return false
	}
	if(thisForm.userEmail.value==""||!isValidEmail(thisForm.userEmail.value)){
		alert("You must provide a valid email address!");
		thisForm.userEmail.focus()
		return false
	}
	if(thisForm.confirmPassword.value!=thisForm.userPassword.value){
		alert("The password and confirm password do not match!");
		thisForm.confirmPassword.focus()
		return false
	}
	
	return true;
}

// This function is for validating login retrieval submissions
function validateRetrieval(thisForm){
	if(thisForm.retemail.value==""||!isValidEmail(thisForm.retemail.value)){
		alert("You must provide an email address!");
		thisForm.retemail.focus()
		return false
	}
	
	return true;
}

// This function is for validating section submissions
function validateSection(thisForm){
	if(thisForm.secName.value==""){
		alert("You must provide an name for this section!");
		thisForm.secName.focus()
		return false
	}
	if(!cboSelectionMade(thisForm.groupID)){
			alert("You must choose a group for this section!");
			thisForm.groupID.focus()
			return false
		}
	
	return true;
}

// This function is for validating resource submissions
function validateResource(thisForm,mode){
	if(mode==0){
		if(!cboSelectionMade(thisForm.secID)){
			alert("You must choose a section for this resource!");
			thisForm.secID.focus()
			return false
		}
	}
	if(thisForm.docTitle.value==""){
		alert("You must provide an title for this resource!");
		thisForm.docTitle.focus()
		return false
	}
	if(thisForm.docDescription.value==""){
		alert("You must provide an description for this resource!");
		thisForm.docDescription.focus()
		return false
	}
	if(mode==0){
		if(thisForm.docFile.value==""){
			alert("You must choose a file to upload for this resource!");
			thisForm.docFile.focus()
			return false
		}
	}
	
	return true;
}

function validateActivation(thisForm){
	if(thisForm.userEmail.value==""||!isValidEmail(thisForm.userEmail.value)){
		alert("Please provide a valid email!");
		thisForm.userEmail.focus();
		return false;
	}
	if(thisForm.userUsername.value==""){
		alert("Please provide a Username!");
		thisForm.userUsername.focus();
		return false;
	}
	if(thisForm.userPassword.value==""){
		alert("Please provide a Password!");
		thisForm.userPassword.focus();
		return false;
	}
	if(thisForm.userPassword.value!=thisForm.confirmPassword.value){
		alert("Your password and confirmation password do not match!");
		thisForm.confirmPassword.focus();
		return false;
	}
	
	return true;
}