/*global $, $F, Ajax */
function initListing(bodyID)  {
	var baseWidth = 755;
	var itemWidth = 357;

	var bodyWidth = baseWidth;

	var list = $('content').getElementsByClassName('portfolioItem');
	if (list) {
		var count = list.length;
		if (count > 2) {
			var diff = count-2;
			bodyWidth = baseWidth + (diff*itemWidth);
		}
	}
	$(bodyID).setStyle( {width: bodyWidth+'px'} );
}
function initListingDetail(bodyID)  {
	var baseWidth = 716;
	var itemWidth = 304;

	var bodyWidth = baseWidth;

	var list = $('content').getElementsByClassName('portfolioImg');
	if (list) {
		var count = list.length;
		if (count > 1) {
			var diff = count-1;
			bodyWidth = baseWidth + (diff*itemWidth);
		}
		if (count < 3) {
			$('lnkFirst').hide();
		}
	}
	$(bodyID).setStyle( {width: bodyWidth+'px'} );
}
function isEmailValid(email) {
	return /^[\w\-\.]+@([\w\-]+\.)+[\w\-]{2,4}$/i.test(email);
}
function trim(strText) {
	while (strText.substring(0,1) === ' ') {
		strText = strText.substring(1, strText.length);
	}
    while (strText.substring(strText.length-1,strText.length) === ' ') {
    	strText = strText.substring(0, strText.length-1);
    }
    return strText;
}
function isEmpty(strValue) {
	if (strValue === null) {
		return true;
	}
    if (strValue.length === undefined) {
    	return true;
    }
    strValue = trim(strValue);
    if (strValue.length === 0) {
    	return true;
    }
    return false;
}

function contactUs(contextPath) {
	var name  = $F('name');
	var email = $F('email');
	var subj  = $F('subject');
	var mesg  = $F('message');

	if (isEmpty(name)) {
		alert('Please enter your name.');
		return false;
	}
	if (isEmpty(email)) {
		alert('Please enter your email address.');
		return false;
	}
	if (!isEmailValid(email)) {
		alert('Please enter a valid email address.');
		return false;
	}
	if (isEmpty(mesg)) {
		alert('Please enter your message.');
		return false;
	}
	var params = {
		'name': name,
		'email': email,
		'subject': subj,
		'message': mesg,
		'submit': 'on'
	};
	var x = new Ajax.Request(contextPath + 'scripts/form.php', {method:'post',parameters: params, onSuccess:
			function(transport) {
				var text = transport.responseText;
				$('contactEntry').hide();
				var panel = $('contactPanel');
				panel.update(text);
				panel.show();
			}
		});
}