
function checkEmail(email) {
	badEmail = 0;
	at = "@";
	dot = ".";
	lat = email.indexOf(at);
	lstr = email.length;
	ldot = email.indexOf(dot);
	if (email.indexOf(at)==-1){ badEmail = 1; }
	if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){ badEmail = 1; }
	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){ badEmail = 1; }
	if (email.indexOf(at,(lat+1))!=-1){ badEmail = 1; }
	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){ badEmail = 1; }
	if (email.indexOf(dot,(lat+2))==-1){ badEmail = 1; }
	if (email.indexOf(" ")!=-1){ badEmail = 1; }
	return badEmail;
}

function stripLeadingSpace(str)
{
	while(str.charAt(0) == " "){
		str = str.substring(1,str.length);
	}
	return str;
}

function validateStep1(){
	msg = '';
	document.step1.email.value = stripLeadingSpace(document.step1.email.value);
	if (checkEmail(document.step1.email.value) == 1){ msg += 'Enter a valid email address\n'; }
	document.step1.firstname.value = stripLeadingSpace(document.step1.firstname.value);
	if (document.step1.firstname.value == ''){ msg += 'Enter your first name\n'; }
	document.step1.lastname.value = stripLeadingSpace(document.step1.lastname.value);
	if (document.step1.lastname.value == ''){ msg += 'Enter your last name\n'; }
	if (document.step1.copies.value == 0){ msg += 'Select how many copies you would like to order\n'; }
	
	if (msg != ''){ 
		msg = 'Please correct the following errors:\n' + msg;
		alert(msg);
	}
	else{
		document.step1.action = 'system1.asp';
		document.step1.submit();
	}
	
}

function validateStep2(){
	msg = '';
	document.step2.email.value = stripLeadingSpace(document.step2.email.value);
	if (checkEmail(document.step2.email.value) == 1){ msg += 'Enter a valid email address\n'; }
	document.step2.firstname.value = stripLeadingSpace(document.step2.firstname.value);
	if (document.step2.firstname.value == ''){ msg += 'Enter your first name\n'; }
	document.step2.lastname.value = stripLeadingSpace(document.step2.lastname.value);
	if (document.step2.lastname.value == ''){ msg += 'Enter your last name\n'; }
	document.step2.ordernum.value = stripLeadingSpace(document.step2.ordernum.value);
	if (document.step2.ordernum.value == ''){ msg += 'Enter your order number\n'; }
	
	if (msg != ''){ 
		msg = 'Please correct the following errors:\n' + msg;
		alert(msg);
	}
	else{
		document.step2.action = 'system2.asp';
		document.step2.submit();
	}
}

function validateUnsub(){
	msg = '';
	document.unsub.email.value = stripLeadingSpace(document.unsub.email.value);
	if (checkEmail(document.unsub.email.value) == 1){ msg += 'Enter a valid email address\n'; }
	if (msg != ''){ 
		msg = 'Please correct the following errors:\n' + msg;
		alert(msg);
	}
	else{
		document.unsub.action = 'system3.asp';
		document.unsub.submit();
	}
}