/***********************************************************************************
 * Class: CLookup
 * Dependencies: CObject
 * Description:
 *   Lookup window class
 *
 ***********************************************************************************/

	// Include pages
	if(window.CPage) {
		CPage.include("CObject");
		CPage.includeLocal("CWindowDim");
	}
	
	function CLookup() { return this; };
	
	// Append nodes
	CLookup.appendNodes = function(parentTag, node) {
		var tagName = node[0];
		var attributes = node[1];
		var textNode = null;
		var name, value;
		var i;

		// Check for name attribute in form tags
		if(document.all && attributes != null && (tagName == "input" || tagName == "form" || tagName == "iframe")) {
			// Get name attribute
			value = null;
			i = 0;
			while(i < attributes.length && attributes[i].substring(0, 5) != "name=")
				i++;
			
			// If name found, set tagname => tagname name=name
			if(i < attributes.length) {
				name = attributes[i].split(/=/)[1];
				tagName = '<'+tagName+' name="'+name+'"/>';
			}
		}

		// Create tag
		var tag = document.createElement(tagName);

		// Set attributes
		if(attributes != null) {
			for(i = 0; i < attributes.length; i++) {
				var attr = attributes[i].split(/=/);
				name = attr[0];
				value = (attr.length == 2) ? attr[1] : "";

				if(name == "class")
					name = "className";
					
				// Check if text node
				if(name == "_text")
					textNode = document.createTextNode(value);
				// If event, set it
				else if(name.substring(0, 2) == "on") {
					// If function name, dont wrap in function object
					if(value.match(/^[\w\.]+;?$/))
						eval('tag.'+name+'='+value+';');
					// Create event
					else
						eval('tag.'+name+'= new Function("'+value+'");');
				}
				// Skip name attribute
				else if(!document.all || name != "name")
					tag[name] = value;
			}
		}

		// Append to parent
		parentTag.appendChild(tag);

		// Loop through child nodes
		for(i = 2; i < node.length; i++)
			CLookup.appendNodes(tag, node[i]);

		// Append text node if any
		if(textNode != null)
			tag.appendChild(textNode);

		return tag;
	};
	
	// Create window
	CLookup.createWindow = function() {
		id = "lookup-window";
		if(CLookup.obj)
			return CLookup.obj;
		
		var htmlNodes = 
		["div", ["id=lookup-window"],
			["div", ["id=lookup-window-header"],
				["div", ["id=lookup-window-icon"],
					["a", ["href=javascript:CLookup.close();", "_text=X"]]
				],
				["div", ["id=lookup-window-title", "_text= "]]
			],
			["iframe", ["id=lookup-window-iframe", "name=lookup-window-iframe",	"frameBorder=0", "scrolling=auto", "src=javascript:void(0);"]],
			["div", ["id=lookup-window-footer", "_text= "],
				["input", ["id=lookup-window-button-cancel", "name=lookup-window-button-cancel", "type=button", "class=button", "value=Cancel", "onclick=CLookup.close"]]
			]
		];

		// Append HTML to body
		return CLookup.appendNodes(document.body, htmlNodes);
	}
	
	CLookup.init = function() {
		if(CLookup.obj)
			return;
			
		// Create window
		CLookup.obj = CLookup.createWindow();
		var id = CLookup.obj.id;
		
		// Need mask for ie
		if(document.all) {
			var mask = document.createElement("IFRAME");
			mask.id = id+"-mask";
			mask.src = "about:blank"; // javascript:void(0);
			mask.frameBorder = 0;
			mask.scrolling = "no";
			mask.className = "lookup-mask";
			document.body.appendChild(mask);
			CLookup.objMask = mask;
		}

		// Get upload objects
		CLookup.objTitle = CObject.get(id+"-title");
		CLookup.objFrame = CObject.get(id+"-iframe");
		CLookup.objButtonCancel = CObject.get(id+"-button-cancel");
		CLookup.objFrameWindow = window.frames[id+"-iframe"];
		CLookup.objForm = CObject.get(id+"-form");
		CLookup.visible = false;
	};
	
	// Update window size
	CLookup.updateSize = function(width, height) {
		if(!width)
			width = 640;
		if(!height)
			height = 265;

		// Update frame height and width
		CObject.setWidth(CLookup.obj, width);
		CObject.setHeight(CLookup.objFrame, height);
		CLookup.updatePosition();
	}

	// Center popup in window
	CLookup.updatePosition = function() {
		if(!CLookup.obj)
			return;

		// Get window and object sizes
		var windowSize = CWindowDim.getInnerSize();
		var windowScroll = CWindowDim.getScrolling();
		var size = CObject.getSize(CLookup.obj);

		// Calculate positions
		var x = parseInt((windowSize.width - size.width) / 2) + windowScroll.x;
		var y = parseInt((windowSize.height - size.height) / 2) + windowScroll.y;
					
		// Update position
		CObject.moveTo(CLookup.obj, x, y);
		if(CLookup.objMask) {
			// Update mask size
			CObject.setWidth(CLookup.objMask, size.width);
			CObject.setHeight(CLookup.objMask, size.height);
			// Move mask to behind frame
			CObject.moveTo(CLookup.objMask, x, y);
		}
	};

	// Open upload window
	CLookup.open = function(form, fieldId, titleFieldId, formScript, respFunc) {
		// Init (if not created)
		CLookup.init();

		// Ignore open command if already open - CHANGED - if visible, hide it first
		if(CLookup.visible) {
			CLookup.close();
			//return;
		}

		// Only re-init if dest field is different
		if(!CLookup.destField || CLookup.destField != CLookup.form[fieldId]) {
			// Update position
			CLookup.updatePosition();
			
			// Set window title
			var title = "Lookup";
			CLookup.setTitle("Loading...");
			CLookup.respFunc = respFunc;
			CLookup.formScript = formScript;
			CLookup.form = document.forms[form];
			CLookup.destField = CLookup.form[fieldId];
			CLookup.destTitleField = CLookup.form[titleFieldId];

			// Set script source
			CLookup.objFrameWindow.name = CLookup.objFrame.name;
			CLookup.objFrameWindow.document.location.href = formScript;
		}
		// Otherwise simply call frame onload
		else
			CLookup.frameOnload();

		// Set visible flag
		CLookup.visible = true;
		
		// Show window
		CObject.show(CLookup.obj);
		if(CLookup.objMask)
			CObject.show(CLookup.objMask);
			
		return true;
	};
	
	CLookup.ok = function() {
		;
	}
	
	// Hide window
	CLookup.close = function() {
		if(CLookup.visible) {
			// Hide window
			if(CLookup.objMask)
			   CObject.hide(CLookup.objMask);
			CObject.hide(CLookup.obj);
			
			// Hide frame
			CObject.hide(CLookup.objFrame);
			
			// Reset window size
			CLookup.updateSize(150, 10);
			
			// Reset visible flag
			CLookup.visible = false;
		}
	};

	// Select item and close window
	CLookup.selectItem = function(value, title) {
		var destField = CLookup.destField;
		
		// Reverse url encoding
		title = title.replace(/\+/g, ' ');
		
		if(destField.options) {
			// Check if item already exists in list
			var i = 0;
			while(i < destField.options.length && destField.options[i].value != value)
				i++;
			if(i < destField.options.length) {
				alert("Select item already exists in list");
				return;
			}
			
			var index = destField.options.length++;
			destField.options[index].text = title;
			destField.options[index].value = value;
		}
		else {
			// Set value and title value
			destField.value = value;
			if(CLookup.destTitleField)
				CLookup.destTitleField.value = title;
		}
		
		CLookup.close();
	};
	
	// Clear current looked up value
	CLookup.clear = function(form, fieldId, titleFieldId, defTitle, defValue) {
		var form = document.forms[form];
		var destField = form[fieldId];
		var destTitleField = form[titleFieldId];
		
		// Set value and title value
		destField.value = defValue;
		if(destTitleField)
			destTitleField.value = defTitle;
	};

	// Set window title
	CLookup.setTitle = function(title) { CLookup.objTitle.innerHTML = title; }
	
	// Init frame
	CLookup.frameOnload = function(title, width, height) {
		if(!CObject.isVisible(CLookup.objFrame)) {
			if(title)
				CLookup.setTitle(title);
			CLookup.updateSize(width, height);
			CObject.show(CLookup.objFrame);
		}
	};
	