var buttonClicked = false;

function isValidEmailAddress(email) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	return re.test(email);
}

function isValidCreditCard(type, ccnum) {
	ccnum = replaceSubstring(ccnum, " ", "-");
	ccnum = replaceSubstring(ccnum, "-", "");
	if (type == "Visa") {
		// Visa: length 16, prefix 4.
		var re = /^4\d{15}$/;
	} else if (type == "MC") {
		// Mastercard: length 16, prefix 51-55.
		var re = /^5[1-5]\d{14}$/;
	} else if (type == "Disc") {
		// Discover: length 16, prefix 6011.
		var re = /^6011-?\d{12}$/;
	} else if (type == "AmEx") {
		// American Express: length 15, prefix 34 or 37.
		var re = /^3[4,7]\d{13}$/;
	} else if (type == "Diners") {
		// Diners: length 14, prefix 30, 36, or 38.
		var re = /^3[0,6,8]\d{12}$/;
	}
	if (!re.test(ccnum)) return false;
	// Checksum (Mod 10)
	// Add even digits in even length strings or odd digits in odd length strings.
	var checksum = 0;
	for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
		checksum += parseInt(ccnum.charAt(i-1));
	}
	// Analyze odd digits in even length strings or even digits in odd length strings.
	for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+= 2) {
		var digit = parseInt(ccnum.charAt(i-1)) * 2;
		if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
	}
	if ((checksum % 10) == 0) return true; else return false;
}

function submitForm() {
	if (buttonClicked) {
		alert("Please click the \"Submit\" button only one time!");
		return false;
	}
	buttonClicked = true;
	var form = document.forms["DownloadForm"];
	if (form.License.constructor == Array) { var license = form.License[0]; } else { var license = form.License; }
	if (!license.checked) {
		alert("Please read the license agreement and click the check box that you agree with the terms.");
		buttonClicked = false;
		return false;
	}
	if (trim(form.Company.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your company name.");
		form.Company.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.UserName.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter a contact name.");
		form.UserName.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.Email.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter the contact\'s email address.");
		form.Email.focus();
		buttonClicked = false;
		return false;
	}
	if (!isValidEmailAddress(form.Email.value)) {
		alert("The email address \"" + form.Email.value + "\" does not appear to be valid.");
		form.Email.select();
		form.Email.focus();
		buttonClicked = false;
		return false;
	}
	if (form.CCName.selectedIndex == 0) {
		alert("Fields marked with an asterisk are required.\nPlease choose the credit card to use.");
		form.CCName.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.CCFirstName.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your first name as it appears on the credit card.");
		form.CCFirstName.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.CCLastName.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your last name as it appears on the credit card.");
		form.CCLastName.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.Address1.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your address.");
		form.Address1.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.City.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your city or town.");
		form.City.focus();
		buttonClicked = false;
		return false;
	}
	if (form.State.selectedIndex == 0) {
		alert("Fields marked with an asterisk are required.\nPlease choose your state, or choose \"Not Applicable\".");
		form.State.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.Zip.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your zip/postal code.");
		form.Zip.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.Phone.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter your phone number.");
		form.Phone.focus();
		buttonClicked = false;
		return false;
	}
	if (trim(form.CCNum.value) == "") {
		alert("Fields marked with an asterisk are required.\nPlease enter the credit card number.");
		form.CCNum.focus();
		buttonClicked = false;
		return false;
	}
	if (!isValidCreditCard(form.CCName[form.CCName.selectedIndex].value, form.CCNum.value)) {
		alert("The credit card number \"" + form.CCNum.value + "\" does not appear to be valid.");
		form.CCNum.select();
		form.CCNum.focus();
		buttonClicked = false;
		return false;
	}
	if (form.Hear.selectedIndex == 0) {
		alert("Please tell us where you heard about the Reusable Object Library product.");
		form.Hear.focus();
		buttonClicked = false;
		return false;
	}
	if (document.all) {
		document.all["SubmitButton"].value = "Please wait...";
	} else if (document.getElementById) {
		document.getElementById("SubmitButton").value = "Please wait...";
	}
	form.submit();
}

function newWinLinkToDocument(name) {
	var height = Math.floor(screen.height * 0.75);
	var width = Math.floor(screen.width * 0.75);
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) * 0.25);
	var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
	winParms += "toolbar=no,scrollbars=yes,resizable=yes,status=yes";
	for (var i=0; i<linkNames.length; i++) {
		if (linkNames[i] == name) {   // This is the document to show
			var newURL = "/" + dbname + "/Plain?OpenNavigator&" + linkUNIDs[i];
			var win = window.open(newURL, "Example", winParms);
			if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
		}  // Ends the check to see if this is the document to link to
	}  // Go on to the next document name
}   // Ends the "Link To Document" function

function secureSite(url) {
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
self.name = "mainWin";
}

