﻿

function checkIt(evt) 
{
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    
    if (charCode == 13) 
    { 
        return false; 
    }
    
    return true;
}

function SkontrolujEnterAjCisla(evt) 
{
    evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    
    if ((charCode != 44 && charCode > 31) && (charCode < 48 || charCode > 57) || charCode == 13 ) 
    { 
        return false; 
    }
    
    return true;
}

function Vymaz(prvok, hodnota, udalost) 
{
    if(prvok.value == hodnota)
    {
        prvok.value = '';
    }
    else if(prvok.value == '' && udalost == 'blur')
    {
        prvok.value = hodnota;
    }
}
   
function parseInt2( str )
{
   return str.substring(str.indexOf("_",0)+1);      
        
}

function codeSelected(select_object)
{
  codedString = '';
  for (i=0; i<select_object.options.length; i++)
  {
    codedString = codedString + ',' + parseInt2(select_object[i].value);
  }
  return codedString.substring(1);
}


// Add new item to select field
function addItemToSelect(select_field, value, description)
{
    var i, j, len;
	
    for (i=0; i<select_field.options.length; i++)
    {
        if (select_field.options[i].value == value)
        {
	        break;
        }
        else
        {
	        if (parseInt(select_field.options[i].value) > parseInt(value))
	        {
		        len = select_field.options.length;
		        for (j=len-1; j>=i; j--)
		        {
			        select_field.options[j+1] = new Option(select_field.options[j].text, select_field.options[j].value);
			        //select_field.options[j+1] = select_field.options[j];
		        }
		        select_field.options[i] = new Option(description, value);
		        break;
	        }        			
        }
    }
	
    if (i == select_field.options.length)
    {
        select_field.options[i] = new Option(description, value);
    }
}

// Remove field from select box
function removeItemFromSelect(select_field, index)
{
    select_field.options[index] = null;
}


// Move all selected fields from one select box to another
function moveItemsFromSelectToSelect(select_field_from, select_field_to)
{
    var i;
	
    unselectAllItems(select_field_to);

    if((select_field_to.name.lastIndexOf('2') >= 0 && select_field_to.length >= 4))
    {
        alert('Je možné zadať maximálne 4 položky do zoznamu.');
    }
    else
    {
        for (i=select_field_from.options.length-1; i>=0; i--)
        {
            if (select_field_from.options[i].selected)
            {
	            addItemToSelect(select_field_to, 
					            select_field_from.options[i].value, 
					            select_field_from.options[i].text);
            }
        }
        for (i=select_field_from.options.length-1; i>=0; i--)
        {
            if (select_field_from.options[i].selected)
            {
	            removeItemFromSelect(select_field_from, i); 
            }
        }
	    document.forms[0].ctl00$ContentPlaceHolder1$hidVyberteOdvetvie.value = '';
        for (i=document.forms[0].ctl00$ContentPlaceHolder1$lbxVyberteOdvetvie2.options.length-1; i>=0; i--)
        {
            document.forms[0].ctl00$ContentPlaceHolder1$hidVyberteOdvetvie.value += document.forms[0].ctl00$ContentPlaceHolder1$lbxVyberteOdvetvie2.options[i].value + '#';
	    }
	}
}

// Select all fields in the select box
function selectAllItems(select_field)
{
    for (i=0; i<select_field.options.length; i++)
    {
        select_field.options[i].selected = true;
    }
}

function unselectAllItems(select_field)
{
    for (i=0; i<select_field.options.length; i++)
    {
        select_field.options[i].selected = false;
    }
}

// move all selected items up
function moveUp(select_field)
{
    var i, new_option;
		
for (i=1; i<=select_field.options.length-1; i++)
    {
        if ((select_field.options[i].selected) && (!(select_field.options[i-1].selected)))
        {
			        new_option = new Option(select_field.options[i-1].text, select_field.options[i-1].value);
//					select_field.options[i-1] = select_field.options[i];
			        select_field.options[i-1] = new Option(select_field.options[i].text, select_field.options[i].value);
			        select_field.options[i-1].selected = true;
			        select_field.options[i] = new_option;  		
        }
    }
}

// move all selected items down
function moveDown(select_field)
{
    var i, new_option;
		
    for (i=select_field.options.length-2; i>=0; i--)
    {
        if ((select_field.options[i].selected) && (!(select_field.options[i+1].selected)))
        {
			        new_option = new Option(select_field.options[i+1].text, select_field.options[i+1].value);
//					select_field.options[i+1] = select_field.options[i];
			        select_field.options[i+1] = new Option(select_field.options[i].text, select_field.options[i].value);
			        select_field.options[i+1].selected = true;
			        select_field.options[i] = new_option;  		
        }
    }
}

// checks max allowed input count for positions, categories, education_levels
function checkMaxInput(from_field, to_field, max, alert_msg, alert_msg_end )
{
    selected = to_field.options.length;
    to_move = 0;
    for (i=0; i<from_field.options.length; i++)
    {
        if (from_field.options[i].selected)
        {
	        to_move++;
        }
    }

    if (selected+to_move > max)
    {
      window.alert(alert_msg+' '+max + ' ' + alert_msg_end );
        return false;
    }
    return true;
}

var DigitsAfterDecimal = 2;
function CheckUnit(txt)
    {
        
        if(document.getElementById(txt).value == '' )
        {
            alert("Please enter valid decimal value");
            return false;
        }
        else
        {
            
           if(isNaN(document.getElementById(txt).value))
           {
                alert("Please enter valid decimal value");
                return false;
           }
           else
           {
                var val = document.getElementById(txt).value;
                if(val.indexOf(",") > -1)
                {
                    
                    if(val.length - (val.indexOf
                        (".")+1) > 
                           DigitsAfterDecimal)
                    {
                        alert("Please enter valid decimal value. Only " + DigitsAfterDecimal + " digits are allowed after decimal.");
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    if(parseInt(val) > 0)
                    {
                        return true;
                    }
                    else
                    {
                        alert("Value must be greater than 0"); 
                        return false;
                     }
                }
           }
        }
    }