﻿// [dFilter] - A Numerical Input Mask for JavaScript
// Written By Dwayne Forehand - March 27th, 2003
// Please reuse & redistribute while keeping this notice.

var dFilterStep

function dFilterStrip(dFilterTemp, dFilterMask) {
	dFilterMask = replace(dFilterMask, '#', '');
	for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++) {
		dFilterTemp = replace(dFilterTemp, dFilterMask.substring(dFilterStep, dFilterStep + 1), '');
	}
	return dFilterTemp;
}

function dFilterMax(dFilterMask) {
	dFilterTemp = dFilterMask;
	for (dFilterStep = 0; dFilterStep < (dFilterMask.length + 1); dFilterStep++) {
		if (dFilterMask.charAt(dFilterStep) != '#') {
			dFilterTemp = replace(dFilterTemp, dFilterMask.charAt(dFilterStep), '');
		}
	}
	return dFilterTemp.length;
}

function dFilter(key, textbox, dFilterMask) {
	dFilterNum = dFilterStrip(textbox.value, dFilterMask);

	if (key == 9) {
		return true;
	}
	else if (key == 8 && dFilterNum.length != 0) {
		dFilterNum = dFilterNum.substring(0, dFilterNum.length - 1);
	}
	else if (((key > 47 && key < 58) || (key > 95 && key < 106)) && dFilterNum.length < dFilterMax(dFilterMask)) {
		dFilterNum = dFilterNum + String.fromCharCode(key);
	}

	var dFilterFinal = '';
	for (dFilterStep = 0; dFilterStep < dFilterMask.length; dFilterStep++) {
		if (dFilterMask.charAt(dFilterStep) == '#') {
			if (dFilterNum.length != 0) {
				dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
				dFilterNum = dFilterNum.substring(1, dFilterNum.length);
			}
			else {
				dFilterFinal = dFilterFinal + "";
			}
		}
		else if (dFilterMask.charAt(dFilterStep) != '#') {
			dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep);
		}
		//		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
	}


	textbox.value = dFilterFinal;
	return false;
}

function replace(fullString, text, by) {
	// Replaces text with by in string
	var strLength = fullString.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) return fullString;

	var i = fullString.indexOf(text);
	if ((!i) && (text != fullString.substring(0, txtLength))) return fullString;
	if (i == -1) return fullString;

	var newstr = fullString.substring(0, i) + by;

	if (i + txtLength < strLength)
		newstr += replace(fullString.substring(i + txtLength, strLength), text, by);

	return newstr;
}

function DisableSubmitButtonClick(field) {
	field.disabled = true; field.value = 'Processing...';
}

this.ImagePreview = function() {
	/* CONFIG */

	xOffset = 10;
	yOffset = 30;

	imageWidth = 400;
	imageHeight = 400;

	// these 2 variable determine popup's distance from the cursor
	// you might want to adjust to get the right result

	/* END CONFIG */
	$("a.screenshot").hover(function(e) {
		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";

		$("body").append("<p id='screenshot'><img src='" + this.rel + "' alt='url preview' />" + c + "</p>");
		var img = new Image();
		img.src = this.rel;


		screenWidth = screen.width;
		screenHeight = screen.height;

		pointerX = e.pageX;
		pointerY = e.pageY;

		differenceX = screenWidth - pointerX;
		differenceY = screenHeight - pointerY;

		imageHeight = img.height + 125;
		/// case 1 : differenceY > img.height && differenceX > img.width
		if (differenceY > imageHeight && differenceX > img.width) {
			xOffset = 10;
			yOffset = 30;
			$("#screenshot")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
		}
		/// case 2 : differenceY <=  img.height && differenceX > img.width
		else if (differenceY <= imageHeight && differenceX > img.width) {
			xOffset = 10 + img.height;
			yOffset = -30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}
		/// case 3 : differenceY <=  img.height && differenceX <= img.width
		else if (differenceY <= imageHeight && differenceX <= img.width) {
			xOffset = 10 + img.height;
			yOffset = img.width + 30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}
		else {
			/// case 4 : differenceY >  img.height && differenceX <= img.width
			xOffset = 10;
			yOffset = img.width + 30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}

		$("#screenshot").fadeIn("fast");
	},
	function() {
		this.title = this.t;
		$("#screenshot").remove();
	});

	$("a.screenshot").mousemove(function(e) {

		this.t = this.title;
		this.title = "";
		var c = (this.t != "") ? "<br/>" + this.t : "";

		var img = new Image();
		img.src = this.rel;

		screenWidth = screen.width;
		screenHeight = screen.height;

		pointerX = e.pageX;
		pointerY = e.pageY;

		differenceX = screenWidth - pointerX;
		differenceY = screenHeight - pointerY;

		imageHeight = img.height + 125;

		///alert("  img.width : " + img.width + " screenWidth " + screenWidth + " pointerX  " + pointerX + " differenceX " + differenceX);

		/// case 1 : differenceY > img.height && differenceX > img.width
		if (differenceY > imageHeight && differenceX > img.width) {
			//alert("Case 1");
			xOffset = 10;
			yOffset = 30;
			$("#screenshot")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px")
		}
		/// case 2 : differenceY <=  img.height && differenceX > img.width
		else if (differenceY <= imageHeight && differenceX > img.width) {
			//alert("Case 2");
			xOffset = 10 + img.height;
			yOffset = -30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}
		/// case 3 : differenceY <=  img.height && differenceX <= img.width
		else if (differenceY <= imageHeight && differenceX <= img.width) {
			//alert("Case 3");
			xOffset = 10 + img.height;
			yOffset = img.width + 30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}
		else {
			/// case 4 : differenceY >  img.height && differenceX <= img.width
			//alert("Case 4");
			xOffset = 10;
			yOffset = img.width + 30;
			$("#screenshot")
			    .css("top", (e.pageY - xOffset) + "px")
			    .css("left", (e.pageX - yOffset) + "px")
		}

	});
};

