﻿//---------------------------------------------
// General.js 
// Copyright (C) Jetshop AB 2009
//---------------------------------------------
function Products_GetProductData(controlID, productID)
{
	// Create new AddToCartItem
	var item = new JetShop.StoreControls.Services.AddToCartItem();
	
	// Quantity
	item.Quantity = Products_GetQuantity(controlID + "_tbQuantity")
	// Attributes
	item.AttributeData = Products_GetAttributeData(controlID + "_dpa_ddlItems1", controlID + "_dpa_ddlItems2");
	// ProductID
	item.ProductID = productID;
	// Specification items
	item.SpecificationIdList = Products_GetSpecificationData(controlID + "_dss");
	// CommentItems
	item.Comments = Products_GetComments(controlID + "_dcl");
	
	return item;
}

function Products_GetQuantity(strQuantity)
{
	var objQuantity = $get(strQuantity);
	if (objQuantity != "undefined" && objQuantity != null)
	{
		return objQuantity.value;
	}
	else
	{
		return 1;
	}
}

function Products_GetID(strID)
{
	var objID = $get(strID);
	if (objID != "undefined" && objID != null)
	{
		return objID.value;
	}
	else
	{
		return -1;
	}
}

function Products_GetAttributeData(strItem1, strItem2)
{
	var result = new Array();
	
	result[0] = "";
	result[1] = "";
	result[2] = "";
	result[3] = "";
	
	var objItem1 = $get(strItem1);
	var objItem2 = $get(strItem2);	
	
	if (objItem2 != 'undefined' && objItem2 != null)
	{
		result[1] = objItem2.options[objItem2.selectedIndex].text;
		result[3] = objItem2.options[objItem2.selectedIndex].value;
	}
	if (objItem1 != 'undefined' && objItem1 != null)
	{
		result[0] = objItem1.options[objItem1.selectedIndex].text;
		result[2] = objItem1.options[objItem1.selectedIndex].value;
	}
	
	return result;
}

function Products_GetSpecificationData(containerID)
{	
	var sValues, sTmpData, currentDropDown;
	var container = $get(containerID);

	sValues = '';
	if (container != "undefined" && container != null)
	{
		var aItems = container.getElementsByTagName('select');
		
		for (var idx = 0; idx < aItems.length; idx++)
		{
			currentDropDown = aItems[idx];
			sTmpData = currentDropDown.options[currentDropDown.selectedIndex].value;
			
			sValues += sTmpData.split('|')[0];
			sValues += "|";
		}
	}
	else
	{
		sValues = "";
	}
	
	return sValues;
}

function Products_GetComments(containerID)
{
	var sTitle, commentItem, aComments;
	var container = $get(containerID);
	
	aComments = new Array();
	
	if (container != "undefined" && container != null)
	{
		var aItems = container.getElementsByTagName('*');
		
		for (var idx = 0; idx < aItems.length; idx++)
		{
			if (aItems[idx].id.lastIndexOf('tbComment') > -1)
			{
				sTitle = aItems[idx].id.replace('tbComment', 'lblCommentTitle');
				
				commentItem = new Array();
				
				commentItem[0] = $get(sTitle).innerHTML;
				commentItem[1] = aItems[idx].value;
				
				aComments.push(commentItem);
			}
		}
	}

	return aComments;
}

function Products_AttributeValueData(result, context, methodName)
{
    var ddlist2 = $get(context + '_dpa_ddlItems2');
    
    var objDpa = $get(context + '_dpa');

    if (ddlist2 != "undefined" && null != ddlist2)
    {
        ddlist2.disabled = false;
        ddlist2.options.length = 0;

        var optionIndex = 0;
        
        if (objDpa.useDropDownChooseText == "true")
        {    
             ddlist2.options[0] = new Option(objDpa.dropDownChooseText, '-1');
             optionIndex = 1;
        }

        for (var idx = 0; idx < result.length-1; idx++)
	    {
	        ddlist2.options[optionIndex + idx] = new Option(result[idx][0], result[idx][1]);
	    }
	    
	    ddlist2.selectedIndex = 0;
	    
	    Products_RefreshData(result[result.length -1], context, '');
    }
}

