var confirmCloseMsg;

//
// General funcions
//
function SetConfirmCloseMsg(msg)
{
    confirmCloseMsg = msg;
}

function OnChange()
{
    if(window.onbeforeunload || !$defined(confirmCloseMsg))
	return;
    window.onbeforeunload = function (event)
    {
	if(!event) event = window.event;
	event.returnValue = confirmCloseMsg;
	return event.returnValue;
    }
}

function OnSave()
{
    window.onbeforeunload = null;
}

function OnComposedFieldKeyUp(fieldName,fieldId,length)
{
    var field1 = document.getElementById(fieldName+'_'+fieldId);
    var field2 = document.getElementById(fieldName+'_'+(fieldId+1));
    if(field1.value.length == length) field2.focus();
}

function OnComposedFieldChange(fieldName,numOfFields)
{
    var field = document.getElementById(fieldName);
    field.value = "";
    for(var i=0;i<numOfFields;i++)
    {
	var compField = document.getElementById(fieldName+'_'+(i+1));
	field.value += compField.value;
    }
    OnChange();
}

function SetReadOnly(id)
{
    var field = document.getElementById(id);
    if(field)
    {
	field.setAttribute('readOnly','readOnly');
	field.className += ' disabled';
    }
}

function SetDisabled(id)
{
    var field = document.getElementById(id);
    if(field)
    {
	field.setAttribute('disabled','disabled');
	field.className += ' disabled';
    }
}

function SetEnabled(id)
{
    var field = document.getElementById(id);
    if(field)
    {
	field.removeAttribute('disabled');
	field.className = field.className.replace(/disabled/g,"");
    }
}

//
// Entity reference fields 
//
function InitEntityRef(id, value)
{
    var search = (value=='');

    if(search)
    {
	document.getElementById(id+'_search').style.display='block';
	var changeSelection = document.getElementById(id+'_changeSelection');
	if(changeSelection)
	    changeSelection.style.display='block';
    }
    else
	document.getElementById(id+'_selected').style.display='block';
}

function UpdateEntityRef(entityId, id, title, fieldName, histEntityType)
{
    document.getElementById(fieldName).value = entityId;
    document.getElementById(fieldName).setAttribute('title',id);
    document.getElementById(fieldName+'_title').value = title;
    histEntityType = histEntityType;
    document.getElementById(fieldName + '_search').style.display='none';
    document.getElementById(fieldName + '_selected').style.display='block';
    OnChange();
}

function UnselectEntityRef(fieldName)
{
    document.getElementById(fieldName).value='';
    document.getElementById(fieldName + '_selected').style.display='none';
    document.getElementById(fieldName + '_search').style.display='block';
    OnChange();
}

function InitLEORNPEntityRef(id, value, entityRefType)
{
    InitEntityRef(id, value);
    if(entityRefType=='naturalPerson')
	document.getElementById(id+'_entityRefType1').checked = true;
}


//
// Text area
//
function OnTextAreaKeyPress(field,size)
{
    if(field.value.length >= size)
	field.value = field.value.substring(0,size);
}

//
// Date field
//
function OnDateFieldChange(fieldName)
{
    var field = document.getElementById(fieldName);
    var fieldDay = document.getElementById(fieldName+'Day');
    var fieldMonth = document.getElementById(fieldName+'Month');
    var fieldYear = document.getElementById(fieldName+'Year');
    if(fieldYear.value=='' && fieldMonth.value=='' && fieldDay.value=='')
	field.value = '';
    else
	field.value = fieldYear.value+'-'+fieldMonth.value+'-'+fieldDay.value;
    OnChange();
}


//
// Float fields
//
var fracDigits,decimalSep,thousandSep,locFracDigits,locDecimalSep,
    locThousandSep;
function SetLocaleConv(fracDigits, decimalSep, thousandSep)
{
    locFracDigits = fracDigits;
    locDecimalSep = decimalSep;
    locThousandSep = thousandSep;
}

function GetFracDigits(fracDigits)
{
    if($defined(fracDigits))
	return fracDigits;
    if($defined(locFracDigits))
	return locFracDigits;
    return 2;
}

function GetDecimalSep(decimalSep)
{
    if($defined(decimalSep))
	return decimalSep;
    if($defined(locDecimalSep))
	return locDecimalSep;
    return '.';
}

function GetThousandSep(thousandSep)
{
    if($defined(thousandSep))
	return thousandSep;
    if($defined(locThousandSep))
	return locThousandSep;
    return ',';
}

