$(document).ready(function() {
	var page = $("body").attr('id');
	if(page=='home'){	
		$("#site-images").cycle({
			fx: 'scrollLeft'
		});
	}
		
	$("#grid a").click(function(){
		var theAnchor = $(this).attr('href');
		$.scrollTo( theAnchor, 1100, {offset : {top: -110}} );	
	});

	$("a.top").click(function(){
		$.scrollTo( '#container', 1100 );	
	});
	
	// Open external links in new window
    /*$("a[rel='external']").click(function() { 
    	alert(this)
    	window.open($(this).href); 
    	return false; 
    });*/
});

function valid_email(email) {
	if(!(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/.test(email))){
		return false;
	}	
}
function check_form() { 
	var comm = "";
	var email = $.trim(document.getElementById('email').value);
	if (valid_email(email) == false) {
		comm += '<span class="error">Oops! That doesn\'t look like a real email address to me.</span>';
	}
	if (comm.length == 0) {
		$.post("newsletter-process.php", { email: email }, function(){
			
			$("#footer form").html("<p><strong>Thanks for joining our newsletter.</strong></p>");
		} );
	} 
	else {
		$("#footer form .error").remove()
		$("#footer form").append(comm);
		return false;
		
	}
	return false;
}

function check_contact() {
	var comm = "";
	var name = $.trim(document.getElementById('name').value);
	var email = $.trim(document.getElementById('cont_email').value);
	var phone = $.trim(document.getElementById('phone').value);
	var business = $.trim(document.getElementById('business').value);
	var type = $.trim(document.getElementById('type').value);
	var services = $.trim(document.getElementById('services').value);
	var other = $.trim(document.getElementById('other').value);

	if (name.length == 0) {
		comm = '<li>You need to enter your first name</li>';
	}
	if (email.length == 0) {
		comm += '<li>You need to enter an email address</li>';
	}	

	if (email.length != 0 && valid_email(email) == false) {
		comm += '<li>Please ensure you entered a valid email address</li>';
	}
	if (comm.length == 0) {
		$.post("form.php", { name: encodeURIComponent(name), email: encodeURIComponent(email), other: encodeURIComponent(other), phone: encodeURIComponent(phone), business: encodeURIComponent(business), type: encodeURIComponent(type), services: encodeURIComponent(services)  }, function(){
			$("#contact-us").html("<p><strong>Your form has been submitted, thank you.</strong></p>");
		} );
	} 
	else {
		$("#contact-us ul").remove();
		$("#contact-us").prepend('<ul>' + comm + '</ul>');
		return false;
		
	}
	return false;
}

// open external links in new window
function externalLinks() { 
	if (!document.getElementsByTagName) return;  
	var anchors = document.getElementsByTagName("a");  
	for (var i=0; i<anchors.length; i++) {  
		var anchor = anchors[i];  
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external"){ 
		 anchor.target = "_blank";
		}
	}  
}  
window.onload = externalLinks;
