//================================================
// Make's field readonly
//================================================
function controls_Readonly(state,elementId){
	var element;
	element = document.getElementById(elementId);
	if (element == null) return;
	element.readOnly = (state) ? true : false;	
}

//================================================
// Show's/hide's element
//================================================
function controls_Visible(state,elementId){
	var element;
	element = document.getElementById(elementId);
	if (element == null) return;
	element.style.visibility = (state) ? 'visible' : 'hidden';
}

//================================================
// Show's/hide's element
//================================================
function controls_Display(state,elementId){
	var element;
	element = document.getElementById(elementId);
	if (element == null) return;
	element.style.display = (state) ? 'block' : 'none';
}

//================================================
// Set's value of some element
//================================================
function controls_SetValue(elementId,value){
	var element;
	element = document.getElementById(elementId);
	if (element == null) return;
	switch(element.nodeName){
		case "INPUT":	
			if( !element.disabled ) element.value = value;
			break;
		case "SELECT":  
			if( !element.disabled ) element.selectedIndex = value;				
			break
		default:		
			break;
	}
}

function controls_DropDownData(element)
{
	data = document.getElementById(element.id + "_Data").childNodes;
	var newData = Array();
	var y=0;
	for (var i=0;i<data.length;i++){
		if (data[i].nodeName == "SPAN") {
			newData[y] = data[i];			
			y++;
		}		
	}		
	return newData;
}

// Fill textbox from 
function controls_ConstantSymbolSelected(caller, textboxId)
{
	if(caller.selectedIndex >= 0)
	{
		// get data
		var dataCol = controls_DropDownData(caller);
		var data = dataCol[caller.selectedIndex];
		document.getElementById(textboxId).value = data.getAttribute("Symbol");
	}
}

// Find constant symbol in combo that match data entered in textbox
// caller = textbox
// comboId = combo
function controls_ConstantSymbolFind(caller, isAddZeros, comboId)
{
	// modify constant symbol
	var entrySymbol = caller.value;
	if(entrySymbol==null) entrySymbol = '';
	
	if(isAddZeros && entrySymbol.length > 0)
	{
		entrySymbol = '0000' + entrySymbol;
		var len = entrySymbol.length;
		entrySymbol = entrySymbol.substring(len - 4, len);
		caller.value = entrySymbol;
	}
	
	var combo = document.getElementById(comboId);
	var dataCol = controls_DropDownData(combo);	
	for(var i=0;i<dataCol.length;i++)
	{		
		var data = dataCol[i];
		var symbol = data.getAttribute("Symbol");		
		if(entrySymbol == symbol)
		{
			if (combo.selectedIndex != i) 
				combo.selectedIndex = i;
			return;
		}		
	}
	combo.selectedIndex = 0;
}

//========================================================
// Select's row in table  [SELECTABLETABLEWITHCAPTION CONTROL]
//========================================================
function controls_SelectRow(rowElement)
{		
	var table = rowElement.parentNode.parentNode;
	if ( table.nodeName == "TABLE"){			
		var row = rowElement.getAttribute("row");
		if(row == null) // user selected no row, probably header
		{
			return;
		}
		
		var tableID = table.id;		
		var hidden = document.getElementsByName(tableID + "_row")[0];
		
		var zeroRow = parseInt(table.getAttribute("zeroRow"));
		var oldRow = parseInt(hidden.value);
		var currRow = parseInt(row);

		// unselect old row
		if(oldRow != -1)
		{
			var oldRowElement = table.rows[zeroRow + oldRow];
			oldRowElement.className = oldRowElement.getAttribute("defClass");
			oldRowElement.setAttribute("outClass", oldRowElement.className);
			hidden.value = -1;
		}

		// select new row, but only if it is different then old one
		if(currRow != oldRow)
		{
			rowElement.className = "selectedClass";
			rowElement.setAttribute("outClass", rowElement.className);
			hidden.value = currRow;
		}

		// fires event handler when row is selected
		var onSelectRow = table.getAttribute("OnSelectRow");
		if(onSelectRow != null)
			eval(onSelectRow + "(table,parseInt(hidden.value),param)");
	}		
}

function controls_SelectedRow(tableID)
{
	var hidden = document.getElementsByName(tableID + "_row")[0];
	return parseInt(hidden.value);
}

function controls_UnselectRow(table)
{
	if(table == null)
		return;

	var hidden = document.getElementsByName(table.id + "_row")[0];
	var oldRow = parseInt(hidden.value);
	
	if(oldRow != -1)
	{
		var zeroRow = parseInt(table.getAttribute("zeroRow"));		
		var oldRowElement = table.rows[zeroRow + oldRow];

		oldRowElement.className = oldRowElement.getAttribute("defClass");
		oldRowElement.setAttribute("outClass", oldRowElement.className);
		hidden.value = -1;		
		
		// fires event handler when row is unselected
		var onSelectRow = table.getAttribute("OnSelectRow");
		if(onSelectRow != null)
			eval(onSelectRow + "(table,-1,param)");
	}
}

