debug_tlds = getCookie('debug_tlds') || undefined;

$.fn.outerHtml = function(){
	return $('<div></div>').append( $(this[0]).clone() || '' ).html();
};
function get_hash() {
	var pos = location.hash.indexOf('#');
	if (pos == -1)
		return [];

	var params = location.hash.substring(pos + 1);
	params = params.split('/');
	return params;
}

var validate_count = 0;
var do_proceed = undefined;
function validate_input(jobj) {
	validate_count++;

	if (jobj.attr('empty') == 1 && jobj.val() == '') {
		jobj.attr('validated', 1).css('border', '1px solid green');
		validate_count--;
		return;
	}

	var as = jobj.attr('as');
	var value = encodeURIComponent(jobj.val());
	if (as == 'vies') {
		var vatPrefix = $('[name="country"]').val();
		if (!value.startsWith(vatPrefix)) {
			value = vatPrefix + '-' + value;
		} else {
			value = vatPrefix + '-' + value.slice(vatPrefix.length);
		}
	}
	$.get('ajax.php?action=validate&value=' + value + '&as=' + as, function(data){
		if (!data['status']) {
			jobj.attr('validated', 0).css('border', '1px solid red');
			if (data['msg'] && !$('#invalid_msg_' + as)[0]) {
				$.each($('#invalid_msg_' + as), function() {
					$(this).closest('div.achtungFail').achtung('close');
				});
				showMsg('error', '<span id="invalid_msg_' + as + '"></span>' + data['msg']);
			}
		} else {
			
			if ($('#invalid_msg_' + as).closest('div.achtungFail').length)
				$('#invalid_msg_' + as).closest('div.achtungFail').achtung('close');
			else if ((jobj.attr('id') != undefined) && $('#invalid_msg_' + jobj.attr('id')).closest('div.achtungFail').length)
			 	$('#invalid_msg_' + jobj.attr('id')).closest('div.achtungFail').achtung('close');

			jobj.attr('validated', 1).css('border', '1px solid green');
		}
		
		validate_count--;
		
		if (validate_count == 0  && typeof do_proceed == 'function' && data['status']) {
			do_proceed();
			do_proceed = undefined;
		}

	}, 'json');
}

function validate_all(container) {
	$('input.validate:visible,select.validate:visible', container).each(function(){
		if (typeof $(this).attr('empty') == 'undefined') {
			if ($(this).val() == '') {
				$(this).attr('validated', 0).css('border', '1px solid red');
				var msg_id = typeof $(this).attr('id') != 'undefined' ? $(this).attr('id') : $(this).attr('as');
				
				$.each($('#invalid_msg_' + msg_id), function() {
					$(this).closest('div.achtungFail').achtung('close');
				});
				
				if (typeof invalid_details['invalid_' + msg_id] == 'undefined')
					console.error('invalid_' + msg_id + ' is not defined');
				else 
					showMsg('error', '<span id="invalid_msg_' + msg_id + '"></span>' + invalid_details['invalid_' + msg_id]);
			}
		} else {
			if ($(this).val() == '') {
				$(this).attr('validated', 1).css('border', '1px solid green');
				
				if ($('#invalid_msg_' + $(this).attr('id')).closest('div.achtungFail').length)
					$('#invalid_msg_' + $(this).attr('id')).closest('div.achtungFail').achtung('close');
				else if ($('#invalid_msg_' + $(this).attr('as')).closest('div.achtungFail').length)
			 		$('#invalid_msg_' + $(this).attr('as')).closest('div.achtungFail').achtung('close');
			}
		}
	});

	var errors = $('input.validate:visible,select.validate:visible', container).filter("[validated='0']");
	if (errors.length > 0) {
		errors.eq('0').focus();
		return false;
	}

	return true;		
}

function updateTotalPrices() {
	var curr = getCurrency();
	var amount = parseFloat(curr.attr('price'));

	var domains_amount = 0;
	
	var debug = false;
	
	var upgrades = domains = ssl = tldssl = storage = 0
	$('span.upgrade_price:visible', panel).each(function() { 
		if (!isNaN(parseFloat($(this).attr('price')))) {
			var period = getPeriod().val();
			var _price = parseFloat($(this).attr('price'));
			amount = amount + _price;
			upgrades = upgrades + _price;
		}
	});

	$('span.tld-price,div.tld-price-id-protect', $('#selected_domains')).each(function(){
		if (!isNaN(parseFloat($(this).attr('price')))) {
			amount = amount + parseFloat($(this).attr('price'));
			domains = upgrades + parseFloat($(this).attr('price'));
			domains_amount += parseFloat($(this).attr('price'));
		}
	});

	$('.additional_ssl .ssl_price', $('#products-block, #Host_Domain:visible')).each(function(){
		if (!isNaN(parseFloat($(this).data('price')))) {
			amount = amount + parseFloat($(this).data('price'));
			ssl = ssl + parseFloat($(this).attr('price'));
		}
	});

	$('.tld-price-ssl-certificate', $('#selected_domains')).each(function(){
		if (!isNaN(parseFloat($(this).data('price')))) {
			amount = amount + parseFloat($(this).data('price'));
			tldssl = tldssl + parseFloat($(this).data('price'));
		}
	});

	$('[name^=storage_configuration]:visible', panel).each(function() {
		if (($(this).val() == ''))
			return;

		var currency = curr.val();
		var elem = $(this).find('[value="' + $(this).val() + '"]');
		var disk_price = elem.data('price-' + currency.toLowerCase());
		var default_disk_price = $(this).find('option[data-price="0"]').data('default-price-' + currency.toLowerCase());
		var period = getPeriod().val();

		switch($(this).data('disk-type')) {
			case "default":
				if ((typeof disk_price == 'undefined') || (typeof default_disk_price == 'undefined') || (disk_price == 0))
					return;
				
				var disk_price = parseFloat(disk_price - default_disk_price);
				
				if (isNaN(disk_price))
					return;
			break;
			case "additional":
				if ((typeof disk_price == 'undefined'))
					return;
				
				if (isNaN(disk_price))
					return;
			break;
			default:
				return;				
		}
		
		amount = amount + (disk_price * period);	
		storage = storage + (disk_price * period);	
	});
	
	if (debug) {
		console.log('plan: ' + amount);
		console.log('period: ' + period);
		console.log('upgrades: ' + upgrades);
		console.log('domains: ' + domains);
		console.log('ssl: ' + ssl);
		console.log('tldssl: ' + tldssl);
		console.log('storage: ' + storage);
	}
	
	var currency = curr.val();
	var country = $('#client_country');
	var option = $('option:selected', country);

//kriss
	var tax_group = $('input#tax_group').val();
	var check_state = option.attr('states');
	var get_state = $('#client_state_us').val();
	var data_tax_attr = $('#payment-block span.selected a').attr('data-tax');
	if (typeof data_tax_attr == 'undefined' || data_tax_attr == '')
		var vat = '';
	else
		var vat = parseFloat(data_tax_attr);
	//alert (vat);
	
	discount = 0;
	if (typeof discount_applied_msg != 'undefined') discount_applied_msg.remove();
	if (typeof discount_is_zero_msg != 'undefined') discount_is_zero_msg.remove();
	if (typeof discount_error_msg != 'undefined') discount_error_msg.remove();

	$('#original_amount').parent().hide();
	$('#amount_total').parent().hide();

	$('#euro_amounts').hide();
	$('#original_amount, #amount_discount').parent().hide();
	//$('#original_amount, #amount_discount').html('');

	if ($('input[name="promo_code"]').val()) {
		$.ajax({
			type: "POST",     
			async: false,
			url: 'ajax.php?action=get_discount&code=' + $('input[name="promo_code"]').val(),
			data: $('#orderForm').serialize(),
			dataType: 'json',
			success: function(data) {
				discount = 0;
				
				if (data.discount) {
					$('#original_amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
					$('#original_amount').parent().show();

					promo_code_discount = data.discount;
					amount = amount - promo_code_discount;
					discount = discount + promo_code_discount;

					if ($('#amount_discount').html() == '')
						showMsg('success', discount_applied);

					$('#amount_discount').html('-' + sprintf(currency_symbols[currency] + '%0.2f ' + currency, discount));
					$('#amount_discount').parent().show();
					$('#euro_amounts').show();
					
				} else { 
					//$('#original_amount, #amount_discount').html('');
				
					if (data.error_msg){
						discount_error_msg = showMsg('error', data.error_msg);
					} else {
						discount_is_zero_msg = showMsg('error', discount_is_zero);
					}
				}
			}
		});
	} else {
		if ($('#original_amount').is(':visible')) {
			$('#original_amount').html('');
			$('#original_amount').parent().hide();
		}

		if ($('#amount_discount').is(':visible')) {
			$('#amount_discount').html('');
			$('#amount_discount').parent().hide();
		}

		if ($('#euro_amounts').is(':visible')) {
			$('#euro_amounts').hide();
		}
	}
	
	// check for promo_domain
	if (get_promo() != 'none') {
		var promo_domain_element = $('#selected_domains_table input[name="promo"]');
		if ($('#selected_' + promo_domain_element.attr('value')).length) {
			promo_domain_discount = $('#selected_' + promo_domain_element.attr('value').replace(/\./, '_') + ' .tld-price').attr('price');
			if (promo_domain_discount) {
				$('#original_amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));		
				$('#original_amount').parent().show();
				amount = amount - promo_domain_discount;
				discount = discount + promo_domain_discount;
			}
			
		}
	}

	var trial = getSelectedPlanOption().data('trial');

	if (vat !== '') {
		$('#amount_discount').parent().css({'margin-bottom': '5px'});
		var amount_vat = amount * (vat / 100);
	
		total_amount = amount + amount_vat;

		domains_amount += (domains_amount * (vat / 100));
		/*
		if (discount) {
			$('#amount_discount').html('-' + sprintf(currency_symbols[currency] + '%0.2f ' + currency, discount));
			$('#amount_discount').parent().show();
		} else {
			$('#amount_discount').parent().hide();
			$('#amount_discount').html('');
		}
		*/

		//alert (vat);
		$('#vat_percent').text((vat) + '%');
		$('#amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
		$('#amount_vat').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount_vat));
	
		if (trial) {
			setTrialPriceInfo(total_amount - domains_amount, domains_amount, trial);
		} else {
			removeTrialPriceInfo();
			$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, total_amount));
		}
		

		$('#euro_amounts').show();
		//alert(1);
	} else {
		total_amount = amount;
		/*
		if (discount) {
			$('#amount_discount').html('-' + sprintf(currency_symbols[currency] + '%0.2f ' + currency, discount))
			$('#amount_discount').parent().show();
		} else {
			$('#amount_discount').parent().hide();
			$('#amount_discount').html('');
		}
		*/

		//alert(2);
		$('#amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
		$('#amount_vat').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount_vat));

		if (trial) {
			setTrialPriceInfo(total_amount - domains_amount, domains_amount, trial);
		} else {
			removeTrialPriceInfo();
			$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, total_amount));
		}
	}
	
	$('#amount_total').parent().show();
	
	/*
	if (tax_group == 'uk' && typeof option.attr('uk_business_tax') !== 'undefined'
			&& option.attr('uk_business_tax') !== false
			&& typeof option.attr('uk_not_business_tax') !== 'undefined'
			&& option.attr('uk_not_business_tax') !== false) {
			$('#euro_amounts').css('display', '');
		if ($('input.is_business:checked').val() == 1)
			var vat = parseFloat(option.attr('uk_business_tax'));
		else
			var vat = parseFloat(option.attr('uk_not_business_tax'));
		var amount_vat = amount * (vat / 100);
		$('#vat_percent').text((vat) + '%');
		$('#amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
		$('#amount_vat').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount_vat));
		$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount + amount_vat));
	} else if (tax_group == 'us' 
			&& typeof option.attr('us_tax') !== 'undefined' 
			&& option.attr('us_tax') !== false 
			&& get_state.indexOf(check_state) >= 0) {
			$('#euro_amounts').css('display', '');
		var vat = parseFloat(option.attr('us_tax'));
		var amount_vat = amount * (vat / 100);
		$('#vat_percent').text((vat) + '%');
		$('#amount').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
		$('#amount_vat').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount_vat));
		$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount + amount_vat));
	} else {
		$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));
		$('#euro_amounts').css('display', 'none');
	}*/
}

