﻿function getElement(id)
{
	return document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
}

function setText(el, txt)
{
	if (document.all)
		el.innerText = txt;
	else
		el.textContent = txt;
}

function getText(el)
{
	return document.all ? el.innerText : el.textContent;
}

function getElementStyle(el, hyphenName)
    {
        // must use this function to read applied style (.style only returns the style set inline via style tags or script)
        // ie also needs hyphens removed and uppcase on first char after hyphen
        if (el.currentStyle) // ie uses current style
        {                
            var ieName;
            if (hyphenName.indexOf('-')!= -1)
            {
                var sArray; 
                sArray = hyphenName.split('-');
                ieName = sArray[0];
                for(var i=1;i < sArray.length;i++)
                {
                    ieName=ieName + sArray[i].charAt(0).toUpperCase() + sArray[i].substring(1);
                }                
            }
            else
            {
                ieName = hyphenName;
            }
            return el.currentStyle[ieName];
        }
        else if (window.getComputedStyle) // mozilla use getComputedStyle
        {
            var elstyle=window.getComputedStyle(el, '');
            return elstyle.getPropertyValue(hyphenName);
        }
        else // this should probably never happen
        {
            return el.style[hyphenName]        
        }
    }
    
    function getLeft(element)
    {
        return parseInt(getElementStyle(element,'left'));
    }
    
    function setLeft(element, value)
    {
        element.style.left = value + 'px';
    }
    
    function getTop(element)
    {
        return parseInt(getElementStyle(element,'top'));
    }
    
    function setTop(element, value)
    {
        element.style.top = value + 'px';
    }
    
    function setOpacity(element, value)
    {
		if(value > 100)
			value = 100;
		if(value < 0 )
			value = 0;
		if (element.filters && element.filters.item("DXImageTransform.Microsoft.Alpha")) //ie
		{
			element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = value;
			element.filters.item("DXImageTransform.Microsoft.Alpha").enabled = value != 100; // disable opacity filter if 100% (makes text clearer)
		}
		else if (getElementStyle(element, 'opacity')) //mozilla
		{
			element.style.opacity = (value / 100) + '';
		}
    }
    
    function getOpacity(element)
    {
        if (element.filters && element.filters.item("DXImageTransform.Microsoft.Alpha"))
        {
            return parseInt(element.filters.item("DXImageTransform.Microsoft.Alpha").opacity);
        }
        else if (getElementStyle(element, 'opacity')) //mozilla
        {
           return parseInt(parseFloat(getElementStyle(element, 'opacity'))*100);
        }
        return 100;
    }

	function FadeElement(elementId, targetOpacity, callBackFunction, step, frequency, priority, delay, linkedElements)
    {
		var element = getElement(elementId);
		if (element==null) return;

		if (!element.fader) {
			element.fader = {};
			element.fader.element = element;
			element.fader.elementId = elementId;
			element.fader.fade = function() {
				if (!this.active)
					return;
				this.currentOpacity += this.step * (this.targetOpacity - this.currentOpacity);
				this.currentOpacity = Math.min(Math.max(0,this.currentOpacity), 100);
				var intOpp = Math.round(this.currentOpacity);
				setOpacity(this.element, intOpp);
				for(var i=0; i<this.linkedElements.length; i++) {
					var item = this.linkedElements[i];
					var ele = getElement(item.elementId);
					var eleOpacity = (typeof(item.factor)=="undefined"?1:item.factor) * this.currentOpacity + (typeof(item.offset)=="undefined"?0:item.offset);
					eleOpacity = Math.min(Math.max(0,eleOpacity), 100);
					if (ele)
						setOpacity(ele, Math.round(eleOpacity));
				}
				if (intOpp!=this.targetOpacity) {
					this.delayFade(this.frequency);
				} else {
					element.fader.active = false;
					clearTimeout(element.fader.timer);
					if (typeof(this.callBackFunction)=="function")
						this.callBackFunction(this.element);
				}
			};
			element.fader.delayFade = function(delay) {
				this.timer = setTimeout("getElement('" + this.elementId + "').fader.fade()", delay);
			};
		} else {
			if (priority)
				clearTimeout(element.fader.timer);
		}

		if (frequency==null) frequency = 33;
		if (step==null) step = 0.4;
		step = Math.max(0.01, step);

		element.fader.callBackFunction = callBackFunction;
		element.fader.linkedElements = linkedElements==null?[]:linkedElements;
		element.fader.targetOpacity = Math.round(Math.min(Math.max(0,targetOpacity), 100));
		element.fader.currentOpacity = getOpacity(element);
		element.fader.frequency = frequency;
		element.fader.step = step;
		element.fader.active = true;
		if (delay) {
			element.fader.delayFade(delay);
		} else {
			element.fader.fade();
		}
    }

printf = function()
{
	var num = arguments.length;
	var oStr = arguments[0];
	for (var i = 1; i < num; i++)
	{
		var pattern = "\\{" + (i-1) + "\\}";
		var re = new RegExp(pattern, "g");
		oStr = oStr.replace(re, arguments[i]);
	}
	return oStr;
}

