var bySms = false;
var byCard = false;
var isUsername = false;
var buyType = 0;
var shortVer = false;
var selCur = false;
var isVat = false;
var lastCountry = -1;
var useNetto = true;
var isUe = false;
var isPremium = false;
$(function(){
	$('#jQ_ownerContact').click(function(){
		$.blockUI( { message: $('#jQ_ownerContactBox'), css: { top: '100px', cursor: 'default' } } );
	});
	$('.jQ_ownerContactCancel').click(function(){
//		$('#jQ_ownerContactBoxEmail').val('');
		$('#jQ_ownerContactBoxTopic').val('');
		$('#jQ_ownerContactBoxMessage').val('');
		$.unblockUI();
	});
	$('#jQ_ownerContactSend').click(function(){
		data = { topic: $('#jQ_ownerContactBoxTopic').val(), message: $('#jQ_ownerContactBoxMessage').val(), domain: $('#jQ_ownerContactBoxDomain').val() }
		$.post( '/ajax/domain/contact', data, function( e ){
//			$('#jQ_ownerContactBoxEmail').val('');
			$('#jQ_ownerContactBoxTopic').val('');
			$('#jQ_ownerContactBoxMessage').val('');
			$.blockUI( { message: $('#jQ_ownerContactBoxMsg'), css: { top: '100px', cursor: 'default' } } );
			setTimeout ( function(){$.unblockUI();}, 2000 );
		},'json');
	});
	if( $('#jQ_shortVer').val() == 1 ) shortVer = true;
	isVat = $('#jQ_fVat').attr('checked');
	$('#jQ_country').click(function(){
		if( lastCountry != $('#jQ_country').val() ) {
			lastCountry = $('#jQ_country').val();
			data = { country: $('#jQ_country').val() }
			$.post( '/ajax/domain/country', data, function( e ){
				isUe = e.isUe;
				useNetto = !isUe || isUe && isVat;
				if( shortVer ) $('#jQ_curBuyDomain').click();
			},'json');
		}
		
	});
	$('#jQ_fVat').click(function(){		
		if( $(this).attr('checked') ) {
			$('.jQ_fVat').show();
			isVat = true;
			useNetto = true;
		} else {
			$('.jQ_fVat').hide();
			isVat = false;
			useNetto = isUe ? false : true;
		}
		if( shortVer ) $('#jQ_curBuyDomain').click();
	});
	$('#jQ_formCardSubmit').click(function(){
		$('.jQ_fvatErr').hide();
		if( isVat ) {
			data = {
					nip: $('#jQ_formNip').val(), 
					companyName: $('#jQ_companyName').val(), 
					phone: $('#jQ_formPhone').val(), 
					fax: $('#jQ_formFax').val(),
					amount: $('#jQ_cardAmount').text(),
					currency: $('#jQ_curBuyDomain').val(),
					email: $('#jQ_cardEmail').val(),
					firstName: $('#firstname').val(),
					lastName: $('#lastname').val(),
					street: $('#street').val(),
					number: $('#jQ_formNumber').val(),
					flat: $('#jQ_formFlat').val(),
					city: $('#city').val(),
					zip: $('#zip').val(),
					country: $('#jQ_country').val()
				};
			$.post( '/ajax/domain/fvat', data, function( e ){
				if( e.isError ) {
					for( i = 0; i < e.errors.length; i++ ) {
						$('#'+e.errors[i]).show();
					}
					return false;
				} else {
					$('#jQ_cardPaymentForm').submit();
					return false;
				}
			},'json');
		} else {
			$('#jQ_cardPaymentForm').submit();
			return false;
		}
		return false;
	});
	$('#jQ_curBuyDomain').click(function(){
		selCur = parseInt( $('#jQ_curBuyDomain').val() );
		var netto = '';
//		console.log( 'netto: ' + useNetto );
		if( useNetto ) netto = 'netto';
		switch( buyType ) {
			case 1:
					if( selCur == parseInt( $('#jQ_curUSD').val() ) ) {
						$('#jQ_cardAmount').text( $('#jQ_priceDomainUSD'+netto).val() );
					} else {
						$('#jQ_cardAmount').text( $('#jQ_priceDomainEUR'+netto).val() );
					}
				break;
			case 2:
					if( selCur == parseInt( $('#jQ_curUSD').val() ) ) {
						$('#jQ_cardAmount').text( $('#jQ_priceMailUSD'+netto).val() );
					} else {
						$('#jQ_cardAmount').text( $('#jQ_priceMailEUR'+netto).val() );	
					}
				break;
		}
	});	
	$('#jQ_smsCodeBut').click(function(){
		$('#jQ_smsCodeErr').hide();
		$('#jQ_smsCodeBut').attr( 'disabled', true );
		data = {phone: $('#jQ_usernameReserved').val(), domain: $('#jQ_domainName').val(), type: buyType, code: $('#jQ_smsCode').val(), promotion: $('#jQ_promotionCodeSpan').text() };
		$.post( '/ajax/buy', data, function( e ){
			if( e.status == 1 ) {				
				document.location.href = e.url;
			} else {
				$('#jQ_smsCodeBut').attr( 'disabled', false );
				$('#jQ_smsCodeErr').show();
			}
		},'json');
	});
	$('#jQ_showWhoisDetail').click( function(){ $('#jQ_whoisDetail').show(); } );
	$('#jQ_usernameChange').click(function(){
		$('#jQ_usernameChange').hide();
		$('#jQ_usernameSpan').hide();
		$('#jQ_username_value').show();
		$('#jQ_sendEmail').show();
	});
	$('#account_1').click(function(){		
		if( $('#jQ_isPremium').val() == 2 || $('#jQ_isPremium').val() == 3 ) {
			isPremium = true;
			$('.account_2').hide();
			$('#jQ_step_4_enter_username').show();
			document.location.href = '#jQ_step_4_enter_username';
			if(  $('#jQ_isPremium').val() == 2 ) {
				$('#jQ_cardAmount').text( parsePriceStr( domainPrices.premium2 ) );
			} else {
				$('#jQ_cardAmount').text( parsePriceStr( domainPrices.premium3 ) );				
			}
		} else {
			$('.account_2').hide();
			$('#jQ_step_2_domain_type').show();		
			document.location.href = '#jQ_step_2_domain_type';
		}
	});
	$('#account_2').click(function(){
		$('.account_1').hide();
		$('#jQ_step_2_login_form').show();
		document.location.href = '#jQ_step_2_login_form';		
		$('#domain_type_1').attr('checked', false);
		$('#domain_type_2').attr('checked', false);
		$('#payment_type_1').attr('checked', false);
		$('#payment_type_2').attr('checked', false);		
		$('#payment_type_11').attr('checked', false);		
		$('#payment_type_12').attr('checked', false);		
		$('#jQ_payPalBtn').attr('checked', false);		
	});
	$('.account_2click').click( function(){
		$('#login1').val( $('#jQ_username_value').val() );
		$('#account_2').click(); 
	});
	$('#domain_type_1').click(function(){
		var netto = '';
		if( useNetto ) netto = 'netto';
		if( shortVer ) {
			buyType = 1;
			$('#jQ_cardBuyTypeHid').val( buyType );
			if( selCur && selCur == parseInt( $('#jQ_curUSD').val() ) ) {
				$('#jQ_cardAmount').text( $('#jQ_priceDomainUSD'+netto).val() );
			} else {
				$('#jQ_cardAmount').text( $('#jQ_priceDomainEUR'+netto).val() );
			}
			$('#jQ_step_4_enter_username').show();
			if( isUsername ) {
				$('#jQ_step_5_payment_type_2').show();
				document.location.href = '#jQ_step_5_payment_type_2';
			} else {
				document.location.href = '#jQ_step_4_enter_username';
				$('#jQ_step_5_payment_type_2').hide();
			}
		} else {
			if( bySms ) $('#payment_type_1').attr( 'checked', true );
			if( byCard ) $('#payment_type_2').attr( 'checked', true );
			setSms( $('#domain_type_1') );
			$('#jQ_cardAmount').text( parsePriceStr( domainPrices.domain ) );
			$('#jQ_step_3_payment_type').show();
			document.location.href = '#jQ_step_3_payment_type';
		}
	});
	$('#domain_type_2').click(function(){
		byCard = true;
		bySms = false;
		var netto = '';
		if( useNetto ) netto = 'netto';
		buyType = 2;
		$('#jQ_cardBuyTypeHid').val( buyType );
		if( shortVer ) {
			if( selCur && selCur == parseInt( $('#jQ_curUSD').val() ) ) {
				$('#jQ_cardAmount').text( $('#jQ_priceMailUSD'+netto).val() );
			} else {
				$('#jQ_cardAmount').text( $('#jQ_priceMailEUR'+netto).val() );
			}
			$('#jQ_step_4_enter_username').show();
			if( isUsername ) {
				$('#jQ_step_5_payment_type_2').show();
				document.location.href = '#jQ_step_5_payment_type_2';
			} else {
				document.location.href = '#jQ_step_4_enter_username';
				$('#jQ_step_5_payment_type_2').hide();
			}
		} else {
			$('#jQ_cardAmount').text( parsePriceStr( domainPrices.domain5 ) );
			$('#jQ_step_3_payment_type').hide();
			$('#jQ_step_4_enter_username').show();
			$('#jQ_step_5_payment_type_phone').hide();
//			document.location.href = '#jQ_step_4_enter_username';
			if( isUsername ) {
				$('#jQ_step_5_payment_type_2').show();
				document.location.href = '#jQ_step_5_payment_type_2';
			} else {
				document.location.href = '#jQ_step_4_enter_username';
				$('#jQ_step_5_payment_type_2').hide();
			}
		}
	});
	$('#payment_type_1').click(function(){
		if( shortVer ) {
			bySms = false;
			byCard = true;
		} else {
			bySms = true;
			byCard = false;
		}
		$('#jQ_EmailErr').hide();
		$('#jQ_EmailExist').hide();
		$('#jQ_EmailReserved').hide();
		$('#jQ_EmailOk').hide();
		$('#jQ_EmailAsigned').hide();
		$('#payment_type_11').attr('checked', false);		
		$('#payment_type_12').attr('checked', false);		
		$('#jQ_payPalBtn').attr('checked', false);
		$('#jQ_step_4_enter_username').show();
		if( isUsername ) {
			$('#jQ_step_5_payment_type_2').hide();
			$('#jQ_step_5_payment_type_3').hide();
			$('#jQ_step_7_payment_type_3').hide();
			$('#jQ_step_5_payment_type_phone').show();
			document.location.href = '#jQ_step_5_payment_type_phone';
		} else {
			document.location.href = '#jQ_step_4_enter_username';
			$('#jQ_step_5_payment_type_2').hide();
			$('#jQ_step_5_payment_type_3').hide();
			$('#jQ_step_7_payment_type_3').hide();			
		}
	});
	$('#payment_type_2').click(function(){
		bySms = false;
		byCard = true;
		$('#jQ_EmailErr').hide();
		$('#jQ_EmailExist').hide();
		$('#jQ_EmailReserved').hide();
		$('#jQ_EmailOk').hide();
		$('#jQ_EmailAsigned').hide();
		$('#jQ_step_4_enter_username').show();
		if( isUsername ) {
			$('#jQ_step_5_payment_type_phone').hide();
			$('#jQ_step_5_payment_type_2').show();
			document.location.href = '#jQ_step_5_payment_type_2';
		} else {
			document.location.href = '#jQ_step_4_enter_username';
			$('#jQ_step_5_payment_type_phone').hide();
		}
	});
	$('#jQ_sendEmail').click(function(){
		$('#jQ_EmailErr').hide();
		$('#jQ_EmailExist').hide();
		$('#jQ_EmailReserved').hide();
		$('#jQ_EmailOk').hide();
		$('#jQ_EmailAsigned').hide();
		data = {username: $('#jQ_username_value').val(), domain: $('#jQ_domainName').val() };
		$.post( '/ajax/domain/reserv', data, function( e ){
			if( e.status == 1 || e.status == 2 ) {
				$('#jQ_usernameReserved').val( $('#jQ_username_value').val() );
				$('#jQ_usernameSpan').text( $('#jQ_username_value').val() );
				$('#jQ_username_value').hide();
				$('#jQ_sendEmail').hide();
				$('#jQ_usernameSpan').show();
				$('#jQ_usernameChange').show();		
				if( !isUsername ) {			
					if( bySms ) {
						$('#jQ_step_5_payment_type_phone').show();		
						document.location.href = '#jQ_step_5_payment_type_phone';
					} else {
						$('#jQ_step_5_payment_type_2').show();
						document.location.href = '#jQ_step_5_payment_type_2';
					}
				} else {
					if( bySms ) {
						document.location.href = '#jQ_step_5_payment_type_phone';
						$('#jQ_step_5_payment_type_phone').show();		
					} else {
						$('#jQ_step_5_payment_type_2').show();
						document.location.href = '#jQ_step_5_payment_type_2';
					}
				}
				isUsername = true;
				$('#jQ_cardEmail').val( $('#jQ_username_value').val() );
				$('#jQ_cardEmailHid').val( $('#jQ_username_value').val() );
				$('#jQ_EmailOk').show();
			} else if( e.status == 0 ) {
				$('#jQ_EmailErr').show();
			} else if( e.status == -1 ) {
				$('#jQ_EmailExist').show();
			} else if( e.status == -2 ) {
				$('#jQ_EmailReserved').show();
			} else if( e.status == -3 ) {
				$('#jQ_EmailAsigned').show();
			}
		}, 'json' );
	});
});

