function goBox(theItem) {
	destURL = trim(theItem.options[theItem.selectedIndex].value);
	theItem.options[0].selected=true;
	if (destURL!="") {
		top.location = destURL;
	}
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

function validate_LoginForm(theForm) {
	if (trim(theForm.username.value)=="") {
		alert("You must enter your username before proceeding.");
		theForm.username.value="";
		theForm.username.focus();
		return false;
	}
	if (theForm.userpass.value=="") {
		alert("You must enter your password before proceeding.");
		theForm.userpass.value="";
		theForm.userpass.focus();
		return false;
	}
}

function validate_RegistrationForm(theForm) {
	//trim all fields
	vUsername     = trim(theForm.username.value);
	vUserpass1    = trim(theForm.userpass1.value);
	vUserpass2    = trim(theForm.userpass2.value);
	vEmailAddress = trim(theForm.email_address.value);
	vLocation     = trim(theForm.location.value);
	//place trimmed values back in the form
	theForm.username.value      = vUsername;
	theForm.userpass1.value     = vUserpass1;
	theForm.userpass2.value     = vUserpass2;
	theForm.email_address.value = vEmailAddress;
	theForm.location.value      = vLocation;
	
	if ( vUsername=="" ) {
		alert("Please enter your desired username before proceeding.");
		theForm.username.focus();
		return false;
	} else {
		//make sure it's 5 or more characters long
		if (vUsername.length<6) {
			alert("Your desired username must be at least 6 characters long");
			theForm.username.focus();
			return false;
		}
	}
	if ( vUserpass1=="" ) {
		alert("Please enter your desired password.");
		theForm.userpass1.focus();
		return false;
	} else {
		//make sure it's 5 or more characters long
		if (vUserpass1.length<5) {
			alert("Your password must be at least 5 characters long");
			theForm.userpass1.focus();
			return false;
		}
	}
	if ( vUserpass2=="" ) {
		alert("Please confirm your desired password.");
		theForm.userpass2.focus();
		return false;
	} else {
		//verify the passwords match
		if (vUserpass1!=vUserpass2) {
			alert("Your passwords do not match. Make sure to enter the same password in both fields before proceeding.");
			theForm.userpass1.value="";
			theForm.userpass2.value="";
			theForm.userpass1.focus();
			return false;
		}
	}
	if (vEmailAddress=="") {
		alert("Please enter your email address before proceeding.");
		theForm.email_address.value = "";
		theForm.email_address.focus();
		return false;
	} else {
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(vEmailAddress))) {
			alert("Please enter a valid email address before proceeding, for example, john.smith@sherwin.com");
			theForm.email_address.select();
			return false;
		}
	}
	if ( vLocation=="" ) {
		alert("Please enter your location before proceeding, for example, Cleveland, OH USA.");
		theForm.location.focus();
		return false;
	}
	if (theForm.acceptAgreement.checked!=true) {
		alert("You must read and indicate that you agree with the terms of use by checking the box before proceeding.");
		theForm.acceptAgreement.focus();
		return false;
	}
}

function validate_sectionForm(theForm) {
	theForm.section_title.value = trim(theForm.section_title.value);
	theForm.section_desc.value = trim(theForm.section_desc.value);
	if (theForm.section_title.value=="") {
		alert("You must enter the title for this new section before proceeding.");
		theForm.section_title.focus();
		return false;
	}
	if (theForm.descRequired.value=="1") {
		//section desc is only required for top level sections
		if (theForm.section_desc.value=="") {
			alert("You must enter the description for this new section before proceeding.");
			theForm.section_desc.focus();
			return false;
		}
	}
}

function validate_documentUploadForm(theForm) {
	theForm.doc_title.value = trim(theForm.doc_title.value);
	theForm.doc_desc.value  = trim(theForm.doc_desc.value);
	if (theForm.doc_title.value=="") {
		alert("You must enter the title of this document before proceeding.");
		theForm.doc_title.focus();
		return false;
	}
	if (theForm.doc_file.value=="") {
		alert("You must select the file you are uploading before proceeding.");
		theForm.doc_file.focus();
		return false;
	}
}

function validate_documentEditForm(theForm) {
	theForm.doc_title.value = trim(theForm.doc_title.value);
	theForm.doc_desc.value  = trim(theForm.doc_desc.value);
	if (theForm.doc_title.value=="") {
		alert("You must enter the title of this document before proceeding.");
		theForm.doc_title.focus();
		return false;
	}
	/*** does not require a file to be uploaded ***/
}

