$(document).ready(function(){

	$.validator.setDefaults({
		ignoreTitle:true,
		errorClass: 'input-invalid',
		validClass: 'input-valid',
		errorElement: 'div',
		onkeyup: false
	});

	function trackConv(google_conversion_id, google_conversion_label) {
        image = new Image(1,1);
        //image.src = "http://www.googleadservices.com/pagead/conversion/"+google_conversion_id+"/?label="+google_conversion_label+"&script=0&value=100&format=2&language=lt";
	    image.src = "http://www.googleadservices.com/pagead/conversion/"+google_conversion_id+"/?value=1&amp;label="+google_conversion_label+"&amp;guid=ON&amp;script=0";
    }

	if($('#contact_form').length > 0) {

		$('#contact_form').validate({
			rules: {
				zinute : "required",
				telefonas : "required",
				email: {
					required: true,
					email: true
				}
			},
			errorPlacement: function(error, element) {
				if($(element).attr('type') == 'radio' || $(element).attr('type') ==  'checkbox'){
					error.insertAfter($(element).closest('fieldset'));
				}else{
					error.insertAfter(element);
				}
			},
			submitHandler: function(form) {
				$.post(
					'/contactus/',
					$(form).serialize(),
					function(data){
						$('#contact_form_holder').html(data);
						//append = '<script type="text/javascript">/* <![CDATA[ */var google_conversion_id = 1040386411;var google_conversion_language = "lt";var google_conversion_format = "2";var google_conversion_color = "ffffff";var google_conversion_label = "K4ULCP_B3wEQ65KM8AM";var google_conversion_value = 100;/* ]]> */</script><script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script>';
						//$('head').append(append);
						trackConv("1040386411", "K4ULCP_B3wEQ65KM8AM");
					}
				);
			}
		});

	}

	if($('#contactus').length > 0 ) {
		var dtext = '';

		$('#contactus').submit(function(e){
			e.preventDefault();
			//$('#contact-block-form-hld').hide();
			
			if($('#contactus input[name=phone]').val().length < 7) {
				if(dtext == ''){
					dtext = $('#we-call').html();
				}
				$('#contactus #we-call').html('Klaidingas telefono numeris!').css('color', '#ff0000');
				$('#contactus input[name=phone]').focus();

				$('#contactus input[name=phone]').keyup(function(){
					if($('#contactus input[name=phone]').val().length > 7 && $('#contactus input[name=phone]').val() != dtext){
						$('#contactus #we-call').html(dtext).removeAttr('style');
					}
				});

			} else {
				$.post(
					'/contactusphone/',
					$('#contactus').serialize(),
					function(data){
						$('#contact-block').html(data);
						//append = '<script type="text/javascript">/* <![CDATA[ */ var google_conversion_id = 1040386411; var google_conversion_language = "lt"; var google_conversion_format = "2"; var google_conversion_color = "ffffff"; var google_conversion_label = "K4ULCP_B3wEQ65KM8AM"; var google_conversion_value = 100;/* ]]> */</script><script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script>';
						//$('head').append(append);
						trackConv("1040386411", "K4ULCP_B3wEQ65KM8AM");
					}
				);
				return false;
			}
		})

	}

	if($('#order_form').length > 0) {

		//define varibles
		var main_price = 0;
		var design_price = 0;
		var paymentperiod_price = 0;
		var total = 0;
		var pvm = 0;
		var total_whit_pvm = 0;
		var discount = 0;

		//rent activation
		activateRent = function() {
			main_price = priceRent;
			$('#pdf').attr('href', '/i/File/Sutartis_parduotuve_nuoma_2011.pdf');
			$('#typerent').attr('class','price-block1 certain-left');
			$('#typebuy').attr('class','price-block2 opacity');
			$('#typeleasing').attr('class','price-block3 opacity');
			$('#certain-price').show();
			$('#rentperiod').css('display', 'inline');
			$('#buttonRent').hide();
			$('#buttonBuy').show();
			$('#buttonLeasing').show();
			$('#order-form-submit').hide();
			$('#servicetype').val('rent');
			$('#order_form').validate().resetForm();
			check_all();
		}

		//buy activation
		activateBuy = function () {
			paymentperiod_price = priceBuy;
			$('#pdf').attr('href', '/i/File/Sutartis_parduotuve_isigijimas_2011.pdf');
			$('#typerent').attr('class','price-block1 opacity');
			$('#typebuy').attr('class','price-block2 certain-center');
			$('#typeleasing').attr('class','price-block3 opacity');
			$('#certain-price').show();
			$('#rentperiod').hide();
			$('#buttonRent').show();
			$('#buttonBuy').hide();
			$('#buttonLeasing').show();
			$('#order-form-submit').hide();
			$('#servicetype').val('buy');
			$('#order_form').validate().resetForm();
			check_all();
		}

		//leasing activation
		activateLeasing = function() {
			paymentperiod_price = priceLeasing;
			$('#pdf').attr('href', '/i/File/Sutartis_parduotuve_lizingas_2011.pdf');
			$('#typerent').attr('class','price-block1 opacity');
			$('#typebuy').attr('class','price-block2 opacity');
			$('#typeleasing').attr('class','price-block3 certain-right');
			$('#certain-price').show();
			$('#rentperiod').hide();
			$('#buttonRent').show();
			$('#buttonBuy').show();
			$('#buttonLeasing').hide();
			$('#order-form-submit').hide();
			$('#servicetype').val('leasing');
			$('#order_form').validate().resetForm();
			check_all();
		}

		//count price
		update_price = function() {
			total = design_price + paymentperiod_price - discount;
			pvm = total * vat;
			total_whit_pvm = total + pvm;
			if(discount > 0) {
				$('span#discount').html(discount.toFixed(2));
				$('#discount_holder').show();
			}else{
				$('#discount_holder').hide();
			}
			$('span#total').html(total.toFixed(2));
			$('span#pvm').html(pvm.toFixed(2));
			$('span#total_whit_pvm').html(total_whit_pvm.toFixed(2));
		}

		//checks all radio buttons on load
		check_all = function() {
			check_designchoise();
			check_paymentperiod();
			update_price();
			change_client_type();
		}

		//count design price
		check_designchoise = function() {
			if($('input[name=designchoise]:checked:visible').val() == 'buy'){
				design_price = priceDesign;
			}else{
				design_price = 0;
			}
		}

		//count main price, discount
		check_paymentperiod = function() {
			val = $('input[name=paymentperiod]:checked:visible').val();
			if(val){
				paymentperiod_price = main_price * val;
				discount = paymentperiod_price * window['discountm' + val];
			}
//			if(val == 3){
//				paymentperiod_price = main_price * val;
//				discount = 0;
//			}else if(val == 6) {
//				paymentperiod_price = main_price * val;
//				discount = paymentperiod_price * 0.10;
//			}else if(val == 9) {
//				paymentperiod_price = main_price * val;
//				discount = paymentperiod_price * 0.15;
//			}else if(val == 12) {
//				paymentperiod_price = main_price * val;
//				discount = paymentperiod_price * 0.20;
//			}else{
//				discount = 0;
//				paymentperiod_price = main_price;
//			}
		}

		//change type person/company
		change_client_type = function() {
			client_type = $('input[name=clienttype]:checked').val();
			if(client_type == 'company'){
				$('#person').hide();
				$('#company').show();
				$('#order-form-submit').show();
			}else if(client_type == 'person') {
				$('#company').hide();
				$('#person').show();
				$('#order-form-submit').show();
			}
		}
		//
		check_agreement = function() {
			$('#order_form #order-form-submit .fl div').remove();
			if(!$('#order_form #agreementcheck').is(":checked")){
				$('#order_form #order-form-submit .fl').append('<div><em class="invalid">' + $.validator.messages.required +  '</em></div>');
				return false;
			}else{
				return true;
			}
		}

		//change design
		$('input[name=designchoise]').click(function(){
			check_designchoise();
			update_price();
		});

		//chnage payment period
		$('input[name=paymentperiod]').click(function(){
			check_paymentperiod();
			update_price();
		});

		//change client type
		$('input[name=clienttype]').click(function(){
			change_client_type();
		});

		//validate checkbox
//		$('#order_form').submit(function(){
//			if(check_agreement() && $('#order_form').valid()) {
//				$(this).submit();
//			}else{
//				return false;
//			}
//		});

//		$('#order_form #agreementcheck').change(function(){
//			check_agreement();
//		});

		//validate form
		$('#order_form').validate({
			rules: {
				//company validate
				companyname: {required: "div#company:visible"},
				companycode: {required:"div#company:visible"},
				//companyvat: {required:"div#company:visible"},
				companyaddress: {required:"div#company:visible"},
				companycity: {required:"div#company:visible"},
				companypersonname: {required:"div#company:visible"},
				companyposition: {required:"div#company:visible"},
				companypersonphone: {required:"div#company:visible"},
				//companypersonfax: {required:"div#company:visible"},
				companypersonemail: {
					required: "div#company:visible",
					email: "div#company:visible"
				},
				//person validate
				personname: {required:"div#person:visible"},
				personcode: {required:"div#person:visible"},
				personaddress: {required:"div#person:visible"},
				personcity: {required:"div#person:visible"},
				personphone: {required:"div#person:visible"},
				personemail: {
					required: "div#person:visible",
					email: "div#person:visible"
				},

				agree: "required",
				buydomain: "required",
				designchoise: "required",
				clienttype: "required",
				paymentperiod: {required:"#rentperiod:visible"}
			},
			errorPlacement: function(error, element) {
				if($(element).attr('type') == 'radio' || $(element).attr('type') ==  'checkbox'){
					error.insertAfter($(element).closest('fieldset'));
				}else{
					error.insertAfter(element);
				}
			}
		});

		//check default values on load
		check_all();
	}

	if($('#payment_form').length > 0){

		$('#payment_form ul li a').click(function(e){
			if($(this).attr('href') == '#order'){
				e.preventDefault();
			}
			$('#payment_form ul li a.selected').removeClass('selected');
			$(this).addClass('selected');
		});

		setpayment = function(method){
			$('#paymet').val(method);
		}

	}
	
});
