<!--
//DHTML Window script- Copyright Dynamic Drive (http://www.dynamicdrive.com)
//For full source code, documentation, and terms of usage,
//Visit http://www.dynamicdrive.com/dynamicindex9/dhtmlwindow.htm

var dragapproved = false;
var minrestore = 0;
var initialwidth, initialheight;
var ie5 = document.all && document.getElementById;
var ns6 = document.getElementById && !document.all;

function iecompattest()
{
	return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}

function drag_drop(e)
{
	if (ie5&&dragapproved&&event.button==1)
	{
		document.getElementById("dwindow").style.left=tempx+event.clientX-offsetx+"px";
		document.getElementById("dwindow").style.top=tempy+event.clientY-offsety+"px";
	}
	else if (ns6 && dragapproved)
	{
		document.getElementById("dwindow").style.left=tempx+e.clientX-offsetx+"px";
		document.getElementById("dwindow").style.top=tempy+e.clientY-offsety+"px";
	}
}

function initializedrag(e)
{
	offsetx=ie5? event.clientX : e.clientX;
	offsety=ie5? event.clientY : e.clientY;
	document.getElementById("dwindowcontent").style.display="none"; //extra
	tempx=parseInt(document.getElementById("dwindow").style.left);
	tempy=parseInt(document.getElementById("dwindow").style.top);

	dragapproved=true;
	document.getElementById("dwindow").onmousemove=drag_drop;
}

function subBox(x, y, w, h, bgc, id) {
	var v = document.createElement("div");
	v.setAttribute("id", id);
	v.style.position = "absolute";
	v.style.left = x + "px";
	v.style.top = y + "px";
	v.style.width = w + "px";
	v.style.height = h + "px";
	if (bgc != "") v.style.backgroundColor = bgc;
	v.style.visibility = "visible";
	v.style.padding = "0px";
	if (ie5)
		v.style.filter = "alpha(opacity=30)";
	else
		v.style.MozOpacity = .30;
	v.style.zIndex = -1;
	return v;
}

function loadwindow(title, url, width, height, left, top)
{
	if (!ie5 && !ns6)
	{
		var strArgs = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
		strArgs += ",toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0";
		window.open(url, "", strArgs);
	}
	else
	{
		document.getElementById("cframe").src = url;
		document.getElementById("dwindow").style.width = initialwidth = width + "px";
		document.getElementById("dwindow").style.height = initialheight = height + "px";
		document.getElementById("dwindow").style.left = "30px";
		document.getElementById("dwindow").style.top = ns6 ? window.pageYOffset * 1 + 30 + "px" : iecompattest().scrollTop * 1 + 30 + "px";
		document.getElementById("dwindowtitlebar").innerHTML = title;
		document.getElementById("dwindowcontent").style.height = (height - 15) + "px";
//		document.getElementById("dwindow").style.display = '';

		// Create/Update the drop-shadow.
		var tw = (ie5) ? width : width + 4;
		var th = (ie5) ? height : height + 6;
		var shadow = document.getElementById("dwindow_shadow");
		if (shadow == null)
		{ // This window does not have a shadow yet, so add one.
			shadow = new subBox(15, 15, tw, th, "#000000", 'dwindow_shadow');
			document.getElementById("dwindow").appendChild(shadow);
		}
		else
		{ // A shadow already exists, so update its size (location is relative).
			shadow.style.width = tw + "px";
			shadow.style.height = th + "px";
		}
	}
}

function maximize(){
	if (minrestore==0)
	{
		minrestore=1; //maximize window
		document.getElementById("maxname").setAttribute("src","restore.gif");
		document.getElementById("dwindow").style.width=ns6? window.innerWidth-20+"px" : iecompattest().clientWidth+"px";
		document.getElementById("dwindow").style.height=ns6? window.innerHeight-20+"px" : iecompattest().clientHeight+"px";
	}
	else{
		minrestore=0; //restore window
		document.getElementById("maxname").setAttribute("src","max.gif");
		document.getElementById("dwindow").style.width=initialwidth;
		document.getElementById("dwindow").style.height=initialheight;
	}
	document.getElementById("dwindow").style.left=ns6? window.pageXOffset+"px" : iecompattest().scrollLeft+"px";
	document.getElementById("dwindow").style.top=ns6? window.pageYOffset+"px" : iecompattest().scrollTop+"px";
}

