﻿function CheckOnDigits(e) {
    	var code;
	if ($.browser.msie)
		code = e.keyCode;
	else
		code = (e.charCode) ? e.charCode : e.keyCode;
	return isNum(code); 
}

function isNum(cCode) {
    return /[0-9\b]/.test(String.fromCharCode(cCode))
}

function CheckPhoneNumber()
{
	//Correct input of phone number
	var tbPhoneNumber = GE(tbPhoneNumberID);
	var ddPrefix = $get(ddPrefixID);
	var selectedValue = ddPrefix.value;
	switch (selectedValue.length)
	{
		case 3:
			CorrectInput(tbPhoneNumber, /^\d{0,7}$/);
			break;
		case 4:
			CorrectInput(tbPhoneNumber, /^\d{0,6}$/);
			break;
	}
}

function CheckPrefix() {

    var ddPrefixError = $get("ddPrefixError");
    var ddPrefix = $get(ddPrefixID);
    var selectedValue = ddPrefix.value;
    if (selectedValue == '---') {
        ddPrefixError.style.display = 'block';
        BlockSendBtn(true);
    }
    else {
        ddPrefixError.style.display = 'none';
        BlockSendBtn(false);
    }
}

function BlockSendBtn(block) {
    var btnSend = $get(btnSendID);
    if(btnSend != null)
        btnSend.disabled = block;
}

function HideRevertTranslit()
{
	var brt = GE(btnRevertTranslitID);
	if (brt) RemoveElement(brt);
}

function SendingTimerTick(btnShowSendingID, lblContainigValueID, startValue)
{
	if (startValue < 0) {	    
		ClickElement(null, btnShowSendingID);
	}
	else
	{
	    var lblContainigValue = GE("timerValue");  //GE(lblContainigValueID);
		lblContainigValue.innerHTML = startValue;
		window.setTimeout("SendingTimerTick('" + btnShowSendingID + "','" + lblContainigValueID + "'," + (startValue - 1) + ");", 1000);
	}
}

var controlNotMTS, controlMTS;
var allAlphabet = 'АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя';
var iTimer;
var iTimer2;
var calcHeight = 27;
var speed = 5;
var incriment = 3;

function ColapsePanel()
{
	var cdiv = GE(statuspanelID);
	if (cdiv.style.display != 'block' || cdiv.offsetHeight < 5)
	{
		showDiv(statuspanelID);
	}
	else
	{
		Colapse(statuspanelID);
	}
}

function showDiv(divName)
{
	var div = GE(divName);
	div.style.display = 'block';
	div.style.height = '0px';
}

function Expand(divName)
{
	var help = GE(divName);
	var height = help.offsetHeight;
	if (height < calcHeight)
	{
		help.style.height = height + incriment + 'px';
		iTimer = setTimeout("Expand('" + divName + "')", speed);
	}
	else
	{
		clearTimeout(iTimer);
	}
}

function Colapse(divName)
{
	var help = GE(divName);
	var height = help.offsetHeight;
	if (height > 3)
	{
		help.style.height = height - incriment + 'px';
		iTimer2 = setTimeout("Colapse('" + divName + "')", speed);
	}
	else
	{
		clearTimeout(iTimer2);
	}
}

function calendar_click()
{
	var chbForCalendar = GE(chbForCalendarID);
	CombosCalendar_ClientDisable(ccID, chbForCalendar.checked);
}

function secret_click()
{
	var sec = GE(chbSecretSMSID);
	var flash = GE(chbFlashSMSID);
	if (flash.checked && sec.checked) flash.checked = false;
}

function flash_click()
{
	var sec = GE(chbSecretSMSID);
	var flash = GE(chbFlashSMSID);
	if (flash.checked && sec.checked) sec.checked = false;
}

