
function onFreeQuoteSubmitSuccess(response){
	//console.log(response);	
	if(response.errors.length==0){
		clearnFreeQuoteForm();
	}
	$('#free-quote-notice').html(response.message);

	forceRerenderingIE67();
}

/*When we change #free-quote-notice we change height of its parent .box-talling.
 *In this situating ie6-7 rerender only .box-talling but its sublings(#customize and #spec_offers)
 *need be rerendered too. Therefore we change their height to force ie rerender them too.
 *@author andrew
 **/
function forceRerenderingIE67() {
    if ($.browser.msie && ($.browser.version == '6.0' || $.browser.version == '7.0')){            
            if($('#customize').css('height')=='138px' && $('#spec_offers').css('bottom')=='155px'){
                $('#customize').css('height','139px');
                $('#spec_offers').css('bottom','156px');
            }
            else{
                $('#customize').css('height','138px');
                $('#spec_offers').css('bottom','155px');
            }
        }
}

function onFreeQuoteSubmitError(response){
	$('#free-quote-notice').html('Unknown error occurred while submitting the form. Please try later.');
}

function clearnFreeQuoteForm(){
	var $form = $('#free_quote');
	$form.find('input:text,textarea').val('');
	$form.find('select>option').attr('selected', false);
	$form.find('select').each(function(){
		$(':first', this).attr('selected', true);
	});
}

$('#free_quote').submit(function(){
	$.ajax({
		type: 'POST',
		url: '/get-a-free-quote',
		//data: $(this).serialize(),
		data: {
			'actform'    : 'sendmail',
			'y_name'     : this.y_name.value,
			'y_mail'     : this.y_mail.value,
			'y_phone'    : this.y_phone.value,
			'y_url'      : this.y_url.value,
			'y_position' : this.y_position.value,
			'y_services' : this.y_services.value,
			'template_id': this.template_id.value,
			'y_budget'   : this.y_budget.value,
			'y_comments' : this.y_comments.value
		},
		success: onFreeQuoteSubmitSuccess,
		error: onFreeQuoteSubmitError,
		dataType: "json"
	});
	return false;
});

if($.browser.msie && $.browser.version>=8){
	$('#free_quote input:text').keypress(function(e){
		if(e.which==13){
			e.stopPropagation();
			$(this.form).submit();
			return false;
		}
	});
}
