/*************************************************************************************************************
enformiaRequired.js

this functions are requiter for soem tags in enfoemia tag library
function names and implementation are not to be changes !!!

how to use:
===========
include this script in each page which uses tag libs:
		&lt;script language="JavaScript" type="text/javascript"
			src="../Static/Inc/enformiaRequired.js"&gt;&lt;/script&gt;

functions
=========
name							description
----							-----------
enformiaRTESetFormSubmitEvent	set form onsubmit event to pass RTE value to hidden input
enformiaAppletUpdateHTML		handle setting RTE applet data to hidden inputs on form
enformiaRTEUpdateHTML			handle setting RTE ActiveX data to hidden inputs on form
enformiaCalendarPopup			popup calendar
enformiaPopulateDates			repopulate date select list when month or year changes
confirmDelete					prompt user to confirm delete
enformiaToggleStatusDateFields 	toggle the visibility of status date fields
*************************************************************************************************************/

/*
	set form onsubmit event to pass RTE value to hidden input
	params:
		frmName - name of the form
		type - type of RTE - applet / activex (def)
*/
function enformiaRTESetFormSubmitEvent(frmName, type)
{
	var frm=document.forms[frmName];
	if(!frm)return;
	if(!frm.enformiaRTEFlag)
	{
		frm.enformiaRTEFlag=true;
		var fn=frm.onsubmit;
		var updateFunctionName="enformiaRTEUpdateHTML(this);"
		if(type && type.toLowerCase()=="applet")
			updateFunctionName="enformiaAppletUpdateHTML(this);"
		
		//attach event
		frm.onsubmit=function()
		{
			eval(updateFunctionName);
			if(typeof fn=="function")return fn();
		}
	}
}

//handle setting RTE applet data to hidden inputs on form
function enformiaAppletUpdateHTML(frm)
{
	var el,elName,attrName,ix;
	var len=document.applets.length;
	for (var i=0;i<len;i++)
	{
		el=document.applets[i];
		elName=el.name;
		ix=elName.indexOf("$APPLETBROKER$");
		if (ix!=-1 && ix>0)
		{
			attrName=elName.substring(0,ix);
			if(frm.elements[attrName])
			{
				frm.elements[attrName].value = document.applets[elName].getHTML();
			}
		}
	}
}

//handle setting RTE ActiveX data to hidden inputs on form
function enformiaRTEUpdateHTML(frm)
{
	var el,elName,attrName,objRTEName,objRTE;
	//get all marker inputs
	for(var i=0;i<frm.elements.length;i++)
	{
		el=frm.elements[i];
		//should be an input element
		if("hidden"!=el.type)continue;
		
		elName=el.name;
		//if starts with '_ENMARKER_' and contains '_ENRTE'
		if(elName.indexOf("_ENMARKER_")==0 && elName.indexOf("_ENRTE")!=-1)
		{
			//get RTE name
			objRTEName=elName.substring(9);
			//get attr name
			attrName=objRTEName.substring(1,objRTEName.indexOf("_ENRTE"));
			
			//get RTE (differs on browsers)
			objRTE=eval("window."+objRTEName);
			if(!objRTE)objRTE=eval("frm."+objRTEName);
			
			//pass value
			if(frm.elements[attrName] && objRTE)
			{
				frm.elements[attrName].value = objRTE.GetHTML();
				/*
				//save the RTE text value to attribute named 'attrName_INNER_TEXT'
				if(frm.elements[attrName+"_INNER_TEXT"])
					frm.elements[attrName+"_INNER_TEXT"].value=objRTE.GetText();
				*/
			}
		}
	}	
}


//popup calendar
function enformiaCalendarPopup(qstring)
{
	var path=enUtils.getBaseUrl()+"Static/Common/Calendar/calendar.htm";
	if(qstring)
	{
		if(qstring.substring(0,1)!="?")qstring="?"+qstring;
		path=path+qstring;
	}
	if(!this.openedCaledar || this.openedCaledar && this.openedCaledar.closed)
	{
		this.openedCaledar = window.open(path,null,
			"width=275,height=180,resizable=0,status=0,menubar=0,scrollbars=0,fullscreen=0");
	}
	else
	{
		this.openedCaledar.location.href=path;
	}
	this.openedCaledar.focus();
	return this.openedCaledar;
}

