function validate_before_login(){
	if ( document.login.nn.value == "" ){
		alert("Please provide your member nickname to login.");
        document.login.nn.focus();
        return;
    }
    else if ( document.login.pwd.value == "" ){
		alert("Please provide your member password to login.");
        document.login.pwd.focus();
        return;
    }
    else{
		document.login.submit();
    }
}

function validate_before_search(){
	if ( document.search_form.search_text.value == "" ){
		alert("Please provide search text");
        document.search_form.search_text.focus();
        return;
    }
    else{
		document.search_form.submit();
    }
}

function validate_before_forgot(){
	if ( document.login.nn.value == "" ){
		alert("Please provide your member nickname before clicking the 'forgot password' link.");
        document.login.nn.focus();
        return;
    }
    else{
		document.login.action = "password_email.php";
        document.login.submit();
    }
}

function swap_image( img, divid ){
	if ( img.src == "images/expand.gif" ){
		img.src = "images/contract.gif";
    }
    else{
		img.src = "images/expand.gif";
    }
    
    display_div( divid );
}

function display_div( divid ){
	var div = document.getElementById( divid );
    if ( div.style.display == "none" ){
		div.style.display = '';
    }
    else{
		div.style.display = 'none';
    }
}
   
function validate_before_signup(){
	if ( document.add_profile_form.nickname.value == "" ){
		alert('You must enter a nickname to sign-up.');
		document.add_profile_form.nickname.focus();
		return;
	}

	if ( document.add_profile_form.nickname.value.indexOf(" ") != -1 ){
		alert('You cannot have any blanks in your nickname.');
		document.add_profile_form.nickname.focus();
		return;
	}

	var amp_index;
	var per_index;

	amp_index = document.add_profile_form.email.value.indexOf('@',1);

	if ( amp_index == -1 )
		per_index = document.add_profile_form.email.value.indexOf('.',1);
	else
		per_index = document.add_profile_form.email.value.indexOf('.', amp_index);

	if ( amp_index == -1 || per_index == -1 || per_index < amp_index ){
		alert('Please provide a proper email address.');
		document.add_profile_form.email.focus();
		return;
	}

	if ( document.add_profile_form.password.value == "" ){
		alert('You must provide a password to protect your account.');
		document.add_profile_form.password.focus();
		return;
	}

	if ( document.add_profile_form.cpassword.value == "" ){
		alert('You must provide a confirmation password ensure your password is correct.');
		document.add_profile_form.cpassword.focus();
		return;
	}

	if ( document.add_profile_form.password.value != document.add_profile_form.cpassword.value ){
		alert('Your confirmation password does not match the password you provided.');
		document.add_profile_form.password.focus();
		return;
	}

	if ( document.add_profile_form.sex.value == "NIL" ){
		alert('You must pick a gender to continue with the sign-up.');
		document.add_profile_form.sex.focus();
		return;
	}

	if ( document.add_profile_form.country.value == "NIL" ){
		alert('Country is a required field for registration.');
		document.add_profile_form.country.focus();
		return;
	}

	if ( document.add_profile_form.verify.value == "" ){
		alert('Please enter the CAPTCHA code at the end of the form to complete registration.');
		document.add_profile_form.verify.focus();
		return;
	}

	document.add_profile_form.submit();
}

function check_count( text_area_name, span_name, max_size ){
	var textarea = document.getElementById( text_area_name );
	var span = document.getElementById( span_name );
	var count = textarea.value.length;
   
	if ( (max_size - count) <= 0 ){
		span.innerText = 0;
		textarea.value = textarea.value.substring(0, max_size-1);
		alert('You are at or have exceeded the limit of this field.');
		textarea.focus();
	}	
	else{
		span.innerText = max_size-count;
	}

	return;
}
 
function check_before_submitbio(){
   if( document.bio_update_form.aboutme.value.length > 4096 ){
      alert("\'About me\' field cannot contain more than 4096 characters. Please correct and resubmit.");
      document.bio_update_form.aboutme.focus();
      return;
   }
 
   if( document.bio_update_form.achievement.value.length > 4096 ){
      alert("Achievements field cannot contain more than 4096 characters. Please correct and resubmit.");
      document.bio_update_form.achievement.focus();
      return;
   }
 
   if( document.bio_update_form.education.value.length > 512 ){
      alert("Education field cannot contain more than 512 characters. Please correct and resubmit.");
      document.bio_update_form.education.focus();
      return;
   }
 
   document.bio_update_form.submit();
}

function validate_before_comment(){
   if ( document.comment_form.comment.value.length > 0 && document.comment_form.comment.value.length <= 1024 && document.comment_form.subject.value.length > 0){
      document.comment_form.submit();
   }
   else{
      if ( document.comment_form.comment.value.length <= 1024 ){
         alert('The comment you are providing cannot exceed 1024 characters. Please correct and resubmit.');
         document.comment_form.comment.focus();
         return;
      }

      if ( document.comment_form.comment.value.length == 0 ){
         alert('Please provide a comment before submitting.');
         document.comment_form.comment.focus();
         return;
      }

      if ( document.comment_form.subject.value.length == 0 ){
         alert('Please provide a subject before submitting.');
         document.comment_form.subject.focus();
         return;
      }
   }
}

