/**
 * Filename: scripts.js
 * @author: Simon Jensen
 */

/***************************************************************************/
//GENERAL PURPOSE FUNCTIONS
/***************************************************************************/
function is_email(email)
{
	var apos = email.indexOf("@");
	var dotpos = email.lastIndexOf(".");
	
	if (apos<1||dotpos-apos<2) {
		return false;
	} else {
		return true;
	}
}

/***************************************************************************/
// APPLY - STEP 1
/***************************************************************************/
function get_kids_html() {
	var selected_option = "";
	$("#kids_select option:selected").each(function () {
		selected_option = $(this).val();
	});
	if(selected_option > 0) {
		$.post("/actions/apply.php", {
			todo: "get_num_of_kids_html",
			num_of_kids: selected_option
		},
		function(data) {
			$("#kids_option").html(data).slideDown("slow");
		});
	} else {
		$("#kids_option").slideUp("slow", function() {
			$("#kids_option").html("");
		});
	}
}

$(document).ready(function() {
	
	$("input[name='married']").change(function() {
		var checked_mariate_status = $("input[name='married']:checked").val();
		if(checked_mariate_status == "married") {
			$("#married_option").slideDown("slow");
		} else {
			$("#married_option").slideUp("slow");
		}
	});
	
	var apply_form_step_1_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#errors").html('<a class="right" href="#" onclick="$(\'#errors\').fadeOut(); return false;">x</a>').css("display", "none");
			$("#submitter").attr("disabled", "true"); 
			$("#loading_anim").show();
		},
		success: function(data) {
			$("#submitter").removeAttr("disabled"); 
			$("#loading_anim").hide();
			if(data.err == 0) {
				location.href = ""+data.redir+"";
			} else {
				$.each(data.errors, function(i, val) {
					$("#errors").append(val.message+'<br />');
				});
				$("#errors").slideDown();
				$.scrollTo('#errors', 800);
			}
		}
	};
	$('#apply_form_step_1').ajaxForm(apply_form_step_1_options);
	
});

/***************************************************************************/
// APPLY - STEP 2
/***************************************************************************/
function select_plan(product_id) {
	$("#selected_plan").val(product_id);
	$('#apply_form_step_2').submit();
}

$(document).ready(function() {
	
	var apply_form_step_2_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#errors").html('<a class="right" href="#" onclick="$(\'#errors\').fadeOut(); return false;">x</a>').css("display", "none");
			$("#submitter").attr("disabled", "true"); 
			$("#loading_anim").show();
		},
		success: function(data) {
			$("#submitter").removeAttr("disabled"); 
			$("#loading_anim").hide();
			if(data.err == 0) {
				location.href = ""+data.redir+"";
			} else {
				$.each(data.errors, function(i, val) {
					$("#errors").append(val.message+'<br />');
				});
				$("#errors").slideDown();
				$.scrollTo('#errors', 800);
			}
		}
	};
	$('#apply_form_step_2').ajaxForm(apply_form_step_2_options);
	
});

/***************************************************************************/
//  APPLY - STEP 3
/***************************************************************************/
function reload_to_anchor(anchor) {
	location.href = location.href.split(/\?|#/)[0] + '#' + anchor;
	location.reload(true);
	return false;
}

function add_product(product_id, language_id) {
	$.post("/actions/apply.php", {
		todo: "add_product",
		product_id: product_id,
		language_id: language_id
	},
	function(data) {
		location.href = data.redir;
	}, "json");
}

function remove_product(product_id) {
	$.post("/actions/apply.php", {
		todo: "remove_product",
		product_id: product_id
	},
	function(data) {
		location.reload();
	});
}

$(document).ready(function() {
	
	$("#dd_radio").click(function() {
		$("#payment_cc").fadeOut(function() {
			$("#payment_dd").fadeIn();
			$("#payment_key").val(8);
		})
	});
	
	$("#cc_radio").click(function() {
		$("#payment_dd").fadeOut(function() {
			$("#payment_cc").fadeIn();
			$("#payment_key").val(6);
		})
	});
	
});

/***************************************************************************/
// LOGIN
/***************************************************************************/
$(document).ready(function() {
	
	var customer_number_form_options = {
		type: 'post',
		dataType: 'json',
		resetForm: false,
		beforeSubmit: function() {
			$("#customer_number_").attr("disabled", "true"); 
			$("#customer_number_loading_anim").show();
		},
		success: function(data) {
			$("#customer_number_").removeAttr("disabled"); 
			$("#customer_number_loading_anim").hide();
			location.href = ""+data.redir+"";
		}
	};
	$('#customer_number_form').ajaxForm(customer_number_form_options);
	
});

/***************************************************************************/
// OTHER
/***************************************************************************/
function toggle_fade(container) {
	var div_object = $("#"+container);
	if(div_object.css("display") == "none") {
		div_object.fadeIn("fast");
	} else {
		div_object.fadeOut("fast");
	}
}

$(document).ready(function() {
	
	// Change language
	$("#language_select").change(function () {
		var selected_language = "";
		$("#language_select option:selected").each(function () {
			selected_language = $(this).val();
		});
		if(selected_language != "") {
			location.href = ''+selected_language+'';
		}
	}).change();
	
	// Set country_phone_code based on selected country (iso2)
	$("#country_dd").change(function () {
		var country_iso = "";
		$("#country_dd option:selected").each(function () {
			country_iso = $(this).val();
		});
		if(country_iso != "") {
			$.post("/actions/country.php", {
				todo: "get_country",
				iso: country_iso
			},
			function(data) {
				if(data.err == 0) {
					$("#phone_country_code").val(data.country_code);
					$("#phone_mobile_country_code").val(data.country_code);
				}
			}, "json");
		}
	}).change();
	
	// Use effect to open link
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	$(".tooltip_tricker").tooltip({ effect: 'slide'});
	
	// Language menu
	$("#language_menu").mouseenter(function() {
		$("#languages").slideDown(200);
	}).mouseleave(function() {
		$("#languages").slideUp(200);
	});
	
	//Check in every minute
	var checkin = setInterval(function() {
		$.post("/actions/login.php", {
			todo: "ping"
		},
		function(data) {
			// console.log(data.message);
		}, "json");
	}, 60000);
	
});
