function selectAll(cbxBox)
{
    var bChecked = cbxBox.checked;
	
	var tbl = document.getElementById("applicantList");
	var row, cell, ctrl;
	
	for(var i=0;i<tbl.rows.length;i++)
	{
		row = tbl.rows[i];
		
		cell = row.cells[row.cells.length-1];
		
		for(var y = 0;y<cell.childNodes.length;y++)
		{
		    ctrl = cell.childNodes[y];
    		
		    if(ctrl != null && ctrl.tagName != null && ctrl.tagName == "INPUT")
		    {
			    //alert(ctrl.tagName);
			    ctrl.checked = bChecked;
		    }
		}
	}
}



function loadDepartmentData(ctrl, defaultValue, useValue)
{
    //debugger;
    $('select.department').attr("disabled",true).emptySelect();
    $('select.business').attr("disabled",true).emptySelect();

    loadListData(ctrl, defaultValue, 'department', '/xml/Departments.ashx', useValue);

}

function loadBusinessData(ctrl, defaultValue, useValue)
{
    //debugger;
    $('select.business').attr("disabled",true).emptySelect();
    loadListData(ctrl, defaultValue, 'business', '/xml/jobbusinessunits.ashx', useValue);

}

function loadListData(ctrl, defaultValue, dropdown, url, useValue)
{
    //debugger;
    var value = ''; 
    
    if(useValue)
    {
        value = ctrl.value;
	}
	else
	{
        for (var i=0; i < ctrl.length;i++) {
	        value += ctrl.options[i].value + ',';
	    }	 
	}
	
    var dropdownSet = $('select.' + dropdown);
   $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
      alert(textStatus);
      alert(errorThrown);
      alert(XMLHttpRequest.responseText);
  }});

    if (value == defaultValue  || value == '') 
    {
          dropdownSet.attr("disabled",true);
          dropdownSet.emptySelect();
          
  
    }
    else
    {

        dropdownSet.attr("disabled",false);
        $.getJSON(
             url,
            {id:value},
            function(data){
              dropdownSet.loadSelect(data);
            }
        );

        
    }
}

function ConfirmAttach()
{
	return confirm("Are you sure you wish to attach this applicant to this vacancy?");
}

function printSelector(url,id)
{
	printUrl = url;
	var preview = document.getElementById("previewSelector");
	
	if(preview != null)
	{
	    preview.style.display = "block";
	    changedPreviewValue(id);
	}
}

function showHideApplicationSections(ctrlId, id, valueToShow)
{
    var ctrl = document.getElementsByName(ctrlId);
    
    if(ctrl == null)
    {
        return;
    }
    
    for(var i=0;i<ctrl.length;i++)
    {
        if(ctrl[i].value == valueToShow)
        {
            if(ctrl[i].checked)
            {
                showRows(id);
            }
            else
            {
                hideRows(id);
            }
        }
    }
}

function showHideEthnicOriginDDL(ctrlId, id, valuesToShow)
{      
    var ctrl = document.getElementsByName(ctrlId);   
    
    if(ctrl == null)
    {
        return;
    }
   
    for(var i=0; i<ctrl.length; i++)
    {
      for(var j=0; j<valuesToShow.length; j++)
      { 
            
        if(ctrl[i].value == valuesToShow[j])
        {
          showRow(id);
          return;
        }
        else
        {
          hideRow(id);
        }
      }   
    }
}

function printApplicationForm(url, type)
{
	window.location.href = url + type;
}

function deleteDocument()
{
    return confirm("Are you sure you wish to delete this document?");
}

function uploadDocument(id)
{
    var obj = document.getElementById(id);
  
    if(obj != null)
    {
        try
        {
  
            obj.style.display = 'table-row';
        }
        catch(err)
        {
            obj.style.display = 'inline';
        }
    }
    
}

function showHideImmigrationSection(ctrlId1,ctrlId2, rowNo,rowYes,rowOther)
{
    var ctrl1 = document.getElementsByName(ctrlId1);
    var ctrl2 = document.getElementById(ctrlId2);
    
    if(ctrl1 == null)
    {
        return;
    }
 
    for(var i=0;i<ctrl1.length;i++)
    {
        if(ctrl1[i].value == 1)
        {
            if(ctrl1[i].checked)
            {
                hideRow(rowNo);
                showRow(rowYes);
                if (ctrl2.value ==6)
                    showRow(rowOther);
                else
                    hideRow(rowOther);
            }
        }
        if(ctrl1[i].value == 2)
        {
            if(ctrl1[i].checked)
            {
                showRow(rowNo);
                hideRow(rowYes);
                hideRow(rowOther);
            }
        }
    }
}

function showRowOnlyOne(id)
{
    var strIds = new String(id);
    var Ids = strIds.split(" ,, ");
        
    for(var j=0; j<1;j++)
    {
        var obj = document.getElementById(Ids[j]);
        
        if(obj != null)
        {
            try
            {
                obj.style.display = 'table-row';
            }
            catch(err)
            {
                obj.style.display = 'inline';
            }
        }
    }
}

function showHideApplicationSection(ctrlId, id, valueToShow)
{
    
    var ctrl = document.getElementsByName(ctrlId);
    
    if(ctrl == null)
    {
        return;
    }
     
    for(var i=0;i<ctrl.length;i++)
    {
         if(ctrl[i].value == valueToShow)
        {
            if(ctrl[i].checked)
            {
                showRow(id);
            }
            else
            {
                hideRow(id);
            }
        }
    }
}

