function formatText(text, xsize, ysize, name)
{
	var measureName = "FormatText_2006_measure"+name;
	var separators = Array(' ', '\t', '[', ']', '-', '=', '+', '!', '@', '#', '$', '%', 
						   '^', '*', '(', ')', '}', '{', '|', '"', '\'', ':', ',', 
						   '<', '>', '.', '/', '\\', '?');
	
	document.write("<div style=\"position: absolute;width: "+xsize*ysize+"px; left:-"+xsize*ysize+"px; top: -300;\">");
	document.write("	<span id=\""+measureName+"\" style=\"border: 0px; padding: 0px; margin: 0px;\">a<br/></span>");
	document.write("</div>");

	var measureStick = document.getElementById(measureName);
	
	var lineHeight = measureStick.offsetHeight;
	
	var ysize = Math.floor(ysize / lineHeight);

	if (ysize <= 0)
	{
		return "";
	}
	
	measureStick.innerHTML = "";
	
	var formatedText = "";
	
    	var width = 0;
    	var canMeasure = true;
    	var previousText = "";
    	var currentLine = 0;

	var returnText = "";
	var wholeWord = "";
	var clearWord = false;
	var htmlTag = false;
	var lastLine = false;
	
	var currentSize = xsize;

	for (i = 0; i < text.length; i++)
	{
		wholeWord = wholeWord + text.charAt(i);

		measureStick.innerHTML = formatedText + wholeWord;
		
		if (text[i] == "&")
		{
			canMeasure = false;
		}
		else if (text[i] == ";")
		{
			formatedText = formatedText + wholeWord;
			clearWord = true;
			canMeasure = true;

//whole ends with &gt;
			if (htmlTag && wholeWord == "&gt;")
			{
				htmlTag = false;
				clearWord = true;
			}
			else if (wholeWord == "&lt;")
			{
				htmlTag = true;
				clearWord = false;
			}
		}
	
		for (j = 0; j < separators.length; j++)
		{
			if (text.charAt(i) == separators[j])
			{
				formatedText = formatedText + wholeWord;
				clearWord = true;
				break;
			}
		}
		
		if (canMeasure && !htmlTag)
		{
			width = measureStick.offsetWidth;

			if (width >= xsize)
			{
				currentLine++;

				returnText = returnText + previousText;

				if (currentLine >= ysize)
				{
					measureStick.innerHTML = "";
					return returnText;
				}

				previousText = formatedText;
				formatedText = "";
			}
			else 
			{
				previousText = formatedText;
			}
		}
		
		if (clearWord)
		{
			wholeWord = "";
			clearWord = false;
		}
				
		if (lastLine == false && currentLine >= ysize-1)
		{
			lastLine = true;
			xsize = xsize - 55;
		}
	}

	measureStick.innerHTML = "";

	return returnText + previousText + wholeWord;
}

function getStringPixelSize(text)
{
	document.write("<div style=\"position: absolute;top: -30;\">");
	document.write("	<span id=\"measurestart\"></span>");
	document.write("	<span id=\"measure\" style=\"border: 0px; padding: 0px; margin: 0px;\">"+Text+"</span>");
	document.write("	<span id=\"measurend\"></span>");
	document.write("</div>");

    return findPosXByName("measurend")-findPosXByName("measurestart");
}

function findPosWithoutObjectPos(objName)
{
    var obj = document.getElementById(objName);

	if (obj == "undefined")
	{
		return 0;
	}
	
    var curleft = 0;

    if(obj.offsetParent)
    {
       while(1)
       {
          if(obj.offsetParent == null || typeof obj.offsetParent == "undefined")
          {
             break; 
          }

          obj = obj.offsetParent;
          
          curleft += obj.offsetLeft;
		}
    }
    
    return curleft;
}

function findPosXByName(objName)
{
    var obj = document.getElementById(objName);

	if (obj == "undefined" || obj == null)
	{
		return 0;
	}
	
	return findPosX(obj);
}

function findPosYByName(objName)
{
    var obj = document.getElementById(objName);

	if (obj == "undefined" || obj == null)
	{
		return 0;
	}
	
	return findPosY(obj);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curleft += obj.y;
	}
	return curleft;
}

