// ********************************************************************************************************
// Content Page Constants
window.name = "AMControlPanel";

var FieldChanged = false;
var ConfirmedSave = false;
var BeforeUnloadWarning = "____________________________________________________            \n\n\nWARNING:  You have made changes to this form without saving\nthem.  If you leave this page now, any changes you have made\nwill be lost.\n\n____________________________________________________            \n";
var SmartClickURL = "";
var NoEdit = false;

var SelectedPanel = null;
var PageLoaded = false;
var MasterPage = false;

var HelpWindow = null;
var PreviewWindow = null;
var ia = new Array();
var ia_on = new Array();

// ********************************************************************************************************
// Content Page Event Handlers
function __onLoadHandler(master) {
  if (getId("xMasterPage")) MasterPage = true;
	if (MasterPage)
	{
	  if (window.location.href != top.location.href) top.location.replace(window.location.href);
  	GetWindowSize();
	}
	if (getId("xNoEdit")) NoEdit = (getId("xNoEdit").value == "1");

  PageLoaded = true;
	
	if (window.PreLoadHandler) PreLoadHandler();

	FieldChanged = false;
	ConfirmedSave = false;
	LockdownForm();
	if (window.FixDialogPosition) FixDialogPosition();

	if (window.PostLoadHandler) PostLoadHandler();
	window.scrollTo(0,0);
	if (window.ShowPageAlert) ShowPageAlert();
}

function __onBeforeUnloadHandler(e) {
	HTMLChangeCheck();
	if (FieldChanged && !ConfirmedSave && !NoEdit) e.returnValue = BeforeUnloadWarning;
}

function __onMouseDownHandler(e) {
	if (MasterPage) HidePanels();
	if (window.CustomMouseDownHandler) CustomMouseDownHandler(e);
}

function __onMouseMoveHandler(e) {
	if (document.all) {
    button = (e.button == 1);
		mx = e.x;
		my = e.y;
  } else {
    button = (e.button == 0);
		mx = e.pageX;
		my = e.pageY;
  }
	
	if (DialogDrag) {
		if (button) {
			FixDialogPosition(mx, my);
		} else {
			DialogDrag = false;
		}
	}
}

function __onMouseUpHandler(e) {
  StopDialogDrag(e);
}

function __onKeyDownHandler(e) {
  var panelType = "";
  if (getId("xDialogPanelType")) panelType = getId("xDialogPanelType").value;
  if (panelType == "FreezePage") return false;

	if (window.CustomKeyDownHandler) {
		if (CustomKeyDownHandler(e)) return true;
	}

	var key = "";
	var src = "";
	var id = "";

	if (document.all) {
		key = e.keyCode;
		src = e.srcElement.tagName;
		id = e.srcElement.id;
	} else {
		key = e.which;
		src = e.target.tagName;
		id = e.target.id;
	}
	
	switch (key) {
		case 13 :
		  if (MasterPage && getId("tblDialogPanel").style.display == "") {
		    CloseDialogPanel();
		  } else {
			  if (src!="TEXTAREA") {
				  // Try to save or click OK on Enter keypresses
				  if (window.Click_SAVE) { Click_SAVE(e); break; }
				  if (window.Click_OK) { Click_OK(e); break; }
				  if (window.Click_EXIT) { Click_EXIT(e); break; }
			  }
			}
			break;
		case 27 :
		  if (MasterPage && getId("tblDialogPanel").style.display == "") {
		    CancelDialogPanel();
		  } else {
			  // Try to cancel or click Close on Escape keypresses
			  if (window.Click_CANCEL) { Click_CANCEL(e); break; }
			  if (window.Click_CLOSE) { Click_CLOSE(e); break; }
			  break;
			}
	}
}

function __onContentMouseOverHandler() {
	if (window.ContentMouseOverHandler) ContentMouseOverHandler();
}

function __onResizeHandler() {
  GetWindowSize();
  FixDialogPosition();
}

function __onSelectStartHandler() {
  return (!DialogDrag);
}

// ********************************************************************************************************
// Onload lockdown

function LockdownForm() {
	// Disable ability to edit form values when the user does not have edit priviledges
	if (NoEdit) {
		// If editing is blocked, then lockdown the editable fields.
		getId("divMainContent").className = "ContentGray";
		
		// SELECT fields cannot be set read-only, so remove all other options other than the selected option
		coll = document.getElementsByTagName("SELECT");
		for (i = 0; i < coll.length; i++) {
		  coll[i].disabled = true;
		}

		// INPUT fields can be text, radio buttons, or checkboxes, and all need to be handled differently
		coll = document.getElementsByTagName("INPUT");
		for (i = 0; i < coll.length; i++) {
 		  coll[i].disabled = true;
			if (coll[i].type == "text" || coll[i].type == "password") {
			  if (coll[i].className.indexOf("ReadOnly") == -1) coll[i].className += " ReadOnly";
			}
		}

		// TEXTAREA fields, like text input, can just be set readonly
		coll = document.getElementsByTagName("TEXTAREA");
		for (i = 0; i < coll.length; i++) {
			coll[i].disabled = true;
			if (coll[i].className.indexOf("ReadOnly") == -1) coll[i].className += " ReadOnly";
		}
	}
}

// ********************************************************************************************************
// Freeze dialog

function FreezePage(title, detail) {
  if (title == null) title = "Saving changes...";
  if (detail == null) detail = "Please wait...";
  
  ShowDynamicDialog(title, URL_AMWebRoot + "Dialogs/PleaseWait.html?Detail=" + detail, "Gray", 400, 75);
  getId("xDialogPanelType").value = "FreezePage";
}