function HideModalEditor(bReload)
{
	if (bReload) // Fetch the page again, and the editor is hidden by default.
		parent.document.location.reload(true);
	else // Just hide the editor--no changes were made.
		parent.document.getElementById("dwindow").style.display = "none";
}

function stopdrag()
{
	dragapproved=false;
	document.getElementById("dwindow").onmousemove=null;
	document.getElementById("dwindowcontent").style.display=""; //extra
}

var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = '';
ModalDialog.eventhandler = '';

function ModalDialogMaintainFocus()
{
  try
  {
    if (ModalDialogWindow.closed)
     {
        window.clearInterval(ModalDialogInterval);
        eval(ModalDialog.eventhandler);       
        return;
     }
    ModalDialogWindow.focus(); 
  }
  catch (everything) {}
}

function ModalDialogRemoveWatch()
{
  ModalDialog.value = '';
  ModalDialog.eventhandler = '';
}

function ShowModalEditor(strTitle, strAction, iWidth, iHeight, iLeft, iTop)
{
//  var strURL = "ts_modal_editor.asp?winTitle=" + strTitle + "&cmd=" + strAction;
  var strURL = "ts_modal_editor.asp?winTitle=&cmd=" + strAction;

  loadwindow(strTitle, strURL, iWidth, iHeight, iLeft, iTop);
}

function IsNumeric(strText, iDecimalPlaces)
{
	var strNumbers = "0123456789";
	if (iDecimalPlaces > 0 && strText.indexOf(".") > -1)
	{ // Decimals are allowed and the string contains at least one.
		if ((strText.indexOf(".", strText.indexOf(".") + 1) > -1) || (strText.length - strText.indexOf(".") - 1 > iDecimalPlaces))
			return false; // The string contains more than 1 decimal or too many decimal places.
		// Add a decimal to the string of allowed characters.
		strNumbers = strNumbers + ".";
	}
	var bIsNumeric = true;

	for (var i = 0; i < strText.length && bIsNumeric == true; i++)
		if (strNumbers.indexOf(strText.charAt(i)) == -1)
			bIsNumeric = false;

	return bIsNumeric;
}


/* This variable is shared by HandleFocus() and HandleChanges() below. */
var strLastFieldValue;


/* Remember the value of the text element before changes can occur. */
function HandleFocus(objElement)
{
	strLastFieldValue = objElement.value;
}


/* Strip non-numeric entries and move to the next form field. */
function HandleChanges(iForm, objElement, iDecimalPlaces)
{
	if (objElement.value.length != 0)
	{
		if (!IsNumeric(objElement.value, iDecimalPlaces))
			objElement.value = strLastFieldValue;
	}
	strLastFieldValue = objElement.value
	if (objElement.value.length == objElement.maxLength)
	{
		for (var i = 0; document.forms[iForm].elements[i].name != objElement.name; i++);
		document.forms[iForm].elements[++i].focus();
	}
}


/* Verify that all fields whose labels contain a specified indicator are filled in. */
/* This function does not account for arrays of elements (such as radio buttons)
   because, frankly, I did not have the need to do so at this time. */
function RequiredFieldsArePopulated(theForm, strIndicator)
{
	var bPopulated = true;
	var theElement;
	var theLabel;
	for (var i = 0; i < theForm.elements.length; i++)
	{ // Loop through every element.
		theElement = theForm.elements[i];
		if (theElement.type != "checkbox" && theElement.type != "button" && theElement.type != "submit" && theElement.type != "hidden")
		{ // The field can contain a value.
			try
			{ // Some fields might not have labels associated with them.
				theLabel = document.getElementById(theElement.name + "Label");
				if (theLabel.innerHTML.indexOf(strIndicator) > -1)
				{ // The associated label has the indicator for required fields.
					if (theLabel.innerHTML.indexOf("&gt;&nbsp;") > -1) // This error was marked before.
					{
						theLabel.innerHTML = theLabel.innerHTML.substr(10, theLabel.innerHTML.length - 10);
						theLabel.className = "";
					}
					if ((theElement.type == "select-one" && theElement.selectedIndex == 0) || (theElement.value == "")) // The field does not contain a value.
					{
						theLabel.innerHTML = "&gt;&nbsp;" + theLabel.innerHTML;
						bPopulated = false;
						theLabel.className = "requires_attention";
					}
				}
			}
			catch (strError) {  /* Move along... There's nothing to see here... */ }
		}
	}
	return bPopulated;
}

