function chooseback(IDBack)
{
    document.getElementById('txtBackground').value = IDBack;
}
function chooseFont(IDBack)
{
    document.getElementById('txtFont').value = IDBack;
}

function TestDataCheck()
{
	if (TestNull(document.getElementById("txtFont").value))
	{
		alert('Chưa nhập mã kiểu chữ');
		document.getElementById('txtFont').focus();
		return;
	}
	else
	{
		if (!isInteger(document.getElementById("txtFont").value))
		{
			alert('Xem mã kiểu chữ bên trên để nhập chính xác.');
			document.getElementById('txtFont').focus();
			return;
		}
	}
	
	if (TestNull(document.getElementById("txtBackground").value))
	{
		alert('Chưa nhập mã nền');
		document.getElementById('txtBackground').focus();
		return;
	}
	else
	{
		if (!isInteger(document.getElementById("txtBackground").value))
		{
			alert('Xem mã nền bên trên để nhập chính xác.');
			document.getElementById('txtBackground').focus();
			return;
		}
	}
	if (TestNull(document.getElementById("txtContent").value))
	{
		alert('Chưa nhập nội dung');
		document.getElementById('txtContent').focus();
		return;
	}
	
	var cboSizeImage = document.getElementById("optSizeImage");
	var cboFontSize = document.getElementById("optFontSize");
		
web123SMS.media.ThuPhapTuTao.doImageTuTao(document.getElementById("txtFont").value,document.getElementById("txtBackground").value,document.getElementById("txtContent").value,document.getElementById("txtColor").value,cboSizeImage.options[cboSizeImage.selectedIndex].value,cboFontSize.options[cboFontSize.selectedIndex].value, doLoadImageTuTaoRes)
	
	return false;
}

function doLoadImageTuTaoRes(res) {
	var str = res.value;
	if (str == "err")
	{
		alert("Lỗi!! thông số nhập không chính xác")
	}
	else if (str == "font")
	{
		alert("Mã kiểu chữ không chính xác. Vui lòng xem bên trên")
	}
	else if (str == "back")
	{
		alert("Mã nền không chính xác. Vui lòng xem bên trên")
	}
	else if (str == "sizeback")
	{
		alert("Cỡ hình không chính xác. Vui lòng chọn lại")
	}
	else
	{
		document.getElementById('hinhminhhoa').innerHTML = str;
	}
}


function TestDataCheck1()
{
	if (TestNull(document.getElementById("txtFont").value))
	{
		alert('Chưa nhập mã kiểu chữ');
		document.getElementById('txtFont').focus();
		return;
	}
	else
	{
		if (!isInteger(document.getElementById("txtFont").value))
		{
			alert('Xem mã kiểu chữ bên trên để nhập chính xác.');
			document.getElementById('txtFont').focus();
			return;
		}
	}
	
	if (TestNull(document.getElementById("txtBackground").value))
	{
		alert('Chưa nhập mã nền');
		document.getElementById('txtBackground').focus();
		return;
	}
	else
	{
		if (!isInteger(document.getElementById("txtBackground").value))
		{
			alert('Xem mã nền bên trên để nhập chính xác.');
			document.getElementById('txtBackground').focus();
			return;
		}
	}
	if (TestNull(document.getElementById("txtContent").value))
	{
		alert('Chưa nhập nội dung');
		document.getElementById('txtContent').focus();
		return;
	}
	if (TestNull(document.getElementById("txtMaXacNhan").value))
	{
		alert('Chưa nhập chuỗi xác nhận');
		document.getElementById('txtMaXacNhan').focus();
		return;
	}
	
	var cboSizeImage = document.getElementById("optSizeImage");
	var cboFontSize = document.getElementById("optFontSize");
	
	document.getElementById("Ketquataohinh").innerHTML = "<img border=\"0\" src=\"/images/iconwait.gif\" width=\"26\" height=\"26\"> <b>Đang xử lý ...</b>";
	
web123SMS.media.ThuPhapTuTao.doTaoImage(document.getElementById("txtFont").value,document.getElementById("txtBackground").value,document.getElementById("txtContent").value,document.getElementById("txtColor").value,cboSizeImage.options[cboSizeImage.selectedIndex].value,cboFontSize.options[cboFontSize.selectedIndex].value,document.getElementById("txtMaXacNhan").value, doTaoImageRes)
	
	return false;
}

function doTaoImageRes(res) {
	var str = res.value;
	if (str == "err")
	{
		document.getElementById('Ketquataohinh').innerHTML='Lỗi!! thông số nhập không chính xác';
		//alert("Lỗi!! thông số nhập không chính xác");		
	}
	else if (str == "font")
	{
		document.getElementById('Ketquataohinh').innerHTML='Mã kiểu chữ không chính xác. Vui lòng xem bên trên';
		//alert("Mã kiểu chữ không chính xác. Vui lòng xem bên trên");		
	}
	else if (str == "back")
	{
		//alert("Mã nền không chính xác. Vui lòng xem bên trên");
		document.getElementById('Ketquataohinh').innerHTML='Mã nền không chính xác. Vui lòng xem bên trên';
	}
	else if (str == "sizeback")
	{
		//alert("Cỡ hình không chính xác. Vui lòng chọn lại");
		document.getElementById('Ketquataohinh').innerHTML='Cỡ hình không chính xác. Vui lòng chọn lại';
	}
	else if (str == "maxacnhan")
	{
		//alert("Mã xác nhận không chính xác.");
		document.getElementById('Ketquataohinh').innerHTML='Mã xác nhận không chính xác.';
	}
	else
	{
		document.getElementById('Ketquataohinh').innerHTML = str;
		document.getElementById("btnTaoHinh").disabled = true;
		document.getElementById("btnXemHinh").disabled = true;
		document.getElementById("txtMaXacNhan").disabled = true;
	}
}


/*Ham ho tro*/
function TestNull(str)
{
	if (str.length <=0)
		return true;

		var j=0;
		var flag=0;
		for (j=0;j<str.length;j++)
		{
			if(str.charAt(j) == " ")
				flag=flag+1;
		}
	if(flag!=str.length){		
		return false;
	}
	return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
