﻿/*
	Common utility functions
*/



// Pads a value with padString on the left until it reaches length
function padValue(value, length, padString)
{

	var str = '' + value;

	while (str.length < length)
	{
		str = padString + str;
	}

	return str;

}

// // Pads a value with 0 (zero) on the left until it reaches length
function padZeros(value, length)
{
	return padValue(value, length, '0');
}


// Checks that a date value entered is later than today
function checkDate(sender, args)
{
	if (sender._selectedDate < new Date())
	{
		sender._selectedDate = new Date();
		// set the date back to the current date
		sender._textbox.set_Value(sender._selectedDate.format(sender._format))
	}
}


// Returns a KeyPress event's key code - Works for IE and FF
function GetEventKeyCode(e)
{
	e = (window.event) ? event : e;

	var intKey = (e.keyCode) ? e.keyCode : e.charCode;

	return intKey;

}


function CancelEvent(e)
{
	e.cancelBubble = true;
	e.returnValue = false;

	if (e.stopPropagation)
	{
		e.stopPropagation();
		e.preventDefault();
	}

	return false;

}


function ContinueEvent(e)
{
	e.returnValue = true;
	return true;
}


// Blocks any hebrew characters types in a textbox
function BlockHebrew(e)
{
	var intKey = GetEventKeyCode(e);

	if ((intKey >= 1488 && intKey <= 1514) &&
		(intKey != 8) && (intKey != 9))
	{
		return CancelEvent(e);
	}
	else
	{
		return ContinueEvent(e);
	}
}

// Block non-number
function BlockNonNumbers(e)
{
	var intKey = GetEventKeyCode(e);

	if ((intKey >= 48 && intKey <= 57) ||
		(intKey == 8) || (intKey == 9))
	{
		return ContinueEvent(e);
	}
	else
	{
		return CancelEvent(e);
	}
}


// Checks whether an ID number (TEUDAT ZEHUT) is valid
function CheckIDValid(oSrc, args)
{
	var ID = args.Value;
	var iLength = ID.length;
	
	if (iLength < 9)
	{
		args.IsValid = false;
		return;
	}

	iLength = ID.length;

	var iTotal = 0;
	var iTemp = 0;

	for (var i = 0; i < 9; i++)
	{
		iTemp = parseInt(ID.substring(i, i + 1));

		if ((i % 2) > 0)
		{
			iTemp *= 2;
		}

		if (iTemp > 9)
		{
			// it will always be in the teens
			iTemp = (iTemp % 10) + 1;
		}

		iTotal += iTemp;
	}

	args.IsValid = ((iTotal % 10) == 0);
}