// Debug instance
var Debug = {
	Print : function(message) {
		if (typeof(this.LoggerUrl)=="string") {
			if (typeof(Ajax)=="object" && typeof(Ajax.Request)=="function") {
				new Ajax.Request(this.LoggerUrl, {
					method: 'post',
					postBody: "timestamp=" + new Number(new Date()) + "&message=" + escape(message)
				} );
			}
		}
	}
};

function advFixLinks()
{
	try
	{
		// fix sna masthead menu links
		var as = $('masthead').getElementsBySelector('a');
		as.each( function(linkEle, idx) {
			var href = linkEle.readAttribute('href');
			if (href != null && href.startsWith("/sna/"))
				linkEle.href = "http://accessories.us.dell.com" + href;
		} );

		// fix premier links
		var isPrem = window.location.pathname.toLowerCase().indexOf('/premier') != -1;
		if (!isPrem)
		{
			var premToWwwFx = function(linkEle, idx)
			{
				var href = linkEle.readAttribute('href');
				if (href != null && href.length > 0)
					linkEle.href = linkEle.href.replace("://premier.dell.com/", "://www.dell.com/");
			}

			// fix breadcrumb links
			$$('a.lnk_crumb43').each( premToWwwFx );
			// fix footer links
			$$('p.para_small a').each( premToWwwFx );
		}
		if (typeof(fixLinksDone)=="undefined")
			setTimeout('advFixLinks()', 200);
	}
	catch(e) {}

	window.fixLinksDone = true;
}

// Modal Dialog

ModalDialog = {
	body : null,
	show : function(setup) {
		// init if needed
		if (!this.inited)
		{
			jQuery('body').append('\
				<div id="genericModalDialogContainer" style="display:none" style="padding:0">\
					<div class="modalDialog" id="genericModalDialog" style="padding:0">\
						<div class="modalDialogHeader"></div>\
						<div class="modalDialogBody"></div>\
						<div class="modalDialogButtons" style="text-align:right"></div>\
					</div>\
				</div>');
			this.inited = true;
		}

		var dlg = jQuery('#genericModalDialog');
		var btnsBox = dlg.find('.modalDialogButtons');
		
		// set title and body
		dlg.find('.modalDialogHeader').html(setup.title);
		dlg.find('.modalDialogBody').html(setup.body);
		
		// remove any old buttons
		btnsBox.find('span')
			.unbind()
			.remove();

		// add buttons
		for (var btnIdx = 0; btnIdx < setup.buttons.length; btnIdx++)
		{
			var btn = setup.buttons[btnIdx];

			// add spacer if needed
			if (btnIdx > 0)
				btnsBox.append('<span style="float:left">&nbsp;</span>');

			// add basic html
			btnsBox.append('\
				<span>\
					<div class="btnL"></div>\
					<div class="btnM"></div>\
					<div class="btnR"></div>\
				</span>');

			// set text, style, and click handler
			btnsBox.find('span:last')
				.unbind()
				.bind('click', typeof(btn.click)=="function" ? btn.click : ModalDialog.hide )
				.addClass('btn' + btn.style)
				.find('div.btnM').text(btn.text);
		}

		if (setup.url != null)
			tb_show("", setup.url + "?TB_iframe=true&height=" + setup.height + "&width=" + setup.width + "&inlineId=genericModalDialogContainer&xxmodal=true", null);
		else
			tb_show("", "#TB_inline?height=" + setup.height + "&width=" + setup.width + "&inlineId=genericModalDialogContainer&modal=true", null);

		// disable click-background-to-close
		jQuery("#TB_overlay").unbind();

		ModalDialog.body = dlg.find('.modalDialogBody');
		ModalDialog.size();
	},
	hide : function() {
		tb_remove();
	},
	size : function() {
		var md = jQuery('div.modalDialog');
		var tb = md.parent();
		var totH = tb.height();
		totH -= 45; // account for padding
		var bodyH = totH - md.find('.modalDialogHeader').height() - md.find('.modalDialogButtons').height();
		md.find('.modalDialogBody').height(bodyH);
		tb.css("overflow", "hidden");
	}	
};

function ModalDialogSetup(title, body, width, height)
{
	this.title = title;
	this.body = null;
	this.url = null;
	if (body.substring(0,7)=="http://" || body.substring(0,7)=="https://")
		this.url = body;
	else
		this.body = body;
	this.width = width==null ? 415 : width;
	this.height = height==null ? 180 : height;
	this.buttons = [];

	this.addButton = function(text, style, click) {
		var buttonDef = {
			text: text,
			style: style,
			click: click
			};
		this.buttons.push(buttonDef);
	};
}

function showModalTitleUrl(title, url)
{
	// set title, url, width, height
    var dialog = new ModalDialogSetup(title, url, 800, 600);
    
    // show
    ModalDialog.show(dialog);

	// add maximize button
	jQuery('#TB_closeAjaxWindow').append('<img src="images/expand.0.gif" onmouseover="this.src=this.src.replace(\'.0.\',\'.1.\')" onmouseout="this.src=this.src.replace(\'.1.\',\'.0.\')" alt="maximize" style="padding-left:10px" onclick="openNewWindow(\'TB_iframeContent\');" />');
    
    // return false to prevent navigation to link
}

