$(document).ready(function(){
    // CUFON
    Cufon.replace('h1, h2, h3, h4, #topnav li a, ul.sub li a, #masthead p, .footer-info ul li, ul.ads li > a > p, .pagination div, .visit-footer ul li a');
    Cufon.replace('.footer-nav a', {hover: true});


    // MAIN NAV AND SUB NAV
    
    // open main nav section with subnavs on hover
    $("ul#topnav li").hoverIntent(
        function(){
            if (!$(this).hasClass('default')){
                $(this).children("ul.sub").stop(true,true).fadeIn(300);
            }
        },
        function(){
            if (!$(this).hasClass('default')){
                $(this).children("ul.sub").stop(true,true).fadeOut(300);
            }
        }
    );
    
    // FORM BLUR SELECT INPUT       
    $("input, textarea").focus(
        function(){
            if ($(this).attr('alt') != ''){
                if ($(this).val() == $(this).attr('alt')){
                    $(this).addClass('active');
                    $(this).val('');
                }
            }
        }
    );
    $("input, textarea").blur(
        function(){
            if ($(this).attr('alt') != ''){
                if ($.trim($(this).val()) == ''){
                    $(this).removeClass('active');
                    $(this).val($(this).attr('alt'));
                }
            }
        }
    );

    $('a.rlist').live('click',function(){
        var id = $(this).attr('href').substr(1);
        var group = $(this).closest('li');
        
        if ($(this).hasClass('active')){
            $(this).removeClass('active');
        } else {
            $(this).addClass('active');
        }
        
        $(this).closest('li').find('.resultlist').each(function(index){
            if ($(this).hasClass(id)){
                if ($(this).hasClass('active')){
                    $(this).removeClass('active');
                    $(this).slideUp(250);
                } else {
                    $(this).addClass('active');
                    $(this).slideDown(250);
                }
            }
        });

        return false;
    });
    
    // scroller ads/imgs/etc
    if ($('.scroller').length > 0){
        $('.scroller').each(function(){
            $scroller = $(this);
            $list = $scroller.find('ul.scroller-list');
            $list.children('li:last-child').css({'margin-right':'0px'});
            var adNum = $list.children().length;
            var adWidth = $list.children('li:first-child').width();
            var adMargin = parseInt($list.children('li:first-child').css('margin-right'));
            var totalScrollWidth = (adNum*adWidth)+((adNum-1)*adMargin);
            $list.css({'width':totalScrollWidth+'px'});

            if ($scroller.hasClass('ads')){
                var type = 'ads';
                var displayMax = 3;
            } else if ($scroller.hasClass('imgs')){
                var type = 'gallery';
                var displayMax = 4;
            } else if ($scroller.hasClass('imgsbig')){
                var type = 'gallery';
                var displayMax = 7;
            }

            if ($list.children().length > displayMax){
                $scroller.append('<div class="scroller-nav"><span class="prev disabled">Previous</span><span class="next">Next</span></div>');
                $scroller.find('.scroller-nav span').click(function(){
                    if (!$(this).hasClass('working')){
                        if ($(this).hasClass('next')){
                            var direction = 'next';
                        } else if ($(this).hasClass('prev')){
                            var direction = 'prev';
                        }

                        var btnTarget = $(this);
                        var shellTarget = btnTarget.parent().parent().find('.scroller-shell');
                        var shellWidth = shellTarget.width();
                        var shellLeft = 0;

                        var listTarget = btnTarget.parent().parent().find('.scroller-list');
                        var listWidth = listTarget.width();
                        var listLeft = parseInt(listTarget.css('left'));

                        var newListPos = listLeft;
                        if (direction == 'next'){
                            if ($(this).hasClass('disabled')){
                                newListPos = 0;
                            } else {
                                newListPos = listLeft-(shellWidth+adMargin);
                            }
                            if (newListPos < 0-(listWidth-shellWidth)){ newListPos = 0-(listWidth-shellWidth); }
                        } else if (direction == 'prev'){
                            if ($(this).hasClass('disabled')){
                                newListPos = 0-(listWidth-shellWidth);
                            } else {
                                newListPos = listLeft+(shellWidth+adMargin);
                            }
                            if (newListPos > 0){ newListPos = 0; }
                        }

                        btnTarget.siblings('span').addClass('working');
                        btnTarget.addClass('working');

                        listTarget.stop(true,false).animate({"left": newListPos+"px"}, 700, function(){
                            var newListLeft = parseInt(listTarget.css('left'));

                            if (direction == 'next'){
                                if (newListLeft+listWidth <= shellLeft+shellWidth){
                                    btnTarget.addClass('disabled');
                                } else {
                                    btnTarget.removeClass('disabled');
                                }
                                if (newListLeft == shellLeft){
                                    btnTarget.siblings('span.prev').addClass('disabled');
                                } else {
                                    btnTarget.siblings('span.prev').removeClass('disabled');
                                }
                            } else if (direction == 'prev'){
                                if (newListLeft < shellLeft){
                                    btnTarget.removeClass('disabled');
                                } else {
                                    btnTarget.addClass('disabled');
                                }
                                if (newListLeft+listWidth <= shellLeft+shellWidth){
                                    btnTarget.siblings('span.next').addClass('disabled');
                                } else {
                                    btnTarget.siblings('span.next').removeClass('disabled');
                                }
                            }
                            btnTarget.siblings('span').removeClass('working');
                            btnTarget.removeClass('working');
                        });
                    }
                });
            }
        });
    }
    
    // img gallery thumb changing
    if ($('.imggallery .scroller').length > 0){
        $('.imggallery .scroller-list a').click(function(){
            var src = $(this).attr('href');
            $(this).parent().parent().parent().parent().siblings('.imghero').attr('src', src);
            return false;
        });
    }

    $('a.external').live('click', function(){
        window.open(this.href);
        return false;
    });

    $('.cmscontent a').live('click', function(){ // force new window opening on external and file links
        if (!$(this).hasClass('external')){
            var u = $(this).attr('href');
            if (u.indexOf("http") == 0 || u.indexOf("content/") == 0){
                window.open(this.href);
                return false;
            }
        }
    });

    $('a.facebook').click(function(){
        var width  = 760;
        var height = 400;
        
        var h = $(window).height();
        var w = $(window).width();
        
        var params = 'width='+width+', height='+height;
        params += ', top='+(h-height)/2+', left='+(w-width)/2;
        params += ', directories=no';
        params += ', location=no';
        params += ', menubar=no';
        params += ', resizable=no';
        params += ', scrollbars=no';
        params += ', status=no';
        params += ', toolbar=no';
        newwin=window.open(this.href,'Facebook', params);
        if (window.focus) {newwin.focus()}
        return false;
    });

    $('a.twitter').click(function(){
        var width  = 800;
        var height = 600;

        var h = $(window).height();
        var w = $(window).width();

        var params = 'width='+width+', height='+height;
        params += ', top='+(h-height)/2+', left='+(w-width)/2;
        params += ', directories=no';
        params += ', location=no';
        params += ', menubar=no';
        params += ', resizable=no';
        params += ', scrollbars=no';
        params += ', status=no';
        params += ', toolbar=no';
        newwin=window.open(this.href,'Facebook', params);
        if (window.focus) {newwin.focus()}
        return false;
    });

    // ADS MODAL
    $.extend($.modal.defaults, {
        closeHTML: "<a href='#'>Close</a>",
        minHeight: 250,
        maxHeight: 700,
        minWidth: 300,
        maxWidth: 800
    });
    if ($('.ads').length > 0){
        $('.admodal-trigger').click(function(){
            $(this).siblings('.admodal').modal({
                overlayClose:true,
                onOpen: function (dialog) {
                    dialog.overlay.fadeIn('fast', function(){
                        dialog.data.hide();
                        dialog.container.fadeIn('fast', function(){
                            dialog.data.fadeIn('fast');
                        });
                    });
                },
                onClose: function (dialog) {
                    dialog.data.fadeOut('fast', function(){
                        dialog.container.fadeOut('fast', function(){
                            dialog.overlay.fadeOut('fast', function(){
                                $.modal.close();
                            });
                        });
                    });
                }
            });
            return false;
        });
    }
    
    if ($('.lightbox').length > 0){
        $('.lightbox-trigger').click(function(){
            var lightboxTarget = 'lightbox-'+$(this).attr('href').substr(1);
            $('#'+lightboxTarget).modal({
                overlayClose:true,
                onOpen: function (dialog) {
                    dialog.overlay.fadeIn('fast', function(){
                        dialog.data.hide();
                        dialog.container.fadeIn('fast', function(){
                            dialog.data.fadeIn('fast');
                        });
                    });
                },
                onClose: function (dialog) {
                    dialog.data.fadeOut('fast', function(){
                        dialog.container.fadeOut('fast', function(){
                            dialog.overlay.fadeOut('fast', function(){
                                $.modal.close();
                            });
                        });
                    });
                }
            });
            return false;
        });
    }

    $('#vasearch .actions a').live('click',function(){
        $('#actionmodal').remove();
        $('body').prepend('<div id="actionmodal" class="admodal"></div>');
        $('#actionmodal').load($(this).attr('href'), function(){
            $('#actionmodal').modal({
                overlayClose: true,
                minHeight: 100,
                onOpen: function (dialog) {
                    dialog.overlay.fadeIn('fast', function(){
                        dialog.data.hide();
                        dialog.container.fadeIn('fast', function(){
                            dialog.data.fadeIn('fast');
                            $('#wrapper').addClass('noprint');
                        });
                    });
                },
                onClose: function (dialog) {
                    dialog.data.fadeOut('fast', function(){
                        dialog.container.fadeOut('fast', function(){
                            dialog.overlay.fadeOut('fast', function(){
                                $.modal.close();
                                $('#actionmodal').remove();
                                $('#wrapper').removeClass('noprint');
                            });
                        });
                    });
                }
            });
        });
        return false;
    });
    
    $('#actionmodal .actionbtn').live('click', function() {
      if(!window.validate_actionmodal_form_submit) { alert('Action failed.'); return false; }
      if(!window.validate_actionmodal_form_submit()) { return false; }
      var $form = $('#actionmodal form');
      $.post($form.get(0).action, $form.serialize(), function(data) {
      window.json_action_return_object = data;
      if(data.ok) {
        if(window.actionmodal_form_success) {
          window.actionmodal_form_success(data.message);
        } else {
          alert(data.message);
        }
      } else {
        alert(data.message);
      }
      }, 'json');
      return false;
    });

    $('#vasearch .matches a.offer').live('click',function(){
        if ($(this).hasClass('active')){
            $(this).removeClass('active');
            $(this).text('See Offers');
            $(this).parent().parent().parent().siblings('.offers').slideUp(250);
        } else {
            $(this).addClass('active');
            $(this).text('Hide Offers');
            $(this).parent().parent().parent().siblings('.offers').slideDown(250);
        }
        return false;
    });

    // events pagination and search
    if ($('.paginate').length > 0){
        $('.paginate').click(function(){
            $('#searchform').attr('action', $(this).attr('href'));
            if($('#event-search').val() == $('#event-search').attr('alt')) $('#event-search').val('');
            $('#searchform').submit();
            return false;
        });
    }

    $('#date-filter, #category-filter').change(function(){
        if($('#event-search').val() == $('#event-search').attr('alt')) $('#event-search').val('');
         $(this).closest("form").submit();
    });
    
    // attractions hover
    

    if ($('.attraction').length > 0){
        $('.attraction').hover(
            function(){
                $('.attraction').each(function(){
                    $(this).css({'z-index':'1'});
                    $('.attraction').children('.info').hide();
                });
                $(this).css({'z-index':'100'});
                $(this).children('.info').fadeIn(250);
                
                $(this).find('img').addClass('hover');
                $('.attraction .slide a').each(function() {
                    $(this).children('img:not(.hover)').stop(true,false).animate({opacity:.3}, 500);
                });
            },
            function(){
                $(this).children('.info').hide();
                
                $('.attraction .slide a img').each(function() {
                    $(this).removeClass("hover").stop(true,false).animate({opacity:1}, 500);
                });
            }
        );
        $('.attraction a.close').click(function(){
            $(this).parent().hide();
            $('.attraction .slide a img').each(function() {
                $(this).removeClass("hover").stop(true,false).animate({opacity:1}, 500);
            });
            return false;
        });
    }

    // custom css expression for a case-insensitive contains()
    jQuery.expr[':'].Contains = function(a,i,m){
        return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
    };

    $("input, textarea").focus(
        function(){
            if ($(this).attr('alt') != ''){
                if ($(this).val() == $(this).attr('alt')){
                    $(this).addClass('active');
                    $(this).val('');
                }
            }
        }
    );

    $('.list-input').click(function(){
        if($(this).attr("class") == 'list-input-active') clearAll();
        else{
            clearAll();
            $(this).attr("class","list-input-active");
            updateList($(this).text());
        }
    });

    $("#category-clear").click(function() {
        clearAll();
    });

    $("#search-clear").click(function() {
        $('#search-input').val($('#search-input').attr('alt'));
        
        if($('.list-input-active').text() != '')
            updateList($('.list-input-active').text());
        else
            updateList($('#category-input').val());

        $(this).slideUp(300);
    });

    $(".toggle_details").click(function() {
        toggleDetails();
    });
    
    $(".toggle_list").click(function() {
        toggleList();
    });

}); // end $(document).ready function

