/* 
*********************************************************************
**  File: productInfo.js
**	Original Author: 
**	Date: 
**	Purpose: javascript functions for quickOrderPopup  
**  
**
*********************************************************************
**	Revision History:
**	Who		Date		Description
**  BPW		6/10/09		moved js code from dfs_productInfo.cfc to this file
**	MH:	7/14/09 added total price function and made duplicate functions for dynamic variables.  these had to be duplicates because the easy buy needed dynamic names
**		and the quick order did not use dynamic variable names.
*********************************************************************
*/
var FormIsReady = false,
	g_productid = 0,
	g_attrib1 = 0,
	g_attrib2 = 0,
	g_productvariantid = 0,
	g_shiptypeid = 1,
	g_eachPrice=0;

var _StringFormatInline = function()
{
	var txt = this;
	for(var i=0;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i) + '\\}','gm');
		txt = txt.replace(exp,arguments[i]);
	}
	return txt;
}

var _StringFormatStatic = function()
{
	for(var i=1;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
		arguments[0] = arguments[0].replace(exp,arguments[i]);
	}
	return arguments[0];
}

if(!String.prototype.format)
{
	String.prototype.format = _StringFormatInline;
}

if(!String.format)
{
	String.format = _StringFormatStatic;
}

/*
 * Original:  Cyanide_7 (leo7278 at hotmail.com)
 Web Site:  http: www7.ewebcity.com/cyanide7
*/ 
var dollarFormat = function(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}




var cleanup = function() {
	ColdFusion.Window.destroy('qoPreview',true);
}

var logit = function(msg) {
	//console.log(msg);
	return;
	/*
	var p = new prodClass();
	p.logAjax('[bpwdebug]'+msg);
	*/
}


/*  coldfusion.window.create  version */  
var qoPopup = function(link) {
	var windowName = 'qoPreview',
	title = '',
	config = new Object();
	if (gup(link,'sqob') == '1')
	{
		config.height = 620; // make window bigger if showing user buy section
		config.width = 520;
		config.modal = true;
		config.bodystyle="background-color:#BBBBBB";
		config.draggable=false;
		config.callbackHandler=initTabs;
	}
	else
	{
		config.height = 485;
		config.width = 540;
		config.modal = false;
		config.callbackHandler=initTabs_v2;
	}
	ColdFusion.Window.create(windowName,title,link,config);
	ColdFusion.Window.onHide(windowName,cleanup);
}

function whyPopup(text)
{
	var title = '';
	var config = new Object();
	config.height = 175; // make window bigger if showing user buy section
	config.width = 400;
	config.center = true;
	config.draggable=true;
	ColdFusion.Window.create('WhyPopup',title,'',config);
	var win = ColdFusion.Window.getWindowObject("WhyPopup");
	win.body.dom.innerHTML= '<br/>'+text;
}
function whyClose()
{
	ColdFusion.Window.destroy('WhyPopup');
}
var initTabs = function() {
	var height = resizeWindow();
	selectArticle('0',height); // tab,size

	// testing omniture call
	var catname = document.getElementById('categoryname').innerHTML; // get the category name
	s.pageName="QuickOrderPopup:"+catname;
	s.prop5="QuickOrderPopup";
	s.prop4=catname;
	
	void(s.t()); // call to omniture


}
/* MH: 7/14/09 added v2 function to handle easy buy popup without the height variables that are not defined in that section of the quick order popup */
var initTabs_v2 = function() {
	var height = document.getElementById('categoryImage').height + 85;
	//var height = resizeWindow();
	selectArticle('0',height); // tab,size
}

var resizeWindow = function() {
	var height = document.getElementById('boxheight').value;
	var winheight = document.getElementById('qoPreviewContent').offsetHeight+60;
	ColdFusion.Window.getWindowObject('qoPreview').resizeTo(520,winheight);
	return height;	
}

var qoPopupAddCartConfirm = function(qopvid) {
	
	var windowName = 'qoConfirm';
	var title = 'Thank you';
	var config = new Object();
	var link='/product/quickOrderConfirm.cfm?pvid='+qopvid;
	config.height = 300;
	config.width = 520;
	config.modal = true;
	config.draggable=false;
	config.bodystyle="background-color:#FFFFFF";
	ColdFusion.Window.create(windowName,title,link,config);
	//Get window object
	var myWindow = ColdFusion.Window.getWindowObject(windowName);
	 
	//use the center function to center the window.
	myWindow.center();
}

