
function tp_SwitchSides()
{
	var tpid_tabs = $('tpid_tabs');
	var tpid_FrontSide = $('tpid_FrontSide');
	var tpid_BackSide = $('tpid_BackSide');

	if (!tpFrontSide)
	{
		tpid_tabs.style.backgroundImage = 'url(dogtag_images/tabs_front_' + Lang + '.png)';
		tpid_FrontSide.style.display = 'block';
		tpid_BackSide.style.display = 'none';

		tpFrontSide = true;
	}
	else
	{
		tpid_tabs.style.backgroundImage = 'url(dogtag_images/tabs_back_' + Lang + '.png)';
		tpid_FrontSide.style.display = 'none';
		tpid_BackSide.style.display = 'block';

		tpFrontSide = false;
	}

	tp_BuildTagText();
}

function tp_PetTag(shape, size)
{

	if (tpSetShape != shape)
	{
		tpSetShape = shape;
	}

	if (tpSetSize != size)
	{
		tpSetSize = size;
	}

	//set on/off for shape
	for (var shapeName in tpTagsKind)
	{
		var tpid_select = $('tpid_' + shapeName + 'select');
		if (shape == shapeName)
		{
			tpid_select.style.backgroundImage = 'url(dogtag_images/tags_' + shapeName + '_on_' + Lang + '.png)';
		}
		else
		{
			tpid_select.style.backgroundImage = 'url(dogtag_images/tags_' + shapeName + '_off_' + Lang + '.png)';
		}
	}

	//set on/off for size
	var tpTagsSizeKind = tpTagsKind[shape][tpSetColor];
	for (var index in tpTagsSizeKind)
	{
		var sizeName = tpTagsSizeKind[index];
		var tpid_size = $('tpid_' + sizeName + 'select');

		if (sizeName == size)
		{
			tpid_size.style.backgroundImage = 'url(dogtag_images/size_' + sizeName + '_on.png)';
		}
		else
		{
			tpid_size.style.backgroundImage = 'url(dogtag_images/size_' + sizeName + '_off.png)';
		}
	}

	//set on/off for color
	var flag = false;
	var firstColorName = null;
	for (var index = 0; index < tpTagsColorKind.length; index++)
	{
		var colorName = tpTagsColorKind[index];
		var tpid_color = $('tpid_' + colorName + 'select');
		
		var isExist = false;

		for (var colorTempName in tpTagsKind[shape])
		{
			if (colorTempName == colorName)
			{
				isExist = true;
				break;
			}
		}

		if (!isExist)
		{
			tpid_color.style.display = 'none';
			continue;
		}
		tpid_color.style.display = '';
		
		if (!firstColorName)
		{
			firstColorName = colorName;
		}

		if (colorName == tpSetColor)
		{
			tpid_color.style.backgroundImage = 'url(dogtag_images/' + colorName + '_on_' + Lang + '.png)';
			flag = true;
		}
		else
		{
			tpid_color.style.backgroundImage = 'url(dogtag_images/' + colorName + '_' + Lang + '.png)';
		}
	}

	if (!flag)
	{
		tpSetColor = firstColorName;
		var colorName = tpSetColor;
		var tpid_color = $('tpid_' + colorName + 'select');
		tpid_color.style.backgroundImage = 'url(dogtag_images/' + colorName + '_on_' + Lang + '.png)';
	}

	tp_changeImage();
}

function tp_SelectTagColor(color)
{
	tpSetColor = color;
	tp_PetTag(tpSetShape, tpSetSize);
}

function tp_changeImage()
{
	var tagImageFileName = tp_GetPetTagImage();
	var tpid_ttpic = $('tpid_ttpic');
	
	tpid_ttpic.style.backgroundImage = 'url(dogtag_images/' + tagImageFileName + ')';

	tp_BuildTagText();
}


function tp_GetPetTagImage(shape, color, size)
{
	if (typeof shape == 'undefined')
	{
		shape = tpSetShape;
	}
	
	if (typeof color == 'undefined')
	{
		color = tpSetColor;
	}
	
	if (typeof size == 'undefined')
	{
		size = tpSetSize;
	}
	return 'tag' + '_' + size + '_' + shape + '_' + color + '_' + Lang + '.png';
}