function removeTrialPriceInfo() {
	$('#amount-trial-info').hide();
}

function setTrialPriceInfo(amount, domains_amount, trialPeriod) {

	$('#amount_total').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, domains_amount) + ' <span class="red">*</span>');
	$('#amount-trial-info .trial-info').html(tr('trial_info_msg', {days: trialPeriod}));

	var date = new Date();
	date.setDate(date.getDate() + trialPeriod);

	$('#amount-trial-info .due-on').html(date.toISOString().split('T')[0]);

	$('#amount-trial-info .subtotal').html(sprintf(currency_symbols[currency] + '%0.2f ' + currency, amount));

	$('#amount-trial-info').show();
}

function getCurrency() {
	//var container = $('div.prices:visible', panel);
	switch($(panel).attr('id')) {
		case "SSL":
			var ssl_type = $('#SSL select[name="ssl_data[type]"]').val();	
			var container = '#' + ssl_type + '_prices_' + getSelectedPlanOption().attr('value') + '_' + getPeriod().val();
		break;
		default:
			var container = '#price_' + getSelectedPlanOption().attr('value') + '_' + getPeriod().val();
	}
	
	return $('input.currencies:checked', $(container));
}

function getPeriod(container) {
	if (container)
		return $('input.periods:checked:visible', container);

	var p = '#periods_' + getSelectedPlanOption().val();
	switch($(panel).attr('id')) {
		case "SSL":
			return $('#SSL select[name="ssl_data[period]"]');
		//default:
		//case "Domain_Names":
		//case "Dedicated":
		//	return $('input.periods:checked:visible', panel);
		default:
			return $(p + ' input.periods:checked', panel);
	}
}

function getSelectedPlanOption() {
	switch($(panel).attr('id')) {
		case "Domain_Names":
		case "SSL":
			return $('#' + $(panel).attr('id') + ' input.plans');
		break;
		default:
			return $('select.plans option:selected', panel);
	}
}

function get_promo() {
	var opt = getSelectedPlanOption();
	var period = getPeriod().val();
	var _promo = 'none';

	if (period > 1 && opt.attr('free') == '1') {
    	$('span.promo_text', $('#selected_domains_table')).text(promo_msg);
    	$('td.promo_td', $('#selected_domains_table')).css('visibility', 'visible');
    	_promo = 'free';
	} else if (period > 1 && opt.attr('free') == '0') {
		$('span.promo_text', $('#selected_domains_table')).text(promo_msg);
		$('td.promo_td', $('#selected_domains_table')).css('visibility', 'visible');
    	_promo = 'promo';
	} else {
		$('td.promo_td', $('#selected_domains_table')).css('visibility', 'hidden');
	}		
	
	// loop added domains and hide promo_td if necessary
	$('td.promo_td', $('#selected_domains_table')).each(function() {
		var tr = $(this).closest('tr');
		var tld = tr.attr('tld');
		var domain = tr.attr('id').replace(/^selected_/, '').replace(/_/g, '.');
		var action = tr.data('action');
		var period = tr.find('.domain_period').val();
		var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(tld) != -1;
		var prefix = '[' + domain + '] get_promo() ';

		var hide = true;
		
		var d_price = get_tld_price('domain', tld, action, period, currency);
		var normal_d_price = get_tld_price('domain', tld, 'prices', period, currency);

		var rd_price = get_tld_price('registerDomain', tld, action, period, currency);
		var normal_rd_price = get_tld_price('registerDomain', tld, 'prices', period, currency);
		
		if (rd_price && normal_d_price && d_price == false && rd_price < normal_d_price)
			normal_d_price = false;		
		
		if (debug) {
			console.log(prefix + 'normal_rd_price: ' + normal_rd_price + '; rd_price:' + rd_price + '; normal_d_price: ' + normal_d_price + '; d_price: ' + d_price);
		}
		
		if (
			is_tld_free(tld) 
			|| (d_price && normal_d_price && d_price < normal_d_price)
			|| (normal_d_price && normal_rd_price && normal_d_price < normal_rd_price)
			|| (rd_price && normal_rd_price && rd_price < normal_rd_price)
		)
			hide = false;
		
		if (!hide)
			return;

		$(this).css('visibility', 'hidden');
	});
		
	return _promo;
}

function is_tld_free (tld) {
	for (i in free_tlds) {
		if (i != getSelectedPlanOption().val())
			continue;
		
		for (x in free_tlds[i])
			if (tld == free_tlds[i][x])
				return true;
	}
	
	return false;

	/*
	var hasMatch = false;
	var opt = getSelectedPlanOption();
	
	$.each(free_tlds, function(plan_id, tlds) {
		if (plan_id == opt.val()) {
			$.each(tlds, function(k, t) {
				if (t == tld) {
					hasMatch = true;
					return false;
				}
			});
			
			if (hasMatch)
				return false;
		}
	});

	if (hasMatch)
		return true;

	return false;
	*/
}

function set_promo_price(domain, promo) {
	if (typeof domain == 'object') {
		var dom_tr = domain;
		var domain = dom_tr.attr('id').replace(/\_/g, '.');
	} else {
		var dom_tr = $('#selected_' + domain.replace(/\./g, '_'));
	}

	var price = $('span.tld-price', dom_tr);
	var period = $('input.domain_period', dom_tr).val();
	var action = dom_tr.find('input.transfer_domain').size() ? 'transfer' : 'register';
	
	var tld = domain.substring(domain.indexOf('.') + 1);

	var prefix = 'set_promo_price(' + (typeof domain != 'undefined' ? domain : 'undefined') + ', ' + (typeof promo != 'undefined' ? promo : 'undefined') + ') ';
	var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(tld) != -1;

	if (price.hasClass('promo-price'))
		price.removeClass('promo-price');

	if (promo != 'free') { 
		var promotions = registerdomainProducts[tld]['promotions'];
		if (typeof promotions != 'undefined') {
			for (var promo_id in promotions) {
				var promo_details = promotions[promo_id];
				break;
			}
	
			var d_price = get_tld_price('domain', tld, 'prices', promo_details['period'], currency);
			var d_promo_price = get_tld_price('domain', tld, promo_details['promo_type'], promo_details['period'], currency);
			
			if (d_price && d_promo_price && d_promo_price < d_price)
				promo = 'promo';
		}
	}
		
	var d_price = get_tld_price('domain', tld, action, period, currency);
	var normal_d_price = get_tld_price('domain', tld, 'prices', period, currency);

	var rd_price = promoPrice = get_tld_price('registerDomain', tld, action, period, currency);
	var normal_rd_price = normalPrice = get_tld_price('registerDomain', tld, 'prices', period, currency);

	if (rd_price && normal_d_price && d_price == false && rd_price < normal_d_price)
		normal_d_price = false;

	var renewal_price = get_tld_renewal_price(tld);

	prefix += ' promo_domain: ' + (typeof promo_domain != 'undefined' ? promo_domain : '') + ' ';

	if (typeof promo_domain != 'undefined' && promo_domain == domain && promo == 'none') {
		if (d_price) d_price = false;
		if (normal_d_price) normal_d_price = false;
	}
	
	var first_period = get_tld_first_period(tld);

	var first_period_normal_d_price = get_tld_price('domain', tld, 'prices', first_period, currency);

	var domain_info_tooltip = promo_price_renewal_tooltip.replace('@years@', domain_period_text(period)).replace('@renewal_price@', currency_symbols[currency] + sprintf('%0.2f', renewal_price));

	if (debug)
		console.log(prefix + 'd_price: ' + d_price + '; normal_d_price: ' + normal_d_price + '; rd_price: ' + rd_price + '; normal_rd_price: ' + normal_rd_price);
	
	if (promo == 'free' && is_tld_free(tld)) {
		var first_period = get_tld_first_period(tld, 'domain');

		if (debug)
		    console.log(prefix + 'promoPrice changed from ' + promoPrice + ' to ' + (normal_d_price - first_period_normal_d_price) + '; normal_d_price: ' + normal_d_price + '; discount price: ' + first_period_normal_d_price);
			
		var promoPrice = normal_d_price - first_period_normal_d_price;
		
		if (promoPrice < normal_d_price) {
			var normalPrice = promoPrice == 0 ? normal_rd_price : normal_d_price;

			dom_tr.find('.hide-if-promo').hide();
			
			var domain_info_tooltip = (promoPrice == 0 ? promo_price_free_tooltip : promo_price_renewal_tooltip).replace('@years@', domain_period_text(first_period)).replace('@renewal_price@', currency_symbols[currency] + sprintf('%0.2f', normalPrice));
		}
	} else if ((d_price || normal_d_price) && (promo == 'promo' || (promo == 'free' && !is_tld_free(tld)))) {
		if (d_price && d_price < normal_d_price) {
	    	if (debug)
		    	console.log(prefix + 'promoPrice changed from ' + promoPrice + ' to d_price ' + d_price);

	    	var promoPrice = d_price;
			var domain_info_tooltip = promo_special_price_renewal_tooltip;
		} else if (normal_d_price < normal_rd_price) {
	    	if (debug)
		    	console.log(prefix + 'promoPrice changed from ' + promoPrice + ' to normal_d_price ' + normal_d_price);

	    	var promoPrice = normal_d_price;
	    }

		dom_tr.find('.hide-if-promo').hide();
	} else {
		dom_tr.find('.hide-if-promo').show();
	}
	
	if (debug)
		console.log(prefix + 'normalPrice: ' + normalPrice + '; promoPrice: ' + promoPrice);
	
	if (promoPrice === false || promoPrice >= normalPrice) {
		price.html(currency_symbols[currency] + sprintf('%0.2f', normalPrice));
		price.attr('price', normalPrice);
		return;
	}

	if (debug) {
		console.log(prefix + 'promo-price class added');
		console.log(price);
	}
	
	price.addClass('promo-price').attr('price', promoPrice).html(currency_symbols[currency] + sprintf('%0.2f', promoPrice))
		.append(' <span class="info tip" title="' + domain_info_tooltip + '"></span>')
		.append('<span class="normal-price">' + currency_symbols[currency] + sprintf('%0.2f', normalPrice) + '</span>');
}

