
$(function(){

	var minOtherFieldLen = 2;
	var maxOtherFieldLen = 32;
	var securityCodeLen = 4;
	var sentInfoMess = new InfoMessage();
	sentInfoMess.setMessageCont($("div.id_sent_info_mess"));
	var emptyDataMess = new InfoMessage();
	emptyDataMess.setMessageCont($("div.id_empty_data_mess"));
	
	$("a.id_reload_reg_ahctpac").click(function(event){
		event.preventDefault();
		reloadAhctpac();
	});
	
	$('a[class^="checkbox"]').click(function(event){
		event.preventDefault();
		$target = $(event.target);
		if(isCheckboxChecked($target)) {
			uncheck($target);
		} else {
			check($target);
		}
		
	});
		
	function check($checkbox) {
		$checkbox.removeClass("checkbox");
		$checkbox.addClass("checkbox_active");
	}
	
	function uncheck($checkbox) {
		$checkbox.removeClass("checkbox_active");
		$checkbox.addClass("checkbox");
	}
	
	function isCheckboxChecked($checkbox) {
		var checkboxClass = $checkbox.attr("class");
		if(checkboxClass.indexOf("active") != -1) {
			return true;
		}		
		return false;
	}
	
	$("a.id_send_request").click(function(event){ 
		event.preventDefault();		
		sentInfoMess.showLoader();
		
		if(!$("form.id_quote_request").valid()){
			sentInfoMess.hideLoader();
			return;
		};
		
		if(areAllSectionsEmpty()) {
			sentInfoMess.hideLoader();
			emptyDataMess.show();
			return;
		}
		
		var newSectionCheckedOptions = getChechedOptionsForSection($("div.id_new_section"));
		var newSectionOther = $('input[name="new_section_other"]').val();
		
		var upgradeSectionCheckedOptions = getChechedOptionsForSection($("div.id_upgrade_section"));
		var upgradeSectionOther = $('input[name="upgrade_section_other"]').val();
		
		var otherSectionCheckedOptions = getChechedOptionsForSection($("div.id_other_section"));
		var otherSectionOther = $('input[name="other_section_other"]').val();
			
		var email = $('input[name="email"]').val();
		var $securityCode = $('input[name="security_code"]');
		var securityCode = $securityCode.val();
		
		var data = {
				email: email,
				securityCode: securityCode,
				newSectionCheckedOptions: newSectionCheckedOptions,
				newSectionOther: newSectionOther,
				upgradeSectionCheckedOptions: upgradeSectionCheckedOptions,
				upgradeSectionOther: upgradeSectionOther,
				otherSectionCheckedOptions: otherSectionCheckedOptions,
				otherSectionOther: otherSectionOther
		};
		
		$.ajax({
			url: "/" + lang + "/index/sendrequest/",
			type: "POST",
			data: data,
			success:  function(data) {
				reloadAhctpac();
				sentInfoMess.hideLoader();
				if(data == "wrongSecurityCode") { 
					var $securityCodeErrorCont = $("div.id_error_security_code");
					$securityCodeErrorCont.html(translate('Not_valid_security_code'));
					$securityCodeErrorCont.css("display", "block");
					$securityCode.addClass("security_code_error");
					$securityCode.val("");
					return;
				} 
				emptyQuoteRequestFields();
				sentInfoMess.show();
			},
			error : function(){
				sentInfoMess.hideLoader();
			}
		});
	});
	
	function areAllSectionsEmpty() {
		var $activeCheckBoxes = $("a.checkbox_active");
		var newSectionOtherValue = $('input[name="new_section_other"]').val();
		var upgradeSectionOtherValue = $('input[name="upgrade_section_other"]').val();
		var otherSectionOtherValue = $('input[name="other_section_other"]').val();
		var areAllInputsEmpty = (newSectionOtherValue == "" && upgradeSectionOtherValue == "" && otherSectionOtherValue == "");
		return ($activeCheckBoxes.get(0) === undefined && areAllInputsEmpty);
	}
	
	function emptyQuoteRequestFields() {
		var $activeCheckboxes = $("a.checkbox_active");
		$activeCheckboxes.attr("class", "checkbox");
		
		var $inputFields = $("input", "form.id_quote_request");
		$inputFields.val("");
		
		var $invalidDataCont = $(".id_invalid_data");
		$invalidDataCont.html("");
	}
	
	function getChechedOptionsForSection($section) {
		var checkedMessages = new Array();
		var $checkedElems = $("a.checkbox_active", $section);
		$checkedElems.each(function(){
			var text = $(this).html();
			checkedMessages.push(text);
		});
		
		return checkedMessages;
	}
	
	$("form.id_quote_request").validate({
		errorClass: null,
		showErrors: function(errorMap, errorList) {
			$errorFields = $("div.error_field"); 
			$errorFields.css("display", "none");
			$errorFields.html("");
			var $inputElemConts = $("div.input_cont_error");
			$inputElemConts.removeClass("input_cont_error");	
			
	        if(errorList.length) {
	        	$firstErrorElem = $(errorList[0]['element']);
	        	var $errorCont = $firstErrorElem.siblings("div.error_field");
	        	$errorCont.html(errorList[0]['message']);
				$errorCont.css("display", "block");
				$firstErrorElemCont = $firstErrorElem.parent("div.input_cont");
				$firstErrorElemCont.addClass("input_cont_error");
	        }
	    },
		rules: {
			new_section_other: {
				minlength: minOtherFieldLen,
				maxlength: maxOtherFieldLen
			},
			upgrade_section_other: {
				minlength: minOtherFieldLen,
				maxlength: maxOtherFieldLen
			},
			other_section_other: {
				minlength: minOtherFieldLen,
				maxlength: maxOtherFieldLen
			},
			email: {
				required: true,
				email: true
			},
			security_code: {
				required: true,
				maxlength: securityCodeLen,
				minlength: securityCodeLen
			}
		},
		messages: {
			new_section_other : {
				minlength: translate('Min_field_size', minOtherFieldLen),
				maxlength: translate('Max_field_size', maxOtherFieldLen)
			}, 
			upgrade_section_other: {
				minlength: translate('Min_field_size', minOtherFieldLen),
				maxlength: translate('Max_field_size', maxOtherFieldLen)
			}, 
			other_section_other: {
				minlength: translate('Min_field_size', minOtherFieldLen),
				maxlength: translate('Max_field_size', maxOtherFieldLen)
			}, 
			email: {
				required: translate('Field_is_required'),
				email: translate('Valid_email')
			},
			security_code: {
				required: translate('Field_is_required'),
				maxlength: translate('Exact_field_size', securityCodeLen),
				minlength: translate('Exact_field_size', securityCodeLen),
			}
		}
	});
	
	
});