function loadDetails(store) {
    $('#store-details').html(''); // clear content so you don't see the flash of old before the new comes in
    $(myXML).find('mapobject').each(function(){
        if(store == $(this).attr('url')){
            var events = '';
            var feature = '';
            var parking = '';
            var pmod = 'lot';
            var address = $(this).find('address').text();
            var website = $(this).find('website').text();
            var offers = $(this).find('offers').text();
            var content = $(this).find('narrative').text();
            var phone = $(this).find('phonenumber').text();
            var name = $(this).attr('name');
            var suite = $(this).find('suite').text();

            if(website != '') website = '<br><a href="'+website+'" onclick="window.open(this.href); return false;">visit website</a>';
            if(address != '') address = '<br>'+address;
            if(offers != '') offers = '<h3>Offers</h3><p>'+offers+'</p>';
            if(content != '') content = '<p class="desc">'+content+'</p>';
            
            if(name == "Bloomingdale's" || name == "Bloomingdale's Court" || name == 'Sears' || name == 'Sears Court' || name == "Best Buy® Rotunda" || suite.substring(0, 1) == 'E'){
                parking = 'East';
                pmod = 'ramp';
            }else if(name == "Macy's" || name == "LUSH" || suite.substring(0, 1) == 'S'){
                parking = 'South';
            }else if(name == "Nordstrom" || name == "Nordstrom Court" || name == "West Market Square" || name == "Macy's Court" || suite.substring(0, 1) == 'W'){
                parking = 'West';
                pmod = 'ramp';
            }else if(suite.substring(0, 1) == 'N'){
                parking = 'North';
            }
            
            if(parking != '') parking = '<h3>Parking</h3><p>Park in '+parking+' parking '+pmod+' and enter through '+parking+' entrance.</p>';
            
            var maplinkid = $(this).attr('maplinkid');
            var d = new Date();
            var utime = d.getTime();
            
            $(eXML).find('event').each(function(){
                if($(this).children('tenant').text() == maplinkid || $(this).children('location').text() == maplinkid){
                    d1 = new Date($(this).find('promote > stop').text());
                    ctime = d1.getTime();
                    if(ctime >= utime)
                        events = events+'<li><a href="events/view/'+$(this).attr('id')+'">'+$(this).find('fulldescription > section > title').text()+'</a></li>';
                }
            });

            if(events != '') events = '<h3>Events</h3><ul>'+events+'</ul>';
            
            $(this).find('featuredcategory').each(function(){
                if($(this).text() == 'New Store'){
                    feature = '<span class="status">NEW STORE</span><br />';
                    return;
                }else if($(this).text() == 'Coming Soon'){
                    feature = '<span class="status">COMING SOON</span><br />';
                    return;
                }
            });
            
            var basehtml = '<p>'+feature+'<strong>'+phone+'</strong>'+address+website+'</p>'+parking+content+events+offers;
            
            $.ajax({
                url:'content/imgs/logos/logo'+$(this).attr('maplinkid')+'.png',
                type:'HEAD',
                error:
                    function(){
                        $('#store-details').html('<h2>'+name+'</h2>'+basehtml);
                    //  Cufon.replace('#store-details h2');
                    //  Cufon.replace('#store-details h3');
                    },
                success:
                    function(){
                        $('#store-details').html('<img class="logo" src="content/imgs/logos/logo'+maplinkid+'.png" height="75" width="175" alt="">'+basehtml);
                    //  Cufon.replace('#store-details h3');
                    }
            });
            
            
            /* disabled peding aproval
            // pull offers by tennant
            $.ajax({
                url:'search/offer_by_url.php',
                data:{url:$(this).attr('url')},
                dataType: 'json',
                success:
                    function(return_value){
                        if(return_value.error) return;
                        for(var i = 0; i < return_value.offers.length; ++i) {
                            var this_offer = return_value.offers[i];
                            if(this_offer.discount) $('#store-details').append('<p>'+this_offer.discount+'</p>');
                            $('#store-details').append('<p>'+this_offer.description+'</p><p>'+this_offer.dates+'</p>');
                        }
                        window.foobar = return_value;
                    }
            });
            */
        }
    });

    showTenant(store);
    $(".highlight-result2").attr("class","catlist");
    toggleDetails();
}

