// ******************************************** Numeric functions *************************************** \\
function isNumericValue ()
{
	if (window.event.keyCode < 48 || window.event.keyCode > 57)
	{
		return (false);
	}
	else
	{
		return (true);
	}
}

function checkNumericValue ()
{
	if (!isNumericValue())
	{
		window.event.returnValue = false;
	}
}

function checkNumericValuePlusDot ()
{
	if (window.event.keyCode != 46 && !isNumericValue())
	{
		window.event.returnValue = false;
	}
}

function checkShortDateValue ()
{
	if (window.event.keyCode < 45 || window.event.keyCode > 57)
	{
		window.event.returnValue = false;
	}
}

function checkFullFormalDateValue ()
{
	if (window.event.keyCode > 58 || (window.event.keyCode < 47 && window.event.keyCode != 32))
	{
		window.event.returnValue = false;
	}
}

function checkPhoneValue ()
{
	if (window.event.keyCode != 45 && !isNumericValue())
	{
		window.event.returnValue = false;
	}
}

// ********************************************** Form functions **************************************** \\
function splitStringToArrayByMaxLength (strValue, nMaxLength)
{
	var aValues = new Array();
	var nValueLength = strValue.length;
	if (nMaxLength < nValueLength)
	{
		var nCurrentPosition = 0;
		while (nCurrentPosition < nValueLength)
		{
			var nCurrentLimit = nCurrentPosition + nMaxLength;
			if (nCurrentLimit < nValueLength)
			{
				aValues.push(strValue.substring(nCurrentPosition, nCurrentLimit));
			}
			else
			{
				aValues.push(strValue.substring(nCurrentPosition, nValueLength));
			}
			nCurrentPosition = nCurrentLimit;
		}
	}
	else
	{
		aValues.push(strValue);
	}
	return(aValues);
}

function createInputElementsByArrayInForm (strFormName, strFormInputElementName, aValues)
{
	var elemForm = document.getElementById(strFormName);
	if (elemForm != null)
	{
		var nValuesCounter = 0;
		for (; nValuesCounter < aValues.length ; nValuesCounter++)
		{
			var elemInput = document.createElement("INPUT");
			elemInput.type = "hidden";
			elemInput.name = strFormInputElementName;
			elemInput.value = aValues[nValuesCounter];
			elemForm.appendChild(elemInput);
		}
	}
}

// ******************************************** General functions *************************************** \\
function isnull (sValue)
{
   try
   {
	  sValue = sValue+'';
	  return (sValue == '' || sValue == 'null' || sValue == undefined || sValue == 'undefined');
   }
   catch (e) {return true;}
}

function limitSize (obj,maxSize)
{
	var thisInnerText = obj.innerText;

	if (obj.value.length > maxSize)
	{
		obj.value = thisInnerText.substr(0, maxSize);
	}
}

function CStrZeroFill(Num, Size)
{
	if (isNaN(Num))
	{
		return(null);
	}
	else
	{
		var Str;
		Str = new String(Num);
		if (Str.length > Size)
		{
			return(Str);
		}
		else
		{
			return(JSString(Size - Str.length,"0") + Str);
		}
	}
}

function checkDateAndTime (secs, mins, hour, day, month, year)
{
	var returnVal = false;

	var checkedYear = new Number(year);
	var checkedMonth = new Number(month - 1);
	var checkedDay = new Number(day);
	var checkedHour = new Number(hour);
	var checkedMins = new Number(mins);
	var checkedSecs = new Number(secs);

	var checkedDate = new Date (checkedYear, checkedMonth, checkedDay,
								checkedHour, checkedMins, checkedSecs);

	if (!isNaN (checkedDate))
	{
		if (checkedDate.getFullYear() == checkedYear &&
			checkedDate.getMonth() == checkedMonth &&
			checkedDate.getDate() == checkedDay &&
			checkedDate.getHours() == checkedHour &&
			checkedDate.getMinutes() == checkedMins &&
			checkedDate.getSeconds() == checkedSecs)
		{
			returnVal = checkedDate;
		}
	}

	return (returnVal);
}

