//STOP-N-POP for MULTIPAGE
//V1.2
//02.12.2008 - create, markm
//03.17.2008 - added "category" promo funcationaly & offer/cat flag, markm

//KEEP THIS AT THE BOTTOM OF YOU PAGE (ABOVE /BODY)

//*********************************************************************************************************
//SETTINGS
//*********************************************************************************************************
//LightboxSNP images
var pch_filePromoImg = strAkamai + "stop-pop-spare-key-hider.jpg"

var snp_externalButtons = false;
var pch_lb_snp_buttons = [];
pch_lb_snp_buttons.push(1);
pch_lb_snp_buttons.push(2);

var snp_type = 1; //KEY: 1=offer, 2=category, 3=stop-n-sell
var snp_subType = 0; //for SUB seatting of each major type (currently not used)

var snp_summaryOverride = false; //KEY: true = SNP page will have submit. False = user will have to continue through all reamining pages
var snp_keyCartCount = 0; //STOP N POP will trigger with a cart item count less than or equal to this value;
//OPS...
var snp_backendVersion= "20084086" //YYYYWWJJ

//IF snp_type = 1 OR 3 = offer THEN set snp_offerCode...
var snp_offerCode = "F5A6";
var snp_hidden=false;
//STOP N SELL (snp_type = 3) ONLY
var snp_offerCartText = "[Stop N Sell Cart Text]"
var snp_offerPCHPrice = "19.99"
var snp_offerQTRPrice = "4.99"

//END SETTINGS
//*********************************************************************************************************
//DO NOT EDIT BELOW
//*********************************************************************************************************
var snp_seen = false //Global Seen Stop-N-Pop flag
var _gsnp = false // global stop'n'pop active flag
function stopnpop(){
	_gsnp=true;
	//TEST FOR MULTIPAGE (ERROR HANDLING)
	if (!$("mpWrap")) alert("PCH ERROR: This device is for MULTI-PAGE only.");	
	if(iCount > snp_keyCartCount) return true; //If they ordered more than the MAX for this SNP do no show SNP
	if(snp_seen) return true; //Only show SNP once

	//create hidden form field
	if(snp_hidden){
		fObjCreator("input",$("mpHidden"),"type=hidden~name=OrderItem_"+snp_offerCode+"~id=OrderItem_"+snp_offerCode);

	//Add it to the array
		arrProds.push({prodtitle: snp_offerCartText});
					
		//offers
		arrOffers.push([ { 
			offerCode: snp_offerCode,
			pchPrice:snp_offerPCHPrice,
			qtrPrice: snp_offerQTRPrice,
			cartText: snp_offerCartText
		}]);

		//images
		arrImages.push([]);
	}

	snp_seen = true;
	initLightboxSNP();
	myLightboxSNP.start();
	return false;
}

function stopnpop_findit(){
	var tmpProd=-1;
	for(var i=0;i<arrOffers.length;i++){
		for(var x=0;x<arrOffers[i].length;x++){
			if(arrOffers[i][x].offerCode.toString() == snp_offerCode){
				tmpProd = i;
				break; //leave inner loop
			}
		}
		if(tmpProd >=0) break; //leave outter loop
	}

	if(tmpProd<0){
		//ERROR HANDLING
		alert("PCH ERROR: Product Not found.");	
		return false;
	}
	
	return tmpProd;
}

function stopnpop_continue(inThis){	

	var snpButtonID = parseInt(inThis.id.replace("pchButton_",""));
	

//inBoolLookAgain
	//stop the lightbox
	myLightboxSNP.end();
	if(snpButtonID != pch_lb_snp_buttons.length){
		
		

		//SET BACKEND VERSION AFTER "TAKE ANOTHER LOOK"		
		if(snp_backendVersion=="YYYYWWJJ") alert("PCH ERROR:\nBackend version needs to be set");
		document.forms[0].tcIdA.value = snp_backendVersion;	

		//SHOW SUBMIT ON PAGE?
		gbSubmitOverride = snp_summaryOverride

		var iOfferPosition;
		switch (snp_type){
			case 1:// LOOK AGAIN, PRODUCT
				//FIND IT...
				iOfferPosition = stopnpop_findit();
				//find my page (same code as "fCartGo") but this needs to happen first
				var iPageMax=0;
				var myPage;
				for(var ip=0;ip<gProdPerPageDisp.length;ip++){
					iPageMax += gProdPerPageDisp[ip];
					if(iOfferPosition<iPageMax){
						myPage = ip+1;
						break;
					}
				}
				
				//THEN GO TO IT...
				fCartGo(iOfferPosition)
				break;

			case 2: // LOOK AGAIN, CATEGORY
				fQuickPage(snpButtonID)
				break;

			case 3: //STOP-N-SELL
				//FIND IT...
				iOfferPosition = stopnpop_findit();
				//BUY IT (ADD TO CART)
				fCart(iOfferPosition,0,false);
				break;				
			default:
				alert("PCH ERROR: \nInvalid snp_type ("+ snp_type+")");
				break;
		}
		
	}else{
		//No thanks, submit page
				_globalSubmit=true;
		document.frmOrder.submit();
		//newCheck();
	}
}
