//********************************************************************
//*-------------------------------------------------------------------
//* Licensed Materials - Property of IBM
//*
//* WebSphere Commerce
//*
//* (c) Copyright IBM Corp. 2007
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
		
	/************************************************************
	 *	WORK PENDING											*
	 *	1. JavaDoc for Compare Function							*
	 *	2. Dependency of HTML forms and element					*
	 *	3. Moving Global variable and Functions to Global.js	*
	 *	4. Fixing few defect in Compare Functions				*
	 *		i.e:Should not accept same item in compareZone		*
	 *			Should allow to frag items from compareZone		* 
	 ************************************************************/


	dojo.require("dojo.widget.PopupContainer");
	dojo.require("dojo.widget.ContentPane");
	
	/******************************************************************************************************************
	 *                                        GLOBAL VARIABLES & FUNCTIONS                                            *
	 *                                                    START                                                       *
	 ******************************************************************************************************************/
QuickViewJS={
		waitMessage : "UNDEFINED",
		itemNo :"UNDEFINED",
		colorShown :"UNDEFINED",
		scene7 :"UNDEFINED",
		waitOnAdd: false,
		waitDisplayQV: false
		}
	       
function showQVContents(url, data) { 
	// get quick view pane by id
    var pane = dojo.widget.byId(this);

    data = pane.splitAndFixPaths(data, url);
    // show the contents recieved as response of XMLHttpRequest
	pane.setContent(data);
} 
 
function showQVWaitIcon(arguments) { 

	
		// hide the default loading... message                 
	    arguments.preventDefault();
	    // get quick view pane by id
	    var pane = dojo.widget.byId(this);
	    // wait icon, will be showed before the actual contents ready to load
	    var data = QuickViewJS.waitMessage;
		// show the wait icon
		pane.setContent(data);
	

}  

// update add to cart params, when quick view pop up hides
function onQVHideHandler(){ 
	/*
	try{
		var accessory_id = dojo.byId("whichaccessory"); 
		var accessory_catentid = dojo.byId("accessory_catentry");
		var accessory_qty = dojo.byId("accessory_qty");
		an_accessoryqty1 = eval("document.OrderItemAddForm.quantity_" + accessory_id.value);
			
		an_accessory1 = eval("document.OrderItemAddForm.catEntryId_" + accessory_id.value); 
		an_accessory1.checked = false;
		if(accessory_catentid.value != '' && accessory_catentid.value != '0'){
			an_accessory1.checked = true;
			an_accessory1.value = accessory_catentid.value;
			an_accessoryqty1.value = accessory_qty.value;
			
		}
	}catch(e){

	}
	*/ 
	// copied from dojo source
	var popup = dojo.widget.byId(this);
	dojo.widget.HtmlWidget.prototype.onHide.call(popup);
	//restore size of the domnode, see comment in
	//function onShow()
	/*if(popup.openedSize){             
		with(popup.domNode.style){
			width=popup.openedSize.w;        
			height=popup.openedSize.h;  
		}       
	}*/
	popup.processQueue();
	// end copy
}

function displayQV(quickviewId)
{
	//the quick view will fail unless the page is fully loaded
	if (QuickViewJS.waitDisplayQV)
	{
		var qvicon = document.getElementById(quickviewId);
		qvicon.style.visibility = "visible";
		qvicon.style.cursor = "pointer";
		qvicon.parentNode.display = "block";       
	}    
} 

function hideQV(quickviewId)
{
	document.getElementById(quickviewId).style.visibility = "hidden";                     
} 
function updateQVDisplay(prodId){ 
	
	

	var selectBox = dojo.byId("accessory_select"+prodId);

	user_input = selectBox.options[selectBox.selectedIndex].value
	
	var disp;
	var imgName;
	var color;
	var prodId;
	//alert("qv_"+user_input);
	var elem = dojo.byId("qv_"+user_input);
	//alert(elem);
	var dispStr ='';  
		
	if (elem == null)
		return;
	disp = elem.innerHTML;    
	splitter = disp.split(':');
	disp = splitter[0]; 
	imgName = splitter[1];
	color = splitter[2];
	esd = splitter[3];
	prodId = splitter[4];	
	price = splitter[5];				
			
		        
		
	// change the sku text
	var tmpId = 'qv_skuText_' + prodId;
    var skuText = dojo.byId(tmpId); 
	skuText.innerHTML = 'Item No.' + '  '+disp;
	
	 // change the text
	tmpId = 'qv_colorShown_' + prodId;
	
    var altViewText = dojo.byId(tmpId); 
	altViewText.innerHTML = 'Color Shown:' +'  '+ color;
	
	// change the esd text
	tmpId = 'qv_esd_' + prodId;
    var esdtext = dojo.byId(tmpId); 
	esdtext.innerHTML = 'Est. Ship Date:' +'  '+esd;
	
	var priceText = dojo.byId("productPrice"); 
   	if (priceText) 
		priceText.innerHTML = price;
		
	// change the main image
	tmpId = 'qv_mainImage_' + prodId;
	var mainimg = dojo.byId(tmpId);
	// change the button image back
    mainimg.setAttribute("src",'http://s7ondemand1.scene7.com/is/image/MoosejawMB/' + imgName);
    
    if (dojo.byId('bubble'))
 	{
 		dojo.html.setDisplay('bubble',false); 
 	}
 	
 	
}