function showHideApplicationSectionDDL(ctrlId, id, valueToShow)
{
    var ctrl = document.getElementsByName(ctrlId);
    
    if(ctrl == null)
    {
        return;
    }
    
    for(var i=0;i<ctrl.length;i++)
    {
       
        if(ctrl[i].value == valueToShow)
        {
            showRow(id);
        }
        else
        {
            hideRow(id);
        }
    }
}

function showHideApplicationSectionWithCompare(ctrlId, id, valueToCompare, condition)
{
    var ctrl = document.getElementsByName(ctrlId);
    if(ctrl == null)
        return;
    for(var i=0;i<ctrl.length;i++)
    {

        switch(condition)
        {
            case ">":
               
                if(ctrl[i].value > valueToCompare)
                    showRow(id);
                else
                    hideRow(id);                        
                break;
            case "<":
                if(ctrl[i].value < valueToCompare)
                    showRows(id);
                else
                    hideRows(id);                        
                break;
            case "<=":
                if(ctrl[i].value <= valueToCompare)
                    showRows(id);
                else
                    hideRows(id);                     
                break;
            case ">=":
                if(ctrl[i].value >= valueToCompare)
                    showRows(id);
                else
                    hideRows(id);                 
                break;
            case "==":
                if(ctrl[i].value == valueToCompare)
                    showRows(id);
                else
                    hideRows(id); 
                break;
            case "!=":
                if(ctrl[i].value != valueToCompare)
                    showRows(id);
                else
                    hideRows(id); 
                break;
        }

    }
}

/*
function showHideEthnicOriginDDL(ctrlId, id)
{  
    //following are "id" values from "ehnics" Database table 
    //for all ethnicity fields with "please specify".
    var valuesToShow = new Array(5);
    valuesToShow[0] = 3;
    valuesToShow[1] = 7;
    valuesToShow[2] = 11;
    valuesToShow[3] = 14;
    valuesToShow[4] = 16;
    
    var ctrl = document.getElementsByName(ctrlId);   
    
    if(ctrl == null)
    {
        return;
    }
   
    for(var i=0; i<ctrl.length; i++)
    {
      for(var j=0; j<valuesToShow.length; j++)
      { 
            
        if(ctrl[i].value == valuesToShow[j])
        {
          showRow(id);
          return;
        }
        else
        {
          hideRow(id);
        }
      }   
    }
}
*/


 
function showHideEthnicOriginDDL(ctrlId, id, valuesToShow)
{      
    var ctrl = document.getElementsByName(ctrlId);   
    
    if(ctrl == null)
    {
        return;
    }
   
    for(var i=0; i<ctrl.length; i++)
    {
      for(var j=0; j<valuesToShow.length; j++)
      { 
            
        if(ctrl[i].value == valuesToShow[j])
        {
          showRow(id);
          return;
        }
        else
        {
          hideRow(id);
        }
      }   
    }
}


function hideRow(id)
{
    var strIds = new String(id);
    var Ids = strIds.split(" ,, ");
        
    for(var j=0; j<Ids.length;j++)
    {
        var obj = document.getElementById(Ids[j]);
    
        if(obj != null)
        {
            obj.style.display = 'none';
        }
    }
}

function showRow(id)
{
    var strIds = new String(id);
    var Ids = strIds.split(" ,, ");
        
    for(var j=0; j<Ids.length;j++)
    {
        var obj = document.getElementById(Ids[j]);
        
        if(obj != null)
        {
            try
            {
                obj.style.display = 'table-row';
            }
            catch(err)
            {
                obj.style.display = 'inline';
            }
        }
    }
}

function showHideApplicationSectionDL(ctrlId, id, id1, valueToShow, endosorsementsId)
{
    var ctrl = document.getElementsByName(ctrlId);
    
    if(ctrl == null)
    {
        return;
    }
    
    for(var i=0;i<ctrl.length;i++)
    {
        if(ctrl[i].value == valueToShow)
        {
            if(ctrl[i].checked)
            {
                showRow(id);
            }
            else
            {
                var obj = document.getElementsByName(endosorsementsId);
        
                for(var j=0;j<obj.length;j++)
                {
                    obj[j].checked = false;
                }
                
                hideRow(id + ' ,, ' + id1);
            }
        }
    }
}

function showHideApplicationSectionYesNo(ctrlId, yesIds, noIds)
{
    var ctrl = document.getElementsByName(ctrlId);
    
    if(ctrl == null)
    {
        return;
    }
    
    for(var i=0;i<ctrl.length;i++)
    {
        if(ctrl[i].value == 1)
        {
            if(ctrl[i].checked)
            {
                showRow(yesIds);
            }
            else
            {
                hideRow(yesIds);
            }
        }
        else if(ctrl[i].value == 2)
        {
            if(ctrl[i].checked)
            {
                showRow(noIds);
            }
            else
            {
                hideRow(noIds);
            }
        }
    }
}

function CheckNumaricField(evt)
{
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    
    if ((charCode < 45 || charCode > 57) && charCode != 8 && charCode != 37 && charCode != 39 && charCode != 9)
    {
        alert("This field accepts numbers only")
        return false    
    }
    return true;
}

function isInternalOrExternal(value)
{
    if(value == 1)
    {
        $("tr.external").show();
        $("tr.internal").hide();
    }
    else if(value == 2)
    {
        $("tr.external").hide();
        $("tr.internal").show();
    }
}

function selectIsInternalOrExternal()
{
    $("td.internalexternal input").each(function (e){
        if(this.checked)
        {
            isInternalOrExternal(this.value);
        }
    });
}