var closeConfirmPopup = function() {
	ColdFusion.Window.destroy('qoConfirm',true);
}

var trackCartAdd = function() {
	// 8/17/09 - add productsku - bpw
	var sku = document.getElementById('productsku').value;
	s.pageName="QuickOrderPopup:AddToCart:"+sku;
	
	void(s.t()); // call to omniture
	
}

// special handling if monogramming is selected - theForm.submit() takes client to cart_process -> monogram_1 include
var addToCart = function(theForm){
	trackCartAdd(); // omniture tracking
	if (theForm.monogram.checked) {
		theForm.submit();
	}
	else {
		// update cart instead of reloading page // ColdFusion.Ajax.submitForm('ffoForm','cart_process.cfm',reloadPage);
		ColdFusion.Ajax.submitForm('ffoForm','/product/cart_process.cfm',updateCartDisplay);
		ColdFusion.Window.hide('qoPreview');
	}
}	

// if this is a monogrammable item, addToCartAndCheckout will just addToCart so we can get the personalization info
var addToCartAndCheckout = function(theForm){
	if (theForm.monogram.checked) {
		addToCart(theForm);
	}
	else {
		trackCartAdd();
		ColdFusion.Ajax.submitForm('ffoForm','/product/cart_process.cfm',cartProcessCallback);
		ColdFusion.Window.hide('qoPreview'); 
		setTimeout("goToCheckout()",2000); // set a delay so cart_process has time to work...
	}
}	

// returns the value of cookie with name passed in
var get_cookie = function(cookie_name) {
	var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
	if ( results )
		return ( unescape ( results[2] ) );
	else
		return null;
}

// get cart quantity and subtotal
var updateCartDisplay = function() {
	
	var rc,sc = new shoppingCartClass(),
		dfsCookie = get_cookie('FSAID');

	if (dfsCookie) {
		sc.setCallbackHandler(setCartDisplay);// when the function returns it will pass the results to this function
		rc=sc.GetShoppingCartInfo(dfsCookie);
	}
}

// takes the query results passed in and display them on the window
var setCartDisplay = function(results) {
	var count,countlabel='items';
	if (results.DATA && results.DATA.length > 0 && document.getElementById('cartItemCount') && document.getElementById('cartSubtotal')) {
		
		count = results.DATA[0][0];
		if (parseInt(count) == 1) countlabel='item'; 
		document.getElementById('cartItemCount').innerHTML = ' ' + count + ' ';
		document.getElementById('cartItemCountLabel').innerHTML = ' ' + countlabel + ' ';
		
		if (parseInt(count) > 0) {
			document.getElementById('cartSubtotal').innerHTML = ' ' + dollarFormat(results.DATA[0][1]) + ' ';
			document.getElementById('cartSubtotalLabel').innerHTML = ' Subtotal: ';
		}
	}
	// show the confirmation window 
	qoPopupAddCartConfirm(g_productvariantid);

}



/* bpw 8/11/09  get rid of reload page - update cart display instead
 
var reloadPage = function() {
	//console.log('reloadPage:1');//bpwdebug
	var p = new prodClass();
	p.setSessionVar('quickOrderPVID',g_productvariantid);
	
	var url = window.location.href;
	window.location.href = url;
}
*/

var cartProcessCallback = function (response) {
	goToCheckout();
}

var goToCheckout = function() {
	window.location.href = '/checkout/modify_cart.cfm';
}

var popupWin2 = function(source, left, top, width, height, options) 
{
	var left = left + 40
	var windowproperties = options + ",left=" + left + ",top=" + top + ",width=" + width + ",height=" + height
	popup = window.open(source,"PopupAddinfo",windowproperties)
	popup.focus()
}

var popupWin3 = function(source, left, top, width, height, options) 
{
	popupWin2(source, left, top, width, height, options);
}

