$(document).ready(function() {
	placeholderFix();
	radioButtonsFunctionality();
	tabsFunctionality();
	selectHowToSubtab();
	howToSubtabFunctionality();
	$("a[rel^='lightbox']").prettyPhoto({
		opacity: 0.90
	});
	
});

function placeholderFix(){
	var input = document.createElement('input');
	if(!('placeholder' in input)){
		$('input, textarea').each(function(){
			if($(this).val()=='' && $(this).attr('placeholder')!=''){
				$(this).val($(this).attr('placeholder'));
				$(this).focus(function(){
					if($(this).val()==$(this).attr('placeholder')) $(this).val('');
				});
				$(this).blur(function(){
					if($(this).val()=='') $(this).val($(this).attr('placeholder'));
				});
			}
		});
	}
}

function radioButtonsFunctionality(){
	$("#formBox input[type='checkbox']").bind("change", function() {
		changeText();
	});
	
	$("#checkbox1label").bind("click", function(){
		$("#checkbox1").attr("checked","checked");
		changeText();
	});
	$("#form_comments").bind("focus", function(){
		$("#checkbox1").attr("checked","checked");
		changeText();
	});
	$("#checkbox2label").bind("click", function(){
		$("#checkbox2").attr('checked', !$("#checkbox2").attr('checked'));
		changeText();
	});
	$("#checkbox3label").bind("click", function(){
		$("#checkbox3").attr('checked', !$("#checkbox3").attr('checked'));
		changeText();
	});
}
function changeText(){
	if($("#checkbox1").is(':checked')){
		$("#formBox .title").html("Need More Information?");
		$("#formBox #form_comments").css("height","50px");
		$("#formBox #button_text").html("Get More Information").css("padding-left","30px");
	}else{
		$("#formBox #form_comments").css("height","13px");
	}
	
	if($("#checkbox2").is(':checked') && !$("#checkbox1").is(':checked') && !$("#checkbox3").is(':checked')){
		$("#formBox .title").html("Get The Newsletter");
		$("#formBox #button_text").html("Join Mailing List");
	}
	
	if($("#checkbox3").is(':checked')){
		$("#formBox .title").html("Start Immediately");
		$("#formBox #button_text").html("Start My Registration");
		
	}
	
	if(!$("#checkbox1").is(':checked') && !$("#checkbox2").is(':checked') && !$("#checkbox3").is(':checked')){
		$("#formBox .title").html("Need More Information?");
		$("#formBox #form_comments").css("height","13px");
		$("#formBox #button_text").html("Get More Information");
	}
}

function tabsFunctionality(){
	
	$("#tabs li").bind("click", function(){
		var tab = $(this).attr("tab");
		if(tab!="howitworks"){
			OURTrackPageView(tab);
		}
		if(tab != "dummy"){
			$(".tabContent").hide();
			$("#"+tab).show();
			$("li").removeClass('current');
			$("li[tab|='"+tab+"']").addClass("current");
		}
		randomizeTestimonials();
	});
}

function randomizeTestimonials(){
	$.ajax({
		url: "includes/testimonial-rotate-include.php",
		dataType: 'html',
	 	success: function(data){
	    	$("#testimonials").html(data);
	  	}
	});
	
}

function selectHowToSubtab(){
	var selectedTab = $("#nav li[class|='selectedMain']").attr("id");
	if(selectedTab == "cat1"){
		$("#howitworks-practitioner").show();
		$("#howitworks-master").hide();
	}
	if(selectedTab == "cat2"){
		$("#howitworks-practitioner").hide();
		$("#howitworks-master").show();
	}

}

function howToSubtabFunctionality(){
	$(".howto-subtabs_item").bind("click",function(){
		var tab = $(this).attr("subtab");
		OURTrackPageView("howitworks/"+tab);
		if(tab == "cat1"){
			$("#howitworks-practitioner").show();
			$("#howitworks-master").hide();
		}
		if(tab == "cat2"){
			$("#howitworks-practitioner").hide();
			$("#howitworks-master").show();
		}
		randomizeTestimonials();
	})
	
}

