/*******************************************************************************/
/*                           Miscellaneous Functions                           */
/*******************************************************************************/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// 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');
} 

function changeImage(obj,ele,state){
	if(state==1){
	docuemnt.obj.getElementById(ele).src='images/edit_purple.gif';
	}
	else
	{
	docuemnt.getElementById(ele).src='images/edit_white.gif';
	}
}

//This function checks the size of a text filed.
function textareaMaxLength(obj,mlen) {

  var maxlen=mlen;
  if (obj.value.length>maxlen) {
    obj.value=obj.value.substring(0, maxlen)
    alert('You have reached your maximum length');
  }
  
}
// This function pops the color picker window
function popColorPicker(){
	window.open('colorpic.html','colorpic','width=300,height=220');
}

/*******************************************************************************/
/*                             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 to validate add and edit groups
function validateIGroup(thisForm){

	if(thisForm.groupName.value==""){
		alert('You must provide a group name.');
		thisForm.groupName.focus();
		return false;
	}
	return true;
}
//This function is to validate add user
function validateAddIUser(thisForm){
	if(!isValidEmail(thisForm.userEmail.value)){
		alert('You must provide a  valid email address.');
		thisForm.userEmail.focus();
		return false;
	}
	return true;
}
//This function is to validate add and edit resourceFolders
function validateRFolder(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(thisForm.categoryName.value==""){
		alert('You must provide a name for the category.');
		thisForm.categoryName.focus();
		return false;
	}
	return true;
}
function validateLink(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(!cboSelectionMade(thisForm.theCat)){
		alert('You must select a category.');
		thisForm.theCat.focus();
		return false;
	}
	if(thisForm.linkURL.value=="" || thisForm.linkURL.value=='http://'){
		alert('You must provide a URL for this link.');
		thisForm.linkURL.focus();
		return false;
	}

return true;
}
function validateAddResource(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(!cboSelectionMade(thisForm.theCat)){
		alert('You must select a category.');
		thisForm.theCat.focus();
		return false;
	}
	if(thisForm.resFile.value==""){
		alert('You must select a file off your local computer.');
		thisForm.resFile.focus();
		return false;
	}
	if(thisForm.resName.value==""){
		alert('You must provide a name for this resource.');
		thisForm.resName.focus();
		return false;
	}
	

return true;
}
function validateEditResource(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(!cboSelectionMade(thisForm.theCat)){
		alert('You must select a category.');
		thisForm.theCat.focus();
		return false;
	}
	if(thisForm.resName.value==""){
		alert('You must provide a name for this resource.');
		thisForm.resName.focus();
		return false;
	}
	

return true;
}
function validateMyProfile(thisForm){
	
return true;
}

function validateRetrieval(thisForm){
	if(!isValidEmail(thisForm.retemail.value)){
		alert('You must provide a  valid email address.');
		thisForm.retemail.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.userFName.value==""){
		alert("Please provide your first name!");
		thisForm.userFName.focus();
		return false;
	}
	if(thisForm.userLName.value==""){
		alert("Please provide your last name!");
		thisForm.userLName.focus();
		return false;
	}
	if(thisForm.userPhone.value==""){
		alert("Please provide your phone number!");
		thisForm.userPhone.focus();
		return false;
	}
	if(!cboSelectionMade(thisForm.department)){
		alert('You must select a department.');
		thisForm.department.focus();
		return false;
	}
	if(thisForm.userPosition.value==""){
		alert("Please provide your position!");
		thisForm.userPosition.focus();
		return false;
	}
	if(thisForm.userLocation.value==""){
		alert("Please provide your Location!");
		thisForm.userLocation.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;
}
function validateCalEntry(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(!cboSelectionMade(thisForm.theCat)){
		alert('You must select a category.');
		thisForm.theCat.focus();
		return false;
	}
	if(!isValidDate(thisForm.ceventFrom.value)){
		alert('You must select a valid date from date.');
		thisForm.ceventFrom.focus();
		return false;
	}
	if(thisForm.ceventHeadline.value==""){
		alert("Please provide a headline!");
		thisForm.ceventHeadline.focus();
		return false;
	}
	if(thisForm.ceventDesc.value==""){
		alert("Please provide a description!");
		thisForm.ceventDesc.focus();
		return false;
	}
	return true;
}
function validateCCategory(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(thisForm.categoryName.value==""){
		alert("Please provide a category name!");
		thisForm.categoryName.focus();
		return false;
	}
	return true;
}
function validateCLink(thisForm){
	if(!cboSelectionMade(thisForm.theGroup)){
		alert('You must select a group.');
		thisForm.theGroup.focus();
		return false;
	}
	if(thisForm.categoryName.value==""){
		alert("Please provide a category name!");
		thisForm.categoryName.focus();
		return false;
	}
	return true;
}