function setValue(form, element, value)
{
	var myForm = document.forms[form];

	if (myForm)
	{
		var myElement = myForm.elements[element];

		if (myElement)
		{
			myElement.value = value;
		}
	}
}

function getChangedValue(form, element, value)
{
	var myForm = document.forms[form];
	var next = "(next)";

	if (myForm)
	{
		var myElement = myForm.elements[element];

		if (myElement)
		{
			if (myElement.value == value)
			{
				return "";
			}
			else
			{
				return form+":"+element+":"+myElement.value+next;
			}
		}
		else
		{
			return "";
		}
	}
	else
	{
		return "";
	}
}

function getChangedValues(originalValues)
{
	var next = "(next)";
	var values = "";

	if (originalValues)
	{
		var position = originalValues.indexOf(next);
		var actualString = "";

		while (position != -1)
		{
			actualString = originalValues.substr(0,position);

			if (actualString.length > 0)
			{
				var colonPos = actualString.indexOf(":");

				if (colonPos != -1)
				{
					var form = actualString.substr(0,colonPos);
					actualString = actualString.substr(colonPos+1);
					colonPos = actualString.indexOf(":");

					if (colonPos != -1)
					{
						var element = actualString.substr(0,colonPos);
						var value = actualString.substr(colonPos+1);

						values = values+getChangedValue(form, element, value);
					}
				}
			}

			originalValues = originalValues.substr(position+next.length);
			position = originalValues.indexOf(next);
		}
		return values;
	}
	else
	{
		return "";
	}
}

function backupPage()
{
	var length = document.forms.length;
	var values = "";
	var next = "(next)";

	for (i = 0; i < length; i++) {
		var myForm = document.forms[i];

		if (myForm)
		{
			var elementsLength = myForm.elements.length;

			for (j = 0; j < elementsLength; j++) {
				var myElement = myForm.elements[j];

				if (myElement)
				{
					values = values+""+myForm.id+":"+myElement.id+":"+myElement.value+next;
				}
			}
		}
	}

	return values;
}

function showOriginalImage(URL)
{
	window.open(URL);
}

function GetSelectedItem(FormName,comboBox)
{
	if (document.getElementById(comboBox) == null)
	{
		return -1;
	}
	
	var comboBoxObject = document.getElementById(comboBox);

	if (comboBoxObject.options.length == 0)
		return -1;

	var selected = comboBoxObject.selectedIndex;

	if (selected < 0 || selected >= comboBoxObject.options.length)
		return "";

	return comboBoxObject.options[selected].value;
}

function FillDayCombo(yearCombo, monthCombo, dayCombo)
{
	var selectedYear = 0;

	if (document.getElementById(yearCombo))
	{
		selectedYear = document.getElementById(yearCombo).value;
	}

	var selectedMonth = 0;

	if (document.getElementById(monthCombo))
	{
		selectedMonth = document.getElementById(monthCombo).value;
	}

	var days = [31,28,31,30,31,30,31,31,30,31,30,31];

	var dayCount = days[selectedMonth];

	if (selectedYear % 4 == 0 && selectedMonth == 1)
		dayCount = 1;

	if (document.getElementById(dayCombo))
	{
		document.getElementById(dayCombo).options.length = 0;

		for (i = 0; i < dayCount; i++)
		{
			var optionName = new Option(String(i+1), (i+1), false, false);

	        var Length = document.getElementById(dayCombo).options.length;
	        document.getElementById(dayCombo).options[Length] = optionName;
		}
	}
}

function FillComboboxSimple(FormName, comboBox, text, comboArray)
{
	if (document.getElementById(comboBox) == null)
	{
		return;
	}
	
	var comboBoxObject = document.getElementById(comboBox);
 	comboBoxObject.options.length = 0;

	var optionName = new Option(text,0, false, false);

    comboBoxObject.options[0] = optionName;

	for (i = 0; i < comboArray.length; i++)
	{
		if (comboArray[i] != undefined)
		{
			if (comboArray[i][0] != undefined && comboArray[i][0].length > 1)
			{
				optionName = new Option(comboArray[i][0], i, false, false);
			}
			else
			{
				optionName = new Option(comboArray[i], i, false, false);
		    }

	        var Length = comboBoxObject.options.length;
	        comboBoxObject.options[Length] = optionName;
		}
	}
}