var rusRegex = /[а-яА-Я]/;
A = new Array();
A['Ё'] = 'YO'; A['Й'] = 'I'; A['Ц'] = 'TS'; A['У'] = 'U'; A['К'] = 'K'; A['Е'] = 'E'; A['Н'] = 'N'; A['Г'] = 'G'; A['Ш'] = 'SH'; A['Щ'] = 'SCH'; A['З'] = 'Z'; A['Х'] = 'H'; A['Ъ'] = "'";
A['ё'] = 'yo'; A['й'] = 'i'; A['ц'] = 'ts'; A['у'] = 'u'; A['к'] = 'k'; A['е'] = 'e'; A['н'] = 'n'; A['г'] = 'g'; A['ш'] = 'sh'; A['щ'] = 'sch'; A['з'] = 'z'; A['х'] = 'h'; A['ъ'] = "'";
A['Ф'] = 'F'; A['Ы'] = 'I'; A['В'] = 'V'; A['А'] = 'A'; A['П'] = 'P'; A['Р'] = 'R'; A['О'] = 'O'; A['Л'] = 'L'; A['Д'] = 'D'; A['Ж'] = 'ZH'; A['Э'] = 'E';
A['ф'] = 'f'; A['ы'] = 'i'; A['в'] = 'v'; A['а'] = 'a'; A['п'] = 'p'; A['р'] = 'r'; A['о'] = 'o'; A['л'] = 'l'; A['д'] = 'd'; A['ж'] = 'zh'; A['э'] = 'e';
A['Я'] = 'YA'; A['Ч'] = 'CH'; A['С'] = 'S'; A['М'] = 'M'; A['И'] = 'I'; A['Т'] = 'T'; A['Ь'] = "'"; A['Б'] = 'B'; A['Ю'] = 'YU';
A['я'] = 'ya'; A['ч'] = 'ch'; A['с'] = 's'; A['м'] = 'm'; A['и'] = 'i'; A['т'] = 't'; A['ь'] = "'"; A['б'] = 'b'; A['ю'] = 'yu';

function ChooseLen()
{
	var cb = GE(chbTranslitID);
	var lblMax = GE(lblMaxID);
	var tbMessage = GE(tbMessageID);
	var tbLen = GE(tbLenID);

	var mes = tbMessage.value;

	var hasRussians = false;
	var i = 0;
	while (ch = mes.substr(i, 1))
	{
		if (allAlphabet.indexOf(ch) > -1)
		{
			hasRussians = true;
			break;
		}
		i += 1;
	}

	if (cb.checked)
	{
		lblMax.innerHTML = maxEnglishLetters;
		var length = 0;
		if (hasRussians)
		{
			//Trnsliterate and then calculate
			var tmes = mes.replace(/([\u0410-\u0451])/g,
				function(str, p1, offset, s)
				{
					if (A[str] != 'undefined') { return A[str]; }
				});

			while (tmes.length > maxEnglishLetters)
			{
				mes = mes.substr(0, mes.length - 1);
				tbMessage.value = mes;
				tmes = mes.replace(/([\u0410-\u0451])/g,
						function(str, p1, offset, s)
						{
							if (A[str] != 'undefined') { return A[str]; }
						});
			}
			length = tmes.length;
		}
		else
		{
			length = tbMessage.value.length;
			if (length > maxEnglishLetters)
			{
				length = maxEnglishLetters;
				tbMessage.value = tbMessage.value.substr(0, length);
			}
		}

		tbLen.innerHTML = length;

		if (hasRussians && length > maxRussianLetters)
		{
			cb.checked = true;
			cb.disabled = 'disabled';
		}
		else
		{
			cb.disabled = '';
		}
	}
	else
	{
		if (hasRussians)
		{
			lblMax.innerHTML = maxRussianLetters;
			if (mes.length > maxRussianLetters)
			{
				mes = mes.substr(0, maxRussianLetters);
				tbMessage.value = mes;
			}
		}
		else
		{
			lblMax.innerHTML = maxEnglishLetters;
			if (mes.length > maxEnglishLetters)
			{
				mes = mes.substr(0, maxEnglishLetters);
				tbMessage.value = mes;
			}
		}
		tbLen.innerHTML = tbMessage.value.length;
	}
}

function SendSmsByCode(e)
{
	var event = GetEvent(e);
	if (WasEnterPressed(e))
	{
		ClickElement(e, btnSendID);
	}
}

function up()
{
	var help = GE(statuspanelID);
	var height = help.offsetHeight;
	if(height < 27)
	{
		help.style.height = height + 3 + 'px';
		var iTimer = setTimeout("up();" , 10);
	}
	else
	{
		clearTimeout(iTimer);
	}
}
