var expired = "Wed, 13 Jun 2000 00:00:01 UTC";
var expires;
setExpires(24 * 365); // 1 year for cookies.

var ismac=(navigator.appVersion.indexOf("Mac")!=-1)?true:false;

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for (i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)	x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if (d.images) {
		if (!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for (i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0) {
				d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];
			}
	}
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for (i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null) {
			document.MM_sr[j++]=x; if (!x.oSrc)	x.oSrc=x.src;x.src=a[i+2];
		}
}

function MM_findObj(n, d) {	//v4.0
	var p,i,x;  if (!d)	d=document;if ((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if (!(x=d[n])&&d.all) x=d.all[n];for (i=0;!x&&i<d.forms.length;i++)	x=d.forms[i][n];
	for (i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if (!x && document.getElementById) x=document.getElementById(n);return x;
}

function getKey (name) {
	return "s:" + name;
}

function removeCookie (name) {
	emptyStr = "=; path=/; expires=" + expired;
	document.cookie = name + emptyStr;
}

function resetExpires() {
	expires = expired;
}

// hours past nowo setExpires(24) would expire in 1 day.
function setExpires (hours) {
	var now = (new Date()).getTime();
	var d   = new Date(now + hours * 60 * 60 * 1000);
	expires = d.toGMTString();
	return expires;
}

function setCookie(name, value) {
	document.cookie = name + "=" + value + "; path=/" + "; expires=" + expires;
	return value;
}

function setKey(name, value) {
	setCookie(getKey(name), value);
	return value;
}

function sumCookie(name, value) {
	var qtynow = getCookie(name);
	var qty = parseInt(value) + parseInt(qtynow);
	// setCookie(name, qty);
	return qty;
}

function sumKey(name, value) {
	return sumCookie(getCookie(getKey(name)), value);
}

function removeKey (name) {
	removeCookie(getKey(name));
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return document.cookie.substring(offset, endstr);
}

function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)	break;
	}
	return 0;
}

function addToCart(sku, name, price, qty, invcount) {
	var key = getKey(sku);
	var msg = "";
	var doit = false;
	document.cookie = "testcookie=mytestvalue; path=/";
	if (getCookie("testcookie")!="mytestvalue") {
		alert("\nNO COOKIE SUPPORT\n \nYou will not be able to use the shopping basket without cookie support.\n \nFor more information on Cookie Support and turning it on please see your Browsers help section.\n");
	} else if (qty > 0) {
		qty = sumCookie(key,qty);
		if (invcount == 0) {
			alert("Your selection is out-of-stock, please make another choice");
		} 
		// We now do inventory check at checkout, since we can't do the javascript pop-up action
		// anymore.
		//else if (qty > invcount) {
	    //qty = setCookie(key,invcount);
		//alert("You exceeded the quantity available, we have reduced your quantity.");
		//}
		else {
			// alert(msg + "You now have " + qty + " of " + unescape(name) + " in your shopping basket.");
			doit = true;
		}
	} else {
		removeCookie(key);
		alert("Removed " + unescape(name) + " from shopping basket.");
	}  
	return doit;    
}

function toPrice(price) {
	var s = (price + ".0").toString().split(".");
	var t = "";
        s[1] = s[1].substring(0,2);
	if (s[1] < 10) {
        t = "0";
	}
	else {
        t = "";
	}

    return '$' + s[0] + "." + s[1] + t;
}

function calcTotal(qty, price) {
	var q = parseInt(qty);
	var p = toFloat(price);
	var total = 0;
	for (i = 0; i < q; i++) {
		total += p;
	}              
	return toPrice(total);
}

function toFloat(price) {
	return parseFloat(price.substr(1));    
}

function checkValue(ret, name, val) {
	var s = ret;
	if (!val) {
		alert("The value of " + name + " needs to be filled in.");
		s = "";
	}
	return s;
}

function condSetCookie(name, value) {
	if (getCookie('rememberme') != "") {
		setCookie(name, value);
	}
}

function condGetCookie(name) {
	var s = getCookie(name);
	return s ? s : "";
}

function emptyField(field) {
	return(!field || field.value == "") ? true : false;
}

function makeArray(n){
	this.length = n;
	for (var i = 1; i <= n; i++) {
		this[i] = 0;
	}
	return this;
}

