/* utilities.js 09-10 */

// Pretty Photo lightbox functions.
$(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto();
});

// Put all events required from load here

window.onload=function() {
	if (document.getElementById('newsletter-strip')) {
		document.getElementById('newsletter-strip').onsubmit=function() { return chkNewsletter('email1'); };
		document.getElementById('email1').onfocus=function() { clearField(this,'NEWSLETTER SIGNUP'); };
		document.getElementById('email1').onblur=function() { refillField(this,'NEWSLETTER SIGNUP'); };
	}
	if (document.getElementById('client-nl') || document.getElementById('consult-nl')) {
		var obj = (document.getElementById('client-nl'))?document.getElementById('client-nl'):document.getElementById('consult-nl');
		obj.onsubmit=function() { return chkNewsletter('email2'); };
		document.getElementById('email2').onfocus=function() { clearField(this,'EMAIL ADDRESS'); };
		document.getElementById('email2').onblur=function() { refillField(this,'EMAIL ADDRESS'); };
	}
	if (document.getElementById('contact-form')) {
		document.getElementById('contact-form').onsubmit=function() { return chkContact(); };
	}
	if (document.getElementById('culture-div')) { timeout=null; head_animation(); }
};

// Exclusively for the new consultants entry page.

function head_animation () {
	if (timeout) { clearTimeout(timeout); }
	$("#culture-div h3").css({display:'block',opacity: 0.0,marginLeft:'-300px'});
	$("#culture-div h3").animate({
    opacity: 1,
    marginLeft:'30px'
  }, 3000, function () { timeout=setInterval( function() { fadeOut() }, 15000)});
}

function fadeOut() {
	$("#culture-div h3").animate({ opacity: 0}, 1500, function() { head_animation(); });
}


function chkNewsletter(emailfield) {
	var regex = /.*\@.*[.].*/;
	if (regex.test(document.getElementById(emailfield).value)) { return true; }
	else { alert('Please enter a valid email address.'); return false; }
}

function clearField(obj, def_value) {
	if (obj.value==def_value) { obj.value=''; }
}

function refillField(obj, def_value) {
	if (obj.value=='') { obj.value=def_value; }
}

function chkContact() {
	var msg = '';
	var regex = /.*\@.*[.].*/;
	if (! regex.test(document.getElementById('email').value)==true) {
		msg += 'Please enter a valid email address.\n'; 
	}
	if (document.getElementById('visitor-name').value == '') {
		msg += 'Please enter your name.\n';
	}
	if (msg != '') { alert(msg); return false; }
	else { return true; }
}
