// contact form interactive chat/assistance

var text = null, name = null, first = null, emailAdd = null, num = null, comment = null;

var classes = null, country = null, city = null, cHeight = new Array(), cWidth = new Array();

initchattyform = function ()
{
	// chatty form
	if ($('.cForm').length > 0)
	{
		$('.cForm *').bind('change', function () { blablabla(this); });
		$('.cForm input').bind('focus', function () { $(this).addClass('focus'); });
		$('.cForm textarea').bind('focus', function () { $(this).addClass('focus'); });
		$('.cForm input').bind('blur', function () { $(this).removeClass('focus'); });
		$('.cForm textarea').bind('blur', function () { $(this).removeClass('focus'); });
		//$('.cForm input.formSubmit').bind('click', formSubmit);
	}
}

blablabla = function (inpt) {
	
	var conv = $('#haveachat');
	var convinner = $('#haveachat p');
	
	if ($(inpt).attr('id') == 'name')
	{
		name = $(inpt).val();
		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.removeClass();
		$(inpt).removeClass('errorFocus');
		
		if (first && !emailAdd)
		{
			conv.addClass('convemailfocus');
			text = 'Thanks <strong>' + first + '</strong>, Now enter the best email address where we can reach you.';
		}
		else if (first && !comment)
		{
			conv.addClass('convcommentfocus');
			text = 'Thanks <strong>' + first + '</strong>. So, what can you tell us about your business or project?';
		}
		else if (!first)
		{
			text = 'Not sure I&rsquo;ve heard such a name. How do you pronounce that?';
			conv.addClass('convnamefocus');
			$(inpt).addClass('errorFocus');
			name = null;
		}
		else 
		{
			text = 'Thanks <strong>' + first + '</strong>, Hit "SEND", and someone will get back to you shortly.';
		}

		convinner.html(text);
	}
	
	if ($(inpt).attr('id') == 'email')
	{
		emailAdd = $(inpt).val();
		conv.removeClass();
		$(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.addClass('convemailfocus');
			$(inpt).addClass('errorFocus');
			emailAdd = null;
		}

		/* check fields */
		else if (first && !num) 
		{
			conv.addClass('convphonefocus');
			text = 'How about a phone number? (Its not required.)';
		}
		else if (first && !comment)
		{
			conv.addClass('convcommentfocus');
			text = 'So <strong>' + first + '</strong>, what can you tell us about your business or project?';
		}
		else if (!first) 
		{
			conv.addClass('convnamefocus');
			text = 'Thanks, but i dont think we managed to catch your name?';
		}
		else
		{
			text = 'Great, now hit "SEND", and someone will get back to you shortly.';
		}

		convinner.html(text);
	}
	
	if ($(inpt).attr('id') == 'phone')
	{
		num = $(inpt).val();
		var numAdd = 0;

		conv.removeClass();

		for (i=0; i<num.length; i++)
		{
			var digit = parseInt(num.charAt(i));
			if (digit) numAdd += digit;
		}
		
		/* check fields */
		if (first && !emailAdd) 
		{
			conv.addClass('convemailfocus');
			text = 'Great! Now enter the best email address where we can reach you.';
		}
		else if (first && !comment) 
		{
			conv.addClass('convcommentfocus');
			text = 'Great. So <strong>' + first + '</strong>, what can you tell us about your business or project?';
		}
		else if (!first)
		{
			conv.addClass('convnamefocus');
			text = 'Great! although i dont believe i caught your name?';
		}
		else text = 'Thanks, now just hit "SEND", and someone will get back to you shortly.';
		{
		}

		convinner.html(text);
	}
	
	if ($(inpt).attr('id') == 'comment')
	{		
		comment = $(inpt).val();
		conv.removeClass();
		$(inpt).removeClass('errorFocus');
		
		if (name && emailAdd && name.length < 24) 
		{
			text = 'Thanks for your info <strong>' + name + '</strong>. It looks like we&rsquo;ve got everyhing we need there, Hit "SEND" and we&rsquo;ll be back in touch with you shortly';
		}
		else if (name && emailAdd) 
		{
			text = 'All done. Hit "SEND" and we&rsquo;ll be back in touch with you shortly';
		}
		else if (name && !emailAdd) 
		{
			conv.addClass('convemailfocus');
			text = 'We&rsquo;re still not sure we got your email. We&rsquo;ll need it to get back to you.';
		}
		else if (!name && emailAdd) 
		{
			conv.addClass('convnamefocus');
			text = 'Thanks, but i dont think we managed to catch your name?';
		}
		else 
		{
			conv.addClass('convnamefocus');
			text = 'Hmmm... interesting. Still not sure I got your name or email.';
		}
		
		convinner.html(text);
	}

}

/*

formSubmit = function ()
{
	var conv = $('#conversation');
	
	if ( name && emailAdd && comment )
	{
		
		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.';

		convinner.html(text);

	}
	else
	{
		text = 'Whoops! Looks as though you&rsquo;re missing ';

		conv.addClass('convbad');

		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');
		}
		
		convinner.html(text);
	}
	
}

*/
