// Rev.T2 6.16.06 - Last change: GoToFeaturedStore, Address2S, SetShipTo, ToArrayOfNumbers, IsEmailOk, toFixed/ToDecimal2
function GoToFeaturedStore(form)
{
	if(form.menu.options[form.menu.selectedIndex].value) {
		if(form.menu.options[form.menu.selectedIndex].value.indexOf("stores.php") > -1)
			window.location.href = form.menu.options[form.menu.selectedIndex].value;
		else
			window.open(form.menu.options[form.menu.selectedIndex].value);
	}
}
function openStoreInNewWindow(targ,selObj,restore){ 
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function PopupWin(theURL, winName, width, height)
{
	window.open(theURL, winName, "menubar=no,toolbar=no,location=no,scrollbars=no,resizable=yes,status=no,width="+width+",height="+height);
}

function chkEnterKey(e)
{
	if (!e)
	{
		if (window.event)
			e = window.event;
		else
			return false;
	}
	
	if (typeof( e.which ) == 'number')
	{
		//NS 4, NS 6+, Mozilla 0.9+, Opera
		e = e.which;
	}
	else if (typeof( e.keyCode ) == 'number')
	{
		//IE, NS 6+, Mozilla 0.9+
		e = e.keyCode;
	}
	else if (typeof( e.charCode ) == 'number')
	{
		//also NS 6+, Mozilla 0.9+
		e = e.charCode;
	}
	else
		return false;
	
	if (e == 13)
		return true;
}

function IsEmailOk(email)
{
	var strMsg = 'Please log in using the EMAIL address associated with your account!';
	if (email.value == "" || email.value.replace(/\s/g, "").length == 0) {
		alert(strMsg);
		email.select(); return false;
	}
	else {
		if (email.value.search(/['\s]/g) > -1) {
			alert("Invalid Email Address! \n " + strMsg);
			email.select(); return false;
		}
		if (email.value.search(/.+@.+\.[a-z]{2,}/i) == -1) {
			alert("Invalid Email Address! \n " + strMsg);
			email.select(); return false;
		}
	}
	return true;
}

function IsAddressInfoOk(form)
{
	if (form.FirstNameB.value == "" || form.FirstNameB.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out First Name of the Billing Address!");
		form.FirstNameB.select(); return false;
	}
	if (form.LastNameB.value == "" || form.LastNameB.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out Last Name of the Billing Address!");
		form.LastNameB.select(); return false;
	}
	if (form.Phone.value == "" || form.Phone.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out Phone of the Billing Address!");
		form.Phone.select(); return false;
	}
	if (form.Address1B.value == "" || form.Address1B.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out Address of the Billing Address!");
		form.Address1B.select(); return false;
	}
	if (form.CityB.value == "" || form.CityB.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out City of the Billing Address!");
		form.CityB.select(); return false;
	}
	if(form.CountryB)
	{
		if (form.CountryB.value == "") {
			alert("Please fill out Country of the Billing Address!");
			form.CountryB.focus(); return false;
		}
	}
	if (!form.ShipToSame.checked) 
	{
		if (form.FirstNameS.value == "" || form.FirstNameS.value.replace(/\s/g, "").length == 0) {
			alert("Please fill out First Name of the Shipping Address!");
			form.FirstNameS.select(); return false;
		}
		if (form.LastNameS.value == "" || form.LastNameS.value.replace(/\s/g, "").length == 0) {
			alert("Please fill out Last Name of the Shipping Address!");
			form.LastNameS.select(); return false;
		}
		if (form.Address1S.value == "" || form.Address1S.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out Address of the Shipping Address!");
		form.Address1S.select(); return false;
		}
		if (form.CityS.value == "" || form.CityS.value.replace(/\s/g, "").length == 0) {
			alert("Please fill out City of the Shipping Address!");
			form.CityS.select(); return false;
		}
		if (form.CountryShippingDD)
		{
			if (form.CountryShippingDD.value == "") {
				alert("Please fill out Country of the Shipping Address!");
				form.CountryShippingDD.focus(); return false;
			}
		}
		if (form.CountryS)
		{
			if (form.CountryS.value == "") {
				alert("Please fill out Country of the Shipping Address!");
				form.CountryS.focus(); return false;
			}
		}		
	}
	return true;
}

function IsCreditCardOk(form)
{
	if(form && form != null)
	{
		var ccNumber = form.CCNumber;
		var securityCode = form.CCSecurityCode;
		var month = form.CCMonth;
		var year = form.CCYear;
		var owner = form.CCOwner;
		var bankPhone = form.CCBankPhone;
		if(form.CCBankName)
			var bankName = form.CCBankName;
	}
	else
	{
		var ccType = document.getElementById('CCType');
		var ccNumber = document.getElementById('CCNumber');
		var securityCode = document.getElementById('CCSecurityCode');
		var month = document.getElementById('CCMonth');
		var year = document.getElementById('CCYear');
		var owner = document.getElementById('CCOwner');
		var bankPhone = document.getElementById('CCBankPhone');
		if(document.getElementById('CCBankName'))
			var bankName = document.getElementById('CCBankName');
		if (ccType.value == "") {
			alert("Please choose a 'Credit Card Type'!");
			ccType.focus(); return false;
		}
	}
	if (ccNumber.value == "" || ccNumber.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out the field 'Credit Card Number'!");
		ccNumber.select(); return false;
	}
	else {
		if (ccNumber.value.search(/[^\d]/) > -1) {
			alert("Please use digits only!");
			ccNumber.select(); return false;
		}
	}
	if (securityCode) {
		if (securityCode.value == "" || securityCode.value.replace(/\s/g, "").length == 0) {
			alert("Please fill out the field 'Security Code'!");
			securityCode.select(); return false;
		}
		else {
			if (securityCode.value.search(/[^\d]/) > -1) {
				alert("Please use digits only!");
				securityCode.select(); return false;
			}
		}
	}
	if (month.value == "") {
		alert("Please choose an 'Expiration Month'!");
		month.focus(); return false;
	}
	if (year.value == "") {
		alert("Please choose an 'Expiration Year'!");
		year.focus(); return false;
	}
	if (owner.value == "" || owner.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out the field 'Card Owner'!");
		owner.select(); return false;
	}
	if (bankPhone.value == "" || bankPhone.value.replace(/\s/g, "").length == 0) {
		alert("Please fill out the field 'Bank Customer Service Phone Number'!");
		bankPhone.select(); return false;
	}
	else {
		if (bankPhone.value.search(/'/g) > -1) {
			alert("Please don't use quotes in this field!");
			bankPhone.select(); return false;
		}
	}
	if(document.getElementById('CCBankName') || form.CCBankName)
	{
		if (bankName.value == "" || bankName.value.replace(/\s/g, "").length == 0) {
			alert("Please fill out the field 'Bank Name'!");
			bankName.select(); return false;
		}
	}
	return true;
}
function IsUsernameOk(username)
{
	if(!isSpace(username))
		return false;
	if (username.value.length < 6) 
	{
		alert ('Your username must have at least 6 characters!');
		username.focus();
		return false;
	}
	return true;
}
function isSpace(cntrl)
{
	if (cntrl.value.indexOf(' ') != -1)
	{
		alert('Invalid Username: Space is not allowed in Username');
		cntrl.focus();
		return false;
	}
	else
		return true;
}
function isUserValid(username)
{
/*
	if (username.value.search(/[^a-zA-Z\d]/) > -1)
	{
		alert ('Your username can contain letters and digits only!');
		username.focus();
		return false;
	}
*/
	return true;
}
function IsPasswordOk()
{
	var pw = arguments[0];
	if (arguments.length > 1) var pwV = arguments[1];
	if (pw.value.length < 4) {
		alert("Your password must have at least 4 characters!");
		pw.select(); pw.focus(); return false;
	}
	if	(pw.value.search(/[^a-zA-Z\d]/) > -1) {
		alert("Your password can contain letters and digits only!");
		pw.select(); pw.focus(); return false;
	}
	if	(pwV && pw.value != pwV.value) {
		alert("The passwords don't match!");
		pwV.select(); pwV.focus(); return false;
	}
	return true;
}

function CheckBrowser()
{
	//var mt_versions_IE = new Array(0, 312);
	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 = GetBrowserInfo();
	//browser[1] = 125.9;
	//alert("'" + browser[0] + "' " + browser[1]);
	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)
{
	var msg = "You are using " + name + " " + version;
	if (name == "Safari") msg += " (build-version)";
	msg += " as your browser.\n";
	if (mt_v[0] > 0)
		msg += "This website requires " + name + " " + mt_v[0] + " or higher! \nPlease update your browser!";
	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 GetBrowserInfo()
{
	var name;
	var v = navigator.userAgent.replace(/\s/g, "/");
	var x = v.split("/");
	x = x[x.length - 1];
	x = x.substr(x.length - 1);
	//alert(x);
	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];
	//alert("|" + name + "|" + version + "|");
	var rv = new Array(name, version);
	return rv;
}

function UncheckSame(form)
{
	form.ShipToSame.checked = false;
}

function SetShipTo(form)
{
	var country = form.ShippingCountry.value.replace(/\\/, "");
	if (form.ShipToSame.checked) {
		// Billing country = ShippingCountry
		form.CountryB.options[0].text = country;
		form.CountryB.options[0].value = country;
		form.CountryB.options[0].selected = true;
		// Disable B. country dropdown
		//form.CountryB.enabled = false;
		// 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;
		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 {
		// Enable B. country dropdown
		//form.CountryB.enabled = true;
		form.CountryS.options[0].text = country;
		form.CountryS.options[0].value = country;
		form.CountryS.options[0].selected = true;
	}
}

function InitCountry(countryDropDown, country)
{
	country = country.replace(/\\/, "");
	countryDropDown.options[0].text = country;
	countryDropDown.options[0].value = country;
}

function InitCC(form, ccType, ccNumber, ccMonth, ccYear)
{
	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;
	}
}

function InitCurrConverter(form, currency_idx)
{
//	form.CurrConvCountry.selectedIndex = currency_idx;
	form.CurrConvCountry.value = currency_idx;
}

function GoBack(form, domain, page, sid)
{
	if (form) {
		form.action = domain + page + (sid ? "?" + sid : "");
		form.submit();
	}
	else
		window.location.href = domain + page + (sid ? "?" + sid : "");
}

function Round(number)
{
	number = parseFloat(number.toString().replace(',', ''));
//	number = parseFloat(number);
	var x = Math.round(number * 100) / 100;
	return x;
}

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

function Currency(price)
{
	price = ToDecimal2(Round(price));
	//price = price.toFixed(2);
	//price = String(Round(price));
	//alert("Currency:" + 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 ToArrayOfNumbers(csvString)
{
	arrayOfNumbers = new Array();
	arrayOfStrings = csvString.split(",");
	for (var i = 0; i < arrayOfStrings.length; i++)
		arrayOfNumbers[i] = parseFloat(arrayOfStrings[i]);
	return arrayOfNumbers;
}
function removeSpaces(string) 
{
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
		tstring += splitstring[i];
	return tstring;
}