/* INIT */
$(document).ready(
	function () {
		$('#zmz-txt').keyup(
			function () {
				CountSimbols('#zmz-txt', '.simbols-left', 143); 
			}
		).keydown(
			function () {
				CountSimbols('#zmz-txt', '.simbols-left', 143);
			}
		);
		$('#phone-form .submit').click (function () {
			$('#phone-form').submit();
			return false;
		});
		$('#phone-form').submit(
			function () {
				SendSMS();
				return false;
			}
		);
if ($('#ex3c').get(0))
{
	$('#ex3c')
		.jqm({
			trigger: false,
			overlay: 50,
			onShow: function(h) {
				/* callback executed when a trigger click. Show notice */
				h.w.css('opacity', 0.92).slideDown(); 
			},
			onHide: function(h) {
				/* callback executed on window hide. Hide notice, overlay. */
				h.w.slideUp("slow", function() { if (h.o) h.o.remove(); }
			);} 
	});
	}
		
	$('.captcha img').click(
		function () {
		return RegenerateCapcha ("");
		}
	);
	$('.captchax img').click(
		function () {
		return RegenerateCapcha ("friend");
		}
	);
		
	}
);
$(document).keydown( function( e ) {
  if( e.which == 27 && $.reallyvisible($('#ex3c'))==true) {  // escape, close box
	$('#ex3c').jqmHide();
  }
}); 
			/* FUNCTIONS */

function CountSimbols($source, $target, $maxlength) {
	var $target = $($target);
	var $source = $($source);
	var $text = $source.val().replace(/\r\n/gi,'\n');
	$text= $text.replace(/\n/gi,' ');
	if ($text.length > $maxlength) {
		$source.val($text.substring(0, $maxlength));
	} else {
/*		$source.val($text); */
	}
	$target.val(parseInt($maxlength - $source.val().length).toString());
}


/* Phone */

function SendSMS() {
	CountSimbols('#zmz-txt', '.simbols-left', 143);
	var $sms_number= jQuery.trim($('#zmz-num').val());
	var $sms_text= jQuery.trim($('#zmz-txt').val());
	var $sms_hash= jQuery.trim($('#hash').val());
	var $sms_error= false;
	if ($sms_number.length == 0) {
		ShowSMSError(1);
		$sms_error = true;
	} else if ($sms_text.length == 0) {
		ShowSMSError(2);
		$sms_error = true;
	} else if ($sms_hash.length != 4) {
		ShowSMSError(3);
		$sms_error = true;
	}
	if (!$sms_error) {
		SendSMSAjax($sms_number, $sms_text, $sms_hash);
	}
}

function SendSMSAjax($sms_number, $sms_text, $sms_hash) {
	jQuery.post(
		'/sms.inc.php',
		{'ajax': 1, 'number' : $sms_number, 'text' : $sms_text, 'hash' : $sms_hash},
		function ($data) {
			if ($data == 0) {
				/*
				$('#zmz-num').val('');
				$('#zmz-txt').val('');
				*/
				$('#hash').val('');
				CountSimbols('#zmz-txt', '.simbols-left', 143);
			}
			ShowSMSError($data);
			RegenerateCapcha("");
		}
	);
}

function ShowSMSError($error_code) {
	where = $('.phone .messages');
	err = $SMS_ERRORS_ARRAY[$error_code];
	if ($error_code=='0') $('.smsnotimg').attr('src','/images/ok.png');
	else $('.smsnotimg').attr('src','/images/stop.png');
	$('.jqmnContent').html(err);
	pos = $("#ok-widget").offset();
	$('#smsNoticeBox').css({'top' : pos.top, 'left' : pos.left});
	$('#ex3c').jqmShow();	
}

function RegenerateCapcha(what) {
try {
	if (what=='') {
		var param2 = "";
		$('.captcha img').attr('src', '/smuksimg.php?'+(new Date()).getTime());
		
		$('#hash').val('');
		} 
	else {
		var param2 = "";
		$('.captchax img').attr('src', '/smuksimg.php?'+(new Date()).getTime()+'&opt=friend');
		
		$('#hashFriend').val('');
		} 
	}
	catch (e) {}
			return false;
	}

function clearSmsForm () {
	$('#zmz-num').val('');
	$('#hash').val('');
	$('#zmz-txt').val('');
	$('.simbols-left').val(143);
} 