var iCurrentPage = 0;
var strAction;
//pricing control center variables
var thePCC;
var PCCtop;
var PCCstop;
var IsNetscape = (navigator.appName.indexOf("Netscape") != -1);
var px = document.layers ? "" : "px";
var PccLocation = 0;
var VMessages;
var RMessages;
var Vclosebox;
var Vtextbox;
var _NothingSelectedExpanded;
var _NothingSelectedCollapsed;
var _currPageSeq;

function makeWin(url) {makeWin2(url);}

function makeWin2(url, p_Width, p_Height) {
	params  = GetWindowOpenParams(92, 0, p_Width, p_Height);
	ProcessOpenWindow(url, "Sitelet", params, false);
}

function OpenDetailsPop(sName, sOrderCode, bNoCaching){
	params  = GetWindowOpenParams(0, 0, 560, 350);
	ProcessOpenWindow('details_popup.aspx?name=' + sName + '&oc=' + sOrderCode, "Sitelet", params, bNoCaching);
}

//use for posting to the lead time shipping date details page
function OpenPostedDetailsPop(url, p_Width, p_Height, bNoCaching){
	windowName = "ShipDateDetails";
	
	params  = GetWindowOpenParams(92, 0, p_Width, p_Height);

	document.CONFIG_FORM.target = windowName;		  
	
	ProcessOpenWindow(url, windowName, params, bNoCaching);
	
	document.CONFIG_FORM.action = url;
	document.CONFIG_FORM.submit();
}	  

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 OpenWindow(url, windowName, params, noCaching)
{
	if (noCaching == true)
		return window.open(url + '&nc=true', windowName , params);
	else
		return window.open(url, windowName , params);
}
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;
}

function upgrade_selection_update_price(iCurrPage, sAction, mod, opt)
{
	getAnElement(mod).value = opt;
	AddHiddenFormElement('upsell_item',  mod + ':' + opt);
	var upsoldList = getAnElement("upsold_list");
	if (upsoldList.value.length > 0)
		upsoldList.value += "," + mod.substr(2);
	else
		upsoldList.value = mod.substr(2);
	update_price(iCurrPage, sAction);
}
      
function add_selection_update_price(iCurrPage, sAction, mod, opt)
{
	AddHiddenFormElement(mod,  opt);
	AddHiddenFormElement('upsell_item',  mod + ':' + opt);

	var upsoldList = getAnElement("upsold_list");
	if (upsoldList.value.length > 0)
		upsoldList.value += "," + mod.substr(2);
	else
		upsoldList.value = mod.substr(2);
	update_price(iCurrPage, sAction);
}
      
function go_collapse(display, childName)
{
	child = document.getElementById(display+childName);
	if (document.all)
		child.style.display = 'none';
		
	child.style.visibility = 'hidden';
	child.style.position = 'absolute';
}

function update_composite_config(iCurrPage, sAction, parent, parentName, childName)
{
	var obj = getElementsForCC(childName);

	if (parent.checked)
	{
		//close out other single select options first
		if (parent.type=="radio")
			close_composite_config(parentName);
			
		showSelectedCC(obj);
	}
	//close up the already rendered data
	else
	{
		hideUnSelectedCC(obj);
	}
}
	  
function close_composite_config(sModName)
{
	var option;
	var obj;
	var list = document.getElementsByName("list_" + sModName)[0].value.split(",");

	var i = 0;	
	while (sOptName = list[i++])
	{
		sName = sModName +  ":" + sOptName;
		option = document.getElementsByName(sName)[0];
		if (option != null && !option.checked)
		{
			obj = getElementsForCC(sName);

			if (obj.child1 != null && obj.child2 != null)
			{
				hideUnSelectedCC(obj);
			}
		}
	}
}
function hideUnSelectedCC(obj)
{
	obj.img_out.src = obj.img_out.src.replace("selected_open.gif","unselected.gif");
	obj.img_out.src = obj.img_out.src.replace("selected_closed.gif","unselected.gif");

	obj.href_obj.onmouseover = function(){window.status='';return true;};
	obj.href_obj.style.cursor = "default";				

	if (document.all)
	{
		obj.child1.style.display = 'none';
		obj.child2.style.display = 'none';
	}

	obj.child1.style.visibility = 'hidden';
	obj.child2.style.visibility = 'hidden';
	obj.child1.style.position = 'absolute';
	obj.child2.style.position = 'absolute';
}

