
    //%b123401230123012^foobar/jon^0901111111111?;123401230123012=0901111111111?

    /*First pass in the credit card string to the function
    Next the ‘%B’ amd ‘%b’ are removed from the string as we don’t need these
    Next I split the string into an array based on the carrot symbol ‘^’
    The first element, arr[0], is the actual credit card number
    The month is stored in the third element, arr[2], but I need to substring the element to get the month out
    The year is also stored in the same element as the month string and again I need to substring it out of the element
    Finally to get the first and last name I look in the second element, arr[1], and split on the slash ‘/’
    I store the split string as nameArr, with the first element nameArr[0] containing the last name and nameArr[1] containing the first name
	 */
	function DecodeMagStripe()
	{
 	  
	  //find the credit card nunmber, exp month, exp year, name
	  card_number_element = document.getElementById("card_number");
	  exp_month_element = document.getElementById("card_expire_Month");
	  exp_year_element = document.getElementById("card_expire_Year");
	  		
		mag_stripe_string = card_number_element.value;
		
		//we got track 1
		if (mag_stripe_string[0] == '%')
		{
			card_number = mag_stripe_string.replace('%B', '');
			card_number = mag_stripe_string.replace('%b', '');

			arr = mag_stripe_string.split('^');
			card_number = arr[0];
			
			month = '';
			year = '';
			if (arr.length > 2)//this should always be true
			{
				month = arr[2].substring(2, 4);
				year = arr[2].substring(0, 2);
				year = '20'+year;
			}

			first_name = '';
			last_name = '';

			if (arr.length > 1)//this should always be true
			{
				nameArr = arr[1].split('/');
				first_name = nameArr[1];
				last_name = nameArr[0];
			}
		}
		
		//we got track 2
		if (mag_stripe_string[0] == ';')
		{
			card_number = mag_stripe_string.replace(';', '');
			arr = card_number.split('=');
			card_number = arr[0];
			month = arr[1].substring(2, 4);
			year = arr[1].substring(0, 2);
			year = '20'+year;
		}
		
		//TODO - we need to do track three
		
		card_number_element.value = card_number;
		exp_month_element.value = month;
		exp_year_element.value = year;
		//alert("number: " + card_number_element.value + "   month: " + month + "   year: " + year + "  fname: " + first_name + "    lname: "+last_name);
   }

 
function toggle_block(block_name)
{
	element = document.getElementById(block_name);
	if (element == null)
	{
		alert("failed to find element " + block_name);
		return;
	}

	if (element.style.display == "none")
		element.style.display = "block";
	else
		element.style.display = "none";
}
function show_block(block_name)
{
	element = document.getElementById(block_name);
	if (element == null)
	{
		alert("failed to find element " + block_name);
		return;
	}

	element.style.display = "block";
}
function hide_block(block_name)
{
	element = document.getElementById(block_name);
	if (element == null)
	{
		alert("failed to find element " + block_name);
		return;
	}

	element.style.display = "none";
}

function checkRequiredArray(the_required_fields) {
var errors = new Array();
var x, type;
var cnt = 0;

	//alert("entering checkRequiredArray");
	for(x = 0; x < the_required_fields.length; x++) 
	{
	    var elementid = the_required_fields[x][0];
	    if(document.getElementById(elementid))
	    {
		//alert("looking at: " + document.getElementById(elementid).name + "  " + document.getElementById(elementid).value);
		if(document.getElementById(elementid).value == '' || document.getElementById(elementid).value == '..')
		    errors[cnt++] = new Array(the_required_fields[x][1],elementid);
	    }
	    else
		    alert("internal error - null element in checkRequiredArray at: " + x + "  " + elementid);
	}
	
	if(errors.length == 0)
		return true;

	if(errors.length == 1)
		alert_string = "The required field " + errors[0][0] + " is empty.";//+ errors[0][1]
	else
	{
		alert_string = "The following required fields are empty:  ";
		for(x = 0; x < errors.length; x++)
		    alert_string += errors[x][0] + ", ";
		alert_string = alert_string.substring(0, alert_string.length-2);
		alert_string += ".";
	}
	alert(alert_string);//+ errors[0][1]
	//alert("above focus " + errors[0][1]);
	element = document.getElementById(errors[0][1])
	if (element != null)
		element.focus();
	//alert("below focus");
	
	return false;
}