function sendForm(button){
	$('#formBox').find("input").removeClass("wrongField");
	var goal = $("#goal").val();
	var notes = "";
	if($('#form_comments').val()!=$('#form_comments').attr("placeholder")){
		notes = $('#form_comments').val();
	}
	if ($('#checkbox2').is(':checked')){
		notes = notes+"\n---\nGet monthly news & special offers";
	}
	
	if ($('#checkbox2').is(':checked') && !$('#checkbox1').is(':checked') && !$('#checkbox3').is(':checked')){
		goal = $('#checkbox2').attr("val");
		request = $('#topic').val();
	}
	
	var request = $('#form_request').val();
	if ($('#checkbox3').is(':checked')){
		goal = $('#checkbox3').attr("val");
		request = $('#topic').val();
	}
	if(!validateFields($("#formBox").find("input[required='required']"))) return;
	$('#formBox').find("input, textarea").each(function(){
    	if($(this).attr("placeholder")!=""){
        	if($(this).val()==$(this).attr("placeholder")) $(this).val("");
   
        }
	});
	changeButton($("#formBox"));
	$.ajax({
		url: '/includes/form_processing_ajax.php',
		type: 'POST',
		data: {
			name : $('#form_name').val(), 
			email : $('#form_email').val(), 
			phone : $('#form_phone').val(),
			notes : notes+'\n---\nFrom URL: '+$('#current_url').val()+"\nPlan Type: "+$('#plan').val()+"\nTopic: "+$('#topic').val(),
			request_sent : $('#request_sent').val(),
			referrer : $('#referrer').val(),
			ipaddr : $('#ipaddr').val(),
			hostname : $('#hostname').val(),
			request_uri : $('#request_uri').val(),
			goal : goal,
			plan : $('#plan').val(),
			topic : $('#topic').val(),
			request: request,
			antibot: navigator.appName
		},
		dataType: 'json',
	  	success: function(data) {
			processFormProcessingResult(data);
			trackGoal(goal);
	  	}
	});
}

function validateFields(fields){
	var namePattern = /^[A-Za-z0-9 ]{3,40}$/; //Alphabets, numbers and space. No special characters and min 3 and max 40 characters.
	var emailPattern = /^[A-Za-z0-9.'_%+-]+@[A-Za-z0-9.'_%+-]+\.[A-Za-z]{2,4}$/; //Email
	var numberPattern = /^\d$/; //Just numbers
	//Phone numbers (http://regexlib.com/RETester.aspx?regexp_id=688)
	var phonePattern = /^(?:(?:[\+]?([\d]{1,3}(?:[ ]+|[\-.])))?[(]?([\d]{3})[\-\/)]?(?:[ ]+)?)?([a-zA-Z2-9][a-zA-Z0-9 \-.]{6,})(?:(?:[ ]+|[xX]|(i:ext[\.]?)){1,2}([\d]{1,5}))?$/;
	var allCorrect = true;
	$(fields).each(function (i) {
		if($(this).attr("placeholder") == $(this).val()) {
			$(this).addClass("wrongField");
			allCorrect = false;
		};
		switch($(this).attr("validate")){
			case 'name':
				var name = $(this).val();
				if(name.length == 0){
					$(this).addClass("wrongField");
					allCorrect = false;
				}
				break;
			case 'email':
				if(!emailPattern.test($(this).val())){
					$(this).addClass("wrongField");
					allCorrect = false;
				}
				break;
			case 'number':
				if(!numberPattern.test($(this).val())){
					$(this).addClass("wrongField");
					allCorrect = false;
				}
				break;
			case 'phone':
				if(!phonePattern.test($(this).val())){
					$(this).addClass("wrongField");
					allCorrect = false;
				}
				break;
			default:

				break;
		}
	});
	return allCorrect;
}

function changeButton(form){
	$("#form_result").hide();
	$("#button_sending").show();
}

function processFormProcessingResult(result){
	if(result.id > 0 && result.id < 100){
		//$('#form_result').html('<div style="width:95%; text-align:center; height:34px"><b>'+result.string+'</b></div>');	
		result.string = "Thank you!<br />You'll be hearing from us soon.<br />In the meantime,<br />have you seen our<br /><a href='/internet-marketing-training-certification-courses-tour?top=about&topic=Tour'>video tour?</a>";
		$('#audit_box').css("height","241px").html('<div style="padding-top:50px;line-height:25px; text-align:center;font-size:16px"><b>'+result.string+'</b></div>');	
	}else if(result.id > 100){
		//redirects
		$('#audit_box').css("height","241px").html('<div style="padding-top:80px;line-height:25px; text-align:center;font-size:16px"><b>'+result.string+'</b></div>');	
		document.location.href = result.url;
	}else if(result.id < 0){
		$('#button_sending').hide();
		$('#form_error').show();
		//$('#form_result').html('<div class=\'form_error\'>There was en error sending the form, try again please.</div>');
	}
}
function recoverError(){
	$('#form_error').hide();
	$('#form_result').show();
	
}