function controls_TableSelectableStart(tableID)
{
	var table = document.getElementById(tableID);
	var currRow = controls_SelectedRow(tableID);
	var zeroRow = parseInt(table.getAttribute("zeroRow"));
	
	var row;
	for (var i = zeroRow; i < table.rows.length; i++) 
	{
		row = table.rows[i];
		row.setAttribute("defClass", row.className);
	}
	
	if(currRow != -1)
	{
		row = table.rows[zeroRow + currRow];
		row.className = "selectedClass";
		row.setAttribute("outClass", row.className);
	}
}

function controls_OverRow(rowElement){
	if (rowElement.className != "selectedClass") {
		rowElement.setAttribute("outClass", rowElement.className);
		rowElement.className = 'overClass';
	}
}

function controls_OutRow(rowElement){
	if (rowElement.className != "selectedClass")  {
		rowElement.className = rowElement.getAttribute("outClass");
	}
}


//========================================================
// Hide's group of tables [SHOWINGTABLEWITHCAPTION CONTROL]
//========================================================
function controls_HideGroup(group){
	// var i;
	//get all tables in document
	tables = document.getElementsByTagName('table');
		
	//if table id contains variable group then hide this table
	for (var i=0;i<tables.length;i++)
	{	
		var tbl = tables[i];
		var attrGrp = tbl.getAttribute("group");
		if(attrGrp == '') attrGrp = null; // fix for opera

		if (group != null && attrGrp != null)
		{	//is set group, hide all tables in this group
			if (attrGrp.indexOf(group) != -1)
				controls_HideTable(tbl);
		} 
		else 
		{
			var attrType = tbl.getAttribute("typeO"); // typeO because of opera
			//not set group, hide all tables without attribute group
			if(attrType == "showingTable" && attrGrp == null)
				controls_HideTable(tbl);
		}
	}
}


//========================================================
// Show's a table [SHOWINGTABLEWITHCAPTION CONTROL]
//========================================================
function controls_ShowTable(element){
	var table;
	
	//if element is not table set variable table on his parent node 
	if (element.nodeName != "TABLE") table = element.parentNode;
	else table = element;

	//while table is not table go to parent element
	while( table.nodeName != "TABLE" ){
		table = table.parentNode;
	}
	
	//show rows
	for(i=1;i<table.rows.length;i++){
		try {
			table.rows[i].style.display = 'table-row';				
		} catch(exp){
			table.rows[i].style.display = 'block';
		}
	}
	
	//set text, buton image and onclick action	
	if (document.getElementById(table.id + "_text") != null) document.getElementById(table.id + "_text").value = lang_Hide;
	if (document.getElementById(table.id + "_button") != null) {
		document.getElementById(table.id + "_button").src = "Img/arrow_hide_blue.gif";
		document.getElementById(table.id + "_button").onclick = new Function("controls_HideTable(this)");
	}
}

//========================================================
// Hide's a table [SHOWINGTABLEWITHCAPTION CONTROL]
//========================================================
function controls_HideTable(element){
	var table;
	
	//if element is not table set variable table on his parent node 
	if (element.nodeName != "TABLE") table = element.parentNode;
	else table = element;

	while( table.nodeName != "TABLE" ){
		table = table.parentNode;
	}
	
	//hide rows
	for(i=2;i<table.rows.length;i++){
		table.rows[i].style.display = 'none';				
	}
	//set text, buton image and onclick action
	if (document.getElementById(table.id + "_text") != null) document.getElementById(table.id + "_text").value = lang_Show;
	if (document.getElementById(table.id + "_button") != null) {
		document.getElementById(table.id + "_button").src = "Img/arrow_show_blue.gif";
		document.getElementById(table.id + "_button").onclick = new Function("controls_ShowTable(this)");
	}
}


