﻿// Global variables
var selectedGiftCat;
var arrSelectedGifts = new Array();
var giftTotalValue = 0;

//function goItem(itemID)
//{
//	document.form1.action = "ItemDetail.aspx?itemID=" + itemID;
//	document.form1.submit();
//}

function imageChange(element)
{
    mainImgsrc = document.getElementById("mainImg").src;
    subImgsrc = element.src;
    
    // Remove end of filename and add ref to thumbnail filename
    var iLen = String(mainImgsrc).length;
    mainImgsrc = String(mainImgsrc).substring(0, iLen - 4);
    mainImgsrc = mainImgsrc + "_thumb.jpg";
        
    // Remove end of filename and add ref to main filename
    var iLen = String(subImgsrc).length;
    subImgsrc = String(subImgsrc).substring(0, iLen - 10);
    subImgsrc = subImgsrc + ".jpg";
    
    document.getElementById("mainImg").src = subImgsrc;
    element.src = mainImgsrc;
}

function selectGiftCat(giftcatID, radio)
{
	//Hide the current Gifts
	document.getElementById("giftcat" + selectedGiftCat).style.display = 'none';
	document.getElementById("giftcat" + selectedGiftCat).style.visibility = 'hidden';
	
	// Unselect Gift Category Tab
	document.getElementById("GiftcatTabL" + selectedGiftCat).className = 'giftCats';
	document.getElementById("GiftcatTabR" + selectedGiftCat).className = 'tabData';

	// Show the new Gifts
	document.getElementById("giftcat" + giftcatID).style.display = 'block';
	document.getElementById("giftcat" + giftcatID).style.visibility = 'visible';
	
	// Select the new Gift Category Tab
	document.getElementById("GiftcatTabL" + giftcatID).className = 'giftCatsSelected';
	document.getElementById("GiftcatTabR" + giftcatID).className = 'tabDataSelected';
	
	// Check the 'I don't want a card' radio button as default when changing tabs
	// And disable txtDeliveryMessage ** AL SAYS NO! **
//	if (radio == 'True')
//	{
//		document.getElementById("gcNone").checked = 'checked';
//		noGiftCardSelected();
//	}
	
	// Store the current Gift Category ID as a global variable
	selectedGiftCat = giftcatID;
}

function noGiftCardSelected(id)
{
	//document.getElementById("txtDeliveryMessage").value = '';
	/*document.getElementById("DeliveryMessage").style.visibility="hidden";
	document.getElementById("DeliveryMessage").style.display="none";*/
	/*document.getElementById("txtDeliveryMessage").disabled = true;
	document.getElementById("txtDeliveryMessage").style.background="#ccc";*/
	document.getElementById("hdnRadioID").value=id;
}

function GiftCardSelected(id)
{
	/*document.getElementById("DeliveryMessage").style.visibility="visible";
	document.getElementById("DeliveryMessage").style.display="block";*/
	/*document.getElementById("txtDeliveryMessage").disabled = false;
	document.getElementById("txtDeliveryMessage").style.background="#fff";*/
	document.getElementById("hdnRadioID").value=id;
}
function setGiftCardCat(catID)
{
    document.getElementById("hdnCardCatID").value=catID;
}
function gift(itemID, publicationCode, itemNo, price, description)
{
    // Object definition
    
    // Object properties
    this.itemID = itemID;
    this.publicationCode = publicationCode;
    this.itemNo = itemNo;
    this.price = price;
    this.description = description;
}

function AddGiftItem(itemID, publicationCode, itemNo, price, description)
{
    // Get the maximum amount of gifts that can be ordered with the selected Item
    var intMaxGiftCount = document.getElementById("MaxGiftCount").value;

    // Check if the maximum gift count has alreadt been met
    if (arrSelectedGifts.length < intMaxGiftCount)
    {
        var boolGiftAlreadyInBag = 0;

        for (var i = 0; i < arrSelectedGifts.length; i++)
        {
            if (arrSelectedGifts[i].itemID == itemID)
            {
                boolGiftAlreadyInBag = 1;
            }
        }
    
        // check if the Gift has already been added to the bag
        if (boolGiftAlreadyInBag == 0)
        {        
            // Add the gift to the Array of selected gifts
            arrSelectedGifts[arrSelectedGifts.length]=new gift(itemID, publicationCode, itemNo, price, description)
           
            // Show the Selected Gifts to the user
            BuildSelectedGiftsHTML();
            
            UpdateAvailableGiftCountRemainingHTML(itemID);
            
            UpdateGiftTotal();
            
            UpdateGiftsHiddenField();
        }
        else
        {
            // Display a message to the Customer stating that the gift can only
            // be ordered once per order    
            document.getElementById("divDuplicatedGift_" + itemID).style.display = 'block';
            document.getElementById("divDuplicatedGift_" + itemID).style.visibility = 'visible';            
            
            // Wait 3 seconds, and then hide the above message via HideDuplicatedGiftMessage()
            setTimeout("HideDuplicatedGiftMessage("+itemID+")", (3 * 1000));
        }
    }
    else
    {
        // Display a message to the Customer stating that the no more gifts can be
        // added to the order
        var intMaxGiftCount = document.getElementById("MaxGiftCount").value;
        document.getElementById("divgiftMaxMessage_" + itemID).innerHTML = 'A maximum of ' + intMaxGiftCount + ' Gift(s) can be added to the Order';
        document.getElementById("divgiftMaxMessage_" + itemID).className = 'giftMaxMessage';
        document.getElementById("divMaximumGiftCountMet_" + itemID).style.display = 'block';
        document.getElementById("divMaximumGiftCountMet_" + itemID).style.visibility = 'visible';
        
        // Wait 3 seconds, and then hide the above message via HideMaxGiftMessage()
        setTimeout("HideMaxGiftMessage("+itemID+")", (3 * 1000));
    }
}

