// 4/15/09
function CheckEmail(email) // login.js, unlock.js
{
	if (email.value == "" || email.value.replace(/\s/g, "").length == 0) {
		alert("Our quality control department requires an email address.");
		email.select(); return false;
	}
	else {
		if (email.value.search(/['\s]/g) > -1) {
			alert("Please verify your email!\nThe email you have entered does not adhere to a valid email address format.\nQuotes and spaces are not allowed.");
			email.select(); return false;
		}
		if (email.value.search(/.+@.+\.[a-z]{2,}/i) == -1) {
			alert("Please verify your email!\nThe email you have entered does not adhere to a valid email address format.");
			email.select(); return false;
		}
	}
	return true;
}

function CheckAddress(form) // account.js, c_billing.js
{
	if (form.FirstNameB.value == "" || form.FirstNameB.value.replace(/\s/g, "").length == 0) {
		alert("We would like to know your first name, please.");
		form.FirstNameB.select(); return false;
	}
	if (form.LastNameB.value == "" || form.LastNameB.value.replace(/\s/g, "").length == 0) {
		alert("We would like to know your last name, please.");
		form.LastNameB.select(); return false;
	}
	if (!CheckEmail(form.Email)) return false;

	if (form.Phone.value == "" || form.Phone.value.replace(/\s/g, "").length == 0) {
		alert("We would like to know your phone number, please.");
		form.Phone.select(); return false;
	}
	if (form.Address1B.value == "" || form.Address1B.value.replace(/\s/g, "").length == 0) {
		alert("We would like to know your address, please.");
		form.Address1B.select(); return false;
	}
	if (form.CityB.value == "" || form.CityB.value.replace(/\s/g, "").length == 0) {
		alert("We would like to know your city, please.");
		form.CityB.select(); return false;
	}
	if (form.CityB.value.search(/\d/g) != -1) {
		alert("Please do NOT use NUMBERS in the field \"City\"!");
		form.CityB.select(); return false;
	}
	if (form.CountryB.value == "") {
		alert("We would like to know your country, please.");
		form.CountryB.focus(); return false;
	}
	if (!form.ShipToSame.checked) {
		if (form.FirstNameS.value == "" || form.FirstNameS.value.replace(/\s/g, "").length == 0) {
			alert("Please enter a \"First Name\" here!");
			form.FirstNameS.select(); return false;
		}
		if (form.LastNameS.value == "" || form.LastNameS.value.replace(/\s/g, "").length == 0) {
			alert("Please enter a \"Last Name\" here!");
			form.LastNameS.select(); return false;
		}
		if (form.Address1S.value == "" || form.Address1S.value.replace(/\s/g, "").length == 0) {
		alert("Please enter an \"Address\" here!");
		form.Address1S.select(); return false;
		}
		if (form.CityS.value == "" || form.CityS.value.replace(/\s/g, "").length == 0) {
			alert("Please enter a \"City\" here!");
			form.CityS.select(); return false;
		}
		if (form.CityS.value.search(/\d/g) != -1) {
			alert("Please do NOT use NUMBERS in the field \"City\"!");
			form.CityS.select(); return false;
		}
	}
	return true;
}

function CheckCreditCard(form) // account.js, c_billing.js
{
	if (form.CCType.value == "") {
		alert("Which credit card would you prefer to use for this transaction?");
		form.CCType.focus(); return false;
	}
	if (form.CCNumber.value == "" || form.CCNumber.value.replace(/\s/g, "").length == 0) {
		alert("In order to process your order we would like your Credit Card Number.");
		form.CCNumber.select(); return false;
	}
	else {
		if (form.CCNumber.value.search(/[^\d]/) > -1) {
			alert("Unfortunately we can only accept NUMBERS (no spaces) in this field.");
			form.CCNumber.select(); return false;
		}
	}
	if (form.CCSecurityCode) {
		if (form.CCSecurityCode.value == "" || form.CCSecurityCode.value.replace(/\s/g, "").length == 0) {
			alert("You may need to turn your credit card over to find the Security Code we are requesting.");
			form.CCSecurityCode.select(); return false;
		}
		else {
			if (form.CCSecurityCode.value.search(/[^\d]/) > -1) {
				alert("Unfortunately we can only accept NUMBERS in this field.");
				form.CCSecurityCode.select(); return false;
			}
		}
	}
	if (form.CCMonth.value == "") {
		alert("Which month does your credit card expire?");
		form.CCMonth.focus(); return false;
	}
	if (form.CCYear.value == "") {
		alert("What year does your credit card expire?");
		form.CCYear.focus(); return false;
	}
	if (form.CCOwner.value == "" || form.CCOwner.value.replace(/\s/g, "").length == 0) {
		alert("Please tell us the Cardholder's Name as it appears on the credit card (usually your name)!");
		form.CCOwner.select(); return false;
	}
	if (form.CCBankName.value == "" || form.CCBankName.value.replace(/\s/g, "").length == 0) {
		alert("Please tell us the name of the credit card issuing bank!");
		form.CCBankName.select(); return false;
	}
	if (form.CCBankPhone.value == "" || form.CCBankPhone.value.replace(/\s/g, "").length == 0) {
		alert("For your security we are respectfully requesting your financial institution?s phone number.");
		form.CCBankPhone.select(); return false;
	}
	else {
		if (form.CCBankPhone.value.search(/'/g) > -1) {
			alert("Unfortunately we are unable to accept quotation marks in this particular field.\nPlease verify your information!");
			form.CCBankPhone.select(); return false;
		}
	}
	return true;
}

function CheckPassword() // account.js, c_summary.js, login.js
{
	var pw = arguments[0];
	if (arguments.length > 1) var pwV = arguments[1];
	var pw_length_min = 6;
	if (pw.value.length < pw_length_min) {
		alert("We require a minimum of " + pw_length_min + " characters for any password. We will gladly accept NUMBERS and/or LETTERS.");
		pw.select(); pw.focus(); return false;
	}
	if	(pw.value.search(/[^a-zA-Z\d]/) > -1) {
		alert("We thought NUMBERS and LETTERS were enough. We are unable to process your password request because we cannot process symbols.\nPlease re-enter!");
		pw.select(); pw.focus(); return false;
	}
	if	(pwV && pw.value != pwV.value) {
		alert("Our Verification Department regrets to inform you that your passwords do not match.\nPlease re-enter!");
		pwV.select(); pwV.focus(); return false;
	}
	return true;
}

function DetectBrowser() // index.php
{
	var mt_versions_IE = new Array(5.5, 0);
	var mt_versions_NS = new Array(6, 0);
	var mt_versions_FF = new Array(1, 0);
	var mt_versions_OP = new Array(0, 8.5);
	var mt_versions_SF = new Array(0, 312);
	var browser = GetBrowser();
	switch (browser[0]) {
		case "Microsoft Internet Explorer":
			if ( browser[1] < mt_versions_IE[0] || (mt_versions_IE[0] == 0 && browser[1] < mt_versions_IE[1]) )
				BrowserWarning(browser[0], browser[1], mt_versions_IE); break;
		case "Netscape":
			if ( browser[1] < mt_versions_NS[0] || (mt_versions_NS[0] == 0 && browser[1] < mt_versions_NS[1]) )
				BrowserWarning(browser[0], browser[1], mt_versions_NS); break;
		case "Firefox":
			if ( browser[1] < mt_versions_FF[0] || (mt_versions_FF[0] == 0 && browser[1] < mt_versions_FF[1]) )
				BrowserWarning(browser[0], browser[1], mt_versions_FF); break;
		case "Opera":
			if ( browser[1] < mt_versions_OP[0] || (mt_versions_OP[0] == 0 && browser[1] < mt_versions_OP[1]) )
				BrowserWarning(browser[0], browser[1], mt_versions_OP); break;
		case "Safari":
			if ( browser[1] < mt_versions_SF[0] || (mt_versions_SF[0] == 0 && browser[1] < mt_versions_SF[1]) )
				BrowserWarning(browser[0], browser[1], mt_versions_SF); break;
		default:
			BrowserWarning(browser[0], browser[1], 0);
	}
}

function BrowserWarning(name, version, mt_v) // DetectBrowser
{
	var msg = "Our Network Administrator has verified that you are using " + name + " " + version;
	if (name == "Safari") msg += " (build-version)";
	msg += " as your browser.\n";
	if (mt_v[0] > 0)
		msg += "Our website requires " + name + " " + mt_v[0] + " or higher!nPlease update your browser!\nIf you proceed without updating your browser, you may encounter errors during the payment and processing phase and you will not be able to shop.";
	else {
		if (mt_v[1] > 0)
			msg += "We tested our website with " + name + " " + mt_v[1] + ".\nEarlier versions of this browser, however, might not work properly.\nIn case of technical problems please update your browser!";
		else
			msg += "This website requires a modern browser that supports JavaScript 1.5 in order to work flawlessly.\nPlease consult our Help-page for further information about browser requirements.";
	}
	
	alert(msg);
}

function GetBrowser() // DetectBrowser
{
	var name;
	var v = navigator.userAgent.replace(/\s/g, "/");
	var x = v.split("/");
	x = x[x.length - 1];
	x = x.substr(x.length - 1);
	if (x == ")") { // Internet Explorer
		v = v.split(";");
		v = v[1].split("/");
		name = "Microsoft Internet Explorer";
	}
	else {
		v = v.split("/");
		name =  v[v.length - 2];
	}
	v = v[v.length - 1];
	// Ensure decimal format (just one dot)
	v = v.split(".");
	var version = v[0];
	if (v.length > 1) version += "." + v[1];
	var rv = new Array(name, version);
	return rv;
}

function UncheckSame(form) // account.php, c_billing.php
{
	form.ShipToSame.checked = false;
}

function SetShipTo(form) // account.php, c_billing.php
{
	if (form.ShippingCountry)
		var sCountry = form.ShippingCountry.value.replace(/\\/, "");
	else
		var sCountry = form.CountryS.value.replace(/\\/, "");
	//alert("SetShipTo: " + sCountry);
	if (form.ShipToSame.checked) {
		// Copy Billing address to Shipping address
		form.FirstNameS.value = form.FirstNameB.value;
		form.LastNameS.value = form.LastNameB.value;
		form.CompanyS.value = form.CompanyB.value;
		form.Address1S.value = form.Address1B.value;
		form.Address2S.value = form.Address2B.value;
		form.CityS.value = form.CityB.value;
		form.StateS.value = form.StateB.value;
		form.ZipS.value = form.ZipB.value;
		//alert("Form element type of CountryS: " + form.CountryS.type);
		// if checkout (not account form), Billing country = ShippingCountry
		if (form.name == "orderform"/*form.CountryS.type == "hidden"*/) {
			form.CountryB.options[0].text = sCountry;
			form.CountryB.options[0].value = sCountry;
			form.CountryB.options[0].selected = true;
		}
		else {
			form.CountryS.options[0].text = form.CountryB.options[form.CountryB.selectedIndex].text;
			form.CountryS.options[0].value = form.CountryB.options[form.CountryB.selectedIndex].value;
			form.CountryS.options[0].selected = true;
		}
	}
	else {
		// if account form (not checkout), Shipping country dropdown = ShippingCountry
		if (form.name != "orderform"/*form.CountryS.type != "hidden"*/) {
			form.CountryS.options[0].text = sCountry;
			form.CountryS.options[0].value = sCountry;
			form.CountryS.options[0].selected = true;
		}
	}
}

function InitCountry(countryDropDown, country) // account.php, c_billing.php
{
	country = country.replace(/\\/, "");
	//alert("InitCountry: " + country);
	countryDropDown.options[0].text = country;
	countryDropDown.options[0].value = country;
}

function InitCC(form, ccType, ccNumber, ccMonth, ccYear, ccSecCode) // account.php, c_billing.php
{
	if (ccType != "") {
		form.CCType.options[0].text = ccType;
		form.CCType.options[0].value = ccType;
	}
	if (ccNumber != "") form.CCNumber.value = ccNumber;
	if (ccMonth != "" && ccYear != "") {
		form.CCMonth.options[0].text = ccMonth;
		form.CCMonth.options[0].value = ccMonth;
		form.CCYear.options[0].text = ccYear;
		form.CCYear.options[0].value = ccYear;
	}
	if (ccSecCode != "") form.CCSecurityCode.value = ccSecCode;
}

function GoBack(form, domain, page) // c_billing.php, c_summary.php, error_cc.php
{
	if (form) {
		form.action = domain + page;
		form.submit();
	}
	else
		window.location.href = domain + page;
}

function CurrencyFormat(price) // curr_conv.js, order.js
{
	/* price = ToDecimal2(Round(price));
	var parts = price.split(".");
	var digitcount = parts[0].length;
	var p = new Array();
	var j = 0;
	for (var i = digitcount; i >= 0; i--) {
		p.push(parts[0].substr(i, 1));
		if (j % 3 == 0 && j != 0 && i != 0) p.push(",");
		j++;
	}
	p = p.reverse().join("");
	price = p + "." + parts[1]; */
	return price; 
}

function Round(number) // CurrencyFormat
{
	number = parseFloat(number);
	var x = Math.round(number * 100) / 100;
	return x;
}

function ToDecimal2(number) // CurrencyFormat
{
	number = String(number);
	if (number.indexOf(".") > -1) {
		var parts = number.split(".");
		if (parts[1].length == 1) number += "0";
	}
	else
		number += ".00";
	return number;
}

function PopupWin() // c_billing.php, c_shipping.php, c_summary.php
{
	var theURL = arguments[0];
	var winName = arguments[1];
	var width = arguments[2];
	var height = arguments[3];
	var sb = "no";
	if (arguments.length > 4) sb = arguments[4];
	window.open(theURL, winName, "menubar=no,toolbar=no,location=no,scrollbars=" + sb +",resizable=yes,status=no,width=" + width + ",height=" + height);
}

