/**

Ext JS Helper
ext_js_helper.js

Created By   : Myron L Stewart
Created      : 08/09/2007
Last Updated : 08/10/2007

A collection of functions and classes and wrappers for Ext JS

*/


var g_dlgWindow = null;
 
function createDialogEx(src, dialogTitle, iframeID, iframeName, dialogWidth, dialogHeight, buttonList)
{

	if(dialogWidth == null)
	{
		dialogWidth = 560;
	}

	if(dialogHeight == null)
	{
		dialogHeight = 600;
	}

	if((childNode = Ext.get("dialogContent").child("iframe")) != null)
	{
		childNode.remove();
	}
	
	Ext.get("dialogContent").createChild({tag: 'iframe', id:iframeID, name:iframeName, allowtransparency: "true", style: "background:transparent;", width: dialogWidth - 10, height: "100%", border:0,frameborder: 0, src: src});
	
	if(g_dlgWindow == null)
	{
	
		g_dlgWindow = new Ext.BasicDialog("dialog-frame", {
			width: dialogWidth,
			height: dialogHeight,
			minHeight: 100,
			minWidth: 150,
			modal: true,
			proxyDrag: true,
			shadow: true,
			//animateTarget: "account_info",
			resizable: false,
			title: dialogTitle
		});
		/*
		g_dlgWindow.addKeyListener(27, g_dlgWindow.hide, g_dlgWindow); // ESC can also close the dialog
		g_dlgWindow.addButton('Save', function()
		{
			Ext.MessageBox.alert('Status', 'Changes saved successfully.', function(){g_dlgWindow.hide();});
		}
		, g_dlgWindow);    // Could call a save function instead of hiding
		g_dlgWindow.addButton('Cancel', g_dlgWindow.hide, g_dlgWindow);
		*/
		
		if(buttonList != null)
		{
			for(itm in buttonList)
			{

				g_dlgWindow.addButton(buttonList[itm]['label'], buttonList[itm]['handler'], g_dlgWindow);
			}
		}
		else
		{
			g_dlgWindow.addButton('Close', g_dlgWindow.hide, g_dlgWindow);
		}
	
	}
	
	g_dlgWindow.show();
	
	return false;
}



function initButtons(buttonList, urlBase)
{

	if(Ext) // Make sure Ext JS exists
	{
		
		for(itm in buttonList)
		{
			//alert(btnItem);
			
			btnRef = new Ext.Button(buttonList[itm]['id'], buttonList[itm]);
			
			if(buttonList[itm]['href'] && !buttonList[itm]['handler'])
			{
				btnRef.on("click", function(e)
				{
					window.open(urlBase + this.href, "_top");
				});
			}
			
		}
		/**/
	}
}

function initFields(fieldList)
{

	//Ext.form
	if(Ext)
	{
		for(itm in fieldList)
		{
		
			if(fieldList[itm]['type'] == "text")
			{
				fldRef = new Ext.form.TextField(fieldList[itm]);
			}
			else if(fieldList[itm]['type'] == "select")
			{
				//fldRef = new Ext.form.ComboBox(fieldList[item]);
			}
			else if(fieldList[itm]['type'] == "textarea")
			{
				fldRef = new Ext.form.TextArea(fieldList[itm]);
			}
			else
			{
				fldRef = new Ext.form.TextField(fieldList[itm]);
			}
			
			//alert("Field ID " + fieldList[item]['id']);
			
			fldRef.applyTo(fieldList[itm]['id']);
			
			/*
			fldRef.on("click", function(e)
			{
				window.open(urlBase + this.href, "_top");
			});
			*/
		}
	}
}

function convertForm(targetForm)
{
	
	//console.log(targetForm);
	
	
	for(field in targetForm)
	{
		
		var fieldID = "";
		var fieldType = "";
		//console.log(field + " = " + targetForm[field].id);
		
		try
		{
			fieldID = targetForm[field].id;
			fieldType = targetForm[field].type;
		}
		catch(e)
		{
		}
		
		if(fieldID != null)
		{
			
			var opt = {"id": fieldID, style: "position: absolute"};
			//var testField = new Ext.form.TextField( {id: "ac_firstName", value : "test", type: "text", style: "position: absolute"}  );

			//console.log(field + " = " + fieldID + " type = " + fieldType);

			var fldRef = null;
			
			if(fieldType == "text" || fieldType == "password")
			{
				opt.inputType = fieldType;
				fldRef = new Ext.form.TextField(opt);
			}
			else if(fieldType == "select-one")
			{
				fldRef = new Ext.form.ComboBox({
				typeAhead: true,
				triggerAction: 'all',
				transform:fieldID,
				width:135,
				forceSelection:true,
				//shadow: "frame",
				maxHeight: 128
				});
			}
			else if(fieldType == "checkbox")
			{
				//fldRef = new Ext.form.ComboBox(fieldList[item]);
				fldRef = new Ext.form.Checkbox(opt);
			}
			else if(fieldType == "textarea")
			{
				fldRef = new Ext.form.TextArea(opt);
			}
			else
			{
				//fldRef = new Ext.form.TextField(fieldID);
			}
			
			//alert("Field ID " + fieldList[item]['id']);
			if(fldRef != null)
			{
				var elem = Ext.get(fieldID);
				
				//elem.applyStyles("font-size: 11px; height: 18px; margin: 0 auto");
				
				//get the height of the field
				var fieldHeight = 0;
				
				if(fieldType == "select-one")
				{
					fieldHeight = elem.getHeight() + 26;
					//console.log("SELECT");
				}
				else
				{
					fieldHeight = elem.getHeight() + 6;
				}
				
				//Add a wrapper layer to contain the drow shadow
				//elem.wrap("<div style='position: absolute;'>");
				//var conRef = elem.findParent("td");
				
				//increase the height of the field
				//Ext.get(conRef).applyStyles("height: " + fieldHeight + "px");
				
				//console.log(conRef);
				
				//divRef.applyStyles("border: 2px solid #000000");
				//console.log(field + " = " + targetForm[field].id + " type = " + fieldType);
				
				if(fieldType == "select-one")
				{
				}
				else
				{
					//fldRef.applyTo(elem);
					//var sdw = new Ext.Shadow({mode: "frame"});
					//sdw.show(elem);	
				}

			}
		}
		
	}
}


