﻿//function to get any element postion
function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curtop, curleft];
}

//instead of location.href = x use this so that document.referer is correct for ie.
function redirect(url) { 
        if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
                var referLink = document.createElement('a'); 
                referLink.href = url; 
                document.body.appendChild(referLink); 
                referLink.click(); 
        } else { 
                location.href = url; 
        } 
} 

function createCenteredPopupDiv(innerHTML, bNotUsed, GivenTitle, functionOnClose){
	var div = null;
	div  = document.createElement("div");
	
	if(div==null)
		return null;

	div.innerHTML = innerHTML;
	div.id = "divDialog";
	document.body.appendChild(div);
	
	if (arguments.length > 3) {
	    if (functionOnClose == "goBack") {
	        $(div).dialog({ title: GivenTitle, beforeClose: function (event, ui) { history.go(-1); }, modal: true });
	    }
	    else if (functionOnClose == "goHome") {
	        $(div).dialog({ title: GivenTitle, beforeClose: function (event, ui) { location.href='../ShoppingCart/ShoppingCart.aspx'; }, modal: true });
	    }
	    else {
	        $(div).dialog({ title: GivenTitle, close: functionOnClose, modal: true, width: $(div).width(), height: $(div).height() });
	    }
	        
	}
	else {
		$(div).dialog({title:GivenTitle,modal:true});
	}
	

	
	return div;
}

function removeButtonPopup()
{
	$("#divDialog").dialog("destroy"); 
	document.body.removeChild(document.getElementById("divDialog"));
}


function dialogAlert(dialogTitle, content, dialogModal)
{
	divDialog = document.createElement("div");
	divDialog.id = "divDialog";
	divDialog.innerHTML = content;
	document.body.appendChild(divDialog);


	$("#divDialog").dialog({ title: dialogTitle, modal: dialogModal, close: closeDivDialog, width: 460 });
	
	
}
function closeDivDialog()
{
	$("#divDialog").dialog("destroy")
	divDialog = document.getElementById("divDialog");
	document.body.removeChild(divDialog);
}


function modalOverlay(content)
{
	divModalOverlay = document.createElement("div");
	divModalOverlay.id = "divModalOverlay";
	divModalOverlay.innerHTML = "&nbsp;"
	$(divModalOverlay).addClass("ui-widget-overlay")
	$(divModalOverlay).height($(document.body).height());
	$(divModalOverlay).hide();
	document.body.appendChild(divModalOverlay);
	$(divModalOverlay).show();
	divModalContent = document.createElement("div");
	divModalContent.id = "divModalContent";
	divModalContent.innerHTML = content;
	$(divModalContent).css("background-color", 'white');
	$(divModalContent).css("z-index", $(divModalOverlay).css("z-index")+1);
	$(divModalContent).css("position","absolute");
    $(divModalContent).css("top", ( $(window).height() - $(divModalContent).height() ) / 2+$(window).scrollTop() + "px");
    $(divModalContent).css("left", ( $(window).width() - $(divModalContent).width() ) / 2+$(window).scrollLeft() + "px");
   
	$(divModalContent).hide();
	document.body.appendChild(divModalContent);
	$(divModalContent).show();
}
function closeModalOverlay()
{

	divModalOverlay = document.getElementById("divModalOverlay");
	document.body.removeChild(divModalOverlay);
	
	divModalContent = document.getElementById("divModalContent");
	document.body.removeChild(divModalContent);
	
}

function winpop (url,windowName,w,h,scroll,resize,center) 
{
	if (center) {
		var winPos = ',top='+((screen.height - h) / 2)+',left='+((screen.width - w) / 2);
	}
	var scrollArg = (scroll == false) ? '' : ',scrollbars=1';
	var resizeArg = (resize == false) ? '' : ',resizable=1';
	flyout = window.open (url,windowName+scroll+resize+center,"width=" + w + ",height=" + h + scrollArg + resizeArg + winPos);
	flyout.resizeTo(w,h);
	flyout.focus();
}

function addRemoveFilter(element)
{
	filter = $(element).attr("name");
	if(element.checked)
	{
		if(location.href.indexOf("?") != -1)
		{
				
			filterName = filter.substring(0, filter.indexOf("="));
			if(location.href.indexOf(filterName) != -1)
			{
				startReplaceIndex = location.href.indexOf(filterName);
				endReplaceIndex = location.href.indexOf("&", startReplaceIndex)
				if(endReplaceIndex != -1)
				{
					replaceString = location.href.substring(startReplaceIndex, endReplaceIndex);
				}
				else
				{
					replaceString = location.href.substring(startReplaceIndex);
				}
				newURL = location.href.replace(replaceString, filter);
				if(newURL.indexOf("?") == newURL.length - 1)
				{
					newURL = newURL.substring(0, newURL.length - 1);
				}
				redirect(newURL);
			}
			else
			{
				redirect(location.href + "&" + filter);
			}
			
			
		}
		else
		{
			redirect(location.href + "?" + filter);
		}
		//location.href = location.href + "&" + filter;
	}
	else
	{
		newURL = location.href.replace("&" + filter, "").replace("?" + filter, "?").replace("?&","?");
		if(newURL.indexOf("?") == newURL.length - 1)
		{
			newURL = newURL.substring(0, newURL.length - 1);
		}
		redirect(newURL);
		//	redirect(location.href.replace("&" + filter, "").replace("?" + filter, "?").replace("?&","?"));
	}
}



