var smsInfo = new storeCheckData();

$('document').ready(function() {
	decreaseCountdowns();
});

function decreaseCountdowns()
{
	if ($('.countdown').length > 0) {
		var decreased = false;
		$('.countdown').each(function() {
			var value = parseInt($(this).html());
			if (value > 0 || $(this).hasClass('canBeNegative')) {
				$(this).html(value - 1);
				decreased = true;
			}
		});
		if (decreased) {
			var timer = setTimeout('decreaseCountdowns()', 1000);
		}
	}
}

function initSmsCounter(asciiMax, utfMax, isFree)
{
	checkSMSLength($("#smsText"), asciiMax, utfMax, isFree, null);
	$("#smsText").keyup(function(event) {
		checkSMSLength($(this), asciiMax, utfMax, isFree, event);
	});
}

function checkSMSLength(elem, asciiLength, utfLength, unregistered, e) {
	if (e && e.altKey) {
		smsInfo.assign(elem, asciiLength, utfLength, unregistered);
		var t = setTimeout('checkSMSLength(smsInfo.elem, smsInfo.asciiLength, smsInfo.utfLength, smsInfo.unregistered)', 200);
		return;
	}
	var text = elem.val();
	var currentLength = asciiLength;
	if (typeof elem.charsLeft == 'undefined') {
		elem.charsLeft = $("#text_reminder");
		elem.submitButton = $("#smsSubmit");
	}
	if (utfLength == 0) {
		text = text.utf2ascii();
		text = text.utf2gsm7();
		elem.val(text);
	} else if (text.isUtf()) {
		currentLength = utfLength;
	}
	if (unregistered) {
		if (typeof elem.adFrame == 'undefined') {
			elem.adFrame = $('#smsAdInfo');
		}
		if (text.length > 0) {
			elem.adFrame.html(text + '<span class="show">' + strings.appendedAd + '</span>');
		} else {
			elem.adFrame.html('');
		}
	}
	if (text.length > currentLength && !elem.hasClass("smsLong")) {
		elem.addClass("smsLong");
		elem.charsLeft.addClass("smsLong");
		elem.submitButton.addClass("disabledButton");
		elem.submitButton.attr('DISABLED', 'DISABLED');
	} else if (text.length <= currentLength && elem.hasClass("smsLong")) {
		elem.removeClass("smsLong");
		elem.charsLeft.removeClass("smsLong");
		elem.submitButton.removeClass("disabledButton");
		elem.submitButton.removeAttr('DISABLED');
	}
	elem.charsLeft.html(elem.charsLeft.html().replace(/\-?\d+/, (currentLength - text.length)));
	elem.before = text;
}

function checkSMS(smsForm, send, preview) {
	errmsg = "";
	adress = document.getElementById('target');
	if (!preview && adress.value.trim() == "") {
		errmsg += strings.emptyReciever + "\n";
		adress.focus();
	}
	text = document.getElementById('text');
	if (!preview && text.value.trim() == "") {
		errmsg += strings.emptyMessage + "\n";
		text.focus();
	}
	if (send && text.className.match("smsLong")) {
		errmsg += strings.smsTooLong + "\n";
	}
	code = document.getElementById('code');
	if (code.value.trim() == "" && send) {
		errmsg += strings.emptyCaptcha + "\n";
		code.focus();
	}
	if (errmsg == "") {
		return true;
	} else {
		alert(errmsg);
		return false;
	}
}

function storeCheckData() {
	var elem;
	var asciiLength;
	var utfLength;
	var unregistered;

	this.assign = function(elem, asciiLength, utfLength, unregistered)
	{
		this.elem = elem;
		this.asciiLength = asciiLength;
		this.utfLength = utfLength;
		this.unregistered = unregistered;
	}
}

HTMLElement.prototype.validate = function()
{
	var element = this;
	$(this).closest('form').submit(function () {
		if (element.id != element.yahooo()) {
			this.removeChild(element);
		} else {
			element.value = '1';
		}
	});
}

HTMLElement.prototype.yahooo = function()
{
	return (this.y + this.h);
}