function FloatToStr(num, fracDigits, decimalSep, thousandSep)
{
    fracDigits = GetFracDigits(fracDigits);
    decimalSep = GetDecimalSep(decimalSep);
    thousandSep = GetThousandSep(thousandSep);
	       
    if(!isFinite(num))
	num = 0;
    var result = '';
    var numInt;
    var numDec;
    if(fracDigits > 0)
    {
        numInt = parseInt(num).toString();
	numDec = Math.round(Math.abs(num-numInt)*Math.pow(10,fracDigits)).toString();
 	while(numDec.length < fracDigits)
 	    numDec = '0' + numDec; 

	if(parseInt(numDec).toString().length > fracDigits)
        {
 	    numDec = numDec.substr(1);
	    numInt = parseInt(numInt)+1;
	    numInt = numInt.toString();
        }
    }
    else
        numInt = Math.round(num).toString();
    var numChar = 0;
    for(var i=numInt.length-1; i >= 0;i--)
    {
	if(numChar>0 && numChar%3 == 0 && !(num<0 && i==0))
	    result = thousandSep + result;
	result = numInt.charAt(i) + result;
	numChar++;
    }
    if(fracDigits > 0)
	result = result + decimalSep + numDec;
    return result;
}

function StrToFloat(num, decimalSep, thousandSep)
{
    decimalSep = GetDecimalSep(decimalSep);
    thousandSep = GetThousandSep(thousandSep);

    if(typeof num != 'string')
	return num;
    if(thousandSep == '.')
	num = num.replace(/\./g, '');
    else
	num = num.replace(RegExp(thousandSep, 'g'), '')
    if(decimalSep != '.')
	num = num.replace(decimalSep, '.');
    num = parseFloat(num);
    if(isNaN(num))
	return (parseFloat(0));
    return num;
}

function OnFloatFieldChange(field)
{
    field.value=FloatToStr(StrToFloat(field.value, decimalSep, thousandSep), 
			   fracDigits, decimalSep, thousandSep);
    OnChange();
    EvalFormulas();
}


//
// Integer fields
//
function OnIntegerFieldChange()
{
    OnChange();
    EvalFormulas();
}



//
// Select fields
//
function OnSelectFieldChange(value,targetField,extraFields,keyField)
{
    OnChange();
    if(targetField != '')
    {
	var link = 'urlLoader.location=\'index.php?pageId=' + loadFieldPageId;
	link += '&fieldName='+targetField+'&sourceId=' + value;
	if($defined(extraFields))
	    link += extraFields;
	link += '\'';
	eval(link);
    }
}

//
// Formula fields
//
var expList;
function InitFormulas()
{
    expList = new Array();
}

function AddFormula(expression)
{
    var idx = expList.length;
    expList[idx] = expression;
    return idx;
}

function InsertFormula(expression,idx)
{
    expList.splice(idx,0,expression);
}

function EvalFormulas()
{
    if(!$defined(expList))
	return;
    for(var i=0; i<expList.length;i++)
	eval(expList[i]);
}

function SetNodeValue(id, expression)
{
    eval('window.'+id+'='+expression);
    if(!isFinite(eval('window.'+id)))
	eval ('window.'+id+'=0');
    var value = FloatToStr(eval('window.'+id), fracDigits, decimalSep,
			   thousandSep);

    var node = document.getElementById(id);
    if(!node)
	return;

    switch(node.nodeName)
    {
    case 'TD':
	if(node.firstChild)
	    node.firstChild.nodeValue = value;
	else
	    node.appendChild(document.createTextNode(value));
	break;
    case 'INPUT':
	node.value = value;
	break;
    }
}

function GetNodeValue(id)
{
    if(eval('typeof window.'+ id) != 'undefined' &&
       eval('typeof window.'+ id) != 'object')
	return eval('window.' + id);

    var node = document.getElementById(id);
    if(!node)
    {
	alert(id + ' not found!');
	return '';
    }

    switch(node.nodeName)
    {
    case 'TD':
	if(node.firstChild) 
	    return node.firstChild.nodeValue;
	break;
    case 'INPUT':
	return node.value;
    }
}

function InitCheckbox(fieldName, value)
{
    if(value=='1')
	$(fieldName+'Checkbox').set('checked',true);
}

function OnCheckboxChange(fieldName)
{
    if($(fieldName+'Checkbox').checked)
	$(fieldName).value = '1';
    else
	$(fieldName).value = '0';
    OnChange();
}
