/*
	Include file - Javascript code for all other php files"
	-----------------------------------------------------
	Code by Hari Szovik - szovik.hari@ge.com - May 2005 -
	-----------------------------------------------------

	The code snipets below were developed for the purpose of having "real-time"
	value consistency checking and correction while the user types the value (not
	only when the focus leaves the fields), according to defined "natural" rules like
	"numeric only", "numeric with comma (%)", "currency only", "minimum/maximum" and
	according to project defined specific rules to meet expectations of relational
	behaviors of input controls. These checkings happen right at the moment when the
	user types, and	not when the user leaves the fields, to help guide the user better
	into not entering erroneous values. The checkings take into account typed characters,
	inserted characters, and pasted characters.

	These codes, mainly the procedures defined under specific "natural" logic can be
	reused in other pages, and are not specifically "tied" to the php page.
	Only a very few minor changes might be necessary to reuse these codes on
	other pages.

*/

// general width in pixels of characters in input boxes
var lCharWidthPix = 0

// is status about to calculate or about to reprice php
var vanEAtarazGomb = false

// temp variable storing input value changes from special keys delete and backspace
var tempInputValue = ""

// general function called on body.onload event to retrieve width in pixels
// of characters input boxes, check calculating or repricing status
// to avoid inadvertently reprice without uncalculated values by user.
function AdjustCharPixelWidth()
{
	for (i = 0;  i < document.links.length;  i++)
	{
		if (document.links.item(i).id == "atarazlink")
		{
			vanEAtarazGomb = true
			var temp = document.getElementById("nematarazhat")
			temp.style.visibility = 'hidden'
		}
	}

	// if pixels width of character empty, set from hidden input boxes
	// in frontend_headbar frame
	if (lCharWidthPix == 0)
	{
		lCharWidthPix = window.parent.frames("frontend_headbar").document.getElementById("CharWidth").value
	}
}

// onpaste event of input boxes to check validity of input data
// numeric only, amount or  account number
// check data consistency of data in input boxe, and of data
// in clipboard and decides after character types, but before
// showing data, if bypass or let go
function InputNumericOnPaste(bForint, lMin, lMax)
{
	// function to check if data numeric only
	if (CheckValueNumeric(window.clipboardData.getData("Text")) == false)
	{
		event.returnValue = false
	}
	else
	{
		// general function to check consistency of data entered (all types of numeric data)
		if (CheckNumericOnKeyPressAndOnPaste(true, 0, bForint, lMin, lMax) == false)
		{
			event.returnValue = false
		}
		else
		{
			// function call to check whether values changed, and have user recalculate
			// first before repricing with new values
			AllControlsOnChange()
			event.returnValue = true
		}
	}
}

// onkeypress event of input boxes to check validity of input data
// numeric only, amount or  account number
// check data consistency of data in input boxe, and of data
// in clipboard and decides after character types, but before
// showing data, if bypass or let go and accept
function InputNumericOnKeyPress(kCode, bForint, lMin, lMax)
{
	// allow submitting form by pressing enter
	if ((document.URL).search("szamlaszam.php") != -1)
	{
		if (kCode == 13)
		{
			document.getElementById("realsubmit").value = 2
			document.form1.submit()
		}
	}


	if ((parseInt(kCode) <= 57) && (parseInt(kCode) >= 48))
	{
		// general function to check consistency of data entered (all types of numeric data)
		if (CheckNumericOnKeyPressAndOnPaste(false, kCode, bForint, lMin, lMax) == false)
		{
			event.keyCode = ""
			return false
		}
		else
		{
			// function call to check whether values changed, and have user recalculate
			// first before repricing with new values
			AllControlsOnChange()
			return true
		}
	}
	else
	{
		event.keyCode = ""
		return false
	}
}

