﻿var clientIds = new Object;
var ddlOrder;
var ddlSpecies;
var rdoNamingCommon;
var rdoNamingScientific;
var hidSpecies;

addLoadEvent(init);

function init()
{
    var btn = document.getElementById(clientIds.btnNaming);
    btn.style.display = "none";
    btn = document.getElementById(clientIds.btnOrder);
    btn.style.display = "none";
    rdoNamingCommon = document.getElementById(clientIds.rdoNamingCommon);
    rdoNamingCommon.onclick = namingChange;
    rdoNamingScientific = document.getElementById(clientIds.rdoNamingScientific);
    rdoNamingScientific.onclick = namingChange;
    ddlOrder = document.getElementById(clientIds.ddlOrder);
    ddlOrder.onchange = orderChange;
    ddlSpecies = document.getElementById(clientIds.ddlSpecies);
    ddlSpecies.onchange = speciesChange;
    hidSpecies = document.getElementById(clientIds.hidSpecies);
    hidSpecies.value = ddlSpecies.value;
}

function namingChange()
{//change between scientific and common naming
    var naming;
    var order = ddlOrder.value;
    var currentOrder = ddlOrder.selectedIndex;
    ddlOrder.options.length = 0;
    switch (this)
    {
        case rdoNamingCommon:
            ddlOrder.options[0] = new Option("Grasshoppers & crickets", "Orthoptera");
            ddlOrder.options[1] = new Option("Cockroaches & praying mantids", "Dictyoptera");
            ddlOrder.options[2] = new Option("Earwigs", "Dermaptera");
            ddlOrder.options[3] = new Option("Stick insects", "Phasmida");
            naming = "Common"
            break;
        case rdoNamingScientific:
            ddlOrder.options[0] = new Option("Orthoptera", "Orthoptera");
            ddlOrder.options[1] = new Option("Dictyoptera", "Dictyoptera");
            ddlOrder.options[2] = new Option("Dermaptera", "Dermaptera");
            ddlOrder.options[3] = new Option("Phasmida", "Phasmida");
            naming = "Scientific"
            break;
    }
    ddlOrder.selectedIndex = currentOrder;
    proxyTaxonService.GetNamesByOrder(order, naming, successSelectTaxa);
}

function orderChange()
{//change species list for different orders
    var naming;
    var order = ddlOrder.value;
    if (rdoNamingCommon.checked == true) naming = "Common";
    if (rdoNamingScientific.checked == true) naming = "Scientific";
    proxyTaxonService.GetNamesByOrder(order, naming, successSelectTaxa);
}

function successSelectTaxa(result)
{//callback for proxyTaxonService
    var currentSpecies = ddlSpecies.value;
    var currentSpeciesValid = false;
    ddlSpecies.options.length = 0;
    for (var i in result)
    {
        ddlSpecies.options.add(new Option(result[i].Text, result[i].Value));
        if (result[i].Value == currentSpecies) currentSpeciesValid = true;
    }
    if (currentSpeciesValid) 
        ddlSpecies.value = currentSpecies;
    else
        ddlSpecies.selectedIndex = 0;
    hidSpecies.value = ddlSpecies.value;
}

function speciesChange()
{//save species in hidden field so server can access selected value
    hidSpecies.value = ddlSpecies.value;
}

function vldLocation_ClientValidate (source, arguments)
{//it is not valid to nave neither gridref nor postcode
    gridref = document.getElementById(clientIds.txtGridRef).value;
    postcode = document.getElementById(clientIds.txtObsPostcode).value;
    
    if (gridref == "" & postcode == "")
    {
        arguments.IsValid = false;
    }
    else
    {
        arguments.IsValid = true;
    }
}

function vldVerificationEmail_ClientValidate (source, arguments)
{//it is not valid to ask for verification without an email
    verification = document.getElementById(clientIds.chkVerification).checked;
    email = document.getElementById(clientIds.txtEmail).value;
    if (email == "" & verification == true)
    {
        arguments.IsValid = false;
    }
    else
    {
        arguments.IsValid = true;
    }
}

function vldVerificationPhoto_ClientValidate (source, arguments)
{//it is not valid to ask for verification without a photo
    verification = document.getElementById(clientIds.chkVerification).checked;
    photo0 = document.getElementById(clientIds.uplPhoto0).value;
    photo1 = document.getElementById(clientIds.uplPhoto1).value;
    photo2 = document.getElementById(clientIds.uplPhoto2).value;
    photo3 = document.getElementById(clientIds.uplPhoto3).value;
    if (photo0 == "" & photo1 == "" & photo2 == "" & photo3 == "" & verification == true)
    {
        arguments.IsValid = false;
    }
    else
    {
        arguments.IsValid = true;
    }
}

function vldPhoto_ClientValidate (source, arguments)
{////files are only valid if they have one of the following extensions.
    allowedExts  = new Array(".jpg", ".jpeg", ".png", ".gif");

    arguments.IsValid = false
    filename = arguments.Value
    if (filename != "")
    {
        startExt = filename.lastIndexOf(".");
        if (startExt)
        {//file has extension extension
            fileExt = filename.substr(startExt);
            for (j in allowedExts)
            {//check extension against allowed values
                if (fileExt.toLowerCase() == allowedExts[j])
                {//until a match is made
                    arguments.IsValid = true
                    return;
                }
            }
        }
    }
}




function fileNameValid(filename)
{//files are only valid if they have one of the following extensions.

    
}