var urlTest = new RegExp("^(ht|f)tp(s?)\:\/\/(([a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+)+)|localhost)(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?([\d\w\.\/\%\+\-\=\&amp;\?\:\\\&quot;\'\,\|\~\;]*)$");

function getRecentNews(){
//<li><span class="date">03.06.10</span> <a href="link">Employment Workshop Offered: Life After Spinal Cord Injury Employment Workshop</a></li>
 	$.get("/site/apps/nl/rss2.asp?c=ajIRK9NJLcJ2E&b=6350111",function(news){
		//find each 'item' in the file and parse it
		var rssdata = [];
		var additem = '';
		$(news).find('item').each(function(index, item) {
			//name the current found item this for this particular loop run
			additem += '<li><span class="date">'+formatDate($(this).find('pubDate').text())+'</span> <a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a></li>';
		});
		$('#recent-news-list').append(additem);
	});
}
 
function getHomeUpcomingEvents(){
//<li class="first"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
//<li class="last"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
 	$.get("/site/apps/nl/rss2.asp?c=ajIRK9NJLcJ2E&b=6350209",function(events){
		//find each 'item' in the file and parse it
		var rssdata = [];
		var additem = '', addclass = '', index = 0;
		$(events).find('item').each(function(i, item) {
			var currdate = new Date();
			if (new Date($(this).find('pubDate').text()) >= currdate) {
				//name the current found item this for this particular loop run
				if (index == 0) addclass = "first";
				if (index == 1) addclass = "last";
				if (index > 1) return;
				additem += '<li class="'+addclass+'"><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a><h4>'+$(this).find('[nodeName=k:subtitle]').text()+'</h4>'+$(this).find('[nodeName=k:keywords]').text()+' <a class="learn-more" href="'+$(this).find('link').text()+'"><span>learn more</span></a></li>';
				index++;
			}
		});
		$('#upcoming-events').append(additem);
	});
}
 
function getUpcomingEvents(){
//<li class="first"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
//<li class="last"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
 	$.get("/site/apps/nl/rss2.asp?c=ajIRK9NJLcJ2E&b=6350209",function(events){
		//find each 'item' in the file and parse it
		var rssdata = [];
		var additem = '', addclass = '';
		$(events).find('item').each(function(index, item) {
			//name the current found item this for this particular loop run
			if (index > 2) return;
			additem += '<li><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a><h4>'+$(this).find('[nodeName=k:subtitle]').text()+'</h4>'+$(this).find('[nodeName=k:byline]').text()+'<br/>'+$(this).find('[nodeName=k:keywords]').text()+'</li>';
		});
		$('#int-upcoming-events').append(additem);
	});
}
 
function getUpcomingEventsList(){
//<li class="first"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
//<li class="last"><a href="">Shooting and Hunting</a><h4>North Central PVA Tournament</h4>September 24-26, 2010 <br />Fresno, CA <a class="learn-more"><span>learn more</span></a></li>
 	$.get("/site/apps/nl/rss2.asp?c=ajIRK9NJLcJ2E&b=6350209",function(eventslist){
		//find each 'item' in the file and parse it
		var rssdata = [];
		var additem = '', addclass = '';
		$(eventslist).find('item').each(function(index, item) {
			//name the current found item this for this particular loop run
			additem += '<li><a href="'+$(this).find('link').text()+'">'+$(this).find('title').text()+'</a><h4>'+$(this).find('[nodeName=k:subtitle]').text()+'</h4>'+$(this).find('[nodeName=k:byline]').text()+'<br/>'+$(this).find('[nodeName=k:keywords]').text()+'</li>';
		});
		$('#wb-upcoming-events').append(additem);
	});
}
 
function formatDate(date) {
	date = new Date(date);
	var dd = (date.getDate() <= 9) ? '0' + date.getDate() : date.getDate();
	var mm = (date.getMonth() < 9) ? '0' + (date.getMonth()+1) : (date.getMonth()+1);
	var key = mm + '.' + dd + '.' + date.getFullYear(); 
	return key;
}

function formatEvDate(date) {
	date = new Date(date);
	var dd = (date.getDate() <= 9) ? '0' + date.getDate() : date.getDate();
	var mm = ['January','February','March','April','May','June','July','August','September','October','November','December'];
	var key = mm[date.getMonth()] + ' ' + dd + ', ' + date.getFullYear(); 
	return key;
}


function populate(form)
{
    if ( location.search == null || location.search.length < 1 ) return; // no querystring

    var pairs = location.search.substring(1).split("&");
    for ( var p = 0; p < pairs.length; ++p )
    {
        var pair = pairs[p].split("=");
        var name = pair[0];
        var value = unescape( pair[1].replace(/\+/g, " ") );
        var fld = form.elements[name];
        var ftype = null;
        var farray = false;
        var atype = Array;

        if ( fld != null )
        {
            if ( fld.length != null && fld.length >= 1 && fld[0].type != null && fld[0].type != undefined )
            {
                ftype = fld[0].type;             
                farray = true;
            } else {
                ftype = fld.type;
            }
        }
        switch ( ftype )
        {
            case "text": case "hidden": case "textarea":
                if ( farray ) fld = fld[0]; // only handle first-named for this type
                fld.value = value;
                break;
            case "select-one": case "select-multiple":
                if ( farray ) fld = fld[0]; // only handle first-named for this type
                for ( var o = 0; o < fld.options.length; ++o )
                {
                    var opt = fld.options[o];
                    var oval = opt.value;
                    if ( oval == null || oval == "" ) oval = opt.text;
                    if ( oval == value )
                    {
                        opt.selected = true;
                        break;
                    }
                }
                break;
            case "checkbox": case "radio":
                if ( ! farray )
                {
                    // single checbox or radio of that name:
                    fld.checked = true;
                } else {
                    for ( var cr = 0; cr < fld.length; ++cr )
                    {
                        if ( fld[cr].value == value )
                        {
                            fld[cr].checked = true;
                            break;
                        }
                    }
                }
                break;
            default:
                alert("Unknown field type encountered for field " + name + ": " + ftype);
                break;
        } // end of switch
    } // end of loop on fields from qs
}

function doClear(theText) {
     if (theText.value == theText.defaultValue) {
         theText.value = ""
     }
 }

// Onload Events
$(document).ready(function() {
   if ($('#search').length > 0) {
	   $('#search .submit').hover(function(){
			$(this).parent().parent().css('background-position','0 -129px');
	   },function(){
			$(this).parent().parent().css('background-position','0 -107px');
	   });
   }
   if ($('#newsletter').length > 0) {
	   $('#newsletter .submit').hover(function(){
			$(this).css('background-position','-116px -68px');
	   },function(){
			$(this).css('background-position','-7px -68px');
	   });
   }
   
   if ($("ul.tabs").length > 0) $("ul.tabs").tabs("div.panes > div");

   if (document.SUPPORTER450263) populate(document.SUPPORTER450263);
   
   $('input[name=first_name], input[name=last_name], input[name=address_line_1], input[name=address_line_2]').blur(function(){
		if ($(this).val().match(urlTest)) {
		   alert("Value entered is invalid. Please re-enter");
		   setFocus(this);
		   return;
		}
	});
});

function setFocus(id) {
	setTimeout(function(){$(id).focus();},50);
}
