// JScript source code
function toXml(Content)
{
	var m_Content=new String(Content);
	m_Content=m_Content.replace("&","&amp;");
	m_Content=m_Content.replace("\"","&quot;");
	m_Content=m_Content.replace("<","&lt;");
	m_Content=m_Content.replace(">","&gt;");
	return m_Content;
}

// replace a parameter value from a URL location
function ChangeParameter(Location,ParaName,Value)
{
	var m_location=new String(Location);
	var m_token_pos=m_location.lastIndexOf("?");
	if (m_token_pos==-1)
		m_location+="?"+ParaName+"="+Value;
	else
	{
		var m_location_file=new String(m_location.substr(0,m_token_pos));
		var m_location_parameter=new String(m_location.substring(m_token_pos+1,m_location.length));
		var m_parameters=m_location_parameter.split("&");
		var m_new_parameter=new String("");
		
		for (i=0;i<m_parameters.length;i++)
		{
			if (m_parameters[i].length>=ParaName.length)
				if (m_parameters[i].substr(0,ParaName.length)==ParaName)
					m_parameters[i]=ParaName+"="+Value;
			
			m_new_parameter+=m_parameters[i]+"&";
		}
		
		if (m_new_parameter=="")
			m_new_parameter=ParaName+"="+Value;
		else if (m_new_parameter.lastIndexOf(ParaName)==-1)
			m_new_parameter+=ParaName+"="+Value;
		else
			m_new_parameter=m_new_parameter.substr(0,m_new_parameter.length-1);
		
		m_location=m_location_file+"?"+m_new_parameter;
	}
	return m_location;
}

// report print
function PrintData(TableName,Filter)
{
	var m_parameter=new Array();
	m_parameter[0]="\t<table>"+toXml(TableName)+"</table>\r\t<filter>"+toXml(Filter)+"</filter>\r";
	window.showModalDialog("/bin/print_config.asp?table="+TableName,m_parameter,"status:no;help:no;dialogHeight:25;dialogWidth:37;");
}

// date selecter
function DateSelector(DateControl)
{
	var m_date=window.showModalDialog("/bin/date_selector.asp"+(document.all[DateControl].value==""?"":"?date="+document.all[DateControl].value),"","status:no;help:no;dialogHeight:22;dialogWidth:25;");
	if (!(m_date==""||m_date==null||typeof(m_date)=="undefined"))
		document.all[DateControl].value=m_date;
}

// customer selecter
function CustomerSelector(CustomerListCtl)
{
	var m_customer=window.showModalDialog("/bin/customer_selector.asp"+(document.all[CustomerListCtl].value==""?"":"?customer="+document.all[CustomerListCtl].value),"","status:no;help:no;dialogHeight:30;dialogWidth:36;");
	if (!(m_customer==""||m_customer==null||typeof(m_customer)=="undefined"))
		document.all[CustomerListCtl].value=m_customer;
}

// user selecter
function UserSelector(UserListCtl,SingleSelect)
{
	var m_parameter=new Array();
	m_parameter[0]=document.all[UserListCtl+"_xml"].value;
	var m_user_xml=window.showModalDialog("/bin/user_selector.asp?single_select="+(SingleSelect?"1":"0"),m_parameter,"status:no;help:no;dialogHeight:20;dialogWidth:30;");
	if (!(m_user_xml==""||m_user_xml==null||typeof(m_user_xml)=="undefined"))
	{
		document.all[UserListCtl+"_xml"].value=m_user_xml;
		var m_Dom=new ActiveXObject("msxml2.domdocument");
		m_Dom.loadXML(m_user_xml);
		var m_Users=m_Dom.selectNodes("/users/user");
		document.all[UserListCtl].value="";
		for (i=0;i<m_Users.length;i++)
			document.all[UserListCtl].value+=m_Users[i].selectSingleNode("./@name").value+"; ";
	}
	
// XML format for users
////////////////////////////////////////////////////////////
// <users>
//		<user id="1" name="XXXXX" />
//		<user id="2" name="XXXXX" />
// </users>
}

// attachment file selector
function AttachmentSelector(AttachmentListCtl)
{
	var m_parameter=new Array();
	m_parameter[0]=document.all[AttachmentListCtl+"_xml"].value;
	var m_attachment_xml=window.showModalDialog("/bin/attachment_selector.asp",m_parameter,"status:no;help:no;dialogHeight:20;dialogWidth:30;");
	
	
	if (!(m_attachment_xml==""||m_attachment_xml==null||typeof(m_attachment_xml)=="undefined"))
	{
		document.all[AttachmentListCtl+"_xml"].value=m_attachment_xml;
		var m_Dom=new ActiveXObject("msxml2.domdocument");
		m_Dom.loadXML(m_attachment_xml);
		var m_Attachments=m_Dom.selectNodes("/attachments/attachment");
		document.all[AttachmentListCtl].value="";
		for (i=0;i<m_Attachments.length;i++)
			document.all[AttachmentListCtl].value+=m_Attachments[i].selectSingleNode("./@filename").text+"; ";
	}
	
// XML format for attachment files
////////////////////////////////////////////////////////////
// <attachments>
//		<attachment id="1" filename="XXXXX" folder="" />
//		<attachment id="2" filename="XXXXX" folder="" />
// </attachments>
}

// formula builder
function FormulaBuilder(FormulaControl,TableList)
{
	var m_parameter=new Array();
	m_parameter[0]=document.all[FormulaControl+"_xml"].value;
	var m_formula_xml=window.showModalDialog("/bin/formula_builder.asp?tablelist="+TableList,m_parameter,"status:no;help:no;dialogHeight:29;dialogWidth:30;");
	if (!(m_formula_xml==""||m_formula_xml==null||typeof(m_formula_xml)=="undefined"))
	{
		var m_Dom=new ActiveXObject("msxml2.domdocument");
		m_Dom.loadXML(m_formula_xml);
		document.all[FormulaControl].value=m_Dom.selectSingleNode("/formula/express").text;
		document.all[FormulaControl+"_xml"].value=m_formula_xml;
		//alert(m_formula_xml);
	}
}