function printDateNow() {
	var now = new Date();
	document.write (now.toLocaleString());
}
function stripSpace(strNumber) {
	var strNoSpace = "";
	var i;

	for (i = 0; i < strNumber.length; i++) {
		var c = strNumber.charAt (i);

		if (c == ' ')
			continue;

		strNoSpace += c;
	}

	return strNoSpace;
}

var luhntries = 0;
function verifyCard() {
	// is card already expired?
	var dateNow = new Date ();
	var nCurrentYear = dateNow.getFullYear()-2000;


    luhntries += 1;
    if (luhntries > 4) {
		alert ("For security reasons we have chosen to not accept any more credit card attempts.\nPlease contact customer service if you are continuing to have problems entering your credit card.");
		return false;
    }
	if (document.billing.cartyear.selectedIndex < nCurrentYear) {
		alert ("The expiry date you specified is in the past!\n\nPlease use another credit card or enter the proper expiry date.");
		document.billing.cartyear.focus();
		return false;
        }
        else if (document.billing.cartyear.selectedIndex == nCurrentYear) {
		var nSelectedMonth = document.billing.cartmonth.selectedIndex;
		var nCurrentMonth = dateNow.getMonth () + 1;

		if (nSelectedMonth < nCurrentMonth) {
			alert ("The expiry date you specified is in the past!\n\nPlease use another credit card or enter the proper expiry date.");
			document.billing.cartmonth.focus ();
			return false;
		}
	}

	// is the card number using valid characters (only digits plus space)?
	if (document.billing.cartcreditcardnumber.value.search ("^[0-9]([0-9]| )*[0-9]$")) {
		alert ("Use only digits to specify your credit card number!");
		document.billing.cartcreditcardnumber.focus ();
		return false;
	}

	var ccnumber = stripSpace (document.billing.cartcreditcardnumber.value);
	var sum = 0;
	var i;
	var digit;
	var lastDoubled = 0;

	for ( i = ( ccnumber.length - 1 ); i >= 0; i-- ) {
		digit = ccnumber.charAt( i );
		sum += parseInt( digit );
		lastDoubled++;

		if ( lastDoubled > 1 ) {
			lastDoubled = 0;
			sum += parseInt( digit );
			if ( digit > 4 )
				sum -= 9;
		}
	}

	var strSum = sum.toString();
	if ( strSum.charAt( strSum.length - 1 ) != "0" ||
		 ccnumber.length < 10 || ccnumber.length > 17 || sum == 0 ) {
		alert ("Invalid Credit Card Number.");
		document.billing.cartcreditcardnumber.focus();
		return false;
	}

	return true;
}

function checkHow() {
    if (document.billing.tagentstring.selectedIndex == 0) {
        alert('Please tell us how you found out about us!');
        document.billing.tagentstring.focus();
	    return false;
    }
	return true;
}

function checkSubmitCreditCard() {
	var required        = new makeArray(3);
	var requiredMessage = new makeArray(3);
	var i;

	required[0]="document.billing.cartcardholdername";
	required[1]="document.billing.cartcreditcardnumber";
	required[2]="document.billing.cartcreditcardtype";

	requiredMessage[0]='"Card Holder Name" is a required field.';
	requiredMessage[1]='"Credit Card Number" is a required field.';
	requiredMessage[2]='"Credit Card Type" is a required field.';

	if (!loopValid(required,requiredMessage)) {
	    return false;
    }

	if (document.billing.cartmonth.selectedIndex == 0) {
		alert('"Expiry Month" is a required field.');
		document.billing.cartmonth.focus();
		return false;
	}

	if (document.billing.cartyear.selectedIndex == 0) {
		alert('"Expiry Year" is a required field.');
		document.billing.cartyear.focus();
		return false;
	}

	return verifyCard();
}

function checkRegister(j)
{
	var required        = new makeArray(10);
	var requiredMessage = new makeArray(10);

	required[0]="document.billing.user";
	required[1]="document.billing.password";
	required[2]="document.billing.extrapassword";
	required[3]="document.billing.firstname";
	required[4]="document.billing.lastname";
	required[5]="document.billing.address";
	required[6]="document.billing.city";
	required[7]="document.billing.state";
	required[8]="document.billing.zip";
	required[9]="document.billing.email";

	requiredMessage[0]='"User Name" is a required field.';
	requiredMessage[1]='"Password" is a required field.';
	requiredMessage[2]='"Password Again" is a required field.';
	requiredMessage[3]='"First Name" is a required field.';
	requiredMessage[4]='"Last Name" is a required field.';
	requiredMessage[5]='"Address" is a required field.';
	requiredMessage[6]='"City/Town" is a required field.';
	requiredMessage[7]='"State/Province" is a required field.';
	requiredMessage[8]='"Zip" is a required field.';
	requiredMessage[9]='"Email" is a required field.';

	return loopValid(required,requirdMessage) && VerifyEmailAddress(document.billing);
}

