$(document).ready(function() {
	// make blocks height equal
	$(".twocols .stretch_height").each(function() {
		var tallest = $(this).parents('.leftcol, .rightcol').innerHeight();
		// find the tallest column first
		$(this).parents('.twocols').find('.rightcol, .leftcol').each(function() {
			tallest = $(this).height() > tallest ? $(this).height() : tallest;
		});
		var siblingsHeight = $(this).siblings().outerHeight(true);
		
		var margins = $(this).outerHeight(true) - $(this).height();
		$(this).height(tallest - siblingsHeight - margins);
	});
	
	var restyle_labels = function restyle_labels() {
		$('label').text(function(index, text) {
			if ($(this).hasClass('required')) {
				$(this).css('color', '#000000');
				$(this).html(text.replace(/[:*]/, '<span class="asterisk"> *</span>'));
			} else {
				$(this).html(text.replace(':', ''));
			}
		});
		
		$('div.asterisk_message, #required_fields_message').text(function(index, text) {
			$(this).html(text.replace('*', '<span class="asterisk"> *</span>'));	
		});
	};
	
	//this will be called once ajax stop
    $("body").ajaxStop(restyle_labels);
	
	restyle_labels();
});


function get_dimensions() 
{
	var dims = {width:0,height:0};
	
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    dims.width = window.innerWidth;
    dims.height = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    dims.width = document.documentElement.clientWidth;
    dims.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    dims.width = document.body.clientWidth;
    dims.height = document.body.clientHeight;
  }
  
  return dims;
}

function add_tax(priceField) 
{
	var taxPercentage = $("#tax_percent_name_1").val();
	var fieldName = "#" + priceField;
	var unitPrice = $(fieldName).val(unitPrice);
	var totalPrice = 0;
	if (unitPrice && unitPrice.length != 0) 
	{
		unitPrice = parseFloat(unitPrice);
		var factor = parseFloat(1 + taxPercentage / 100);
		totalPrice = unitPrice * factor;
	}
	$("#taxed_" + priceField).val(totalPrice);
};

function substract_tax(priceField, taxPercentage) 
{
	var totalPrice  = $("#taxed_" + priceField).val();
	var unitPrice = 0;
	if (totalPrice && totalPrice.length != 0) 
	{
		totalPrice = parseFloat(totalPrice);
		var factor = parseFloat(1 + taxPercentage / 100);
		var taxAmount = totalPrice * factor;
		unitPrice = totalPrice / factor;
	}
	unitPrice = unitPrice.toFixed(2);
	$("#" + priceField).val(unitPrice);
};

function add_tax_calculators(priceField, taxPercentage) {
	$("#" + priceField).keyup(function() 
			{
		add_tax(priceField, taxPercentage);
	});
	// calculate the item unit price based on the total price and the tax amount
	$("#taxed_" + priceField).keyup(function() {
		substract_tax(priceField, taxPercentage);
	});
}

//use class augmentation to add a rounding method to the default javascript number class
if (!Number.toFixed) 
{
	Number.prototype.toFixed=function(n) {
		return Math.round(this*Math.pow(10, n)) / Math.pow(10, n);
	};
}


function handle_validation(response) 
{
	if (!response.success && !response.validated) 
	{
		var error_message_box = 'ul#error_message_box';
		var index;
		// server side validation failed.. record won't be saved
		$(error_message_box).empty();
		for(index in response.error_messages) 
		{
			// get validation messages from array and show those to the user
			var message = response.error_messages[index];
			$(error_message_box).append("<li>" + message +  "</li>");
			$(error_message_box).css("display", "");
		}
		return false;
	}
	return true;
}

function handle_server_validation(response) 
{
	if (handle_validation(response)) 
	{
		if (!response.redirect) {
			tb_remove();
			post_form_submit(response);
		} else {
			// redirect and show success mesage here
			var current_url = $(location).attr('href');
			var customer_id = response.id;
			// build a url here and redirect..
			var new_url = current_url.replace('subscribe', 'confirm_subscription');
			$(location).attr('href', new_url + '/' + customer_id);
		}
	}
}

function count(object)
{
	var c = 0, i=0;
	for(i in object) {
		if (object.hasOwnProperty(i)) {
			c++;
		}
	}
	return c;
};

function set_feedback(text, classname, keep_displayed)
{
	if(text!='')
	{
		$('#feedback_bar').removeClass();
		$('#feedback_bar').addClass(classname);
		$('#feedback_bar').html(text);
		$('#feedback_bar').css('opacity','1');

		if(!keep_displayed)
		{
			$('#feedback_bar').fadeTo(5000, 1);
			$('#feedback_bar').fadeTo("fast",0);
		}
	}
	else
	{
		$('#feedback_bar').css('opacity','0');
	}
}