function fill_it_in(){
   if ( document.sponsor_form.categoryid.options[0].selected ){
      document.sponsor_form.categoryname.value = ""; 
      document.sponsor_form.description.value = ""; 
      document.sponsor_form.categoryname.focus();
   }
   else{
      document.sponsor_form.categoryname.value = "existing"; 
      document.sponsor_form.description.value = "existing"; 
      document.sponsor_form.company.focus();
   }
 
   return;
}

function validate_before_sponsor(){
	if ( document.sponsor_form.description.value.length > 1024 ){
		alert("The DESCRIPTION you are providing cannot exceed 1024 characters. Please correct and resubmit.");
        document.sponsor_form.description.focus();
        return;
    }

    if ( document.sponsor_form.categoryname.value.length == 0 ){
		alert("Please provide a CATEGORY NAME before submitting.");
        document.sponsor_form.categoryname.focus();
        return;
    }

    if ( document.sponsor_form.description.value.length == 0 ){
		alert("Please provide a DESCRIPTION of the category you wish to sponsor before submitting.");
        document.sponsor_form.description.focus();
        return;
    }
	
    if ( document.sponsor_form.company.value.length == 0 ){
		alert("Please provide a COMPANY name before submitting.");
        document.sponsor_form.company.focus();
        return;
    }
	
    if ( document.sponsor_form.name.value.length == 0 ){
		alert("Please provide a CONTACT NAME before submitting.");
        document.sponsor_form.name.focus();
        return;
    }
	
    if ( document.sponsor_form.phone.value.length == 0 && document.sponsor_form.email.value.length == 0 ){
		alert("Please provide a EMAIL or PHONE number before submitting.");
        if ( document.sponsor_form.email.value.length == 0 ){
			document.sponsor_form.email.focus();
		}
		else{
			document.sponsor_form.phone.focus();
		}
        return;
    }
	
    document.sponsor_form.submit();
}

function validate_before_suggest(){
	if ( document.suggestion_form.description.value.length > 0 && document.suggestion_form.description.value.length <= 1024 && document.suggestion_form.categoryname.value.length > 0){
		document.suggestion_form.submit();
	}
	else{
		if ( document.suggestion_form.description.value.length > 1024 ){
			alert("The description you are providing cannot exceed 1024 characters. Please correct and resubmit.");
			document.suggestion_form.description.focus();
			return;
		}

		if ( document.suggestion_form.categoryname.value.length == 0 ){
			alert("Please provide a category name before submitting.");
			document.suggestion_form.categoryname.focus();
			return;
		}

		if ( document.suggestion_form.description.value.length == 0 ){
			alert("Please provide a description of your category before submitting.");
			document.suggestion_form.description.focus();
			return;
		}
	}
}

function validate_before_message(){
	if ( document.message_form.message.value.length > 0 && document.message_form.message.value.length <= 1024 ){
		document.message_form.submit();
	}
	else{
		if ( document.message_form.message.value.length > 1024 ){
			alert('Message body cannot be greater than 1024 characters. Please correct and resubmit.');
			document.message_form.message.focus();
		}
		else{
			alert('Please provide your message text before submitting.');
			document.message_form.message.focus();
		}
	}
}

function validate_before_change_password(){
   if ( document.changeform.pwd1.value == '' ){
      alert('Please provide your new password.');
      document.changeform.pwd1.focus();
      return;
   }
   else if ( document.changeform.pwd2.value == '' ){
      alert('Please provide your confirm password.');
      document.changeform.pwd2.focus();
      return;
   }
   else if ( document.changeform.pwd1.value != document.changeform.pwd2.value ){
      alert('Your confirm password does not match your new password. Please correct and re-submit.');
      document.changeform.pwd1.focus();
      return;
   }
   else{
      document.changeform.submit();
   }
}

function validate_before_submission_email(){
   if ( document.message_form.from_name.value.length == 0 ){
      alert('Please provide your first name.');
      document.message_form.from_name.focus();
      return;
   }

   if ( document.message_form.to_name.value.length == 0 ){
      alert('Please provide your friend\'s first name.');
      document.message_form.to_name.focus();
      return;
   }

   if ( document.message_form.to_email.value.length == 0 ){
      alert('Please provide your friend\'s email address.');
      document.message_form.to_email.focus();
      return;
   }
 
   document.message_form.submit();
}

function validate_before_category_email(){
   if ( document.message_form.from_name.value.length == 0 ){
      alert('Please provide your first name.');
      document.message_form.from_name.focus();
      return;
   }

   if ( document.message_form.to_name.value.length == 0 ){
      alert('Please provide your friend\'s first name.');
      document.message_form.to_name.focus();
      return;
   }

   if ( document.message_form.to_email.value.length == 0 ){
      alert('Please provide your friend\'s email address.');
      document.message_form.to_email.focus();
      return;
   }

   document.message_form.submit();
}