/* The following functions are specific to the TS ordering page. */

function GetSKU(strElementName)
{
	if (strElementName.length <= 3)
		return "";
	else
		return strElementName.substring(3, strElementName.length);
}

function Old_OrderFormInit(theForm)
{
	for (var i = 0; i < theForm.elements.length; i++)
	{
		if (theForm.elements[i].type == "checkbox" && !theForm.elements[i].checked)
		{
			theForm.elements["qty" + GetSKU(theForm.elements[i].name)].value = "";
			theForm.elements["qty" + GetSKU(theForm.elements[i].name)].disabled = true;
		}
	}
}

function OrderFormInit(theForm)
{
	for (var i = 0; i < theForm.elements.length; i++)
	{
		if (theForm.elements[i].type == "checkbox")
		{
			try
			{
				if (!theForm.elements[i].checked)
				{
					theForm.elements["qty" + GetSKU(theForm.elements[i].name)].value = "";
					theForm.elements["qty" + GetSKU(theForm.elements[i].name)].disabled = true;
				}
				if (IsNumeric(theForm.elements[i].value))
				{ // The value is the parent SKU ID.
					if (!theForm.elements["sku" + theForm.elements[i].value].checked)
					{ // The parent is not checked, so disable the child.
						theForm.elements[i].disabled = true;
					}
					theForm.elements[i].value = ""; // This data is no longer needed.
				}
			} catch (e) {/* Customer details now contains checkboxes. */}
		}
	}
}

function HandleCheckSKU(theForm, theElement, iDefaultQuantity)
{
	if (theElement.type != "checkbox") return;

	var strQtyField = "qty" + GetSKU(theElement.name);
	if (theElement.checked)
	{ // Enable the quantity box and set its default value.
		theElement.value = "on"; // Explicitly set the value to on.
		theForm.elements[strQtyField].disabled = false;
		theForm.elements[strQtyField].value = iDefaultQuantity;
		theForm.elements[strQtyField].focus();
	}
	else
	{ // Disable the quantity box and clear its value.
		theElement.value = ""; // Explicitly clear the value.
		theForm.elements[strQtyField].disabled = true;
		theForm.elements[strQtyField].value = "";
	}
}

function SubmitOrderPage(theForm, iNextStep)
{
	var strErrors = "";

	if (theForm.hidStepName.value == "custDetails")
	{ // These checks are only applicable to the customer details form.
		if (iNextStep > theForm.hidStepNumber.value)
		{
			if (!RequiredFieldsArePopulated(theForm, "*"))
			{ // The user is trying to move to the next step without filling in the required fields.
				strErrors = "All fields marked with an asterisk (*) are required.";
			}
			else if (theForm.txtEmail.value != theForm.txtEmail2.value)
			{
				strErrors = "The e-mail address entered does not match the verification entry.";
			}
		}
	}
	else
	{ // This is a product selection list or the confirmation page (no editable form fields).
		var objElement;
		for (var i = 0; i < theForm.elements.length; i++)
		{
			objElement = theForm.elements[i];
			if (objElement.type == "checkbox")
			{
				// Do I want to do anything here?
			}
			else if (objElement.type == "text" && objElement.name.substring(0, 3) == "qty")
			{
				if (!objElement.disabled)
				{
					if (objElement.value == "" || !IsNumeric(objElement.value, 0))
						strErrors += "You selected \"" + document.getElementById("label" + GetSKU(objElement.name)).innerHTML + ",\" so you must enter a valid quantity.\n";
					else if (+objElement.value > +theForm.elements["sku" + GetSKU(objElement.name) + "MaxQty"].value)
						strErrors += "The maximum quantity for " + document.getElementById("label" + GetSKU(objElement.name)).innerHTML + " is " + theForm.elements["sku" + GetSKU(objElement.name) + "MaxQty"].value + ".\n";
				}
			}
		}
	}

	if (strErrors.length == 0)
	{ // No errors were found, so post/move to the specified page.
		theForm.action = "/off/ts/ts_order.asp?step=" + iNextStep;
		theForm.submit();
		return true;
	}
	else
	{ // Display the error(s) and sit right were we are.
		alert(strErrors);
		return false;
	}
}