function replaceQVImage(imageId, imageName, altTextId,altText)
{ 
	// change the image
	var mainimg = dojo.byId(imageId);
    mainimg.setAttribute("src",imageName);
    
    // change the text
	
    var altViewText = dojo.byId(altTextId);                                 
	altViewText.innerHTML = altText;       
}

function qv_AddToCart(prodId)
{ 
	if (!QuickViewJS.waitOnAdd)
	{
		QuickViewJS.waitOnAdd = true;
		var selectBox = eval("document.QVForm.accessory_select"+prodId);
		user_input = selectBox.options[selectBox.selectedIndex].value;
	    if (user_input == "0") {
	    	alert("Please select an item from the list to add it to your cart.");
	    	//Common.formErrorHandleClient("accessory_select"+prodId,Common.errorMessages["REQUIRED_FIELD_ENTER"],document.QVForm.name,'svg_product-pop'); 
	    	//Common.formStoreErrorFields("accessory_select"+prodId,Common.errorMessages["REQUIRED_FIELD_ENTER"]); 
			//bErrorFound = true;
			//Common.formErrorDisplay(document.QVForm.name,'svg_product-pop');
			QuickViewJS.waitOnAdd = false;
	    	return;
	    }
	   	document.QVForm.catEntryId.value=user_input;
	   		//if element for lastcmdname exists as a div, use it.
	   	if (dojo.byId('qv_lastCmdName'))
	   	{
	   		var cmdname = dojo.byId('qv_lastCmdName').innerHTML;
	   		cmdname = unescape(cmdname);
	   		while (cmdname.indexOf('&amp;') > -1)
				cmdname = cmdname.replace('&amp;','&');		
	   		document.QVForm.URL.value=cmdname+"&add=1";
	   	}
	   	
	   	//document.QVForm.action = "OrderItemAdd";
	   //	document.QVForm.submit();
	   	//add dojo js into header user mod..
		var params = [];
		params.storeId		= document.QVForm.storeId.value;
		params.catalogId	= document.QVForm.catalogId.value;
		params.orderId 		= document.QVForm.orderId.value;
		params.quantity		= document.QVForm.quantity.value;		
		params.orderItemId_old	= document.QVForm.orderItemId_old.value;
		params.orderItemId_new	= document.QVForm.orderItemId_new.value;
		params.catEntryId		= user_input;
		params.pageName			= document.QVForm.pageName.value;
		params.calculateOrder	= document.QVForm.calculateOrder.value;
		params.whichaccessory	= document.QVForm.whichaccessory.value;
		params.whichaccessory	= document.QVForm.whichaccessory.value;
		
		params.langId	= "-1";
		params.reLogonURL	= "AjaxActionErrorResponse";
		params.URL	= "AjaxActionResponse";
				
	
		wc.service.invoke("AjaxAddOrderItem",params);

		cursor_wait(); 
	   	
	}
    
}

function qv_UpdateCart(orderId, prodId, existCatEntryId)
{
	if (!QuickViewJS.waitOnAdd)
		{
			QuickViewJS.waitOnAdd = true;
			var selectBox = eval("document.QVForm.accessory_"+prodId);
			user_input = selectBox.options[selectBox.selectedIndex].value;
		    if (user_input == "0")
		    {
		    	Common.formStoreErrorFields("accessory_select"+prodId,Common.errorMessages["REQUIRED_FIELD_ENTER"]); 
				bErrorFound = true;
				Common.formErrorDisplay(document.QVForm.name,'svg_product-pop');
				QuickViewJS.waitOnAdd = false;
		    	return;
		    }
		    
		    document.QVForm.orderItemId_old.value=existCatEntryId;
		   	document.QVForm.orderItemId_new.value=user_input;
		   	//if element for lastcmdname exists as a div, use it.
		   	if (dojo.byId('qv_lastCmdName'))
		   	{
		   		var cmdname = dojo.byId('qv_lastCmdName').innerHTML;
		   		cmdname = unescape(cmdname);
		   		while (cmdname.indexOf('&amp;') > -1)
					cmdname = cmdname.replace('&amp;','&');		
		   		document.QVForm.URL.value=cmdname;
		   	}
		   	document.QVForm.action = "QuickUpdate";
		   	document.QVForm.submit();
	 	}
    
}