function FillComboboxDepended(FormName,parentIndex, comboBox, text, comboArray)
{
	if (document.getElementById(comboBox) == null)
	{
		return;
	}
	
	var comboBoxObject = document.getElementById(comboBox);
 	comboBoxObject.options.length = 0;

	var optionName = new Option(text,0, false, false);

	comboBoxObject.options[0] = optionName;

	if (parentIndex < 1)
		return;

	for (var i = 0; i < comboArray.length; i++)
	{
		if (comboArray[i] != undefined)
		{
			if (comboArray[i][1] == parentIndex)
			{
				optionName = new Option(comboArray[i][0], i, false, false);
	            var Length = comboBoxObject.options.length;
		        comboBoxObject.options[Length] = optionName;
			}
		}
	}
}
function DeleteFirstElement(formName, comboBox, text)
{
	var combo = document.getElementById(comboBox);
	var index = combo.selectedIndex;
	var count = combo.options.length;

	if (count > 0 && combo.options[0].text == text)
	{
		count = count - 1;

		for (i = 0; i < count; i++)
		{
	        	combo.options[i].text = combo.options[i+1].text;
	        	combo.options[i].value = combo.options[i+1].value;
		}

		combo.options.length = count;

		if (index > 0)
		{
			combo.selectedIndex = index-1;
		}
	}
}

function ShowHideElement(FormItem, Show)
{
	if (document.all)
	{
		if (document.all[FormItem] != null)
		{
			if (Show)
			{
				document.all[FormItem].className = "item_on";
			}
			else
			{
				document.all[FormItem].className = "item_off";
			}
		}
	}
	else
	{
		if (document.getElementById(FormItem) != null)
		{
			if (Show)
			{
				document.getElementById(FormItem).className = "item_on";
			}
			else
			{
				document.getElementById(FormItem).className = "item_off";
			}
		}
	}
}

function setSelected(combo, comboIndex)
{
	for (var i = 0; i < combo.options.length; i++)
	{
		if (combo.options[i].value == comboIndex)
		{
			combo.selectedIndex = i;
			break;
		}
	}
	return;
}

function ClearEditBox(editBoxID, text)
{
	try
	{
		if (document.all)
		{
			if (document.all[editBoxID] &&
				text == document.all[editBoxID].value)
			{
				document.all[editBoxID].value = "";
			}
		}
		else
		{
			if (document.getElementById(editBoxID) &&
				text == document.getElementById(editBoxID).value)
			{
				document.getElementById(editBoxID).value = "";
			}
		}
	}
	catch (e)
	{
	}
}

function ChangeImage(ImageName, NewImageSrc)
{
	if (document.images[ImageName] != null)
	{
		document.images[ImageName].src = NewImageSrc;
	}
}

function SubmitForm(FormName)
{
	if (document.forms)
	{
		if (document.forms[FormName])
		{
			document.forms[FormName].submit();
		}
	}
}

function FillEmptyEditBox(editBoxID, text)
{
	if (document.getElementById(editBoxID) &&
    	document.getElementById(editBoxID).value == "")
	{
		document.getElementById(editBoxID).value = text;
	}
}


function ChangeBackgroundColor(color, elementID)
{
	if (color == 'undefined')
	{
		return;
	}

	if (document.getElementById(elementID))
	{
		document.getElementById(elementID).style.background = color;
	}
}

function ChangeFontColor(elementName,colorS)
{
	if (colorS == 'undefined')
	{
		return;
	}

	if (document.getElementById(elementName))
	{
		document.getElementById(elementName).style.color = colorS;
	}
}

function ShowHideInput(inputID,enable)
{
	if (document.getElementById(inputID))
	{
		document.getElementById(inputID).disabled = !enable;
	}
}