function Products_RefreshData(result, context, methodName)
{
	//AN
	var spnAN = $get(context + "_spnAN");
	if (spnAN != "undefined" && spnAN != null)
	{
	    if(result != "undefined" && result != null)
	    {
		    spnAN.innerHTML = result[0];
		}
		else
		{
		    spnAN.innerHTML = '';
		}
	}
	
	// Price
	var spnPrice = $get(context + "_spnPrice");
	var lblPrice = $get(context + "_lblPriceStandard");
	if (spnPrice != "undefined" && spnPrice != null)
	{
	    if(result != "undefined" && result != null)
	    {
	        if(result[0].length > 0)
	        {
		        spnPrice.innerHTML = result[1];
		    }
		    else
		    {
		        spnPrice.innerHTML = '';
		    }

            // Product has discount, so change css-class
		    if (result[2].length == 0) {
		        if (result[7] == 'true') {
		            if (lblPrice != "undefined" && lblPrice != null){
		                lblPrice.className = "PriceStandard";
		            }
		        }
		        else {
		            spnPrice.className = "priceStandard";
		        }
		    }
		    else
		    {
		        if (result[7] == 'true')
		        {
		            if (lblPrice != "undefined" && lblPrice != null){
		                lblPrice.className = "PricePromotion";
		            }
		        }
		        else {
		            spnPrice.className = "priceExtra";
		        }               
		    }
		}
		else
		{
		    spnPrice.innerHTML = '';
		}
	}
	
	// PrevPrice
	var spnPrevPrice = $get(context + "_spnPrevPrice");
	var spnPrevPriceText = $get(context + "_spnPrevPriceText"); 
	if (spnPrevPrice != "undefined" && spnPrevPrice != null)
	{
		if(result != "undefined" && result != null)
	    {
	        if(result[0].length > 0)
	        {
	            if (spnPrevPriceText != "undefined" && spnPrevPriceText != null)
	            {
	                if (result[2].length > 0)
	                {
	                    spnPrevPriceText.style.visibility = "visible";
	                }
	                else
	                {
	                    spnPrevPriceText.style.visibility = "hidden";
	                }
	            }
		        spnPrevPrice.innerHTML = result[2];
		    }
		    else
		    {
		        spnPrevPrice.innerHTML = '';
		        if (spnPrevPriceText != "undefined" && spnPrevPriceText != null)
            	{
		            spnPrevPriceText.style.visibility = "hidden";
		        }
		    }
		}
		else
		{
		    spnPrevPrice.innerHTML = '';
		    if (spnPrevPriceText != "undefined" && spnPrevPriceText != null)
	        {
		        spnPrevPriceText.style.visibility = "hidden";
		    }
		}
	}
		
	// Images
	if(result != "undefined" && result != null)
	{
		// Empty image
		if (result[3].length > 0)
		{
			var hlImage = $get(context + "_imgProduct");
			
			if (hlImage != "undefined" && hlImage != null)
			{
				ID_SetImage($get(context + "_imgProduct"), $get(context + "_hlImageProduct"), result[3]);
			}
			else
			{
				ID_ChangeImage(context + "_idProduct_imgID", context + "_idProduct_hlID", result[3], result[4], context + "_idProduct_hdnID");
			}
		}
	}
	
	//StockStatus
	var lblStockStatus = $get(context + "_lblStockStatus");
	var divOrder = $get(context + "_divOrder");
	var atccAddToCart = $get(context + "_atc");
	if(lblStockStatus != "undefined" && lblStockStatus != null)
	{
	    if (result != "undefined" && result != null)
	    {
	        if (result[8] != "5")
	        {
	            lblStockStatus.innerHTML = result[5];
	        }
		}
		else
		{
		    lblStockStatus.innerHTML = '';
		}
	}
	
	if(divOrder != "undefined" && divOrder != null)
	{
		if(result != "undefined" && result != null)
	    {
		    if (result[6] == "true" && result[5].length > 0)
		    {
			    divOrder.style.visibility = "visible";
		    }
		    else
		    {
			    divOrder.style.visibility = "hidden";
		    }
		}
		else
		{
		    divOrder.style.visibility = "hidden";
		}
	}
	
	if(atccAddToCart != "undefined" && atccAddToCart != null)
	{
		if(result != "undefined" && result != null)
	    {
		    if (result[6] == "true" && result[5].length > 0)
		    {
			    atccAddToCart.style.visibility = "visible";
		    }
		    else
		    {
			    atccAddToCart.style.visibility = "hidden";
		    }
		}
		else
		{
		    atccAddToCart.style.visibility = "hidden";
		}
	}	
	
	var lblQuantity = $get(context + "_lblQuantity");
	var tbQuantity = $get(context + "_tbQuantity");
	if (tbQuantity != "undefined" && tbQuantity != null)
	{
	    if (result != "undefined" && result != null)
	    {
	        if (result[6] == "true" && result[5].length > 0)
	        {
	            tbQuantity.style.visibility = "visible";
	        }
	        else
	        {
	            tbQuantity.style.visibility = "hidden";
	        }
	    }
	    else
	    {
	        tbQuantity.style.visibility = "hidden";
	    }
	}
	
	if (lblQuantity != "undefined" && lblQuantity != null)
	{
	    if (result != "undefined" && result != null)
	    {
	        if (result[6] == "true" && result[5].length > 0)
	        {
	            lblQuantity.style.visibility = "visible";
	        }
	        else
	        {
	            lblQuantity.style.visibility = "hidden";
	        }
	    }
	    else
	    {
	        lblQuantity.style.visibility = "hidden";
	    }
	}	
}

