/**************************************************************************************************************
 * 
 * Additional Scripts (S. Frater - 23/06/2009)
 *
 **************************************************************************************************************/

// Globals
var handler = "/default.aspx";

function EditItem(obj, styleCode) {
    try {
        if (obj != null) {
            var itemID = obj.id.replace('editlink_', '');
            var item_area = $('#edit_form_' + itemID);
            if ((itemID > 0) && (item_area != null)) {
                $('table.cart .edit_form').css({ 'display': 'none' });
                $('table.cart .item').css({ 'display': '' });
                clearEditItemContent(itemID);
                setOrderItemContent(item_area, itemID, styleCode);
            }
        }
    } catch (e) { }
    return false;
}

function InsertElementOnPage(target, data, cleanUp) {
    if (cleanUp == 1) {
        $(target).html("&nbsp;");
        if (data != '') { $(target).empty(); }
    }
    $(target).append(data);
}

function clearEditItemContent(id) {
    if (id > 0) {
        InsertElementOnPage(document.getElementById('edit_form_' + id), '&nbsp', 1);
    }
}

function setOrderItemContent(obj, itemID, styleCode) {
    if (obj != null && !isNaN(itemID)) {
        $("#item_" + itemID).css({ 'display': 'none' });
		$('#edit_form_' + itemID).html('<td class="Image">&nbsp;</td><td class="Description"><img src="/images/loader.gif" /></td><td class="Price">&nbsp;</td><td class="Qty">&nbsp;</td><td class="Total">&nbsp;</td>').css({ 'display': '' });
        var url = handler;
        var data = { "Z": "c", "action": "edititem", "cs": styleCode, "lc": itemID }
        jQuery.ajax({
            "url": url,
            "data": data,
            "success": function(data) { ShowEditItemForm(data, itemID); },
            "cache": false
        });
    }
}

function ShowEditItemForm(data, itemID) {
    try {
        var editItem = $('#edit_form_' + itemID);
        editItem.stop();
        editItem.html(data);
        var colour = $("#changeClID_" + itemID).val();
        editItem.css({ 'display': '' });
		$('.Edit a',editItem).bind('click',function(event){event.preventDefault();})
		$('input.quantity',editItem).upDownArrows();        
        fn_ShowSizes(colour, itemID); 
    } catch (e) {
        CancelUpdateItem(itemID);
    }
}

function UpdateItem(itemID) {
    setChangedQty(itemID);
    var colour = $('input#changeClID_' + itemID).val();
    var size = $('input#changeSzid_' + itemID).val();
    var qty = $('input#changeQuantity_' + itemID).val();
    if (qty && size && colour && qty > 0 && size >= 0 && colour >= 0) {
        $("#frmPost_" + itemID).submit();
    } else {
        alert("Please select the colour and size that you require before adding this item to your cart");
        return false;
    }
}

function CancelUpdateItem(itemID) {
    $("#edit_form_" + itemID).css({ 'display': 'none' });
    $("#item_" + itemID).css({ 'display': '' }); ;
    clearEditItemContent(itemID);
}

function setChangedSize(clicked, itemID) {
    var iSz = clicked.options[clicked.selectedIndex].value;
    $('input#changeSzid_' + itemID).val(iSz);
    $("select[id^='changeSizes_" + itemID + "' ]").each(function() {
        $(this).val(iSz);
        //if ($(this).val() != iSz) { $(this).val(""); }
    });
    setChangedPrice(itemID);
}

function setChangedColour(clicked, itemID)
{
    var iCl = clicked.options[clicked.selectedIndex].value;
    fn_ShowSizes(iCl, itemID);
    $('input#changeClID_' + itemID).val(iCl);
    setChangedPrice(itemID);
}

function fn_ShowSizes(cid, itemID)
{ 
	var toShowID='changeSizes_'+itemID;
	if(cid){
		toShowID += '_'+cid
	}
	showSizeSelect(toShowID,'#edit_form_' + itemID +' .Size');
	$('input#changeSzid_' + itemID).val($('#' + toShowID).val());
}

