﻿//
// CSS Functions
//

function ChangeElementClass(elementId, className) {
	element = document.getElementById(elementId);
	element.className = className;
}

function ToggleElementVisibility(elementId) {
	element = document.getElementById(elementId);
	element.style.display = (element.style.display == "none") ? "block" : "none";
}

function ChangeElementVisibility(elementId, visible) {
	element = document.getElementById(elementId);
	element.style.display = (visible) ? "block" : "none";
}

function ToggleRowElementVisibility(elementId) {
	element = document.getElementById(elementId);
	element.style.display = (element.style.display == "") ? "none" : "";
}

function ChangeElementContent(elementId, content) {
	element = document.getElementById(elementId);
	element.innerHTML = content;
}

function ToggleElementContent(elementId, contentOne, contentTwo) {
	element = document.getElementById(elementId);
	element.innerHTML = (element.innerHTML == contentOne) ? contentTwo : contentOne;
}

// Trims whitespaces around a string
function Trim(inputString) 
{
	while (inputString.substring(0,1) == ' ')
	{
		inputString = inputString.substring(1, inputString.length);
	}
	while (inputString.substring(inputString.length-1, inputString.length) == ' ')
	{
		inputString = inputString.substring(0,inputString.length-1);
	}
	return inputString;
}

//
//include CSS
//
function IncludeCustomCss(url_, media_) 
{
    // We are preventing loading a file already loaded
    // var _links = document.getElementsByTagName("link");
    var _head = document.getElementsByTagName("head")[0];                
   
    // Loop through the length of the links
    //for( i = 0; _links.length > i; i++)
    //{
       // If the href is already present, remove it
    //    if (_links[i]["href"] == url_)
    //    {
    //        _head.removeChild(_links[i]);
    //    }
    //}

    // Optional parameters check
    var _media = media_ === undefined || media_ === null ? "all" : media_;
   
    // Build link element
    var _elstyle = document.createElement("link");
    _elstyle.setAttribute("rel", "stylesheet");
    _elstyle.setAttribute("type", "text/css");
    _elstyle.setAttribute("media", _media);
    _elstyle.setAttribute("href", url_);

    // Add style
    _head.appendChild(_elstyle);
}

//Validacija polja u formi Employment
function validateFormFieldsEm(warningTitle, fieldsArray, className) {
		warningMessage = '';
		for (i=0; i<fieldsArray.length; i++) {
			if (!isFormFieldFilled(fieldsArray[i][0])) {
				warningMessage += '\t- ' + fieldsArray[i][1] + '\n';
				if (fieldsArray[i][2] != '' && isFormFieldExists(fieldsArray[i][0]) && fieldsArray[i][0].indexOf("#")==-1 && fieldsArray[i][0].indexOf("$")==-1)
				{
					document.getElementById(fieldsArray[i][0]).className = fieldsArray[i][2];
				}
				
				// two fields with value comparison
				if (fieldsArray[i][0].indexOf("$") != -1)
				{
					formItem = fieldsArray[i][0].split('$');
					document.getElementById(formItem[0]).className = fieldsArray[i][2];
					document.getElementById(formItem[1]).className = fieldsArray[i][2];
				}
			}
		}
		if (warningMessage != '') {
			alert(warningTitle + '\n\n' + warningMessage);
			return false;
		}
		else
			return true;
	}
	
	//Rollover Image Scripts
	
	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
var arrPodnosioci = new Array();	

function DisableRadioButonOnSelect(selectedValue, ruleList, disableElementId)
{
    var disableElement = null;
    if(document.getElementById(disableElementId) != null)
    {
        disableElement= document.getElementById(disableElementId).getElementsByTagName('input');
    }
    
    if(disableElement != null)
    {
        var arrValue = new Array();
        arrValue = ruleList;
        
        //disable all
        for(var j = 0; j < disableElement.length; j++) 
        {
            try 
            {
                disableElement[j].parentElement.disabled=true; 
            }
            catch (eee) 
            {
            }
            
            disableElement[j].disabled = true;
            disableElement[j].checked = false;

        }
                                
        for(var i = 0; i < arrValue.length; i++)
        {
            if (arrValue[i].value == selectedValue.value) 
            {
                for(var j = 0; j < disableElement.length; j++) 
                {
                    if(disableElement[j].value == arrValue[i].enableId) 
                    {

                        try 
                        {
                            disableElement[j].parentElement.disabled=false; 
                        }
                        catch (eee) 
                        {
                        }
                        
                        disableElement[j].disabled = false;
                        
                        if(arrValue[i].selected == 'true')
                        {
                            disableElement[j].checked = true;
                        }
                        break;
                    }
                }
            }
        }
    }
}	