function show_hide(parentName, childValue, childName, isDisabled, isBottom) 
{
	var obj = getElementsForCC(childName);

	if (isDisabled == "True")
	{
		determineShowHide(obj, isBottom);
	}
	else
	{
		var elements = document.getElementsByName(parentName);
		var i = 0;	
		while (theElement = elements[i++])
		{
			//don't do anything unless the option is selected
			if (theElement.value == childValue && theElement.checked)
				determineShowHide(obj, isBottom);
		}
	}
}
function determineShowHide(obj, isBottom)
{
	if (obj.child2 != null)
	{
		if ((document.all && obj.child2.style.display == "none") || (!document.all && obj.child2.style.visibility == "hidden"))
			showSelectedCC(obj);
		else
			hideSelectedCC(obj, isBottom);
	}
}
function showSelectedCC(obj)
{
	document.getElementsByName("expanded_"+obj.name)[0].value = "yes";
	obj.img.src = obj.img.src.replace("selected_closed.gif","selected_open.gif");
	obj.img_out.src = obj.img_out.src.replace("selected_closed.gif","selected_open.gif");
	obj.img_out.src = obj.img.src.replace("unselected.gif","selected_open.gif");
	obj.sel.innerHTML = obj.sel.innerHTML.replace(obj.show_selections, obj.hide_selections);
	
	obj.href_obj.onmouseover = function(){return false;};
	obj.href_obj.style.cursor = "hand";

	if (document.all)
	{
		obj.child1.style.display = "block";
		obj.child2.style.display = "block";
	}

	obj.child1.style.visibility = "visible";
	obj.child1.style.position = 'relative';
	obj.child2.style.visibility = "visible";
	obj.child2.style.position = 'relative';
}
function hideSelectedCC(obj, isBottom)
{
	document.getElementsByName("expanded_"+obj.name)[0].value = "no";
	obj.img.src = obj.img.src.replace("selected_open.gif","selected_closed.gif");
	obj.img_out.src = obj.img_out.src.replace("selected_open.gif","selected_closed.gif");
	obj.sel.innerHTML = obj.sel.innerHTML.replace(obj.hide_selections, obj.show_selections);
	if (document.all)
		obj.child2.style.display = "none";

	obj.child2.style.visibility = "hidden";
	obj.child2.style.position = 'absolute';

	if (isBottom == "true")
	{
		//only do this if it's IE4+ or NS6+
		if (document.all || document.getElementById)
			go_to(GetAbsPosition(obj.child2).y-200);
		else
			window.location.hash = obj.name;
	}
}
function getElementsForCC(name)
{
	var obj = new Object;
	obj.name = name;
	//content under the opt (first Show Selections)
	obj.child1 = document.getElementById("display1_"+name);
	//content after the first Show Selections
	obj.child2 = document.getElementById("display2_"+name);
	//image next to the Hide Selections text
	obj.img = document.getElementsByName("img_"+name)[0];
	//image next to the radio button/checkboxes
	obj.img_out = document.getElementsByName("img_out_"+name)[0];
	//Show Selections/Hide Selections text
	obj.sel = document.getElementById("sel_"+name);
	obj.href_obj = document.getElementById("href_"+name);
						
	//show selections text
	obj.show_selections = document.CONFIG_FORM.show_selections.value;
	//hide selections text
	obj.hide_selections = document.CONFIG_FORM.hide_selections.value;
	
	return obj;
}
	
function scroll_to(location)
{
	document.CONFIG_FORM.target = window.name;
	window.location.hash = location;
}

function go_to(y_pos)
{
	document.CONFIG_FORM.target = window.name;
	window.scrollTo(0, y_pos)		
}

function submitPage(sAction, current_action, iCurrPage, current_action_param)
{
	document.CONFIG_FORM.action = sAction;
	document.CONFIG_FORM.target = window.name;
	if (current_action_param)
		document.CONFIG_FORM.current_action_params.value = current_action_param;
	if (current_action)
		document.CONFIG_FORM.current_action.value = current_action;
	if (iCurrPage)
	{
		document.CONFIG_FORM.pageseq.value = iCurrPage;
	}
	document.CONFIG_FORM.expand_list.value = getExpandedModules();
	if (_doDynamicPricing)
		document.CONFIG_FORM.dpperf.value =  _totalDPReqs + ':' + _totalDPReqTime;
	document.CONFIG_FORM.submit();					
}
function update_price(iCurrPage, sAction)
{
	document.CONFIG_FORM.y_offset.value = document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	submitPage(sAction, 'update_price_top', iCurrPage);				
}