function setChangedQty(itemID) {
    var iQty = $('input#qty_' + itemID).val();
    if(iQty) { $('input#changeQuantity_' + itemID).val(iQty); }
}

function setChangedPrice(itemID)
{
    var colour = $('input#changeClID_' + itemID).val();
    var size = $('input#changeSzid_' + itemID).val();
    $("span.item_price").hide();
    $("#selection_price_" + colour + "_" + size).show();
}

function showSizeSelect(toShowID,toHideSelector){
	if(toHideSelector){
		$(toHideSelector).css('display','none');
	}
	if(toShowID) {
	  $('#'+toShowID).css('display','');
	}
}


// Function to allow the display of an additional message on the login popup.
function setLoginMessage(message, clearAdditionalFormData) {
  if (clearAdditionalFormData) {
    try { // Reset form data
      $("#loginfrm3 #isams").remove();
      $("#loginfrm3 #pdID").remove();
      $("#loginfrm3 #catID").remove();
      $("#loginfrm3 #szID").remove();
      $("#loginfrm3 #clID").remove();
      $("#loginfrm3 #q").remove();
    } catch (e) { }
  }

  if (message != undefined && message.length > 0) {
    $("#login-box-message").text(message);
    $("#login-box-message").show();
  } else {
    $("#login-box-message").text();
    $("#login-box-message").hide();
  }
}

// Function to set the id of a gift registry
function setRegistry(id) {
  try {
    if (id != undefined && id.length > 0) { $("#gID").val(id); } 
    else { $("#gID").val(); }
  } catch(e) { }
}

// Function to ensure customer has either applied promotions or chosen to ignore them
function chkPromotions() {
	if (promotionsChanged == 2 && promotionsCount == 0) {
		return true;
	} else if (promotionsChanged == 0 && promotionsCount == 0) {
		alert("Please select which offer you would like to use with this order, then click the 'APPLY OFFERS' button. If you do not wish to apply any offers, please select \"I'll wait until next time thanks\".");
		return false;
	} else if (promotionsChanged > 0) {
		alert("Please make sure you click the 'Apply Offers' button to redeem your offer.");
		return false;
	} else {
		return true;
	}
}

// Function to ensure the customer has modified the promotions before clicking 'Apply Offer'
function chkChangedPromotion() {
	try {
		var ipromo = $(".ipromotion:checked").length;
		var opromo = $(".opromotion:checked").length;
		var nopromo = $(".nopromotion:checked").length;

		if (ipromo == 0 && opromo == 0 && nopromo == 0) { // nothing is selected.
			alert("Please select which offer you would like to use with this order, then click the 'APPLY OFFERS' button. If you do not wish to apply any offers, please select \"I'll wait until next time thanks\".");
		} else if ((nopromo > 0 && promotionsCount == 0) || promotionsChanged == 0) { // wait until next time is selected and there were no promotions previously applied
			// do nothing.
		} else {
			document.getElementById('frmPost_Promotions').submit();
		} 
	} catch (e) { }
}

function fFindInStore() {
	try {
		var size = "", colour = "";
		size = $("#AddToCartForm input[name = 'szID']").val();
		if (!size) { size = $("#szID option:selected").val(); }
		colour = $("#AddToCartForm input[name = 'clID']").val();
		var prod = $("#AddToCartForm input[name = 'pdID']").val();
		var quantity = $("#AddToCartForm input[name = 'qty']").val();
		if (quantity && size && colour && prod && prod > 0 && quantity > 0 && size >= 0 && colour >= 0) {
			$("#find_link").attr('href', '#find_/default.aspx?Z=selfhelp&pdID=' + prod + '&szID=' + size + '&clID=' + colour + '&qty=' + quantity);
		} else {
			$("#find_link").attr('href', '#show_');
			alert("Please select the colour, size and quantity that you require before finding this item in store.");
		}
	} catch (e) { }
	return false;
}




