/*Variables from jsTools.js*/
var _dialogPromptID=null;
var _blackoutPromptID=null;
var validEsc = false;
/*---Variables for tracking---
tEvent(category-loctn,action-orgnl_TO_upgrd,label-orgnl);*/
var loctn = 'defLocation';
var orgnl = 'orgItemNr';
var upgrd = 'newItemNr';

function promptUpsell(innertxt,def,ok,cancel,title,random,upsellItemnumber,itemTitle,baseUnitsInStock,txtItemnumber,unitPrice,location,orgItemNumber,salesUnitOfMeasureRI, switchAndSave) {
		that=this;
		validEsc = true;
		loctn = location;
		orgnl = orgItemNumber;
		upgrd = upsellItemnumber;
        publishUpSellStartedGAEvent(loctn, orgnl, upgrd);

		
	   this.wrapupPrompt = function (canceled) {
	      // wrapupPrompt is called when the user enters or cancels the box.
	         val=document.getElementById('iepromptfield').value;
	         _dialogPromptID.style.display='none';
	         _blackoutPromptID.style.display='none';
			 validEsc = false;
	         document.getElementById('iepromptfield').value = '';
	         if (canceled) { val = 'canceled' }
	         promptCallbackUpSellOpportunity(val,innertxt,def,ok,cancel,title,random,upsellItemnumber,itemTitle,baseUnitsInStock,txtItemnumber,unitPrice,salesUnitOfMeasureRI,switchAndSave);
	      return false;
	   }

	   //if def wasn't actually passed, initialize it to null
	   if (def==undefined) { def=''; }
	   if( random == undefined) { random = '';}

	      if (_dialogPromptID==null) {
	         var tbody = document.getElementsByTagName("body")[0];
	         tnode = document.createElement('div');
	         tnode.id='IEPromptBox';
	         tbody.appendChild(tnode);
	         _dialogPromptID=document.getElementById('IEPromptBox');
	         tnode = document.createElement('div');
	         tnode.id='promptBlackout';
	         tbody.appendChild(tnode);
	         _blackoutPromptID=document.getElementById('promptBlackout');
	         _blackoutPromptID.style.opacity='.9';
	         _blackoutPromptID.style.position='absolute';
	         _blackoutPromptID.style.top='0px';
	         _blackoutPromptID.style.left='0px';
	         _blackoutPromptID.style.backgroundColor='#555555';
	         _blackoutPromptID.style.filter='alpha(opacity=90)';
	         _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
	         _blackoutPromptID.style.display='block';
	         _blackoutPromptID.style.zIndex='50';
	         _dialogPromptID.style.backgroundColor='#FFFFFF';
		 _dialogPromptID.style.position='fixed';
	         _dialogPromptID.style.width='430px';
	         _dialogPromptID.style.zIndex='100';
	      }
	      //Promt box
	   	  var tmp ='<div class="genericTopLeft"></div>'
		  tmp +='<div class="genericTopCenter" style="width:423px;">'
		  tmp +='<div class="black">'+title+'</div>'
		  tmp +='</div>'
		  tmp +='<div class="genericTopRight" ></div>'
	      tmp += '<div style="padding: 10px; border: 1px solid #999999;">'+innertxt;
	      tmp += '<div id="encapsulateContainer" style="margin-top:15px;">';
	      if(switchAndSave != '' &&switchAndSave != 'N') {
		tmp +='<div style="margin-left:5px; width:50px;height:50px; display:inline-block;vertical-align: top;" id="switchAndSave"><img src="Objectstore/images?file=switchandsave.png" /></div>';
		tmp += '<div style="margin-left:45px; display:inline-block;"> <div class="medium bold black" style="text-align:left;">'+itemTitle+'</div>';
	      } else {
		tmp += '<div style="margin-left:100px; display:inline-block;"> <div class="medium bold black" style="text-align:left;">'+itemTitle+'</div>';
	      }
	      tmp += '<div style="text-align:left">'+txtItemnumber+' '+upsellItemnumber+'</div>';
	      tmp += '<div style="text-align:left" class="orange bold">'+unitPrice+'</div>';
	      tmp += '<div style="margin-top 10px"> <img src="/web/Objectstore/content/image?itemnumber='+upsellItemnumber+'&imgsize=7&imgref=h1" /></div></center>';
	      tmp += '<form name="upSellForm" action="" onsubmit="return that.wrapupPrompt()">';
	      tmp += '<input id="iepromptfield" name="iepromptdata" type=hidden size=46 value="">';
	      tmp += '<br><br></div>';
	      tmp += '<div style="margin-left:100px">';
	      tmp += '<div class="inline">';
	      tmp += '<a class="chooseSuggestedUpsaleCancel_rollover" onclick="publishUpSellFailGAEvent(loctn, orgnl, upgrd);document.getElementById(\'iepromptfield\').value=\'canceled\';return that.wrapupPrompt();" href="#"></a>';
	      tmp += '</div>';
	      tmp += '<div style="margin-left:20px" class="inline">';
	      tmp += '<a class="chooseSuggestedUpsale_rollover" onclick="publishUpSellSuccessGAEvent(loctn, orgnl, upgrd);document.getElementById(\'iepromptfield\').value=\'go\';return that.wrapupPrompt();" href="#"></a>';
	      tmp += '</div>';
	      tmp += '</div>';
	      tmp += '</div>';
	      tmp += '</form></div>';

	      // make all other elements on the page unclickable.
	      _blackoutPromptID.style.height=(document.body.offsetHeight<screen.height) ? screen.height+'px' : document.body.offsetHeight+20+'px'; 
	      _blackoutPromptID.style.width='100%';
	      _blackoutPromptID.style.display='block';
	      _dialogPromptID.innerHTML=tmp;
	      _dialogPromptID.style.top='25%';
	      _dialogPromptID.style.left=parseInt((document.body.offsetWidth-315)/2)+'px';
	      _dialogPromptID.style.display='block';
		  
		//event tracking clicks outside the upsale window. onclick runs "canceled"
		document.getElementById('promptBlackout').onclick = function(clck){
			if(validEsc){
				document.getElementById('iepromptfield').value='canceled';return that.wrapupPrompt();
			}
		}
	}
	/**END IEPrompt **/