var promo_domain = '';
function choose_promo(domain) {
	if ($('tr.domain_tr', $('#selected_domains_table')).length == 0)
		return;

	var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(get_tld(domain)) != -1;
	if (debug)
		console.log('choose_promo(' + domain + ')');
		
	if (domain == '') return;
	
	var pr = get_promo();
	if (debug)
		console.log('pr: ' + pr);
	
	set_promo_price(domain, pr);
	if (debug)
		console.log('promo_domain: ' + promo_domain);

	if (promo_domain != '' && promo_domain != domain && $('#selected_' + promo_domain.replace(/\./g, '_'))[0])
		set_promo_price(promo_domain, 'none');

	if (debug)
		console.log('choose_promo() promo_domain changed to ' + domain);

	promo_domain = domain;
	
	updateDomainPrices();
	updateTotalPrices();
}

function select_owner_details() {
	if ($('#selected_owner_details')[0]) {
		$('#selected_owner_details').html(
			'<div class="split">' +
				($('#client_company_name').val() != '' ? '<div>' + $('#client_company_name').val() + 
				($('#client_vat_number').val() != '' ? ' (' + $('#client_vat_number').val() + ')' : '') + '</div>' : '') +
				'<div>' + $('#client_firstname').val() + ' ' + $('#client_lastname').val() + '</div>' +
				'<div>' + $('#client_email').val() + '</div>' +
				'<div>' + $('#client_phone').val() + '</div>' +
			'</div>' +
			'<div class="split">' +
				'<div>' + $('#client_address1').val() + '</div>' +
				($('#client_address2').val() != '' ? '<div>' + $('#client_address2').val() + '</div>' : '') +
				'<div>' + $('#client_city').val() + ', ' + 
				($('#client_country').val() == 'US' ? $('#client_state_us option:selected').text() : $('#client_state').val()) + ' ' + $('#client_zip').val() + '</div>' +
				'<div>' + $('#client_country option:selected').text() + '</div>' +
			'</div>'
		);
	//$('#add_owner_details').val('edit_owner_details');
	}
}

function vtip() {
	this.xOffset = -40; // x distance from mouse
	this.yOffset = 18; // y distance from mouse
	//this.xOffset = -10; // x distance from mouse
	//this.yOffset = 15; // y distance from mouse

	$("div.tip").unbind()
	.on('mouseenter', function() {
		this.t = this.title;
		this.title = '';
		this.offset = $(this).offset();

		$('body').append('<p id="vtip"><img id="vtipArrow" />' + this.t + '</p>');
		$('p#vtip #vtipArrow').attr("src", 'images/vtip_arrow.png');
		$('p#vtip').css("top", (this.offset.top + yOffset) + "px").css("left", (this.offset.left + xOffset) + "px").fadeIn("slow");
	})
	.on('mouseleave', function() {
		this.title = this.t;
		$("p#vtip").fadeOut("slow").remove();
	});
};

function toggle_host_domain(host_domains) {
	if (host_domains == 0) {
		if ($('#host_domain_option:visible')[0]) $('#register_domains_option a').click();
		$('#host_domain_option:visible').hide();
	} else {
		$('#host_domain_option:hidden').show();	
	}
}

function toggle_business(is_business) {
	var get_period_value = $('#products-block > .ui-widget-content > div:visible input[name^="period\["]:checked').val();
	get_tax(get_period_value);

	if (is_business == 1) {
		$('#vat_number').css('display', '');
	} else {
		$('#vat_number').css('display', 'none');
	}
}

function updateDomainPrices() {
	
	var tld, period;
	$('tr.domain_tr').each(function(){
		var tld = $(this).attr('tld');
		var domain = $(this).attr('id').replace(/selected\_/, '').replace(/\_/g, '.');
		
		var period = $('.domain_period', $(this)).val();

		var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(tld) != -1 && $(this).closest('table').attr('id') == 'selected_domains_table';
		
		if (!period)
			return;
		var action = $(this).find('input[name^=transfer]').size() ? 'transfer' : 'register';
		var plansTab = get_active_tab() == 'plans';
		
		var prefix = '[' + domain +'] updateDomainPrices() plansTab: ' + plansTab + '; ';
		
		var rd_price = promoPrice = get_tld_price('registerDomain', tld, action, period, currency);
		var normal_rd_price = normalPrice = get_tld_price('registerDomain', tld, 'prices', period, currency);

		var d_price = get_tld_price('domain', tld, action, period, currency);
		var normal_d_price = get_tld_price('domain', tld, 'prices', period, currency);
		
		if (rd_price && normal_d_price && d_price == false && rd_price < normal_d_price)
			normal_d_price = false;		

		var renewal_price = get_tld_renewal_price(tld);

		var firstPeriod = get_tld_first_period(tld);

		if (plansTab && (typeof promo_domain == 'undefined' || promo_domain == domain)) {
			if (is_tld_free(tld)) {
				promoPrice = period.replace(/period\_/, '') > 12 ? normal_rd_price - renewal_price : 0;
				if (debug)
					console.log(prefix + 'period: ' + period + '; promoPrice set to ' + promoPrice);
			} else if (d_price) {
				promoPrice = d_price;
				if (debug)
					console.log(prefix + 'promoPrice set to ' + promoPrice + ' (d_price)');
			} else if (normal_d_price && normal_d_price < normal_rd_price) {
				promoPrice = normal_d_price;
				if (debug)
					console.log(prefix + 'promoPrice set to ' + promoPrice + ' (normal_d_price)');
			}
			
			if (promoPrice !== false && $(this).find('.promo_td').css('visibility') == 'visible') {
				$(this).find('.promo_td').find('input.choose_promo').prop('checked', true)
				$(this).find('.promo_td').find('.promo_text').css('font-weight', 'bold');
			}
		}

		if (debug) {
			console.log(prefix + action + ' period: ' + period + '; promo_domain: ' + (typeof promo_domain != 'undefined' ? promo_domain : '') + ' d_price: ' + d_price + '; normal_d_price: ' + normal_d_price + '; rd_price: ' + rd_price + '; normal_rd_price: ' + normal_rd_price + '; renewal_price: ' + renewal_price + '; normalPrice: ' + normalPrice + '; promoPrice: ' + promoPrice);
			console.log(prefix + '.promo-price removed; attr-price && .tld-price set to ' + normalPrice);
		}

		$('.tld-price', $(this)).removeClass('promo-price').attr('price', normalPrice).html(currency_symbols[currency] + sprintf('%0.2f', normalPrice));
		
		if (promoPrice !== false && promoPrice < normalPrice) {
			if (debug)
				console.log(prefix + 'promoPrice set to ' + promoPrice);

			$('.tld-price', $(this)).attr('price', promoPrice).addClass('promo-price').html(currency_symbols[currency] + sprintf('%0.2f', promoPrice));
			
			var info_html_title = promo_price_renewal_tooltip.replace('@years@', domain_period_text(firstPeriod)).replace('@renewal_price@', currency_symbols[currency] + sprintf('%0.2f', renewal_price));
			
			if (!$('.tld-price', $(this)).find('.info').length)
				$('.tld-price', $(this)).append(' <span class="info tip"></span>');

			$('.tld-price', $(this)).find('.info').attr('title', info_html_title);
			
			if (!$('.tld-price', $(this)).find('.normal-price').length)
				$('.tld-price', $(this)).append('<span class="normal-price"></span>');
			
			if (debug)
				console.log(prefix + 'normalPrice set to ' + normalPrice);

			$('.tld-price', $(this)).find('.normal-price').html(currency_symbols[currency] + sprintf('%0.2f', normalPrice));
		}
				
		if (domainsInfo[tld]['id_protect'])
			$('.tld-price-id-protect', $(this))
				.attr('price', idpProduct.prices[period][currency])
				.html(sprintf(currency_symbols[currency] + '%0.2f', idpProduct.prices[period][currency]));
		
		var ssl_period = $('.ssl_period', $(this)).val();
		var ssl_type = $('.ssl_type', $(this)).val();
		if (ssl_period && ssl_type) {
			$('.tld-price-ssl-certificate', $(this))
				.attr('price', ssl_certificates[ssl_type]['prices'][ssl_period][currency])
				.html(sprintf(currency_symbols[currency] + '%0.2f', ssl_certificates[ssl_type]['prices'][ssl_period][currency]));
		}
	});
}

function select_upgrade(upgrade) {
	var price_el = upgrade.parent().next();

	if (upgrade.val() == '' || upgrade.val() == 0) {
		price_el.empty().attr('price', 0);
		return;
	}
	
	if (!upgrade.is(':visible')) {
		price_el.empty().attr('price', 0);
		return;
	}
	
	var plan = upgrade.attr('plan');
	var plan_id = $('select[name="plan[' + plan + ']"]').val();
	var upgrade_product = upgrade.attr('is_panel') ? upgrade.val() : upgrade.attr('upgrade');
	var dc = $('select[name="datacenter[' + plan + ']"]').val();

	var period = getPeriod($('#' + plan + '_settings')).val();

	if (dc == 'sis_group' && typeof upgrades_au[plan][plan_id][upgrade_product][1][currency] != 'undefined') {
		var amount = upgrades_au[plan][plan_id][upgrade_product][1][currency];
	} else {
		if (typeof upgrades[plan][plan_id] == 'undefined')
			return;
		var amount = upgrades[plan][plan_id][upgrade_product][1][currency];
	}

	//if (!isNaN(parseFloat(upgrade.val())) && parseFloat(upgrade.val()) !== upgrade.val()) {
	//	amount = amount * parseFloat(upgrade.val());
	//}

	if (!upgrade.attr('id').match(/_installation_troubleshooting/)) {
		amount = parseFloat(amount) * getPeriod($('#' + plan + '_settings')).val();
	}

	price_el.html('<span class="currency-symbol">' + currency_symbols[currency] + '</span>' + sprintf('%0.2f', amount) + ' <span class="currency">' + currency + '</span>').attr('price', amount);
}