function GetString(stringName) {
    if (stringName.length > 0 && isDefined("GlobalStrings." + stringName)) {
		return eval("GlobalStrings." + stringName);
	}
	else {
		return "N/A";
	}
}

function isDefined(variable)
{
	return eval("(typeof("+variable+") != 'undefined');");
}


//fix safari and chrome padding problem
function paddingFix4Safari() {
    if (jQuery) {
        $(document).ready(function () {
            paddingFix();
        });
    }
    else {
        paddingFix();
    }
}

function paddingFix() {
     //check if its safari/chrome/iphone to run the padding fix
    if (navigator.userAgent.indexOf('Safari') != -1 || navigator.userAgent.indexOf('iPhone') != -1) {
        for (var i = 0; i < document.styleSheets.length; i++) {
            var mysheet = document.styleSheets[i];
            var myrules = mysheet.cssRules ? mysheet.cssRules : mysheet.rules;
            for (var j = 0; j < myrules.length; j++) {
                //only fix the searchbox
                if (myrules[j].style != undefined && myrules[j].selectorText.indexOf("divSearchSubject") != -1)
                {
                    if (!isNaN(parseInt(myrules[j].style.height)) && myrules[j].style.height.indexOf('px') > 0) {
                        if (parseInt(myrules[j].style.paddingBottom) > 0 && !isNaN(parseInt(myrules[j].style.paddingBottom))) {
                            myrules[j].style.height = (parseInt(myrules[j].style.height) + parseInt(myrules[j].style.paddingBottom)) + 'px';
                        }
                        if (parseInt(myrules[j].style.paddingTop) > 0 && !isNaN(parseInt(myrules[j].style.paddingTop))) {
                            myrules[j].style.height = (parseInt(myrules[j].style.height) + parseInt(myrules[j].style.paddingTop)) + 'px';
                        }
                    }
                    if (!isNaN(parseInt(myrules[j].style.width)) && myrules[j].style.width.indexOf('px') > 0) {
                        if (parseInt(myrules[j].style.paddingLeft) > 0 && !isNaN(parseInt(myrules[j].style.paddingLeft))) {
                            myrules[j].style.width = (parseInt(myrules[j].style.width) + parseInt(myrules[j].style.paddingLeft)) + 'px';
                        }
                        if (parseInt(myrules[j].style.paddingRight) > 0 && !isNaN(parseInt(myrules[j].style.paddingRight))) {
                            myrules[j].style.width = (parseInt(myrules[j].style.width) + parseInt(myrules[j].style.paddingRight)) + 'px';
                        }
                    }
                }
            }
        }
    }
}




function printBody(bIsFrench) {
    printTXT = GetString('str_Print_Print'); //getString('print', bIsFrench);
    closeText = GetString('str_Print_Close'); //getString('close', bIsFrench);


    newwindow2 = (window.open('', printTXT, 'resizable=1,scrollbars=1,width=' + $("body").width() * 2 / 3));
    var tmp = newwindow2.document;
    windowHMLT = '';
    windowHMLT = windowHMLT + ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type">');
    $("link").each(function (index) {
        windowHMLT = windowHMLT + ('<link rel="stylesheet" href="' + $(this).attr("href") + '">');
    });
    $("script").each(function (index) {

        typeValue = '';
        srcValue = '';
        languageValue = '';
        htmlValue = '';
        if ($(this).attr("type") != undefined && $(this).attr("type") != '') {
            typeValue = 'type="' + $(this).attr("type") + '"%20';
        }
        if ($(this).attr("language") != undefined && $(this).attr("language") != '') {
            languageValue = 'language="' + $(this).attr("language") + '"%20';
        }
        if ($(this).attr("src") != undefined && $(this).attr("src") != '') {
            srcValue = ' src="' + $(this).attr("src") + '"%20';
        }
        if ($(this).html() != undefined && $(this).html() != '') {
            htmlValue = unescape($(this).html());
        }
        if (htmlValue.indexOf("$(document).ready") != -1) {
            return true;
        }

        windowHMLT = windowHMLT + (unescape('%3Cscript%20' + typeValue + languageValue + srcValue + '%3E' + htmlValue + '%3C/script%3E'));
    });
    windowHMLT = windowHMLT + ('</head><body style="background-color:white;">');
    windowHMLT = windowHMLT + '<table width="100%"><tr><td align="right">' + createPrintButton(bIsFrench);
    windowHMLT = windowHMLT + ('</td></tr></table>');
    if ($("#divContent").length > 0) {
        windowHMLT = windowHMLT + ($("#divContent").html());
    }
    else if ($("#divFilteredProducts").length > 0) {
        windowHMLT = windowHMLT + ($("#divFilteredProducts").html());
    }
    else if ($("#divProduct").length > 0) {
        windowHMLT = windowHMLT + ($("#divProduct").html());
    }
    else if ($(".divContent").length > 0) {
        windowHMLT = windowHMLT + ($(".divContent").html());
    }
    else if ($("#divPageBodyNormal").length > 0) {
        windowHMLT = windowHMLT + ($("#divPageBodyNormal").html());
    }
    else if ($("#divPageBodyBreadcrumb").length > 0) {
        windowHMLT = windowHMLT + ($("#divPageBodyBreadcrumb").html());
    }
    windowHMLT = windowHMLT + ('</body></html>');
    tmp.write(windowHMLT);
    tmp.close();


}


