/* 
	GET URL PARAMETERS
*/
function getUrlParams()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

// Will dynamically check a box in the given form; the substring should be chosen to match a single box.
function checkBoxInForm(form_id, checkbox_substring)
{
	$("#" + form_id + " fieldset label").each(function(){
		if($(this).text().toLowerCase().search(checkbox_substring) > -1)
		{
			$("#" + $(this).attr("for")).attr("checked", true);
		}

	});
}

function openLink(e, link, newWindow, propagateEvent)
{
	if(newWindow == undefined)
		newWindow = true;

	if(propagateEvent == undefined)
		propagateEvent = false;

	if(newWindow == true)
	{
		newWindow = window.open(link);
		newWindow.focus();
	}
	else
	{
		window.location = link;
	}

	if(propagateEvent == false)
	{
		if (!e)
		{
			var e = window.event;
		}
		e.cancelBubble = true;
		if (e.stopPropagation)
		{
			e.stopPropagation();
		}
	}
}

// Allows us to initialize form fields with instructions
// (for instance, write "first name" in that input field
// instead of having a label "first name" next to the field.)
// Call using onblur="initFormField(this,'label');"
function initFormField(field, fieldValue) {
	if(field.tagName == 'textarea')
	{
		if(field.innerHTML == '')
		{
			field.innerHTML = fieldValue;
		}
	}
	else
	{
		if(field.value == '')
		{
			field.value = fieldValue;
		}		
	}
}

// Allows us to check whether a field has been filled in.
// We check to make sure (1) field is not empty and
// (2) field does not contain its default value (a field
// label such as 'first name' may be the default value for the
// field, yet it indicates that the field has obviously not been
// filled in by the user.)
function isFilledIn(field, defaultValue) {
	filledIn = true;	
	if(field.tagName == 'textarea')
	{
		if(field.innerHTML == '' || field.innerHTML == defaultValue)
		{
			filledIn = false;
		}
	}
	else
	{
		if(field.value == '' || field.value == defaultValue)
		{
			filledIn = false;
		}		
	}
	return filledIn;
}

/*
	COLLAPSIBLE LISTS
*/

// CLOSED_IMAGE='/liberationnews/assets/images/website/plus.png'; 	// CLOSED_IMAGE - the image to be displayed when the sublists are closed
// OPEN_IMAGE='/liberationnews/assets/images/website/minus.png';	// OPEN_IMAGE   - the image to be displayed when the sublists are opened

/* makeCollapsible - makes a list have collapsible sublists
 * listElement - the element representing the list to make collapsible */
function makeCollapsible(listElement)
{
	// loop over all child elements of the list
	var child=listElement.firstChild;
	while (child!=null)
	{
		// only process li elements (and not text elements)
		if (child.nodeType==1)
		{
			child.onclick=createToggleFunction(child);
		}
		child=child.nextSibling;
	}
}

/* createToggleFunction - returns a function that toggles the sublist display
 * 
 * toggleElement  - the element representing the toggle gadget
 * sublistElement - an array of elements representing the sublists that should
 *                  be opened or closed when the toggle gadget is clicked
 */
function createToggleFunction(toggleElement)
{
	return function()
	{
		// toggle status of toggle gadget
		if(toggleElement.getAttribute('class')=='collapsibleClosed')
		{
			toggleElement.setAttribute('class','collapsibleOpen');
		}
		else
		{
			toggleElement.setAttribute('class','collapsibleClosed');
		}
	}
}


/*
	GEOCODING
*/
var dc_branch = {
	"lat": 38.9159990,
	"lng": -77.0206150
 }

var la_branch = {
	"lat": 34.0745482,
	"lng": -118.2869456
 }

var sf_branch = {
	"lat": 37.7496536,
	"lng": -122.4181368
 }

var chicago_branch = {
	"lat": 41.9684478,
	"lng": -87.7126841
 }

var nyc_branch = {
	"lat": 40.8149190,
	"lng": -73.9435210
 }

var baltimore_branch = {
	"lat": 39.2903848,
	"lng": -76.6121893
 }

var syracuse_branch = {
	"lat": 43.0481221,
	"lng": -76.1474244
 }

var albuquerque_branch = {
	"lat": 35.0844909,
	"lng": -106.6511367
 }

var columbus_branch = {
	"lat": 39.9611755,
	"lng": -82.9987942
 }

var boston_branch = {
	"lat": 42.3583333,
	"lng": -71.0602778
 }


function getDistance(lat1, long1, lat2, long2) {
	// Calculate approximate distance between two sets of coordinates (lat, long) in miles
	x = 69.1 * (lat2 - lat1);
	y = 69.1 * (long2 - long1) * Math.cos(lat1/57.3);
	dist = Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
	return dist;
}