function cancel_order() {
	$("a[product_type='" + order_params['product_type'] + "']").click();
	$("select.plans", panel).val(order_params['plan']).change();
	$("input.periods[value='" + order_params['period'] + "']", panel).click();
	$('input.currencies_' + order_params['currency'] + ':first', panel).click();
	
	$('#username').val(order_params['username']);
	$('#client_firstname').val(order_params['firstname']);
	$('#client_lastname').val(order_params['lastname']);
	$('#client_address1').val(order_params['address1']);
	$('#client_address2').val(order_params['address2']);
	$('#client_city').val(order_params['city']);
	$('#client_state').val(order_params['state']);
	$('#client_state_us').val(order_params['state_us']);
	$('#client_zip').val(order_params['zip']);
	$('#client_email').val(order_params['email']);
	$('#client_phone').val(order_params['phone']);
	$('#client_country').val(order_params['country']).change();
	if (order_params['eu'] == 1)
		$("input.is_business[value='" + order_params['is_business'] + "']").click();
	select_owner_details();

	if (order_params['product_type'] == 'vps' || order_params['product_type'] == 'dedicated') {
		$("input[name='" + order_params['product_type'] + "\\[hostname\\]']").val(order_params[order_params['product_type']]['hostname']);
		$("input[name='" + order_params['product_type'] + "\\[os\\]']").val(order_params[order_params['product_type']]['os']);
		$("input[name='" + order_params['product_type'] + "\\[rootpass\\]']").val(order_params[order_params['product_type']]['rootpass']);
		$("input[name='" + order_params['product_type'] + "_repass']").val(order_params[order_params['product_type']]['rootpass']);
		for (var k in order_params['upgrades'])
			$('#' + order_params['product_type'] + '_' + k).val(order_params['upgrades'][k]).change();
	}
	
	if (order_params['host_domain'] == 1) {
		$('host_domain_option a').click();
		$("input[name='hosted_domain']", $('#Host_Domain')).val(order_params['hosted_domain']);
	} else if (order_params['domains']) {

		var contacts = ['country', 'firstname', 'lastname', 'organizationname', 'jobtitle', 'address1', 'address2', 'city', 'stateprovince', 'stateprovince_us', 'postalcode', 'emailaddress', 'phone', 'fax'];

		for (i = 0; i < contacts.length; i++) {
			$('#registrant' + contacts[i]).val(order_params['registrant' + contacts[i]]);
			$('#admin' + contacts[i]).val(order_params['admin' + contacts[i]]);
			$('#tech' + contacts[i]).val(order_params['tech' + contacts[i]]);
			$('#billing' + contacts[i]).val(order_params['billing' + contacts[i]]);
		}

		$("input.copyadmin[value='" + order_params['copyadmin'] + "']", $('#whois_form')).click();
		$("input.copytech[value='" + order_params['copytech'] + "']", $('#whois_form')).click();
		$("input.copybilling[value='" + order_params['copybilling'] + "']", $('#whois_form')).click();

		var dom;
		for (var i in order_params['domains']) {
			dom = order_params['domains'][i];
			domainCheck(dom.substring(0, dom.indexOf('.')), dom.substring(dom.indexOf('.') + 1), function(dom){
				$("select[name='period\\[" + dom + "\\]']").val(order_params['domain_periods'][dom]).change();
				if (order_params['id_protect'] && order_params['id_protect'][dom])
					$("input[name='idp\\[" + dom + "\\]']").click();
				choose_domains();
				updateTotalPrices();
			});
		}
		/*
		for (dom in order_params['domain_periods']) {
			$("input[name='period\\[" + dom + "\\]']").val(order_params['domain_periods'][dom]).click();
		}
		if (order_params['id_protect']) {
			for (dom in order_params['id_protect']) {
				$("input[name='idp\\[" + dom + "\\]']").click();
			}
		}
		choose_domains();
		*/
		if (order_params['extra_attributes']) {
			for (dom in order_params['extra_attributes']) {
				extra_attributes[dom] = order_params['extra_attributes'][dom];
			}
		}
		if (order_params['promo'])
			$("input.choose_promo[value='" + order_params['promo'] + "']").click();
	}
	
	if (order_params['datacenter'])
		$('#' + order_params['product_type'] + '_datacenter').val(order_params['datacenter']);

	if (order_params['script_id'])
		$('#' + order_params['product_type'] + '_script_id').val(order_params['script_id']);

	if (order_params['cc_type'])
		$("input[name='cc_type']").filter("[value='" + order_params['cc_type'] + "']").click();

	$('#pm_' + order_params['payment_method']).click();
}

function phone_format(phone, code) {
	var phone_regex = /^\+\d{1,3}\.\d{4,12}$/;
	if (phone.match(phone_regex))
		return phone;

	phone = phone.replace(/[()]+/g, ' ');
	phone = $.trim(phone);
	if (phone == '')
		return '';

	phone = phone.replace(/^0{2,}/, '+');
	var country_code = phone.match(/^\+(\d{1,3})( |\-|\.)/);
	if (country_code !== null)
		code = country_code[1];

	phone = phone.replace(/^0+/, '');
	phone = phone.replace(/[^\d]+/g, '');
	var phone_match = new RegExp('^' + code + '(\\d+)$');
	phone_match = phone.match(phone_match);
	if (phone_match === null) {
		return '+' + code + '.' + phone;
	} else {
		return '+' + code + '.' + phone_match[1];
	}
}

var pre_domains_search = false;
function pre_domains(sld, tld) {
	$('#first_sld').val(sld);
	$('#first_tld').val(tld);

	//var searched = punycode.toASCII($.trim($('#first_sld').val()).toLowerCase().replace(/^www\./, '')).match(/([a-z0-9-]+)(?:\.([a-z0-9-.]+))?$/);
	
	var _searched = $.trim($('#first_sld').val() + '.' + $('#first_tld').val()).toLowerCase().replace(/^www\./, '');
	var searched = (window.punycode ? punycode.toASCII(_searched) : _searched).match(/([a-z0-9-]+)(?:\.([a-z0-9-.]+))?$/);
	
	if (searched && searched[1]) {
		$('#first_sld').val((window.punycode ? punycode.toUnicode(searched[1]) : searched[1]));
		if (searched[2]) {
			$('#first_tld').val(searched[2]).change();
		}
		
		if ($('#first_sld').val() + '.' + $('#first_tld').val() != $('#sld').val()) {
			pre_domains_search = true;
			var _domain = $('#first_sld').val() + '.' + $('#first_tld').val();
			
			$('#sld').val(window.punycode ? punycode.toUnicode(_domain) : _domain);
			$('#domain_search_form').submit();
			//setTimeout('choose_domains();', 2000);
		}
	}
}

function getQueryParams(qs) {
	if (typeof qs == 'undefined') qs = location.search
    qs = qs.split('+').join(' ');

    var params = {},
        tokens,
        re = /[?&]?([^=]+)=([^&]*)/g;

    while (tokens = re.exec(qs)) {
        params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
    }

    return params;
}

var panel = null;
var extra_attributes = {};
//var upgrades = {};

create_dialog($('#domain_dialog'));
var features_buttons = {};
features_buttons[close_msg] = function() { $(this).dialog('close') };
create_dialog($('#features_dialog'), { 'buttons' : features_buttons });
create_dialog($('#terms_dialog'), { 'buttons' : features_buttons, 'width' : 900, 'height' : 600 });

//kriss
	function set_the_tax() {
		var tax_text = $('#payment-block span.selected a').data('tax-text') || '';
		if (typeof tax_text === 'undefined' || tax_text == '') {
			$('#the_amount, #the_tax').hide();
		} else {
			$('#the_amount, #the_tax').show();
		}

		var tax = $('#payment-block span.selected a').data('tax');

		if (tax_text == '') {
			$('#tax_name').parent().parent().hide();
			$('#amount').parent().hide();
		} else {
			if (tax > 0) {
				$('#tax_name').html(tax_text);
				$('#vat_percent').html(tax + '%');
				$('#tax_name').parent().parent().show();
				$('#amount').parent().show();
			}
		}
		/*
		if ($('input#tax_group').val() == 'us' 
				&& $('#client_country option:selected').val() == 'US' 
				&& $('#client_state_us option:selected').val() == 'FL') {
			$('#tax_name').html($('#client_country option:selected').attr('us_human'));
		} else if ($('input#tax_group').val() == 'uk' && $('#client_country option:selected').attr('euro') == 1) {
			if ($('input.is_business:checked').val() == 1) {
				$('#tax_name').html($('#client_country option:selected').attr('uk_business_human'));
			} else {
				$('#tax_name').html($('#client_country option:selected').attr('uk_not_business_human'));
			}
		}*/
	}

