$(document).ready( function() {

    var houseId;
    var isiBlueHouse;

	$('.showA50').click(function() {
	    isiBlueHouse = $(this).parents('.action').find('input[name$=hndIsiBlueHouse]').val();
	    houseId = $(this).parents('.action').find('input[name$=hndHouseId]').val();
	    
	    SetInitialState();
	    
	    $('.A50Step2').find('input[id$=hdnHouseId]').val(houseId);	    
	});
	
	$('.p4ShowA50').click(function() {	    
	    houseId = $(this).parents('.p4').find('input[name$=hndHouseId]').val();
	    isiBlueHouse = $(this).parents('.p4').find('input[name$=hndIsiBlueHouse]').val();
	    
	    SetInitialState();
	    
	    $('.A50Step2').find('input[id$=hdnHouseId]').val(houseId);	    
	});	
	
	$('.A50step1 .together').click(function() {
	    $('.A50step1').hide();    
	    $('.A50Step1Samen').show();
	});	
	
	$('.createoffer').click(function() {
	    $('.A50step1').hide();    
	    $('.A50Step2').show();
    	    	    	
        SetHouseDetails(houseId);	    	
	});
	
	// prevent non-numeric characters in the offer price input field
	var nonNumericRE = /[^0-9]*/g;
	$('.A50Step2 input[name$=tboxOfferStep2]').bind('keyup', function(e) {
		var val = $(this).val();
		$(this).val(val.replace(nonNumericRE, ''));
	});
		
	$('.continuetostep3').click(function(e) {
		
		// let the validation take precedence
		setTimeout(function() {
			// than check the validation result
			if ( e.isDefaultPrevented() ) return;
			
			$('.A50Step2').hide();
			$('.A50Step3').show();
			
			//transfer data from step2 to step3	    
			$('.A50Step3 span[id$=lblOfferStep3]').text(formatNumber($('.A50Step2 input[id$=tboxOfferStep2]').val()));			
			
			var myBidToDate = $('.A50Step2 input[id$=tboxMyBidToDate]').val();
			var tranferDate = $('.A50Step2 input[id$=tboxTransferDate]').val();
					
			$('.A50Step3 span[id$=lblMyBidTo]').text(myBidToDate);
            $('.A50Step3 span[id$=lblTransferDate]').text(tranferDate);
			
			//lblFunding
			if ($('.A50Step2 input[id$=cboxFunding]').attr('checked'))
				$('.A50Step3 span[id$=lblFunding]').text('Ja');
			else 
				$('.A50Step3 span[id$=lblFunding]').text('Nee');
				
			//lblMortgage
			if ($('.A50Step2 input[id$=cboxMorgage]').attr('checked'))
				$('.A50Step3 span[id$=lblMortgage]').text('Ja');
			else 
				$('.A50Step3 span[id$=lblMortgage]').text('Nee');
							
			//lblEngineeringInspection
			if ($('.A50Step2 input[id$=cboxEngineeringInspection]').attr('checked'))
				$('.A50Step3 span[id$=lblEngineeringInspection]').text('Ja');
			else 
				$('.A50Step3 span[id$=lblEngineeringInspection]').text('Nee');
							
			//lblGoodsConditions
			if ($('.A50Step2 input[id$=cboxGoods]').attr('checked'))
				$('.A50Step3 span[id$=lblGoodsConditions]').text($('.A50Step2 textarea[id$=tboxGoodsCondition]').val());
			else
				$('.A50Step3 span[id$=lblGoodsConditions]').text('Nee');
					
			//lblOtherConditions
			if ($('.A50Step2 input[id$=cboxOtherConditions]').attr('checked'))
				$('.A50Step3 span[id$=lblOtherConditions]').text($('.A50Step2 textarea[id$=tboxOtherConditions]').val());
			else
				$('.A50Step3 span[id$=lblOtherConditions]').text('Nee');

//			//Mortgage Call
//			if ($('.A50Step2 input[id$=cboxMorgageAlreadyTalked]').attr('checked')) {
//				$('.A50Step3 .data_sent .yes').show();
//				$('.A50Step3 .data_sent .no').hide();
//			} else {
//				$('.A50Step3 .data_sent .yes').hide();
//				$('.A50Step3 .data_sent .no').show();
//			}
			
			//Your Information
			$('.A50Step3 span[id$=lblNameStep3]').text($('.A50Step2 input[id$=tboxNameStep2]').val());
			$('.A50Step3 span[id$=lblEmailStep3]').text($('.A50Step2 input[id$=tboxEmailStep2]').val());
			$('.A50Step3 span[id$=lblPhoneStep3]').text($('.A50Step2 input[id$=tboxPhoneNoStep2]').val());
		}, 1);
	});
	
    function SetInitialState() {

        $('.A50step1').hide();
        $('.A50step1a').hide();
	    $('.A50Step1Samen').hide();
	    $('.A50Step2').hide();
	    $('.A50Step3').hide();

	    if (a50HasPurchase)	{
	        $('.A50step1a').show();
	    } else {
            if (isiBlueHouse == 'true') {
                $('.A50Step2').show();
                SetHouseDetails(houseId);
            } else {
                $('.A50step1').show();
            }
	    }

	    //Set initial values of controls
        $('.A50Step2').find('span[id$=lblHouseAddress]').html('');            
        $('.A50Step2').find('span[id$=lblPostCode]').html('');
        $('.A50Step2').find('span[id$=lblCity]').html('');    
        $('.A50Step2').find('span[id$=lblSellingPartyName]').html('');
        $('.A50Step2').find('span[id$=lblSellingPartyPhone]').html('');	

		$('.A50Step2 input[name$=tboxOfferStep2]').val('');
		$('.A50Step2 input[name$=tboxMyBidToDate]').val('');
		$('.A50Step2 input[name$=tboxTransferDate]').val('');
		
		$('.A50Step2 input[id$=cboxFunding], .A50Step2 input[id$=cboxMorgage], .A50Step2 input[id$=cboxEngineeringInspection], ' + 
			'.A50Step2 input[id$=cboxGoods], .A50Step2 input[id$=cboxOtherConditions], .A50Step2 input[id$=cboxMorgageAlreadyTalked]')
			.each(function(){
				if ( this.checked ) {
					this.checked = false;
					$(this).parent().removeClass('checked');
					$(this).parents('dt').next().hide();
				}
			});

		$('.A50Step2 input[name$=tboxGoodsCondition]').val('').html('');
		$('.A50Step2 input[name$=tboxOtherConditions]').val('').html('');
    	
        $('.A50Step3').find('span[id$=lblHouseAddressStep3]').html('');
        $('.A50Step3').find('span[id$=lblPostCodeStep3]').html('');
        $('.A50Step3').find('span[id$=lblCityStep3]').html('');
        
        $('.A50Step3').find('span[id$=lblSellingPartyNameStep3]').html('');
        $('.A50Step3').find('span[id$=lblSellingPartyPhoneStep3]').html('');
    }	
    
    function SetHouseDetails(houseId)
    {    
        var webServiceInput = { "houseId": houseId };
        var jsonStr = JSON.stringify(webServiceInput);
        $.ajax({
            type: 'POST',
            url: '/_vti_bin/PortalHousesService.asmx/GetHouseDetails',
            data: jsonStr,
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            success: function(msg) {
                //A50Step2
                $('.A50Step2').find('img[id$=imgHouseThumbnail]').attr("src", msg.d[3]);
                $('.A50Step2').find('span[id$=lblHouseAddress]').text(msg.d[0]);
                $('.A50Step2').find('span[id$=lblPostCode]').text(msg.d[1]);
                $('.A50Step2').find('span[id$=lblCity]').text(msg.d[2]);
                $('.A50Step2').find('span[id$=lblHousePriceStep2]').text('\u20AC ' + msg.d[8]);
                
                $('.A50Step2').find('span[id$=lblSellingPartyName]').text(msg.d[4]);
                $('.A50Step2').find('span[id$=lblSellingPartyPhone]').text(msg.d[5]);
                
                $('.A50Step2').find('#lblSalesCondition').text(' ' + msg.d[6]);

                //A50Step3
                $('.A50Step3').find('img[id$=imgHouseThumbnailStep3]').attr("src", msg.d[3]);
                $('.A50Step3').find('span[id$=lblHouseAddressStep3]').text(msg.d[0]);
                $('.A50Step3').find('span[id$=lblPostCodeStep3]').text(msg.d[1]);
                $('.A50Step3').find('span[id$=lblCityStep3]').text(msg.d[2]);
                $('.A50Step3').find('span[id$=lblHousePriceStep3]').text('\u20AC ' + msg.d[8]);
                
                $('.A50Step3').find('span[id$=lblSellingPartyNameStep3]').text(msg.d[4]);
                $('.A50Step3').find('span[id$=lblSellingPartyPhoneStep3]').text(msg.d[5]);     
                
                $('.A50Step3').find('span[id$=lblSalesConditionStep3]').text(' ' + msg.d[6]);
                
                if (msg.d[7] == 'iblue') {
                    $('.A50Step3 .step3continue').show();
                    $('.A50Step3 .step3createpdf').hide();            
                } else {
                    $('.A50Step3 .step3continue').hide();
                    $('.A50Step3 .step3createpdf').show();            
                }
 
                //If house is not iblue then "hypotheek" section will not be visible
                if (isiBlueHouse == 'false') {
                    $('.A50Step2 .hypotheek').hide();
                }
            }
        });       
    }

    var formatNumber = function(val) {
        val += '';
        x = val.split(',');
        x1 = x[0];
        x2 = x.length > 1 ? ',' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + '.' + '$2');
        }
        return x1 + x2;
    }    
});