// create category select list and give it a change handler
function categoryFilter(header){
    $(myXML).find('mapdata > categories > category').each(function()
    {
        var caturl = $(this).attr('url');
        var catname = $(this).attr('name');
        $(header).
            append($("<option></option>").
            attr("value",catname).
            text(catname));
    });
    
    /* sort alphabetically
    $(header).html($(header).children('option').sort(function (a, b) {
        return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
    }));
    
    $(header).children("option[value='']").text('SELECT A RETAIL CATEGORY');
    */
    
    // filter by main category
    $(header).change(function(){
        clearOChildren();
        var val = $(this).val();
        if($(this).val() == '') $('#category-clear').slideUp(300);
        else $('#category-clear').slideDown(300);
        updateList(val);
    })
}

function listFilter(list) { // header is any element, list is an unordered list

    $("#search-input")
    .change(function(){
        toggleList();
        $(".toggle_details").hide();
        var filter = $(this).val();
        $('#search-spinner').css("visibility", "visible");
        $('#search-clear').slideDown(300);
        if(filter) {
        // this finds all links in a list that contain the input,
        // and hide the ones not containing the input while showing the ones that do
        $(list).find("span:not(:Contains(" + filter + "))").parent().hide();
        $(list).find("span:Contains(" + filter + ")").parent().show();
        } else {
        $(list).find("li").slideDown();
        }
        setTimeout(function()
        {
            $('#search-spinner').css("visibility", "hidden");
        }, '1000');
        return false;
    })
    .keyup(function(){
        // fire the above change event after every letter
        $(this).change();
    });
}