function promptCallbackUpSellOpportunity(val,innertxt,def,ok,cancel,title,random,upsellItemnumber,itemTitle,baseUnitsInStock,txtItemnumber,unitPrice,salesUnitOfMeasureRI, switchAndSave) {   
	 
	  if(val=='canceled') {
		if(random!='' && random != 'null') {
				document.forms['item'+random].action.value='shoppingcart';
				document.forms['item'+random].spec.value='addItem';
				document.forms['item'+random].submit();
				return false;
			} else {
			 setLocked();
			 document.itemdetail.action.value='shoppingcart';
			 document.itemdetail.spec.value='addItem';
			 document.itemdetail.submit();
			 return false;
			}
	  } else if(val!='' && val!=null) {
			if(random!='' && random != 'null') {
				document.forms['item'+random].itemNumber_1.value=upsellItemnumber;
				document.forms['item'+random].unitOfMeasure_1.value=salesUnitOfMeasureRI;
				document.forms['item'+random].action.value='shoppingcart';
				document.forms['item'+random].spec.value='addItem';
				document.forms['item'+random].submit();
				return false;
			} else {
			 setLocked();
			 document.itemdetail.itemNumber_1.value=upsellItemnumber;
			 // add fake option to select box
			 // to be able to pass upsell item default unit of measure
			 var fakeoption=document.createElement("option");
			 fakeoption.value=salesUnitOfMeasureRI;
			 fakeoption.text=salesUnitOfMeasureRI;
			 document.itemdetail.unitOfMeasure_1.appendChild(fakeoption);
			 document.itemdetail.unitOfMeasure_1.value=salesUnitOfMeasureRI;
			 document.itemdetail.action.value='shoppingcart';
			 document.itemdetail.spec.value='addItem';
			 document.itemdetail.submit();
			 return false;
			}
	  }
}
//keydown event for escape-button. on event; run 'canceled'
document.onkeydown = function(evt) {
	evt = evt || window.event;
	if (evt.keyCode == 27 && validEsc) {
        publishUpSellAbortedGAEvent(loctn, orgnl, upgrd);
		document.getElementById('iepromptfield').value='canceled';return that.wrapupPrompt();
	}
}
