// JavaScript Document


function returnShortDate(d) {
	var yr = d.getFullYear().toString();  
	var mo = (d.getMonth() + 1).toString(); 
	var dy = d.getDate().toString(); 
	yr = yr.substr(2, 2);
	if (mo.length < 2) mo = '0' + mo;
	if (dy.length < 2) dy = '0' + dy;
	return yr + mo + dy;
}

function returnFormattedDate(d) {
	var mo = d.substr(2,2);
	var dy = d.substr(4,2);
	var yr = d.substr(0,2);
	if (yr == '99') {
		yr = '19' + yr;
	} else {
		yr = '20' + yr;
	}
	
	return returnMonth(parseInt(mo) - 1) + ' ' + dy + ', ' + yr;
}

//from sourceforge/sarissa forums
function loadFromUrl(url){  

	try {
		var xmlhttp = new XMLHttpRequest();  
		xmlhttp.open("GET", url, false); 
		xmlhttp.send("");  
		var responseText = xmlhttp.responseText; 
		var oDomDoc = Sarissa.getDomDocument();  
		oDomDoc = (new DOMParser()).parseFromString(responseText, "text/xml"); 
		
		if (Sarissa.getParseErrorText(oDomDoc) != Sarissa.PARSED_OK) {
			throw ('empty XML file!');
		}
		
		return oDomDoc;   
	} catch (e) {
		//alert(e);
		throw 'no load sarissa';
	}
	
} 

function setRegion(abbr) {
	switch(abbr) {
		case "HP":
			return "High Plains";
		case "W":
			return "West";
		case "MW":
			return "Midwest";
		case "SE":
			return "Southeast";
		case "S":
			return "South";
		case "N":
			return "Northeast";
	}
}

function setState(abbr) {	
	switch(abbr) {
		case "AK":
			return "Alaska";
		case "HI":
			return "Hawaii";
		case "PR":
			return "Puerto Rico";
		case "WA":
			return "Washington";
		case "OR":
			return "Oregon";
		case "CA":
			return "California";
		case "AZ":
			return "Arizona";
		case "NM":
			return "New Mexico";
		case "NV":
			return "Nevada";
		case "ID":
			return "Idaho";
		case "MT":
			return "Montana";
		case "UT":
			return "Utah";
		case "MN":
			return "Minnesota";
		case "IA":
			return "Iowa";
		case "MO":
			return "Missouri";
		case "WI":
			return "Wisconsin";
		case "MI":
			return "Michigan";
		case "IL":
			return "Illinois";
		case "IN":
			return "Indiana";
		case "OH":
			return "Ohio";
		case "KY":
			return "Kentucky";
		case "TX":
			return "Texas";
		case "OK":
			return "Oklahoma";
		case "LA":
			return "Louisiana";
		case "AR":
			return "Arkansas";
		case "TN":
			return "Tennessee";
		case "MS":
			return "Mississippi";
		case "AL":
			return "Alabama";
		case "GA":
			return "Georgia";
		case "FL":
			return "Florida";
		case "SC":
			return "South Carolina";
		case "NC":
			return "North Carolina";
		case "VA":
			return "Virginia";
		case "ME":
			return "Maine";
		case "VT":
			return "Vermont";
		case "NH":
			return "New Hampshire";
		case "DE":
			return "Delaware";
		case "MA":
			return "Massachusetts";
		case "NJ":
			return "New Jersey";
		case "NY":
			return "New York";
		case "PA":
			return "Pennsylvania";
		case "WV":
			return "West Virginia";
		case "MD":
			return "Maryland";
		case "RI":
			return "Rhode Island";
		case "CT":
			return "Connecticut";
		case "NE":
			return "Nebraska";
		case "KS":
			return "Kansas";
		case "SD":
			return "South Dakota";
		case "ND":
			return "North Dakota";
		case "WY":
			return "Wyoming";
		case "CO":
			return "Colorado";
	}
}

function returnPreviousThursday(d) {
	var newDate, numWeeks;
	//if (d.getDay() != 4) {
		//it's not Thursday so need to figure out how to offset to get the PREVIOUS Thursday
		d = ReturnThursday(d);
		numWeeks = 7;
	/*} else {
		//make sure it's before 7 (NE; or 8 eastern) 
		if (d.getHours() < 7) {
			// it's still technically last week so need archive of 2 Thursdays ago (since today is Thursday)
			numWeeks = 14;
		} else {
			//it's the current week so need last week's archives
			numWeeks = 7;
		}
	}*/
	
	//reset the correct number of weeks
	numWeeks *= (1000 * 60 * 60 * 24);
	newDate = new Date(d.valueOf() - numWeeks);

	return newDate;
}

function addDays(d, days) {
	days *= (1000 * 60 * 60 * 24);
	var nd = new Date(d.valueOf() + days);
	return nd;
}

function ReturnThursday(d) {
	var offset;
	var dy = d.getDay();
	switch (dy) {
		case 0:
			offset = 3;
			break;
		case 1:
			offset = 4;
			break;
		case 2:
			offset = 5;
			break;
		case 3:
			offset = 6;
			break;
		case 4:
			offset = 0;
			break;
		case 5:
			offset = 1;
			break;
		case 6:
			offset = 2;
			break;
		default:
			offset = 0;
			break;
	}
	//need to convert to the number of milliseconds in a day
	offset *= (1000 * 60 * 60 * 24);
	var x = new Date(d.valueOf() - offset);
	return x;
}

function ReturnTuesday(d) {
	var offset;
	var dy = d.getDay();
	switch (dy) {
		case 0:
			offset = -2;
			break;
		case 1:
			offset = -1;
			break;
		case 2:
			offset = 0;
			break;
		case 3:
			offset = 1;
			break;
		case 4:
			offset = 2;
			break;
		case 5:
			offset = 3;
			break;
		case 6:
			offset = 4;
			break;
		default:
			offset = 0;
			break;
	}
	//need to convert to the number of milliseconds in a day
	offset *= (1000 * 60 * 60 * 24);
	var x = new Date(d.valueOf() - offset);
	return x;
}

function returnSunday(d) {
	var offset;
	var dy = d.getDay();
	switch (dy) {
		case 0:
			offset = 0;
			break;
		default:
			offset = dy;
			break;
	}
	//need to convert to the number of milliseconds in a day
	offset *= (1000 * 60 * 60 * 24);
	var x = new Date(d.valueOf() - offset);
	return x;
}

function returnMonth(m) {
	switch (m) {
		case 0:
			return "January";
		case 1:
			return "February";
		case 2:
			return "March";
		case 3:
			return "April";
		case 4:
			return "May";
		case 5:
			return "June";
		case 6:
			return "July";
		case 7:
			return "August";
		case 8:
			return "September";
		case 9:
			return "October";
		case 10:
			return "November"
		case 11:
			return "December";
		default:
			return "";
	}
}