function cart_add(sAction)
{
	submitPage(sAction, 'cart_add');
}
function cart_add_override(sAction)
{
	submitPage(sAction, 'cart_add', document.CONFIG_FORM.pageseq.value, 'overrideCart');
}
function wishlist_add(sAction)
{
	submitPage(sAction, 'wishlist_add');
}	  
function wishlist_add_override(sAction)
{
	submitPage(sAction, 'wishlist_add', document.CONFIG_FORM.pageseq.value, 'overrideWishList');
}
function changeDPStatus(newStatus, errormsg, donotpost)
{
	AddHiddenFormElement("dpstatus",  newStatus);
	if (errormsg)
		AddHiddenFormElement("dperrormsg",  errormsg);
	if (!donotpost || donotpost == "false")
		submitPage(document.CONFIG_FORM.current_page.value + document.CONFIG_FORM.current_qs.value, 'update_price_change_dp_status');
}
function next_page(sAction)
{
	submitPage(sAction, 'next'); //  + "&pg=" + ("" + (+_currPageSeq + 1))
}
	
//going from pages that don't have compatibility issues to the first page with compatibility issues 
function compat_page(iMinConfigLogicPage, sAction)
{
	submitPage(sAction + "#logicerror", 'compat_page', iMinConfigLogicPage);
}

//going from any page to the any page
function to_page(iToPage, sAction)
{
	submitPage(sAction, 'next', iToPage);
}	

//going from review page to any page, and scroll to a particular mod
function to_page_mod(iToPage, iToMod, sAction)
{
	document.CONFIG_FORM.scroll_to_mod.value = iToMod;
	submitPage(sAction, 'review', iToPage);
}	

//Content functions
function goPopUp(sUrl){	  
	params  = GetWindowOpenParams(600, 500, p_Width, p_Height);
	ProcessOpenWindow(url, "LearnAbt", params, false);
}

function winopen(url,stuff,morestuff) {
	window.open(url,stuff,morestuff).focus();
} 

function checkSelect(module, table, select)
{
	var aDiv = getAnElement("module_options_" + module);
	if (select)
	{
		getAnElement("mod_selector_left_" + module).src = "http://img.dell.com/images/global/configurator/general/bevel_selected.gif";
		getAnElement("mod_selector_middle_" + module).className = "Mod_Selector_cell_middle_selected";
		if (aDiv.className == "option_display_visible") //currently expanded
		{
			getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/collapse_selected.gif";
		}
		else
		{
			getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/expand_selected.gif";
		}
	}
	else
	{
		getAnElement("mod_selector_left_" + module).src = "http://img.dell.com/images/global/configurator/general/bevel_notselected.gif";
		getAnElement("mod_selector_middle_" + module).className = "Mod_Selector_cell_middle_notselected";
		if (aDiv.className == "option_display_visible") //currently expanded
		{
			getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/collapse_notselected.gif";
		}
		else
		{
			getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/expand_notselected.gif";
		}
	}
	
}
	
function toggleOptionDisplay(module, table){
	var aDiv = getAnElement("module_options_" + module);
	var expandImage = getAnElement("expand_module_image_" + module);
	var moduleName = getAnElement("mh_" + module).innerHTML;
	var collapsedDesc = _NothingSelectedCollapsed.replace("{0}", moduleName);
	var currDesc = getAnElement("mhd_m_" + module);

	if(aDiv.className == "option_display_visible"){
		aDiv.className="option_display_none";
		getAnElement("modoptshow_" + module).value="0";
		setOptionDisplay(table, module, false);
		if (currDesc.innerHTML == _NothingSelectedExpanded)
			currDesc.innerHTML = collapsedDesc;
	}
	else
	{
		aDiv.className="option_display_visible";
		getAnElement("modoptshow_" + module).value="1";
		setOptionDisplay(table, module, true);
		if (currDesc.innerHTML == collapsedDesc)
			currDesc.innerHTML = _NothingSelectedExpanded;
	}
}
function setOptionDisplay(table, module, isopen)
{
	//table.className="Mod_Selector_table_selected";
	getAnElement("mod_selector_left_" + module).src = "http://img.dell.com/images/global/configurator/general/bevel_selected.gif";
	getAnElement("mod_selector_middle_" + module).className = "Mod_Selector_cell_middle_selected";
	if (isopen)
		getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/collapse_selected.gif";
	else
		getAnElement("mod_selector_right_" + module).src = "http://img.dell.com/images/global/configurator/general/expand_selected.gif";
}
	