$(function(){

	dedicated_servers_assembly_delay = [60554, 60557];

	old_sld = ($('[name="sld"]').length ? $('[name="sld"]').val() : '');
	 
	$('[name="sld"], #first_sld').on('keyup', function() {
		if (old_sld != $(this).val()) {
			if ($(this).val() == '') {
				old_sld = '';
				return;
			}
			
			var new_sld = domain_search_get_sld($(this).val());
			$(this).val(window.punycode ? punycode.toUnicode(new_sld) : new_sld);
		}
		if (window.langtag_required_func)
			langtag_required_func();
	});		

	set_the_tax();
	
	$('#domain_dialog').dialog({ 'open' : function(event, ui){
		// reset first_promo_domain if set
		if (get_first_promo_domain())
			first_promo_domain = '';
		var _searched = $.trim($('#first_sld').val()).toLowerCase().replace(/^www\./, '');
		var searched = (window.punycode ? punycode.toASCII(_searched) : _searched).match(/([a-z0-9-]+)(?:\.([a-z0-9-.]+))?$/);

		if (searched && searched[1]) {
			$('#first_sld').val(window.punycode ? punycode.toUnicode(searched[1]) : searched[1]);
			if (searched[2]) {
				$('#first_tld').val(window.punycode ? punycode.toUnicode(searched[2]) : searched[2]);
			}
			
			//if ($('#first_sld').val() + '.' + $('#first_tld').val() == $('#sld').val()) return;
			
			var _domain = $('#first_sld').val() + '.' + $('#first_tld').val();
			$('#sld').val(window.punycode ? punycode.toUnicode(_domain) : _domain);
			//$('#sld').val(punycode.toUnicode($('#first_sld').val()));
			$('#domain_search_form').submit();
		}
	} });

	if ($('#owner_dialog')[0]) {
		var owner_buttons = {};
		owner_buttons[close_msg] = function() { $(this).dialog('close') };
		owner_buttons[continue_msg] = function() {
			var thiz = $(this);
			do_proceed = function() {
				if (!validate_all($('#owner_form'))) {
					//showMsg('error', invalid_owner_details_msg);
					return;
				}

				select_owner_details();
				thiz.dialog('close');
			}

			if (validate_count == 0) {
				do_proceed();
				do_proceed = undefined;
			}
		};
		create_dialog($('#owner_dialog'), { 'width' : 600, 'buttons' : owner_buttons });

		$('#add_owner_details').click(function(){
			$('#owner_dialog').dialog('open');
		});
	}

	vtip();

	$(document).on('click','div.tabs-new span a', function(e) {
		var id = $(this).attr('href').replace('#', '');
		var tabs_head = $(this).parent().parent();
		var tabs_content = tabs_head.next();

		tabs_head.children().removeClass('ui-corner-top ui-widget-content selected');
		$(this).parent().addClass('clickedTab ui-corner-top ui-widget-content selected');
		tabs_content.children().css('display', 'none');

		if ($(this).attr('load')) { // ajax load
			//if (!$('#' + id)[0]) // if container is missing add it
			//	tabs_content.append('<div id="' + id + '"></div>');
		//	if ($('#' + id).text() == '') // if container is empty load
		//		loadSafe($('#' + id), $(this).attr('load'));
		}

		$('#' + id).css('display', '');
		
		if ($.inArray(id, ['Plans', 'Semi_Dedicated']) > -1) {
			$('#trial-info section').hide();
			$('#trial-info section.' + id.toLowerCase().replace('_', '-')).show();
			$('#trial-info').show();
		} else
			$('#trial-info').hide();

		// set price for selected upgrades
		$('select.upgrades:visible').each(function() { 
			if ($(this).val() != '') 
				select_upgrade($(this)); 
		});
		
		vps_warnings();
		e.preventDefault();
		
		if (id == 'Dedicated') {
			show_hide_dedicated_storage_config();
			set_dedicated_stock();
		}
			
		check_dc_requirements();
	});

	$('a[product_type]', $('#products-block')).click(function() {
		panel = $($(this).attr('href'));
		$('#product_type').val($(this).attr('product_type'));
		toggle_host_domain($(this).attr('host_domains'));
		
		if ($(this).attr('href') == '#Dedicated') {
			$('.assembly_time_note').hide();
			if ($.inArray(parseInt($(this).val()), dedicated_servers_assembly_delay) > -1)
				$('.assembly_time_note').show();
		}
		
		if (panel.text() != '') {
			choose_promo(promo_domain);
			updateTotalPrices();
			$('#first_tld').change();
		}
		
		if (panel.attr('id') == 'SSL') {
			$('#SSL select[name="ssl_data[period]"]').trigger('change');
		}
		
		if (!$('#promo_code').is(':visible'))
			$('#promo_code').show();
		else if ($(this).attr('product_type') == 'dedicated') {
			setTimeout(function() {
				panel.find('input[name^="period[dedicated]"]:visible:checked').trigger('click');
			}, 500);
		}
	});

	$('#register_domains_option,#host_domain_option').click(function(){
		if ($(this).attr('id') == 'host_domain_option') {
			$('#host_domain').val(1);
		} else {
			$('#host_domain').val(0);
		}
	});
	
	$('select[name^="datacenter"]').on('change', function() {
		vps_warnings();
		
		check_dc_requirements();

		set_dedicated_stock();
		
		// recalculate prices for dc
		$('select.upgrades:visible').each(function() { 
			if ($(this).val() != '') {
				select_upgrade($(this)); 
				updateTotalPrices();
			}
		});

		if ($(this).val() == 'sis_group') {
			$('label[for="personal_data_agree"], input[name="personal_data_agree"]').show();
		} else {
			$('label[for="personal_data_agree"], input[name="personal_data_agree"]').hide();
		}
		
	});

	$('select[name^="ssl_data"]').on('change', function() {
		updateTotalPrices();
	});

	$('select.plans').on('change', function(e) {
		checked_period = $('#periods_' + $(this).val() + ' input:checked').val();
		$('div.features:visible', panel).css('display', 'none');
		$('#features_' + $(this).val()).css('display', '');

		show_hide_dedicated_storage_config();

		$('div.prices:visible', panel).css('display', 'none');
		
		$('#price_' + $(this).val() + '_' + checked_period).css('display', '');
		$('div[id^="period"]:visible', panel).css('display', 'none');
		$('#periods_' +$(this).val()).css('display', '');

		$('select.upgrades:visible').each(function(){ 
			select_upgrade($(this)); 
		});

		choose_promo(promo_domain);
		updateTotalPrices();
		$('#first_tld').change();
		check_upgrades();
		check_dc_requirements();
	});
	
	$('input[name="promo_code"]').on('change', function() {
		updateTotalPrices();
	}).on('keypress', function(e) {
		if (e.keyCode == 13) {
			e.preventDefault();
			$(this).blur();
		}
	});	

	function check_dc_requirements() {

		plan = $('[name^="plan"]', panel);
		dc = $('[name^="datacenter"]', panel);

		if (dc.val() == 'london') {
			$('.uk-dc-notice').show();
		} else {
			$('.uk-dc-notice').hide();
		}

		switch(panel.attr('id')) {
			case "Semi_Dedicated":
			case "Plans":
				ip = plan.find('option[value="' + plan.val() + '"]').attr('ip');
				// check dc against plan ip
				if (ip > 0 && dc.val() == 'sis_group') {
					$('.au-ip-notice', panel).show();
				} else {
					$('.au-ip-notice', panel).hide();
				}
			break;
			default:
				$('.au-ip-notice').hide();
		}
	}

	function check_upgrades(selected_panel) {
		if (!selected_panel)
			selected_panel = panel;

		var option = $('select.plans option:selected', selected_panel);
		for (var i = 0; i < plan_included_upgrades.length; i++) {
			if (plan_included_upgrades[i] != 'control_panel') {
				if (option.attr(plan_included_upgrades[i])) {
					$('div.' + plan_included_upgrades[i] + '_block', selected_panel).hide();
					$('select.' + plan_included_upgrades[i], selected_panel).val('').parent().next().empty().attr('price', 0);
				} else {
					$('div.' + plan_included_upgrades[i] + '_block', selected_panel).show();
				}
			}
		}
		
		// hide backup if managed services is selected
		if (selected_panel.find('select[name$="\[managed_services\]"]').val() != '') {
			selected_panel.find('select[name$="\[backup\]"]').parent().parent().hide();
		}
	}

	$('input.periods').on('click', function() {
		$('div.prices:visible', panel).css('display', 'none');
		$('#price_' + $('select.plans', panel).val() + '_' + $(this).val()).css('display', '');

		$('label', $(this).closest('div')).css('font-weight', 'normal');
		$(this).closest('label').css('font-weight', 'bold');

		//$('select.upgrades').each(function(){ select_upgrade($(this)); });

		$('select.upgrades:visible').each(function() { 
			if ($(this).val() != '') {
				select_upgrade($(this)); 
			}
		});
		
		$('select[name^="storage_configuration"]:visible').change();

		// check for dedicated tab clicked and period 1
		if (panel.attr('id') == 'Dedicated') {
			if ($(this).val() > 1) {
				$('#promo_code input[name="promo_code"]').val('');
				$('#promo_code').hide();
			} else
				$('#promo_code').show();
		}

		choose_promo(promo_domain);
		updateTotalPrices();
		
		$('#first_tld').change();
	});

	$('input.currencies').on('click', function(){
		currency = $(this).val();
		
		period = getPeriod().val();
		if ($("input.periods:visible:checked").length)
			if ($("input.periods:visible:checked").val() > 1)
				period = $("input.periods:visible:checked").val();

		$('input.currencies', $('#products-block')).parent().css('font-weight', 'normal');
		$('input.currencies_' + currency, $('#products-block')).prop('checked', true).parent().css('font-weight', 'bold');
		$(this).parents('.prices').find('.full_price').html(currency_symbols[currency] + parseFloat($(this).attr('full-price') / period).toFixed(2));
		
		$('select.upgrades:visible').each(function() { 
			if ($(this).val() != '') {
				select_upgrade($(this)); 
			}
		});		
		
		$('select[name^="storage_configuration"]:visible').change();

		updateDomainPrices();
		choose_promo(promo_domain);
		updateTotalPrices();
		$('#first_tld').change();
	});
	//setTimeout("$('input.currencies:checked:first').click()", 1);

	$('select.managed_services').on('change', function(){
		if ($(this).val() == 1) {
			$('select.backup', panel).val('').parent().next().empty().attr('price', 0);
			$('div.backup_block', panel).css('display', 'none');
		} else {
			$('div.backup_block', panel).css('display', '');
		}
	});

	$('select.upgrades').on('change', function(e) {
		select_upgrade($(this));
		updateTotalPrices();
	});
	
	$('#first_tld').change(function() {
		var opt = getSelectedPlanOption();
		var curr = getCurrency().val();
		var plan_period = getPeriod().val();
		var tld = $(this).val();
		var promo = get_promo();
		var plansTab = get_active_tab() == 'plans';
		
		var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(tld) != -1;
		var prefix = '[' + tld + '] first tld changed promo: ' + promo + '; plansTab: ' + plansTab + '; ';
		
		// remove first_free_tld used in domain_check dialog
		if (typeof first_free_tld != 'undefined')
			first_free_tld = undefined;
		
		$('#tld_price').removeClass('promo-price').html('');

		var first_period = get_tld_first_period(tld);
		
		var human_period = parseInt(first_period.replace('period_', '')) / 12;
		human_period = human_period + ' ' + (human_period == 1 ? year_msg : years_msg);

		var normal_rd_price = normalPrice = get_tld_price('registerDomain', tld, 'prices', first_period, curr);
		var rd_price = promoPrice = get_tld_price('registerDomain', tld, 'register', first_period, curr);

		var normal_d_price = get_tld_price('domain', tld, 'prices', first_period, curr);
		var d_price = get_tld_price('domain', tld, 'register', first_period, curr);
		
		if (rd_price && normal_d_price && d_price == false && rd_price < normal_d_price)
			normal_d_price = false;

		$('#tld_price').removeClass('promo-price').html(currency_symbols[curr] + sprintf('%0.2f', normal_rd_price) + ' / ' + human_period);
		$('div.plan_promo_domain').css('visibility', 'hidden');

		if (debug)
			console.log(prefix + 'normal_rd_price: ' + normal_rd_price + '; rd_price: ' + rd_price + '; normal_d_price: ' + normal_d_price + '; d_price: ' + d_price + '; is_free_tld: ' + is_tld_free(tld));

		if (!normal_rd_price) {
			if (debug)
				console.log(prefix + ' no registerDomain price');
			return;
		}

		if ((typeof promo_domain == 'undefined' || promo_domain == '') && plansTab && normal_d_price && plan_period > 1) {
			if (opt.attr('free') == '1' && is_tld_free(tld)) {
				if (debug)
					console.log(prefix + 'one free domain');

				$('#tld_price')
					.addClass('promo-price')
					.html('<span class="normal-price">' + $('#tld_price').html() + '</span> (1 ' + free_domain_registration_msg + ')');
				$('div.plan_promo_domain').css('visibility', 'visible');
				return;
			}
			
			var promoPrice = d_price && d_price < normal_d_price ? d_price : normal_d_price;
			
			if (promoPrice >= normalPrice)
				return;

			var info_tooltip = normal_d_price && !d_price ? promo_special_price_renewal_tooltip : promo_domain_asterix_msg;

			if (debug)
				console.log(tld + ' ' + (normal_d_price && !d_price ? 'special' : 'promo') + ' price ' + info_tooltip);

			$('#tld_price')
				.addClass('promo-price')
				.html('<span class="normal-price">' + $('#tld_price').html())
				.append(' (1 ' + promo_domain_registration_msg + ' @ ' + currency_symbols[curr] + sprintf('%0.2f', promoPrice) + ')')
				.append(' <span class="info tip" title="' + info_tooltip + '"></span>');
		} else if (rd_price && rd_price < normalPrice) {
			if (debug)
				console.log(prefix + 'lower rd_price');
			
			if (debug)
				console.log(tld + ' promo price ' + promo_domain_asterix_msg);
			
			$('#tld_price')
				.addClass('promo-price')
				.html('<span class="normal-price">' + $('#tld_price').html() + '</span> ')
				.append(' (' + promo_domain_registration_msg + ' @ ' + currency_symbols[curr] + sprintf('%0.2f', rd_price) + ')')
				.append(' <span class="info tip" title="' + promo_domain_asterix_msg + '"></span>')
		}
	});

	$('a.show_all_features').on('click', function(){
		loadSafe($('#features_dialog'), 'ajax.php?action=features&product_type=' + $('#product_type').val() + '&plan_id=' + $('select.plans', panel).val() + '&currency=' + getCurrency().val(), function(){
			$('#features_dialog').dialog('open');	
		});
		return false;	
	});

	function refresh_panels(obj, plan, plan_id) {
		var panels = $(obj).find('option:selected').attr('panels').split(';');
		if (['vps', 'solusvm', 'kvm'].indexOf(plan) !== -1 && !$('[name="plan[' + plan + ']"] option:selected').attr('control_panel'))
			panels = ['None'];

		var html = '';
		$.each(panels, function(k, v) {
			var panel = v == 'None' ? '' : v;
			var name = v == 0 || v == 'None' ? 'None' : upgrades[plan][plan_id][v]['name'];
			var selected = name == 'Hepsia' ? ' selected="1"' : '';
			html += `<option value="${panel}" ${selected}>${name}</option>`;
		});

		$('#' + plan + '_panel').html(html);
		$('#' + plan + '_panel').change();
	}

	$('select.os_options').on('change', function(){
		var obj = $(this)[0];
		var plan = $(this).attr('plan');
		var plan_id = $('select[name="plan[' + plan + ']"]').val();
		refresh_panels(obj, plan, plan_id);
	});

	$('input.valid-phone').on('change', function(){
		var country_id = $(this).attr('id').replace('phone', 'country').replace('fax', 'country');
		$(this).val(phone_format($(this).val(), $('#' + country_id + ' option:selected').attr('phone_code')));
	});

	$('body').on('change', 'input.validate,select.validate', function(){
		validate_input($(this));
	});
	
	$('#domain_dialog').on('dialogclose', function(event) {
		// remove first_promo_tld if set
		if (typeof first_promo_tld != 'undefined')
			first_promo_tld = undefined;
			
		// clear free and promo-tld in search
		if ($('[data-free-tld="true"]').length) {
			$('[data-free-tld="true"]').data('free-tld', undefined);
			$('[data-free-tld="true"]').removeAttr('data-free-tld');
		}

		if ($('[data-promo-tld="true"]').length) {
			$('[data-promo-tld="true"]').data('promo-tld', undefined);		
			$('[data-free-tld="true"]').removeAttr('data-promo-tld');
		}
	});

	$('#domain_check').click(function() {
		if ($('#domain_dialog').text() == '') {
			loadSafe($('#domain_dialog'), 'ajax.php?action=domain_check_box', function(){
				$('#domain_dialog').dialog('open');
				owner_details(); //if owner details & no whois details, set
			});
		} else {
			back_action();
			$('#domain_dialog').dialog('open');
			owner_details(); //if owner details & no whois details, set
		}
	});

	$('#first_sld').keydown(function(e){
		if (e.keyCode == 13) {
			e.preventDefault();
			$(this).trigger('blur');
			return false;
		}
	});

	$('#first_sld, #first_tld').on('focusout', function(e) {
		if (
			$('#first_sld').val() != '' 
			&& (
				e.relatedTarget == null 
				|| $.inArray($(e.relatedTarget).attr('id'), ['first_sld', 'first_tld', 'domain_check']) == -1
			)
		) {
			$('#domain_check').click();
		}
	});

	$('#client_country, #client_state_us, #client_state, [name^="plan\["], [name^="period\["]').on('change', function() {
		var get_period_value = $('#products-block > .ui-widget-content > div:visible input[name^="period\["]:checked').val();
		get_payment_methods_new(get_period_value);
		get_tax(get_period_value);
		
		if ($(this).attr('name') == 'plan[dedicated]') {
			$('.assembly_time_note').hide();
			if ($.inArray(parseInt($(this).val()), dedicated_servers_assembly_delay) > -1)
				$('.assembly_time_note').show();
		}
		
		$('select.upgrades:visible').each(function() { 
			if ($(this).val() != '') 
				select_upgrade($(this)); 
		});		
		
		// trigger change for dedicated disk
		if ($('select[name^="storage_configuration"]:visible').length) {
			$.each($('select[name^="storage_configuration"]:visible'), function() {
				$(this).change();
			});
		}
	});
	$('a[id^="A_"]').on('click', function() {
		var get_period_value = $('input[name^="period\[' + $(this).attr('product_type') + '\]"]:checked').val();
		get_tax(get_period_value);
	});
	$('table#selected_domains_table').on('click', 'td.remove_selected_domain', function() {
		var get_period_value = $('#products-block > .ui-widget-content > div:visible input[name^="period\["]:checked').val();
		get_tax(get_period_value)
	});

	$('#client_country').change(function(){

		var selected_option = $(this)[0].options[$(this)[0].selectedIndex];
		if (selected_option.value == 'US') {
			$('#client_state_div').css('display', 'none');
			$('#client_state_us_div').css('display', '');
		} else {
			$('#client_state_us_div').css('display', 'none');
			$('#client_state_div').css('display', '');
		}
		if (selected_option.getAttribute('euro') == 1) { //kriss.uglyfix
			if ($('input[name="account_owner_details"]:checked')) {
				$('div.hide_details').css('display', ''); //new
			} else {
				$('div.hide_details').css('display', 'none'); //new
			}
			//$('input.is_business').attr('checked', false); //new
			$('#business_options').css('display', ''); //new
			//toggle_business($('input.is_business:checked').val()); //old
			$('#eu').val(1);
			$('#euro_amounts').css('display', '');
		} else {
			$('div.hide_details').css('display', ''); //new
			$('#business_options').css('display', 'none');
			$('#vat_number').css('display', 'none');
			$('#eu').val(0);
			$('#euro_amounts').css('display', 'none');
		}
		updateTotalPrices();
	});



	$('input.is_business').on('click', function(){
		$('div.hide_details:hidden').css('display', '') //new;
		toggle_business($(this).val());
		updateTotalPrices();

		//var get_period_value = $('#products-block > .ui-widget-content > div:visible input[name^="period\["]:checked').val();
		var get_period_value = getPeriod().val();

		get_payment_methods_new(get_period_value);
		get_tax(get_period_value);
	});

	$('#orderForm').submit(function(e) {
		e.preventDefault();
		var thiz = $(this);
		
		do_proceed = function() {
			if (pre_domains_search)
				owner_details();

			if (!validate_all(thiz)) {
				//showMsg('error', invalid_owner_details_msg);
				return;
			}

			if (!$('#redirect_payment_div')[0])
				$(document.body).append('<div id="redirect_payment_div" style="display:none"></div>');

			var extra_params = '';
			for (var i in extra_attributes)
				extra_params = extra_params + '&' + extra_attributes[i];
			
			//loadSafe($('#redirect_payment_div'), 'ajax.php?action=submit_order', thiz.serialize() + '&' + $('#whois_form').serialize() + '&' + $('#owner_form').serialize() + extra_params);
			var plan_params = $("input[type='hidden'],input:visible,select:visible", thiz).serialize();

			// check for promo_id
			if ($('input.periods:visible:checked').hasClass('promo-period') && $('input.periods:visible:checked').data('promo-id') !== undefined)
				plan_params += '&promo_id=' + $('input.periods:visible:checked').data('promo-id') + '';
			
			loadSafe($('#redirect_payment_div'), 'ajax.php?action=submit_order', plan_params + '&' + $('#whois_form').serialize() + '&' + $('#owner_form').serialize() + extra_params);
		}

		if (validate_count == 0) {
			do_proceed();
			do_proceed = undefined;
		}

		return false;
	});

	$('a.terms').click(function(){
		var product_type_to_terms = { 'domain_names': 'hosting', 'plans': 'hosting', 'semi_dedicated': 'semi', 'vps': 'vps', 'solusvm' : 'vps', 'dedicated': 'dedicated' };
		var product_type_terms = product_type_to_terms[$('#product_type').val()];

		if ($('#terms_dialog').text() == '') {
			var thiz = $(this);
			loadSafe($('#terms_dialog'), 'ajax.php?action=terms', null, function(){
				$('#terms_dialog').dialog('open');
				var s = thiz.attr('s');
				var l = thiz.attr('l');
				if (s.substring(0, 1) == '_') {
					s = product_type_terms + s;
					l = product_type_terms + l;
				}
				$('#' + s).click();
				$('#' + l).click();
			});
		} else {
			$('#terms_dialog').dialog('open');
			var s = $(this).attr('s');
			var l = $(this).attr('l');
			if (s.substring(0, 1) == '_') {
				s = product_type_terms + s;
				l = product_type_terms + l;
			}
			$('#' + s).click();
			$('#' + l).click();
		}
		return false;
	});

	$('#payment-block').on('click', 'input.choose_pm,a.choose_pm', function(){
		if ($(this).attr('pm')) {
			$('#payment_method').val($(this).attr('pm'));
			$('#tax_group').val($(this).attr('tax_group'));
			if (this.nodeName.toLowerCase() == 'input') {
				$('#all_pm > div:visible').hide();
				$('#' + $(this).attr('pm')).show();
			}
		}
		$('label', $(this).closest('div')).css('font-weight', 'normal');
		$(this).closest('label').css('font-weight', 'bold');
	});
//kriss
	$(document).on('click', 'a.choose_pm', function(){
		if (!$('input[name=account_owner_details]').is(':checked')) {
			if ($('#client_country option:selected').attr('euro') == 1) {
				//$('div.hide_details').css('display', 'none'); //new
				//$('input.is_business').attr('checked', false); //new
				$('#business_options').css('display', ''); //new
				$('[name="is_business"]:selected').trigger('click');
			} else {
				//$('div.hide_details').css('display', ''); //new
				$('#business_options').css('display', 'none');
				$('#vat_number').css('display', 'none');
			}
		}
		toggle_business($('input[name="is_business"]:checked').val());
		updateTotalPrices();
		set_the_tax();
	});
	$(document).on('change', '#client_country', function(){
			updateTotalPrices();
			set_the_tax();
			});
	$(document).on('change', '#client_state_us', function(){
			updateTotalPrices();
			set_the_tax();
			});

	$('#email_notification_settings input').click(function(){
		$('label', $(this).closest('div')).css('font-weight', 'normal');
		$(this).closest('label').css('font-weight', 'bold');		
	}).first().click();

	$('input.currencies:checked:first').click();

	//choose tab
	var hash = get_hash();
	var allowed_hash_keys = [];
	$.each($('#products-block .tabs-new a'), function() {
		allowed_hash_keys.push($(this).text());	
		if ($.inArray($(this).attr('href').substring(1), allowed_hash_keys) < 0)
			allowed_hash_keys.push($(this).attr('href').substring(1));
	});
	
	if (Object.keys(hash).length >= 1) {
		var _hash = (Object.keys(hash).length == 1) ? hash[0] : hash[1];
		if ($.inArray(_hash, allowed_hash_keys) >= 0)
			$("a[href='#" + _hash + "']", $('#products-block')).click(); //from url
		else
			$("a[product_type]:first", $('#products-block')).click(); //default
	} else {
		$("a[product_type]:first", $('#products-block')).click(); //default
	}
   
	//choose plan
	if (hash.length > 2 && hash[2].length > 0) {
		var option = $("option[plan_name='" + hash[2] + "']", $('select.plans', panel));
		if (option[0]) {
			option.attr('selected', true);
			$('select.plans', panel).change();
			//setTimeout("$('select.plans', panel).change()", 1);
		}
	}

	//search domain
	if (hash.length > 4 && hash[4].length > 0)
		$('#first_tld').val(hash[4]);

	if (hash.length > 3 && hash[3].length > 0) {
		$('#first_sld').val(window.punycode ? punycode.toUnicode(hash[3]) : hash[3]);
		//$('#domain_check').click();
		setTimeout("$('#domain_check').click();", 1);
	}

	//comming from remote domain search form
	if (domain_search_params.plan != '') {
		var sel = $("select.plans option[value='" + domain_search_params.plan + "']").parent();
		$('#A_' + sel.parent().parent().parent().attr('id')).click();
		sel.val(domain_search_params.plan).change();
	}

	if (domain_search_params.stop_search == '' || domainsInfo[domain_search_params.tld]['extra_attributes']) {
		if (domain_search_params.tld != '') $('#first_tld').val(domain_search_params.tld).change();
		if (domain_search_params.sld != '') {
			$('#first_sld').val(window.punycode ? punycode.toUnicode(domain_search_params.sld) : domain_search_params.sld);
			//$('#domain_check').click();
			setTimeout("$('#domain_check').click();", 1);
		}
	} else {
		if (domain_search_params.tld && domain_search_params.sld)
			pre_domains(domain_search_params.sld, domain_search_params.tld);
	}

	if (domain_search_params.country != '')
		$('#client_country').val(domain_search_params.country.match(/[A-Z]{2}/)).change();
	if (domain_search_params.script != '')
		$('#plans_script_id').val(domain_search_params.script);

	if (domain_search_params.ssl != '') {
		if ($('select[name="ssl_data[type]"] option[data-product-id="' + domain_search_params.ssl + '"]').length) {
			$('select[name="ssl_data[type]"] option[data-product-id="' + domain_search_params.ssl + '"]').prop('selected', true);
			$('a[href="#SSL"]').trigger('click');
		}
	}
	
	if ($('#products-block .tabs-new span.selected a').attr('product_type') == 'dedicated')
		show_hide_dedicated_storage_config();
	
	check_upgrades($('#VPS'));
	check_upgrades($('#SOLUSVM'));
	check_upgrades($('#KVM'));

	$('select[name^="raid"]').on('change', function() {
		var new_val = $(this).val();
		var option = $(this).find('option[value="' + new_val + '"]');
		if (option.data('no-redundancy') == '1') {
			$('.raid_no_redundancy_notification').show();
		} else {
			$('.raid_no_redundancy_notification').hide();
		}
	});
		
	$('select[name^="storage_configuration"]').on('change', function() {
		var new_val = $(this).val();
	
		// check if default disk
		/*
		if ($(this).data('disk-type') == 'default') {
			var current_slot = $(this).prop('name');
			
			$.each($(this).parent().parent().find('select[data-disk-type="default"]'), function(k, v) {
				if ($(this).prop('name') != current_slot) {
					if ($(this).val() != new_val)
						$(this).val(new_val).trigger('change');
				}
			});
		}
		*/
		
		var price_element = $(this).parent().find('.slot-price');
		var default_disk = $(this).find('option[data-price="0"]');
		var default_disk_price = default_disk.data('default-price-' + currency.toLowerCase()) || 0;

		var period = getPeriod().val();
		
		if ($(this).find('option[value="' + new_val + '"]').length) {
			var new_disk_price = $(this).find('option[value="' + new_val + '"]').data('price-' + currency.toLowerCase());
			if (typeof new_disk_price != 'undefined' && new_disk_price > 0) {
				var price = (new_disk_price - default_disk_price) * period;
				price_element.html(' + ' + sprintf(currency_symbols[currency] + '%0.2f ' + currency, (Math.round((price) * 100) / 100)));
			} else {
				price_element.html('');
			}
			
			updateTotalPrices();
		}
		
		dedicated_raid_options();
	});
	
	if (location.search != '') {
		// get parameters
		var params = getQueryParams();
		if (typeof params['period'] != 'undefined') {
			// set period
			$('input[name^="period"][value="' + params['period'] + '"]:visible').click();
		}
	}
});
/*$(function() {
var values = {};
	$('.remember').each(function() {
		$(this).change(function() {
			$.post('ajax.php?action=remember&name=' + $(this).attr('name') + '&value=' + $(this).val(), function() {});
		});
	});
});*/