function stripVowelAccent(str)
{
var s=str;

var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
/[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
/[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
/[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
/[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];

var repChar=['A','a','E','e','I','i','O','o','U','u'];

for(var i=0; i<rExps.length; i++)
s=s.replace(rExps[i],repChar[i]);

return s;
}

function tp_BuildTagText(event)
{
	if (!event)
	{
		event = window.event;
	}

	var tpid_ttareachrome = $('tpid_ttareachrome');
	var tpid_ttarea = $('tpid_ttarea');
	
	var tagStr = '';
	for (var index = 0; index < tpTags_MaxLength[tpSetShape][tpSetSize].length; index++)
	{
		var line = '';
		if (tpFrontSide)
		{
			line = $('tpid_InputFront' + (index * 1 + 1)).value;
		
			if (line.length > tpTags_MaxLength[tpSetShape][tpSetSize][index])
			{
				line = line.substring(0, tpTags_MaxLength[tpSetShape][tpSetSize][index]);
				$('tpid_InputFront' + (index * 1 + 1)).value = line;
			}		

			$('tpid_MaxChrFront' + (index * 1 + 1)).innerHTML = '(' + tpTags_MaxLength[tpSetShape][tpSetSize][index] + ' chr.)';
			$('tpid_InputFront' + (index * 1 + 1)).maxLength = tpTags_MaxLength[tpSetShape][tpSetSize][index];
		}
		else
		{
			line = $('tpid_InputBack' + (index * 1 + 1)).value;

			if (line.length > tpTags_MaxLength[tpSetShape][tpSetSize][index])
			{
				line = line.substring(0, tpTags_MaxLength[tpSetShape][tpSetSize][index]);
				$('tpid_InputBack' + (index * 1 + 1)).value = line;
			}

			$('tpid_MaxChrBack' + (index * 1 + 1)).innerHTML = '(' + tpTags_MaxLength[tpSetShape][tpSetSize][index] + ' chr.)';
			$('tpid_InputBack' + (index * 1 + 1)).maxLength = tpTags_MaxLength[tpSetShape][tpSetSize][index];
		}
		
		var newLine = '';
		for (var i = 0; i < line.length; i++)
		{
			var ch = line.charAt(i);
			var chCode = line.charCodeAt(i);
			//alert(ch + ', ' + chCode);
			if (chCode >= 210)
			{
			  //continue;
			}
			newLine += line.charAt(i);
		}

	tagStr += (newLine != ''?newLine + '<br />':'');
	
	}

	if (tpTags_MaxLength[tpSetShape][tpSetSize].length == 3)
	{
		$('tpid_LineFront4').style.display = 'none';
		$('tpid_LineBack4').style.display = 'none';
		$('tpid_LineFront5').style.display = 'none';
		$('tpid_LineBack5').style.display = 'none';
	}
	else if (tpTags_MaxLength[tpSetShape][tpSetSize].length == 4)
	{
		$('tpid_LineFront5').style.display = 'none';
		$('tpid_LineBack5').style.display = 'none';
	}
	else
	{
		$('tpid_LineFront4').style.display = 'block';
		$('tpid_LineBack4').style.display = 'block';
		$('tpid_LineFront5').style.display = 'block';
		$('tpid_LineBack5').style.display = 'block';
	}

	//tagStr = stripVowelAccent(tagStr);
	//tagStr = tagStr.toUpperCase();
		

	tpid_ttareachrome.style.left = tpTags_Pos[tpSetShape][tpSetSize][0] + 'px';
	tpid_ttareachrome.style.top = tpTags_Pos[tpSetShape][tpSetSize][1] + 'px';
	tpid_ttareachrome.style.width = tpTags_Pos[tpSetShape][tpSetSize][2] + 'px';
	tpid_ttareachrome.style.height = tpTags_Pos[tpSetShape][tpSetSize][3] + 'px';
	
	tpid_ttarea.style.left = parseInt(tpTags_Pos[tpSetShape][tpSetSize][0]) + 'px';
	tpid_ttarea.style.top = parseInt(tpTags_Pos[tpSetShape][tpSetSize][1]) + 'px';
	tpid_ttarea.style.width = parseInt(tpTags_Pos[tpSetShape][tpSetSize][2]) + 'px';
	tpid_ttarea.style.height = parseInt(tpTags_Pos[tpSetShape][tpSetSize][3]) + 'px';
	
	var tpid_ttareachrome_sub = $('tpid_ttareachrome_sub');
	var tpid_ttarea_sub = $('tpid_ttarea_sub');
	tpid_ttarea_sub.innerHTML = tagStr;
	tpid_ttareachrome_sub.innerHTML = tagStr;

	tp_ResizeText();
}

function tp_ResizeText()
{
	var tpid_ttareachrome = $('tpid_ttareachrome');
	var tpid_ttarea = $('tpid_ttarea');

	var tpid_ttareachrome_sub = $('tpid_ttareachrome_sub');
	var tpid_ttarea_sub = $('tpid_ttarea_sub');

	var numerOfLine = tp_GetCount(tpid_ttareachrome_sub.innerHTML, '<br>');
	
	if (numerOfLine == 0)
	{
		numerOfLine = tp_GetCount(tpid_ttareachrome_sub.innerHTML, '<BR>');
		if (numerOfLine == 0)
		{
			return;
		}
	}

	var direction = -1;
	var step = 1;
	var fontSize = tpid_ttareachrome.style.fontSize;
	
	fontSize = fontSize.substring(0, fontSize.length - 2);

	tpid_ttareachrome.style.display = 'block';
	
	//alert(tpid_ttareachrome.scrollHeight + ', ' + tpid_ttareachrome.clientHeight);
	if (tpid_ttareachrome.scrollHeight > tpid_ttareachrome.clientHeight || tpid_ttareachrome.scrollWidth > tpid_ttareachrome.clientWidth)
	{
		direction = -1;
		while (tpid_ttareachrome.scrollHeight > tpid_ttareachrome.clientHeight || tpid_ttareachrome.scrollWidth > tpid_ttareachrome.clientWidth)
		{
			fontSize = fontSize * 1 + direction * step;
			tpid_ttareachrome.style.fontSize = fontSize + 'px';
		}
	}
	else
	{
		direction = 1;
		while (tpid_ttareachrome.scrollHeight <= tpid_ttareachrome.clientHeight && tpid_ttareachrome.scrollWidth <= tpid_ttareachrome.clientWidth)
		{
			fontSize = fontSize * 1 + direction * step;
			tpid_ttareachrome.style.fontSize = fontSize + 'px';
		}
	}
	//alert(fontSize);
	fontSize -= direction * step;
	fontSize--;

	tpid_ttareachrome.style.display = 'block';
	tpid_ttarea.style.fontSize = fontSize + 'px';
	tpid_ttareachrome.style.fontSize = fontSize + 'px';

	var parentHeight = tpid_ttareachrome.clientHeight;
	var childrenHeight = tpid_ttareachrome_sub.offsetHeight;

	var margin_top = Math.max((parentHeight - childrenHeight) / 2, 0);
	
	//alert(parentHeight + ', ' + childrenHeight + ', ' + margin_top);
	tpid_ttareachrome_sub.style.marginTop = margin_top + 'px';
	tpid_ttarea_sub.style.marginTop = margin_top + 'px';

	tp_SetPostData();
}

function tp_SetPostData()
{
	$('tp_Shape').value = tpSetShape;
	$('tp_Color').value = tpSetColor;
	$('tp_Size').value = tpSetSize;
	$('tp_Side').value = (tpFrontSide?'Front':'Back');
}

function $(id)
{
	return document.getElementById(id);
}

function tp_GetCount(src, find)
{
	var cnt = 0;
	var start = 0;
	var next = 0;
	while ((next = src.indexOf(find, start)) >= 0)
	{
		cnt++;
		start = next + find.length;
	}
	return cnt;
}