// rewrites the list of stores w/ category filtration
function updateList(cat){
    cat = $.trim(cat);
    if(cat != '') $("#header-cat").text(cat).slideDown(300);
    $('#search-input').val('SEARCH '+cat);
    $('#search-input').attr("alt","SEARCH "+cat);
    $("#tenants-list").html("");
    $(myXML).find('mapobject').each(function(){
        var found = false;
        var store_url = $(this).attr('url');
        if(store_url !== undefined){
            var store_name = $(this).attr('name');
            var store_suite = $(this).find('suite').text();
            var isnew = false;
            var iscs = false;
            $(this).find('featuredcategory').each(function(){
                if($(this).text() == 'New Store'){
                    isnew = true;
                    return;
                }else if($(this).text() == 'Coming Soon'){
                    iscs = true;
                    return;
                }
            });
            
            if(cat == '') found = true; 
            
            else{
                if(cat == 'New & Coming Soon' && (iscs || isnew)){ 
                    found = true;
                }else if(cat != 'New & Coming Soon'){
                    
                    $(this).find('featuredcategory').each(function(){
                        if($(this).text() == cat){
                            found = true;
                            return;
                        }
                    });
                    
    
                    if(!found){
                        $(this).find('category').each(function(){
                            if($(this).attr('name') == cat){
                                found = true;
                                return;
                            }
                        });
                    }
                }
            }
        
            if(found){
                if(store_suite != '') displayMod = ' ('+store_suite+')';
                else displayMod = '';
                
                if(isnew) mod = '<b>NEW</b> ';
                else if(iscs) mod = '<b>SOON</b> ';
                else mod = '';
                
                $('<li></li>')
                    .html(mod+'<span>'+store_name+displayMod+'</span>')
                    .attr("class","catlist")
                    .appendTo('#panel1 ul')
                    .click(function() {
                        loadDetails(store_url);
                        $(this).attr("class","catlist highlight-result2");
                    });
            }
        }
    }); //close each(
}

