$(document).ready(function(){
	// Wobbling menu
	$('#menu ul').lavaLamp({
		fx: "backout",
		speed: 700
	});
	
	// Numeric fields
	$('.numeric_input').numeric();
	
	// New table rows
	var dnsrowindex = 1;
	
	$('.add_dnsrow').click(function(){
		var clonedRow = $('#dnsadd tr:last').clone();
		$('input', clonedRow).attr('value', '');
		$('.add_dnsrow', clonedRow).remove();
		$('#uniform-dns_new_type_0', clonedRow).remove();
		
		var clonedDD = $('#dns_new_type_0').clone();
		
		// Voeg toe aan de 2e tabelkolom
		$('td:nth-child(2)', clonedRow).append(clonedDD);
		$('#dns_new_type_0', clonedRow).css('opacity', 'inherit');
		$('#dns_new_type_0', clonedRow).uniform();
		$('#dns_new_type_0', clonedRow).attr('id', 'dns_new_type_' + dnsrowindex);
		
		$('#dnsadd').append(clonedRow);
		
		dnsrowindex += 1;
	});
	
	// Copy domain rows
	var rowIndex = $('#rowIndex').val();
	
	$('.add_domainrow').click(function(){
		var clonedRow = $('#domain-1').clone();
		
		// Set id's
		clonedRow.attr('id', 'domain-' + rowIndex);
		$('img', clonedRow).attr('id', 'domain-result-' + rowIndex);
		$('span', clonedRow).attr('id', 'domain-result-txt-' + rowIndex);
		
		// Set classes
		$('input', clonedRow).removeClass('domaininfo-1');
		$('input', clonedRow).addClass('domaininfo-' + rowIndex);
		
		// Set names
		$('input', clonedRow).attr('name', 'domein[' + rowIndex + ']');
		
		$('input', clonedRow).attr('value', '');
		
		// Set onblur
		$('input', clonedRow).removeAttr('onblur');
		$('input', clonedRow).bind('blur', {index: rowIndex}, function(event) { check_domain_adv(event.data.index); });
		
		// Hide results
		$('img', clonedRow).hide();
		$('span', clonedRow).html('');
		
		var clonedRowEpp = $('#epp-domain-1').clone();
		clonedRowEpp.attr('id', 'epp-domain-' + rowIndex);
		
		// Set names
		$('input', clonedRowEpp).attr('name', 'epp[' + rowIndex + ']');
		
		// Hide row
		clonedRowEpp.hide();
		
		var clonedRowLast = $('#move-domain-1').clone();
		clonedRowLast.attr('id', 'move-domain-' + rowIndex);
		
		// Set some id's and for's
		$('#move_domain_1_ja', clonedRowLast).attr('id', 'move_domain_' + rowIndex + '_ja');
		$('#move_domain_1_nee', clonedRowLast).attr('id', 'move_domain_' + rowIndex + '_nee');
		
		$('label[for=move_domain_1_ja]', clonedRowLast).attr('for', 'move_domain_' + rowIndex + '_ja');
		$('label[for=move_domain_1_nee]', clonedRowLast).attr('for', 'move_domain_' + rowIndex + '_nee');

		
		$('#move_domain_' + rowIndex + '_ja', clonedRowLast).attr('checked', false);
		$('#move_domain_' + rowIndex + '_nee', clonedRowLast).attr('checked', false);
		
		$('#move_domain_' + rowIndex + '_ja', clonedRowLast).attr('name', 'move_domain_' + rowIndex);
		$('#move_domain_' + rowIndex + '_nee', clonedRowLast).attr('name', 'move_domain_' + rowIndex);
		
		// Set onclick
		$('#move_domain_' + rowIndex + '_nee', clonedRowLast).removeAttr('onclick');
		$('#move_domain_' + rowIndex + '_nee', clonedRowLast).bind('click', {index: rowIndex}, function(event) { other_domain(event.data.index); });
				
		// Hide row
		clonedRowLast.hide();
		
		$('#orderDomain').append(clonedRow);
		$('#orderDomain').append(clonedRowLast);
		$('#orderDomain').append(clonedRowEpp);
		
		rowIndex++;
	});
	
	// Scroll naar bovenkant
	$('.to_top').click(function() {
	  	$('html,body').animate({scrollTop: 0}, 1000);
	  	return false;
	});

	$('.to_top_sub').click(function() {
	  	$('html,body').animate({scrollTop: 0}, 1000);
	  	return false;
	});
	
	$('div.tabs ul').tabs('div.tabcontent > div').history();
	
	// Tabellijst
	$('table.listtable tbody tr:even').addClass('even');
	
	// Change registrant
	$("#type_p").click(function() {
		$('.bedrijf-data').hide();
		$('table.houderdata tr').removeClass('even');
		$('table.houderdata tr:visible:even').addClass('even');
	});
	
	$("#type_b").click(function() {
		$('.bedrijf-data').show();
		$('table.houderdata tr').removeClass('even');
		$('table.houderdata tr:visible:even').addClass('even');
	});
	
	$("#adm_type_p").click(function() {
		$('.adm_bedrijf-data').hide();
		$('table.admindata tr').removeClass('even');
		$('table.admindata tr:visible:even').addClass('even');
	});
	
	$("#adm_type_b").click(function() {
		$('.adm_bedrijf-data').show();
		$('table.admindata tr').removeClass('even');
		$('table.admindata tr:visible:even').addClass('even');
	});
	
	$("#tech_type_p").click(function() {
		$('.tech_bedrijf-data').hide();
		$('table.techdata tr').removeClass('even');
		$('table.techdata tr:visible:even').addClass('even');
	});
	
	$("#tech_type_b").click(function() {
		$('.tech_bedrijf-data').show();
		$('table.techdata tr').removeClass('even');
		$('table.techdata tr:visible:even').addClass('even');
	});
	
	// Provinces
	var provNL = {
		'0'  : 'Maak een keuze',
		'DR' : 'Drenthe',
		'FL' : 'Flevoland',
		'FR' : 'Friesland',
		'GE' : 'Gelderland',
		'GR' : 'Groningen',
		'LI' : 'Limburg',
		'NB' : 'Noord-Brabant',
		'NH' : 'Noord-Holland',
		'OV' : 'Overijssel',
		'UT' : 'Utrecht',
		'ZE' : 'Zeeland',
		'ZH' : 'Zuid-Holland'
	}
	
	var provBE = {
		'0'	 : 'Maak een keuze',
		'AT' : 'Antwerpen',
		'LI' : 'Limburg',
		'VB' : 'Vlaams Brabant',
		'WV' : 'West Vlaanderen',
		'OV' : 'Oost Vlaanderen',
		'WB' : 'Waals Brabant',
		'HE' : 'Henegouwen',
		'LU' : 'Luik',
		'LX' : 'Luxemburg',
		'NA' : 'Namen'
	}
	
	var provDK = {
		'0'	 : 'Maak een keuze',
		'84' : 'Hovedstaden',
		'82' : 'Midtjylland',
		'81' : 'Nordjylland',
		'85' : 'Sjælland',
		'83' : 'Syddanmark',
	}
	
	$('#copy_to_adm').click(function() {
		// Hmm... radiobutton, tricky...
		if ($('#type_b').is(':checked')) {
			$('#adm_type_b').attr('checked', true);
			$('.adm_bedrijf-data').show();
		} else {
			$('#adm_type_p').attr('checked', true);
			$('.adm_bedrijf-data').hide();
		}
		
		// Provincies horen alleen bij NL en BE
		if ($('#land').val() == 'NL') {
			$('#adm_provincie_val').empty();
			
			$.each(provNL, function(val, text) {
				$('#adm_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#adm_provincie').show();
			
			// Zet woonplaats op readonly!
			$('#adm_woonplaats').attr('readonly', true);
		} else if ($('#land').val() == 'BE') {
			$('#adm_provincie_val').empty();
			
			$.each(provBE, function(val, text) {
				$('#adm_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#adm_provincie').show();
			
			// Zet woonplaats op aanpasbaar
			$('#adm_woonplaats').attr('readonly', false);
		} else {
			$('#adm_provincie').hide();
			$('#adm_provincie_val').val('');
			
			// Zet woonplaats op aanpasbaar
			$('#adm_woonplaats').attr('readonly', false);
		}
		
		$('table.admindata tr').removeClass('even');
		$('table.admindata tr:visible:even').addClass('even');

		$('input[name*=adm_voorletters]').val($('input[name*=voorletters]').val());
		$('input[name*=adm_naam]').val($('input[name*=naam]').val());
		$('input[name*=adm_bedrijfsnaam]').val($('input[name*=bedrijfsnaam]').val());
		$('input[name*=adm_straat]').val($('input[name*=straat]').val());
		$('input[name*=adm_huisnr]').val($('input[name*=huisnr]').val());
		$('input[name*=adm_postcode]').val($('input[name*=postcode]').val());
		$('input[name*=adm_woonplaats]').val($('input[name*=woonplaats]').val());
		$('select[name*=adm_land]').val($('select[name*=land]').val());
		$('select[name*=adm_provincie]').val($('select[name*=provincie]').val());
		$('input[name*=adm_telefoon]').val($('input[name*=telefoon]').val());
		$('input[name*=adm_fax]').val($('input[name*=fax]').val());
		$('input[name*=adm_email]').val($('input[name*=email]').val());
		
		$.uniform.update('select[name*=adm_land]');
		$.uniform.update('select[name*=adm_provincie]');
		$.uniform.update('input[name*=adm_houder_type]');
	});
	
	$('#copy_to_tech').click(function() {
		// Hmm... radiobutton, tricky...
		if ($('#type_b').is(':checked')) {
			$('#tech_type_b').attr('checked', true);
			$('.tech_bedrijf-data').show();
		} else {
			$('#tech_type_p').attr('checked', true);
			$('.tech_bedrijf-data').hide();
		}
		
		// Provincies horen alleen bij NL en BE
		if ($('#land').val() == 'NL') {
			$('#tech_provincie_val').empty();
			
			$.each(provNL, function(val, text) {
				$('#tech_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#tech_provincie').show();
			
			// Zet woonplaats op readonly!
			$('#tech_woonplaats').attr('readonly', true);
		} else if ($('#land').val() == 'BE') {
			$('#tech_provincie_val').empty();
			
			$.each(provBE, function(val, text) {
				$('#tech_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#tech_provincie').show();
			
			// Zet woonplaats op aanpasbaar
			$('#tech_woonplaats').attr('readonly', false);
		} else {
			$('#tech_provincie').hide();
			$('#tech_provincie_val').val('');
			
			// Zet woonplaats op aanpasbaar
			$('#tech_woonplaats').attr('readonly', false);
		}
		
		$('table.techdata tr').removeClass('even');
		$('table.techdata tr:visible:even').addClass('even');

		$('input[name*=tech_voorletters]').val($('input[name*=voorletters]').val());
		$('input[name*=tech_naam]').val($('input[name*=naam]').val());
		$('input[name*=tech_bedrijfsnaam]').val($('input[name*=bedrijfsnaam]').val());
		$('input[name*=tech_straat]').val($('input[name*=straat]').val());
		$('input[name*=tech_huisnr]').val($('input[name*=huisnr]').val());
		$('input[name*=tech_postcode]').val($('input[name*=postcode]').val());
		$('input[name*=tech_woonplaats]').val($('input[name*=woonplaats]').val());
		$('select[name*=tech_land]').val($('select[name*=land]').val());
		$('select[name*=tech_provincie]').val($('select[name*=provincie]').val());
		$('input[name*=tech_telefoon]').val($('input[name*=telefoon]').val());
		$('input[name*=tech_fax]').val($('input[name*=fax]').val());
		$('input[name*=tech_email]').val($('input[name*=email]').val());
		
		$.uniform.update('select[name*=tech_land]');
		$.uniform.update('select[name*=tech_provincie]');
		$.uniform.update('input[name*=tech_houder_type]');
	});
	
	// Change provinces	
	$('#land').change(function() {
		$('#provincie_val').empty();
		$('#provincie').show();
	
		if ($('#land').val() == 'Nederland' || $('#land').val() == 'NL') {
			$.each(provNL, function(val, text) {
				$('#provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#woonplaats').attr('readonly', true);
			
			$('#woonplaats').val('');
			$('#straat').val('');
			$('#huisnr').val('');
			$('#postcode').val('');
			
			$.uniform.update('#provincie_val');
		} else if ($('#land').val() == 'België' || $('#land').val() == 'BE') {
			$.each(provBE, function(val, text) {
				$('#provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#woonplaats').attr('readonly', false);
			
			$('#woonplaats').val('');
			$('#straat').val('');
			$('#huisnr').val('');
			$('#postcode').val('');
			
			$.uniform.update('#provincie_val');
		} else if ($('#land').val() == 'Dansk' || $('#land').val() == 'DK') {
			$.each(provDK, function(val, text) {
				$('#provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#woonplaats').attr('readonly', false);
			
			$('#woonplaats').val('');
			$('#straat').val('');
			$('#huisnr').val('');
			$('#postcode').val('');
			
			$.uniform.update('#provincie_val');
		} else {
			$('#provincie').hide();
			
			$('#woonplaats').attr('readonly', false);
			
			$('#woonplaats').val('');
			$('#straat').val('');
			$('#huisnr').val('');
			$('#postcode').val('');
		}
		
		$('table.houderdata tr').removeClass('even');
		$('table.houderdata tr:visible:even').addClass('even');
	});
	
	$('#adm_land').change(function() {
		$('#adm_provincie_val').empty();
		$('#adm_provincie').show();
	
		if ($('#adm_land').val() == 'Nederland' || $('#adm_land').val() == 'NL') {
			$.each(provNL, function(val, text) {
				$('#adm_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#adm_woonplaats').attr('readonly', true);
			
			$('#adm_woonplaats').val('');
			$('#adm_straat').val('');
			$('#adm_huisnr').val('');
			$('#adm_postcode').val('');
			
			$.uniform.update('#adm_provincie_val');
		} else if ($('#adm_land').val() == 'België' || $('#adm_land').val() == 'BE') {
			$.each(provBE, function(val, text) {
				$('#adm_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#adm_woonplaats').attr('readonly', false);
			
			$('#adm_woonplaats').val('');
			$('#adm_straat').val('');
			$('#adm_huisnr').val('');
			$('#adm_postcode').val('');
			
			$.uniform.update('#adm_provincie_val');
		} else if ($('#adm_land').val() == 'Dansk' || $('#adm_land').val() == 'DK') {
			$.each(provDK, function(val, text) {
				$('#adm_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#adm_woonplaats').attr('readonly', false);
			
			$('#adm_woonplaats').val('');
			$('#adm_straat').val('');
			$('#adm_huisnr').val('');
			$('#adm_postcode').val('');
			
			$.uniform.update('#adm_provincie_val');
		} else {
			$('#adm_provincie').hide();
		
			$('#adm_woonplaats').attr('readonly', false);
			
			$('#adm_woonplaats').val('');
			$('#adm_straat').val('');
			$('#adm_huisnr').val('');
			$('#adm_postcode').val('');
		}
		
		$('table.admindata tr').removeClass('even');
		$('table.admindata tr:visible:even').addClass('even');
	});
	
	$('#tech_land').change(function() {
		$('#tech_provincie_val').empty();
		$('#tech_provincie').show();
	
		if ($('#tech_land').val() == 'Nederland' || $('#tech_land').val() == 'NL') {
			$.each(provNL, function(val, text) {
				$('#tech_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#tech_woonplaats').attr('readonly', true);
			
			$('#tech_woonplaats').val('');
			$('#tech_straat').val('');
			$('#tech_huisnr').val('');
			$('#tech_postcode').val('');
			
			$.uniform.update('#tech_provincie_val');
		} else if ($('#tech_land').val() == 'België' || $('#tech_land').val() == 'BE') {
			$.each(provBE, function(val, text) {
				$('#tech_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#tech_woonplaats').attr('readonly', false);
			
			$('#tech_woonplaats').val('');
			$('#tech_straat').val('');
			$('#tech_huisnr').val('');
			$('#tech_postcode').val('');
			
			$.uniform.update('#tech_provincie_val');
		} else if ($('#tech_land').val() == 'Dansk' || $('#tech_land').val() == 'DK') {
			$.each(provDK, function(val, text) {
				$('#tech_provincie_val').append($('<option></option>').val(val).html(text));
			});
			
			$('#tech_woonplaats').attr('readonly', false);
			
			$('#tech_woonplaats').val('');
			$('#tech_straat').val('');
			$('#tech_huisnr').val('');
			$('#tech_postcode').val('');
			
			$.uniform.update('#tech_provincie_val');
		} else {
			$('#tech_provincie').hide();
			
			$('#tech_woonplaats').attr('readonly', true);
			
			$('#tech_woonplaats').val('');
			$('#tech_straat').val('');
			$('#tech_huisnr').val('');
			$('#tech_postcode').val('');
		}
		
		$('table.techdata tr').removeClass('even');
		$('table.techdata tr:visible:even').addClass('even');
	});
	
	// Vouchers
	$('#voucher-error').hide();
	var origPrijs = $('#totaalprijs').html();
	
	$('#voucher-subm').click(function() {
		var voucherval = $('#voucher').val();
		var user_id = $('#user_id_voucher').val();
		var module = $('#module_voucher').val();

		$.ajax({
			type: 'post',
			url: './js/ajax/coupon.ajax.php',
			data: 'code=' + voucherval + '&user_id=' + user_id + '&module=' + module,
			success: function(results) {
				$('#voucher-error').hide();
				
				if (results == 'ONGELDIG')
				{
					$('#voucher-error').attr('src', 'images/block.png');
					$('#voucher-error').fadeIn();
					
					$('#korting-txt').html('');
					$('#korting-sign').html('');
					$('#korting-val').html('');
					
					$('#totaalprijs').html(origPrijs);
				}
				else
				{
					$('#voucher-error').attr('src', 'images/check.png');
					$('#voucher-error').fadeIn();
					
					// Bereken korting
					var rawVoucher = results.split('||');
					
					if (rawVoucher[0] == 'percentage')
					{
						var newPrijs = (origPrijs / 100) * (100 - rawVoucher[1]);
						$('#totaalprijs').html(newPrijs.toFixed(2));
						
						$('#korting-txt').html('Korting:');
						$('#korting-sign').html('%');
						$('#korting-val').html('- ' + rawVoucher[1]);
					}
					else
					{
						var newPrijs = origPrijs - rawVoucher[1];
						$('#totaalprijs').html(newPrijs.toFixed(2));
						
						$('#korting-txt').html('Korting:');
						$('#korting-sign').html('&euro;');
						$('#korting-val').html('- ' + rawVoucher[1]);
					}
				}
			}
		})
	});
	
	// Bestel hosting
    $('.hide-res').hide();
    
    // Uniform checkboxes, dropdowns and radios.
    $('select, :checkbox, :radio, :file').uniform();
    
    // Modalbox
    $('a[rel*=modal]').facebox(); // Applies modal window to any link with attribute rel="modal"
    
    // Hostingwizard
    $('input[name*=hostingwizard]').change(function() {
    	// Eerst alles omhoog schuiven, indien zichtbaar
    	$('p.hw:visible').slideUp();
    	
    	$('p#hw-exp-' + $(this).val()).slideDown(200);
    });
    
    // Houdertabellen
    $('table.houderdata tr:visible:even').addClass('even');
    $('table.admindata tr:visible:even').addClass('even');
    $('table.techdata tr:visible:even').addClass('even');
});

// Other jQuery based functions
function check_domain(domain, index)
{
	$.ajax({
		type: 'post',
		url: './js/ajax/domaininfo.ajax.php?simple=true',
		data: 'domain=' + domain,
		success: function(results) {
			if (results == 'ERROR')
			{
				$('#loader-' + index).attr('src', 'css/images/icons/icon_error.png');
				$('#result-' + index).html('Ongeldig');
			}
			else if (results == 'BEZET')
			{
				$('#loader-' + index).attr('src', 'css/images/icons/icon_error.png');
				$('#result-' + index).html('Bezet');
				$('#txt-' + index).html('U kunt deze domeinnaam verhuizen');
				$('input[name*=move[' + index + ']]').val('ja');
			}
			else if (results == 'BESCHIKBAAR')
			{
				$('#loader-' + index).attr('src', 'css/images/icons/icon_check.png');
				$('#result-' + index).html('Beschikbaar');
				$('#txt-' + index).html('U kunt deze domeinnaam bestellen');
				$('input[name*=move[' + index + ']]').val('nee');
			}
		}
	});
}

function get_city(pc, huisnr, straat, city_id, country)
{
	if (country == 'NL' || country == 'Nederland') {
		$.ajax({
			type: 'post',
			url: './js/ajax/postcode.ajax.php',
			data: 'pc=' + pc + '&huisnr=' + huisnr + '&straat=' + straat,
			success: function(results) {
				$('#' + city_id).val(results);
			}
		});
	}
}