function initToolTips(itemList)
{
	
	Ext.QuickTips.init();

	for(ttItem in itemList)
	{
		

		Ext.QuickTips.register(
			{
		   	target:  Ext.get(itemList[ttItem]['target']),
		   	text: itemList[ttItem]['text'],
		   	autoHide:true
		 	}
			);

	}
}



function resetField(fieldName)
{

	var fieldRef = Ext.get(fieldName);
	
	if(fieldRef == null)
	{
		return;
	}
	
	fieldRef.removeClass("FieldError");
}

function validateFieldByParent(fieldRef, parentField)
{

	if(validateField(parentField) == 0)
	{
		return validateField(fieldRef);
	}
	else
	{
		return 1;
	}

}


function validateFieldByParentValue(fieldRef, parentField, fieldValue)
{

	//alert( "parent value : " + getFieldValue(parentField) );

	if( fieldValueExists(parentField, fieldValue) )
	{
		return validateField(fieldRef);
	}
	else
	{
		return 1;
	}

}


function getFieldValue(fieldRef)
{
	if(fieldRef == null)
	{ return -1; }
	
	var parentDiv = fieldRef.findParent("div[id^=sect']");
	var subField = Ext.get("display_" + parentDiv.id);
	
	var fieldValue = "";
	
	if(subField != null)
	{
		if(subField.getValue() == 0)
		{ return -1; }
	}

	if($("#" + fieldRef.id).attr("type") == "radio")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked)
			{
				fieldValue += this.value;
				checkedItems++; 
			}
		});
		
		//alert(fieldValue);
		
		return (checkedItems > 0) ? fieldValue : "";
	}
	else if($("#" + fieldRef.id).attr("type") == "checkbox")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked)
			{
				fieldValue += this.value;
				checkedItems++;
			}
		});
		
		//alert(fieldValue);
		
		return (checkedItems > 0) ? fieldValue : "";
	}
	else
	{
		//return (fieldRef.getValue() != "") ? 1 : 0;
		return fieldRef.getValue();
	}
}


function fieldValueExists(fieldRef, fieldValue)
{
	if(fieldRef == null)
	{ return false; }
	
	var parentDiv = fieldRef.findParent("div[id^=sect']");
	var subField = Ext.get("display_" + parentDiv.id);
	
	if(subField != null)
	{
		if(subField.getValue() == 0)
		{ return false; }
	}

	if($("#" + fieldRef.id).attr("type") == "radio")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked && (fieldValue == this.value))
			{
				checkedItems++; 
			}
		});
		
		return (checkedItems > 0);
	}
	else if($("#" + fieldRef.id).attr("type") == "checkbox")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked && (fieldValue == this.value))
			{
				checkedItems++;
			}
		});
		
		//alert(fieldValue);
		
		return (checkedItems > 0);
	}
	else
	{
		return (fieldRef.getValue() == fieldValue);
	}
}


function validateField(fieldRef)
{
	if(fieldRef == null)
	{ return -1; }
	
	var parentDiv = fieldRef.findParent("div[id^=sect']");
	
	//alert(parentDiv.id);
	
	//alert("display_" + parentDiv.id);
	
	//console.log("display_" + parentDiv.id);
	
	var subField = Ext.get("display_" + parentDiv.id);
	
	//alert(subField);
	
	//console.log("subField " + parentDiv.id);
	
	if(subField != null)
	{
		if(subField.getValue() == 0)
		{ return -1; }
	}

	if($("#" + fieldRef.id).attr("type") == "radio")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked)
			{ checkedItems++; }
		});
		
		return (checkedItems > 0) ? 1 : 0;
	}
	else if($("#" + fieldRef.id).attr("type") == "checkbox")
	{
		var checkedItems = 0;
		$("input[@name='" + fieldRef.id + "']").each(function (i) 
		{
			if(this.checked)
			{ checkedItems++; }
		});
		
		return (checkedItems > 0) ? 1 : 0;
	}
	else
	{
		return (fieldRef.getValue() != "") ? 1 : 0;
	}
}