﻿/* form code */

/* global form mandatory validator */
$(document).ready(function() {
    /* trim whitespace from textareas */
    if ($("textarea") && $("textarea").val && $("textarea").val()) {
        $("textarea").val($("textarea").val().replace(/^\s+|\s+$/g, ""));
    }

    /* prevent form submission with enter key */
    $(function() {
        $("form input").keypress(function(e) {
            if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
                $('button[type=submit] .default').click();
                return false;
            } else {
                return true;
            }
        });
    });

    $(".mandatory").change(function() {
        if ($(this).val() == "") {
            $(this).addClass("validationfail");
        } else { $(this).removeClass("validationfail"); }
        $(this).attr("title", "");
    });

    $(".mandatory.text").change(function() {
        if ($(this).val() == "") { $(this).attr("title", "You need to enter some information here"); }
    });

    $(".mandatory.textarea").change(function() {
        if ($(this).val() == "") { $(this).attr("title", "You need to enter some information here"); }
    });

    $(".product").change(function() {
        if ($(this).val() != "") {
            if ($(this).val() == "electricity") {
                $("#pmpanrequired").show();
                $("#pMprNorequired").hide();
            }
            if ($(this).val() == "gas") {
                $("#pMprNorequired").show();
                $("#pmpanrequired").hide();
            }
        }
    });

    /* BF 7 Nov 2010 - terminate letter add */
    $("input[name='psendletterbyemail']").change(function() {
        if ($("input[name='psendletterbyemail']:checked").val() == 'yes')
        // Code for handling value 'yes'
            $("#pemailrequired").show();
        else
        // Code for handling undefined or 'no'
            $("#pemailrequired").hide();
    });


    $(".mandatory.mmyyyy").change(function() {
        if ($(this).val() == "") { $(this).attr("title", "You need to choose a date"); }
    });

    $(".mandatory").keyup(function() { $(".mandatory").trigger("change"); });
    $(".mandatory").trigger("change");
});

/* toggle the checked state of a list of checkboxes based on the state of the 1st one */

function ToggleAll(chk) {
    check = chk[0].checked;
    for (i = 1; i < chk.length; i++)
        chk[i].checked = check;
}

function autotab(currentfield, nextfield, maxlen) {
    // Retrieve which key was pressed.
    var KeyID = (window.event) ? event.keyCode : e.keyCode;

    if (currentfield.value.length == maxlen) {
        if ((KeyID >= 48 && KeyID <= 90) || (KeyID >= 96 && KeyID <= 105)) nextfield.focus();
    }
    return true;
}





