function showrow(cname)
{
	var i;
	var oCont,oMode;
	
	oCont = document.getElementById(cname + '__gridheader');
	if (oCont != null)
		oCont.style.display = '';
	
	for(i=1;i<10000;i++)
	{
		oCont = document.getElementById(cname + '_' + i.toString() + '__container');
		oMode = document.getElementById(cname + '_' + i.toString() + '__mode');
		
		if (oCont == null || oMode == null) 
		{
			alert('Please save the form for more empty rows');
			return;
		}
		if (oCont.style.display == 'none' && oMode.value == 'b')
		{
			oCont.style.display = '';
			oMode.value = 'i';
			return;
		}
	}
}

function delrow(cname,cdesc)
{
	if (cdesc == '') cdesc = 'entry';
	
	if (!window.confirm('Are you sure you want to remove this ' + cdesc + '?')) return;
	
	oCont = document.getElementById(cname + '__container');
	oMode = document.getElementById(cname + '__mode');
	
	oCont.style.display = 'none';
	if (oMode.value == 'i')
		oMode.value = 's';
	else
		oMode.value = 'd';
}

function isNaDate(strDate)
{
	var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{1,2}|\d{4})$/;
	var day;
	var month;
	var year;
	var matcharray;
	
	if (strDate == '') return false;

	matcharray = strDate.match(datePat);
	if (matcharray == null) return true;
	
	month = matcharray[1];
	day = matcharray[3];
	year = matcharray[5];

	if (year > 99 && year < 1753) return true;

	if (month < 1 || month > 12) return true;
		
	if (day < 1 || day > 31) return true;
		
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) return true;
		
	if (month == 2)
	{
		if (day > 29) return true;
		if (!((year % 4) == 0 && (year % 100) != 0 || (year % 400) == 0) && (day == 29)) return true;
	}
	return false;
}

function checkmode(modeId)
{
	var cMode;
	
	if (modeId == '') return true;

	cMode = document.getElementById(modeId).value;

	if (cMode == 'i' || cMode == 'u')
			return true;
		
	return false;

}

function validatefield(elementId,modeId,Reqd,FieldType)
{
	var oElem;
	var bValid;
	
	if (!checkmode(modeId)) return true;

	oElem = document.getElementById(elementId);
	bValid = true;
	
	if (Reqd && oElem.value == '') bValid = false;	
	
	if (bValid)
	{
		switch(FieldType)
		{
			case 2:
			case 8:
				if (isNaN(oElem.value)) bValid = false;
				break;
			case 5:
				if (isNaDate(oElem.value)) bValid = false;
				break;
			case 7:
				if (isNaN(oElem.value.replace(/\x24/g, ''))) bValid = false;
				break;
		}
	}
	
	if (bValid)
	{
		oElem.className.replace(' brfinvalid','');
		return true;
	}
	else
	{
		oElem.className += ' brfinvalid';	
		return false;
	}
}

function validateFCK(instanceName,modeId)
{
	if (!checkmode(modeId)) return true;

	if (FCKeditorAPI.GetInstance(instanceName).GetHTML() == '')
	{
		document.getElementById(instanceName + '_cell').className += ' brfinvalid';
		return false;
	}
	else
	{
		document.getElementById(instanceName + '_cell').className.replace(' brfinvalid','');
		return true;
	}
}

function removefile(baseId,oLink)
{
	if (document.getElementById(baseId + '__remove').value == '')
	{
		if (window.confirm("Are you sure you want to remove this file?"))
		{
			document.getElementById(baseId + '__remove').value = 'y';
			document.getElementById(baseId + '__container').style.display = 'none';
			oLink.style.display = 'none';
		}
	}
}

function validatefile(baseId,modeId)
{
	var bValid;
	
	if (!checkmode(modeId)) return true;

	bValid = false;

	if (document.getElementById(baseId).value != '')
		bValid = true;
	else
	{
		if (document.getElementById(baseId + '__orig').value != '')
			if (document.getElementById(baseId + '__remove').value == '') bValid = true;
	}

	if (bValid)
		document.getElementById(baseId).className.replace(' brfinvalid','');
	else
		document.getElementById(baseId).className += ' brfinvalid';

		
	return bValid;	
}


var oCal;
var oCalDlg;
var oCalSel;

var oAutoComp;
var oAutoCompDS;
var oAutoCompDlg;
var oAutoCompBtnSel;
var oAutoCompIDSel;
var oAutoCompDispSel;
	
function fnOnCalSelect(type,args,obj)
{
	var dates = args[0];   
	var date = dates[0];   
	var year = date[0], month = date[1], day = date[2];   
	var evt;
	
	if (oCalSel)
		oCalSel.value = month + "/" + day + "/" + year;   	
		
	oCalDlg.hide();			

	if (oCalSel)
	{
		if (oCalSel.fireEvent)
			oCalSel.fireEvent('onChange');
		else
		{
			evt = document.createEvent('HTMLEvents');
			evt.initEvent('change', true, true);
			oCalSel.dispatchEvent(evt);
		}
	}
}