function isInArray(findWhat, arrayToSearch)
{
	for (currItem in arrayToSearch)
	{
		if (findWhat == arrayToSearch[currItem])
		{
			return (true);
		}
	}

	return (false);
}

function resizeImage(strContainingTABLEElementId, strIMGElementId)
{
	var WINDOW_WIDTH = 690;
	var WINDOW_HEIGHT = 490;
	var elementContainingTable = document.getElementById (strContainingTABLEElementId);
	var elementImg = document.getElementById (strIMGElementId);
	var nImgWidth = null;
	var nImgHeight = null;
	if (!isnull(elementImg.width))
	{
		var nImgWidth 	= elementImg.width;
		var nImgHeight 	= elementImg.height;

		if (!isnull(elementContainingTable.clientWidth))
		{
			WINDOW_WIDTH = WINDOW_WIDTH - (elementContainingTable.clientWidth - nImgWidth);
			WINDOW_HEIGHT = WINDOW_HEIGHT - (elementContainingTable.clientHeight - nImgHeight);
		}

		// calculate the size of the picture to fit the window
		if (nImgWidth > WINDOW_WIDTH || nImgHeight > WINDOW_HEIGHT)
		{
			var nHorizonalRatio = nImgWidth / WINDOW_WIDTH;
			var nVerticalRatio = nImgHeight / WINDOW_HEIGHT;
			if (nHorizonalRatio > nVerticalRatio)
			{
				picRatio = nHorizonalRatio;
				nImgHeight = parseInt(nImgHeight/picRatio)
				nImgWidth = WINDOW_WIDTH;
			}
			else
			{
				picRatio = nVerticalRatio;
				nImgWidth = parseInt(nImgWidth/picRatio)
				nImgHeight = WINDOW_HEIGHT;
			}
			elementImg.width = nImgWidth;
			elementImg.height = nImgHeight;
		}
	}
}

function resizeWindow(strURL, strMainTABLEElementId)
{
	var SCREEN_WIDTH = 700;
	var SCREEN_HEIGHT = 500;
	/*if (!isnull(window.screen))
	{
		SCREEN_WIDTH = window.screen.availWidth - 30;
		SCREEN_HEIGHT = window.screen.availHeight - 50;
	}*/

	var strScrollBars = "";
	var nWinWidth;
	var nWinHeight;
	var elementMainTable = document.getElementById (strMainTABLEElementId);
	if (!isnull(elementMainTable.clientWidth))
	{
		var nWinWidth 	= elementMainTable.clientWidth;
		var nWinHeight 	= elementMainTable.clientHeight;

		// calculate the size of the picture to fit the screen
		if (nWinWidth > SCREEN_WIDTH || nWinHeight > SCREEN_HEIGHT)
		{
			var nHorizonalRatio = nWinWidth / SCREEN_WIDTH;
			var nVerticalRatio = nWinHeight / SCREEN_HEIGHT;
			if (nHorizonalRatio > nVerticalRatio)
			{
				picRatio = nHorizonalRatio;
				nWinWidth = SCREEN_WIDTH;
				nWinHeight = parseInt(nWinHeight / picRatio);
			}
			else
			{
				picRatio = nVerticalRatio;
				nWinHeight = SCREEN_HEIGHT;
				nWinWidth = parseInt(nWinWidth / picRatio);
			}
			strScrollBars = ", scrollbars=yes";
		}
	}
	else
	{
		nWinWidth	= SCREEN_WIDTH;
		nWinHeight	= SCREEN_HEIGHT;
	}

	window.open(strURL, "_blank" , "height=" + (nWinHeight) + ", width=" + (nWinWidth) + ", resizable=yes, left=0, top=0" + strScrollBars);
	window.close();
}

function openNoBarsWindow (sURL, sName, nWidth, nHeight)
{
	window.open(sURL, sName, "height=" + nHeight + ",width=" + nWidth + ",status=yes, toolbar=no, menubar=yes, resizable=yes,scrollbars=yes");
}

function openInNewWindow (strURL)
{
	window.open(strURL, "_blank", "height=500, width=700, status=yes, toolbar=yes, location=yes, menubar=yes, resizable=yes,scrollbars=yes");
}