//repopulate date select list when month or year changes
function enformiaPopulateDates(theYear,theMonth,theDay)
{
	var e=theDay.options[0].value;
	var selectedDay=theDay.options[theDay.selectedIndex].value;
	//skip if not both month and year are non blanck
	if(!theYear.options[theYear.selectedIndex].text ||
		!theMonth.options[theMonth.selectedIndex].value)return;
	//get no of days in month
	var getDaydInMonth=function(y, m)
	{
		timeA = new Date(y, m, 1);
		timeDifference = timeA - 86400000;
		timeB = new Date(timeDifference);
		return timeB.getDate();
	}
	var daysInMonth=getDaydInMonth(theYear.options[theYear.selectedIndex].text,
		theMonth.options[theMonth.selectedIndex].value);
	//empty all options
	theDay.options.length=0;
	//replace with new options
	var j=0;
	//first blank option - if was present
	if(!e)
	{
		theDay.options[j] = new Option("","");
		j++; 
	}
	if(!selectedDay)selectedDay=1;
	if(selectedDay>daysInMonth)selectedDay=daysInMonth;
	var k=1;
    for (var i = j; i-j < daysInMonth; i++)
	{
     	theDay.options[i] = new Option(k,k);
		if(k==selectedDay)
		{
			theDay.options[i].selected=true;
			//theDay.options[i].defaultSelected=true;			
		}
		k++;
    }
}
//clear the calendar controls (for cross browsers), used in the DateFormTag
function enformiaCalendarClearVal(frm,name)
{
	frm=document.forms[frm];
	var DATE_HIDDEN_EXTS=["_YY","_MM","_DD","_HO","_MI","_SE"];
	var tmpElName=null,tmpEl=null;
	for(var i=0;i<DATE_HIDDEN_EXTS.length;i++)
	{
		tmpElName=name+DATE_HIDDEN_EXTS[i];
		tmpEl=frm[tmpElName];
		if(tmpEl)
		{
			if(tmpEl.type=="text")
				tmpEl.value="";
			else if(tmpEl.type.indexOf("select")!=-1)
				tmpEl.selectedIndex=0;
		}
	}
}

//prompt user to confirm delete
function confirmDelete(URL,item,customMsg)
{
	var msg="";
	if(customMsg)msg=customMsg;
	else
	{
		if(item)msg=enMsg.get("enRequired.confirmDeleteItem").replace("~1~",item);
		else msg=enMsg.get("enRequired.confirmDelete");
	}
	var ret=confirm (msg);
	if(ret && URL)
	{
		location.href=URL;
	}
	else if(!URL)
	{
		return ret;
	}
}

/*
	toggle the visibility of status date fields
	assumes date fields are in dedicated trs with id :trFor_PUBLISH_DATE / ARCHIVE_DATE
	runs on newer browsers only
	params:
		el - combo of status
		status - status to go by
*/
function enformiaToggleStatusDateFields(el,status)
{
	if(!document.getElementById)return;
	var show=false;
	var intervalDependentStatus=5;
	if(!enUtils.isEmpty(status))
	{
		show=(parseInt(status)==intervalDependentStatus);
	}
	else
	{
		if(enUtils.isEmpty(el))el="VISIBILITY_STATUS";
		el=enUtils.objectOrGetId(el);
		if(enUtils.isEmpty(el))return;
		show=(parseInt(enUtils.ntz(enUtils.getValueOf(el)))==intervalDependentStatus);
	}
	//show or hide date fields based on el value
	enUtils.elmntShowHide("trFor_PUBLISH_DATE",show);
	enUtils.elmntShowHide("trFor_ARCHIVE_DATE",show);
}