function checkSubmitAddressPart1()
{
	var required        = new makeArray(8);
	var requiredMessage = new makeArray(8);

	required[0]="document.billing.cartfirstname";
	required[1]="document.billing.cartlastname";
	required[2]="document.billing.cartaddress";
	required[3]="document.billing.cartcity";
	required[4]="document.billing.cartstate";
	required[5]="document.billing.cartzip";
	required[6]="document.billing.cartphone";
	required[7]="document.billing.cartemail";

	requiredMessage[0]='"First Name" is a required field.';
	requiredMessage[1]='"Last Name" is a required field.';
	requiredMessage[2]='"Address" is a required field.';
	requiredMessage[3]='"City/Town" is a required field.';
	requiredMessage[4]='"State/Province" is a required field.';
	requiredMessage[5]='"Zip" is a required field.';
	requiredMessage[6]='"Phone" is a required field.';
	requiredMessage[7]='"Email" is a required field.';

	return loopValid(required,requiredMessage);
}

function checkSubmitAddress() {
	if (checkSubmitAddressPart1()) {
		var required        = new makeArray(6);
		var requiredMessage = new makeArray(6);

		required[0]="document.billing.cartsfirstname";
		required[1]="document.billing.cartslastname";
		required[2]="document.billing.cartsaddress";
		required[3]="document.billing.cartscity";
		required[4]="document.billing.cartsstate";
		required[5]="document.billing.cartszip";

		requiredMessage[0]='"Ship To First Name" is a required field.';
		requiredMessage[1]='"Ship To Last Name" is a required field.';
		requiredMessage[2]='"Ship To Address" is a required field.';
		requiredMessage[3]='"Ship To City/Town" is a required field.';
		requiredMessage[4]='"Ship To State/Province" is a required field.';
		requiredMessage[5]='"Ship To Zip" is a required field.';
		return loopValid(required,requiredMessage);
	} else {
		return false;
	}
}

function saveAddress() {
	setCookie('cartlastname', document.billing.cartlastname.value);
	setCookie('cartcompanyname', document.billing.cartcompanyname.value);
	setCookie('cartfirstname', document.billing.cartfirstname.value);
	setCookie('cartaddress', document.billing.cartaddress.value);
	setCookie('cartextraaddress', document.billing.cartextraaddress.value);
	setCookie('cartcity', document.billing.cartcity.value);
	setCookie('cartstate', document.billing.cartstate.value);
	setCookie('cartzip', document.billing.cartzip.value);
	//setCookie('cartcounty', document.billing.cartcounty.value);
	setCookie('cartcountry', document.billing.cartcountry.value);
	setCookie('cartphone', document.billing.cartphone.value);
	setCookie('cartfax', document.billing.cartfax.value);
	setCookie('cartemail', document.billing.cartemail.value);
}

function fillAddress() {
	
	if (getCookie('cartsasb') == 'true') {
		document.billing.cartsasb.checked = true;
		copyBilling();
	}
}

function copyBilling() {
	document.billing.cartslastname.value = document.billing.cartlastname.value;
	document.billing.cartscompanyname.value = document.billing.cartcompanyname.value;
	document.billing.cartsfirstname.value = document.billing.cartfirstname.value;
	document.billing.cartsaddress.value = document.billing.cartaddress.value;
	document.billing.cartsextraaddress.value = document.billing.cartextraaddress.value;
	document.billing.cartscity.value = document.billing.cartcity.value;
	document.billing.cartsstate.value = document.billing.cartstate.value;
	document.billing.cartszip.value = document.billing.cartzip.value;
	//document.billing.cartscounty.value = document.billing.cartcounty.value;
	document.billing.cartscountry.value = document.billing.cartcountry.value;
	document.billing.cartsphone.value = document.billing.cartphone.value;
	document.billing.cartsfax.value = document.billing.cartfax.value;
	document.billing.cartsemail.value = document.billing.cartemail.value;
}