// ********************************************************************************************************
// Form 
function FieldChange() { FieldChanged = true; }

function HTMLChangeCheck() {
	if (!FieldChanged) {
		if (getId(ControlID + "htmleditorsource")) {
			origSource = getId(ControlID + "htmleditorsource").value;
			var oEditor = FCKeditorAPI.GetInstance(ControlID + "htmleditorsource");
			oEditor.UpdateLinkedField();
			newSource = getId(ControlID + "htmleditorsource").value;
			if (origSource != newSource) FieldChanged=true;
		}
	}
}

function PageLink(page) {
  var pageparts = page.split("?");
  return URL_AMWebRoot + page + ((pageparts.length == 1) ? "?" : "&") + SID + "=" + SessionID;
}

function RedirectToWMPage(page) {
  return SmartClick(PageLink(page));
}

function SmartClick(url) {
	HTMLChangeCheck();
	if (FieldChanged && !ConfirmedSave && !NoEdit) {
		if (document.all && !is_ie) { 
			var coll = document.all.tags("SELECT");
			for (i = 0; i < coll.length; i++) {
				coll[i].style.visibility = "hidden";
			}
		}

    ShowDynamicDialog("Save Your Changes", URL_AMWebRoot + "Dialogs/SmartClick.html", "Blue", 400, 140);
    getId("xDialogPanelType").value = "SmartClick";

		SmartClickURL = url;
	}
	else
	{
	  location.href = url;
	}
	return false;
}

function SmartClick_Save() {
  HideDialogPanel();

	if (getId("xClickThrough")) getId("xClickThrough").value = SmartClickURL;
	if (window.Click_SAVE) {
		Click_SAVE();
	} else if (document.forms[0]) {
    FormSubmit();
	}
	SmartClickURL = "";
}

function SmartClick_Discard() {
  HideDialogPanel();

	ConfirmedSave = true;
	location.href = SmartClickURL;
	SmartClickURL = "";
}

function SmartClick_Cancel() {
  HideDialogPanel();

	if (document.all && !is_ie) { 
		var coll = document.all.tags("SELECT");
		for (i=0; i<coll.length; i++) {
			coll[i].style.visibility = "";
		}
	}
	SmartClickURL = "";
}

// ********************************************************************************************************
// Navigation

function Logout() { SmartClick("Login.aspx"); }
function HelpCenter(topic) { HelpWindow = window.open(PageLink("Support/Default.aspx?T=" + topic),"HelpWindow","height=500, width=900, location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=yes, toolbar=yes"); HelpWindow.focus(); }

// ********************************************************************************************************
// Interface

function ToggleMenu(num) { 
	show = (getId("MenuHeading"+num).className == "MenuHeading");
	for (i = 1; i < 10; i++) {
		if (getId("MenuHeading" + i)) {
			getId("MenuHeading" + i).className = "MenuHeading";
			getId("MenuPrefix" + i).style.display = "none";
			getId("MenuPanel" + i).style.display = "none";
		}
	}
	if (show) {
		getId("MenuHeading" + num).className = "MenuHeading_On";
		getId("MenuPrefix" + num).style.display = "";
		getId("MenuPanel" + num).style.display = "";
	}
}
function BookmarkPage() {
	title = document.title;
	url = window.location.href;
	if (document.all) {
		window.external.AddFavorite(url, title);
	} else {
		if (window.sidebar) window.sidebar.addPanel(title, url, "");
	}
}

// ********************************************************************************************************
// Misc.

function Overload(img, src) {
	if (img.name == "") {
		index = ia.length;
		img.name = index;
		ia[index] = new Image;
		ia[index].src = img.src;
		ia_on[index] = new Image;
		ia_on[index].src = src;
	}
}
function ImgSwap(img, over) {
	img.src = (over==1) ? ia_on[img.name].src : ia[img.name].src;
}

// ********************************************************************************************************
// General Script Utility Functions
// ********************************************************************************************************

function getId(id) {
  C1 = document.getElementById(id);
  if (C1 != null) return C1;
  C2 = document.getElementById(ControlID+id);
  if (C2 != null) return C2;
  C3 = document.getElementById(NavID+id);
  if (C3 != null) return C3;
  C4 = document.getElementById(MasterID+id);
  if (C4 != null) return C4;
  return null;
}

function EmptyField(id, label) {
  if (getId(id).value == "") {
    getId(id).focus();
    alert("Please enter a valid " + label + ".");
    return true;
  } else {
    return false;
  }
}

function FormSubmit(action) {
  ConfirmedSave = true;
  if (action != null) document.forms[0].action = action;
  document.forms[0].submit();
}

// ********************************************************************************************************
// Panels

function DisplayPanel(panel) {
	HidePanels();
	if (window.HideVDLinks) HideVDLinks();
	SelectedPanel = getId("HeaderPanel_" + panel);
	SelectedPanel.style.display = "";
	SelectedPanel.scrollTop = "0";
}

function HidePanels() {
  if (SelectedPanel != null) {
    SelectedPanel.style.display = "none";
    SelectedPanel = null;
  }
}

function ChangeLot(value) {
  if (PageLoaded) {
	  getId("xWorkingLotID").value = value;
	  getId("xAction").value = "NewLot";
	  FormSubmit();
  }
}

// ********************************************************************************************************
// Events

function SuppressStatus() { window.status = ""; return true; }

function CancelBubble(e) {
	if (document.all) {
		e.cancelBubble = true;
	} else {
		e.preventDefault();
		e.stopPropagation();
	}
}