//========================================================
// Fill sended fields [PARTNERSDROPDOWNLIST CONTROL]
//========================================================
function controls_FillPartnersFields(element,input1,drl2,input3,input4,drl5,input6,input7,input8,drl9){ //element = select(dropdown)	
	if(element.selectedIndex < 0)
		return;
	
	var data = controls_DropDownData(element);
	var node = data[element.selectedIndex];	
	
	input1 = document.getElementById(input1);
	drl2 = document.getElementById(drl2);
	input3 = document.getElementById(input3);
	input4 = document.getElementById(input4);
	drl5 = document.getElementById(drl5);
	input6 = document.getElementById(input6);
	input7 = document.getElementById(input7);
	input8 = document.getElementById(input8);
	drl9 = document.getElementById(drl9);
	
	var drl2value;
	if (node.getAttribute("issimpleItem") != "true")
	{	
		// fill partner
		var input1value = node.getAttribute("account_number");	
		if (input1 != null && input1value != null) input1.value = input1value;
		
		drl2value = node.getAttribute("account_bankcode_rowid");
		if (drl2 != null && drl2value != null) 
			if (drl2.options.length > drl2value) drl2.selectedIndex = drl2value;
			
		var input3value = node.getAttribute("amount_value");
		if (input3 != null && input3value != null) 
		{
			if(input3.disabled)
			{
				if(input3.value != input3value)
					element.selectedIndex = 0;
			}
			else
				input3.value = input3value;	
		}
			
		var input4value = node.getAttribute("constantsymbol_symbol");
		if (input4 != null && input4value != null) input4.value = input4value;
		
		var drl5value = node.getAttribute("constantsymbol_rowid");
		if (drl5 != null && drl5value != null) 
			if (drl5.options.length > drl5value) drl5.selectedIndex = drl5value;
			
		var input6value = node.getAttribute("variablesymbol");
		if (input6 != null && input6value != null) input6.value = input6value;					
		
		var input7value = node.getAttribute("specificsymbol");
		if (input7 != null && input7value != null) input7.value = input7value;					
		
		var input8value = node.getAttribute("additionaldata");
		if (input8 != null && input8value != null) input8.value = input8value;					
		
		var drl9value = node.getAttribute("amount_currency_rowid");
		if (drl9 != null && drl9value != null) 
			if (drl9.options.length > drl9value)
				if (drl9value != "0")
					drl9.selectedIndex = drl9value;
				else
				{
					var drl9items = controls_DropDownData(drl9);
					for (i = 0; i < drl9items.length; i++)
					{
						var myCurrency = drl9items[i].getAttribute("rowval");
						if (myCurrency.substring(0,3) == defaultCurrencyCode)
						{
							drl9.selectedIndex = i;
							break;
						}
					}
				}
	} 
	else 
	{ 
		// no partner
		if (input1 != null) input1.value = "";
		
		drl2value = node.getAttribute("account_bankcode_rowid");
		if (drl2 != null && drl2value != null) 
			if (drl2.options.length > drl2value) drl2.selectedIndex = drl2value;
			
		if (input3 != null) input3.value = "";
		if (input4 != null) input4.value = "";
		if (drl5 != null) drl5.selectedIndex = 0;					
		if (input6 != null) input6.value = "";
		if (input7 != null) input7.value = "";
		if (input8 != null) input8.value = "";
		
		drl9value = node.getAttribute("amount_currency_rowid");
		if (drl9 != null && drl9value != null) 
			if (drl9.options.length > drl9value) drl9.selectedIndex = drl9value;					
	}
}
//find partner
function controls_FindPartner(partnerDrlId,input1,drl2,input3,input4,input6,input7,input8,drl9)
{
	var partnerDrl = document.getElementById(partnerDrlId);
	if (partnerDrl == null) return;
	
	var data = controls_DropDownData(partnerDrl);
	
	//overenie ci aktualny vybraty nevyhovuje	
	for(var i=0;i<data.length;i++)
	{
		var node = data[i];
		if (node.getAttribute("issimpleitem") == null)
		{
			if(controls_CompareValues(node,"account_number",input1) && 
				controls_CompareValues(node,"account_bankcode_rowid",drl2) && 
				controls_CompareValues(node,"amount_value",input3) && 
				controls_CompareValues(node,"constantsymbol_symbol",input4) && 
				controls_CompareValues(node,"variablesymbol",input6) && 
				controls_CompareValues(node,"specificsymbol",input7) && 
				controls_CompareValues(node,"additionaldata",input8) && 
				controls_CompareValues(node,"amount_currency_rowid",drl9) )
			{
				var rowid = node.getAttribute("rowid");
				if(partnerDrl.selectedIndex != rowid)
					partnerDrl.selectedIndex = rowid;
				return;
			}
		}
	}	
	
	partnerDrl.selectedIndex = 0;	
}

//find partner ex
function controls_FindPartnerEx(partnerDrlId,input1,drl2)
{
	var partnerDrl = document.getElementById(partnerDrlId);
	if (partnerDrl == null) return;
	
	var data = controls_DropDownData(partnerDrl);
	
	//overenie ci aktualny vybraty nevyhovuje	
	for(var i=0;i<data.length;i++)
	{
		var node = data[i];
		if (node.getAttribute("issimpleitem") == null)
		{
			if(controls_CompareValues(node,"account_number",input1) && 
				controls_CompareValues(node,"account_bankcode_rowid",drl2) )
			{
				var rowid = node.getAttribute("rowid");
				if(partnerDrl.selectedIndex != rowid)
					partnerDrl.selectedIndex = rowid;
				return;
			}
		}
	}	
	
	partnerDrl.selectedIndex = 0;	
}

