$(document).ready(function(){
	getContentSize();
	$('img, object, embed').load(getContentSize);
	
	//Emulate the html5 property of placeholder
	$('input:text, textarea').each(setDefault);
	$('input:text, textarea').blur(setDefault);
	$('input:text, textarea').focus(clearDefault);
	
	$('form').submit(function(){
		$(this).find(':text, textarea').each(clearDefault);
	});
	$('#tba-process-form').submit(validateProcessForm);
	$('form#HelpForm').submit(validateHelpForm);
	$('form.SubmitOfferCode').submit(submitOfferCode);
	
	/* Fancybox iframe */
	$('.iframe,.iframevid').fancybox({
		overlayColor: '#000000',
		autoScale: true,
		centerOnScrol: true
	});
	$('.tba-plan-box').fancybox({
		overlayColor: '#000000',
		type: 'iframe',
		autoScale: true,
		centerOnScroll: true,
		href: '/no-interest.html'
	}).css('cursor','pointer');
});


function submitOfferCode() {
	try { 
        urchinTracker('/offer/submit_btn'); 
	} 
	catch(err) {}
		
	// ajax post offer
	$.ajax({
		type: 'POST',
		url: '/practices/save-offer-code.html',
		data: $('form.SubmitOfferCode').serialize(),
		dataType: 'html',
		success: function(data) {
			if (data) {
				$('form.SubmitOfferCode').replaceWith(data);	
			}
		}
	});
	return false;
}

//makes left and right equal height, call this function anytime something changes the height of either
function getContentSize(){
	$('#tba-left-sidebar, #tba-content-section').css({height:'auto'});
	var height = $('#tba-main').outerHeight();
	
	$('#tba-left-sidebar, #tba-content-section').css({height:height+'px'});
}
//sets the default
function setDefault(){
	var title = $(this).attr('title');
	var val = $(this).val();
	if (val == title || $(this).val() == ''){
		$(this).val(title).addClass('tba-default');
	}
}
//clears the default
function clearDefault(){
	var title = $(this).attr('title');
	var val = $(this).val();
	if (val == title){
		$(this).val('').removeClass('tba-default');
	}
}
//validates the process form
function validateProcessForm(){
	var requireds = [
		{id:'#tba-dental-practice', message:'Please include your dental practice name.'},
		{id:'#tba-your-name', message:'Please include your name.'},
		{id:'#tba-doctor-name', message:'Please include your Doctor\'s name.'},
		{id:'#tba-office-phone', message:'Please include your office phone number.'},
		{id:'#tba-email', message:'Please include an email address.'},
		{id:'#tba-email', message:'Please provide a valid email.', type:'email'}
	];
	var message = validateInputs(requireds);
	if (message == ''){
		return true;
	}else{
		alert(message);
		return false;
	}
}


function validateHelpForm() {
	var requireds = [
		{id:'input#CorporationName', message:'Please include your professional corporation name.'},
		{id:'input#Phone', message:'Please include your office phone number.'},
		{id:'input#EmailAddress', message:'Please include an email address.'},
		{id:'input#EmailAddress', message:'Please provide a valid email.', type:'email'}
	];
	var message = validateInputs(requireds);
	if (message == ''){
		return true;
	}else{
		alert(message);
		return false;
	}
}

//generic validation function takes an array of objects
function validateInputs(inputs){
	//input object structure...
	//id: the id of the element
	//message: the validation message of the input
	//type: optional, if it's something else than checking if it's filled out
	var errors = '';
	$('.tba-error').removeClass('tba-error');
	var fail;
	$.each(inputs, function(n, input){		
		if (input.type == null){
			fail = $(input.id).val() == '' || $(input.id).val() == $(input.id).attr('title');
		}else if (input.type == 'email'){
			fail = !validateEmail($(input.id).val());
		}
		
		if (fail){
			errors += input.message+'\n';
			fail = true;		
			$(input.id).addClass('tba-error');
		}		
	});
	if (errors != '') {
		$(':text, textarea').each(setDefault);
	}
	return errors;
}
//validates an email using unicode characters
function validateEmail(email){
	var emailreg = /^((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/; 
	if (!emailreg.exec(email))
		return false;
	else
		return true;
}

function numberFormat(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1))
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
  return '$' + x1 + x2;
}
	
function stripNonNumeric(str){
  str += '';
  var rgx = /^\d|\.|-$/;
  var out = '';
  for( var i = 0; i < str.length; i++ )
  {
	if( rgx.test( str.charAt(i) ) )
	 {
	  if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) )
		{
		out += str.charAt(i);
	  }
	}
  }
  return out;
}

function roundAndFormatNumber(the_number){
	var the_result;
	
	the_result = stripNonNumeric(the_number);
	the_result = parseFloat(the_result);
	the_result = the_result.toFixed(2);
	the_result = numberFormat(the_result);
	
	return the_result;
}

function calculate2() {
	A = document.getElementById('tba-annual-production').value;
	A = stripNonNumeric(A)
	A = Number(A)
	C = Math.round(A * 0.253)
	D = (A + C)
	D = numberFormat(D)
	$('#tba-result-ammount').html(D);
	//document.getElementById('tba-result-ammount').value = D
}