// Attributes

function Products_ChangeAttribute(containerID, controlID)
{   
	var hiddenField = $get(containerID + '_dpa_hdnAttributes');
	 
	var ddl = $get(controlID);
	
	if ((ddl != "undefined" && ddl != null) && (hiddenField != "undefined" && null != hiddenField))
	{
	    JetShop.StoreControls.Services.Products.GetAttributes(ddl.options[ddl.selectedIndex].value, hiddenField.value, Products_AttributeValueData, null, containerID);
	}
	
		    
	if(ddl.options[ddl.selectedIndex].value == '-1')
	{
	    var ddlist2 = $get(containerID + '_dpa_ddlItems2');   
	    ddlist2.disabled = true;
	}
}

function Products_ChangeAttributeValue(containerID, controlID)
{
	var ddl = $get(controlID);
	
	if ((ddl != "undefined" && ddl != null)) {

	    var value = ddl.options[ddl.selectedIndex].value;

	    if (value == -1) {
	        value = ddl.options[1].value;
	    }
	    JetShop.StoreControls.Services.Products.GetAttributeValues(value, Products_RefreshData, null, containerID);
	}
}

// SendTip

function Products_SendTipResult(result, context, methodName)
{
    var resultBool = result[0];
    
    var resultMessage = result[1];
    
    if (resultBool == "True")
    {
        var tbTo = $get(context + '_tbTo');
        tbTo.value = '';
        
        var tbFrom = $get(context + '_tbFrom');
        tbFrom.value = '';
        
        var tbYourEmail = $get(context + '_tbYourEmail');
        tbYourEmail.value = '';
    }

     var spanResult = $get(context + '_sendTipStatus');
     
     spanResult.innerHTML = resultMessage;
}

function Products_SendTip(textboxTo, txtboxFrom, textboxYourEmail, productID, containerID, currentDisplayCulture)
{
    var tbTo = $get(textboxTo);
    var tbFrom = $get(txtboxFrom);
    var tbYourEmail = $get(textboxYourEmail);
    
    JetShop.StoreControls.Services.General.SendTip(tbTo.value, tbFrom.value, tbYourEmail.value, productID, currentDisplayCulture, Products_SendTipResult, null, containerID);
}

function Products_SendTip_ClearMessage(containerID)
{
     var spanResult = $get(containerID + '_sendTipStatus');
     
     spanResult.innerHTML = '';
}

// ImageDisplay

function ID_ChangeLargeImage(strObjHdn)
{
    var hdnFilename = $get(strObjHdn);

    if (hdnFilename != null)
    {
        ID_ShowLargeImage(hdnFilename.value);
    }
}

function ID_ShowLargeImage(strFilename)
{
	var height = 450;
	var width = 450;

	newWin=window.open('ProductPopup.aspx?filename=' + strFilename,'', 'menubar=no,resizable=no,width=' + width + ',height=' + height + ',titlebar=no,alwaysRaised=yes,status=no,scrollbars=yes,left='+(screen.width/2-width/2)+',top='+(screen.height/2-height/2-height/10)+',screenX=0,screenY=0');
}

function ID_ChangeImage(strObjName, strObj2Name, imgUrl, imgName, strHdnName)
{
	var imgProduct = document.getElementById(strObjName);
	var hlProduct = document.getElementById(strObj2Name);
	var hdnProduct = document.getElementById(strHdnName);
	
	if (imgProduct != null)
	{
		ID_SetImage(imgProduct, hlProduct, imgUrl);
		if (hdnProduct != null)
		{
			hdnProduct.value = imgName;
		}
	}
}

function ID_SetImage(obj, obj2, value)
{
	if (value == "" || value == null)
	{
		if (obj != null)
		{
			obj.style.visibility = "hidden";
		}
		if (obj2 != null)
		{
			obj2.style.visibility = "hidden";
		}
	}
	else
	{
		if (obj != null)
		{
			obj.style.visibility = "";
		}
		if (obj2 != null)
		{
			obj2.style.visibility = "";
		}
		
		obj.src = value;
	}
}
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();