function PopoulateNazivDropDownOnSelect(selectedId, dropDownId, displayElementId)	
{
    ddElement = document.getElementById(dropDownId);
    
    var radioButtons = null;
    if(document.getElementById(selectedId) != null)
    {
        radioButtons= document.getElementById(selectedId).getElementsByTagName('input');
    }
    
    var selectedValue = '';
    
    if(radioButtons != null)
    {
        for (var x = 0; x < radioButtons.length; x ++) 
        {
            if (radioButtons[x].checked)
            {
                selectedValue = radioButtons[x].value;
                break;
            }
        }
    }

    if(ddElement != null)
    {        
        while(ddElement.length > 0)
        {
            ddElement.remove(ddElement.length - 1);
        }
        
        for (var i = 0; i < arrPodnosioci.length; i++)
        {
            if(selectedValue == arrPodnosioci[i].type) 
            {
                ddElement.options.add(new Option(arrPodnosioci[i].naziv, arrPodnosioci[i].id)); 
            }
        }
        
        if(ddElement.length > 0)
        {
            // Okrug display  
            DisplayOkrugOnSelectedChange(ddElement.options[0], displayElementId)
        }
        else
        {
            DisplayOkrugOnSelectedChange(0, displayElementId);
        }
    }
}

function DisplayOkrugOnSelectedChange(selectedValue, displayElementId) {
    
    displayElement = document.getElementById(displayElementId);
    
    if (displayElement != null) {


        //hide display element
        displayElement.value = "";
                        
        for (var i = 0; i < arrPodnosioci.length; i++) {

            if (arrPodnosioci[i].id == selectedValue.value) {
                //show display element
                displayElement.value = arrPodnosioci[i].okrug;
                
                break;
            }
        }  
    }
}

function showHideOnCheck(chk, txtBoxClientId)
{
	var textBox = document.getElementById(txtBoxClientId);

	if (chk.checked) 
	{
		textBox.style.display = '';
	}
	else
	{
		textBox.style.display = 'none';
		textBox.value = '';
	}
}

function ShowHideOnRadioSelect(radioValue, radioId, hideId)
{   
	var hideElement = document.getElementById(hideId);
	
    var radioButtons = null;
    if(document.getElementById(radioId) != null)
    {
        radioButtons= document.getElementById(radioId).getElementsByTagName('input');
    }
    
    if(radioButtons != null && hideElement!= null)
    {
        for (var x = 0; x < radioButtons.length; x ++) 
        {
            if (radioButtons[x].checked && radioButtons[x].value == radioValue)
            {
                hideElement.style.display = '';
                break;
            }
            else
            {
                hideElement.style.display = 'none';
            }
        }
    }
}

function ValidateCheckOnRadioSelect(checkValue, checkId, radioValue, radioId)
{
    var arrValue = new Array();
    arrValue = radioValue;
        
    var radioButtons = null;
    if(document.getElementById(radioId) != null)
    {
        radioButtons = document.getElementById(radioId).getElementsByTagName('input');
    }
    
    var chkList = null;
    if(document.getElementById(checkId) != null)
    {
        chkList = document.getElementById(checkId).getElementsByTagName('input');
    }
    
    var selectedValue = '';
    if(radioButtons != null)
    {
        for (var x = 0; x < radioButtons.length; x ++) 
        {
            if (radioButtons[x].checked)
            {
                selectedValue = radioButtons[x].value;
                break;
            }
        }
    }
    
    if(chkList != null && radioButtons != null)
    {
        for(var j = 0; j < arrValue.length; j++)
        {
            if(selectedValue == arrValue[j])
            {
                for(var i = 0; i < chkList.length; i++)
                {
                    if(chkList[i] != checkValue && checkValue.checked)
                    {

                        chkList[i].checked = false;
                    }
                } 
            }
        }
    }
}

function DeselectCheckList(checkId)
{   
    var chkList = null;
    if(document.getElementById(checkId) != null)
    {
        chkList = document.getElementById(checkId).getElementsByTagName('input');
    }

    if(chkList != null)
    {
        for(var i=0; i<chkList.length; i++)
        {
            chkList[i].checked = false;
        } 
    }
}

function SetValueToHidenField(selectedValue, hidenFieldId)
{
    hidenField = document.getElementById(hidenFieldId);
    
    if(hidenField != null)
    {
        hidenField.value = selectedValue;
    }
}

function SetRadioValueToHidenField(radioId, hidenFieldId)
{
    hidenField = document.getElementById(hidenFieldId);
    
    var radioButtons = null;
    if(document.getElementById(radioId) != null)
    {
        radioButtons = document.getElementById(radioId).getElementsByTagName('input');
    }
    
    var selectedValue = '';
    if(radioButtons != null)
    {
        for (var x = 0; x < radioButtons.length; x ++) 
        {
            if (radioButtons[x].checked)
            {
                selectedValue = radioButtons[x].value;
                break;
            }
        }
    }
    
    if(hidenField != null)
    {
        hidenField.value = selectedValue;
    }
}

var arrOblastiFinansiranja = new Array();
var arrOblastiFinansiranja2 = new Array();