//TODO - move this to the 'add' case of cartfunctions.
function valid_limits(form_name, product_id, the_limit, cart_quantity)
{
    //if (the_limit == 0)
	//return true;
    
    //alert("checking limits: " + product_id + " " + the_limit + "  " + cart_quantity);
    //alert("form: " + form_name + " element name: " + "Product_Quantity:"+product_id);
    element = document.forms[form_name].elements["Product_Quantity:"+product_id];

    if (element == null)
    {
	    alert("element " + "Product_Quantity:"+product_id + " is not defined");
	    return true;
    }

    quantity=element.value;
    //alert("entering: '" + quantity + "'");

    if(isNaN(quantity))
    {
	    alert("Please enter a number: " + quantity);
	    element.value = 0;
	    return true;
    }

    //0 indicates there is no limit for this wine.
    total = parseInt(quantity) + parseInt(cart_quantity);
    if((the_limit > 0) && (total > the_limit))
    {
	    if (cart_quantity > 0)
	    {
		    //alert("quantity: "  + cart_quantity + " adding: " + value + " sum: " + total + " limit: " + the_limit);
		    alert("You have " + cart_quantity + " in your cart and there is a limit of " + the_limit + " bottles per order for this wine.");
		    element.value=the_limit - cart_quantity;
	    }
	    else
	    {
		    alert("Sorry, there is a limit of " + the_limit + " bottles per order for this wine.");
		    element.value=the_limit;
	    }
	    return true;
    }
    return true;
}
//
// Enviroment identificator
//
var localIsDOM = document.getElementById?true:false;
var localIsJava = navigator.javaEnabled();
var localIsStrict = document.compatMode=='CSS1Compat';
var localPlatform = navigator.platform;
var localVersion = "0";
var localBrowser = "";
if(window.opera && localIsDOM) {
	localBrowser = "Opera";
	if(navigator.userAgent.search(/^.*Opera.([\d.]+).*$/) != -1)
		localVersion = navigator.userAgent.replace(/^.*Opera.([\d.]+).*$/, "$1");
	else if(window.print)
		localVersion = "6";
	else
		localVersion = "5";
} else if(document.all && document.all.item)
	localBrowser = 'MSIE';
if(navigator.appName=="Netscape") {
	if(!localIsDOM) {
		localBrowser = 'Netscape';
		localVersion = navigator.userAgent.replace(/^.*Mozilla.([\d.]+).*$/, "$1");
		if(localVersion != '')
			localVersion = "4";
	} else if(navigator.userAgent.indexOf("Safari") >= 0)
		localBrowser = 'Safari';
	else if(navigator.userAgent.indexOf("Netscape") >= 0)
		localBrowser = 'Netscape';
	else if(navigator.userAgent.indexOf("Firefox") >= 0)
		localBrowser = 'Firefox';
	else 
		localBrowser = 'Mozilla';
	
}
if(navigator.userAgent.indexOf("MSMSGS") >= 0)
	localBrowser = "WMessenger";
else if(navigator.userAgent.indexOf("e2dk") >= 0)
	localBrowser = "Edonkey";
else if(navigator.userAgent.indexOf("Gnutella") + navigator.userAgent.indexOf("Gnucleus") >= 0)
	localBrowser = "Gnutella";
else if(navigator.userAgent.indexOf("KazaaClient") >= 0)
	localBrowser = "Kazaa";

if(localVersion == '0' && localBrowser != '') {
	var rg = new RegExp("^.*"+localBrowser+".([\\d.]+).*$");
	localVersion = navigator.userAgent.replace(rg, "$1");
}
var localIsCookie = ((localBrowser == 'Netscape' && localVersion == '4')?(document.cookie != ''):navigator.cookieEnabled);

//
// URL encode
//
function urlEncode(url) {
	return url.replace(/\s/g, "+").replace(/&/, "&amp;").replace(/"/, "&quot;")
}

//I don't think this is being used any more
function populateProfile() {
document.registerform.b_first_name.value = document.registerform.firstname.value;
document.registerform.b_last_name.value = document.registerform.lastname.value
}