function fnOnCalCancel(type,args,obj)
{
	oCalDlg.hide();
}
		
function CalendarLookup(oButton,idField)
{
	oCalSel = document.getElementById(idField);

	if (!oCal)
	{
		oCal = new YAHOO.widget.Calendar("yuicaldiv" , {iframe:false, navigator: true});		

		oCal.selectEvent.subscribe(fnOnCalSelect, oCal, true);  	
		oCal.render();								

		oCalDlg = new YAHOO.widget.Dialog(
			"yuicaldlgdiv",
			{
				buttons:[ {text:"Cancel", handler: fnOnCalCancel}],
				width:"16em",
				modal: true,
				visible:false,
				draggable: false,
				close:true
			});		
						
		oCalDlg.render();															
									
	}
										
	if (oCalSel.value != '' && !isNaDate(oCalSel.value))
	{
		oCal.select(oCalSel.value);
		
		var selectedDates = oCal.getSelectedDates();   
		if (selectedDates.length > 0)
		{   
			var firstDate = selectedDates[0];   
			oCal.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());   
		}
	}	
	else
		oCal.deselectAll();	
	
	oCal.render();							
	oCalDlg.show();
	
	oCalDlg.cfg.setProperty("context",[oButton,"tl","tl"]);		
	
}
	
function fnAutoCompFormatter(oResultItem, sQuery)
{
	return oResultItem[1];
}

function fnAutoCompSelect(type,args,obj)
{			

	oAutoCompIDSel.value = args[2][0];
	oAutoCompDispSel.value = args[2][1];
					
	oAutoCompDlg.hide();
	oAutoCompBtnSel.focus();
}	
	
function AutoCompLookup(oButton,idField,idDisplay,iLookupID)
{
	var oInput, oContainer;
	
	oAutoCompBtnSel = oButton;
	oAutoCompIDSel = document.getElementById(idField);
	oAutoCompDispSel = document.getElementById(idDisplay);
	
	if (!oAutoCompDS)
	{
		oAutoCompDS = new YAHOO.widget.DS_XHR("/conforms/yuidata", ["\n", "\t"]);
		oAutoCompDS.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
		oAutoCompDS.maxCacheEntries = 0;
		oAutoCompDS.queryMatchSubset = true;
	}

	oAutoCompDS.scriptQueryAppend = "t=l&l=" + iLookupID;	
	
	if (!oAutoComp)
	{

		oInput = document.getElementById('acsearch');
		oContainer = document.getElementById('acdd');

		oAutoComp = new YAHOO.widget.AutoComplete(oInput,oContainer,oAutoCompDS);
		oAutoComp.queryDelay = 0;
		oAutoComp.formatResult = fnAutoCompFormatter;	
		oAutoComp.itemSelectEvent.subscribe(fnAutoCompSelect, oAutoComp, true);
		oAutoComp.forceSelection = true;
		oAutoComp.maxResultsDisplayed = 10;
		oAutoComp.useShadow = true;			
	}

	if (!oAutoCompDlg)
	{
		oAutoCompDlg = new YAHOO.widget.Dialog(
			"yuicacdlgdiv",
			{
				width:"20em",
				modal: true,
				visible:false,
				draggable: false,
				close:true
			});		
						
		oAutoCompDlg.render();															
							
		}

	oAutoCompDlg.hide();
	
	document.getElementById('acsearch').value = '';		
																
	oAutoCompDlg.show();
	
	oAutoCompDlg.cfg.setProperty("context",[oButton,"tl","tl"]);		
	
	document.getElementById('acsearch').focus();
	
}

function textLimit(oText,iLen)
{
	if (oText.value.length >= iLen) return false;
	
	return true;
}

