function addComment(f) {
	if (!(f.name.getValue() && f.email.getValue() && f.comment.getValue())) {
		alert('Please complete the form before submitting, all fields are required.');
		return false;
	}
	/*if (f.address.getValue()){
	    return false;
	}*/
	if (f.email.value.search(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)==-1) {
		alert('Please enter a valid email address.');
		return false;
	}
	
	f.effect('opacity').start(0);
	var d = new Element('div');
	d.id = 'addcommentmsg';
	d.setHTML('Please wait, saving your comment...');
	d.setStyle('position', 'absolute');
	d.setOpacity(0);
	d.injectTop('addcommentwrap');
	d.effect('opacity').start(1);
	new Ajax('project.php', {
		method: 'post',
		postBody: { 'name': f.name.getValue(), 'address': f.address.getValue(), 'email': f.email.getValue(), 'comment': f.comment.getValue(), 'memory': f.memory.getValue(), 'ajax': 1 },
		onComplete: function(r) {
			r = r.split("\t");
			if (r[0]=='OK') {
				// add new comment to top of comment list
				var d = new Element('div');
				d.addClass('comment');
				d.setHTML('<p>' + r[1] + '</p><small>' + r[2] + ' &bull; ' + r[3] + '</small>');
				d.injectTop('comments');
				// clear old field input
				$('addcommentbox').value = '';
				$('addcommentbox').setHTML('');
				// change message
				$('addcommentmsg').setHTML('<h3>Thank You</h3><p>Thank you for taking the time to comment on this project.</p>');
			} else if (r[0]=='FILTERED') {
				alert('Sorry, your comment has triggered our automated filter, please check for inappropriate language and try again.');
				$('addcommentmsg').effect('opacity').start(0);
				f.effect('opacity').start(1);
			} else {
				alert('Sorry, an error occurred while saving your comment, please try again.');
			}
		}
	}).request();
	return false;
}

function expandingLink() {
	$$('.expandlink').each(function(lnk) {
		lnk.getParent().getParent().addClass('closed');
		lnk.addEvent('click', function(e) {
			e.stop();
			var p = lnk.getParent().getParent();
			p.toggleClass('closed');
			return false;
		}.bindWithEvent());
		lnk.getParent().addEvent('click', function(e) {
			e.stop();
			var p = lnk.getParent().getParent();
			p.toggleClass('closed');
			return false;
		}.bindWithEvent());
	});
}
window.addEvent('domready', expandingLink);

var ratings = {
	init: function(d) {
		this.rater = d;
		this.stars = $ES('.star', d);
		this.stars.each(function(star, i) {
			star.addEvent('click', function(e) {
				e.stop();
				this.rate(i+1);
			}.bindWithEvent(this));
			star.addEvent('mouseover', function(e) {
				this.stars.each(function(hovstar, hi) {
					if (hi<=i) {
						if (hovstar.hasClass('staron')) {
							hovstar.addClass('staronhov');
						} else {
							hovstar.addClass('starhov');
						}
					}
				});
			}.bindWithEvent(this));
			star.addEvent('mouseout', function(e) {
				this.stars.each(function(hovstar, hi) {
					hovstar.removeClass('starhov');
					hovstar.removeClass('staronhov');
				});
			}.bindWithEvent(this));
		}, this);
	},
	rate: function(v) {
		this.stars.each(function(ratestar, i) {
			if (i<v) {
				ratestar.addClass('staron');
			} else {
				ratestar.removeClass('staron');
			}
		}, this);
		new Ajax('project.php', {
			method: 'post',
			postBody: { 'project': projid, 'rating': v, 'ajax': 1 },
			onComplete: function(r) {
				r = r.split("\t");
				if (r[0]=='OK') {
					$('numratings').setHTML(r[1]);
					// update average rating
					var s='';
					for (i=1; i<=5; i++) {
						s += (i<=r[2]) ? '<div class="star staron"><span>*</span></div>' : '<div class="star"><span>o</span></div>';
					}
					$$('#avgrate .starwrap')[0].setHTML(s);
				}
			}
		}).request();
	}
};
function setupRatings() {
	ratings.init($('yourate'));
}
window.addEvent('domready', setupRatings);

function charlimit(e,fld,max) {
	var e = new Event(e);
	var at=$(fld).getValue().length;
	if (at>max) $(fld).value = $(fld).getValue().substring(0, 255);
	return ((at>=max) && (![0,8,9,27,37,38,39,40,46].test(e.code)) && (e.key!=null)) ? false : true;
}
function setupForm() {
	$('addcommentbox').onkeypress = function(e) {
		return charlimit(e, this, 255);
	}
}
window.addEvent('domready', setupForm);
function loader() {
    $('igsearch').addEvent('click', function() {
        if ($(this).getValue()=='Enter Keyword') $(this).value = '';
        else $(this).select();
    });
    $('igsearch').addEvent('blur', function() {
        if ($(this).getValue()=='') $(this).value = 'Enter Keyword';
    });
}
window.addEvent('domready', loader);