function setPCCStart(startY)
{
	var startX = 750;
	
	thePCC=getAnElement("divPCC");
	PCCtop=getAnElement("topBar"); 
	RMessages = getAnElement("rec-window");
	VMessages = getAnElement("val-error-window");
	Vclosebox = getAnElement('val-closebox');
	Vtextbox = getAnElement('val-text');
	if (VMessages)
		InitializeVMessageHeight();
	if (RMessages)
		InitializeRMessageHeight();
	PCCstop = getAnElement("bottomBar").offsetTop - thePCC.offsetHeight - 30;

	if(document.layers)thePCC.style=thePCC;
	thePCC.style.left = startX + px;
	thePCC.x = startX; thePCC.y = startY;

	setPCC();
}
	
function setPCC()
{
	var pY = (IsNetscape ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop) + 15;
	if (PccLocation == pY)
	{
		if(thePCC.y > PCCstop)thePCC.y = PCCstop;
		else
		{
			var dY = (pY > PCCtop.offsetTop) ? pY : PCCtop.offsetTop;
			thePCC.y += (dY - thePCC.y);
			thePCC.style.top=thePCC.y + px;
			if (VMessages)
			{
				if (VMessages.y)
					VMessages.style.top = thePCC.y + VMessages.y +  px;				
				else 
					VMessages.style.top = thePCC.y + px;
			}
			if (RMessages)
			{
				if (RMessages.y)
					RMessages.style.top = thePCC.y + RMessages.y +  px;				
				else 
					RMessages.style.top = thePCC.y + px;
			}
		}
	}
	
	PccLocation = pY;
	
	setTimeout("setPCC()", 250);
}

function InitializeRMessageHeight()
{
	var rText = getAnElement('rec-text');
	var tHeight = parseInt(rText.offsetHeight);
	RMessages.startHeight = (tHeight + 40) 
	RMessages.style.height= RMessages.startHeight + 'px';
}	
	
function InitializeVMessageHeight()
{
	var valText = getAnElement('val-text');
	valText.style.height='auto';
	var tHeight = parseInt(valText.offsetHeight);
	if (tHeight > 192)
	{
		valText.style.height='192px';
		tHeight = 202;
	}
	VMessages.startHeight = (tHeight + 20) 
	VMessages.style.height= VMessages.startHeight + 'px';
	if (document.CONFIG_FORM.val_module.value != '')
	{
		valmodlink_go(document.CONFIG_FORM.val_module.value,0);
		document.CONFIG_FORM.val_module.value = '';
	}				
}
	
function CloseValErrors()
{
	var lTarget = thePCC.offsetLeft;
	var left = parseInt(VMessages.style.left);
	var width = parseInt(VMessages.style.width);
	if (! (VMessages.y))
		VMessages.y=0;
	if (left < lTarget || VMessages.y < thePCC.offsetHeight )
	{
		if (left < lTarget)
		{
			var ldiff = lTarget - left;
			if (ldiff > 100)
				VMessages.style.left = (left + 25) + px;
			else if (ldiff > 80)
				VMessages.style.left = (left + 6) + px;

			else if (ldiff > 20)
				VMessages.style.left = (left + 4) + px;				
			else 
				VMessages.style.left = (left + 1) + px;				
		}
		if (VMessages.y < thePCC.offsetHeight)
			if (VMessages.y < thePCC.offsetHeight - 100)
				VMessages.y += 20;
			else if (VMessages.y < thePCC.offsetHeight - 80)
				VMessages.y += 10;
			else if (VMessages.y < thePCC.offsetHeight - 60)
				VMessages.y += 6;
			else 
				VMessages.y += 2;
		VMessages.style.top = thePCC.y + VMessages.y +  px;				

	}
	else 
	{
		VMessages.docked = true;
		VMessages.docking = false;

		clearInterval(VMessages.interval);
	}
	
}