function InputNumericOnKeyPressTtablaSorszam(kCode, bForint, lMin, lMax)
{
	// allow submitting form by pressing enter
	// a törlesztőtábla sorszám beadásánál használom, Enterre submit-ol


	if ((document.URL).search("page_ttabla.php") != -1)
	{

		if (kCode == 13)
		{
			document.getElementById("realsubmit").value = 6
			document.form1.submit()
		}
	}



	if ((parseInt(kCode) <= 57) && (parseInt(kCode) >= 48))
	{
		// general function to check consistency of data entered (all types of numeric data)
		if (CheckNumericOnKeyPressAndOnPaste(false, kCode, bForint, lMin, lMax) == false)
		{
			event.keyCode = ""
			return false
		}
		else
		{
			// function call to check whether values changed, and have user recalculate
			// first before repricing with new values
			AllControlsOnChange()
			return true
		}
	}
	else
	{
		event.keyCode = ""
		return false
	}
}


// function to check if data valid as numeric only with dash for account number
function CheckValueNumericDash(checkStr)
{
	var checkOK = "0123456789";
	var lCountComma = 0
	checkOK += "-"

	var allValid = true;

	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
			break;

			if (ch == "-") lCountComma += 1

			if (lCountComma > 1)
			{
				return false
				break
			}

		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		return (false);
	}
}

// function to check if data valid as numeric only or with decimal
function CheckValueNumeric(checkStr)
{
	if (document.activeElement.name == "agreementno")
	{
		var bRet = true
		bRet = CheckValueNumericDash(checkStr)
		return bRet
	}
	else
	{
		var checkOK = "0123456789";
		var allValid = true;

		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
				break;

			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
		}
		if (!allValid)
		{
			return (false);
		}
	}
}