/* kriss */
if ($('input[name="account_owner_details"]').length > 0) {
	$('#the_details').css({'visibility' : 'hidden', 'position' : 'absolute'});
}
$('input[name="account_owner_details"]').on('change', function() {
	if ($(this).is(':checked')) {
		$('#the_details').css({'visibility' : 'hidden', 'position' : 'absolute'});
		if (typeof fill_the_details != 'undefined') {
			fill_the_details();
		}
	} else {
		$('#the_details').css({'visibility' : 'visible', 'position' : 'relative'});
		//$('#the_details input[type="text"]').val('');
	}
	updateTotalPrices();
	set_the_tax();
});
/* kriss */


$(function() {	

	$(".remp span").click(function() {
		parentSpan = $(this).closest("span");
		parentSpan.hasClass("selected")?(
			parentSpan.removeClass("selected")
		):(
			parentSpan.addClass("clickedTab"), 
			parentSpan.siblings().removeClass("clickedTab")
		)
		//$.post('ajax.php?action=remember&name=pmethod' + '&value=' + $('.remp span.clickedTab a').attr('pm'), function() {});
	});

	var ptab = $('#payment-block').attr('pmethod');
	$(".tabs-new a[pm=" + ptab +"]").trigger('click');

	$(".tabs-new span").click(function() {
		parentSpan = $(this).closest("span");
		parentSpan.hasClass("selected")?(
			parentSpan.removeClass("selected")
		):(
			parentSpan.addClass("clickedTab"), 
			parentSpan.siblings().removeClass("clickedTab")
		)
		//$.post('ajax.php?action=remember&name=currTab' + '&value=' + $('.tabs-new span.clickedTab a').attr('id'), function() {});
	});

	var tab = $('#products-block').attr('currTab');
	$(".tabs-new a#" + tab).trigger('click');

	var bc = $('#business_options').attr('bc');
	var currCountry = $('#client_country').attr('currCountry');

	if (bc == 1 && $('#client_country option:selected').attr("euro") == 1)
		$('#vat_number').show();

	if (bc == 0) {
		$('input[name="vat_number"], input[name="company_name"]').removeAttr('value');
		$('#vat_number').hide();
	}

	if ($('#client_country option:selected').attr("euro") == 1)
		$('#business_options').show();

	if (currCountry == 'US') {
		$('#client_state_us_div').show();
		$('#client_state_div').hide();
	} else {
		$('#client_state_us_div').hide();
		$('#client_state_div').show();
	}

	$('body').on('change', 'select[name="raid"]', function() {
		if ($(this).data('changed') !== undefined)
			return;

		$(this).data('changed', true);
	});
});

