/* General Functions for Alerts Pages */
function sampleWin()
{
	window.open("","sample","toolbar=no,location=no,directories=no,status=yes,menubar=yes," +
		"scrollbars=yes,copyhistory=no,resizable=yes,width=700,height=600");
}
function checkinput()
{
	var theEmail = document.regform.email.value;
	if ((document.regform.email.value == '') || (document.regform.email.value == 'Example: joe@isp.com'))
	{
		alert('An Email Address is Required!\nPlease enter your email address.');
		document.regform.email.focus();
		return false;
	}
	if (! ValidateEmail(theEmail))
	{
		alert('The Email Address Entered ' + theEmail + ' is not valid.\nPlease enter a valid email address Example: joe@isp.com');
		document.regform.email.focus();
		return false;
	}
	// Validate the Zip Code
	if (document.regform.wxzip.value.length != 5)
	{
		alert('The ZIP code entered is invalid\nPlease enter a valid US ZIP Code');
		document.regform.wxzip.focus();
		return false;
	}
	if ((! document.regform.getforecast.checked) && (! document.regform.getsevere.checked))
	{
		alert('At least one product needs to be selected.\nPlease select a daily forecast, Severe Weather Warnings, or both');
		return false;
	}
	if (document.regform.getforecast.checked)
	{
		if (document.regform.hours1.selectedIndex == 0)
		{
			alert('A delivery time for your First Delivery is required!\nPlease select a time');
			document.regform.hours1.focus();
			return false;
		}
		var wasSelected = false;
		for (var i=0;i < document.regform.days.length;i++)
		{
			if (document.regform.days[i].checked)
			{
				wasSelected = true;
			}
		}
		if (! wasSelected)
		{
			alert("At least one day of delivery is required.\n Please select at least one delivery day\n for your forecast.");
			return false;
		}
	}
	if (document.regform.username.value.length < 5)
	{
		alert('Username needs to be 5 characters or more');
		document.regform.username.focus();
		return false;
	}
	if (document.regform.username.value.length > 15)
	{
		alert('Username needs to be 15 characters or less');
		document.regform.username.focus();
		return false;
	}
	if (document.regform.pwd.value.length < 6)
	{
		alert('Password needs to be 8 characters or more');
		document.regform.pwd.focus();
		return false;
	}
	if (document.regform.pwd.value.length > 20)
	{
		alert('Password needs to be 20 characters or less');
		document.regform.pwd.focus();
		return false;
	}
	if (document.regform.pwd.value != document.regform.password2.value)
	{
		alert('Password and Verify Password values do not match!');
		document.regform.pwd.focus();
		return false;
	}
	return true;	
}
function verifyZip()
{
	var theZip = document.regform.wxzip.value;
	var url = '/alerts/verifyZip.asp?zipcode='+theZip;
	xmlGet(url,returnResult);
}
function returnResult()
{
	if (req.readyState ==4)			// we loaded the content
	{
		if (req.status == 200)		//HTTP ok
		{
			var zipValid = req.responseXML.getElementsByTagName('zipcode');
			var isVal = zipValid.item(0).getAttribute('isValid');
			if (isVal == 'true')
			{
				return true;
			}
			else
			{
				alert('The Zip Code Selected is not valid.  Please enter a valid US ZIP Code');
				document.regform.wxzip.focus();
			}
		}
	}
}
function ValidateEmail(address)
{
	var at="@"
	var dot="."
	var lat=address.indexOf(at)
	var lstr=address.length
	var ldot=address.indexOf(dot)
	if (address.indexOf(at)==-1)
	{
		return false
	}

	if (address.indexOf(at)==-1 || address.indexOf(at)==0 || address.indexOf(at)==lstr)
	{
		return false
	}

	if (address.indexOf(dot)==-1 || address.indexOf(dot)==0 || address.indexOf(dot)==lstr)
	{
		return false
	}

	if (address.indexOf(at,(lat+1))!=-1)
	{
		return false
	}
	if (address.substring(lat-1,lat)==dot || address.substring(lat+1,lat+2)==dot)
	{
		return false
	}
	if (address.indexOf(dot,(lat+2))==-1)
	{
		return false
	}
	if (address.indexOf(" ")!=-1)
	{
		return false
	}
		return true					
}
function validateUsername(e)
{
	var keyPressed;
	if (window.event)
		keyPressed = window.event.keyCode;
	else if (e)
		keyPressed = e.which;
	else
		return null;
	if ((keyPressed == 0) || (keyPressed == 8) || (keyPressed == 13)|| (keyPressed == 9) ||(keyPressed == 16) || (keyPressed==17) || (keyPressed == 25))
	{
		return true;
	}
	else if ((keyPressed > 122) || ((keyPressed >=91) && (keyPressed <= 96)) || ((keyPressed < 48)) || ((keyPressed >57) && (keyPressed <=63)))
	{
		alert ('Invalid Character.\n Please note, This field only accepts the characters a-z and digits 0-9\n Special characters, Punctuation, and Spaces are not permitted.');
		return false;
	}
	else
	{
		return true;
	}
}
function checkUserForm()
{
	if (document.unform.username.value.length < 5)
	{
		alert('Username needs to be 5 characters or more');
		document.unform.username.focus();
		return false;
	}
	if (document.unform.username.value.length > 15)
	{
		alert('Username needs to be 15 characters or less');
		document.unform.username.focus();
		return false;
	}
	if (document.unform.password.value.length < 6)
	{
		alert('Password needs to be 8 characters or more');
		document.unform.password.focus();
		return false;
	}
	if (document.unform.password.value.length > 20)
	{
		alert('Password needs to be 20 characters or less');
		document.unform.password.focus();
		return false;
	}
	if (document.unform.password.value != document.unform.password2.value)
	{
		alert('Password and Verify Password values do not match!');
		document.unform.password.focus();
		return false;
	}
	return true;
}
function selectAllDays()
{
	for (var i=0;i<document.regform.days.length;i++)
	{
		document.regform.days[i].checked = true;
	}
}
function getCities()
{
	var selectBox = document.getElementById('stateSelection');
	var selectedState = selectBox[selectBox.selectedIndex].value;
	var ajaxUrl = '/alerts/findZip.asp?state=' + selectedState;
	//alert('Please wait while we get the cities in ' + selectedState + '.');
	xmlGet(ajaxUrl,showStates);
}
function showStates()
{
	// ensure that the request loaded
	var letterIndex = 0;
	var cityCount = 1;
	var thisCity;
	var thisZip;
	var zipShow = document.getElementById('zipLookupDiv');
	var thisSelect;
	var ch;
	if (req.readyState ==4)			// we loaded the content
	{
		if (req.status == 200)		//HTTP ok
		{	
			var cities = req.responseXML.getElementsByTagName('city');
			for (var i=0;i<cities.length;i++)
			{
				thisCity = cities[i].getAttribute('city');
				thisZip = cities[i].getAttribute('zip');
				ch = thisCity.substr(0,1).toLowerCase();
				if ((ch == 'a') || (ch == 'b') || (ch == 'c') || (ch == 'd') || (ch == 'e'))
				{
					if (letterIndex != 1)
					{
						thisSelect = document.getElementById('atoe');
						
						thisSelect.options.length=0;
						thisSelect.options[0] = new Option('Cities A-E','#');
						letterIndex = 1;
						cityCount = 1;
					}
				}
				if ((ch == 'f') || (ch == 'g') || (ch == 'h') || (ch == 'i') || (ch == 'j'))
				{
					if (letterIndex != 2)
					{
						thisSelect = document.getElementById('ftoj');
						thisSelect.options.length=0;
						thisSelect.options[0] = new Option('Cities F-J','#');
						letterIndex = 2;
						cityCount = 1;
					}
				}
				if ((ch == 'k') || (ch == 'l') || (ch == 'm') ||(ch == 'n') || (ch == 'o'))
				{
					if (letterIndex != 3)
					{
						thisSelect = document.getElementById('ktoo');
						thisSelect.options.length=0;
						thisSelect.options[0] = new Option('Cities K-O','#');
						letterIndex = 3;
						cityCount = 1;
					}
				}
				if ((ch == 'p') || (ch == 'q') || (ch == 'r') ||(ch == 's') || (ch == 't'))
				{
					if (letterIndex != 4)
					{
						thisSelect = document.getElementById('ptot');
						thisSelect.options.length=0;
						thisSelect.options[0] = new Option('Cities P-T','#');
						letterIndex = 4;
						cityCount = 1;
					}
				}
				if ((ch == 'u') || (ch == 'v') || (ch == 'w') ||(ch == 'x') || (ch == 'y') || (ch == 'z'))
				{
					if (letterIndex != 5)
					{
						thisSelect = document.getElementById('utoz');
						thisSelect.options.length=0;
						thisSelect.options[0] = new Option('Cities U-Z','#');
						letterIndex = 5;
						cityCount = 1;
					}
				}
				thisSelect.options[cityCount] = new Option(thisCity,thisZip);
				cityCount ++;
			}
			if (zipShow.style)
			{
				zipShow.style.visibility = "visible";
				zipShow.style.display = "block";
			}
			else
			{
				zipShow.visibility = "visible";
				zipShow.display = "block";
			}
		}
		else
		{
			alert('This feature is presently unavailable');
		}
	}
}
function setZip(zipValue)
{
	var uZipSel = document.getElementById('userZipSelection');
	var zipShow = document.getElementById('zipLookupDiv');
	uZipSel.value = zipValue;
	if (zipShow.style)
	{
		zipShow.style.visibility = "hidden";
		zipShow.style.display = "none";
	}
	else
	{
		zipShow.visibility = "hidden";
		zipShow.display = "none";
	}
}

