/* Highlight text on entry to text box */
function selectText(input){
 	input.focus();
 	input.select();
}

function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
        		}
      		}
   	}
}

function productGroupPage(args)
{
	window.location.href = 'level1.asp?'+args;
}

function validateReproPhone() {
	/* alert('Entering validateReproPhone') */
	if	( validatePhone( document.frmGetAcct.areacode.value, 3 ) == false ) {
		alert('You must enter a valid area code');
		document.frmGetAcct.areacode.focus();
		return false;	
	}
	if	( validatePhone( document.frmGetAcct.prefix.value, 3 ) == false ) {
		alert('You must enter a valid phone number');
		document.frmGetAcct.prefix.focus();
		return false;	
	}
	if	( validatePhone( document.frmGetAcct.suffix.value, 4 ) == false ) {
		alert('You must enter a valid phone number');
		document.frmGetAcct.suffix.focus();
		return false;	
	}
	/* alert('Assigning reproPhone') */	


	var reproPhone;
	reproPhone = document.frmGetAcct.areacode.value + '/';
	reproPhone = reproPhone + document.frmGetAcct.prefix.value + '-';
	reproPhone = reproPhone + document.frmGetAcct.suffix.value;
	/* alert('JS reproPhone='+reproPhone); */
	
	/* document.frmGetAcct.phone.value = reproPhone;*/
	
	/*
	alert('JS areacode='+document.frmGetAcct.areacode.value);
	alert('JS prefix='+document.frmGetAcct.prefix.value);
	alert('JS suffix='+document.frmGetAcct.suffix.value);
	alert('JS phone='+document.frmGetAcct.phone.value);
	*/
	return true;
}

function validatePhone( numval, numlen ) {

	if (isNum(numval) == false){
		return false;
	}
	if ( numval.length != numlen ) {
		return false;
	}
	return true;
}
function validateLogin() {

	if	( validatePhone( document.frmLogin.areacode.value, 3 ) == false ) {
		alert('You must enter a valid area code');
		document.frmLogin.areacode.focus();
		return false;	
	}
	if	( validatePhone( document.frmLogin.prefix.value, 3 ) == false ) {
		alert('You must enter a valid phone number');
		document.frmLogin.prefix.focus();
		return false;	
	}
	if	( validatePhone( document.frmLogin.suffix.value, 4 ) == false ) {
		alert('You must enter a valid phone number');
		document.frmLogin.suffix.focus();
		return false;	
	}
	/*
	document.frmLogin.proceed.value = "YES";
	document.frmLogin.action='cart.asp';
	document.frmLogin.submit();	
	*/
	var phone;
	phone = document.frmLogin.areacode.value + '/';
	phone = phone + document.frmLogin.prefix.value + '-';
	phone = phone + document.frmLogin.suffix.value;
	
	document.frmLogin.phone.value = phone;
	/*alert('JS phone='+document.frmLogin.phone.value);*/
	document.frmLogin.phoneok.value = "YES";
	/*alert('JS phoneok='+document.frmLogin.phoneok.value);*/
	
	return true;
}

function displayCertificate(file,width,height)
{
	var windowWidth = width + 20;
	var windowHeight = height + 25;

	/*
	This commented method leaves you with a bunch of windows open
	certWindow = window.open ('','certWin','toolbar=no,location=no,statusbar=no,scrollbars=no,width=' + windowWidth + ',height=' + windowHeight + 'resizable=no');
	certWindow.document.write(
	"<HTML><BODY BACKGROUND=tile.jpg><IMG SRC=" + file + " WIDTH=" + width + " HEIGHT=" + height + "></BODY></HTML>")
	*/
	/* This method opens one window and replaces the image */
	certWindow = window.open (file,'certWin','toolbar=no,location=no,statusbar=no,scrollbars=no,width=' + windowWidth + ',height=' + windowHeight + ',resizable=no');
	certWindow.focus()
	}
	
function displayGraphic(itemNumber)
{
	/* alert('itemNumber='+itemNumber); */
	var width = 550;
	var height = 550;
	var file = itemNumber + '.gif';
	/* alert('file='+file); */

	var windowWidth = width + 20;
	var windowHeight = height + 25;

	/*
	This commented method leaves you with a bunch of windows open 
	*/
	/*
	graphicWindow = window.open ('','graphicWin','toolbar=no,location=no,statusbar=no,scrollbars=no,width=' + windowWidth + ',height=' + windowHeight + ',resizable=no');
	graphicWindow.document.write(
	"<HTML><BODY><IMG SRC=" + file + " WIDTH=" + width + " HEIGHT=" + height + "></BODY></HTML>")
	graphicWindow.focus()
	*/
	
	/* This method opens one window and replaces the image */
	
	graphicWindow = window.open (file,'graphicWin','toolbar=no,location=no,statusbar=no,scrollbars=no,width=' + windowWidth + ',height=' + windowHeight + ',resizable=no');
	graphicWindow.focus()
	}

