

// -----------------------------------------------------------------------
//	Supporting function for the RDI set of functions
// -----------------------------------------------------------------------


// -----------------------------------------------------------------------
//	Get a parameter form the URL string.
// -----------------------------------------------------------------------
function getURLParams(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
	
  if( results == null )
    return "";
  else
    return results[1];
}

// -----------------------------------------------------------------------
//	Set the cookie name and value
// -----------------------------------------------------------------------
function rdi_setCookie(name, value) {
	var today = new Date();
	var expire = new Date();
	//expire.setTime(today.getTime() + 10);

//	document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString();
	document.cookie = name + "=" + escape(value);
}

// -----------------------------------------------------------------------
//	Retrieves the value of a cookie
// -----------------------------------------------------------------------
function rdi_getCookie(pName){
	var value = "";
	var pCOOKIES = new Array();
	pCOOKIES = document.cookie.split('; ');
	for(i = 0; i < pCOOKIES.length; i++){
		NmeVal  = new Array();
		NmeVal  = pCOOKIES[i].split('=');
		if(NmeVal[0] == pName){
			value = unescape(NmeVal[1]);
		}
	}
	return value;
}

// -----------------------------------------------------------------------
//	delete the cookie for the specified name
// -----------------------------------------------------------------------
function rdi_deleteCookie(name) {
	document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

// -----------------------------------------------------------------------
//	Developed by Robert Nyman, http://www.robertnyman.com
//	Code/licensing: http://code.google.com/p/getelementsbyclassname/
// -----------------------------------------------------------------------
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

// -----------------------------------------------------------------------
//	End of RDI Supporting function
// -----------------------------------------------------------------------


// -----------------------------------------------------------------------
//	Look to find all of the products where the quantity box is not empty.
//	When found a product to add check to make sure all of the options 
//	are filled in correctly.
// -----------------------------------------------------------------------
//	product structure
//	0 - array: first product to add
//		0 - productid
//		1 - variantid
//		2 - quantity
//		3 - price (team discounted + all options additional price
//		4 - logo - (Y / N)
//		5 - large_logo - (Y / N)
//		6 - embroidery - (text or blank)
//		7 - long_embroidery - (text or blank)
//		8 - embroidered_logo - (Y / N)
//		9 - large_embroidered_logo - (Y / N)
//		
//	1 - array:  second product to add
//	2 - array:  n product to add
// ----------------------------------------------------------------------
function getAllProducts() {

	var products = getElementsByClassName('prodcontainer');

	var prods2cart = new Array();
	var item = 0;
	var firstTime = true;
	var data = '';
	var submitForm = true;		// controls if we submit the form or not.

	for (i = 0; i < products.length; i++) {
		//
		// get the product id
		//
		var prod = products[i].id.split("_");
		var productid = prod[1];
		//
		// get the quantity value.
		//
		var quantity = document.getElementById('qty' + productid).value;

		if (document.getElementById('variant' + productid)) {
			var varid = document.getElementById('variant' + productid).value;
		}
		else
			var varid = -1;

		//
		//  get and verify the data for the product that has been selected.
		//
		if (quantity != -1 || varid > -1) {

			document.getElementById('infoqty' + productid).style.display='none';
			if (quantity == -1) {
				submitForm = false;
				document.getElementById('infoqty' + productid).style.display='inline';
			}

			if(firstTime == true)
				firstTime = false;
			else
				data = data + '||';

			//
			//	get the variant id
			//
			if (document.getElementById('variant' + productid)) {
				document.getElementById('infoattrb' + productid).style.display='none';
				var variantid = document.getElementById('variant' + productid).value;
				if (variantid == -1) {
					// turn on message for this variant to indicate needed data
					document.getElementById('infoattrb' + productid).style.display='inline';
					submitForm = false;
				}
			}
			else
				var variantid = -1;			// no variant available

			//
			// get the price of the product and the additional options
			//
			if (document.getElementById('teamprice' + productid)) {
				var price = document.getElementById('teamprice' + productid).innerHTML;
				price = parseFloat(price);

				if (document.getElementById('additionalprice' + productid)) {
					var addPrice = document.getElementById('additionalprice' + productid).innerHTML;
					if (addPrice != '') {
						addPrice = parseFloat(addPrice);
					}
					else {
						var addPrice = 0.00;
					}
				}

				var totalPrice = price + addPrice;

			}

			//
			// get the data for small logo
			//
			if(document.getElementById('logosmall' + productid))
				if(document.getElementById('logosmall' + productid).checked)
					var logo = 1;
				else
					var logo = 0;
			else
				var logo = 0;

			//
			// get the data for the large logo
			//
			if (document.getElementById('logolrg' + productid))
				if (document.getElementById('logolrg' + productid).checked)
					var large_logo = 1;
				else
					var large_logo = 0;
			else
				var large_logo = 0;

			//
			// get the name to use
			//
			var embroidery = '';
			var long_embroidery = '';
			if (document.getElementById('selembname' + productid)) {
				document.getElementById('infoname' + productid).style.display='none';
				var optName = document.getElementById('selembname' + productid).value

				if(optName != -1) {
					var txtName = document.getElementById('embname' + productid).value;

					if(txtName.length == 0) {
						// set the dispaly message box
						document.getElementById('infoname' + productid).style.display='inline';
						submitForm = false;
					}
					if(optName == 'first') embroidery = txtName;
					if(optName == 'firstlast') long_embroidery = txtName;
				}
			}

			//
			// get the data for embroidered logo
			//
			if (document.getElementById('emblogo' + productid))
				if (document.getElementById('emblogo' + productid).checked)
					var embroidered_logo = 1;
				else
					var embroidered_logo = 0;
			else
				var embroidered_logo = 0;

			//
			// get the data for long embroidered logo
			//
			if (document.getElementById('emblogobig' + productid))
				if (document.getElementById('emblogobig' + productid).checked)
					var large_embroidered_logo = 1;
				else
					var large_embroidered_logo = 0;
			else
				var large_embroidered_logo = 0;

			// Get the product id (the productid id a combo of catid-prodid)
			var temp = productid.split("-");
			var prodID = temp[1];		// product id


// For debugging purposes
//			prods2cart[item] = new Array(
//					prodID
//					, variantid
//					, quantity
//					, logo
//					, large_logo
//					, embroidery
//					, long_embroidery
//					, embroidered_logo
//					, large_embroidered_logo
//			);

			data = data +
			'productid:' + prodID + '~' +
			'variantid:' + variantid + '~' +
			'quantity:' + quantity + '~' +
			'price:' + totalPrice + '~' +
			'logo:' + logo + '~' +
			'large_logo:' + large_logo + '~' +
			'embroidery:' + embroidery + '~' +
			'long_embroidery:' + long_embroidery + '~' +
			'embroidered_logo:' + embroidered_logo + '~' +
			'large_embroidered_logo:' + large_embroidered_logo;

			item++;
		}

	}


//	var tmpString =
//		tmpString +
//		'Product id : ' + prods2cart[0][0] + '\n' +
//		'variantid : ' + prods2cart[0][1] + '\n' +
//		'quantity : ' + prods2cart[0][2] + '\n' +
//		'price:' + totalPrice + '\n' +
//		'logo : ' + prods2cart[0][3] + '\n' +
//		'large_logo : ' + prods2cart[0][4] + '\n' +
//		'embroidery : ' + prods2cart[0][5] + '\n' +
//		'long_embroidery : ' + prods2cart[0][6] + '\n' +
//		'embroidered_logo : ' + prods2cart[0][7] + '\n' +
//		'large_embroidered_logo : ' + prods2cart[0][8] + '\n';
//
//	alert('add products to cart (' + prods2cart.length + ')\n'
//		+ '\n'
//		+ tmpString
//	);

	if (submitForm == true && firstTime == false) {
		document.getElementById('pass_prods').value = data;
		document.team_products.submit();
}
	
}

// -----------------------------------------------------------------------
//	Builds a list of team ID's to be added to the customers table.
// -----------------------------------------------------------------------
function buildTeamList() {

	var listBox = document.getElementById('ListBox2');
	var len = listBox.length;
	var firstTime = true;
	var teamList = '';
	for(var i = 0; i < len; i++) {
		if(firstTime == true) {
			firstTime = false;
			teamList = listBox[i].value;
		}
		else
			teamList = teamList + ', ' + listBox[i].value;
	}

	 document.getElementById('teams_info').value = teamList;

//	alert(teamList);
}


function swatchSwap (optionId) {
	var imgId = document.getElementById('swat' + optionId);
	var prodThumb = document.getElementById("product_thumbnail");

	prodThumb.src = imgId.src
};


// -----------------------------------------------------------------------
//	Changes the color and the images for the color
// -----------------------------------------------------------------------
function changeIt( selectId, selectOpt, selectCl ) {
	var prodThumb = document.getElementById("product_thumbnail");
	var testor = document.getElementById('po' + selectId);

	if (selectCl > -1) {
		var swatch_div = document.getElementById('swatch' + selectCl);
		//var varThumb = document.getElementById('smcolor' + selectOpt);
		//prodThumb.src = varThumb.src;
	}
	else {

		var option = document.getElementById('po' + selectId).value;
		var swatch_div = document.getElementById('swatch' + option);

		for(var i = 0; i < document.getElementById('po' + selectId).length; i++) {
			if(document.getElementById('po' + selectId)[i].value == option) {
				var imageIndex = i;
				break;
			}
		}

		//var varThumb = document.getElementById('smcolor' + imageIndex);
		//prodThumb.src = varThumb.src;
	}

	if (selectOpt > -1)
		testor.selectedIndex = selectOpt;


	if (document.getElementById('swatches').hasChildNodes()) {
		var children = document.getElementById('swatches').childNodes;

		// Loop through the childern
		for (var c = 0; c < children.length; c++) {
			if (children[c].style && children[c].nodeName == 'DIV') {
				children[c].style.display = 'none';
			}
		}
	}

	if (selectCl > -1) {
		if(document.getElementById('swatch' + selectCl))
			swatch_div.style.display="block";
	}
	else {
		var option = document.getElementById('po' + selectId).value;
		if(document.getElementById('swatch' + option))
			swatch_div.style.display="block";

	}

  //document.getElementById('ddinput').value = 'select a size';

	//if(document.getElementById("ddinput")) {
	//	if(document.getElementById('addtocart')) {
	//		document.getElementById('addtocart').disabled = true;
	//		document.getElementById('a2c_img').src = images_dir + '/addtocartoff.jpg';
		//}
	//}

	check_options();
};

// -----------------------------------------------------------------------
//	Update the pricing by adding the option prices when selected.
// -----------------------------------------------------------------------
function updatePrices(cat_prod_id) {

	var addAmount = 0.00;

	if (document.getElementById('logosmall' + cat_prod_id)) {
		if (document.getElementById('logosmall' + cat_prod_id).checked == true)
			addAmount = addAmount + teamOptions[cat_prod_id][0];
	}

	if (document.getElementById('logolrg' + cat_prod_id)) {
		if (document.getElementById('logolrg' + cat_prod_id).checked == true)
			addAmount = addAmount + teamOptions[cat_prod_id][1];
	}

	if (document.getElementById('selembname' + cat_prod_id)){
		if (document.getElementById('selembname' + cat_prod_id).value == 'first')
			addAmount = addAmount + teamOptions[cat_prod_id][2];

		if (document.getElementById('selembname' + cat_prod_id).value == 'firstlast')
			addAmount = addAmount + teamOptions[cat_prod_id][3];

	}

	if (document.getElementById('emblogo' + cat_prod_id)) {
		if (document.getElementById('emblogo' + cat_prod_id).checked == true)
			addAmount = addAmount + teamOptions[cat_prod_id][4];
	}

	if (document.getElementById('emblogobig' + cat_prod_id)) {
		if (document.getElementById('emblogobig' + cat_prod_id).checked == true)
			addAmount = addAmount + teamOptions[cat_prod_id][5];
	}


	if (document.getElementById('additionalprice' + cat_prod_id)) {
		if (addAmount > 0.00) {
			document.getElementById('price_adder' + cat_prod_id).style.display = 'inline';
			document.getElementById('additionalprice' + cat_prod_id).innerHTML = addAmount.toFixed(2);
		}
		else {
			document.getElementById('price_adder' + cat_prod_id).style.display = 'none';
			document.getElementById('additionalprice' + cat_prod_id).innerHTML = '';
		}
	}

//	alert('ProductID : ' + productid + '\n' +
//		'myOptions item count : ' + myOptions[0].length + '\n' +
//		'Team options count : ' + teamOptions[productid].length + '\n' +
//		'Item valu of 1 : ' + teamOptions[productid][0]+ '\n' +
//		'Item valu of 2 : ' + teamOptions[productid][1]+ '\n' +
//		'Item valu of 3 : ' + teamOptions[productid][2]+ '\n' +
//		'Item valu of 4 : ' + teamOptions[productid][3]+ '\n' +
//		'Item valu of 5 : ' + teamOptions[productid][4]+ '\n' +
//		'Item valu of 6 : ' + teamOptions[productid][5]+ '\n'
//	);

}