// function to check if data valid numeric only, as amounts, as account numbers, and
// according to minimum / maximum values
// onkeypress and onpaste events handler
function CheckNumericOnKeyPressAndOnPaste(bPaste, kCode, bForint, lMin, lMax)
{
	// variables needed to calculate position of cursor in input boxes according to
	// width of text in pixels and width of 1 characther, taking into account inserting
	// characters, or writing over characters with a text selection.
	var oTxtRange

	var sValOrig
	var sValSel
	var sValIns
	var sValRes
	var sVR

	var lCurPos
	var bInsert

	var sVal

	oTxtRange = document.selection.createRange()

	var sValSel = oTxtRange.text
	var sValOrig = document.activeElement.value

	var sValIns = ""

	// position of cursor
	lCurPos = (oTxtRange.offsetLeft - 1) / lCharWidthPix

	bInsert = (sValSel.length == 0)

	if (bPaste)
	{
		sValIns = window.clipboardData.getData("Text")
	}
	else
	{
		sValIns = unescape("%" + toHex(kCode))
	}

	// if insertion, that is to say, adding character to the chain of characters
	if (bInsert)
	{
		// create end result string if string was to be allowed to be entered, upon characters insertion
		// by pasting or typing
		sValRes = sValOrig.substring(0, lCurPos) + sValIns + sValOrig.substring(lCurPos, sValOrig.length)

		if (bPaste)
		{
			if (document.activeElement.name = "agreementno")
			{
				if (sValRes.indexOf("-") != -1)
				{
					sValRes = sValRes.substring(0, sValRes.indexOf("-")) + sValRes.substring(sValRes.indexOf("-") + 1, sValRes.length)
					document.activeElement.value = sValRes
					return false
				}
			}
		}

		// if amount or account number, allow or not numbers starting with '0'
		if (bForint)
		{
			if (sValRes.length == 1)
			{
				if (sValRes == "0")
				{
					event.keyCode = ""
					return false
				}
			}
			else
			{
				// recursively remove any '0' at beginning of aamount values or pecentage
				sVR = RecursiveFindZeroBeginnings (sValRes)

				if (sVR != sValRes)
				{
					event.keyCode = ""
					document.activeElement.value = sVR
					return false
				}
			}
		}

		// format logic of numeric values according to minimum or maximum values if any
		if (sValRes.length == 1)
		{
			return true
		}
		else if (sValRes.length > document.activeElement.size)
		{
			event.keyCode = ""
			return false
		}
		else
		{

			if (lMin != 0)
			{
				if (parseInt(sValRes) < parseInt(lMin))
				{
					document.activeElement.value =  lMin
					event.keyCode = ""
					return false
				}
			}

			if (lMax != 0)
			{
				if (parseInt(sValRes) > parseInt(lMax))
				{
					document.activeElement.value =  lMax
					event.keyCode = ""
					return false
				}
			}

			return true
		}
	}
	else // if writing over selection of characters
	{
		// if overwriting with characters on top of selected area of text
		// end result string before showing value on screen and deciding whether to let go or not
		sValRes = sValOrig.substring(0, lCurPos) + sValIns + sValOrig.substring(lCurPos + sValSel.length, sValOrig.length)

		if (bPaste)
		{
			if (document.activeElement.name = "agreementno")
			{
				if (sValRes.indexOf("-") != -1)
				{
					sValRes = sValRes.substring(0, sValRes.indexOf("-")) + sValRes.substring(sValRes.indexOf("-") + 1, sValRes.length)
					document.activeElement.value = sValRes
					return false
				}
			}
		}

		// if amount or account number, allow or not numbers starting with '0'
		if (bForint)
		{
			if (sValRes.length == 1)
			{
				if (sValRes == "0")
				{
					event.keyCode = ""
					return false
				}
			}
			else
			{
				// recursively remove any '0' at beginning of aamount values or pecentage
				sVR = RecursiveFindZeroBeginnings (sValRes)

				if (sVR != sValRes)
				{
					event.keyCode = ""
					document.activeElement.value = sVR
					return false
				}
			}
		}

		// format logic of numeric values according to minimum or maximum values if any
		if (sValRes.length == 1)
		{
			return true
		}
		else if (sValRes.length > document.activeElement.maxLength)
		{
			sValRes = sValRes.substring(0, document.activeElement.maxLength)
		}

		if (lMin != 0)
		{
			if (parseInt(sValRes) < parseInt(lMin))
			{
				document.activeElement.value =  lMin
				event.keyCode = ""
				return false
			}
		}

		if (lMax != 0)
		{
			if (parseInt(sValRes) > parseInt(lMax))
			{
				document.activeElement.value =  lMax
				event.keyCode = ""
				return false
			}
		}

		return true
	}
}

// recursive helper function to remove all '0' at beginning of value
function RecursiveFindZeroBeginnings(sValRes)
{
	if (sValRes.charAt(0)== "0")
	{
		return RecursiveFindZeroBeginnings (sValRes.substring(1, sValRes.length))
	}
	else
	{
		if (sValRes == "")
		{
			sValRes = "0"
		}

		return sValRes
	}
}

// catch special keys delete and backspace for value changing check in input boxes
function CaptureSpecialKeysOnKeyUp(keyCode)
{
	if ((keyCode == 8) || (keyCode == 46))
	{
		if (event.srcElement.value != tempInputValue )
		{
			tempInputValue = event.srcElement.value
			AllControlsOnChange()
		}
	}
}

// catch special keys delete and backspace for value changing check in input boxes
function CaptureSpecialKeysOnKeyDown(keyCode)
{
	if ((keyCode == 8) || (keyCode == 46))
	{
		tempInputValue = event.srcElement.value
		//AllControlsOnChange()
	}
}

// event handler of onchange events of input, combos and radio boxes
// when values changed, do not allow repricing without recalculation
function AllControlsOnChange()
{
	if (event.srcElement.name == "Hogyan")
	{
		if (radioHogyan == event.srcElement.value)
		{
			return false
		}
	}

	if (vanEAtarazGomb == true)
	{
		ShowHideElement (false, "atarazlink")
		ShowHideElement (true, "nematarazhat")
	}
}