function isNum(valpassed) {
	/* alert('Entering isNum valpassed=' +valpassed) */
	if (valpassed == "") {
		return false;
	}
	for (i=0; i<valpassed.length; i++) {
		if (valpassed.charAt(i) < "0") {
			return false;
		}
		if (valpassed.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}
function validZip(zipcode) {
	if ( zipcode == "" ) {
		return false;
	}
	if ( zipcode.length < 5 ) {
		return false;
	}
	len=zipcode.length
	for (i=0; i<len; i++) {
		if (zipcode.charAt(i) < "0"	&& zipcode.charAt(i) != "-") {
			return false;
		}
		/*
		if (zipcode.charAt(i) > "9") {
			return false;
			*/
		if (zipcode.charAt(i) > "9") {
			return false;
		}
	}
	return true;
}

function validSearchNum(searchnum) {

	len=searchnum.length;
	/*
	alert('searchnum='+searchnum+' len='+len);
	alert('validSearchNum: length='+len);  
	*/
	if ( searchnum == "" ) {
		return false;
	}
	if ( len < 3 ) {
		return false;
	}
	return true;
}

function setShipAddress() {
	document.frmCheckout.sname.value = document.frmCheckout.ccname.value
	document.frmCheckout.scompany.value = document.frmCheckout.company.value
	document.frmCheckout.sstreet.value = document.frmCheckout.street.value
	document.frmCheckout.sstreet2.value = document.frmCheckout.street2.value
	document.frmCheckout.scity.value = document.frmCheckout.city.value
	document.frmCheckout.sstate.value = document.frmCheckout.state.value
	document.frmCheckout.szip.value = document.frmCheckout.zip.value
	/*document.frmCheckout.cbshipaddr.checked = false;*/
}

function setMailAddress() {
	document.frmRequest.mname.value = document.frmRequest.name.value
	document.frmRequest.mcompany.value = document.frmRequest.company.value
	document.frmRequest.mstreet.value = document.frmRequest.street.value
	document.frmRequest.mstreet2.value = document.frmRequest.street2.value
	document.frmRequest.mcity.value = document.frmRequest.city.value
	document.frmRequest.mstate.value = document.frmRequest.state.value
	document.frmRequest.mzip.value = document.frmRequest.zip.value
	/*document.frmRequest.cbmailaddr.checked = false;*/
}

function setReproMailAddress() {
	/* alert('Entering setReproMailAddress')' */
	document.frmRepro.mname.value = document.frmRepro.name.value
	document.frmRepro.mcompany.value = document.frmRepro.company.value
	document.frmRepro.mstreet.value = document.frmRepro.street.value
	document.frmRepro.mstreet2.value = document.frmRepro.street2.value
	document.frmRepro.mcity.value = document.frmRepro.city.value
	document.frmRepro.mstate.value = document.frmRepro.state.value
	document.frmRepro.mzip.value = document.frmRepro.zip.value
	/*document.frmRepro.cbmailaddr.checked = false;*/
}

function setReproPickupAddress() {
	/* alert('Entering setReproPickupAddress')' */
	document.frmPickup.mname.value = document.frmPickup.name.value
	document.frmPickup.mcompany.value = document.frmPickup.company.value
	document.frmPickup.mstreet.value = document.frmPickup.street.value
	document.frmPickup.mstreet2.value = document.frmPickup.street2.value
	document.frmPickup.mcity.value = document.frmPickup.city.value
	document.frmPickup.mstate.value = document.frmPickup.state.value
	document.frmPickup.mzip.value = document.frmPickup.zip.value
}

function validateCheckoutData() {
	/* 
	alert (document.frmCheckout.cbshipaddr.value);
	alert (document.frmCheckout.cbshipaddr.checked);
	*/
	
	var testPhone;
	testPhone = document.frmCheckout.areacode.value + '/';
	testPhone = testPhone + document.frmCheckout.prefix.value + '-';
	testPhone = testPhone + document.frmCheckout.suffix.value;
	/* alert('JS testPhone='+testPhone);*/

	
	document.frmCheckout.state.value = document.frmCheckout.state.value.toUpperCase();
	document.frmCheckout.sstate.value = document.frmCheckout.sstate.value.toUpperCase();
	
	if 	( NotEmpty ( document.frmCheckout.ccname.value ) == false ) {
		alert('You must enter your name.');
		document.frmCheckout.ccname.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmCheckout.street.value ) == false ) {
		alert('You must enter your street address.');
		document.frmCheckout.street.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmCheckout.city.value ) == false ) {
		alert('You must enter the city.');
		document.frmCheckout.city.focus();
		return false;
	}
	else if ( validateState ( document.frmCheckout.state.value ) == false ) {
		alert('You must enter a valid state.');
		document.frmCheckout.state.focus();
		return false;
	}
	else if ( validZip (document.frmCheckout.zip.value ) == false ) {
		alert('You must enter a valid zip code.');
		document.frmCheckout.zip.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmCheckout.sname.value ) == false ) {
		alert('You must enter the ship-to name.');
		document.frmCheckout.sname.focus();
		return false;
	}	
	else if ( NotEmpty ( document.frmCheckout.scompany.value ) == false ) {
		alert('You must enter the ship-to company name.');
		document.frmCheckout.scompany.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmCheckout.sstreet.value ) == false ) {
		alert('You must enter the ship-to street.');
		document.frmCheckout.sstreet.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmCheckout.scity.value ) == false ) {
		alert('You must enter the ship-to city.');
		document.frmCheckout.scity.focus();
		return false;
	}
	else if ( validateState ( document.frmCheckout.sstate.value ) == false ) {
		alert('You must enter a valid ship-to state.');
		document.frmCheckout.sstate.focus();
		return false;
	}		
	else if ( validZip ( document.frmCheckout.szip.value ) == false ) {
		alert('You must enter a valid ship-to zip.');
		document.frmCheckout.szip.focus();
		return false;
	}	
	else if ( NotEmpty ( document.frmCheckout.areacode.value ) == false 
		 ||   NotEmpty ( document.frmCheckout.prefix.value ) == false
		 ||   NotEmpty ( document.frmCheckout.suffix.value ) == false ) {
		alert('You must enter a complete phone number in case we need to contact you.');
		document.frmCheckout.areacode.focus();
		return false;
	}
	else if ( testPhone == "999/999-9999" ){
		alert('You must enter a valid phone number in case we need to contact you.');
		document.frmCheckout.areacode.focus();
		return false;
	}
	else if ( validateEmail( document.frmCheckout.email.value ) == false ) {
		document.frmCheckout.email.focus();
		return false;
	}
	else {
		document.frmCheckout.submit();
		return true;
	}
}

