//General function for validating email address
function is_valid_email (email) {
	return /^([a-zA-Z0-9_!#$%&'*+\-\/=?\^`.{|}~\.])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

//General error checking for a field

function validate_field ($fieldID, $message, $destinationID) {
 	$destinationID = $destinationID || 'feedback'; 
 	$destination = document.getElementById($destinationID);
 	$fieldValue = document.getElementById($fieldID).value;

 	if ( $fieldValue == "" ) {
 	    if ( $destination == null ) {
 	    alert($message);
 	    return false;
 	    }
 	    else { 
 	    $destination.innerHTML = $message;
 	    $destination.style.display = 'block';
 	    return false;
 	    }
 	}

 	return true;
}

function validate_checkBox ($checkBoxID, $message, $destinationID) {
	$destinationID = $destinationID || 'feedback';
	$destination = document.getElementById($destinationID);
	$checkBoxValue = document.getElementById($checkBoxID).checked;
	
	if ($checkBoxValue == false) {
		if ( $destination == null ) {
 		alert($message);
 		return false;
 		}
 		else {
 		$destination.innerHTML = $message;
 		$destination.style.display = 'block';
 		return false;
 		}
	}
	
	return true;
} 

// Error check when submitting post to forum.

function validate_form(formID, action) {
var postTitle = document.getElementById("post_title");
var text = document.getElementById(formID).value;

if (postTitle != null)
{
if (postTitle.value=="") {
alert('Please enter a topic title');
}
else {
if (text=="" || text==" ") {
alert('Please enter some text!')
}
else {
return action.submit();
}
}
}
else {

if (text=="" || text==" ") {
alert('Please enter some text!')
}
else {
return action.submit();
}
}

}

function validate_change_password() {
	$feedback = document.getElementById('validation-feedback');
	$currentPass = document.getElementById('current_password').value;
	$newPass1 = document.getElementById('new_password').value;
	$newPass2 = document.getElementById('confirm_password').value;
	
	if ($newPass1 != $newPass2) {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'New Password and Confirm Password must be the same';	
		return false;
	}
	
	else if ($currentPass == '') {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'Please enter your current password';
		return false;
	}
	
	else if ($newPass1 == '') {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'Please enter a new password';
		return false;
	}
	
	else if ($newPass1 == $currentPass) {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'New Password must be different from Current Password';
		return false;
	}
	
	else return true;
}

function validate_send_message() {
	$feedback = document.getElementById('validation-feedback');
	$to = document.getElementById('to').value;
	$subject = document.getElementById('subject').value;
	$body = document.getElementById('message').value;
	
	if($to == '') {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'Please enter a recipient';
		return false;
	}
	
	else if($subject == '') {
		$feedback.style.display = "block";
		$feedback.innerHTML = 'Please enter a subject';
		return false;
	}
	
	else if($body == ''){
		$feedback.style.display = "block";
		$feedback.innerHTML = 'Please enter some message text';
		return false;
	}
	
	return true;
}

//Validate login
function validate_login() {
	if ( validate_field('user_username', 'Please enter a Username') && validate_field('user_password', 'Please enter a password') ) return true;
	else return false;
}

//Validate Recover password
function validate_recover_pass() {
	if (validate_field('user_username_recover', 'Please enter a Username', 'error_recover_pass')) return true;
	else return false;
}

//Validation for registration
function validate_registration() {
	vfeedback = document.getElementById('validation-feedback');
	vusername = document.getElementById('new_user_login');
	vpassword1 = document.getElementById('password1');
	vpassword2 = document.getElementById('confirm2');
	vemail = document.getElementById('user_email');
	
	if(vusername.value == '') {
		vfeedback.innerHTML = 'Please enter a username';
		vfeedback.style.display = 'block';
		return false;
	}
	
	if(vpassword1.value == '') {
		vfeedback.innerHTML = 'Please enter a password';
		vfeedback.style.display = 'block';
		return false;
	}
	
	
	if(vpassword1.value != vpassword2.value) {
		vfeedback.innerHTML = 'The passwords do not match';
		vfeedback.style.display = 'block';
		return false;
	}
	
	if(vemail.value == '' || is_valid_email(vemail.value) == false) {
		vfeedback.innerHTML = 'Please enter a valid email address';
		vfeedback.style.display = 'block';
		return false;
	}
	
	return true;
	
}

//Validation for forum
function validate_forum() {
	tinyMCE.triggerSave();

	topicTitle = document.getElementById('topic');
	topicContent = document.getElementById('post_content');
	feedback1 = document.getElementById('feedback');
	attachment1 = document.getElementById('attachment-1');
	attachment2 = document.getElementById('attachment-2');
	attachment3 = document.getElementById('attachment-3');
	attachment4 = document.getElementById('attachment-4');
		
	if(topicTitle != null && topicTitle.value == '') {
		feedback1.style.display = 'block';
		feedback1.innerHTML = 'Please enter a Topic Title';
		return false;
	}
	
	else if(topicContent != null && topicContent.value == '') {
		feedback1.style.display = 'block';
		feedback1.innerHTML = 'Please enter some content';
		return false;
	}
	
	if(attachment1 != null && attachment1.value > '') {
		parts = attachment1.value.split('.');
		ext = parts[parts.length - 1];		
		if(ext != 'jpg' && ext != 'gif' && ext != 'png' && ext != 'bmp' && ext != 'doc' && ext != 'txt' && ext != 'pdf') {
			attachment1.value = '';
			$("#feedback").html("One of the attachments you have selected is not an allowed filetype!").show("normal") ;
			return false;
		}
	}
	if(attachment2 != null && attachment2.value > '') {
		parts = attachment2.value.split('.');
		ext = parts[parts.length - 1];		
		if(ext != 'jpg' && ext != 'gif' && ext != 'png' && ext != 'bmp' && ext != 'doc' && ext != 'txt' && ext != 'pdf') {
			attachment2.value = '';
			$("#feedback").html("One of the attachments you have selected is not an allowed filetype!").show("normal") ;
			return false;
		}
	}
	if(attachment3 != null && attachment3.value > '') {
		parts = attachment3.value.split('.');
		ext = parts[parts.length - 1];		
		if(ext != 'jpg' && ext != 'gif' && ext != 'png' && ext != 'bmp' && ext != 'doc' && ext != 'txt' && ext != 'pdf') {
			attachment3.value = '';
			$("#feedback").html("One of the attachments you have selected is not an allowed filetype!").show("normal") ;
			return false;
		}
	}
	if(attachment4 != null && attachment4.value > '') {
		parts = attachment4.value.split('.');
		ext = parts[parts.length - 1];		
		if(ext != 'jpg' && ext != 'gif' && ext != 'png' && ext != 'bmp' && ext != 'doc' && ext != 'txt' && ext != 'pdf') {
			attachment4.value = '';
			$("#feedback").html("One of the attachments you have selected is not an allowed filetype!").show("normal") ;
			return false;
		}
	}
	return true;

}

//Validate write wall post
function validate_wall_post() {
	tinyMCE.triggerSave();
	if(document.getElementById('wall_content').value.length > 500) {
		feedback1 = document.getElementById('validation-feedback');
		feedback1.style.display = 'block';
		feedback1.innerHTML = 'Wall post too large, no more than 500 characters allowed!'
		return false;
	}
	else if(!validate_field('wall_content', 'Please enter some text', 'validation-feedback')) {
		validate_field('wall_content', 'Please enter some text', 'validation-feedback');
		return false;
	}
}

//Validation for Submit News form
function validate_write_blog_post() {
	tinyMCE.triggerSave();
	vfeedback = document.getElementById('validation-feedback');
	vtitle = document.getElementById('title');
	vbody = document.getElementById('body');
	
	if(vtitle.value == '') {
		vfeedback.innerHTML = 'Please enter a title';
		vfeedback.style.display = 'block';
		return false;
	}
	if(vbody.value == '') {
		vfeedback.innerHTML = 'Please enter some content';
		vfeedback.style.display = 'block';
		return false;
	}
	return true
}

//Validation for Add Glossary Term
function validate_add_glossary_term() {
	tinyMCE.triggerSave();
	vfeedback = document.getElementById('validation-feedback');
	vtitle = document.getElementById('title');
	vcontent = document.getElementById('content');
	vname = document.getElementById('name');
	vemail = document.getElementById('email');
	
	if(vtitle.value == '') {
		vfeedback.innerHTML = 'Please enter a Term';
		vfeedback.style.display = 'block';
		return false;
	}
	if(vcontent.value == '') {
		vfeedback.innerHTML = 'Please enter a Sentence or URL';
		vfeedback.style.display = 'block';
		return false;
	}
	
	if(vname != null) {
		if(vname.value == '') {
			vfeedback.innerHTML = 'Please enter your Name';
			vfeedback.style.display = 'block';
			return false;
		}
		if(vemail.value == '') {
			vfeedback.innerHTML = 'Please enter your Email Address';
			vfeedback.style.display = 'block';
			return false;
		}
	}
	
	return true
}

//Validation for Edit Profile
function validate_edit_profile() {
	if($("#bio").val().length > 5000) {
		$("#validation-feedback").html("Your Bio contains more than 5000 characters, please correct this").show("fast");
		return false;
	}
	return true;
}

//Validation for Forum Settings
function validate_forum_settings() {
	if($("#forum_signature").val().length > 500) {
		$("#validation-feedback").html("Your Forum Signature contains more than 500 characters, please correct this").show("fast");
		return false;
	}
	return true;
}

function validate_comment() {
	//alert("HELLO");
	//tinyMCE.triggerSave();
	
	if($("#email").length != 0) {
		if($("#author").val() == "" ) {
			$("#validation-feedback").html("Please enter your name").show("fast");
			return false;
		}
		if($("#email").val() == '' || is_valid_email($("#email").val()) == false) {
			$("#validation-feedback").html("Please enter a valid email address").show("fast");
			return false;
		}
	}
	if($("#comment").val() == "" ) {
	    $("#validation-feedback").html("Please enter a comment").show("fast");
	    return false;
	}
	if($("#loggedin").val() != "1") {
		if($("#code").val() == "" || $("#code").val() != getCookie("THECODE")) {
		    $("#validation-feedback").html("Security code does not match").show("fast");
		    return false;
		}
	}
	
	return true;
}

//Validation for Sent To A Friend widget. NOTE: The form is subitted through ajax via this function
function validate_send_to_a_friend(form_id) {
	
	if($("#stafToMail").val() == '' || is_valid_email($("#stafToMail").val()) == false) {
		$("#staf-feedback").html("Please enter your friend's valid email address").show("fast");
		return false;
	}
	
	if($("#stafToName").val() == '') {
		$("#staf-feedback").html("Please enter your friend's name").show("fast");
		return false;
	}
	
	if($("#stafFromName").val() == '') {
		$("#staf-feedback").html("Please enter your name").show("fast");
		return false;
	}
	
	if($("#stafFromMail").val() == '' || is_valid_email($("#stafFromMail").val()) == false) {
		$("#staf-feedback").html("Please enter your valid email address").show("fast");
		return false;
	}
	
	return send_to_a_friend($("#stafToMail").val(), $("#stafToName").val(), $("#stafFromName").val(), $("#stafFromMail").val(), $("#stafUrl").val());

}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}