var actionBtn = null;

if (typeof(window['showModalDialog']) == 'undefined')
{
	window.showModalDialog = function(url, action, init) 
	{
		// var dlg = window.open(url, action, init);
		
		var message = "Your browser does not support modal dialogs!";
		if (typeof(window['MSG_ALERT_NO_MODAL_DIALOGS']) != "undefined")
		{
			message = MSG_ALERT_NO_MODAL_DIALOGS;
		}
		
		alert(message);
		return false;
	};	
}

window.showXDialog = function()
{
	var url = showXDialog.arguments[0];
	var action = showXDialog.arguments[1];
	var init = "dialogWidth:405px;dialogHeight:495px;edge: Raised; center: Yes; help: No; resizable: No; status: No;";
	
	if (showXDialog.arguments.length > 2) 
	{
		init = showXDialog.arguments[2];
	}
	
	return showModalDialog(url, action, init);
}

function fAdd() {
    window.event.srcElement.form.submit();
	return false;
}

function fAlter() {
    window.event.srcElement.form.submit();    
}

function fDelete() {
	var message = "Are you sure want to delete selected record ?";
	if (typeof(window['MSG_CONFIRM_DELETE_RECORD']) != "undefined")
	{
		message = MSG_CONFIRM_DELETE_RECORD;
	}
	if (confirm(message)) 
	{
		//currForm=document.getElementById("oFormList");
		//currForm=window.event.srcElement.form;
		
		currForm = window.event.srcElement.form;
		if (null == currForm || currForm == "undefined") 
			currForm = document.getElementById("oFormList");
		
		frmEl=currForm.elements;

		for (i = 0; i < frmEl.length; i++) {
			el=frmEl[i];
			if (el.name=="id"&el.checked==true) {
				currID=el.value;
				break;
			}

		}

		var oFlag;
		oFlag=document.createElement("INPUT");
		oFlag.setAttribute("type","hidden");
		oFlag.setAttribute("value","delete");
		oFlag.setAttribute("name",currForm.sysobj+"[delete]");
		currForm.appendChild(oFlag);
		currForm.submit();

		return true;
	}
	else
	{
		return false;
	}
}

function fRefresh() {
	document.location = document.location;
}

function fSave() {
	var currForm;
	currForm = window.event.srcElement.form;
	if (checkForPatternMatchingInForm(currForm))
	{
		currForm.submit();
    	return false;
	}
}
function fAddNew() {
    var wnd,currForm,i,el,frmEl;
	currForm = window.event.srcElement.form;
	if (null == currForm || currForm == "undefined") 
		currForm = document.getElementById("oFormList");
	
	wnd=window.showXDialog(document.location.href + ((document.location.href.indexOf("?") != -1)?"&":"?") + "single="+currForm.sysobj+"&"+currForm.sysobj+"[add]=add","");
	if(wnd)document.location.href=document.location.href;
	return false;
}

function fAlterNew() {
    var wnd,currForm,i,el,frmEl,currID;
	currForm = window.event.srcElement.form;
	if (null == currForm || currForm == "undefined") 
		currForm = document.getElementById("oFormList");
	
	frmEl=currForm.elements;

	for (i = 0; i < frmEl.length; i++) {
		el=frmEl[i];
		if (el.name=="id"&el.checked==true) {
		    currID=el.value;
			break;
		}

	}
	wnd=window.showXDialog(document.location.href + ((document.location.href.indexOf("?") != -1)?"&":"?") + "single="+currForm.sysobj+"&id="+currID+"&"+currForm.sysobj+"[alter]=alter","");
	if(wnd)document.location.href=document.location.href;
	return false;
}

function fCancelNew() {
	window.returnValue=false;
    window.close();
	return false;
}

function fSaveNew() {
	var currForm,btn;
	currForm = window.event.srcElement.form;
	if (checkForPatternMatchingInForm(currForm))
	{
		btn=window.event.srcElement;
		currForm.target="oFrame";
		currForm.action="/"+document.location.pathname+"?single="+currForm.sysobj+"&"+btn.name;
		//alert(currForm.action);
		fPrepareData(currForm);
		fDisableBtn();
		currForm.submit();
		window.returnValue=true;
//		window.close();
    	return false;
	}
}

function fPrepareData(frm) {
	var currForm,i,el,oElement;
	for (i = 0; i < frm.elements.length; i++) 
	{
		el=frm.elements[i];
	    if (el.type=="checkbox") {
	        if(el.checked) el.value=1;
			else el.value=0;
			oElement=document.createElement("INPUT");
			oElement.setAttribute("type","hidden");
			oElement.value=el.value;
			oElement.name=el.id;
			frm.appendChild(oElement);
	    }
	}
}

function fEnableBtnParent()
{
	if (null != parent.actionBtn)
	{
		parent.actionBtn.removeAttribute("disabled");
	}
}

function fDisableBtn()
{
	actionBtn = window.event.srcElement;
	actionBtn.setAttribute("disabled", "disabled");
}

function fViewFile(pControlId, pFileName, pFieldType, pBase)
{
	switch (pFieldType)
	{
		case "sfile":
			window.open(pBase + "getfile.php?id=" + pFileName);
		break;
		
		case "file":
		case "img":
			window.open(pBase + "uploads/dbtable/" + pFileName);
		break;
	}
}

function fDeleteFile(pControlId, pFileName, pFieldType)
{
	var btn_view = document.getElementById("btn_view_" + pControlId);
	if (null != btn_view && btn_view != "undefined") btn_view.disabled = true;
	
	var btn_delete = document.getElementById("btn_delete_" + pControlId);
	if (null != btn_delete && btn_delete != "undefined") btn_delete.disabled = true;
	
	var el = document.getElementById("file_id_" + pControlId);
	if (null != el && el != "undefined") el.value = "delete";
	
	var control = document.getElementById(pControlId);
	if (null != control && control != "undefined") control.disabled = true;
}