var actionBtn = null;
var cSortASC="ASC";
var cSortDESC="DESC";
var cSortReset="reset";

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;
	return checkForPatternMatchingInForm(currForm);
}
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 fViewNew() {
    var wnd,currForm,i,el,frmEl;
	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+"[view]=view","");
	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;
	}
   	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 fDisableSubmitOnEnter()
{
    var e = event;
    return (13 == e.keyCode)?false:true;
}

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;
}

function fSwitchSorting(pName,pSortStatus)
{
	if (pSortStatus=="false")
	{
		fSort(pName,cSortASC);
	}
	else
	{
		fSort(pName,cSortReset);
	}
}

function fSort(pName,pDirection)
{
	var currForm = document.getElementById("oFormList");
	var oHidden=document.createElement("input");
	oHidden.setAttribute("type","hidden");
	if (pDirection==cSortReset)
	{
		oHidden.setAttribute("name","sort_reset");
	}
	else
	{
		oHidden.setAttribute("name","sort["+pName+"]");
		oHidden.setAttribute("value",pDirection);
	}
	currForm.appendChild(oHidden);
	currForm.submit();
}

function fXOnLoad()
{
	var frm,els;
	if (document.forms.length>0)
	{
		frm=document.forms[0]
		els=frm.elements;
		for (var i=0; i<els.length; i++)
		{
			initialData[i]=els[i].value;
		}
	}
}

function fXOnUnLoad()
{
	if (!window.returnValue&&initialData)
	{
		frm=document.forms[0];
		realElements=frm.elements;

		for (var i=0; i<realElements.length; i++)
		{
			if (realElements[i].value!=initialData[i])
			{
				event.returnValue = "";
			}
		}
	}
}

function fMoveUp()
{
	var currForm = window.event.srcElement.form;
	if (null == currForm || currForm == "undefined")
		var currForm = document.getElementById("oFormList");

	var frmEl = currForm.elements;

	var currID;

	for (var i = 0; i < frmEl.length; i++)
	{
		var el = frmEl[i];
		if (el.name=="id" && el.checked==true)
		{
		    currID = el.value;
			break;
		}
	}

	oCurrent = document.createElement("INPUT");
	oCurrent.setAttribute("type", "hidden");
	oCurrent.setAttribute("name", currForm.sysobj + "[up]");
	oCurrent.setAttribute("value", "up");

	currForm.appendChild(oCurrent);
	currForm.submit();
}

function fMoveDown()
{
	var currForm = window.event.srcElement.form;
	if (null == currForm || currForm == "undefined")
		var currForm = document.getElementById("oFormList");

	var frmEl = currForm.elements;

	var currID;

	for (var i = 0; i < frmEl.length; i++)
	{
		var el = frmEl[i];
		if (el.name=="id" && el.checked==true)
		{
		    currID = el.value;
			break;
		}
	}

	oCurrent = document.createElement("INPUT");
	oCurrent.setAttribute("type", "hidden");
	oCurrent.setAttribute("name", currForm.sysobj + "[down]");
	oCurrent.setAttribute("value", "down");

	currForm.appendChild(oCurrent);
	currForm.submit();
}