function validateEmail(email) {
	/* alert ( 'made it to validateEmail, email=' +email );*/
	
	invalidChars = " /:,;"
	
	if (email == ""){
		alert("You must enter your email address.");
		return false;
	}
	
	if (email.indexOf("@", 0) == 0){
		alert("Your email address is missing your user name at the beginning.");
		return false;
	}

	if (email.indexOf("@", 1) == -1){
		alert("Your email address is missing the @ sign.");
		return false;
	}
	
	if (email.indexOf(".", 0) == -1){
		alert("Your email address is missing the period.");
		return false;
	}
	
	for (i=0; i<invalidChars.length; i++)
	{
		if (email.indexOf(invalidChars.charAt(i), 0) > -1)
		{
			alert("The email address cannot contain a blank, slash, colon, comma or semicolon.");
			return false;
		}
	}

	return true;  
}

function validateState(code) {
	statecode = new Array("AL","AK","AZ","AR","CA","CO","CT","DC","DE","FL",
	"GA","HI","ID","IL","IN","IA","KS","KY","LA","ME","MD","MA","MI","MN",
	"MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR",
	"PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY")
	
	for (i=0; i<52; i++) {
		if ( code == statecode[i] ) {
			return true;
		}
	}
	return false;
}

function validatePickupData() {
	document.frmPickup.state.value = document.frmPickup.state.value.toUpperCase();
	document.frmPickup.mstate.value = document.frmPickup.mstate.value.toUpperCase();
	
	if 	( NotEmpty ( document.frmPickup.name.value ) == false ) {
		alert('You must enter your name.');
		document.frmPickup.name.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmPickup.company.value ) == false ) {
		alert('You must enter your company name.');
		document.frmPickup.company.focus();
		return false;
	}
	/* The visible areacode, prefix and suffix are on frmGetAcct, not
	   frmPickup.  The values in these fields get assigned to equivalent
	   hidden fields in the frmPickup form to be sent to the repro3.asp
	   program.  Two forms were needed because the user has to enter
	   the phone number and press Get Account to find (or fill in) the 
	   rest of the info that needs to be sent to the next page.  So, the
	   fields in this if statement use frmGetAcct, not frmPickup fields.
	*/
	else if ( NotEmpty ( document.frmGetAcct.areacode.value ) == false 
		 ||   NotEmpty ( document.frmGetAcct.prefix.value ) == false
		 ||   NotEmpty ( document.frmGetAcct.suffix.value ) == false ) {
		alert('You must enter a complete phone number in case we need to contact you.');
		/* Position back to the visible area code */
		document.frmGetAcct.areacode.focus();
		return false;
	}
	if 	( NotEmpty ( document.frmPickup.email.value ) == false ) {
		alert('You must enter your e-mail address.');
		document.frmPickup.email.focus();
		return false;
	}
	/* Only validate the email address if it's not empty */
	else if ( NotEmpty( document.frmPickup.email.value ) == true 
		 &&   validateEmail( document.frmPickup.email.value ) == false ) { 
			document.frmPickup.email.focus();
			return false;
	}
	else {
		document.frmPickup.submit();
		return true;
	}
}

