
			/* INIT */

$(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.html(parseInt(maxlength - source.val().length).toString());
}

/* WEB page */

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;
	$('.sms .messages').empty();
	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 != 3) {
		ShowSMSError(3);
		sms_error = true;
	}
	if (!sms_error) {
		SendSMSAjax(sms_number, sms_text, sms_hash);
	}
}

function SendSMSAjax(sms_number, sms_text, sms_hash) {
	$("#contentLoading").show();  
	jQuery.post(
		'/sms.inc.php', 
		{'ajax': 1, 'number' : sms_number, 'text' : sms_text, 'hash' : sms_hash}, 
		function (data) {
			if (data == 0) {
				var sms_id = 'sms-'+(new Date()).getTime();
				$('#hash').val('');
				CountSimbols('#zmz-txt', '.simbols-left', 143);
				$('.hosted.win').prepend('<div id="'+sms_id+'"><h2><span>'+SMS_TEXT_ARRAY[0]+': '+sms_number+' <a href="javascript:;" onclick="return RemoveSMS(\''+sms_id+'\');">&nbsp;</a></span></h2><div class="capsule"><div id="p_'+sms_id+'"></div></div>');
				$('#p_'+sms_id).text(sms_text);
			}
			ShowSMSError(data);
			$("#contentLoading").fadeOut("fast");
			RegenerateCapcha();
		}
	);
}

function ShowSMSError(error_code) {
	error_code = Number(error_code);
	var where = $('.sms .messages');
	var err = '<strong'+(error_code == 0 ? ' class="good"' : '')+'>'+SMS_ERRORS_ARRAY[error_code]+'</strong>';
	where.html("");
	where.html(err);
}

function RemoveSMS(sms_id) {
	$('#'+sms_id).remove();
	return false;
}

/* Phone */

function SendSMS_Phone() {
	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 mms_img = jQuery.trim($('#mms-img-val').val());
	var who = jQuery.trim($('#who').val());
	var subject = jQuery.trim($('#mms-subject').val());
	var sms_error= false;
	if (sms_number.length == 0) {
		ShowSMSError_Phone(1);
		sms_error = true;
	} else if (sms_text.length == 0 && who == 'sms') {
		ShowSMSError_Phone(2);
		$sms_error = true;
	} else if (sms_hash.length != 3) {
		ShowSMSError_Phone(3);
		sms_error = true;
	} else if (mms_img.length == 0 && who == 'mms') {
		ShowSMSError_Phone(13);
		sms_error = true;
	}
	if (!sms_error) {
		SendSMSAjax_Phone(sms_number, sms_text, sms_hash, mms_img, who, subject);
	}
}

	function SendSMSAjax_Phone(sms_number, sms_text, sms_hash, mms_img, who, subject) {
		$("#contentLoading").show();  
		jQuery.post(
			'/sms.inc.php', 
			{'ajax': 1, 'number' : sms_number, 'text' : sms_text, 'hash' : sms_hash, 'img' : mms_img, 'who' : who, 'subject' : subject},
			function (data) {
				if (data == 0) {
					$('#hash').val('');
					CountSimbols('#zmz-txt', '.simbols-left', 143);
				}
				ShowSMSError_Phone(data);
				$("#contentLoading").fadeOut("fast");
				RegenerateCapcha();
			}
		);
	}

	ShowSMSError_Phone = function(error_code) {
		error_code = Number(error_code);
		if (error_code == 0 && $('#who').val() == 'mms') { error_code = 14; }
		var where = $('.phone .messages');
		var err = SMS_ERRORS_ARRAY[error_code];
		if (error_code == 0 || error_code == 14) { $('#smsnotimg').attr('src','/images/design/ok.png'); }
		else { $('#smsnotimg').attr('src', '/images/design/stop.png'); }
		$('.jqmnContent').html(err);
		$('#ex3c').jqmShow();	
	}

	changeSMSorMMS = function() {
		var link = $(this).attr('rel');
		$('#who').val(link);
		if (link == 'sms') {
			$('.sms-block').css('display', 'block'); $('.mms-block').css('display', 'none'); $('#sms-active').css('font-weight', 'bold'); $('#mms-active').css('font-weight', 'normal');
		} else if (link == 'mms') {
			$('.sms-block').css('display', 'none'); $('.mms-block').css('display', 'block'); $('#sms-active').css('font-weight', 'normal'); $('#mms-active').css('font-weight', 'bold'); 
		}
		return false;
	}

	/* Kad dokuments ielādējies */
	$(document).ready(function() { 

		$('.title a').click(changeSMSorMMS);
		$('.title').css('visibility', 'visible');
		
		$('#zmz-txt').keyup(
			function () {
				CountSimbols('#zmz-txt', '.simbols-left', 143); 
			}
		).keydown(
			function () {
				CountSimbols('#zmz-txt', '.simbols-left', 143);
			}
		);
		
		$('#sms-form').submit(function () { SendSMS(); return false; } );
		
		$('#phone-form').submit(function () { SendSMS_Phone(); return false; } );

		if ($('#ex3c').get(0)) {
			$('#ex3c').jqm({
				trigger: false,
				overlay: 50,
				onShow: function(h) { h.w.css('opacity', 0.92).slideDown(); /* callback executed when a trigger click. Show notice */ },
				onHide: function(h) { h.w.slideUp("slow", function() { if (h.o) h.o.remove(); } /* callback executed on window hide. Hide notice, overlay. */ );} 
			});
		}

		var ex2 = $('#ex2').html();
		if (ex2 != null) {
		  $('#ex2').jqm({
				ajax: '/ajax/mms_galerija.php?act=getGallery', 
				trigger: false,
				overlay: 50,
				onShow: function(h) { h.w.css('opacity', 0.92).slideDown(); /* callback executed when a trigger click. Show notice */ },
				onHide: function(h) { h.w.slideUp("slow", function() { if (h.o) h.o.remove(); } /* callback executed on window hide. Hide notice, overlay. */ );} 
			});
		}

		$('input#mms-img')
			.keyup(function(){$('#ex2').jqmShow()})
			.keydown(function(){$('#ex2').jqmShow()})
			.click(function(){$('#ex2').jqmShow()
		});

});
