// Unit Interactive JS

var text = null, first = null;

$(document).ready(function(){
	
	initUnitJS();	

});

initUnitJS = function () {
	
	// chatty form
	$('#cForm input, #cForm textarea').bind('change', function () { yakky(this); });
	$('#cForm input.formSubmit').bind('click', formSubmit);
	
	$("a[class='modal']").colorbox();
	
}

yakky = function (inpt) {
	
	var name = ($('#name').val() != '') ? $('#name').val() : false,
		emailAdd = ($('#email').val() != '') ? $('#email').val() : false,
		num = ($('#phone').val() != '') ? $('#phone').val() : false,
		comment = ($('#comment').val() != '') ? $('#comment').val() : false,
		conv = $('#conversation');
	
	if ($(inpt).attr('id') == 'name') {
		name = name.replace(/^(\s*)/, '');
		nameCheck = name.split(' ');
		if (24 > nameCheck[0].length > 0) first = nameCheck[0];
		else if (nameCheck[0].length >= 24) first = 'Partner';
		else first = null;
		
		conv.css({ color: '#485E1C' });
		$(inpt).removeClass('errorFocus');
		
		if (first && !emailAdd) text = 'Thanks ' + first + '! Now enter the best email address where we can reach you.';
		else if (first && !comment) text = 'Thanks ' + first + '! So, what&rsquo;s on your mind?';
		else if (!first) {
			text = 'Not sure I&rsquo;ve heard such a name. How do you pronounce that?';
			conv.css({ color: '#ff0000' });
			$(inpt).addClass('errorFocus');
			name = null;
		}
		else text = 'Thanks, ' + first + '! Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'email') {		
		conv.css({ color: '#485E1C' });
		$(inpt).removeClass('errorFocus');
		
		var ereg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
		if (ereg.test(emailAdd) == false) {
			text = 'Not sure that&rsquo;s a valid email. We do need a valid email to get back to you.';
			conv.css({ color: '#ff0000' });
			$(inpt).addClass('errorFocus');
		}
		else if (first && !num) text = 'Uploading to spam server... Ha! Kidding. Got a phone number? (Not required.)';
		else if (first && !comment) text = 'Uploading to spam server... Ha! Kidding. What&rsquo;s on your mind?';
		else if (!first) text = 'Uploading to spam server... Ha! Kidding. Got a name, mystery person?';
		else text = 'Allright. Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'phone') {
		var numAdd = 0;
		
		conv.css({ color: '#485E1C' });
		
		for (i=0; i<num.length; i++) {
			var digit = parseInt(num.charAt(i));
			if (digit) numAdd += digit;
		}
		
		if (first && !emailAdd) text = 'Great! Now enter the best email address where we can reach you.';
		else if (first && !comment) text = 'Great! So, ' + first + ', what&rsquo;s on your mind?';
		else if (!first) text = 'Great! Here&rsquo;s another number: ' + numAdd + '. Its your lucky number. So, what should we call you?';
		else text = 'Great! Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}
	
	if ($(inpt).attr('id') == 'comment') {		
		conv.css({ color: '#485E1C' });
		$(inpt).removeClass('errorFocus');
		
		var linkreg = comment.match(/http:\/\//gi);
		linkreg = (linkreg) ? linkreg.length : 0;
		
		if (linkreg.length > 3) {
			text = 'Um&hellip; I think you&rsquo;ve got some spam here. We only accept 3 URLs or less. Please try again.';
			conv.css({ color: '#ff0000' });
			$(inpt).addClass('errorFocus');
		}
		else if (name && emailAdd && name.length < 24) text = 'Thanks, ' + name + '. Hit submit, and a Unitard will get back to you shortly.';
		else if (!name && !emailAdd) text = 'Hmmm... interesting. Still not sure I got your name or email.';
		else if (name && !emailAdd) text = 'Still not sure I got your email. We&rsquo;ll need it to get back to you.';
		else if (!name && emailAdd) text = 'Hmmm... interesting. Still not sure I got your name. We do require a name.';
		else text = 'Well, it&rsquo;s been fun. Hit submit, and a Unitard will get back to you shortly.';
		conv.html(text);
	}

}

formSubmit = function () {
	
	var name = ( $('#name').attr('class').match('errorFocus') ) ? false : $('#name').val(), 
	emailAdd = ( $('#email').attr('class').match('errorFocus') ) ? false : $('#email').val(), 
	num = $('#phone').val(),
	comment = ( $('#comment').attr('class').match('errorFocus') ) ? false : $('#comment').val();
	
	var linkreg = comment.match(/http:\/\//gi);
	linkreg = (linkreg) ? linkreg.length : 0;
	
	var conv = $('#conversation');
	
	if ( name && emailAdd != false && comment && linkreg < 4 ) {
		
		var iFrameID = 'formSubmitIframe';
		
		var iframe = document.createElement('iframe');
		$(iframe).attr({ id: iFrameID, name: iFrameID, action: "#", src: 'about:blank' });
		$('body').append(iframe);
		if (self.frames[iFrameID].name != iFrameID) { self.frames[iFrameID].name = iFrameID; }
		
		document.cForm.submit();
		
		$('#cForm input#name, #cForm input#email, #cForm input#phone, #cForm textarea#comment').val('');
		
		text = 'Your email was succesfully sent. We&rsquo;ll get back to you soon for a longer chat.';
		conv.html(text);

	}
	else {
		text = 'Whoops! Looks as though you&rsquo;re missing ';
		conv.css({ color: '#ff0000' });
		
		if (!name) {
			text += 'a name.';
			$('#name').addClass('errorFocus');
		}
		else if (!emailAdd) {
			text += 'a valid email address.';
			$('#email').addClass('errorFocus');
		}
		else if (!comment) {
			text += 'a comment.';
			$('#comment').addClass('errorFocus');
		}
		else if (linkreg > 3) {
			text = 'Um&hellip; still think you&rsquo;ve got some spam here. Remember: We only accept 3 URLs or less.';
			$('#comment').addClass('errorFocus');
		}
		
		conv.html(text);
	}
	
}
