﻿// validation
function OpenCommentForm(controlid) {
	$(".spcomments").hide();
	
	if ($("#" + controlid).is(":hidden")) {
		$("#" + controlid).show();
		$("#" + controlid.replace("commentform", "TextBoxComment")).focus();
	}
	else {
		$("#" + controlid).hide();
	}
}

function CheckField(textbox) {
	var result = true;
	if (textbox.value.length <= 0) {
		result = false;
	}
	return result;
}

function CheckEmail(field) {
	var str = field.value; // email string
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (reg2.test(str)) { // if syntax is valid
		return true;
	}
	field.focus();
	field.select();
	return false;
}

function ValidateComment(id, type) {
	var comment = $("#valcomment" + id)[0];
	var username = $("#valusername" + id)[0];
	var email = $("#valemail" + id)[0];
	var website = $("#valwebsite" + id)[0];

	var check = true;
	if (CheckField(comment) == false) { check = false; }
	if (CheckField(username) == false) { check = false; }
	if (CheckField(email) == false) { check = false; }

	if (check) {
		if (CheckEmail(email) == false) {
			check = false;
			alert(email.value + " is an invalid email address.");
		}
		else {
			if (type == "photo")
				AddPhotoComment(id, type, comment.value, username.value, email.value, website.value);
		}
	}
	else
		alert("Please fill in a comment, your name and your (valid) email address.");

	return check;
}

function InitLightbox() {
	// init lightbox
	$('.lightbox').lightbox({ fitToScreen: true, imageClickClose: true });
}