
// Javascript for the BITRE website
// Author: David Tormey
// Created: 16/11/2006

// Handles the IA category checkbox onclick event
function cbIACategory_onclick (cb)
{
    var disabled = false;
    var id = getId (cb);
    
    if (cb.checked)
        disabled = true;
        
    updateChildIACategories (id, disabled);
}

// This function is called by the onclick handler for the IA category checkboxes.  
// This function will recursively check / uncheck all the checkboxes related to 
// the Parent IA that was clicked
function updateChildIACategories (ParentId, disabled)
{
    var i = 0;
    for ( i = 0; i < document.aspnetForm.elements.length; i++ )
    {
        if (document.aspnetForm.elements[i].type == 'checkbox' )
        {
            if (getParentId (document.aspnetForm.elements[i]) == ParentId)
            {
                document.aspnetForm.elements[i].disabled = disabled;
                if (disabled)
                {
                    document.aspnetForm.elements[i].checked = false;
                }
                updateChildIACategories (getId (document.aspnetForm.elements[i]), disabled);
            }
        }        
    }
}

// This function is called by the updateChildIACategories function
function getParentId (cb)
{
    return cb.id.substring (cb.id.indexOf ("__") + 2);
}

// This function is called by the updateChildIACategories function
function getId (cb)
{
    return cb.id.substring (cb.id.indexOf ("cb") + 2, cb.id.indexOf ("__"))
}

function validateNewSubscriberForm ()
{
    var errorMsg = "The following fields are required:";
    
    // validate the form
    if (trim (document.getElementById ("ctl00_cphContent_txtName").value) == "")
    {
        errorMsg += "\n   ▪ Name";
    }
    
    if (trim (document.getElementById ("ctl00_cphContent_txtEmail").value) == "")
    {
        errorMsg += "\n   ▪ Email";
    }
    
    if (trim (document.getElementById ("ctl00_cphContent_txtPassword").value) == "")
    {
        errorMsg += "\n   ▪ Password";
    }
    
    if (trim (document.getElementById ("ctl00_cphContent_txtEmail").value) != trim (document.getElementById ("ctl00_cphContent_txtConfirmEmail").value))
    {
        errorMsg += "\n   ▪ Email and confirm email fields do not match";
    }
      
    if (trim (document.getElementById ("ctl00_cphContent_txtPassword").value) != trim (document.getElementById ("ctl00_cphContent_txtConfirmPassword").value))
    {
        errorMsg += "\n   ▪ Password and confirm password fields do not match"; 
    }
      
    if (trim (document.getElementById ("ctl00_cphContent_txtPassword").value).length < 6  || trim (document.getElementById ("ctl00_cphContent_txtPassword").value).length > 12)
    {
        errorMsg += "\n   ▪ Password length must be between 6 and 12 characters";
    }
    
    if (errorMsg == "The following fields are required:")
    {
        if (validateSubscriptionPreferences ())
        return true;
    }
    else
    {
        alert (errorMsg);
    }
    return false;
}

// This function is called on click of the Subscribe button on the SubscriptionNewMember.aspx
// page, and is used to validate the form.  It will use recursion to drill down to the third
// level IA category, and make sure that the user has clicked at least one IA category
function validateSubscriptionPreferences ()
{
    foundCheckedIACategory = false;
    var currentIALevel = 0;
    var id = 0;
    
    for( i = 0; i < document.aspnetForm.elements.length; i++ )
    {
        if (document.aspnetForm.elements[i].type == 'checkbox' )
        {
            // find the parent, has a parentId of null or ""
            if (getParentId (document.aspnetForm.elements[i]) == "")
            {
                id = getId (document.aspnetForm.elements[i]);
                break;
            }
            else
            {
                alert ("Error validating IA categories");
            }
        }
        
        
    }
    
    validateChildNodes (id, currentIALevel);
    
    if (!foundCheckedIACategory)
    {
        alert ("You must select at least one category from the subscription levels.");
        return false;
    }
    
    return true;
}

// This function is called recursively and used to find out if IA categories at the 
// correct level have been checked.
function validateChildNodes (ParentId, currentIALevel)
{
    if (foundCheckedIACategory)
    {
        return;
    }
    
    var i = 0;
    for ( i = 0; i < document.aspnetForm.elements.length; i++ )
    {
        if (document.aspnetForm.elements[i].type == 'checkbox' )
        {
            if (currentIALevel == 2)
            {
                if (document.aspnetForm.elements[i].checked)
                {
                    // found at least one valid IA category checked.. so break out
                    foundCheckedIACategory = true;
                    return;
                }
            }
            
            if (getParentId (document.aspnetForm.elements[i]) == ParentId)
            {
                currentIALevel++;
                validateChildNodes (getId (document.aspnetForm.elements[i]), currentIALevel);
            }
        }
        
        
    }
}

// Trim whitespace from a string
function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function validateEditSubscriber()
{
    var errorMsg = "The following fields are required:";
    
    // validate the form
    if (document.getElementById ("ctl00_cphContent_txtName").value == "")
    {
        errorMsg += "\n  ▪ Name";
    }

    if (document.getElementById ("ctl00_cphContent_txtEmail").value == "")
    {
        errorMsg += "\n  ▪ Email";
    }

    if (document.getElementById ("ctl00_cphContent_txtPassword").value != document.getElementById ("ctl00_cphContent_txtConfirmPassword").value)
    {
        errorMsg += "\n   ▪ Password and confirm password fields do not match";
    }

    var pword = document.getElementById ("ctl00_cphContent_txtPassword").value;
    var confPword = document.getElementById ("ctl00_cphContent_txtConfirmPassword").value;

    if (document.getElementById ("ctl00_cphContent_txtPassword").value != "") 
    {        
        if ((pword.length < 6) || (confPword.length > 12))
            {
                errorMsg += "\n   ▪ Password length must be between 6 and 12 characters ";
            }
    }     

    // validate categories
    if (errorMsg == "The following fields are required:")
    {
        if (validateSubscriptionPreferences ())
        {
            if (document.getElementById("ctl00_cphContent_rdoActive_1").checked)
            {
            
                if (!confirm("You will be unsubscribed from the BITRE publication subscription service.  Continue?"))
                {
                    return false;
                }
               
            }  
            return true;  
        }
        else
            return false;
        
    }
    else
    {
        alert (errorMsg);
        return false; 
    }
       
}

function validatePublicationSearch()

{
    if ((document.getElementById ("ctl00_cphContent_txtKeyword").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtTitle").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtAbstract").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtDescription").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtISSN").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtISBN").value == "") && 
            (document.getElementById ("ctl00_cphContent_txtPublicationDateStart").value == "") &&
            (document.getElementById ("ctl00_cphContent_txtPublicationDateEnd").value == ""))
    {
        if(document.getElementById ("ctl00_cphContent_dlContentType").value == "All" &&
            document.getElementById ("ctl00_cphContent_lstSubject").value == "All")
        {
        alert("You must enter some criteria to search on");
        return false;        
        }
    }
    
    return true;
}

function validatePublicationSearchSimple()

{
    if (document.getElementById ("ctl00_cphContent_txtKeyword").value == "") 
    {
        alert("You must enter some criteria to search on");
        return false;        
    }
        
    return true;
}

function GetTotalCost(control)
{
    var s = (control.value * document.getElementById("ctl00_cphContent_hidCost").value);
    document.getElementById ("ctl00_cphContent_Wizard1_lblAmount").innerText = "$" + s.toFixed(2).toString();
    //document.getElementById ("ctl00_cphContent_txtAmount").innerText = "$" + s.toFixed(2).toString();

    //alert(s);
}