$(document).ready(function() {
	var get_period_value = $('#products-block > .ui-widget-content > div:visible input[name^="period\["]:checked').val();
	get_payment_methods_new(get_period_value);
	//	console.log(get_period_value);
	get_tax(get_period_value);
	var ms_dedicated = $('#Dedicated .managed_services_block').attr('ms');
	var ms = $('#VPS .managed_services_block').attr('ms');
	var ms_solus = $('#SOLUSVM .managed_services_block').attr('ms');

	if(ms == 1)
		$('#VPS .backup_block').addClass('hidden');

	$('#vps_backup').val('');
	$('#vps_managed_services').change(function() {
    	if($(this).val() == '')
        	$('#VPS .backup_block').removeClass('hidden');
	});

	if(ms_solus == 1)
       	$('#SOLUSVM .backup_block').addClass('hidden');
	
	$('#solusvm_backup').val('');
	$('#solusvm_managed_services').change(function() {
		if($(this).val() == '')
			$('#SOLUSVM .backup_block').removeClass('hidden');
	});

	if(ms_dedicated == 1) {
		$('#Dedicated .backup_block').addClass('hidden');
		$('#dedicated_backup').val('');
	}
		
	$('#dedicated_managed_services').change(function() {
		if($(this).val() == '')
			$('#Dedicated .backup_block').removeClass('hidden');
	});	

	$('input[name="promo_code"]').val($('#promo_code_param').val());
	
	updateTotalPrices();
});

function domain_search_get_sld(sld, strip_last_dash) {
	if ((typeof sld == 'undefined') || (sld == '') || (sld == null) || !sld)
		sld = $.trim($('#sld').val());
	else 
		sld = $.trim(sld);
	
	sld = sld.toLowerCase().replace(/^www\./, '').replace(/^-+/, "").replace(/-{2,}$/, '-');
	
	if (strip_last_dash)
		sld = sld.replace(/-+$/, '');

	return (window.punycode ? punycode.toASCII(sld) : sld);
}

function vps_warnings() {
	$('.au-bandwidth-warning').hide();

	$('.mail_limit_notice').hide();
	
	if (
		($.inArray($('#products-block .tabs-new .selected a').attr('id'), ['A_VPS', 'A_SOLUSVM', 'A_KVM']) >= 0)
	) {
		$('.mail_limit_notice').show();
		if ($('select[name^="datacenter"]:visible').val() == 'sis_group') {
			$('.au-bandwidth-warning').show();
		}
	} else if (
		($.inArray($('#products-block .tabs-new .selected a').attr('id'), ['A_Dedicated']) >= 0)
	) {
		$('.mail_limit_notice').show();
	}
}