function RemoveGiftItem(arrSelectedGiftsPosition)
{
    // Remove the gift from the array of selected gifts
    arrSelectedGifts.splice(arrSelectedGiftsPosition,1)
    
    // Show the Selected Gifts to the user
    BuildSelectedGiftsHTML();
    
    UpdateAvailableGiftCountRemainingHTML();
    
    UpdateGiftTotal();
    
    UpdateGiftsHiddenField();
}

function BuildSelectedGiftsHTML()
{
    var strSelectedGiftsHTML = "<table border='0' cellpadding='0' cellspacing='0'>";

    // Build up a HTML table of selected gifts
    for (var i = 0; i < arrSelectedGifts.length; i++)
    {
        strSelectedGiftsHTML = strSelectedGiftsHTML + "<tr class='giftli'>"
                                                    +   "<td width='175px'>" + (i+1) + ") " + arrSelectedGifts[i].description
                                                    +   "</td>"
                                                    +   "<td>"
                                                    +       "<a href='javascript:RemoveGiftItem("+ i + ");'>delete"
                                                    +       "</a>"
                                                    +   "</td>"
                                                    + "</tr>";
    }

    strSelectedGiftsHTML = strSelectedGiftsHTML + "</table>";

    // Display the selected gifts on the screen
    document.getElementById("divSelectedGifts").innerHTML = strSelectedGiftsHTML;
}

function UpdateAvailableGiftCountRemainingHTML(itemID)
{
    var intRemainingGiftCount = (document.getElementById("MaxGiftCount").value - arrSelectedGifts.length);
    
    if (intRemainingGiftCount != 0)
    {
        document.getElementById("divAvailableGiftCountRemaining").innerHTML = "you may add " + intRemainingGiftCount + " more gift(s) to your Order";
    }
    else
    {
        document.getElementById("divAvailableGiftCountRemaining").innerHTML = "you can't add any more gifts to your order";
    }
}

function CharacterCount(charsAllowed)
{
	val = document.getElementById("ctl00_MessagePlaceHolder_txtDeliveryMessage").value;
	
	if (val.length > charsAllowed) 
	{
		alert('Sorry, the limit of ' + charsAllowed + ' characters has been reached');
		this.value = val.substring(0,charsAllowed);
		document.getElementById("ctl00_MessagePlaceHolder_txtDeliveryMessage").focus()
		document.getElementById("deliveryMsgChars").innerText = 0;
	}
	
	if (document.all)	//IE
	{
		if (document.getElementById("deliveryMsgChars").innerText != 0)
		{
		    document.getElementById("deliveryMsgChars").innerText = charsAllowed-parseInt(val.length);
		}
	}
	else	//Firefox
	{
		if (document.getElementById("deliveryMsgChars").textContent != 0)
		{
		    document.getElementById("deliveryMsgChars").textContent = charsAllowed-parseInt(val.length);
		}
	}
}

function Capitalise(control)
{
    document.getElementById(control).value = document.getElementById(control).value.toUpperCase();
}

function HideMaxGiftMessage(itemID)
{
    // Hide the message stating that the no more gifts can be added to the order
    document.getElementById("divMaximumGiftCountMet_"+itemID).style.visibility = 'hidden';
    document.getElementById("divMaximumGiftCountMet_"+itemID).style.display = 'none';
    
    // Fade the image
    //document.getElementById("divMaximumGiftCountMet_"+itemID).filters[0].apply();
    //document.getElementById("divMaximumGiftCountMet_"+itemID).style.visibility = 'hidden';
    //document.getElementById("divMaximumGiftCountMet_"+itemID).style.display = 'none';    
    //document.getElementById("divMaximumGiftCountMet_"+itemID).filters[0].play();

}