function PopoulateOblastFinansiranjaOnSelectedChange(selectedValue, dropDownId, displayElementId, type)
{
    ddElement = document.getElementById(dropDownId);
    displayElement = document.getElementById(displayElementId);
    
    if(ddElement != null)
    {        
        while(ddElement.length > 0)
        {
            ddElement.remove(ddElement.length - 1);
        }
        
        if(selectedValue == -1)
        {
            if(displayElement != null)
            {
                displayElement.style.display = 'none';
            }
        }
        else if(type == 0)
        {
            for (var i = 0; i < arrOblastiFinansiranja.length; i++)
            {
                if(selectedValue == arrOblastiFinansiranja[i].parentId || arrOblastiFinansiranja[i].parentId == -1) 
                {
                    ddElement.options.add(new Option(arrOblastiFinansiranja[i].naziv, arrOblastiFinansiranja[i].id)); 
                }
            } 
            
            if(displayElement != null)
            {
                displayElement.style.display = '';
            }
        }
        else if(type == 2)
        {
            for (var i = 0; i < arrOblastiFinansiranja2.length; i++)
            {
                if(selectedValue == arrOblastiFinansiranja2[i].parentId || arrOblastiFinansiranja[i].parentId == -1) 
                {
                    ddElement.options.add(new Option(arrOblastiFinansiranja2[i].naziv, arrOblastiFinansiranja2[i].id)); 
                }
            } 
            
            if(displayElement != null)
            {
                displayElement.style.display = '';
            }
        }   
    }
    
}

function ValidateDecimalNumberEntry2(validateElement, ev)
{
    //validateElement = dociument.getElementById(elementId);
   
    ev.returnValue = false;
    
    if(validateElement != null)
    {
        var exp;
        
        if(ev.charCode != null)
        {
            if (ev.charCode == 0) 
            {
                return true;
            }
            else
            {
                exp = String.fromCharCode(ev.charCode);
            }
        }
        else
        {
            exp = String.fromCharCode(ev.keyCode);
        }
        
        var myRegExp = new RegExp("[^0-9,.]", "g");
        
        var count = exp.match(myRegExp);
        
        if (count == null || count.length == 0) 
        {
            
            if(!(validateElement.value.length == 0 && (exp == "." || exp == ",")))
            {
                myRegExp = new RegExp("[.]", "g");
             
                count = validateElement.value.match(myRegExp);
                
                if(count == null || count.length == 0 || (count.length == 1 && exp != "."))
                {
                    myRegExp = new RegExp("[,]", "g");
                 
                    count = validateElement.value.match(myRegExp);
                    
                    if(count == null || count.length == 0 || (count.length == 1 && exp != ","))
                    {
                        ev.returnValue = true;
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}

function ValidateDecimalNumberEntry(validateElement, ev)
{
    //validateElement = dociument.getElementById(elementId);
   
    ev.returnValue = false;
    
    if(validateElement != null)
    {
        var exp;
        
        if(ev.charCode != null)
        {
            if (ev.charCode == 0) 
            {
                return true;
            }
            else
            {
                exp = String.fromCharCode(ev.charCode);
            }
        }
        else
        {
            exp = String.fromCharCode(ev.keyCode);
        }
        
        var myRegExp = new RegExp("[^0-9]", "g");
        
        var count = exp.match(myRegExp);
        
        if (count == null || count.length == 0) 
        {
            ev.returnValue = true;
            return true;             
        }
        else
        {
            return false;
        }
    }
}

function ShowHideOnRadioButon(radioButton, hideElementId, showValue)
{
    var hideElement = document.getElementById(hideElementId);
	
    if(hideElement!= null)
    {
        if (showValue == radioButton)
        {
            hideElement.style.display = '';
        }
        else
        {
            hideElement.style.display = 'none';
        }
    }
}

function ValidateIntNumberEntry(validateElement, ev)
{
    //validateElement = dociument.getElementById(elementId);
   
    ev.returnValue = false;
    
    if(validateElement != null)
    {
        var exp;
        
        if(ev.charCode != null)
        {
            if (ev.charCode == 0) 
            {
                return true;
            }
            else
            {
                exp = String.fromCharCode(ev.charCode);
            }
        }
        else
        {
            exp = String.fromCharCode(ev.keyCode);
        }
        
        var myRegExp = new RegExp("[^0-9]", "g");
        
        var count = exp.match(myRegExp);
        
        if (count == null || count.length == 0) 
        {
            ev.returnValue = true;
            return true;             
        }
        else
        {
            return false;
        }
    }
}

//For Ajax Control Toolkit TabPanel manualu save client state
function OnChanged(sender, args)
{
    sender.get_clientStateField().value = sender.saveClientState();
}

function cleanTextBox(textBoxId)
{
    var textBox = document.getElementById(textBoxId);
	
    if(textBox!= null)
    {
    
        textBox.value = "";
    }
}