function openNewWindow(frameName) {
    document.domain = "dell.com";
    var obj = document.getElementById(frameName);//window.frames[frameName];
    window.location = window.frames[obj.name].location;
}

function makeWin2(url, p_Width, p_Height) {
	params  = GetWindowOpenParams(92, 0, p_Width, p_Height);
	ProcessOpenWindow(url, "Sitelet", params, false);
}

function OpenWindow(url, windowName, params, noCaching)
{
	if (noCaching == true)
		return window.open(url + '&nc=true', windowName , params);
	else
		return window.open(url, windowName , params);
}

function ProcessOpenWindow(url, windowName, params, noCaching)
{
	agent = navigator.userAgent;
	try{
	// close the window to vary the window size
	if (typeof(win) == "object" && !win.closed){win.close();}
	}catch(e){}
	win = OpenWindow(url, windowName, params, noCaching);
	if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) 
	win = OpenWindow(url, windowName, params, noCaching);
	try{
		if (!win.opener) {
			win.opener = window;
		}
	}catch(e){}
  	// bring the window to the front
	try{
		win.focus();	
	}catch(e){}
}

function GetWindowOpenParams(top, left, width, height)
{
	params = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,";
	params += "top=" + top + ",";
	params += "left=" + left + ",";
	if (width) {
	 	params += "width=" + width+ ",";
		params += "height=" + height;
	}
	else {
		params += "width=590,height=350";
	}
	return params;
}

var lastInputServer;
var lastServerTip;
var isMiniAdvisor = false;

function SetMiniAdvisor()
{
    isMiniAdvisor = true;
}

function ShowPopup(idName, inputServer, event)
{
    if(isMiniAdvisor)
        return;
        
    var peStr = inputServer.substring(0,2);
    if(peStr.toUpperCase() != "PE")
        return;   

    //SetPopupPosition(event, 0, 0);

    if(lastInputServer == inputServer)
    {
        document.getElementById("Popup").innerHTML = lastServerTip;
        document.getElementById("Popup").style.visibility = "visible";
        return;
    }
        
    var inputPara = "ServerName=" + inputServer;
    //document.getElementById("Popup").style.top=document.getElementById(idName).offsetTop + document.getElementById(idName).offsetHeight - ;
    //document.getElementById("Popup").style.left=document.getElementById(idName).offsetLeft + 130;
    new Ajax.Request("ProductTipAjax.aspx", 
    {
        method: 'post', 
        postBody: inputPara,
        onSuccess: function(msg) 
        {
            try
            {
                document.getElementById("Popup").innerHTML = msg.responseText;
                document.getElementById("Popup").style.visibility = "visible";
                lastServerTip = msg.responseText;
                lastInputServer = inputServer;
            }
            catch(e)
            {
                return{};
            }
        },
       onFailure: function() 
       {
           document.getElementById("Popup").innerHTML = ""; 
           document.getElementById("Popup").style.visibility = "hidden";
       }
    });
}

function ShowPopup2(msg, event)
{
    if(!isMiniAdvisor)
        return;

    if(msg == "")
        return;
        
    //SetPopupPosition(event, 0, 0);
    var result = "<html xmlns=\"http://www.w3.org/1999/xhtml\" >";
    //position:absolute;top:0px;left:180px;width:120px;height:240px; 
    result+= "<div id=\"tip1\" style=\"width:128px; border:0px solid #CCC; background:#f4daba; color:#de8417\">";
    result+= msg;
    result+="</div></body></html>";
    document.getElementById("Popup1").innerHTML = result;
    document.getElementById("Popup1").style.visibility = "visible";
}

function SetPopupPosition(event, offsetX, offsetY)
{
    // Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
    var IE = document.all?true:false;
    var tempX;
    var tempY;

    if (IE) 
    { // grab the x-y pos.s if browser is IE
        tempX = event.x + document.body.scrollLeft - event.offsetX;
        tempY = event.y + document.body.scrollTop - event.offsetY;
    } else 
    {  // grab the x-y pos.s if browser is NS
        tempX = event.pageX;
        tempY = event.pageY;
    }  
    // catch possible negative values in NS4
    if (tempX < 0){tempX = 0;}
    if (tempY < 0){tempY = 0;} 
    
    //set to zero if negative
    tempY=tempY+190;
    tempX=tempX+675;
//    if (tempX < 0){tempX = offsetX;}
//    if (tempY < 0){tempY = 0;} 
    
    var questionEle = document.getElementsByClassName("stepQuestion");
    //document.getElementById("Popup").style.top=document.getElementById(idName).offsetTop + tempY - 380;
    //document.getElementById("Popup").style.left=document.getElementById(idName).offsetLeft + tempX - 620;
    document.getElementById("Popup").style.posTop=tempY;
    document.getElementById("Popup").style.posLeft=tempX;
}

function HidePopup()
{
    document.getElementById("Popup").innerHTML = "";
    document.getElementById("Popup1").innerHTML = "";
    document.getElementById("Popup1").style.visibility = "hidden";
    document.getElementById("Popup").style.visibility = "hidden";
}