﻿// JScript File





//only submit after page is completely loaded.    
function checkSubmit()
{
    return !(document.getElementById("__EVENTVALIDATION")==null) 
}

//check if the enter-key should submit or let the cursor proceed to the next line.          
function checkkey(e) 
{ 
    var target;
    if(e.target)
    {
        target = e.target;
    }
    else
    {
        if(e.srcElement)
        {
            target = e.srcElement;
        }
    }
    
    if(target.type != 'textarea')
    {  
        //disable the default enter-submit 
        if(e.keyCode==13)
        { 
            if (e && e.preventDefault)
            {              
                //firefox
                e.preventDefault();
	            return false;
            }
            //IE                   
            e.returnValue=false;
            e.cancel = true;
        }                 
    }    
}    
 
 

//validate MeSH terms, should be at least 3.
function validateMeSHTerms(sender, args) 
{  
    var trailingPart = sender.id.substring(0,sender.id.length - 16);
    var itemCountControl = document.getElementById(trailingPart + 'itemCount');
    args.isValid = true;                
    if(itemCountControl != null)
    {
        if(itemCountControl.value < 3)
        {
            args.IsValid = false;  
        }                              
    }                            
}          



//generic function for checking a 'mandatory checkboxlist'
//set the attribute sourceControl on the CustomValidator. 
function validateGenericCheckBoxList(sender, args)
{
    var allNotChecked = false;

    var trailingPart = sender.id.substring(0,sender.id.length - 16);
    var checkBoxListId = sender.getAttribute('sourceControl');        

    //check if there is at least one!
    var checkbox = document.getElementById(trailingPart + checkBoxListId + '_0');
    if(checkbox)
    {
      allNotChecked = isCheckBoxListAllNotChecked(trailingPart + checkBoxListId + '_');
    }
    args.IsValid = !allNotChecked;
}

//Function for checking checkboxlists / radiobuttonlist
//Just checks if one checkbox is filled out, if so returns false else true
function isCheckBoxListAllNotChecked(checkboxString)
{
    var found = true;            
    var allNotChecked = true;
    var i = 0;
    while(found && allNotChecked)
    {
        var checkbox = document.getElementById(checkboxString + i);
        if(checkbox != null)
        {
            if(checkbox.checked)
                allNotChecked =false;
        }
        else
        {
            found = false;
        }
        i++;
    }   
    
    return allNotChecked; 
}


//validate Date Of Birth
function validateDOB(sender, args)
{        
    var trailingPart = sender.id.substring(0,sender.id.length- 16);
    var month = document.getElementById(trailingPart + 'Months');
    var year = document.getElementById(trailingPart + 'Years');
    var day = document.getElementById(trailingPart + 'Days');                
    args.IsValid = true;                
    if(month != null && year != null && day != null)
    {
        if(month.selectedIndex==0 || year.selectedIndex ==0 || day.selectedIndex ==0)
            args.IsValid = false;                 
    }              
}


//generic function used for CVitems.
function validateGenericCVItem(sender, args) 
{ 
    var labelName = sender.getAttribute('sourceControl');    
    var Label =  document.getElementById(labelName); 
    
    args.IsValid = true;             
    if(Label != null) 
    {
        if(Label.innerHTML == "No items present")
            args.IsValid = false; 
    }
} 

//photo mandatory validator.
function validatePhoto(sender, args)
{
    var trailingPart = sender.id.substring(0, sender.id.length - 16);        
    var PhotoPathAndFileName = document.getElementById(trailingPart + '_PhotoPathAndFileName');
     args.IsValid = false;
    if(PhotoPathAndFileName != null)
    {
        if(PhotoPathAndFileName.value != "")
            args.IsValid = true;
    }

} 

//Generic function for clicking a linkbutton.
function genericButtonClick(e, source, target) 
{  
    if(e.keyCode==13)  
    {  
        var controlId = getPathOfControl(source.id) + target;                
        var control = document.getElementById(controlId);
        if(control)
        {
            var postBack = getPostBackString(control.getAttribute('href'));
            __doPostBack(postBack,'');
        }
    }  
} 

function getPostBackString(s1)
{
    var index = s1.indexOf('__doPostBack(');
    s1 = s1.substring(index + 14);
    var index2 = s1.indexOf("',");
    s1 = s1.substring(0,index2);   
    return s1;
}

function convertToPostBackString(s1)
{
    return s1.replace(/_/g,'$');
}

function getPathOfControl(s1)
{
    return s1.substring(0,getLastIndexOf(s1,'_'));
}

function getLastIndexOf(s1, s2)
{
   var rt = s1.length;
   var array = s1.split(s2);                   
   if(array.length > 1)
   {
      var length = array[array.length-1].length;
      rt = (s1.length - length);
   }        
   return rt;
} 


/* Abstract javascript check functions */

     function doCheckBoxListCheck(sender, args)
     {         
         var senderId = "" + sender.id;
         var departmentsId =  senderId.substring(0, senderId.length - 16) + "Departments_";               
         args.IsValid = !isCheckBoxListAllNotChecked(departmentsId);
         //alert('' + departmentsId);         
         //args.IsValid = false;         
     }
     
    //Function for checking checkboxlists..
    function isCheckBoxListAllNotChecked(checkboxString)
    {
        var found = true;            
        var allNotChecked = true;
        var i = 0;
        while(found && allNotChecked)
        {
            var checkbox = document.getElementById(checkboxString + i);
            if(checkbox != null)
            {
                if(checkbox.checked)
                    allNotChecked =false;
            }
            else
            {
                found = false;
            }
            i++;
        }   
        
        return allNotChecked; 
    }     
    
    function doCheckBoxListCheckOnDepartments(sender, args)
    {         
         var senderId = "" + sender.id;
         var HNumber =  senderId.substring(0, senderId.length - 16) + "HNumber";
         var number = document.getElementById(HNumber).getAttribute('Value');        

         var senderId2 = senderId.substring(0, senderId.length - 33);                 
         //ctl00_middleContent_TabD1_AuthorsAndDepartments1_Repeater2_ctl01_CustomValidator2
        
         args.IsValid = !isAllCheckBoxListsNotselected(number - 1,senderId2);
    }
    
    //Function for checking multipe checkboxlists..
    function isAllCheckBoxListsNotselected(numberToCheck, senderId)
    {
        //check for departments, should be at least one selected.
        var toCheck = senderId + '_Repeater1_ctl0';
        var found = true;            
        var allNotChecked = true;
        var i = 1;
        while(found && allNotChecked)
        {
            var checkbox = document.getElementById(toCheck + i + '_Departments_' + numberToCheck);
            if(checkbox != null)
            {
                if(checkbox.checked)
                    allNotChecked =false;
            }
            else
            {
                found = false;
            }
            i++;
        }   
        
        return allNotChecked; 
    }     