function cardChangePayment( e, id ) {
	$('button.active').removeClass('active');
	$('.jQ_pay').hide();
	if( id == 5 ) {
		$('#jQ_step_5_payment_type_3').show();
		document.location.href = '#jQ_step_5_payment_type_3';
	} else {
		$('#jQ_step_5_payment_type_3').show();
		document.location.href = '#jQ_step_5_payment_type_3';
	}
	$('#jQ_step_7_payment_type_3').hide();
	$('#jQ_pay'+id).show();
	$(e).addClass('active');
}

function cardPay( name, id ) {
	$('#jQ_step_7_payment_type_3').show();
	document.location.href = '#jQ_step_7_payment_type_3';
	$('#jQ_cardPaytype').text( name );
	$('#jQ_step_5_payment_type_3').hide();
	$('#jQ_cardPayTypeHid').val( id );
	if(name=='PayPal')
	{
		$(".jQ_payBut button").removeClass("active");
		$('#jQ_payPalBtn').addClass('active');
	}  
}

function setSms( e ) {	
	if( $(e).val() == 2 ) {
		price = parsePriceStr( domainPricesNetto.domain5 );
		nr = phoneCodes.emailSmsNr;
		brutto = parsePriceStr( domainPrices.domain5 );
		body = phoneCodes.emailSmsCode;
//		price = '25,00';
//		nr = '92568';
//		brutto = '30,50';
//		body = 'AP EMAILCO lub AP.EMAILCO';
		buyType = 2;
	} else {
		price = parsePriceStr( domainPricesNetto.domainSms );
		nr = phoneCodes.domainSmsNr;
		brutto = parsePriceStr( domainPrices.domainSms );
		body = phoneCodes.domainSmsCode;
//		price = '6,00';
//		nr = '91968';
//		brutto = '7,62';
//		body = 'AP CODOMENA lub AP.CODOMENA';
		buyType = 1;
	}
	$('#jQ_price').html( price );
	$('#jQ_nr').html( nr );
	$('#jQ_brutto').html( brutto );
	$('#jQ_smsBody').html( body );	
	$('#jQ_cardBuyTypeHid').val( buyType );
	
}

