var acceptsCookies = false;
if(document.cookie == '') {
    document.cookie = 'acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('acceptsCookies=yes') != -1) acceptsCookies = true; 
}
else acceptsCookies = true;

function currencySelect(base,theForm) {
	var disclaimer = "BY CLICKING OK, YOU AGREE TO THE DISCLAIMER BELOW:\n\n";
	disclaimer += "This site use currency conversion from http://quote.yahoo.com/m3.  Exchange rates are updated approximately every 20 minutes.  As such, prices quoted in currencies other than "+base+ ", are approximate only.\n";
	if (confirm(disclaimer)) theForm.submit();
}

function addToCart(name,price,quantityField,item_id,fieldList) {
	if (quantityField.type=="select-one") quantity = quantityField[quantityField.selectedIndex].value;
	else quantity = quantityField.value;

	if (quantity<1) return;
	cookieName = "VS2CART";
	seperator = "|";
	endStr = "[;;]";
    if (confirm("Add "+quantity+" x "+name+" to your shopping?")) {
		if (!acceptsCookies) {
			alert("Sorry, you are not able to accept cookies, so a shopping cart could not be created.");
			return;
		}
		var newStr = "";
		currentStr=readCookie(cookieName);
		if (currentStr) newStr += currentStr;
		newStr += quantity+seperator+name+seperator+price+seperator+item_id+seperator+fieldList+endStr;
		
		var expdate = new Date ();
        expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 31));	//1 months time
		
		//re = /[\r\n]+/g;
		//newStr = newStr.replace("\n","<br>");
		setCookie(cookieName,newStr,expdate,"/");
		document.location = "/site/page=shopping";
	}
}

function makeFieldList(theForm) {
	seperator = "%^";
	listStr = seperator;
	excludeStr = "theName != 'quantity' && theName != 'price' && theName != 'buy'";
	numElements = theForm.elements.length;
	for (i=0;i < numElements;i++) {
		theName = theForm.elements[i].name
		if (eval(excludeStr)) {
			theType = theForm.elements[i].type;
			theValue = "";
			if (theType=="select-one") {
				theValue = eval(theForm.elements[i])[eval(theForm.elements[i]).selectedIndex].value;
				if (theValue=="") theValue = eval(theForm.elements[i])[eval(theForm.elements[i]).selectedIndex].text;
			}
			else if (theType=="checkbox"||theType=="radio") {
				if (theForm.elements[i].checked) theValue = theForm.elements[i].value;
			}
			else
				theValue = theForm.elements[i].value; //Treat as text field
			
			if (theValue!="") listStr += theName+"="+theValue+seperator;
		}
	}
	return listStr;
}

function clearCart() {
	cookieName = "VS2CART";
	killCookie(cookieName);
}

function removeItem(idStr) {
	if (!confirm("Are you sure you want to remove this item from your shopping?")) return;
	cookieName = "VS2CART";
	cookieStr = readCookie(cookieName);
	if (cookieStr=="") {
		alert("There are no items in your shopping to delete.");
		return;
	}
	startPos = cookieStr.indexOf(idStr);
	
	if (startPos==-1) {
		alert("This item is no longer in your shopping.");
		return;
	}
	endPos = startPos + idStr.length;
	pt1 = cookieStr.substr(0,startPos);
	pt2 = cookieStr.substr(endPos,cookieStr.length);
	newCookie = "" + pt1 + pt2;
	setCookie(cookieName,newCookie,1,"/");
	if (confirm("This item has been removed from your shopping, but this page must be reloaded/refreshed so you can view your updated shopping cart.\n\nClick Ok to reload/refresh the page.  Click Cancel if you want to reload/refresh the page later.")) {
		document.location = document.location;	//use rather than reload so quantity fields are not reposted
	}
}

function setCookie(name, value, hours, path, domain, secure) {
    if (acceptsCookies) { // Don't waste your time if the browser doesn't accept cookies.
	var not_NN2 = (navigator && navigator.appName 
		       && (navigator.appName == 'Netscape') 
		       && navigator.appVersion 
		       && (parseInt(navigator.appVersion) == 2))?false:true;

	if(hours && not_NN2) { // NN2 cannot handle Dates, so skip this part
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
	    }
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
    }
} // setCookie


function readCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
	return false; 
    } else { // there is a cookie
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
	var NN2Hack = firstChar + name.length;
	if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    firstChar += name.length + 1; // skip 'name' and '='
	    lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    if(lastChar == -1) lastChar = theBigCookie.length;
	    return unescape(theBigCookie.substring(firstChar, lastChar));
	} else { // If there was no cookie of that name, return false.
	    return false;
	}
    }	
} // readCookie

function killCookie(name, path, domain) {
  var theValue = readCookie(name); // We need the value to kill the cookie
  if(theValue) {
      document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} // killCookie

function invoiceCountryChange(country,theForm,baseCountry,taxName,taxPercentage,taxRestrict,taxStatus,total,currency,conversionRate) {
	if (theForm.Tax) {
		taxStr = "";
		if (taxRestrict=="Yes"||taxRestrict=="yes") { //tax only applies to baseCountry
			taxAmount = (Math.round(((total * taxPercentage / 100)*conversionRate)*100)/100)+" "+currency;

			taxStr += taxAmount;
		}
		else { //tax applies to all countries
			if (taxStatus=="Inclusive"||taxStatus=="inclusive") {
				taxStr = "Included in Total";
			}
			else if (taxStatus=="Exclusive"||taxStatus=="exclusive") {
				
			}
		}
		theForm.Tax.value = taxStr;
	}
}

function deliveryCountryChange(country,theForm,freightStr,currency,conversionRate) {
	if (theForm.Freight) {
		amount = "";
	
		conAr = freightStr.split(",");
		numCon = conAr.length
		for (i=0;i<numCon;i++) {
			elemAr = conAr[i].split(":");
			numElem = elemAr.length;
			if (amount==""&&(elemAr[0]=="DEFAULT"||elemAr[0]=="Default"||elemAr[0]=="default"))
				amount = elemAr[1];
			if (country==elemAr[0])	//if specific freight for this country
				amount = elemAr[1];
		}
		theForm.Freight.value = (amount*conversionRate)+" "+currency;
	}
}

