// MWpopup - displays an informational blurb
var MWpopupObj;
function MWpopup(fldId) {
	if (document.getElementById) {
		var field;
		if (MWpopupObj) { MWpopupObj.close(); }
		if (field = document.getElementById(fldId)) {
			/*
			if (navigator.appVersion.indexOf("MSIE")!=-1){
				MWpopupObj = window.open('popup.html','info','width=300,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no');
				MWpopupObj.document.body.innerHTML = field.innerHTML;
			} else {
			*/
				MWpopupObj = window.open('','info','width=300,height=300,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no');
				MWpopupObj.document.write('<html><head><title>Click to Close</title></head><body style="margin:10px; padding:0; font-family: Arial, Helvetica, sans-serif; font-size: 80%;" onclick="self.close()">' + field.innerHTML + '</body></html>');
			//}
			MWpopupObj.focus();
		}
	}
}

// checkBox - given a form checkbox field ID, will check the box if unchecked
function checkBox(boxId) {
	if (document.getElementById) {
		var box;
		if (box = document.getElementById(boxId)) {
			if (box.checked == false) {
				box.checked = true;
			}
		}
	}
}

// MWcheckDominant - checks for multiple dominant stages selected and asks user to confirm
function MWcheckDominant(species) {
	
}

// MWtoggleSection - shows/hides a div and switches arrow image
function MWtoggleSection(sectionId) {
	if (document.getElementById) {
		var MWsection;
		if (MWsection = document.getElementById(sectionId+'_div')) {
			MWsectionImg = document.getElementById(sectionId+'_img');
			if (MWsection.style.display == 'block') {
				MWsection.style.display = 'none';
				MWsectionImg.src = 'arrow-r.gif';
			} else {
				MWsection.style.display = 'block';
				MWsectionImg.src = 'arrow-d.gif';
			}
		}
	}
}


// setSelectField - sets the select field selid to value val
function setSelectField(selid, val) {
	var sel = document.getElementById(selid);
	for (i=0; i<sel.options.length; i++)
		if (sel.options[i].text == val)
			sel.selecetedIndex = i;
}

// setLocFields - retrieve & set station location fields
function setLocFields(locid, programtype){
	var MWlocFieldsAR = Array('MWalpinearea', 'MWtown', 'MWstate', 'MWroute', 'MWelevation', 'MWlatitude', 'MWlongitude', 'MWlocation_description');
	var ao=false
	if (window.XMLHttpRequest) {
		ao = new XMLHttpRequest();
		if (ao.overrideMimeType)
			ao.overrideMimeType('text/xml')
	} else if (window.ActiveXObject) {
		try { ao = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { }
	} else
		return;
	ao.open('GET', 'getstation.cfm?sid=' + locid, true);
	ao.onreadystatechange = function () {
		if (ao.readyState == 4) {
			if (ao.status == 200) {
				var aodata = ao.responseXML;
				if (aodata.getElementsByTagName('error').length>0)
					alert(aodata.getElementsByTagName('error').item(0).firstChild.data);
				else if (aodata.getElementsByTagName('station').length>0) {
					var locAR = aodata.getElementsByTagName('station');
					// ensure station returned is one still selected
					if (document.getElementById('MWlocation').value == locAR[0].getElementsByTagName('MWstationid')[0].firstChild.nodeValue)
						// update form fields
						for (var MWfield in MWlocFieldsAR) {
							if ((MWlocFieldsAR[MWfield] != 'MWalpinearea') || (programtype != 'Forest')) {
								if (locAR[0].getElementsByTagName(MWlocFieldsAR[MWfield])[0].firstChild)
									document.getElementById(MWlocFieldsAR[MWfield]).value = locAR[0].getElementsByTagName(MWlocFieldsAR[MWfield])[0].firstChild.nodeValue;
								//else if (MWlocFieldsAR[MWfield] == 'MWalpinearea')
								//	setSelectField(MWlocFieldsAR[MWfield], '');
								else
									document.getElementById(MWlocFieldsAR[MWfield]).value = '';
							}
						}
				}
			}
		}
	};
	ao.send(null);
}

// MWupdateLocation - en/disables location fields based on whether saved location selected
function MWupdateLocation(change, programtype) {
	var MWlocFieldsAR = Array('MWnewlocation', 'MWalpinearea', 'MWtown', 'MWstate', 'MWroute', 'MWelevation', 'MWlatitude', 'MWlongitude', 'MWlocation_description'); // 'MWmap',
	if (document.getElementById) {
		var disableit = false;
		var bgcolor = '#eef';
		var loc = document.getElementById('MWlocation').value;
		if (loc != '') {
			disableit = true;
			document.getElementById('MWnewlocation').value = '';
			bgcolor = '#eee';
			setLocFields(loc, programtype);
		}
		for (var MWfield in MWlocFieldsAR) {
			if ((MWlocFieldsAR[MWfield] != 'MWalpinearea') || (programtype != 'Forest')) {
				var fld = document.getElementById(MWlocFieldsAR[MWfield]);
				fld.disabled = disableit;
				fld.style.backgroundColor = bgcolor;
			}
		}
		if ((change == 1) && (loc == '')) {
			document.getElementById('MWelevation').value = '';
			document.getElementById('MWlatitude').value = '';
			document.getElementById('MWlongitude').value = '';
			document.getElementById('MWlocation_description').value = '';
		}
	}
}

// update slope/aspect if flat/none selected, respectively
function MWslopeChange(slopeFld, id) {
	if (slopeFld.value == 'Flat')
		document.getElementById(id + '_aspect').value = 'None';
}
function MWaspectChange(aspectFld, id) {
	if (aspectFld.value == 'None')
		document.getElementById(id + '_slope').value = 'Flat';
}

// check that key pressed is a number
function checkNumberFieldKeyPress(e) {
        var key1 = (e.keyCode) ? e.keyCode : e.charCode;
        var thekey = (key1) ? key1 : e.which;
        if ((thekey >= 48) && (thekey <= 57))   // 0-9
                return true;
        switch (thekey) {
                case 0:
                case 8:  // backspace
                case 9:  // tab
                case 37: // left arrow
                case 39: // right arrow
                //case 46: // delete
                    return true;
                        break;
        }
        return false;
}
