function validate(form, field, message)
{
try { 
    if (field != null && field.value != form.elements[field.id + 'Confirm'].value)
    {
        alert(message);
        return false;
    }
    else
        return true;
}
catch (e)
{ return false; } 
}

// document.form.field.selectedIndex, array, document.form.field)
function onSpeciesSelect(speciesCtrl, breedCtrl, allBreeds, breedNames) 
{ onSelectSubset(speciesCtrl, breedCtrl, allBreeds, breedNames); }

function onSelectCountry(countryCtrl, stateCtrl, allStates, stateNames, stateDiv)
{
    var count = onSelectSubset(countryCtrl, stateCtrl, allStates, stateNames); 
    show(stateDiv, count > 0); 
}

function onSelectSubset(mainCtrl, subCtrl, allIds, allNames)
{
    // Find currently selected ids. 
    var mainId = mainCtrl.selectedIndex == -1 ? "" : mainCtrl.options[mainCtrl.selectedIndex].value; 
    var subId = subCtrl.selectedIndex == -1 ? "" : subCtrl.options[subCtrl.selectedIndex].value; 

    // Kill all current options in sub array. 
    subCtrl.options.length = 0; 
    subCtrl.options[0] = new Option("", ""); 

    // Add sub items for selected main item. 
    var count = 0; 
    var i; 
    for (i = 0; i < allIds.length; i++) 
    {
        if (mainId == null || allIds[i].indexOf(mainId + ".") == 0)
        {
            subCtrl.options[subCtrl.options.length] = new Option(allNames[allIds[i]] ? allNames[allIds[i]] : allIds[i], allIds[i], subId == allIds[i], subId == allIds[i]); 
            count++; 
        }
    }
    
    return count; 
}

function setSpecies(speciesCtrl, breedid)
{ setCtrlBySubId(speciesCtrl, breedid); } 

function setCtrlBySubId(ctrl, subid)
{
    if (subid == null || subid == 'null')
        return; 
        
    for (i = 0; i < ctrl.options.length; i++) 
    {
        if (subid.indexOf(ctrl.options[i].value) == 0)
        {
            ctrl.selectedIndex = i; 
            return; 
        }
    }
    
    ctrl.selectedIndex = 0; 
}

function setBreed(ctrl, value)
{ setCtrlValue(ctrl, value); } 

function setCtrlValue(ctrl, value)
{
    if (value == null || value == 'null')
        return; 
        
    for (i = 0; i < ctrl.options.length; i++) 
    {
        if (value == ctrl.options[i].value)
        { 
            ctrl.selectedIndex = i; 
            return; 
        }
    }
    
    ctrl.selectedIndex = 0; 
}

function showBySelection(ctrl, value, divId, unselectedShow)
{
    if (ctrl.selectedIndex == -1)
        show(divId, unselectedShow); 
    else
        show(divId, ctrl.options[ctrl.selectedIndex].value == value);
}

function showBySelectionContains(ctrl, value, divId, unselectedShow)
{
    if (ctrl.selectedIndex == -1)
        show(divId, unselectedShow); 
    else
        show(divId, ctrl.options[ctrl.selectedIndex].value.indexOf(value) != -1);
}

function showBySelectionId(ctrl, value, divId, unselectedShow)
{
    if (ctrl.selectedIndex == -1)
        show(divId, unselectedShow); 
    else
        show(divId, ctrl.options[ctrl.selectedIndex].value == value);
}

function show(layerName, show)
{
    if (document.getElementById)
    {
        var targetElement = document.getElementById(layerName);
        if (targetElement == null)
            return false;

        if(show)
        {
            targetElement.style.visibility = show ? 'visible' : 'hidden';
            targetElement.style.display = show ? 'block' : 'none';
        }
        else
        {
            targetElement.style.visibility = 'hidden';
            targetElement.style.display = 'none';
        }
    }

    return false;
}

function encryptPassword(form, passwordId)
{
    if (passwordId == null)
        passwordId = "password"; 

    if (form.elements[passwordId].value == "")
        // don't encrypt empty password. 
        return true; 
        
    if (form.elements[passwordId + "Old"] && form.elements[passwordId + "Old"].value == form.elements[passwordId].value)
        // password didn't change - don't encrypt. 
        return true; 
        
    form.elements[passwordId].value = hex_md5(form.elements[passwordId].value); 
    
    return true; 
}

