// JavaScript Document
function isEmailAddr(email) {
  var filterEmail  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (!filterEmail.test(email)) {
	return false;  
  }
  return true;
}

function validEmail(formField,fieldLabel,required) {
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;
}

function validRequired(formField,fieldLabel) {
	var result = true;
	if (formField.value == "") {
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	return result;
}

function validateSalesForm(theForm) {
	if (!validRequired(theForm.full_name,"Full Name")) { return false; }
	if (!validRequired(theForm.street_address,"Street Address")) { return false; }
	if (!validRequired(theForm.city,"City")) { return false; }

	if (theForm.state.selectedIndex == 0) {
			alert('Please select a state.');
			theForm.state.focus();
			return false;
	}
	
	if (!validRequired(theForm.postal_code,"Postal Code")) { return false; }
	if (!validRequired(theForm.phone_number,"Telephone")) { return false; }

	if (validRequired(theForm.email_address,"Email Address")) {
		if (!validEmail(theForm.email_address,"Email Address",true)) { return false; }
	}
	else {
		return false;	
	}
	// check shipping info
	if (theForm.shipping_change.checked) {
		if (!validRequired(theForm.shipping_full_name,"Shipping Full Name")) { return false; }
		if (!validRequired(theForm.shipping_street_address,"Shipping Street Address")) { return false; }
		if (!validRequired(theForm.shipping_city,"Shipping City")) { return false; }
	
		if (theForm.shipping_state.selectedIndex == 0) {
				alert('Please select a shipping state.');
				return false;
		}
		
		if (!validRequired(theForm.shipping_postal_code,"Shipping Postal Code")) { return false; }
	}
	
	return true;
}

function checkState(obj, dstdiv) {
	if (obj.options[obj.selectedIndex].value == "other") {
		// show state box
		var string = new Array();
		
		if (dstdiv == 'shipping_state_additional') {
			var stname = 'shipping_state_other';
		}
		else {
			var stname = 'state_other';
		}
		
		string.push("<br>Other State:&nbsp;<input type=\"text\" name=\""+stname+"\" value=\"\">");
		var writestring = string.join('');
		document.getElementById(dstdiv).innerHTML = writestring;
	}
	else {
		document.getElementById(dstdiv).innerHTML = '';
	}
}

function toggleShipping(obj) {
	var html = new Array();
	
	if (obj.checked) {
		html.push("<table width=\"99%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");	
		html.push("<tr>");	
		html.push("<td><h2>Shipping Information </h2></td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><table width=\"99%\" border=\"0\" cellspacing=\"2\" cellpadding=\"3\">");	
		html.push("<tr>");	
		html.push("<td width=\"29%\"><strong>Full Name: </strong> <span style=\"color:red;\">*</span></td>");	
		html.push("<td width=\"71%\"><label>");	
		html.push("<input name=\"shipping_full_name\" type=\"text\" id=\"shipping_full_name\" size=\"32\" />");	
		html.push("</label></td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><strong>Street Address: </strong> <span style=\"color:red;\">*</span></td>");	
		html.push("<td><input name=\"shipping_street_address\" type=\"text\" id=\"shipping_street_address\" size=\"48\" /></td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><strong>Street Address 2: </strong></td>");	
		html.push("<td><input name=\"shipping_street_address2\" type=\"text\" id=\"shipping_street_address2\" size=\"48\" /></td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><strong>City: </strong> <span style=\"color:red;\">*</span></td>");	
		html.push("<td><input name=\"shipping_city\" type=\"text\" id=\"shipping_city\" size=\"24\" /></td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><strong>State: </strong> <span style=\"color:red;\">*</span></td>");	
		html.push("<td><label>");	
		html.push("<select name=\"shipping_state\" id=\"shipping_state\" onChange=\"javascript:checkState(this, 'shipping_state_additional');\">");	
		
		html.push("<option value='AL'>Alabama</option>");	
		html.push("<option value='AK' >Alaska</option>");	
		html.push("<option value='AS' >American Samoa</option>");	
		html.push("<option value='AZ' >Arizona</option>");	
		html.push("<option value='AR' >Arkansas</option>");	
		html.push("<option value='AA' >Armed Forces (AA)</option>");	
		html.push("<option value='AE' >Armed Forces (AE)</option>");	
		html.push("<option value='AP' >Armed Forces (AP)</option>");	
		html.push("<option value='CA' >California</option>");	
		html.push("<option value='CO' >Colorado</option>");	
		html.push("<option value='CT' >Connecticut</option>");	
		html.push("<option value='DE' >Delaware</option>");	
		html.push("<option value='DC' >District of Columbia</option>");	
		html.push("<option value='FM' >Federated States of Micronesia</option>");	
		html.push("<option value='FL' >Florida</option>");	
		html.push("<option value='GA' >Georgia</option>");	
		html.push("<option value='GU' >Guam</option>");	
		html.push("<option value='HI' >Hawaii</option>");	
		html.push("<option value='ID' >Idaho</option>");	
		html.push("<option value='IL' >Illinois</option>");	
		html.push("<option value='IN' >Indiana</option>");	
		html.push("<option value='IA' >Iowa</option>");	
		html.push("<option value='KS' >Kansas</option>");	
		html.push("<option value='KY' >Kentucky</option>");	
		html.push("<option value='LA' >Louisiana</option>");	
		html.push("<option value='ME' >Maine</option>");	
		html.push("<option value='MH' >Marshall Islands</option>");	
		html.push("<option value='MD' >Maryland</option>");	
		html.push("<option value='MA' >Massachusetts</option>");	
		html.push("<option value='MI' >Michigan</option>");	
		html.push("<option value='MN' >Minnesota</option>");	
		html.push("<option value='MS' >Mississippi</option>");	
		html.push("<option value='MO' >Missouri</option>");	
		html.push("<option value='MT' >Montana</option>");	
		html.push("<option value='NE' >Nebraska</option>");	
		html.push("<option value='NV' >Nevada</option>");	
		html.push("<option value='NH' >New Hampshire</option>");	
		html.push("<option value='NJ' >New Jersey</option>");	
		html.push("<option value='NM' >New Mexico</option>");	
		html.push("<option value='NY' >New York</option>");	
		html.push("<option value='NC' >North Carolina</option>");	
		html.push("<option value='ND' >North Dakota</option>");	
		html.push("<option value='MP' >Morthern Marian Islands</option>");	
		html.push("<option value='OH' >Ohio</option>");	
		html.push("<option value='OK' >Oklahoma</option>");	
		html.push("<option value='OR' >Oregon</option>");	
		html.push("<option value='PW' >Paulau</option>");	
		html.push("<option value='PA' >Pennsylvania</option>");	
		html.push("<option value='PR' >Puerto Rico</option>");	
		html.push("<option value='RI' >Rhode Island</option>");	
		html.push("<option value='SC' >South Carolina</option>");	
		html.push("<option value='SD' >South Dakota</option>");	
		html.push("<option value='TN' >Tennessee</option>");	
		html.push("<option value='TX' >Texas</option>");	
		html.push("<option value='UT' >Utah</option>");	
		html.push("<option value='VT' >Vermont</option>");	
		html.push("<option value='VI' >Virgin Islands</option>");	
		html.push("<option value='VA' >Virginia</option>");	
		html.push("<option value='WA' >Washington</option>");	
		html.push("<option value='WV' >West Virginia</option>");	
		html.push("<option value='WI' >Wisconsin</option>");	
		html.push("<option value='WY' >Wyoming</option>");	
		html.push("<option value='other'>Other (Please Enter)</option>");	
		html.push("</select>");	
		
		html.push("</label>");
		html.push("&nbsp;<div id=\"shipping_state_additional\"></div>");	
		html.push("</td>");	
		html.push("</tr>");	
		html.push("<tr>");	
		html.push("<td><strong>Postal Code:</strong> <span style=\"color:red;\">*</span></td>");	
		html.push("<td><input name=\"shipping_postal_code\" type=\"text\" id=\"postal_code\" size=\"8\" /></td>");	
		html.push("</tr>");	
		html.push("</table></td>");	
		html.push("</tr>");	
		html.push("</table>");		
		html.push("<p>&nbsp;</p>");	
			
		var htmlwrite = html.join('');
		document.getElementById('shipping_information').innerHTML = htmlwrite;
	}
	else {
		document.getElementById('shipping_information').innerHTML = '';
	}
}