﻿var FrameType = {Wooden:1, Alluminium:2};
var PaperType = { Matt: 1, Glossy: 2 };

var Calculator = {
    ImagePath: Constants.VirtualUploadPath + "temp/ClientUpload/",
    IsDemo: false,

    PrintWidth: null,
    PrintHeight: null,

    UnitPrice: null,
    Quantity: 1,
    TotalPrice: null,

    CurrentMaterialID: null,
    CurrentTreatmentID: null,
    CurrentPaperType: null,
    CurrentFrameType: null,
    CurrentWoodenFrameID: null,
    CurrentAlluminiumFrameID: null,
    CurrentImageEffectID: null,
    CurrentTreatments: null,

    ImageFileName: null,

    ImageWidth: null,
    ImageHeight: null,
    ThumbImageWidth: null,
    ThumbImageHeight: null,
    ThumbImageExtUrl: null,

    MinimumPrintWidth: null,
    MaximumPrintWidth: null,

    MinimumPrintHeight: null,
    MaximumPrintHeight: null,

    SizeLegal: false,
	extImageID: null,
	extMediumThumb: null,
	extLargeThumb: null,
	
    ProportionByWidth: function(w) {
        return CalculateProportionByWidth(w, this.ImageWidth, this.ImageHeight);
    },

    ProportionByHeight: function(h) {
        return CalculateProportionByHeight(h, this.ImageWidth, this.ImageHeight);
    },

    onWidthHeightBlur: function() {
        $("#ImageSizeCheckMessage").html("");
        this.onWidthChange();
        this.SizeLegal = true;
        var OB = $(".CalculatorAddToCart");
        OB.removeClass("CalculatorAddToCartDisabled");

    },


    onWidthKeyUp: function(event) {
        var key = event.keyCode;
        //if (key < 47 || key > 58) return;

        var w = $("#fldWidth").val();
        w = parseInt(w);

        if (isNaN(w)) {
            $("#fldHeight").val("");
            return false;
        }

        if (w < this.MinimumPrintWidth) {
            $("#ImageSizeCheckMessage").html(Translate('WebSite.Calculator.SizeTooSmall'));
            this.SizeLegal = false;
        }
        else if (w > this.MaximumPrintWidth) {
            $("#ImageSizeCheckMessage").html(Translate('WebSite.Calculator.SizeTooBig'));
            this.SizeLegal = false;
        }
        else {
            $("#ImageSizeCheckMessage").html("");
            this.SizeLegal = true;
        }

        this.onWidthChange(true);
        return true;
    },

    onHeightKeyUp: function(event) {

        var key = event.keyCode;
        //if (key < 47 || key > 58) return;

        var h = $("#fldHeight").val();
        h = parseInt(h);
        if (isNaN(h)) {
            $("#fldWidth").val("");
            return false;
        }

        if (h < this.MinimumPrintHeight) {
            $("#ImageSizeCheckMessage").html(Translate('WebSite.Calculator.SizeTooSmall'));
            this.SizeLegal = false;
        }
        else if (h > this.MaximumPrintHeight) {
            $("#ImageSizeCheckMessage").html(Translate('WebSite.Calculator.SizeTooBig'));
            this.SizeLegal = false;
        }
        else {
            $("#ImageSizeCheckMessage").html("");
            this.SizeLegal = true;
        }

        this.onHeightChange(true);
        return true;
    },

    onWidthHeightKeyPress: function(event) {
      /*  var key = event.keyCode;
        if (key > 47 && key < 58)
            return;
        else
            event.returnValue = null;*/
    },

    onWidthChange: function(IC) {
		var h;
		
		if (this.IsDemo)
		{
			h = $("#fldHeight").val();
			(h=="")?h=0:h = $("#fldHeight").val();
			h = parseInt(h);
		}
		else
		{
			h = 0;
		}

        var w = $("#fldWidth").val();
        w = parseInt(w);


        var internalCall = (typeof (IC) == "undefined") ? false : IC;

        if (internalCall) {
            if (isNaN(w))
                h = ""
            else
			{
				if (!this.IsDemo)
	                h = this.ProportionByWidth(w);
			}
        }
        else {
            if (isNaN(w) || w < this.MinimumPrintWidth) {
                w = this.MinimumPrintWidth;
                h = this.MinimumPrintHeight;
            }
            else if (w > this.MaximumPrintWidth) {
                w = this.MaximumPrintWidth;
                h = this.MaximumPrintHeight;
            }
            else {
				if (!this.IsDemo)
				{
					h = this.ProportionByWidth(w);
				}

            }
        }

        if (!internalCall) {
            if (h > this.MaximumPrintHeight || w > this.MaximumPrintWidth) {
                h = this.MaximumPrintHeight;
                w = this.MaximumPrintWidth;
            }
            else if (h < this.MinimumPrintHeight || w < this.MinimumPrintWidth) {
                h = this.MinimumPrintHeight;
                w = this.MinimumPrintWidth;
            }
        }


        $("#fldWidth").val(w);
        $("#fldHeight").val(h);

        //if (!internalCall) this.CalculatePrice();

        var OB = $(".CalculatorAddToCart");
        if (!this.SizeLegal) {
            OB.addClass("CalculatorAddToCartDisabled");
        }
        else {
            OB.removeClass("CalculatorAddToCartDisabled");
            this.CalculatePrice();
        }
        return true;
    },

    onHeightChange: function(IC) {
		var w;
		
		if (this.IsDemo)
		{
			w = $("#fldWidth").val();
			(w=="")?w=0:w = $("#fldWidth").val();
			w = parseInt(w);
		}
		else
		{
			w = 0;
		}


        var h = $("#fldHeight").val();
        h = parseInt(h);

        var internalCall = (typeof (IC) == "undefined") ? false : IC;

        if (internalCall) {
            if (isNaN(h))
                w = ""
            else
			{
				if (!this.IsDemo)
	                w = this.ProportionByHeight(h);	
			}
        }
        else {
            if (isNaN(h) || h < this.MinimumPrintHeight) {
                h = this.MinimumPrintHeight;
                w = this.MinimumPrintWidth;
            }
            else if (h > this.MaximumPrintHeight) {
                h = this.MaximumPrintHeight;
                w = this.MaximumPrintWidth;
            }
            else {
				if (!this.IsDemo)
				{
					w = this.ProportionByHeight(h);
				}
            }
        }

        if (!internalCall) {
            if (h > this.MaximumPrintHeight || w > this.MaximumPrintWidth) {
                h = this.MaximumPrintHeight;
                w = this.MaximumPrintWidth;
            }
            else if (h < this.MinimumPrintHeight || w < this.MinimumPrintWidth) {
                h = this.MinimumPrintHeight;
                w = this.MinimumPrintWidth;
            }
        }

		$("#fldWidth").val(w);
        $("#fldHeight").val(h);


        //if (!internalCall) this.CalculatePrice();

        var OB = $(".CalculatorAddToCart");
        if (!this.SizeLegal) {
            OB.addClass("CalculatorAddToCartDisabled");
        }
        else {
            OB.removeClass("CalculatorAddToCartDisabled");
            this.CalculatePrice();
        }
        return true;

        /*
        var h = $("#fldHeight").val();
        if (h < this.MinimumPrintHeight) {
        $("#fldHeight").val(this.MinimumPrintHeight);
        $("#fldWidth").val(this.ProportionByHeight(this.MinimumPrintHeight))
        }
        else if (h > this.MaximumPrintHeight) {
        $("#fldHeight").val(this.MaximumPrintHeight);
        $("#fldWidth").val(this.ProportionByHeight(this.MaximumPrintHeight))
        }
        else {
        $("#fldWidth").val(this.ProportionByHeight(h))
        }

        this.CalculatePrice();
        return true;*/
        
    },

    onQuantityChange: function() {

        var Q = $("#fldQuantity").val();
        this.Quantity = parseInt(Q);
        this.CalculatePrice();
        return true;
    },


    CalculatePrice: function() {
        this.UnitPrice = 0;
        if(Calculator.GetQueryStringParams("imageID")!="" && typeof(Calculator.GetQueryStringParams("imageID"))!="undefined")
			{
			this.UnitPrice = 12;
			}
        this.PrintWidth = parseFloat($("#fldWidth").val());
        this.PrintHeight = parseFloat($("#fldHeight").val());

        $('#ExtraNtificationList .MaterialNotification').remove();
        if (this.CurrentMaterialID != null) {
            var MD = IPS.Materials[this.CurrentMaterialID];
            if (MD.CalculatedPrice) {
                var MDPrice;
                if (Constants.MetricSystem) {
                    MDPrice = this.PrintWidth.CmToMeter() * this.PrintHeight.CmToMeter() * MD.Price;
                }
                else {
                    MDPrice = this.PrintWidth.InchToFoot() * this.PrintHeight.InchToFoot() * MD.Price;
                }
                this.UnitPrice += MDPrice;
            }
            else {
                this.UnitPrice += MD.Price;
            }

            if (MD.ExtraNotification != undefined && MD.ExtraNotification != null && MD.ExtraNotification.length > 0) {
                $('#ExtraNtificationList').append("<div class='ENC MaterialNotification' id='MaterialNotification_" + MD.ID + "'>" + MD.ExtraNotification + "</div>")
            }
        }

        $('#ExtraNtificationList .WoodenFrameNotification').remove();
        if (this.CurrentWoodenFrameID != null) {
            var WFD = IPS.WoodenFrames[this.CurrentWoodenFrameID];
            if (WFD.CalculatedPrice) {
                var FrameLength;
                if (Constants.MetricSystem) {
                    FrameLength = (((this.PrintWidth + this.PrintHeight) * 2) + (WFD.FrameWidth * 2)).CmToMeter();
                }
                else {
                    FrameLength = (((this.PrintWidth + this.PrintHeight) * 2) + (WFD.FrameWidth * 2)).InchToFoot();
                }
                this.UnitPrice += (FrameLength * WFD.Price);
            }
            else {
                this.UnitPrice += WFD.Price;
            }

            if (WFD.ExtraNotification != undefined && WFD.ExtraNotification != null && WFD.ExtraNotification.length > 0) {
                $('#ExtraNtificationList').append("<div class='ENC WoodenFrameNotification' id='WoodenFrameNotification_" + WFD.ID + "'>" + WFD.ExtraNotification + "</div>")
            }
        }

        $('#ExtraNtificationList .AlluminiumFrameNotification').remove();
        if (this.CurrentAlluminiumFrameID != null) {
            var AFD = IPS.AlluminiumFrames[this.CurrentAlluminiumFrameID];
            if (AFD.CalculatedPrice) {
                var FrameLength;
                if (Constants.MetricSystem) {
                    FrameLength = (((this.PrintWidth + this.PrintHeight) * 2) + (Constants.AlluminiumFrameWidth * 2)).CmToMeter();
                }
                else {
                    FrameLength = (((this.PrintWidth + this.PrintHeight) * 2) + (Constants.AlluminiumFrameWidth * 2)).InchToFoot();
                }
                this.UnitPrice += (FrameLength * AFD.Price);
            }
            else {
                this.UnitPrice += AFD.Price;
            }

            if (AFD.ExtraNotification != undefined && AFD.ExtraNotification != null && AFD.ExtraNotification.length > 0) {
                $('#ExtraNtificationList').append("<div class='ENC AlluminiumFrameNotification' id='AlluminiumFrameNotification_" + AFD.ID + "'>" + AFD.ExtraNotification + "</div>")
            }
        }

        $('#ExtraNtificationList .ImageEffectNotification').remove();
        if (this.CurrentImageEffectID != null) {
            var IED = IPS.ImageEffects[this.CurrentImageEffectID];
            if (IED.CalculatedPrice) {
                var IEDPrice;
                if (Constants.MetricSystem) {
                    IEDPrice = this.PrintWidth.CmToMeter() * this.PrintHeight.CmToMeter() * IED.Price;
                }
                else {
                    IEDPrice = this.PrintWidth.InchToFoot() * this.PrintHeight.InchToFoot() * IED.Price;
                }
                this.UnitPrice += IEDPrice;
            }
            else {
                this.UnitPrice += IED.Price;
            }

            if (IED.ExtraNotification != undefined && IED.ExtraNotification != null && IED.ExtraNotification.length > 0) {
                $('#ExtraNtificationList').append("<div class='ENC ImageEffectNotification' id='ImageEffectNotification_" + IED.ID + "'>" + IED.ExtraNotification + "</div>")
            }
        }

        this.CurrentTreatments = new Array();
        $('#ExtraNtificationList .TreatmentNotification').remove();
        $(".TreatmentBox:visible input[name='fldTreatment']:checked").each(function() {
            var TreatmentID = $(this).val();
            Calculator.CurrentTreatments.push(TreatmentID);
            var TD = IPS.Treatments[TreatmentID];
            if (TD.CalculatedPrice) {
                var TDPrice;
                if (Constants.MetricSystem) {
                    TDPrice = Calculator.PrintWidth.CmToMeter() * Calculator.PrintHeight.CmToMeter() * TD.Price;
                }
                else {
                    TDPrice = Calculator.PrintWidth.InchToFoot() * Calculator.PrintHeight.InchToFoot() * TD.Price;
                }
                Calculator.UnitPrice += TDPrice;
            }
            else {
                Calculator.UnitPrice += TD.Price;
            }

            if (TD.ExtraNotification != undefined && TD.ExtraNotification != null && TD.ExtraNotification.length > 0) {
                $('#ExtraNtificationList').append("<div class='ENC TreatmentNotification' id='TreatmentNotification_" + TD.ID + "'>" + TD.ExtraNotification + "</div>")
            }
        });

        this.UnitPrice = Math.ceil(this.UnitPrice);
        $("#fldUnitPrice").val((this.UnitPrice == 0) ? "" : this.UnitPrice);
        $("#fldTotalPrice").val((this.UnitPrice == 0) ? "" : (this.UnitPrice * this.Quantity));
        $("#fldUnitPriceText").val((this.UnitPrice == 0) ? "" : this.UnitPrice.formatMoney(2, '.', ','));
        $("#fldTotalPriceText").val((this.UnitPrice == 0) ? "" : (this.UnitPrice * this.Quantity).formatMoney(2, '.', ','));



        //alert(this.CurrentMaterialID);
        //alert(this.CurrentTreatmentID);
        //alert(this.CurrentPaperType);
        //alert(this.CurrentFrameType);
        //alert(this.CurrentWoodenFrameID);
        //alert(this.CurrentAlluminiumFrameID);        
        //alert(this.CurrentImageEffectID);
    },

    AddToCart: function() {
        if (!this.SizeLegal) return;
        this.CalculatePrice();
        var OrderItem = {};

        OrderItem.PrintWidth = this.PrintWidth;
        OrderItem.PrintHeight = this.PrintHeight;
        OrderItem.PrintWidth = this.PrintWidth;
        OrderItem.PrintHeight = this.PrintHeight;

        OrderItem.ImageFileName = this.ImageFileName;
        OrderItem.ImageWidth = this.ImageWidth;
        OrderItem.ImageHeight = this.ImageHeight;

        OrderItem.MaterialID = (this.CurrentMaterialID == null) ? -1 : this.CurrentMaterialID;
        OrderItem.PaperTypeID = (this.CurrentPaperType == null) ? -1 : this.CurrentPaperType;
        OrderItem.ImageEffectID = (this.CurrentImageEffectID == null) ? -1 : this.CurrentImageEffectID;
        OrderItem.WoodenFrameID = (this.CurrentWoodenFrameID == null) ? -1 : this.CurrentWoodenFrameID;
        OrderItem.AlluminiumFrameID = (this.CurrentAlluminiumFrameID == null) ? -1 : this.CurrentAlluminiumFrameID;
        OrderItem.Price = this.UnitPrice;
        OrderItem.Quantity = this.Quantity;
        OrderItem.Comments = $("#fldCalculatorComments").val();
        OrderItem.Treatments = this.CurrentTreatments;
		
        B2CPrint.G1.WebSite.ShoppingCart.Method.Add(OrderItem, this.AddToCartOnSucceeded, this.AddToCartOnFailed);
    },

    AddToCartOnSucceeded: function(data, args, methodName) {
        if (data.ActionSucceeded != undefined && !data.ActionSucceeded) {
            var MessageObject = {
                ErrorMessage: data.ErrorMessage,
                ErrorDescription: data.ErrorDescription,
                ErrorNumber: data.ErrorNumber,
                ErrorState: data.ErrorState,
                ErrorType: data.ErrorType,
                ErrorSource: data.ErrorSource
            };
            DisplayMessage(MessageObject);
        }
        else {
			if(Calculator.GetQueryStringParams("imageID")!="" || typeof(Calculator.GetQueryStringParams("imageID"))!="undefined")
				{
				window.location = "/ShoppingCart/?extu=" + unescape(Calculator.GetQueryStringParams("smallThumb"));
				}
			else	
				window.location = "/ShoppingCart/";
		}
    },

    AddToCartOnFailed: function(error, userContext, methodName) {
        var MessageObject = {
            ErrorMessage: error._message,
            ErrorDescription: error.timedOut ? Translate('"Common..Error.Timeout"') : error._message,
            ErrorID: error._statusCode,
            ErrorState: 1
        };
        DisplayMessage(MessageObject);
    },

    Initialize: function(MaterialID, ImageFileName, IsDemo) {
		
        this.IsDemo = IsDemo;
        this.ImageFileName = ImageFileName;

        if (IsDemo) {
            this.ImagePath = "/Calculator/";
            this.ImageWidth = 10000;
	    this.ImageHeight = 10000;
        }
        
        try
        {
			
			if((this.GetQueryStringParams("ID")=="" || typeof(this.GetQueryStringParams("ID"))=="undefined") && (Calculator.GetQueryStringParams("imageID")=="" || typeof(Calculator.GetQueryStringParams("imageID"))!="undefined"))
			{
					this.IsDemo=false;
					
					this.extImageID = this.GetQueryStringParams("imageID");
					this.extLargeThumb =  unescape(this.GetQueryStringParams("mediumThumb"));
					this.ImageFileName = this.GetQueryStringParams("imageID");
					this.ImageWidth = this.GetQueryStringParams("width");
					this.ImageHeight = this.GetQueryStringParams("height");
					this.ThumbImageWidth =  this.GetQueryStringParams("smallWidth");
					this.ThumbImageHeight =  this.GetQueryStringParams("smallHeight");
					this.ThumbImageExtUrl = unescape(this.GetQueryStringParams("smallThumb"));
			
					//this.MinimumPrintWidth =  this.GetQueryStringParams("smallHeight");
					//this.MaximumPrintWidth =  this.GetQueryStringParams("width");
					//this.MinimumPrintHeight =  this.GetQueryStringParams("smallHeight");
					//this.MaximumPrintHeight =  this.GetQueryStringParams("height");
					
					
					/*quertString+="title='<%=title%>'&";
					quertString+="description='<%=description%>'&";
					quertString+="editorial='<%=editorial%>'&";
					quertString+="keywords='<%=keywords%>'&";
					quertString+="smallThumb=<%=smallThumb%>&";
					quertString+="mediumThumb=<%=mediumThumb%>&";
					quertString+="small=<%=small%>&";	*/
			}
        }
        catch(err)
        {
        }
        
		if(Calculator.GetQueryStringParams("imageID")=="" || typeof(Calculator.GetQueryStringParams("imageID"))!="undefined")
		{
			$("#UploadedImageThumb").attr("src", this.extLargeThumb);
		}
		else
		{
			if(typeof(this.ImageFileName)=="undefined")this.ImageFileName = "DEMO"
			$("#UploadedImageThumb").attr("src", this.ImagePath + this.ImageFileName + "Thumb.jpg");
        }
         $("input[name='fldMaterial']").val(['1']);
        this.ChangeMaterial(MaterialID);
				
				if (IsDemo && (this.GetQueryStringParams("imageID")=="" || typeof(this.GetQueryStringParams("imageID"))=="undefined")) {
					this.MinimumPrintWidth = 18;
					this.MaximumPrintWidth = 1000;
					this.MinimumPrintHeight = 12;
					this.MaximumPrintHeight = 1000;
				}
				else
				{
				
					if(this.GetQueryStringParams("ID")=="" || typeof(this.GetQueryStringParams("ID"))=="undefined")
					{
					//this.MinimumPrintWidth =  this.GetQueryStringParams("smallHeight");
					//this.MaximumPrintWidth =  this.GetQueryStringParams("width");
					//this.MinimumPrintHeight =  this.GetQueryStringParams("smallHeight");
					//this.MaximumPrintHeight =  this.GetQueryStringParams("height");
					var MMPS = CalculateMinimumMaximumPrintSize(this.GetQueryStringParams("width"), this.GetQueryStringParams("height"));
					}
					else
					{
					var MMPS = CalculateMinimumMaximumPrintSize(this.ImageWidth, this.ImageHeight);
					}
					
					this.MinimumPrintWidth = MMPS.MinimumPrintWidth;
					this.MaximumPrintWidth = MMPS.MaximumPrintWidth;
					this.MinimumPrintHeight = MMPS.MinimumPrintHeight;
					this.MaximumPrintHeight = MMPS.MaximumPrintHeight;
				}
		
        this.PrintWidth = this.MinimumPrintWidth;
        this.PrintHeight = this.MinimumPrintHeight;

        $("input[name='fldWidth']").val(this.PrintWidth);
        $("input[name='fldHeight']").val(this.PrintHeight);

        $("#CalcMaximumPrintSizeWidth").html(this.MaximumPrintWidth);
        $("#CalcMaximumPrintSizeHeight").html(this.MaximumPrintHeight);

        this.SizeLegal = true;
      
		this.CalculatePrice();
		
	},

    ChangeFrameType: function() {

        $CurrentFrameType = $("input[name='fldFrameType']:checked");
        this.CurrentFrameType = ($CurrentFrameType.length == 0) ? null : parseInt($CurrentFrameType.val());

        if (this.CurrentFrameType == FrameType.Wooden) {
            $("#AlluminiumFrameContainer").hide();
            $("#WoodenFrameContainer").show();
        }
        else {
            $("#WoodenFrameContainer").hide();
            $("#AlluminiumFrameContainer").show();
        }

        this.CalculatePrice();
    },

    ChangePaperType: function() {

        $CurrentPaperType = $("input[name='fldTreatmentPaperType']:checked");
        this.CurrentPaperType = ($CurrentPaperType.length == 0) ? null : parseInt($CurrentPaperType.val());

        this.CalculatePrice();
    },

    ChangeWoodenFrame: function(ID) {
        this.RemoveFrame();
        this.CurrentWoodenFrameID = ID;

        this.BuildFramePreview(ID);

        var WFD = IPS.WoodenFrames[ID];
        for (var n = 0; n < WFD.TreatmentSettings.length; n++) {

            var $TreatmentBox = $("#Treatment_" + WFD.TreatmentSettings[n].ID + ":visible");
            if ($TreatmentBox.length > 0) {
                if (WFD.TreatmentSettings[n].Enabled) {
                    $("input[name='fldTreatment']", $TreatmentBox).attr("checked", "true");
                    $("input[name='fldTreatment']", $TreatmentBox).attr("disabled", "true");
                }
                else {
                    $("input[name='fldTreatment']", $TreatmentBox).removeAttr("checked");
                    $("input[name='fldTreatment']", $TreatmentBox).removeAttr("disabled");
                }
            }
        }

        this.CalculatePrice();
    },

    ChangeAlluminiumFrame: function(ID, HtmlColor) {
        this.RemoveFrame();
        this.CurrentAlluminiumFrameID = ID;

        var AFD = IPS.AlluminiumFrames[ID];
        $('#UploadedImageThumb').css("border", "7px solid #" + HtmlColor);
        for (var n = 0; n < AFD.TreatmentSettings.length; n++) {

            var $TreatmentBox = $("#Treatment_" + AFD.TreatmentSettings[n].ID + ":visible");
            $("input[name='fldTreatment']", $TreatmentBox).removeAttr("disabled");

            if ($TreatmentBox.length > 0) {
                if (AFD.TreatmentSettings[n].Enabled) {
                    $("input[name='fldTreatment']", $TreatmentBox).attr("checked", "true");
                    $("input[name='fldTreatment']", $TreatmentBox).attr("disabled", "true");
                }
                else {
                    $("input[name='fldTreatment']", $TreatmentBox).removeAttr("checked");
                    $("input[name='fldTreatment']", $TreatmentBox).attr("disabled", "true");
                }
            }
        }

        this.CalculatePrice();
    },

    ChangeImageEffect: function(ID) {
        this.CurrentImageEffectID = ID;
        $(".CalcImageEffectBox img").css("border", "none");
        $("#ImageEffect_" + ID + " img").css("border", "2px solid red");
        this.CalculatePrice();
    },

    ChangeTreatment: function(ID) {
        this.CurrentTreatmentID = ID;
        var TD = IPS.Treatments[ID];
        this.CalculatePrice();
    },

    ChangeMaterial: function(ID) {
        this.CurrentMaterialID = ID;
        this.CurrentWoodenFrameID = null;
        this.CurrentAlluminiumFrameID = null;
        var MD = IPS.Materials[ID];
        // TreatmentContainer
        var $TreatmentContainer = $("#TreatmentContainer");
        $(".TreatmentBox", $TreatmentContainer).hide();

        $("input[name='fldTreatment']", $TreatmentContainer).removeAttr("checked");
        $("input[name='fldTreatment']", $TreatmentContainer).removeAttr("disabled");

        $("input[name='fldTreatment']", $TreatmentContainer).val(['']);
        for (var n = 0; n < MD.TreatmentSettings.length; n++) {
            $("#Treatment_" + MD.TreatmentSettings[n].ID, $("#TreatmentContainer")).show();
        }
        this.CurrentTreatmentID = null;

        // TreatmentPaperTypeContainer
        var $TreatmentPaperTypeContainer = $('#TreatmentPaperTypeContainer');
        if (MD.AllowPaperTreatments) {
            this.CurrentPaperType = PaperType.Matt;
            $("input[name='fldTreatmentPaperType']", $TreatmentPaperTypeContainer).val(['1']);
            $TreatmentPaperTypeContainer.show();
        }
        else {
            CurrentPaperType = null;
            $("input[name='fldTreatmentPaperType']", $TreatmentPaperTypeContainer).val(['']);
            $TreatmentPaperTypeContainer.hide();
        }

        // FrameTypeContainer
        var $FrameTypeContainer = $('#FrameTypeContainer');
        if (!MD.AllowWoodenFrames && !MD.AllowAlluminiumFrames) {
            CurrentFrameType = null;
            $("input[name='fldFrameType']", $FrameTypeContainer).val(['']);
            $FrameTypeContainer.hide();
            $("#WoodenFrameContainer").hide();
            $("#AlluminiumFrameContainer").hide();
        }
        else if (MD.AllowWoodenFrames) {
            CurrentFrameType = FrameType.Wooden;
            $("input[name='fldFrameType']", $FrameTypeContainer).val(['1']);
            $("#FrameTypeWoodenBox", $FrameTypeContainer).show();
            $FrameTypeContainer.show();
            $("#AlluminiumFrameContainer").hide();
            $("#WoodenFrameContainer").show();
        }
        else if (MD.AllowAlluminiumFrames) {
            if (!MD.AllowWoodenFrames) {
                CurrentFrameType = FrameType.Alluminium;
                $("input[name='fldFrameType']", $FrameTypeContainer).val(['2']);
                $("#FrameTypeWoodenBox", $FrameTypeContainer).hide();
                $("#WoodenFrameContainer").hide();
                $("#AlluminiumFrameContainer").show();
            }
            $("#FrameTypeAlluminiumBox", $FrameTypeContainer).show();
            $FrameTypeContainer.show();
        }
        this.RemoveFrame();
        this.CalculatePrice();
    },

    RemoveFrame: function() {
        this.CurrentWoodenFrameID = null;
        this.CurrentAlluminiumFrameID = null;

        $('.FrameImagePreviewContainer .TL').css('background-image', "none");
        $('.FrameImagePreviewContainer .TR').css('background-image', "none");
        $('.FrameImagePreviewContainer .BL').css('background-image', "none");
        $('.FrameImagePreviewContainer .BR').css('background-image', "none");
        $('.FrameImagePreviewContainer .TC').css('background-image', "none");
        $('.FrameImagePreviewContainer .RC').css('background-image', "none");
        $('.FrameImagePreviewContainer .BC').css('background-image', "none");
        $('.FrameImagePreviewContainer .LC').css('background-image', "none");

        $('.FrameImagePreview').css("height", "auto");
        $('.FrameImagePreview').css("width", "auto");

        $('.FrameImagePreviewContainer .BR').css("padding", "0px");
        $('.FrameImagePreviewContainer .BR').css("height", "auto");
        $('.FrameImagePreviewContainer .BR').css("width", "auto");

        $('#UploadedImageThumb').css("border", "none");

        var $TreatmentContainer = $("#TreatmentContainer");
        $("input[name='fldTreatment']:disabled", $TreatmentContainer).removeAttr("checked");
        $("input[name='fldTreatment']:disabled", $TreatmentContainer).removeAttr("disabled");

        this.CalculatePrice();

    },

    RemoveImageEffect: function() {
        $(".CalcImageEffectBox img").css("border", "none");
        this.CurrentImageEffectID = null;
        this.CalculatePrice();
    },

    BuildFramePreview: function(WoodenFrameID) {
        var UP = Constants.VirtualUploadPath + "WoodenFrame/";
        $('.FrameImagePreviewContainer .TL').css('background-image', "url('" + UP + WoodenFrameID + "TL.gif')");
        $('.FrameImagePreviewContainer .TR').css('background-image', "url('" + UP + WoodenFrameID + "TR.gif')");
        $('.FrameImagePreviewContainer .BL').css('background-image', "url('" + UP + WoodenFrameID + "BL.gif')");
        $('.FrameImagePreviewContainer .BR').css('background-image', "url('" + UP + WoodenFrameID + "BR.gif')");
        $('.FrameImagePreviewContainer .TC').css('background-image', "url('" + UP + WoodenFrameID + "TC.gif')");
        $('.FrameImagePreviewContainer .RC').css('background-image', "url('" + UP + WoodenFrameID + "RC.gif')");
        $('.FrameImagePreviewContainer .BC').css('background-image', "url('" + UP + WoodenFrameID + "BC.gif')");
        $('.FrameImagePreviewContainer .LC').css('background-image', "url('" + UP + WoodenFrameID + "LC.gif')");

        $('.FrameImagePreview').css("height", this.ThumbImageHeight + (IPS.WoodenFrames[WoodenFrameID].FrameGUISize * 2) + "px");
        $('.FrameImagePreview').css("width", this.ThumbImageWidth + (IPS.WoodenFrames[WoodenFrameID].FrameGUISize * 2) + "px");

        $('.FrameImagePreviewContainer .BR').css("padding", IPS.WoodenFrames[WoodenFrameID].FrameGUISize + "px");
        $('.FrameImagePreviewContainer .BR').css("height", this.ThumbImageHeight + "px");
        $('.FrameImagePreviewContainer .BR').css("width", this.ThumbImageWidth + "px");
    },
    
    GetQueryStringParams: function(ji) 
    {
		hu = window.location.search.substring(1);
		gy = hu.split("&");
		for (i=0;i<gy.length;i++) 
		{
			ft = gy[i].split("=");
			if (ft[0] == ji) 
				return ft[1];
		}
	}
	
    
}