function popUp_newsWindow(againInd) {
	urlData = "";
	if (againInd==1) {
		urlData = "?redisplay=1";
	}
  WinName  = "newsWin";
  width    = 640;
  height   = 450;
  posX     = (screen.availWidth/2)-(width/2);
  posY     = (screen.availHeight/2)-(height/2);
  NewWinId = null;
  NewWinId = window.open('displayNews.cfm'+urlData,WinName,'width='+width+',height='+height+',resizable=yes,scrollbars=yes,menubar=no,toolbar=no,location=no,directories=no,status=no,top='+posY+',left='+posX);
  NewWinId.focus();
}

function validate_newsForm(theForm) {	
	/***
	checkedAccessCount = 0;
	for (i=0; i<theForm.access_level.length; i++) {
		if (theForm.access_level[i].checked) {
			checkedAccessCount++;
		}
	}
	if ( !checkedAccessCount ) {
		alert("You must select which user types will see this news item before proceeding.");
		theForm.news_body.focus();
		return false;
	}
	***/
	theForm.news_title.value = trim(theForm.news_title.value);
	if (theForm.news_title.value=="") {
		alert("You must enter the title for this news item before proceeding.");
		theForm.news_title.focus();
		return false;
	}
	theForm.news_body.value = trim(theForm.news_body.value);	
	if (theForm.news_body.value=="") {
		alert("You must enter the body of this news item before proceeding.");
		theForm.news_body.focus();
		return false;
	}
}

function validate_linkForm(theForm) {
	theForm.link_title.value = trim(theForm.link_title.value);
	theForm.link_desc.value = trim(theForm.link_desc.value);
	if (theForm.link_title.value=="") {
		alert("You must enter the title of this link before proceeding.");
		theForm.link_title.focus();
		return false;
	}
	if (theForm.link_url.value=="") {
		alert("You must enter a URL before proceeding.");
		theForm.link_url.focus();
		return false;
	}
}

function validate_accessLevelScreen(theForm) {
	for (i=0; i<theForm.permission_name.length; i++) {
		//trim all fields
		theForm.permission_name[i].value = trim(theForm.permission_name[i].value);
		theForm.permission_desc[i].value = trim(theForm.permission_desc[i].value);
		//turn on checkboxes for those that are not null and are not "NOT USED".
		if ( theForm.permission_name[i].value!="" && theForm.permission_name[i].value!="NOT USED" ) {
			theForm.permission_id[i].checked = true;
		}
		if ( theForm.permission_name[i].value=="" ) {
			//replace empty ones with "NOT USED"
			theForm.permission_name[i].value = "NOT USED";
			theForm.permission_desc[i].value = "No description entered";
			//make sure checkbox is not on
			theForm.permission_id[i].checked = false;
		}
	}
}

/* called by sort functions */
function moveOption(direction,field) {
  myField = eval(field);
	mySelectedVal1 = myField.selectedIndex;
  if (mySelectedVal1>-1) {  
  	if(direction == 'UP' && myField.selectedIndex > 0) {
  		mySelectedVal2 = mySelectedVal1 - 1;
  	}
  	else if(direction == 'DOWN' && myField.selectedIndex < myField.options.length-1) {
  		mySelectedVal2 = mySelectedVal1 + 1;
  	}
  	else return;
  	myTextVal1 = myField[mySelectedVal1].text;
  	myOptionVal1 = myField.options[mySelectedVal1].value;
  	myField.options[mySelectedVal1].value = myField.options[mySelectedVal2].value
  	myField.options[mySelectedVal2].value = myOptionVal1;
  	myField[mySelectedVal1].text = myField[mySelectedVal2].text;
  	myField[mySelectedVal2].text = myTextVal1;
  	myField.selectedIndex = mySelectedVal2;
  } else {
    alert("Please select an item before clicking the up/down buttons.");
    return false;
  }
}
/* selects all items in a "select multiple" prior to submitting the form */
function selectSortedItems(theForm) {
	for(x=0; x<theForm.idField.options.length; x++) {
		theForm.idField.options[x].selected=true;
	}
}

function deleteDocument(docType,docId) {
	if ( confirm("Are you sure you want to delete this " + docType + "?\nClick \"OK\" to proceed, otherwise click \"Cancel\".") ) {
		top.location = "index.cfm?action=delete&object="+docType+"&id="+docId;
	}
}