// onchange event handler of input boxes with amount values, and format value properly
function InputNumericForintOnChange()
{
	if (event.srcElement.value == "")
	{
		event.srcElement.value = "0"
		AllControlsOnChange()
	}
	else
	{
		event.srcElement.value = RecursiveFindZeroBeginnings(event.srcElement.value)
		AllControlsOnChange()
	}
}

// onchange event handler of input boxes with months number values, and format value properly
// with minimum and maximum limits
function InputOnChangeAdjustMinMaxValue(lMin, lMax)
{
	if (event.srcElement.value == "")
	{
		if (lMin != 0)
		{
			event.srcElement.value = lMin
			AllControlsOnChange()
		}
	}

	if (lMin != 0)
	{
		if (parseInt(event.srcElement.value) < parseInt(lMin))
		{
			event.srcElement.value = lMin
			AllControlsOnChange()
		}
	}

	if (lMax != 0)
	{
		if (parseInt(event.srcElement.value) > parseInt(lMax))
		{
			event.srcElement.value = lMax
			AllControlsOnChange()
		}
	}
}

// do not allow to apply same banning policy (ban or permit) on repricing twice and show current status
function TiltasCheckedOnClick()
{
	document.getElementById("tilt").value = document.getElementById("tiltcheck").checked

	if (document.getElementById("tiltcheck").checked)
	{
		document.getElementById("tiltlabel").innerText = "Igen"
	}
	else
	{
		document.getElementById("tiltlabel").innerText = "Nem"
	}
}

// helper function to hide (or show) an object and move relatively sibling objects to position
function ShowHideElement(s, name)
{
	var temp = document.getElementById(name)

  	if (s == false)
  	{
		temp.style.position = 'absolute'
    	temp.style.visibility = 'hidden'
  	}
	else
  	{
        temp.style.position = 'relative'
		temp.style.visibility = 'visible'
	}
}

// helper function to hide (or show) a table and move relatively sibling objects to position
function showhideTable(s, tablename) {
  var temp = document.getElementById(tablename);

  if (s==false) {
	  temp.style.position = 'absolute';
	  temp.style.visibility = 'hidden';
  }
  else {
	  temp.style.position = 'relative';
	  temp.style.visibility = 'visible';
  }
}

// send confirmation message box with text and given value for submitting forms or not
function check(text,val){

	if (confirm(text)) {

		document.getElementById("realsubmit").value = val
		document.form1.submit()
	}
}

function check2(text,val){

	if (confirm(text)) {

		document.getElementById("realsubmit").value = val
		document.form2.submit()
	}
}


function check3(text,val,confirmkell){

	if (confirm(text)) {
		if (confirmkell == 1 )
		{
			if (confirm ('Figyelmeztetés!!!\u000DEgyes törlesztőrészletek esedkessé válnának, vagy éppenséggel megszűnne az esedékessége!\u000DFolytatja?'))
			{

			document.getElementById("realsubmit").value = val
			document.form1.submit()
			}
		}
		else
		{
			document.getElementById("realsubmit").value = val
			document.form1.submit()
		}
	}
}


// handles typing Enter key
function handler(e) {
    var key = (navigator.appName == "Netscape") ? e.which : e.keyCode;
    if (key == 13) return false;
}

// helper functions to convert decimal to hexadecimal
function toHex(value)
{
    var val = "" + toHex2(Math.floor(value / 16));
    val += toHex2(value & 15);
    return val;
}

function toHex2(value)
{
	if (parseInt(value) <= 9)
	{
		return value
	}
	else
	{
		switch (value)
		{
			case 10:
				return "A"
				break

			case 11:
				return "B"
				break

			case 12:
				return "C"
				break

			case 13:
				return "D"
				break

			case 14:
				return "E"
				break

			case 15:
				return "F"
				break

			default:
				break

		}
	}
}