function HideDuplicatedGiftMessage(itemID)
{
    var duplicatedGift = document.getElementById("divDuplicatedGift_"+itemID);

    // Hide the message stating that the gift can only be ordered once
    if (duplicatedGift)
    {
        duplicatedGift.style.visibility = 'hidden';
        duplicatedGift.style.display = 'none';
    }
}

function UpdateGiftTotal()
{
    var intGiftPriceTotal = 0;

    for (var i = 0; i < arrSelectedGifts.length; i++)
    {
        intGiftPriceTotal = intGiftPriceTotal + arrSelectedGifts[i].price;
    }
    document.getElementById(giftTotalValue).innerHTML = "£" + intGiftPriceTotal.toFixed(2);
}

function GetOrder()
{
    // Build up the Selected Gifts in the Hidden Field
   
    var hdnSelectedGifts = "<Order><Gifts>";
    
    for (var i = 0; i < arrSelectedGifts.length; i++)
    {
        hdnSelectedGifts = hdnSelectedGifts + "<Gift ItemID='" + arrSelectedGifts[i].itemID + "' />";
    }
    
    hdnSelectedGifts = hdnSelectedGifts + "</Gifts></Order>";
   
    if (document.getElementById("ctl00_hdnSelectedGifts"))
    {
        document.getElementById("ctl00_hdnSelectedGifts").value = hdnSelectedGifts;
    }
}

function UpdateGiftsHiddenField()
{
    // Build up the Selected Gifts in the Hidden Field
   
    var hdnSelectedGifts1 = "";
    
    for (var i = 0; i < arrSelectedGifts.length; i++)
    {
        hdnSelectedGifts1 = hdnSelectedGifts1 + arrSelectedGifts[i].itemID + "|" + arrSelectedGifts[i].publicationCode + "|" + arrSelectedGifts[i].itemNo + "|" + arrSelectedGifts[i].price + "|" + arrSelectedGifts[i].description + "~"
    }
    
    document.getElementById("ctl00_hdnSelectedGifts1").value = hdnSelectedGifts1;
}

function openWin(URL,aName,wName,Scroll,W,H,T,L)
{
    window.name = aName
    aWindow = window.open(URL,wName,"toolbar=no,location=no,directories=no,status=no,,scrollbars="+ Scroll +",width="+ W +",height="+ H +",top="+ T +",left="+ L +",resize=yes,menubar=no");
}

function getSelectedGifts()
{
    var strGifts = document.getElementById("ctl00_hdnSelectedGifts1").value;
    
    if (strGifts != "")
    {
        do
        {
            var Item = strGifts.substring(0,strGifts.indexOf("~"));
            var tempGift = new Array();
            tempGift = Item.split('|');

            if (tempGift.length = 5)
            {
                arrSelectedGifts[arrSelectedGifts.length]=new gift(parseInt(tempGift[0]), tempGift[1], tempGift[2], parseFloat(tempGift[3]), tempGift[4]);
                
                strGifts = strGifts.substring(strGifts.indexOf("~")+1, strGifts.Length);
                
                // Show the Selected Gifts to the user
                BuildSelectedGiftsHTML();
                
                UpdateAvailableGiftCountRemainingHTML(parseInt(tempGift[0]));
                
                UpdateGiftTotal();
            }
        }
        while (strGifts != "")
    }
}

function altTagging(id) {
	var divtag = document.getElementById('details' + id);
	

	divtag.style.visibility = 'visible';
	divtag.style.display = 'inline';

	var tabletag = document.getElementById('table' + id);

}

function hideDetails(id)
{
	var divtag = document.getElementById('details' + id);
	
	divtag.style.visibility = 'hidden';
	divtag.style.display = 'none';
}

function popup2(sPicURL)
{
    window.open( "popup.htm?"+sPicURL, "",  "resizable=0,HEIGHT=100,WIDTH=100");
}

function getQueryVariable(variable) 
{  var query = window.location.search.substring(1);
    var vars = query.split("&");  
    for (var i=0;i<vars.length;i++) 
    {    
        var pair = vars[i].split("="); 
        if (pair[0] == variable) 
        {
            preloadImages(pair[1]);
            return pair[1];
        }
    }
}

function preloadImages(itemNo)
{
    if (document.images)
    {
        pic1= new Image(245,245);
        pic1.src='http://content.next.co.uk/flowers/images/'+ itemNo + '1.jpg';
        pic2 = new Image(124,120);
        pic2.src='http://content.next.co.uk/flowers/images/'+ itemNo + '2_thumb.jpg';
        pic3 = new Image(245,245);
        pic3.src='http://content.next.co.uk/flowers/images/'+ itemNo + '2.jpg';
        pic4 = new Image(124,120);
        pic4.src='http://content.next.co.uk/flowers/images/'+ itemNo + '1_thumb.jpg';
    }
}