var PageDisplay = {
	ShowHideMenu: function(linkId, divId, cssId, leftVal, topVal) {
		var isVisible = false;
		var isDivHoverRegistered = false;
		/// Add hover effects to menu image 
		$("#" + linkId).hover(
          function() {

          	isVisible = $(divId).is(":visible");

          	var pos = $("#" + linkId).offset();

          	$("#" + divId).css({ 'left': pos.left - leftVal, 'top': (pos.top + topVal) });
          	isVisible = setTimeout(function() { $("#" + divId).fadeIn("slow"); }, 100);

          },
          function() {
          	isVisible = setTimeout(function() {

          		if (isDivHoverRegistered == false) {
          			$("#" + divId).fadeOut("slow");
          		}
          	}, 500);

          }
        );

		/// Add hover effects to menu image 
		$("#" + divId).hover(
            function() {
            	isDivHoverRegistered = true;
            	$("#" + linkId).removeClass(cssId);
            	$("#" + linkId).toggleClass(cssId + "_hover", true);
            },
            function() {
            	isVisible = setTimeout(function() {
            		$("#" + divId).fadeOut("slow");
            	}, 500);

            	isDivHoverRegistered = false;
            	$("#" + linkId).removeClass(cssId + "_hover", true);
            	$("#" + linkId).toggleClass(cssId);
            }
        );
	},
	AdditionalImagePreview: function(hoverImageId, sourceImageId, imageId) {


	$("#" + hoverImageId).hover(
            function() {
	        	$("#" + sourceImageId).attr("src", "/ImageDisplay.aspx?a=1&id=" + imageId);
            },
            function() {

            }
        );

	}
}

function HideModalPopup(popup) {
	$find(popup).hide();
}

function DisplayModalPopup(ctrl, id, name,  idCtrl, nameCtrl, popup) {
    $("#" + idCtrl).val(id);
	$("#" + nameCtrl).html(name);
    $find(popup).show();	
}

function AssociateProductFlags(checkboxlistID, productID, linkID)
{
    var names = new Array();
    var counter = 0;   
    
    $("input[type=checkbox][checked]").each(
    function() {
    names[counter] = $(this).next().text();
    
    counter = counter+1;
    });
    $("#flags" + productID).text(names.toString().replace(",",", "));
 
  eCommerce.WebSite.WebServices.eCommerceWebService.AssociateProductFlags(names,productID, FlagsSucceededCallback, FlagsFailedCallback, linkID);
  
  
}

function FlagsSucceededCallback(result, linkID) {
    $("#"+linkID).attr("style","background-color:#e8e4f1;"); 
    $("#"+linkID).fadeTo('slow', 0.1, function() {
        $("#"+linkID).attr("style","color:black;"); 
    });
//    $("#"+linkID).attr("onclick","");
//    $("#"+linkID).text("Added");
}

function FlagsFailedCallback(error) {
    alert("failed" + error);
}

function AssociateProductOccasions(checkboxlistID, productID, linkID)
{
 var names = new Array();
 var counter = 0;   
 $("input[type=checkbox][checked]").each(
  function() {
    names[counter] = $(this).next().text();
    counter = counter+1;
  });
 
  eCommerce.WebSite.WebServices.eCommerceWebService.AssociateProductOccasions(names,productID, OccasionsSucceededCallback, OccasionsFailedCallback, linkID);
  
  
}

function OccasionsSucceededCallback(result, linkID) {
    $("#"+linkID).attr("style","background-color:#e8e4f1;"); 
    $("#"+linkID).fadeTo('slow', 0.1, function() {
        $("#"+linkID).attr("style","color:black;"); 
    });
    $("#"+linkID).attr("onclick","");
    $("#"+linkID).text("Added");
}

function OccasionsFailedCallback(error) {
    alert("failed" + error);
}