// function to clear out all filters and reset list
function clearAll() {
    toggleList();
    $(".toggle_details").hide();
    $('#category-input').val('');
    $('#search-input').val('SEARCH');
    $('#search-input').attr("alt","SEARCH");
    $(".list-input-active").attr("class","list-input");
    $("#header-cat").slideUp(300).text('');
    $("#header-subcat").slideUp(300).text('');
    $('.clearX').slideUp(300);
    updateList('','');
}

// clear only children of the main category pulldown ( search box, and narrow results )
function clearChildren() {
    toggleList();
    $(".toggle_details").hide();
    $('.category-input').val('');
    $('#search-input').val('SEARCH');
    $('#search-input').attr("alt","SEARCH");
    $('#search-clear').slideUp(300);
    $('.clearX').slideUp(300);
    $("#header-cat").slideUp(300).text('');
    updateList('',$(".list-input-active").text());
}

// clear only children of the main category pulldown ( search box, and narrow results )
function clearOChildren() {
    toggleList();
    $(".toggle_details").hide();
    $(".list-input-active").attr("class","list-input");
    $('#search-input').val('SEARCH');
    $('#search-input').attr("alt","SEARCH");
    $('#search-clear').slideUp(300);
    $('.clearX').slideUp(300);
    $("#header-subcat").slideUp(300).text('');
    updateList('',$(".list-input-active").text());
}

