/*******************************************************************************/
/*                           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.options[cbo.selectedIndex].value!="")
		isGood=true;
		
	return isGood;
}

//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');
} 

// 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;		
}

/*******************************************************************************/
/*                             Validation Functions                            */
/*******************************************************************************/

// validate Newsletter submission
function validateNewsletter(thisForm){
	if(thisForm.nlName.value==""){
		alert("You must provide a name for the newsletter!");
		thisForm.nlName.focus();
		return false;
	}
	
	return true;
}

// This function is for validating Cabinet Category submissions
function validateCabCat(thisForm){
	if(thisForm.ccatName.value==""){
		alert("You must provide a name for the cabinet category!");
		thisForm.ccatName.focus();
		return false;
	}
	
	return true;
}


// This function is for validating Cabinet Category submissions
function validateCalCat(thisForm){
	if(thisForm.calcatName.value==""){
		alert("You must provide a name for the calendar category!");
		thisForm.calcatName.focus();
		return false;
	}
	
	return true;
}

function validateAddAdmin(thisForm){
	if(thisForm.username.value==""){
		alert("You must provide a username!");
		thisForm.username.focus();
		return false;
	}
	if(thisForm.userPassword.value==""){
		alert("You must provide a userPassword!");
		thisForm.userPassword.focus();
		return false;
	}
	if(thisForm.conPass.value != thisForm.userPassword.value){
		alert("You must the same password in both fields!");
		thisForm.userPassword.focus();
		return false;
	}
	
	return true;
}

// This function is for validating Link Category submissions
function validateLinkCat(thisForm){
	if(thisForm.linkName.value==""){
		alert("You must provide a name for the link category!");
		thisForm.linkName.focus();
		return false;
	}
	
	return true;
}  

// This function is for validating Link Category submissions
function validateLink(thisForm){
    if(thisForm.linkCat.value==""){
	    alert("You must provide a category for the link!");
	    thisForm.linkCat.focus();
	    return false;
    }
	if(thisForm.linkName.value==""){
		alert("You must provide a name for the link!");
		thisForm.linkName.focus();
		return false;
	}
	if(thisForm.linkURL.value==""){
		alert("You must provide a url for the link!");
		thisForm.linkURL.focus();
		return false;
	}
	
	return true;
}  
function validateQlink(thisForm){
    if(thisForm.linkType.value==""){
	    alert("You must provide a link type!");
	    thisForm.linkType.focus();
	    return false;
    }
	if(thisForm.linkName.value==""){
		alert("You must provide a name for the link!");
		thisForm.linkName.focus();
		return false;
	}
	if(thisForm.linkURL.value==""){
		alert("You must provide a url for the link!");
		thisForm.linkURL.focus();
		return false;
	}
	
	return true;
}  

// This function is for validating Cabinet Category submissions
function validateCarCat(thisForm){
	if(thisForm.carcatName.value==""){
		alert("You must provide a name for the career category!");
		thisForm.carcatName.focus();
		return false;
	}
	
	return true;
} 

// This function is for validating Menu submissions
function validateMenu(thisForm){
	if(thisForm.mnuName.value==""){
		alert("You must provide a name for the menu!");
		thisForm.mnuName.focus();
		return false;
	}
	if(!cbxSelectionMade(thisForm.mnuParent)){
		alert("You must choose a menu parent for this page!");
		return false;
	}
	
	return true;
}
// This function is for validating BlogTitle submissions
function validateBlogTitle(thisForm,mod){
	if(thisForm.bTitle.value==""){
		alert("You must provide a title for the blog!");
		thisForm.bTitle.focus()
		return false;
	}
	if(!cboSelectionMade(thisForm.bcatID)){
		alert("You must choose a category for the blog!");
		thisForm.bcatID.focus()
		return false;
	}
	if(mod==0)
	{
		if(thisForm.bText.value==''){
			alert("You must provide the first post for the blog!");
			return false;
		}
	}
	
	return true;
}

// This function is for validating Cabinet File submissions
function validateCabFile(thisForm,mode){
	if(!cbxSelectionMade(thisForm.ccatID)){
		alert("You must choose a cabinet category to upload this file to!");
		return false;
	}
	if(mode==0)
	{
		if(thisForm.cfileName.value==''){
			alert("You must choose a file to upload!");
			return false;
		}
	}
	if(thisForm.cfileTitle.value==""){
		alert("You must provide a friendly user name for the file!");
		thisForm.cfileTitle.focus()
		return false;
	}
	
	return true;
}