/*
function br_yed(strName)
{
    var Dom = YAHOO.util.Dom,
        Event = YAHOO.util.Event;	
	
	var state = 'off';
    var myConfig = {
        height: '300px',
        width: '650px',
        animate: true,
        dompath: true,
        focusAtStart: true		
    };
	
	var yed = new YAHOO.widget.Editor(strName, myConfig);

	yed.on('toolbarLoaded', function() {
		var tableConfig = {
			type: 'menu', label: 'Insert HTML Table', value: 'inserttable', menu:
			[
				{ text: '1x1'},
				{ text: '2x1'},
				{ text: '3x1'},
				{ text: '4x1'},
				{ text: '1x2'},
				{ text: '2x2'},
				{ text: '3x2'},
				{ text: '4x2'},
				{ text: '1x3'},
				{ text: '2x3'},
				{ text: '3x3'},
				{ text: '4x3'},
				{ text: '1x4'},
				{ text: '2x4'},
				{ text: '3x4'},
				{ text: '4x4'}
			]
		}; 			
		
		var codeConfig = {
			type: 'push', label: 'Edit HTML Code', value: 'editcode'
		};
		
		this.toolbar.addButtonToGroup(tableConfig, 'insertitem');
		this.toolbar.addButtonToGroup(codeConfig, 'insertitem');
		
		this.toolbar.on('editcodeClick', function() {
												  
            var ta = this.get('element'),
                iframe = this.get('iframe').get('element');
				
            if (state == 'on') {
                state = 'off';
                this.toolbar.set('disabled', false);
                this.setEditorHTML(ta.value);
                if (!this.browser.ie) {
                    this._setDesignMode('on');
                }

                Dom.removeClass(iframe, 'editor-hidden');
                Dom.addClass(ta, 'editor-hidden');
                this.show();
                this._focusWindow();
            } else {
                state = 'on';
                this.cleanHTML();
                Dom.addClass(iframe, 'editor-hidden');
                Dom.removeClass(ta, 'editor-hidden');
                this.toolbar.set('disabled', true);
                this.toolbar.getButtonByValue('editcode').set('disabled', false);
                this.toolbar.selectButton('editcode');
                this.dompath.innerHTML = 'Editing HTML Code';
                this.hide();
            }		
			
            return false;

		}, this, true);		
		
		var oMenu = this.toolbar.getButtonByValue('inserttable').getMenu();
		
		oMenu.subscribe("click",onInsertTableClick);
		
		this.toolbar.on('inserttableClick', function(ev) {																							
													
			var oMenu = this.toolbar.getButtonByValue('inserttable').getMenu();							
													
			this.execCommand('inserthtml',oMenu.id);
			
		}, this, true);
		
		this.on('cleanHTML', function(ev) {
            this.get('element').value = ev.html;
        }, this, true);
        
        this.on('afterRender', function() {
            var wrapper = this.get('editor_wrapper');
            wrapper.appendChild(this.get('element'));
            this.setStyle('width', '100%');
            this.setStyle('height', '100%');
            this.setStyle('visibility', '');
            this.setStyle('top', '');
            this.setStyle('left', '');
            this.setStyle('position', '');

            this.addClass('editor-hidden');
        }, this, true);		
	});
	
	yed.render();
	
	this.yed = yed;
	this.state = state;

	this.preValid = function ()
	{
		this.yed.saveHTML();
	}
}
*/

function AutoCompKey(e)
{
	var keynum;
	var oLUElem;
	var oValElem;
	
	if(window.event)
	{
		if (window.event.keyCode != 46) return;

		oLUElem = window.event.srcElement;
	}
	else
	{
		if (e.which != 46) return;

		oLUElem = e.target;
	}
	
	oValElem = document.getElementById(oLUElem.id.substr(0,oLUElem.id.length-4));

	if (oValElem)
	{
		oValElem.value = '';
		oLUElem.value = '';
	}

}

function YUIRT_ShowCode(oEditor)
{
	var ta = oEditor.get('element'),
		iframe = oEditor.get('iframe').get('element');																								
								
	if (YAHOO.util.Dom.hasClass(ta,'editor-hidden'))
	{
		oEditor.cleanHTML();
		YAHOO.util.Dom.addClass(iframe, 'editor-hidden');
		YAHOO.util.Dom.removeClass(ta, 'editor-hidden');		
		oEditor.toolbar.set('disabled', true);
		oEditor.toolbar.getButtonByValue('editcode').set('disabled', false);
		oEditor.toolbar.selectButton('editcode');
		oEditor.dompath.innerHTML = 'Editing HTML Code';
		oEditor.hide();	
	}
	else
	{
		oEditor.toolbar.set('disabled', false);
		oEditor.setEditorHTML(ta.value);		
		if (!oEditor.browser.ie)
			oEditor._setDesignMode('on');			
		YAHOO.util.Dom.removeClass(iframe, 'editor-hidden');		
		YAHOO.util.Dom.addClass(ta, 'editor-hidden');
		oEditor.show();
		oEditor._focusWindow();		
	}		
	
	iframe.style.position = 'absolute';
	
	return false;
}

function YUIRT_Init(oEditor)
{

	var codeConfig = {
		type: 'push', label: 'Edit HTML Code', value: 'editcode'
	};
	oEditor.toolbar.addButtonToGroup(codeConfig, 'insertitem');

	oEditor.toolbar.on('editcodeClick', function () {YUIRT_ShowCode(this);}, oEditor, true);

	oEditor.on('afterRender', function() {									   	
		var wrapper = this.get('editor_wrapper');
				
		wrapper.appendChild(this.get('element'));
		this.setStyle('width', '100%');
		this.setStyle('height', '400px');
		this.setStyle('visibility', '');
		this.setStyle('top', '');
		this.setStyle('left', '');
		this.setStyle('position', '');

		this.addClass('editor-hidden');
	}, oEditor, true);

	oEditor.on('cleanHTML', function(ev) {
		oEditor.get('element').value = ev.html;
	}, oEditor, true);
	
	oEditor._cleanIncomingHTML = function(html) { return html; };
	
	oEditor.invalidHTML = {html: true};
}

function YUIRT_OnSave(oEditor)
{
	var ta = oEditor.get('element');
								
	if (YAHOO.util.Dom.hasClass(ta,'editor-hidden'))
		ta.value = oEditor.getEditorHTML();
	
	return false;	
}