var goToProductPage = function (c,pcatid) {

	var pSep = '?';
	var uParm = '';
	if (c != 0) {
		uParm = uParm + pSep + 'c=' + c;
		pSep = '&';
	}
	if (pcatid != 0) {
		uParm = uParm + pSep + 'pcatid=' + pcatid;
		pSep = '&';
	}
	url = '/product/prod_display.cfm' + uParm;
	window.location.href = url;
}


/* 
 * gup - get url param
 * code found at http://www.netlobo.com/url_query_string_javascript.html
 * 
 *  */
var gup = function(link,name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( link );
  if( results == null )
    return "";
  else
    return results[1];
}

var getElementsByClass = function(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

var showElementsByClass = function(searchClass,node,tag,vis) {

	var el = getElementsByClass(searchClass,node,tag);
	for (i=0; i<el.length; i++) {
		if (vis)
			el[i].style.display='block';
		else
			el[i].style.display='none';
	}
}

var giveFocus = function(id,size) {
	if(document.getElementById('mainContainer').style.height==size){
		var y = document.body.scrollTop;
		var x = document.body.scrollLeft;
		if (y == 0){
			if (window.pageYOffset){y = window.pageYOffset; x = window.pageXOffset;}
			else{y = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0; x = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;}
		}
		document.getElementById(id).scrollIntoView(true);
		window.scroll(x,y);
	}
	else{document.getElementById(id).scrollIntoView(true);}
}

var selectArticle = function(i,size) {
	wtm_clear(size);
	document.getElementById('content_inc_'+i).style.display='block'; document.getElementById('content_inc_'+i).style.visibility='visible';
	document.getElementById('tab_inc_'+i).style.display='none'; document.getElementById('tab_inc_'+i).style.visibility='hidden';
	document.getElementById('no_tab_inc_'+i).style.display='block'; document.getElementById('no_tab_inc_'+i).style.visibility='visible';
	document.getElementById('mainContainer').style.height=size; document.getElementById('mainContainer').style.overflow='auto';
}

var selectCompactView = function(i,size) {
	wtm_clear(size);
	document.getElementById('content_inc_'+i).style.display='block'; document.getElementById('content_inc_'+i).style.visibility='visible';
	
	document.getElementById('tab_inc_'+i).style.display='none'; document.getElementById('tab_inc_'+i).style.visibility='hidden';
	document.getElementById('no_tab_inc_'+i).style.display='block'; document.getElementById('no_tab_inc_'+i).style.visibility='visible';
	document.getElementById('mainContainer').style.height=size; document.getElementById('mainContainer').style.overflow='auto';
}

var wtm_clear = function(size){
	document.getElementById('mainContainer').style.height=size; 
	document.getElementById('mainContainer').style.overflow='auto';
	for(var i=0; i<7; i++){
		if(document.getElementById('content_inc_'+i)){
			document.getElementById('content_inc_'+i).style.display='none'; 
			document.getElementById('content_inc_'+i).style.visibility='hidden';
			}
		if(document.getElementById('no_tab_inc_'+i)){
			document.getElementById('no_tab_inc_'+i).style.display='none'; 
			document.getElementById('no_tab_inc_'+i).style.visibility='hidden';
			}
		if(document.getElementById('tab_inc_'+i)){
			document.getElementById('tab_inc_'+i).style.display='block'; 
			document.getElementById('tab_inc_'+i).style.visibility='visible';
			}
		}
}
/* MH: 7/14/09  the total price function handles calculating the price for each line item and then the total price as each line item is calculated. */
function totalPrice(num,count,use)
{
	var totalPrice = 0;
	if(use == 1)
	{
		for (i=1;i<=count;i++)
		{
			var currentPrice = 'ProductPrice_'+i;
			var removeBox = 'removeBox_'+i;
			var attrib1 = 'Attrib1_'+i;
			var attrib2 = 'Attrib2_'+i;
			var use_remove_box = eval('document.ffoForm.'+removeBox+'.checked');
			if(document.getElementById(attrib1) && document.getElementById(attrib1).value != -1 && document.getElementById(attrib2) && document.getElementById(attrib2).value != -1)
			{
				 if(document.getElementById(currentPrice) && use_remove_box == true)
				 {
					var product = document.getElementById(currentPrice).value;
					var currentProduct = 'ProductID_'+i;
					var	productPrice = product.toString().replace(/\$|\,/g,'');
					if(!(isNaN(parseFloat(productPrice)))){}else{productPrice = 0;}
					var totalPrice = parseFloat(productPrice) + parseFloat(totalPrice);
				 }
			 }
		}
	}
	var product_price = num.toString().replace(/\$|\,/g,'');
	if(use == 1)
	{
		var use_innerHtml = '$'+totalPrice.toFixed(2);
		document.getElementById('subTotalPrice_1').innerHTML = use_innerHtml;
		document.getElementById('subTotalPrice_2').innerHTML = use_innerHtml;
	}
	return '$'+product_price;
}
var toggleVisByID = function(ID) {
	var el = document.getElementById(ID);
	if (el.style.display == "block")
		ShowDivByID(ID,0);
	else
		ShowDivByID(ID,1);
		
}

var ShowDivByID = function(ID,vis,active){
	var saDiv = document.getElementById(ID);
	if (saDiv) {
		if(vis==1)
			saDiv.style.display="block"
		else
			saDiv.style.display="none"
	}
	if(active == 1){resizeWindow();}
}

var ShowElementByID = function(ID,vis,display){
	var d_style='none'; // defaults to no display
	var saDiv = document.getElementById(ID);
	if (saDiv) {
		if(vis==1) {
			if (display==0) d_style='block';
			if (display==1) d_style='inline';
		}
		saDiv.style.display=d_style;
	}
}

var getProducts = function(id) 
{
	var p = new prodClass(),
		prodArray = p.getProductsV1(id);
	
	if (prodArray.length > 1) {
		prodArray.unshift([0,'SELECT PRODUCT']); // adds an element to the beginning of the array
	}
	return prodArray; 
}
/* MH: 7/14/09 this v2 version handles dynamic variable names it also enables and disables the remove box and checks it based on item availability*/	
var getProductVariantID_v2 = function(attrib2,i) {
	var ProductID = 'ProductID_'+i;
	var Attribute1 = 'Attrib1_'+i;
	var MaxProductQty = 'MaxProductQty_'+i;
	var Quantity = 'Quantity_'+i;
	var productnum = 'productnum_'+i;
	var removeBox = 'removeBox_'+i;
	if(document.getElementById(ProductID)){var productid = document.getElementById(ProductID).value;}
	if (productid==0) return 0;
	if(document.getElementById(Attribute1)){var	attrib1 = document.getElementById(Attribute1).value;}
	var p,vInfo,pvid=0,maxQty,expdate,backordered,oktobo,notooe,
		oktopurchase=false,statusMsg="",productnum;

// if called with same parameters as last call, just return the same productvariantid
	if (productid==g_productid && attrib1==g_attrib1 && attrib2==g_attrib2)  {
		return g_productvariantid;
	}
	else {
		if (attrib1=='')
			attrib1=0;
		if (attrib2=='')
			attrib2=0;
		p = new prodClass();
		vInfo = p.GetProductVariantID(productid,attrib1,attrib2);
		// whenever we successfully retrieve the productvariantid, also get the max allowable qty for this product
		// bpw 5/26/09 - this call combined into GetProductVariantID // maxQty = p.getMaxProdQty(result);
		if (vInfo.DATA && vInfo.DATA.length > 0) {
			
			pvid = vInfo.DATA[0][0];
			maxQty = vInfo.DATA[0][1];
			expdate = vInfo.DATA[0][2];
			backordered = vInfo.DATA[0][3];
			oktobo = vInfo.DATA[0][4];
			notooe = vInfo.DATA[0][5];

			monogrammable = vInfo.DATA[0][6]; // added 6/15/09 bpw
			productnum = vInfo.DATA[0][7]; // added 6/18/09 bpw
			productnum = productnum.replace(/[^0-9]/,''); // strips out non-digits - bpw - 6/24/09
			
			document.getElementById(MaxProductQty).value = maxQty ;
			if (document.getElementById(Quantity).value > maxQty) {
				document.getElementById(Quantity).value = maxQty;
			}
			if(document.getElementById(productnum)){document.getElementById(productnum).value = productnum;} // 6/18/09 bpw
			var itemDiv = 'itemNumber_'+i;
			document.getElementById(itemDiv).innerHTML = productnum;
			document.getElementById(itemDiv).style.visibility = "visible";
			document.getElementById(itemDiv).style.display = "block";
			g_productid = productid;
			g_attrib1 = attrib1;
			g_attrib2 = attrib2;
			g_productvariantid = pvid;
			/*alert(productnum+' '+attrib1+' '+g_productid+' '+attrib2);*/
			// figure out backorder and oktopurchase status
			if (notooe) {
				statusMsg="Item is not available for purchase at this time."
			}
			else {
				// notooe == 'N'
				if (backordered){
					statusMsg = "Item is on backorder";
					if (oktobo) { 
						statusMsg += ".";
						oktopurchase = true;
					}
					else {
						statusMsg += "; not available for order at this time.";
					}
					statusMsg += " Expected in on " + expdate;
				}
				else {
					// not backordered
					oktopurchase=true;
				}
			}
			
			if (!oktopurchase) {
				statusMsg = "Please make another selection.<br>" + statusMsg;
			}
			var BackOrdered = 'BackOrdered_'+i;
			document.getElementById(BackOrdered).innerHTML = statusMsg;
			if(oktopurchase == true)
			{
				var use_remove_box = eval('document.ffoForm.'+removeBox+'.checked = true;');
				//var enable_remove_box = eval('document.ffoForm.'+removeBox+'.disabled = false;');
			}
			else
			{
				var use_remove_box = eval('document.ffoForm.'+removeBox+'.checked = false;');
				//var disable_remove_box = eval('document.ffoForm.'+removeBox+'.disabled = true;');
			}
			ShowDivByID(BackOrdered,statusMsg.length > 0,0);
	
			var qtyprice = 'qtyprice_'+i;
			var personalization = 'personalization_'+i;
			// hide or show qty and price block
			ShowDivByID(qtyprice,oktopurchase,0);
			
			// display monogrammable if needed
			ShowDivByID(personalization,oktopurchase && monogrammable,0);
		}
		return pvid;
	}

}
//	5/14/09 - bpw - changed this function to be called only with attrib2 - the other 2 params will be read directly from the
//		document - this will avoid unnecessary calling of this function (by bound fields)
//		function getProductVariantID(productid,attrib1,attrib2) {
//	5/26/09	bpw	changed the way this js function and prodFuncs.GetProductVariantID work together. ProdFuncs.GetProductVariatnID now returns more 
//			information (productvariantid, maxquantity,expdate,backordered,oktobo,notooe)
var getProductVariantID = function(attrib2) {
	var productid = document.getElementById('ProductID').value;
	
	if (productid==0) return 0;
	var	attrib1 = document.getElementById('Attribute1').value,
		p,vInfo,pvid=0,maxQty,expdate,backordered,oktobo,notooe,
		oktopurchase=false,statusMsg="",productnum;
	

		if (attrib1=='')
			attrib1=5;
		if (attrib2=='')
			attrib2=5;
		p = new prodClass();
		vInfo = p.GetProductVariantID(productid,attrib1,attrib2);
		// whenever we successfully retrieve the productvariantid, also get the max allowable qty for this product
		// bpw 5/26/09 - this call combined into GetProductVariantID // maxQty = p.getMaxProdQty(result);
		// bpw 8/17/09 - add retrieval of productsku
		if (vInfo.DATA && vInfo.DATA.length > 0) {
			
			pvid = vInfo.DATA[0][0];
			maxQty = vInfo.DATA[0][1];
			expdate = vInfo.DATA[0][2];
			backordered = vInfo.DATA[0][3];
			oktobo = vInfo.DATA[0][4];
			notooe = vInfo.DATA[0][5];
			monogrammable = vInfo.DATA[0][6]; // added 6/15/09 bpw
			productnum = vInfo.DATA[0][7]; // added 6/18/09 bpw
			productnum = productnum.replace(/[^0-9]/,''); // strips out non-digits - bpw - 6/24/09
			price = vInfo.DATA[0][8]; // added 7/1/09 bpw
			productsku = vInfo.DATA[0][9]; // added 8/17/09 bpw
			
			document.getElementById('MaxProductQty').value = maxQty ;
			if (document.getElementById('quantity').value > maxQty) {
				document.getElementById('quantity').value = maxQty;
			}
			document.getElementById('productnum').value = productnum; // 6/18/09 bpw
			document.getElementById('productsku').value = productsku; // 8/17/09 bpw
			g_productid = productid;
			g_attrib1 = attrib1;
			g_attrib2 = attrib2;
			g_productvariantid = pvid;
			g_eachPrice = price;
			
			// figure out backorder and oktopurchase status
			if (notooe) {
				statusMsg="Item is not available for purchase at this time."
			}
			else {
				// notooe == 'N'
				if (backordered){
					statusMsg = "Item is on backorder";
					if (oktobo) { 
						statusMsg += ".";
						oktopurchase = true;
					}
					else {
						statusMsg += "; not available for order at this time.";
					}
					statusMsg += " Expected in on " + expdate;
				}
				else {
					// not backordered
					oktopurchase=true;
				}
			}
			
			if (!oktopurchase) {
				statusMsg = "Please make another selection.<br>" + statusMsg;
			}
			document.getElementById('BackOrdered').innerHTML = statusMsg;
			ShowDivByID('BackOrdered',statusMsg.length > 0,1);
	
			// hide or show qty and price block
			ShowDivByID('qtyprice',(oktopurchase && pvid != 0),1);
			
			// display monogrammable if needed
			//ShowDivByID('personalization',oktopurchase && monogrammable);
			ShowElementByID('personalization',oktopurchase && monogrammable,1); // display inline if needed
		}
		return pvid;
}

var validateProductID = function(id) {
	var p = new prodClass(),
		result = p.validateProductID(id);
	getVariants1(result);
	getVariants2(result);
	return result; 
}
/*  MH: 7/14/09 this handles the dynamic version of the getvariants1 function */
var getVariants1_v2 = function(id,i,type) {
	if(type == 1)
	{
		var itemDiv = 'itemNumber_'+i;
		document.getElementById(itemDiv).style.visibility = "hidden";
		document.getElementById(itemDiv).style.display = "none";
	}
	var p = new prodClass(),
		result = p.getAttributes(id,1),
		attrib1count = result.length;
	var attrib1div = 'attrib1div_'+i;
	if ((attrib1count > 0) && (result[0][0] != 0)) {
		document.getElementById(attrib1div).style.display = 'block';
		document.getElementById(attrib1div).style.visibility = 'visible';
		}
	else {
		document.getElementById('attrib1div_'+i).style.display = 'none';
		document.getElementById('attrib1div_'+i).style.visibility = 'hidden';
		}
	return result; 
}

var getVariants1 = function(id) {
	var p = new prodClass(),
		result = p.getAttributes(id,1),
		attrib1count = result.length;

	ShowDivByID('attrib1div',(attrib1count > 0) && (result[0][0] != 0))
	resizeWindow(); // bpw - make sure window stays big enough
/*
	if ((attrib1count > 0) && (result[0][0] != 0)) {
		document.getElementById('attrib1div').style.display = 'block';
		document.getElementById('attrib1div').style.visibility = 'visible';
		// document.getElementById('attrib1name').innerHTML = 'Select ' + p.getAttributeName(id,1);
		}
	else {
		document.getElementById('attrib1div').style.display = 'none';
		document.getElementById('attrib1div').style.visibility = 'hidden';
		}
logit('getVariants1:id={0}, result={1}'.format(id,result)); // bpwdebug
*/
	return result; 
}

var getVariants2 = function(id) 
{
	var productid=document.getElementById('ProductID').value;
	var p = new prodClass(),
		result = p.getAttributes(productid,2),
		attrib2count = result.length;
		
	ShowDivByID('attrib2div',(attrib2count > 0) && (result[0][0] != 0))
	resizeWindow(); // bpw - make sure window stays big enough
	return result;
}

/*  MH: 7/14/09 this is the dynamic version of the getvariants2 function */
var getVariants2_v2 = function(id,i) 
{
	var ProductID = 'ProductID_'+i;
	var productid=document.getElementById(ProductID).value;
	var p = new prodClass(),
		result = p.getAttributes(productid,2),

		attrib2count = result.length;
	if ((attrib2count > 0) && (result[0][0] != 0)) 
	{
		var attrib2div = 'attrib2div_'+i;
		document.getElementById(attrib2div).style.display = 'block';
		document.getElementById(attrib2div).style.visibility = 'visible';
		// document.getElementById('attrib2name').innerHTML = 'Select ' + p.getAttributeName(id,2);
	}
	else 
	{
		var attrib2div = 'attrib2div_'+i;
		document.getElementById(attrib2div).style.display = 'none';
		document.getElementById(attrib2div).style.visibility = 'hidden';
	}
	return result; 
}
/*  MH: 7/14/09 this is the dynamic version of the getpvprice function */
var getPVPrice_v2 = function(id,qty,i,count,prefix,productid,catalogcodeid,pcatid) {
	var p,result=0;
	//alert(id+' '+qty+' '+i+' '+count+' '+prefix+' '+productid+' '+catalogcodeid+' '+pcatid);
	if((id != null) && (id != 0) && id.length>0 && id != 'undefined')
	{
		if (qty.length > 0) 
		{
			if (isNaN(qty)) 
			{
				alert("Product Quantity must be numeric!");
				var Quantity = 'Quantity_'+i;
				document.getElementById(Quantity).value = 1;
				qty=1;
			}
			else {
				// make sure qty is an int value
				qty = parseInt(qty);
				if (isNaN(qty) || qty<0)
					qty=1;
					var Quantity = 'Quantity_'+i;
				document.getElementById(Quantity).value=qty;
			}
			if(qty != 0)
			{
				p = new prodClass();
				// check qty
				var MaxProductQty = 'MaxProductQty_'+i;
				if (parseInt(document.getElementById(Quantity).value) > parseInt(document.getElementById(MaxProductQty).value) ) 
				{
					alert('Product Quantity exceeds allowable quantity');
					qty = parseInt(document.getElementById('MaxProductQty_'+i).value);
					document.getElementById(Quantity).value = qty;
				}
				result = p.GetPVPrice_v2(id,qty,'prefix',productid,catalogcodeid);
			}
		}
	}
	return result;
}

var getPVPrice = function(id,qty) 
{
	var p,result=0;
	if((id != null) && (id != 0) && id.length>0 && id != 'undefined')
	{
		if (qty.length > 0) 
		{
			if (isNaN(qty)) 
			{
				alert("Product Quantity must be numeric!");
				document.getElementById('quantity').value = 1;
				qty=1;
			}
			else 
			{
				// make sure qty is an int value
				qty = parseInt(qty);
				if (isNaN(qty) || qty<1)
					qty=1;
				document.getElementById('quantity').value=qty;
			}
			// check qty
			if (parseInt(document.getElementById('quantity').value) > parseInt(document.getElementById('MaxProductQty').value) ) 
			{
				alert('Product Quantity exceeds allowable quantity');
				qty = parseInt(document.getElementById('MaxProductQty').value);
				document.getElementById('quantity').value = qty;
			}
//			result = g_eachPrice * qty;
			p = new prodClass();
			result = p.GetPVPrice(id,qty);

		}
	}
	return result;
}

var trim = function(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

var IsNumeric = function(sText) 
{
	var ValidChars = "0123456789.",i;
	for (i = 0; i < sText.length; i++) 
	{
		if (ValidChars.indexOf(sText.charAt(i)) == -1) 
		{
			return false;
		}
	}
	return true;
}

var isValidObject = function(testObj) 
{
	if (null == testObj) 
	{
		return false;
	}
	if ("undefined" == typeof(testObj) ) 
	{
		return false;
	}
	return true;
}
/*  MH: 7/14/09 this handles preselecting the products, and attributes for the easy buy bundles. */
function preselect(productid,count,selectedProductId,value)
{//alert(productid+' '+count+' '+selectedProductId+' '+value);
	var use_value = value+'_'+count;
	for(x=0;x<document.getElementById(use_value).length;x++)
	{
		if(document.getElementById(use_value).selectedIndex == 0)
		{
			if(document.getElementById(use_value).options[x].value == selectedProductId)
			{
				document.getElementById(use_value).options[x].selected = true;
				break;
			}
		}
	}
}