
function fragment(url){
    $('#main_content').hide();
    $('#loading').show();
    $.ajax({
        type: "GET",
        url: url,
        success: function(msg){
            if($.support.style){
                $('#main_content').css('height','100%'); // reset height in case some other routine has modified it (ie online forms)
            }
            $('#loading').hide();
            $("#main_content").html(msg);
            $('#main_content').show();
        }
    });
    $("#error").ajaxError(function(event, request, settings){
        $('#loading').hide();
        $(this).html("Error: " + request.responseText);
        $(this).show();
    });
    }
function toggle_banner(){
    $('#content_header_image_block_alt').toggle();
    $('#content_header_image_block').toggle()
}
function trim(str){
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function client_data_fill(select){
   var select_id = '#' + select.id;
   var form = $('form')[0]; // ie has to be first form
   var client_id = $(select_id).val();
   if(client_id == 'Null'){
       return true;
   }
   if(client_id == 'Clear Form'){
       if(confirm("Reset form? \n\nThis will clear all entered data!")){
           clear_highlite_invalids();
           form.reset();
           $(select_id).val('Null');
       }
       return true;
   }
   form.reset();
   var data = client_data[client_id];
   $.each(data, function(i,a){
        $(a[0]).val(a[1]);
   });
   return true;
}

/*
* From NS version of NMS Formmail
*
* NOTE: the NS version of NMS is not used as offered as it is too limiting
* but their JS validation is just fine for this application...
*
* What has been changed here is that the required vallues are derived from the required
* hidden element in the form rather than being manually entered.  This does require that
* the id of 'required_fields' be added to the element's attributes
*
* Also...added in both the modified form's email name to the validation (versus hidden).
* The hidden is used to always have a reply and from in the header
*
* And so on...
*/
function required_fields(){
   var fields = $('#required_fields').val().split(',');
   return fields;
}
function highlite_invalids(names){
    var error = false;
    $('.required').each( function() {
        if($.inArray(this.name, names) != -1){
        error = true;
        $(this).css('border','1px solid #FF0000');
        }
    });
    if(error){
       $('#required_data_error').html('Please complete the required fields below.');
       $('#required_data_error').show();
       $('html').scrollTop('#data_required_error');
    }
}
function clear_highlite_invalids(){
    $('#required_data_error').html('')
    $('#required_data_error').hide();
    $('.required').each( function() {
        $(this).css('border','1px solid #999999');
    });
}
function validate(frm) {
    //var inputFields = new Array("one required");
    var inputFields = required_fields();
    var counter;
    var name;
    var msg = "Please complete the following fields:\n";
    var badFields = "";
    var invalids = [];
    clear_highlite_invalids();
    for (counter = 0; counter < inputFields.length; counter++) {
        name = inputFields[counter];
        if (frm.elements[name].value.length == 0) {
            invalids.push(name);
            if (name == "formmail_mail_email") {
                badFields = badFields + "  - email required \n";
            } else {
                badFields = badFields + "  - " + name + "\n";
            }
        }
    }
    if (badFields.length != 0) {
        highlite_invalids(invalids);
        alert(msg + badFields);
        return false;
    }
    if(frm.Email_Address.value.length > 0){
        return emailCheck(frm.Email_Address.value);
    }else {
        return true;
    }
}

function emailCheck(emailStr) {
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);

    if (matchArray==null) {
        alert("Email address seems incorrect (check @ and .'s)");
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    if (user.match(userPat)==null) {
        alert("The username doesn't seem to be valid.");
        return false;
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
        if (IPArray[i]>255) {
            alert("Destination IP address is invalid!");
            return false;
            }
        }
        return true
    }
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
        alert("The domain name doesn't seem to be valid.");
        return false;
    }
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
    alert("The address must end in a three-letter domain, or two letter country.");
        return false;
    }
    if (len<2) {
        var errStr="This address is missing a hostname!";
        alert(errStr);
    return false;
    }
    return true;
}