function getClosestBranch(radiusInMiles) {

	// Get user's latitude and longitude using Maxmind geoip service
	var reader_lat = geoip_latitude();
	var reader_lng = geoip_longitude();

	// Calculate distances from each branch
	la_dist = getDistance(la_branch.lat, la_branch.lng, reader_lat, reader_lng);
	chicago_dist = getDistance(chicago_branch.lat, chicago_branch.lng, reader_lat, reader_lng);
	dc_dist = getDistance(dc_branch.lat, dc_branch.lng, reader_lat, reader_lng);
	sf_dist = getDistance(sf_branch.lat, sf_branch.lng, reader_lat, reader_lng);
	nyc_dist = getDistance(nyc_branch.lat, nyc_branch.lng, reader_lat, reader_lng);
	baltimore_dist = getDistance(baltimore_branch.lat, baltimore_branch.lng, reader_lat, reader_lng);
	syracuse_dist = getDistance(syracuse_branch.lat, syracuse_branch.lng, reader_lat, reader_lng);
	albuquerque_dist = getDistance(albuquerque_branch.lat, albuquerque_branch.lng, reader_lat, reader_lng);
	columbus_dist = getDistance(columbus_branch.lat, columbus_branch.lng, reader_lat, reader_lng);
	boston_dist = getDistance(boston_branch.lat, boston_branch.lng, reader_lat, reader_lng);
	
	distance = la_dist;
	branch = "los-angeles";
	if(chicago_dist < distance) {
		distance = chicago_dist;
		branch="chicago";
	}
	if(dc_dist < distance) {
		distance = dc_dist;
		branch="dc";
	}
	if(sf_dist < distance) {
		distance = sf_dist;
		branch="san-francisco";
	}
	if(nyc_dist < distance) {
		distance = nyc_dist;
		branch="new-york-city";
	}
	if(baltimore_dist < distance) {
		distance = baltimore_dist;
		branch="baltimore";
	}
	if(syracuse_dist < distance) {
		distance = syracuse_dist;
		branch="syracuse";
	}
	if(albuquerque_dist < distance) {
		distance = albuquerque_dist;
		branch="albuquerque";
	}
	if(columbus_dist < distance) {
		distance = columbus_dist;
		branch="columbus";
	}
	if(boston_dist < distance) {
		distance = boston_dist;
		branch="boston";
	}

	if(distance > radiusInMiles)
		branch = null;

	return branch;
}

function convertAbbreviationToState(abbreviation)
{
	abbreviation = abbreviation.toUpperCase();
	switch(abbreviation)
	{
	case 'AL':
		state = 'Alabama';
		break;
		
	case 'AK':
		state = 'Alaska';
		break;
		
	case 'AZ':
		state = 'Arizona';
		break;
		
	case 'AR':
		state = 'Arkansas';
		break;
		
	case 'CA':
		state = 'California';
		break;
		
	case 'CO':
		state = 'Colorado';
		break;
		
	case 'CT':
		state = 'Connecticut';
		break;
		
	case 'DE':
		state = 'Delaware';
		break;
		
	case 'FL':
		state = 'Florida';
		break;
		
	case 'GA':
		state = 'Georgia';
		break;
		
	case 'HI':
		state = 'Hawaii';
		break;
		
	case 'ID':
		state = 'Idaho';
		break;
		
	case 'IL':
		state = 'Illinois';
		break;
		
	case 'IN':
		state = 'Indiana';
		break;
		
	case 'IA':
		state = 'Iowa';
		break;
		
	case 'KS':
		state = 'Kansas';
		break;
		
	case 'KY':
		state = 'Kentucky';
		break;
		
	case 'LA':
		state = 'Louisiana';
		break;
		
	case 'ME':
		state = 'Maine';
		break;
		
	case 'MD':
		state = 'Maryland';
		break;
		
	case 'MA':
		state = 'Massachusetts';
		break;
		
	case 'MI':
		state = 'Michigan';
		break;
		
	case 'MN':
		state = 'Minnesota';
		break;
		
	case 'MS':
		state = 'Mississippi';
		break;
		
	case 'MO':
		state = 'Missouri';
		break;
		
	case 'MT':
		state = 'Montana';
		break;
		
	case 'NE':
		state = 'Nebraska';
		break;
		
	case 'NV':
		state = 'Nevada';
		break;
		
	case 'NH':
		state = 'New Hampshire';
		break;
		
	case 'NJ':
		state = 'New Jersey';
		break;
		
	case 'NM':
		state = 'New Mexico';
		break;
		
	case 'NY':
		state = 'New York';
		break;
		
	case 'NC':
		state = 'North Carolina';
		break;
		
	case 'ND':
		state = 'North Dakota';
		break;
		
	case 'OH':
		state = 'Ohio';
		break;
		
	case 'OK':
		state = 'Oklahoma';
		break;
		
	case 'OR':
		state = 'Oregon';
		break;
		
	case 'PA':
		state = 'Pennsylvania';
		break;
		
	case 'RI':
		state = 'Rhode Island';
		break;
		
	case 'SC':
		state = 'South Carolina';
		break;
		
	case 'SD':
		state = 'South Dakota';
		break;
		
	case 'TN':
		state = 'Tennessee';
		break;
		
	case 'TX':
		state = 'Texas';
		break;
		
	case 'UT':
		state = 'Utah';
		break;
		
	case 'VT':
		state = 'Vermont';
		break;
		
	case 'VA':
		state = 'Virginia';
		break;
		
	case 'WA':
		state = 'Washington';
		break;
		
	case 'WV':
		state = 'West Virginia';
		break;
		
	case 'WI':
		state = 'Wisconsin';
		break;
		
	case 'WY':
		state = 'Wyoming';
		break;
		
	case 'AS':
		state = 'American Samoa';
		break;
		
	case 'DC':
		state = 'District of Columbia';
		break;
		
	case 'FM':
		state = 'Federated States of Micronesia';
		break;
		
	case 'GU':
		state = 'Guam';
		break;
		
	case 'MH':
		state = 'Marshall Islands';
		break;
		
	case 'MP':
		state = 'Northern Mariana Islands';
		break;
		
	case 'PW':
		state = 'Palau';
		break;
		
	case 'PR':
		state = 'Puerto Rico';
		break;
		
	case 'VI':
		state = 'Virgin Islands';
		break;
	}
	
	return state;
}