function fillShipping(firstname,lastname,companyname,address,extraaddress,city,county,state,zip,country,phone,fax,email) {
	document.billing.cartsfirstname.value = firstname;
	document.billing.cartslastname.value = lastname;
	document.billing.cartscompanyname.value = companyname;
	document.billing.cartsaddress.value = address;
	document.billing.cartsextraaddress.value = extraaddress;
	document.billing.cartscity.value = city;
	//document.billing.cartscounty.value = county;
	document.billing.cartsstate.value = state;
	document.billing.cartszip.value = zip;
	document.billing.cartscountry.value = country;
	document.billing.cartsphone.value = phone;
	document.billing.cartsfax.value = fax;
	document.billing.cartsemail.value = email;
}

function fillBilling(firstname,lastname,companyname,address,extraaddress,city,county,state,zip,country,phone,fax,email) {
	document.billing.cartfirstname.value = firstname;
	document.billing.cartlastname.value = lastname;
	document.billing.cartcompanyname.value = companyname;
	document.billing.cartaddress.value = address;
	document.billing.cartextraaddress.value = extraaddress;
	document.billing.cartcity.value = city;
	//document.billing.cartcounty.value = county;
	document.billing.cartstate.value = state;
	document.billing.cartzip.value = zip;
	document.billing.cartcountry.value = country;
	document.billing.cartphone.value = phone;
	document.billing.cartfax.value = fax;
	document.billing.cartemail.value = email;
}

function getState(i) {
	var states = new makeArray(53);
	states[0] = '';
	states[1] = 'AL';
	states[2] = 'AK';
	states[3] = 'AZ';
	states[4] = 'AR';
	states[5] = 'CA';
	states[6] = 'CO';
	states[7] = 'CT';
	states[8] = 'DE';
	states[9] = 'DC';
	states[10] = 'FL';
	states[11] = 'GA';
	states[12] = 'HI';
	states[13] = 'ID';
	states[14] = 'IL';
	states[15] = 'IN';
	states[16] = 'IA';
	states[17] = 'KS';
	states[18] = 'KY';
	states[19] = 'LA';
	states[20] = 'ME';
	states[21] = 'MD';
	states[22] = 'MA';
	states[23] = 'MI';
	states[24] = 'MN';
	states[25] = 'MS';
	states[26] = 'MO';
	states[27] = 'MT';
	states[28] = 'NE';
	states[29] = 'NV';
	states[30] = 'NH';
	states[31] = 'NJ';
	states[32] = 'NM';
	states[33] = 'NY';
	states[34] = 'NC';
	states[35] = 'ND';
	states[36] = 'OH';
	states[37] = 'OK';
	states[38] = 'OR';
	states[39] = 'PA';
	states[40] = 'PR';
	states[41] = 'RI';
	states[42] = 'SC';
	states[43] = 'SD';
	states[44] = 'TN';
	states[45] = 'TX';
	states[46] = 'UT';
	states[47] = 'VT';
	states[48] = 'VA';
	states[49] = 'WA';
	states[50] = 'WV';
	states[51] = 'WI';
	states[52] = 'WY';
	return(states[i]);
}

function clearBilling() {
	document.billing.cartslastname.value = "";
	document.billing.cartscompanyname.value = "";
	document.billing.cartsfirstname.value = "";
	document.billing.cartsaddress.value = "";
	document.billing.cartsextraaddress.value = "";
	document.billing.cartscity.value = "";
	document.billing.cartsstate.value = "";
	document.billing.cartszip.value = "";
	document.billing.cartscounty.value = "";
	document.billing.cartscountry.value = "";
	document.billing.cartsphone.value = "";
	document.billing.cartsfax.value = "";
	document.billing.cartsemail.value = "";
}