//compare attribute value with element value
function controls_CompareValues(nod,attribute,id){
	var element = document.getElementById(id);
	if (element == null) return true;

	var nodAttr = nod.getAttribute(attribute);
	if(nodAttr == null) return true;

	var value;	
	if (element.nodeName == "SELECT") value = element.selectedIndex;
	if (element.nodeName == "INPUT") value = common_nbsp_to_sp(element.value);
	
	return (value == common_nbsp_to_sp(nodAttr));
}

//=================================================================
// Show submenu - script for collapsing main menu
//=================================================================
function controls_ShowSubMenu(index){
		var countSubmenu = 11;
			
		for (i=1;i<=countSubmenu;i++){
			subMenu = document.getElementById("subMenu_"+i);
			if (subMenu != null)
				subMenu.style.display = (i == index) ? "block" : "none";
		}
}

//=================================================================
// Show submenu - script for collapsing main menu
//=================================================================
function controls_SelectSubMenuItem(mIndex, iIndex){
	var countSubmenu = 11;
	var countSubmenuItems = 11;
		
	for (i = 1; i <= countSubmenu; i++)
	{
        for (j = 1; j <= countSubmenuItems; j++)
        {
		    subMenuItem = document.getElementById("subItem_" + (mIndex == -1 ? "" : i) + "_" + (mIndex == -1 ? i : j));
		    if (subMenuItem != null)
		    {
		        for (k = 0; k < subMenuItem.childNodes.length; k++)
		        {
		            if (typeof(subMenuItem.childNodes[k].className) != 'undefined')
		            {
		                if (i == mIndex && j == iIndex || mIndex == -1 && i == iIndex)
		                    subMenuItem.childNodes[k].className = "selected";
		                else
		                    subMenuItem.childNodes[k].className = "unselected";
		                break;
		            }
		        }
		    }
	        else
		        break; // ak nenajde, skonci
        }
	}
}

//=================================================================
// controls_HelpWindow - script for help icon
//=================================================================
function controls_HelpWindow(link,params){
	var win;	
	win = window.open(link,null,params);	
	win.focus();
}


//=================================================================
// Enable's/disable's email fields [EMAIL CONTROL]
//=================================================================
function controls_EnableEmail(cg){
	if (cg.Selected.checked){
		common_Enable(true,cg.Text);
		// common_ValidationOn(cg.Text);
		common_Enable(true,cg.Encrypt);
		common_Enable(true,cg.InShort);
	} else {
		common_Enable(false,cg.Text);
		// common_ValidationOff(cg.Text);
		common_Enable(false,cg.Encrypt);
		common_Enable(false,cg.InShort);
	}
}

//=================================================================
// Enable/Disable validation for contact group
//=================================================================
// inputID id of new input control to turn validation on
// oldInput name of global variable where oldInput input control is
// stored
function controls_ContactFromGroup(inputID, oldInputVarName)
{
	var newi = document.getElementById(inputID);
	var oldi;
	eval("try{oldi=" + oldInputVarName + "}catch(e){}");
	
	common_ValidationOff(oldi);
	common_ValidationOn(newi);
	
	eval(oldInputVarName + "=newi");
}


//===================================================================
//	Calendar stuff
//===================================================================
function controls_OpenCalendar(textBoxDateID)
{
	var textBoxDate = document.getElementById(textBoxDateID);
	if(textBoxDate.disabled == false)
	{
		var txtID = textBoxDateID; // textBoxDate.getAttribute("ScreenDataKey");
		var url = hfBaseDir.value + "Common/Calendar.aspx?txtID=" + txtID
			+ "&DateStr=" + escape(textBoxDate.value);
		var wnd = window.open(url, 'CalendarWindow', 'titlebar=no,close=no,resizable=no,width=250,height=250');
		wnd.focus();
	}
}

function controls_SelectDate(textBoxDateID, dateStr)
{
	try
	{
		var openerDocument = window.opener.document;
		var textBoxDate = openerDocument.getElementById(textBoxDateID);
		if(textBoxDate.disabled == false /* && textBoxDate.getAttribute("ScreenDataKey") == screenDataKey */)
		{		
			textBoxDate.value = dateStr;
			
			if (textBoxDate.onblur != null)
				textBoxDate.onblur();
		}
	}
	catch(ex)
	{
	}
	
	window.close();
}

var lastLength = 0;

function controls_AutoTab(source, destination) {
	if (source.getAttribute && 
		lastLength == source.getAttribute("maxlength") - 1 &&
		source.value.length == source.getAttribute("maxlength"))
	{
		lastLength = lastLength + 1;
		
		if (typeof(destination) != 'undefined' && !destination.disabled)
			destination.focus();
	}
}

var Controls = 1;