function addPrintToDialog(dialogFilter, preHTML, bIsFrench ) {
    if ($(dialogFilter).parent().find(".ui-dialog-titlebar-print").length == 0) {
        printTXT = GetString('str_Print_Print'); //getString('print', bIsFrench);
        

        //$(dialogFilter).parent().find(".ui-dialog-titlebar-close").before('<a href="#" class="ui-dialog-titlebar-print ui-corner-all" role="button" unselectable="on" ><span class="ui-icon ui-icon-print" unselectable="on" >print</span></a>');
        $(dialogFilter).parent().find(".ui-dialog-titlebar-close").before('<span class="ui-dialog-titlebar-print ui-corner-all"><img border="0" alt="" src="/Images/Icon_Print_en_UP.gif"><a class="divLanguagehover">&nbsp;' + printTXT + '</a></span>');
        $(dialogFilter).parent().find(".ui-dialog-titlebar-print").click(function () {
            printDialog(dialogFilter, preHTML, bIsFrench);
        });
    }
}

function printDialog(dialogFilter, preHTML, bIsFrench) {

    printTXT = GetString('str_Print_Print'); //getString('print', bIsFrench);
    closeText = GetString('str_Print_Close'); //getString('close', bIsFrench);

    newwindow2 = window.open('', printTXT, 'resizable=1,scrollbars=1,width=500px,height=' + $(dialogFilter).height()*2 + 'px');
    var tmp = newwindow2.document;
    windowHMLT = '';
    windowHMLT = windowHMLT + ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type">');
    $("link").each(function (index) {
        windowHMLT = windowHMLT + ('<link rel="stylesheet" href="' + $(this).attr("href") + '">');
    });

    $("script").each(function (index) {

        typeValue = '';
        srcValue = '';
        languageValue = '';
        htmlValue = '';
        if ($(this).attr("type") != undefined && $(this).attr("type") != '') {
            typeValue = 'type="' + $(this).attr("type") + '"%20';
        }
        if ($(this).attr("language") != undefined && $(this).attr("language") != '') {
            languageValue = 'language="' + $(this).attr("language") + '"%20';
        }
        if ($(this).attr("src") != undefined && $(this).attr("src") != '') {
            srcValue = ' src="' + $(this).attr("src") + '"%20';
        }
        if ($(this).html() != undefined && $(this).html() != '') {
            htmlValue = unescape($(this).html());
        }

        if (htmlValue.indexOf("$(document).ready") != -1) {
            return true;
        }

        windowHMLT = windowHMLT + (unescape('%3Cscript%20' + typeValue + languageValue + srcValue + '%3E' + htmlValue + '%3C/script%3E'));
    });
    windowHMLT = windowHMLT + ('</head><body style="background-color:white;">');
    windowHMLT = windowHMLT + '<table width="100%"><tr><td align="right">' + createPrintButton(bIsFrench);
    windowHMLT = windowHMLT + ('</td></tr></table>');
    windowHMLT = windowHMLT + preHTML + $(dialogFilter).html();
    windowHMLT = windowHMLT + ('</body></html>');
    tmp.write(windowHMLT);
    tmp.close();

}

function createPrintButton(bIsFrench) {
   // return '<a href="javascript:;" onclick="javascript:window.print();" onmouseover="MM_swapImage(\'Button_Print\',\'\',\'/Images/Buttons//Button_Print_' + (bIsFrench ? 'fr' : 'en') + '_OVR.gif\',1)" onmouseout="MM_swapImgRestore()" id="Button_Print_link"><img border="0" alt="" id="Button_Print" src="/Images/Buttons//Button_Print_' + (bIsFrench ? 'fr' : 'en') + '_UP.gif"></a>
    return '<span id="printToHide"><a href="javascript:;" onclick="javascript:hidePrint();window.print();" onmouseover="MM_swapImage(\'Icon_Print\',\'\',\'/Images/Icon_Print_en_OVR.gif\',1)" onmouseout="MM_swapImgRestore()" id="Icon_Print_link"><img border="0" alt="" id="Icon_Print" src="/Images/Icon_Print_en_UP.gif"></a>&nbsp;<a onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'Icon_Print\',\'\',\'/Images//Icon_Print_en_OVR.gif\',1)" href="javascript:hidePrint();window.print();" class="alignTop"><b>' + GetString('str_Print_Print') + '</b></a><span>';
}

function hidePrint() {
    $("#printToHide").hide();
}
function showPrint() {
    $("#printToHide").show();
}