function VerifyEmailAddress(EmailForm)
{
	var Reason  = "Incorrect Email Address! Please click OK and re-enter a valid email address.\n\nReason:"
	var checkStr = EmailForm.email.value;
	var ix = (checkStr.length - 4)
	var RC = true;
	var x = AtSignValid = DoublePeriod = PeriodValid = SpaceValid = ExtValid = RL = 0;

	for (i = 0;  i < checkStr.length;  i++) {
		if ( checkStr.charAt(i) == '@' ) AtSignValid++;
		else if ( checkStr.charAt(i) == '.' ) {
			if ( x > 1 && x == (i-1) ) DoublePeriod++;
			else {
				x = i;
				PeriodValid++;
			}
		} else if ( checkStr.charAt(i) == ' ' )
			SpaceValid ++;
	}
	ExtValid++;
	//if ( checkStr.indexOf(".com", ix) > -1 )	 ExtValid++;
	//else if ( checkStr.indexOf(".edu", ix) > -1 )	  ExtValid++;
	//else if ( checkStr.indexOf(".mil", ix) > -1 )	  ExtValid++;
	//else if ( checkStr.indexOf(".net", ix) > -1 )	  ExtValid++;
	//else if ( checkStr.indexOf(".org", ix) > -1 )	  ExtValid++;
	//else if ( checkStr.indexOf(".gov", ix) > -1 )	  ExtValid++;
	//else if ( checkStr.indexOf(".de", ix) > -1 )	 ExtValid++;
	//else if ( checkStr.indexOf(".uk", ix) > -1 )	 ExtValid++;

	RL = Reason.length;
	if ( AtSignValid != 1 )			 Reason += "\nOnly one '@' allowed, " + AtSignValid + " found.";
	if ( PeriodValid == 0 )			 Reason += "\nAddress must contain at least one period.";
	if ( SpaceValid > 0 )			 Reason += "\nNo Spaces allowed. Address contains " + SpaceValid + "space";
	if ( SpaceValid > 1 )			 Reason += "s.";
	if ( DoublePeriod > 0 )			 Reason += "\nAddress contains multiple periods in a row.";
	if ( ExtValid == 0 )			 Reason += "\nInvalid Domain Suffix entered. If you have an unverifiable suffix that is valid, email us your info at \n(Valid: .com, .edu, .net, .org, .gov, .de, .uk)";
	if ( checkStr.length > 120 )	 Reason += "\nPlease limit the Email Address to 120 characters.";

	if ( RL != Reason.length ) {
		confirm(Reason); EmailForm.email.focus(); RC = false;
	} else
		RC = true;
	return RC;
}

function fieldEmpty( inField, inName )
{
	if ( inField.value == "" ) {
		alert( "\nThe field \"" + inName + "\" must be filled in." );
		inField.focus();
		return true;
	}
	return false;
}

function do_submit(inForm)
{

	if ( ! VerifyEmailAddress(inForm) )
		return false;
	if ( fieldEmpty( inForm.FNAM, "First Name" ) )
		return false;
	if ( fieldEmpty( inForm.LNAM, "Last Name" ) )
		return false;
	if ( fieldEmpty( inForm.ZIP2, "Zip Code" ) )
		return false;
	return true;
}

function checkSubmitAll() {
     return checkSubmitAddress() && checkSubmitCreditCard();
}

function loopValid(required, requiredMessage)
{   
    var i = 0;
	for (i=0; i<required.length; i++) {
		if (emptyField(eval(required[i]))) {
			alert(requiredMessage[i]);
                        if (eval(required[i]).type != "hidden") {
                           eval(required[i]).focus();
                        }
			return false;
		}
	}
	return true;
}

function checkSubmitSingleItem()
{
	var required        = new makeArray(1);
	var requiredMessage = new makeArray(1);

	required[0]="document.billing.cartsinglepayment";
	requiredMessage[0]='"Payment Amount" is a required field.';

	return loopValid(required, requiredMessage);
}

function checkMember(password, extrapassword)
{
	var required        = new makeArray(3);
	var requiredMessage = new makeArray(3);

	required[0]="document.billing.user";
	required[1]="document.billing.password";
	required[2]="document.billing.extrapassword";
	requiredMessage[0]='"User Name" is a required field.';
	requiredMessage[1]='"Password" is a required field.';
	requiredMessage[2]='"Password Again" is a required field.';
	
	if (password.value != extrapassword.value) {
	    alert("Password and Password Again do not match, please try again.");
		eval(required[1]).focus();
		return false;
    }

	return loopValid(required, requiredMessage);
}

function checkSubmitFastPay(domember) {
    if (domember && !checkMember(document.billing.password,document.billing.extrapassword)) {
	    return false;
	}
	return checkSubmitSingleItem() && checkSubmitAll();
}

function checkDelete(s) {
    return confirm('Do you really want to delete this ' + s + '?\nPress Ok if you do, and Cancel otherwise.');
}
function removeCart (cartname) {
   var cart = getCookie('dccart');
   cart = eval("cart.replace(/" + cartname + "/g,\"\")");
   setCookie('dccart',cart);
}