// This function is for validating BlogPost submissions
function validateBlogComment(thisForm){
	if(thisForm.cText.value==''){
		alert("You must provide text for this comment!");
		return false;
	}
	
	return true;
}

// This function is for validating blog category submissions
function validateBlogCat(thisForm){
	if(thisForm.bcatName.value==""){
		alert("You must provide a name for this category!");
		thisForm.bcatName.focus()
		return false;
	}
	
	return true;
}

// This function validates calendar entry submissions
function validateCalEntry(thisForm){
	if(!cbxSelectionMade(thisForm.calcatID)){
		alert("You must choose a calendar category for this entry!");
		return false;
	}
	if(thisForm.ceventHeadline.value==""){
		alert("You must provide a headline for this entry!");
		thisForm.ceventHeadline.focus()
		return false;
	}
	if(thisForm.ceventFrom.value==""||!isValidDate(thisForm.ceventFrom.value)){
		alert("You must provide a valid From date for the entry!");
		thisForm.ceventFrom.focus();
		return false;
	}
	if(thisForm.ceventDesc.value==""){
		alert("You must provide a description for this entry!");
		return false;
	}
	
	return true;
}

// This function validates calendar entry submissions
function validateCareer(thisForm){
	if(!cboSelectionMade(thisForm.carcatID)){
		alert("You must choose a career category for this career!");
		thisForm.carcatID.focus();
		return false;
	}
	if(thisForm.careerName.value==""){
		alert("You must provide a name for this career!");
		thisForm.careerName.focus();
		
		return false;
	}
	if(thisForm.careerDescription.value==""){
		alert("You must provide a career description for this career!");
		return false;
	}
	
	return true;
}

// This function pops the color picker window
function popColorPicker(){
	window.open('colorpic.html','colorpic','width=300,height=220');
}

// This function is for validating FAQ category submissions
function validateFAQcat(thisForm){
	if(thisForm.ncatName.value==''){
		alert("You must provide a category name!");
		thisForm.ncatName.focus()
		return false
	}
	
	return true;
}

// This function is for validating FAQ topic submissions
function validateFAQtopic(thisForm){
	/*if(!cboSelectionMade(thisForm.ncatID)){
		alert("You must choose a category to group the topic under!");
		thisForm.topicName.focus()
		return false
	}*/
	if(thisForm.topicName.value==''){
		alert("You must provide a topic name!");
		thisForm.topicName.focus()
		return false
	}
	
	return true;
}

// This function is for validating FAQ submissions
function validateFAQ(thisForm){
	if(thisForm.needQuestion.value==''){
		alert("You must provide a question for the FAQ!");
		return false
	}
	if(thisForm.needAnswer.value==''){
		alert("You must provide a answer for the FAQ!");
		return false
	}
	if(!cboSelectionMade(thisForm.ntopID)){
		alert("You must choose a topic grouping for the FAQ!");
		return false
	}
	
	return true;
}

// This function is for validating
function validateBlogImg(thisForm,mode){
	if(!cboSelectionMade(thisForm.blogID)){
		alert("You must choose a cabinet category to upload this file to!");
		thisForm.blogID.focus();
		return false;
	}
	if(mode==0)
	{
		if(thisForm.bimgFile.value==''){
			alert("You must choose an image file to upload!");
			thisForm.bimgFile.focus()
			return false;
		}
	}
	var fname=thisForm.bimgFile.value;
	if(fname!=""){
		if(fname.substring(fname.length-4).toLowerCase()!=".jpg"&&fname.substring(fname.length-5).toLowerCase()!=".jpeg"){
			alert("You can only upload images in the .jpg or .jpeg format!");
			thisForm.bimgFile.focus();
			return false;
		}
	}
	
	return true;
}