function validateRequestData() {
	document.frmRequest.state.value = document.frmRequest.state.value.toUpperCase();
	document.frmRequest.mstate.value = document.frmRequest.mstate.value.toUpperCase();
	
	if 	( NotEmpty ( document.frmRequest.name.value ) == false ) {
		alert('You must enter your name.');
		document.frmRequest.name.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRequest.company.value ) == false ) {
		alert('You must enter your company name.');
		document.frmRequest.company.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRequest.areacode.value ) == false 
		 ||   NotEmpty ( document.frmRequest.prefix.value ) == false
		 ||   NotEmpty ( document.frmRequest.suffix.value ) == false ) {
		alert('You must enter a complete phone number in case we need to contact you.');
		document.frmRequest.areacode.focus();
		return false;
	}
	/* Only validate the email address if it's not empty */
	else if ( NotEmpty( document.frmRequest.email.value ) == true 
		 &&   validateEmail( document.frmRequest.email.value ) == false ) { 
			document.frmRequest.email.focus();
			return false;
	}
	else if ( NotEmpty ( document.frmRequest.mname.value ) == false ) {
		alert('You must enter the ship-to name.');
		document.frmRequest.mname.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRequest.mcompany.value ) == false ) {
		alert('You must enter the ship-to company name.');
		document.frmRequest.mcompany.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRequest.mstreet.value ) == false ) {
		alert('You must enter the ship-to street.');
		document.frmRequest.mstreet.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRequest.mcity.value ) == false ) {
		alert('You must enter the ship-to city.');
		document.frmRequest.mcity.focus();
		return false;
	}
	else if ( validateState ( document.frmRequest.mstate.value ) == false ) {
		alert('You must enter a valid ship-to state.');
		document.frmRequest.mstate.focus();
		return false;
	}		
	else if ( validZip ( document.frmRequest.mzip.value ) == false ) {
		alert('You must enter a valid ship-to zip.');
		document.frmRequest.mzip.focus();
		return false;
	}	
	else {
		document.frmRequest.submit();
		return true;
	}
}

function validateReproData() {
	document.frmRepro.state.value = document.frmRepro.state.value.toUpperCase();
	document.frmRepro.mstate.value = document.frmRepro.mstate.value.toUpperCase();
	
	if 	( NotEmpty ( document.frmRepro.name.value ) == false ) {
		alert('You must enter your name.');
		document.frmRepro.name.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRepro.company.value ) == false ) {
		alert('You must enter your company name.');
		document.frmRepro.company.focus();
		return false;
	}
	/* The visible areacode, prefix and suffix are on frmGetAcct, not
	   frmRepro.  The values in these fields get assigned to equivalent
	   hidden fields in the frmRepro form to be sent to the repro3.asp
	   program.  Two forms were needed because the user has to enter
	   the phone number and press Get Account to find (or fill in) the 
	   rest of the info that needs to be sent to the next page.  So, the
	   fields in this if statement use frmGetAcct, not frmRepro fields.
	*/
	else if ( NotEmpty ( document.frmGetAcct.areacode.value ) == false 
		 ||   NotEmpty ( document.frmGetAcct.prefix.value ) == false
		 ||   NotEmpty ( document.frmGetAcct.suffix.value ) == false ) {
		alert('You must enter a complete phone number in case we need to contact you.');
		/* Position back to the visible area code */
		document.frmGetAcct.areacode.focus();
		return false;
	}
	if 	( NotEmpty ( document.frmRepro.jobname.value ) == false ) {
		alert('You must enter a job name or description.');
		document.frmRepro.jobname.focus();
		return false;
	}
	if 	( NotEmpty ( document.frmRepro.email.value ) == false ) {
		alert('You must enter your e-mail address.');
		document.frmRepro.email.focus();
		return false;
	}
	/* Only validate the email address if it's not empty */
	else if ( NotEmpty( document.frmRepro.email.value ) == true 
		 &&   validateEmail( document.frmRepro.email.value ) == false ) { 
			document.frmRepro.email.focus();
			return false;
	}
	else if ( NotEmpty ( document.frmRepro.mname.value ) == false ) {
		alert('You must enter the ship-to name.');
		document.frmRepro.mname.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRepro.mcompany.value ) == false ) {
		alert('You must enter the ship-to company name.');
		document.frmRepro.mcompany.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRepro.mstreet.value ) == false ) {
		alert('You must enter the ship-to street.');
		document.frmRepro.mstreet.focus();
		return false;
	}
	else if ( NotEmpty ( document.frmRepro.mcity.value ) == false ) {
		alert('You must enter the ship-to city.');
		document.frmRepro.mcity.focus();
		return false;
	}
	else if ( validateState ( document.frmRepro.mstate.value ) == false ) {
		alert('You must enter a valid ship-to state.');
		document.frmRepro.mstate.focus();
		return false;
	}		
	else if ( validZip ( document.frmRepro.mzip.value ) == false ) {
		alert('You must enter a valid ship-to zip.');
		document.frmRepro.mzip.focus();
		return false;
	}	
	else {
		document.frmRepro.submit();
		return true;
	}
}