function SubmitOrder(theForm, iNextStep)
{
	var strErrors = "";

	if (theForm.hidStepName.value == "confirm")
	{ // These checks are only applicable to the Confirm the Order form.
		if (iNextStep > theForm.hidStepNumber.value)
		{
			if (!RequiredFieldsArePopulated(theForm, "*"))
			{ // The user is trying to move to the next step without filling in the required fields.
				strErrors = "All fields marked with an asterisk (*) are required.";
			}
		}
	}
	if (strErrors.length == 0)
	{ // No errors were found, so post/move to the specified page.
		var serviceCodeNums = GetServiceCodeNumberFromForm(theForm);
		theForm.action = "/off/ts/ts_process_cmd.asp?cmd=ProcessOrder&serviceCodeNums=" + serviceCodeNums;
		theForm.submit();
		return true;
	}
	else
	{ // Display the error(s) and sit right were we are.
		alert(strErrors);
		return false;
	} 

}

function ViewChecklist(strFilePath)
{
	var winChecklist = window.open(strFilePath, "", "");
	if (!winChecklist) // The new window does not exist.
		alert("Failed to open new window containing the checklist. Please disable any pop-up blocking software you have installed or add www.officedepotservices.com to the list of sites allowed to open new windows.");
	else
		winChecklist.focus();
}

function PrintOrder(iOrderID)
{
	var winPrint = window.open("/off/ts/sku_info/ts_print_order.asp?orderid=" + iOrderID, "", "width=200,height=50,resizeable=0,scrollbars=0");
	if (!winPrint) // The new window does not exist.
		alert("Failed to open new window for printing. Please disable any pop-up blocking software you have installed or add www.officedepotservices.com to the list of sites allowed to open new windows.");
//	else // Set the window to print as soon as the document loads.
//		winPrint.document.onload = winPrint.document.print();
}

function CancelOrder(iOrderID)
{
	if (window.confirm("The work order status cannot be changed after it is cancelled.\n\nPress \"OK\" if you are sure you want to cancel this work order."))
		document.location = "ts_process_cmd.asp?cmd=CancelOrder&orderid=" + iOrderID;
}

function CheckServiceTypeItems(objForm, objElement, iServiceType)
{
	var strElementName;
	for (var i = 0; i < objForm.elements.length; i++)
	{
		if (objForm.elements[i].type == "checkbox")
		{
//			if (objElement == objForm.elements[i]) continue;
			strElementName = objForm.elements[i].name;
			if (strElementName.substring(strElementName.indexOf("_"), strElementName.length) == "_" + iServiceType)
				objForm.elements[i].checked = objElement.checked;
			else // Uncheck this item.
				objForm.elements[i].checked = false;
		}
	}
}

function GetServiceCodeNumberFromForm(theForm)
{
	var fieldValues = '';
	var recordsSeparator = '^';
	var pairSeparator = '$';
	

	for (var i = 0; i < theForm.elements.length; i++)
	{ // Loop through every element.
		theElement = theForm.elements[i];
		
		if (theElement.type != "text" || theElement.id.indexOf('ServiceCodeNumber') < 0)
		{
			continue;
		}
		
		if (fieldValues.length > 0)
		{
			fieldValues = fieldValues + recordsSeparator	
		}

		fieldValues = fieldValues + theElement.name.substring(theElement.name.indexOf("$") + 1, theElement.name.lastIndexOf("$")) + pairSeparator + theElement.value;;	
			
	}
	
	return fieldValues;
}
// -->