function show_hide_dedicated_storage_config() {
	var selected_plan = $('select.plans:visible').val();
	$('.dedicated_storage_config .inside').hide();
	
	if (
		$('.dedicated_storage_config #dedicated_storage_config_' + selected_plan).length && 
		$.trim($('.dedicated_storage_config #dedicated_storage_config_' + selected_plan).html()) != ''
	) {
		$('.dedicated_storage_config #dedicated_storage_config_' + selected_plan).show();
		$('.dedicated_storage_config').show();
	} else {
		$('.dedicated_storage_config').hide();
	}

	dedicated_raid_options();
}

function dedicated_raid_options() {
	if (!$('select[name="raid"]').is(':visible'))
		return;

	selected = $('select[name="raid"]').val();

	var selected_plan = $('select.plans:visible').val();

	var default_disks = $('#dedicated_storage_config_' + selected_plan + ' select[data-disk-type="default"]').length;
	if (default_disks == 0) {
		default_disks = $('#dedicated_storage_config_' + selected_plan + ' input[name^="storage_configuration"]').length;
	}
	
	total_disks = 0;
	
	if (typeof first_disk_size != 'undefined') delete(first_disk_size);
	if (typeof first_disk_type != 'undefined') delete(first_disk_type);

	$.each($('#dedicated_storage_config_' + selected_plan + ' [name^="storage_configuration[' + selected_plan + ']"]'), function(k, v) {
		if ($(this).val() == '') return;

		var selected_disk = $(this).find('option[value="' + $(this).val() + '"]');

		if ($(this).prop('nodeName') == 'INPUT')
			var selected_disk = $(this);

		if ((typeof first_disk_size == 'undefined') && (typeof first_disk_type == 'undefined')) {
			first_disk_size = selected_disk.data('disk-size');
			first_disk_type = selected_disk.data('disk-type');
		}

		if (
			(first_disk_size == selected_disk.data('disk-size')) && 
			(first_disk_type == selected_disk.data('disk-type'))
		) {
			total_disks++;
		}
	});

	raid_options = $('select.plans:visible option:selected').data('raid-options');
	if (typeof raid_options != 'undefined')
		raid_options = raid_options.split(',');

	$('.dedicated_storage_config select[name="raid"] option').each(function() {
		var raid_type = $(this).prop('value');
		if (raid_type == '') return;
		
		// check for raid options
		if (typeof raid_options != 'undefined' && raid_options.indexOf(raid_type.replace(/raid/, '')) == -1) {
			$(this).prop('disabled', true);
			return;
		}
		
		if (typeof $(this).data('required-disks') != 'undefined') {
			if ($(this).data('required-disks') > total_disks) {
				$(this).prop('disabled', true);
				if ($(this).parent().val() == null || $(this).parent().val() == raid_type) {
					$(this).parent().val($(this).parent().find('option:enabled:last').val()).change();
				}
			} else if ($(this).prop('disabled') == true) {
				$(this).prop('disabled', false);
			}
		}
	});
	
	if (total_disks >= default_disks) {
		if ($('.dedicated_storage_config select[name="raid"]').val() == '') {
			if (default_disks == 2)
				$('.dedicated_storage_config select[name="raid"]').val('raid1');
			else if (default_disks == 4)
				$('.dedicated_storage_config select[name="raid"]').val('raid10');
		}
	}
	
	if (selected != '' && $('.dedicated_storage_config select[name="raid"] option[value="' + selected + '"]').prop('disabled') == true) {
		$('.dedicated_storage_config select[name="raid"]').val('');
		
		if ($('.dedicated_storage_config select[name="raid"]').data('changed') === undefined)
			return;
		
		// not working properly
		//showMsg('info', raid_automatically_disabled);
	}
}

function get_domain_product(tld) {
	return window[get_domain_product_key(tld)]
}

function get_domain_product_key(tld) {
	domainProductKey = 'registerdomainProducts';
	
	if (
		panel 
		&& $('#A_' + panel.attr('id')).attr('host_domains') == 1 
		&& typeof domainProducts[tld] != 'undefined'
		&& typeof promo_domain == 'undefined'
	) {
		domainProductKey = 'domainProducts';
	}

	return domainProductKey;
}

function get_tld_price(productType, tld, price_type, period, currency) {
	var product = productType == 'domain' ? domainProducts : registerdomainProducts;
		
	var debug = typeof debug_tlds == 'object' && debug_tlds.indexOf(tld) != -1;

	var prefix = "get_tld_price('" + productType + "', '" + tld + "', '" + price_type + "', '" + period + "', '" + currency + "') ";

	if (typeof price_type == 'undefined')
		price_type = 'prices';

	if (typeof period == 'undefined')
		period = 12;

	if (typeof currency == 'undefined')
		currency = window['currency'];

	if (['register', 'transfer', 'renewal', 'prices'].indexOf(price_type.replace(/prices_/, '')) == -1) {
		if (debug) 
			console.log(prefix + 'invalid price_type: ' + price_type);
		return false;
	}

	if (price_type != 'prices' && price_type.match(/^prices\_/) == null)
		price_type = 'prices_' + price_type;
		
	if (typeof period == 'number' || period.match(/^period\_/) == null)
		period = 'period_' + period

	if (debug) {
		//console.log(prefix);
		//console.trace();
	}

	if (typeof product[tld] == 'undefined') {
		if (debug) 
			console.log(prefix + 'product not found');
		return false;
	}

	if (typeof product[tld][price_type] == 'undefined') {
		if (debug) 
			console.log(prefix + 'price_type ' + price_type + ' not found');
		return false;
	}
	
	if (typeof product[tld][price_type][period] == 'undefined') {
		if (debug) 
			console.log(prefix + 'period ' + period + ' not found');
		return false;
	}	
	
	if (typeof product[tld][price_type][period][currency.toUpperCase()] == 'undefined') {
		if (debug) 
			console.log(prefix + 'currency ' + currency + ' not found');
		return false;
	}	
	
	return parseFloat(product[tld][price_type][period][currency.toUpperCase()]);
}

function get_all_tld_prices(tld, period, currency) {
	if (typeof period != 'number')
		var period = 12;
	if (typeof currency != 'string')
		var currency = 'USD';
	
	ret = {
		'domain': {
			'default': get_tld_price('domain', tld, 'prices', period, currency),
			'register': get_tld_price('domain', tld, 'register', period, currency),
			'transfer': get_tld_price('domain', tld, 'transfer', period, currency),
		},
		'registerDomain': {
			'default': get_tld_price('registerDomain', tld, 'prices', period, currency),
			'register': get_tld_price('registerDomain', tld, 'register', period, currency),
			'transfer': get_tld_price('registerDomain', tld, 'transfer', period, currency),
		}		
	};
	
	console.log('All price for .' + tld + '; period: ' + period + '; currency: ' + currency);
	console.log('domain default: ' + ret['domain']['default'] + '; register: ' + ret['domain']['register'] + '; transfer: ' + ret['domain']['transfer']);
	console.log('registerDomain default: ' + ret['registerDomain']['default'] + '; register: ' + ret['registerDomain']['register'] + '; transfer: ' + ret['registerDomain']['transfer']);
	
	return ret;
}

function get_tld_renewal_price(tld) {
	var firstPeriod = get_tld_first_period(tld);
	
	var renewalPrice = get_tld_price('registerDomain', tld, 'renewal', firstPeriod, currency);
	
	if (renewalPrice !== false)
		return renewalPrice;
		
	return get_tld_price('registerDomain', tld, 'prices', firstPeriod, currency)
}

function get_active_tab() {
	if (panel == null)
		return $('#products-block .tabs-new span:first-child a').attr('product_type');
	
	return panel.attr('id').toLowerCase();
	
	if (!$('#products-block .selected a').length)
		return false;
	
	return $('#products-block .selected a').attr('product_type');
}

function get_tld_first_period(tld, product) {
	var product = (typeof product == 'undefined' || product != 'domain') ? registerdomainProducts : domainProducts;
	
	if (typeof product[tld] == 'undefined' || typeof product[tld]['prices'] == 'undefined')
		return false;
	
	for (firstPeriod in product[tld]['prices'])
		break;
		
	return firstPeriod;
}

function get_tld(domain) {
	if ((domain.match(/\./g) || []).length > 2)
		domain = domain.substring(domain.indexOf('.') + 1);
	
	return domain.substring(domain.indexOf('.') + 1);
}

function get_sld(domain) {
	if ((domain.match(/\./g) || []).length > 2) {
		var string = domain.replace('.' + get_tld(domain), '')

		return ((string.match(/\./g) || []).length >= 1) ? string.substring(string.indexOf('.') + 1) : string;
	}

	return domain.substring(0, domain.indexOf('.'));
}

function getCookie(cname) {
	var name = cname + "=";
	var decodedCookie = decodeURIComponent(document.cookie);
	var ca = decodedCookie.split(';');

	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ')
			c = c.substring(1);

		if (c.indexOf(name) == 0) {
			
			var ret = c.substring(name.length, c.length);
			try {
				var unpackArr = JSON.parse( ret );
				switch(typeof unpackArr) {
					case "object":
						return unpackArr;
					break;
				}
			} catch(err) { }

			return ret;
		}
	}
	
	//return "";
}

function setCookie(cname, cvalue, exdays, path) {
	switch(typeof cvalue) {
		case "object":
			cvalue = JSON.stringify(cvalue);
		break;
	}

	if (typeof exdays != 'number')
		exdays = 1;

	if (typeof path == 'undefined')
		path = '/';
	
	var d = new Date();
	d.setTime(d.getTime() + (exdays*24*60*60*1000));

	var expires = "expires="+ d.toUTCString();

	document.cookie = cname + "=" + cvalue + ";" + expires + ";path=" + path;
}

function deleteCookie(cname) {
	setCookie(cname, false, -1);
}

function tr(str, params) {
	str = (typeof window[str] != 'undefined') ? window[str] : '_' + str + '_';
	
	if (typeof params == 'object') {
		for (i in params) {
			str = str.replace('%' + i + '%', params[i]);
		}
	}
	return str;
}

function set_dedicated_stock() {
	if (get_active_tab() != 'dedicated')
		return;

	var dc = $('#dedicated_datacenter').val();

	$('#dedicated_plan option').each(function(k, v) {
		var productID = $(this).val();

		if (
			typeof dedicatedStock[productID] == 'undefined'
			|| dedicatedStock[productID].indexOf(dc) == -1
		) {
			$(this).prop('disabled', true);
		} else {
			$(this).prop('disabled', false);
		}
	});

	if (getSelectedPlanOption().prop('disabled') == true) {
		var err = selected_plan_out_of_stock.replace(/%planName%/, getSelectedPlanOption().text()).replace(/%datacenter%/, $('#dedicated_datacenter option:selected').text());
		
		showMsg('error', err);

		// select first available server
		$('#dedicated_plan option').each(function() {
			if ($(this).prop('disabled') == false) {
				$('#dedicated_plan').val($(this).attr('value'));
				return false;
			}
		});
	}
}