function valmodlink(modId, pageSeq)
{
	valmodlink_go('mh_' + modId, pageSeq);
}

function valmodlink_comp(displaytag, pageSeq)
{
	valmodlink_go('c_' + displaytag, pageSeq);
}

function valmodlink_go(sLink, pageSeq)
{
	var ModRow = getAnElement(sLink);			
	if (ModRow)
	{

		var location = GetAbsPosition(ModRow);
		if (location)
		{
			window.scrollTo(0,location.y - VMessages.startHeight - 40 );
		}
	}
	else if(pageSeq != 0 && pageSeq != document.CONFIG_FORM.pageseq.value)
	{
		document.CONFIG_FORM.val_module.value = sLink;
		document.CONFIG_FORM.scroll_to_mod.value = sLink;
	   	submitPage(document.CONFIG_FORM.current_page.value + document.CONFIG_FORM.current_qs.value, 'update_price', pageSeq);

	}

}
function HideRec()
{
	RMessages.style.visibility = 'hidden';
}

function ShowRec()
{
	InitializeRMessageHeight();
	RMessages.style.visibility = 'visible';
}
		
		
function ShowHidValErrors()
{
	if (VMessages.docking)
		return;
	if (VMessages.docked)
		ShowValErrors();
	else
		HideValErrors();
}

function HideValErrors()
{
	VMessages.docking = true;
	var	img = getAnElement('val-hid-close-img');
	//here we are
	img.src = img.src.replace('CloseWarning','OpenWarning');
	// Vclosebox.style.visibility = 'hidden';
	Vtextbox.style.visibility = 'hidden';
	// shrink it down to a button
	Vtextbox.style.width = '0px';
	VMessages.style.left = '30px';
	VMessages.style.width = (thePCC.offsetWidth - 6) + 'px';
	Vclosebox.style.left = (thePCC.offsetWidth - 30) + 'px';
	VMessages.style.height='15px';
	// start moving it to the right and down while shrinking
	VMessages.interval = setInterval('CloseValErrors()',20);
}
		
		
		
function ShowValErrors(ShowValErrors)
{
	if (VMessages.docking)
	{
		clearInterval(VMessages.interval);
		VMessages.docking = false;
	}

	
	var	img = getAnElement('val-hid-close-img');
	img.src = img.src.replace('OpenWarning','CloseWarning');
	VMessages.y=0;
	VMessages.docked = false;
	VMessages.style.textAlign = 'left';
	VMessages.style.textAlign = 'left';

	VMessages.style.width = '710px';
	InitializeVMessageHeight();
	VMessages.style.left='20px';
	VMessages.style.top = thePCC.y + px;
	VMessages.style.visibility = 'visible';
	Vclosebox.style.left='684px';
	Vclosebox.style.visibility = 'visible';
	Vtextbox.style.width = '690px';
	Vtextbox.style.visibility = 'visible';
}

function getAnElement(id)
{
	return document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
}

function getExpandedModules()
{
	var div;
	var id;
	var list ;
	var els = document.CONFIG_FORM.elements;
	var x = 0;
	while (el = els[x++])
	{
		if (el.id.substr(0,11) == "modoptshow_")
		{
			id = el.id.substr(11);
			if (el.value=="1")
			{
				div = getAnElement("module_options_" + id)
				if (div && (div.style.display=="inline" || div.className == "option_display_visible"))
					if (list)
						list += "," + id;
					else
						list = id;
			}
		}
	}
	if (list)
		return list;
	else
		return "";
}

function GetAbsPosition(object) {
	var position = new Object;
	position.x = 0;
	position.y = 0;

	if( object ) 
	{
		position.x = object.offsetLeft;
		position.y = object.offsetTop;

		if( object.offsetParent ) 
		{
			var parentpos = GetAbsPosition(object.offsetParent);
			position.x += parentpos.x;
			position.y += parentpos.y;
		}
	}

	return position;
}

function AddHiddenFormElement(name, value)
{
	var el = document.createElement('input');
	el.name = name;
	el.id = name;
	el.value = value;
	el.type = "hidden";
	document.CONFIG_FORM.appendChild(el);
	return el;
}	  