function toggleDetails(){
    /*$("#panel1").hide();
    $("#panel2").show();*/
    $('.directory-box .panel-set').animate({left: '-216px'}, 250);
    $(".toggle_details").show();
    $(".toggle_details").attr("class","toggle_details active");
    $('.toggle_list').attr("class","toggle_list");
}

function toggleList(){
    /*$("#panel2").hide();
    $("#panel1").show();*/
    $('.directory-box .panel-set').animate({left: '0px'}, 250);
    $('.toggle_list').attr("class","toggle_list active");
    $('.toggle_details').attr("class","toggle_details");
}

// directory map functions
function showAmenity(iconUrl) {
    appendToDebugText('Amenity changed: ' + iconUrl);
    var mapObj = document.getElementById('mapContent');
    mapObj.highlightAmenity(iconUrl);
}
function showTenantDetails(tenantUrl) {
    appendToDebugText('Tenant changed: ' + tenantUrl)
    loadDetails(tenantUrl);
}
function showLevel(level) {
    appendToDebugText('Level changed: Level ' + level);
}
function showTenant(tenantUrl) {
    appendToDebugText('Tenant changed: ' + tenantUrl);
    var mapObj = document.getElementById('mapContent');
    mapObj.highlightTenant(tenantUrl);
}
function testButton2() {
    var mapObj = document.getElementById('mapContent');
    mapObj.highlightTenant('bubba-gump-shrimp-co-s396');
}
function testButton3() {
    var mapObj = document.getElementById('mapContent');
    mapObj.highlightAmenity('atm');
}
function testButton4() {
    var mapObj = document.getElementById('mapContent');
    mapObj.changeLevel('1');
}
function testButton5() {
    var mapObj = document.getElementById('mapContent');
    mapObj.changeLevel('2');
}
function appendToDebugText(textValue) {
    if(document.getElementById('debugText')){
        var debugText = document.getElementById('debugText');
        debugText.innerHTML = textValue + '<br>' + debugText.innerHTML;
    }
}


function checkForm(theForm,reqFields)
{
    
    if(pw1 = document.getElementById('password')){
        
        var pw2 = document.getElementById('passwordVerify');
        
        if(pw1.value != pw2.value){
            
            alert('The passwords do not match.');
            pw1.value = '';
            pw2.value = '';
            pw1.focus();
            return false;
            
        }
    }
    
    var fields = reqFields.split(",");
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
    
    for(i in fields){
        
        if(field = document.getElementById(fields[i])){

            if(fields[i] == 'email'){
                
                if (!filter.test(field.value)) {
                    alert('Please provide a valid email address');
                    field.focus();
                    return false;
                }
                
            }
            
            if(field.value == '' || field.value == field.alt){
                alert('The field '+field.name.toUpperCase().replace('_',' ')+' is required.');
                field.focus();
                return false;
            }
            
        }
    }
    
    return true;
}

// function to parse and cache XML
function getXML() {
    var XML = null;
    $.ajax({
        url:      xmlFile,
        dataType: 'xml',
        async:    false,
        success:  function(data) {
            XML = data;
        }
    });
    return XML;
}