function qv_AddToWish(prodId)
{ 

if (!QuickViewJS.waitOnAdd)
	{
    
    	QuickViewJS.waitOnAdd = true;
		var selectBox = eval("document.QVForm.accessory_"+prodId);
		user_input = selectBox.options[selectBox.selectedIndex].value
	    if (user_input == "0")
	    {
	    	Common.formStoreErrorFields("accessory_select"+prodId,Common.errorMessages["REQUIRED_FIELD_ENTER"]); 
			bErrorFound = true;
			Common.formErrorDisplay(document.QVForm.name,'svg_product-pop');
			QuickViewJS.waitOnAdd = false;
	    	return;
	    }
	   
	   	document.QVForm.catEntryId.value=user_input;
	   		//if element for lastcmdname exists as a div, use it.
	   	if (dojo.byId('qv_lastCmdName'))
	   	{
	   		var cmdname = dojo.byId('qv_lastCmdName').innerHTML;
	   		cmdname = unescape(cmdname);
	   		while (cmdname.indexOf('&amp;') > -1)
				cmdname = cmdname.replace('&amp;','&');		
	   		document.QVForm.URL.value=cmdname;
	   	} 
	   	
	   	document.QVForm.action = "InterestItemAdd";
	   	document.QVForm.submit();
   	}
    
}


function selectAccessory(prodId,qvId)
{
	
	
	var selectBox = eval("document.QVForm.accessory_"+prodId);
	
	user_input = selectBox.options[selectBox.selectedIndex].value
    if (user_input == "0")
    {
    	Common.formStoreErrorFields("accessory_select"+prodId,Common.errorMessages["REQUIRED_FIELD_ENTER"]); 
		bErrorFound = true;
		Common.formErrorDisplay(document.QVForm.name,'svg_product-pop');
		
    	return;
    }
    
   	var selectedObj = dojo.byId("qvAccessoryString_"+user_input);
   	var displayObj = dojo.byId("qvColor_"+prodId);
   	var checkboxObj = dojo.byId("qvCheckbox_"+prodId);
   	
   	if (!checkboxObj.checked)
   	 dojo.byId("qvCheckbox_"+prodId).checked=true;
   	
   	checkboxObj.value = user_input;
   	
   	displayObj.innerHTML = selectedObj.innerHTML;
   	
   	
   	
   	hidePopup(qvId);
 
}


function showAccessoryPopUp(prodId, pURL, pContentId)
{
	var checkboxObj = dojo.byId("qvCheckbox_"+prodId);
	
	if (productPageLoaded)
	{
	   	if (!checkboxObj.checked)
	   	{ 
	   		
	   		
	   		dojo.byId("qvColor_"+prodId).innerHTML="";
	   		return true;
	   	}
	   	openPopup(pURL, pContentId);
   	} else
   	{
   		if (checkboxObj.checked)
	    	checkboxObj.checked = false;
	    else
	    	checkboxObj.checked = true; 
	    
	    return true;
   	}
   	
}

function closeAccessoryPopup(prodId,popUpId)
{
	var checkboxObj = dojo.byId("qvCheckbox_"+prodId);
	var selectStrHTML = dojo.byId("qvColor_"+prodId).innerHTML;
	//if checkbox is checked, but nothing was ever selected before...uncheck the box
   	if (checkboxObj.checked && selectStrHTML.length == 0)
   	{ 
   		dojo.byId("qvCheckbox_"+prodId).checked=false;
   		dojo.byId("qvColor_"+prodId).innerHTML="";
   		
   	}
   	
   	hidePopup(popUpId);
}

function goToProductPage(url)
{
   window.location = url;
}

function removeThisImage(img)
{
	//dojo.html.setDisplay(img,false);
	document.getElementById(img).style.display='none';
	return;
}

dojo.addOnLoad(function(){
	//enable the quick view icon when the page loads
	QuickViewJS.waitDisplayQV = true;
});