// This function is for validating What's new submissions
function validateWhatsNew(thisForm){
	if(thisForm.newsTitle.value==""){
		alert("You must provide a title for the article!");
		return false;
	}
	if(thisForm.newsText.value==""){
		alert("You must provide some content for the article!");
		return false;
	}
	
	return true;
}
function validateAdmin(thisForm){
	if(!cbxSelectionMade(thisForm.modAccess)){
			alert("You must choose module access levels for this admin!");
			return false;
		}
	if(!isValidEmail(thisForm.email.value))
	{
		alert("You must provide a valid email!");
		thisForm.email.focus();
		return false;
	}
	if(thisForm.username.value==""){
		alert("You must provide a username!");
		thisForm.username.focus();
		
		return false;
	}	
		if(thisForm.password.value==""){
		alert("You must provide a password!");
		thisForm.password.focus();
		
		return false;
	}	
		if(thisForm.password2.value==""){
		alert("You must confirm your password!");
		thisForm.password2.focus();
		
		return false;
	}	
		if(thisForm.password.value!=thisForm.password2.value){
		alert("You must confirm your password");
		thisForm.password.focus();
		
		return false;
	}	
	return true;
}
function validateEditAdmin(thisForm){
	if(!cbxSelectionMade(thisForm.modAccess)){
			alert("You must choose access levels for this admin!");
			return false;
		}
	if(!isValidEmail(thisForm.email.value))
	{
		alert("You must provide a valid email!");
		thisForm.email.focus();
		return false;
	}
	if(thisForm.username.value==""){
		alert("You must provide a username!");
		thisForm.username.focus();
		
		return false;
	}	
		if(thisForm.password.value!=thisForm.password2.value){
		alert("You must confirm your password");
		thisForm.password.focus();
		
		return false;
	}	
	return true;
}

function validateFeedbackEmails(thisForm){
	if(thisForm.email1.value==""){
		alert("You must provide an email in the email one field!");
		thisForm.email1.focus();
		return false;
	}	
	if(!isValidEmail(thisForm.email1.value))
	{
		alert("You must provide a valid email!");
		thisForm.email1.focus();
		return false;
	}
	
	return true;
}

function validateAddSurvey(thisForm){
	if(thisForm.surveyTitle.value==""){
		alert("You must provide an survey title!");
		thisForm.surveyTitle.focus();
		return false;
	}	
	if(thisForm.surveyDesc.value==""){
		alert("You must provide a description for this survey!");
		thisForm.surveyDesc.focus();
		return false;
	}
	if(thisForm.surveyShow.value==""){
		alert("You must provide a show date for this survey!");
		thisForm.surveyShow.focus();
		return false;
	}	
	if(thisForm.surveHide.value==""){
		alert("You must provide a hide date for this survey!");
		thisForm.surveHide.focus();
		return false;
	}	
	
	
	return true;
}
function validateIGroup(thisForm){
	if(thisForm.groupName.value==""){
			alert("You must provide an group name!");
			thisForm.groupName.focus();
			return false;
		}
	return true;
}
function validateUser(thisForm){

	if(thisForm.userUsername.value==""){
			alert("You must provide a username!");
			thisForm.userUsername.focus();
			return false;
		}	
		if(thisForm.userPassword.value==""){
			alert("You must provide a password!");
			thisForm.userPassword.focus();
			return false;
		}	
		if(thisForm.confirmPassword.value==""){
			alert("You must provide a confirm password!");
			thisForm.confirmPassword.focus();
			return false;
		}	
		if(!cbxSelectionMade(thisForm.groupids)){
			alert("You must choose a group for this user!");
			return false;
		}
		
	return true;
}

function validateAddForm(thisForm){
	if(thisForm.formTitle.value==""){
		alert("You must provide an form title!");
		thisForm.formTitle.focus();
		return false;
	}	
	if(thisForm.formDesc.value==""){
		alert("You must provide a description for this form!");
		thisForm.formDesc.focus();
		return false;
	}
		if(thisForm.formEmail.value==""){
		alert("You must provide an email for this form!");
		thisForm.formEmail.focus();
		return false;
	}	
	
	
	return true;
}
function validatemyAccount(thisForm){
	
	if(!isValidEmail(thisForm.email.value))
	{
		alert("You must provide a valid email!");
		thisForm.email.focus();
		return false;
	}
		if(thisForm.password.value!=thisForm.password2.value){
		alert("You must confirm your password");
		thisForm.password.focus();
		
		return false;
	}	
	return true;
}
function validateConEntry(thisForm){
	if(!cboSelectionMade(thisForm.concatID)){
		alert("You must choose a category");
		thisForm.concatID.focus();
		
		return false;
	}	
	if(thisForm.fullName.value==""){
		alert("You must enter the contact's name");
		thisForm.fullName.focus();
		
		return false;
	}	
	if(thisForm.position.value==""){
		alert("You must enter the contact's position");
		thisForm.position.focus();
		
		return false;
	}	
	
	if(!isValidEmail(thisForm.conEmail.value)||thisForm.conEmail.value=="")
	{
		alert("You must provide a valid email!");
		thisForm.conEmail.focus();
		return false;
	}
	return true;
}
function validateConCat(thisForm){
	if(thisForm.concatName.value==""){
		alert("You must enter the category's name");
		thisForm.concatName.focus();
		
		return false;
	}	
	return true;
}