function validateSearchData() {
	if ( NotEmpty ( document.frmSearch.searchnum.value ) == false ) {
		alert('You must enter at least the first 5 digits of the product number.');
		document.frmSearch.searchnum.focus();
		return false;
	}
	else if ( validSearchNum (document.frmSearch.searchnum.value ) == false ) {
		alert('You must enter at least the first 5 digits of the product number.');
		document.frmSearch.searchnum.focus();
		return false;
	}
	else {
		document.frmSearch.submit();
		return true;
	}
}

function validateStateOld ( statecode )
{
/*
<TD WIDTH=250 NOWRAP>
	<select NAME="state">	
	<option VALUE="" SELECTED> 
	<option VALUE="AK">Alaska
	<option VALUE="AL">Alabama
	<option VALUE="AR">Arkansas
	<option VALUE="AZ">Arizona
	<option VALUE="CA">California
	<option VALUE="CO">Colorado
	<option VALUE="CT">Connecticut
	<option VALUE="DC">District of Columbia
	<option VALUE="DE">Delaware
	<option VALUE="FL">Florida
	<option VALUE="GA">Georgia
	<option VALUE="HI">Hawaii
	<option VALUE="IA">Iowa
	<option VALUE="ID">Idaho
	<option VALUE="IL">Illinois
	<option VALUE="IN">Indiana
	<option VALUE="KS">Kansas
	<option VALUE="KY">Kentucky
	<option VALUE="LA">Louisiana
	<option VALUE="MA">Massachusetts
	<option VALUE="MD">Maryland
	<option VALUE="ME">Maine
	<option VALUE="MI">Michigan
	<option VALUE="MN">Minnesota
	<option VALUE="MO">Missouri
	<option VALUE="MS">Mississippi
	<option VALUE="MT">Montana
	<option VALUE="NC">North Carolina
	<option VALUE="ND">North Dakota
	<option VALUE="NE">Nebraska
	<option VALUE="NH">New Hampshire
	<option VALUE="NJ">New Jersey
	<option VALUE="NM">New Mexico
	<option VALUE="NV">Nevada
	<option VALUE="NY">New York
	<option VALUE="OH">Ohio
	<option VALUE="OK">Oklahoma
	<option VALUE="OR">Oregon
	<option VALUE="PA">Pennsylvania
	<option VALUE="PR">Puerto Rico
	<option VALUE="RI">Rhode Island
	<option VALUE="SC">South Carolina
	<option VALUE="SD">South Dakota
	<option VALUE="TN">Tennessee
	<option VALUE="TX">Texas
	<option VALUE="UT">Utah
	<option VALUE="VA">Virginia
	<option VALUE="VT">Vermont
	<option VALUE="WA">Washington
	<option VALUE="WI">Wisconsin
	<option VALUE="WV">West Virginia
	<option VALUE="WY">Wyoming
</select>
*/
}
function NotEmpty( strValue ) {
	for (var i=0; i < strValue.length; i++ ) {
		if ( strValue.charAt(i) != ' ' ) 
		{
			return true; 
		}
	}
	return false;
}


