// variable for subscriberpublsher/writetopublisherSuccess
subscriberText_changed = false;

/*
	 *  if agree not checked form do not submit
	 */
	function agree()
	{
		if( $('#agree').attr('checked') )
		{	
			$('#register').submit();
		}
		else
		{
			alert('Nepažymėta varnelę Su taisyklėmis susipažinau ir sutinku');
			return false;
		}
	}
	
	/*
	 * if order is/not present
	 */
	function ifPresent()
	{
		if( $('#isPresent').attr('checked') )
		{
			$('#table_present').show();
		}
		else
		{
			$('#table_present').hide();
		}
	}

	/*
	 * if foreign country
	 */
	function ifForeignCountry()
	{
		if( $('#country').val() != 'Lietuva' )
		{
			$('#div_foreign').show();
			$('#div_fatherland').hide();
		}
		else
		{
			$('#div_foreign').hide();
			$('#div_fatherland').show();
		}
	}

	
	/*
	 * if address enter directli
	 */
	function ifAddress(id)
	{
		fill = '';
		if( id != '' )
		{
			fill = '_' + id;
		}

		if( $('#isAddress' + fill).attr('checked') )
		{
			$('#div_foreign' + fill).show();
			$('#address' + fill).attr('disabled', '');
			$('#div_fatherland' + fill).hide();
		}
		else
		{
			$('#div_foreign' + fill).hide();
			$('#address' + fill).attr('disabled', 'disabled');
			$('#div_fatherland' + fill).show();
		}
	}

	
	/*
	 * fill address in the form with customer address 
	 */
	function receiverAsCustomer(id)
	{
		if( $('#receiver_as_customer_' + id).attr('checked') )
		{
			if( $('#client_status_id_1').attr('checked') )
			{
				$('#html_company_' + id).show();
			}
			//$('#company_' + id).attr('disabled', '');
			$('#company_' + id).val($('#company').val());
			$('#name_' + id).val($('#name').val());
			$('#surname_' + id).val($('#surname').val());
			$('#phone_' + id).val($('#phone').val());
			$('#email_' + id).val($('#email').val());
			
			if( $('#city_region_' + id).val() != $('#city_region').val() )
			{
				$('#city_region_' + id).val($('#city_region').val());

				setSelect_vietove($('#city_region_' + id).val(), 'vietove_' + id, $('#vietove').val());
				$('#vietove_' + id).attr('disabled', 'disabled');

			}
			
			//$('#city_region_' + id).val($('#city_region').val());
			//$('#vietove_' + id).val($('#select_vietove').val());
			$('#street_' + id).val($('#street').val());
			$('#house_' + id).val($('#house').val());
			$('#flat_' + id).val($('#flat').val());
			$('#isAddress_' + id).attr('checked', $('#isAddress').attr('checked'));
			$('#address_' + id).val($('#address').val());
			$('#zip_' + id).val($('#zip').val());
			
			$('#name_' + id).attr('disabled', 'disabled');
			$('#surname_' + id).attr('disabled', 'disabled');
			$('#phone_' + id).attr('disabled', 'disabled');
			$('#email_' + id).attr('disabled', 'disabled');
			$('#city_region_' + id).attr('disabled', 'disabled');
			$('#vietove_' + id).attr('disabled', 'disabled');
			$('#street_' + id).attr('disabled', 'disabled');
			$('#house_' + id).attr('disabled', 'disabled');
			$('#flat_' + id).attr('disabled', 'disabled');
			$('#isAddress_' + id).attr('disabled', 'disabled');
			$('#zip_' + id).attr('disabled', 'disabled');
			ifAddress(id);
		}
		else
		{
			$('#html_company_' + id).hide();
			$('#name_' + id).attr('disabled', '');
			$('#surname_' + id).attr('disabled', '');
			$('#phone_' + id).attr('disabled', '');
			$('#email_' + id).attr('disabled', '');
			$('#city_region_' + id).attr('disabled', '');
			$('#vietove_' + id).attr('disabled', '');
			$('#street_' + id).attr('disabled', '');
			$('#house_' + id).attr('disabled', '');
			$('#flat_' + id).attr('disabled', '');
			$('#isAddress_' + id).attr('disabled', '');
			$('#zip_' + id).attr('disabled', '');
		}
	}

	/*
	 * set option to select vietoves from ajax 
	 */
	function setSelect_vietove(savivaldybeId, select_vietove_id, select_vietove_value, publicationId)
	{
		//alert(publicationId);
		
		if(
			savivaldybeId == 103
			 || savivaldybeId == 104
			 || savivaldybeId == 113
			 || savivaldybeId == 73
			 || savivaldybeId == 66 )
		{
			$('#' + select_vietove_id).val('Pasirinkite...');
			$('#' + select_vietove_id).attr('disabled','disabled');
			$('#html_' + select_vietove_id).hide();
			return;
		}
		
		//alert('savivaldybeId=' + savivaldybeId + ' select_vietove_id=' + select_vietove_id + ' select_vietove_value=' + select_vietove_value);
		
		//savivaldybeId = $('#city_region').val();
		
		//selectedId = $('# ' + select_vietove).val();
		
		selectedId = select_vietove_value || $('# ' + select_vietove_id).val();
		
		//alert('selectedId=' + selectedId);
		
		jQuery.ajax({
				type: 'POST'
				, url: '/subscriber/getVietovesBySavivaldybeId?savivaldybeId=' + savivaldybeId + '&selectedId=' + selectedId + '&publicationId=' + publicationId
				, success: function(vietoves_options){
								//alert(vietoves_options);
								oVietoves = eval("(" + vietoves_options + ")");
								
								$('#' + select_vietove_id).empty();
								
								$.each(oVietoves,function(index,data){
									        	  $('#' + select_vietove_id).append($(data));
								});
								
								$('#' + select_vietove_id).attr('disabled','');
								$('#html_' + select_vietove_id).show();
							}
				});
	}
	
	/*
	 * use/unuse coupon
	 */
	function coupon_chk()
	{
		if( $('#chk_coupon').attr('checked') )
		{
			// coupon use
			$('#coupon_kodas').show();
			$('#coupon_kodas :input').attr('disabled', '');
			$("#html_link_use").show();
		}
		else
		{
			// coupon unuse
			$('#coupon_kodas').hide();

			jQuery.ajax({
				type: 'POST'
				, url: '/test.php/coupon/coupon_use?number=' + $('#coupon_number').val()
				, success: function(couponData){
											return coupon_chk_return(couponData);
												}
			});
		}
	}

	function coupon_chk_return(couponData)	
	{
	// responseText values:
	// 1 - kuponas su nurodytu uid nerastas
	// 2 - kuponas nepanaudotas

		//alert(couponData);
		
		a_return = couponData.split('|');					// ok|number|price
		$("#html_coupon_mess").html('');
		if (a_return[0] != 'ok')
		{
			//alert(couponData);
			//alert(a_return[0]);
			$("#html_coupon_mess").html(a_return[0]);
			$("#html_coupon_mess").show();
			return;
		}

		//in_coupon_number = a_return[1];
		in_price  = a_return[2];
		
		$("#id_coupon_value").html('');
		$("id_tr_apmokejimui").hide();
		
		//$("#id_tr_coupon").show();
		$("#html_coupon_mess").html('');
		$("#html_coupon_mess").hide();
		$('#coupon_number').val('');
//alert(bendra_suma);
//alert(in_price);
		
/*
		if( ($('#totalSum').val() - in_price) > 0 )	
		{
			totalSum = (parseFloat($('#totalSum').val(), 10) - parseFloat(in_price, 10)).toFixed(2);
		}
		else
		{
			totalSum = 0;
		}

		aTotalSum = totalSum.toString().split('.');
		if (aTotalSum.length > 1)
			totalSum = aTotalSum[0] + '.' + aTotalSum[1];
		else
			totalSum = aTotalSum[0] + '.00';

//			alert(in_bendra_suma);
		
		$("html_for_payment").html('Apmokėjimui: ' + totalSum . ' LT');
		$("html_for_payment").show();
		//$("html_for_payment_lt").html('Lt');
*/
		return true;
	}

	
	function coupon_use()	
	{
		jQuery.ajax({
			type: 'POST'
			, url: '/test.php/coupon/coupon_use?uid1=' + $('#coupon_uid1').val()
											 + '&uid2=' + $('#coupon_uid2').val()
											 + '&uid3=' + $('#coupon_uid3').val()
											 + '&uid4=' + $('#coupon_uid4').val()
			, success: function(couponData){
										return coupon_use_return(couponData);
											}
		});
	 }
	 

	function coupon_use_return(couponData)
	{
	// couponData values:
	// 1 - kuponas su nurodytu uid nerastas
	// 2 - kuponas jau panaudotas
	// split |
	// 0 - ok
	// 1 - coupon number
	// 2 - price

	//alert(responseText);

		//alert(couponData);
		
		a_return = couponData.split('|');					// ok|number|price
		$("#html_coupon_mess").html('');
		if (a_return[0] != 'ok')
		{
			//alert(couponData);
			//alert(a_return[0]);
			$("#html_coupon_mess").html(a_return[0]);
			$("#html_coupon_mess").show();
			return;
		}

		//in_coupon_number = a_return[1];
		in_price  = a_return[2];
		
		$("#id_coupon_value").html(a_return[2]);
		$("id_tr_apmokejimui").show();
		
		//$("#id_tr_coupon").show();
		$("#html_coupon_mess").html('Kupono numeris ' + a_return[1] + ' - ' + a_return[2] + ' LT');
		$("#html_coupon_mess").show();
		$('#coupon_number').val(a_return[1]);
		$("#html_link_use").hide();
		
//alert(bendra_suma);
//alert(in_price);

		if( ($('#totalSum').val() - in_price) > 0 )	
		{
			totalSum = (parseFloat($('#totalSum').val(), 10) - parseFloat(in_price, 10)).toFixed(2);
		}
		else
		{
			totalSum = 0;
		}

		aTotalSum = totalSum.toString().split('.');
		if (aTotalSum.length > 1)
			totalSum = aTotalSum[0] + '.' + aTotalSum[1];
		else
			totalSum = aTotalSum[0] + '.00';

//			alert(in_bendra_suma);
		
		$("#html_for_payment").html('Apmokėjimui: ' + totalSum + ' LT');
		$("#html_for_payment").show();
		//$("html_for_payment_lt").html('Lt');

		return true;
	}

	/*
	 * call from subscriberpublisher/writetopublisherSuccess
	 */
	function check_form_topublisher(txtbox)
	{
		if( subscriberText_changed == false )
		{
			alert ('Būtina padaryti pakeitimus laiške, kad laiškas būtų išsiųstas.');
			return false;
		}
		
		if( txtbox == '' )
		{
			return false;
		}
		
		jQuery("#form_topublisher").submit();
	}

	/*
	 * call from userbankpending
	 * 
	 * @param string:
	 * 		  - delete
	 * 		  - payment
	 */
	function ifSelectCart(type)
	{
		cartSelectedLength = $("input:checked").length;
		//alert(cartSelectedLength);
		if( cartSelectedLength > 1 && type == 'payment' )
		{
			alert("Apmokėjimui pasirinkite tik vieną krepšelį");
			return false;
		}
		
		if( cartSelectedLength < 1 )
		{
			alert("Pasirinkite krepšelį");
			return false;
		}
		
		if( type == 'payment' )
		{
			$('#bankpending_form').attr('action', 'cart/searchpaymethod');
		}
		else
		{
			if( confirm('Ar tikrai norite tęsti?') == false )
			{
				return false;
			}
			
			$('#bankpending_form').attr('action', 'cart/deleteFromUser');
		}
		
		$('#bankpending_form').submit();
	}
	
