// ***********************************************************************************************************************
// <file name="Utilities.js">
//	<summary>Contains general utility functions for the client-side scripting</summary>
//	<owner>HAriharan Dalavai</owner>
//	<revision>$Revision: 0 $</revision>
//	<moddate>$Modtime: 12/12/01 3:40p $</moddate>
//	<modby>$Author: air4 $</modby>
//	<functions>
//		<function name="atAlert"/>
//		<function name="button_over"/>
//		<function name="button_out"/>
//		<function name="button_down"/>
//		<function name="button_down"/>
//		<function name="EscapeXML"/>
//		<function name="ExecuteXMLHTTP"/>
//		<function name="DisplayHelp"/>
//		<function name="RaiseGUIError"/>
//		<function name="RemoveXMLTag"/>
//		<function name="ReplaceAll"/>
//	</functions>
// </file>
// ***********************************************************************************************************************


// ***********************************************************************************************************************
// <function name="atAlert">
//	<summary>Default alert/message.</summary>
//	<params>
//		<param name="iStrMessage">The message to display</param>
//		<param name="iLngButtons">Buttons to display. 0 = Ok, 1 = Yes/No, 2 = Yes/No/Details.</param>
//		<param name="iLngImg">Image to use. 1 = Question, 2 = Exclamation, 3 = Stop, 9 = Info.</param>
//	</params>
//	<returns>The return value of the window opened.</returns>
// </function>
// ***********************************************************************************************************************
function atAlert(iStrMessage, iLngButtons, iLngImg)
{
	var lObjArgs = new Object();
	var lStrDisplay
	lObjArgs.popContent	= iStrMessage;
	lObjArgs.popButtons	= iLngButtons;
	lObjArgs.popImage	= iLngImg;
	
	// Determine the height/width needed for the dialog
	var lLngWidth, lLngHeight;
	var lStrLen = iStrMessage.length;
	
	if(lStrLen < 99)
	{
		lLngWidth = 350;
		lLngHeight = 50;
	}
	else if((lStrLen > 100) && (lStrLen < 200))
	{
		lLngWidth = 380;
		lLngHeight = 50;
	}
	else if((lStrLen > 199) && (lStrLen < 300))
	{
		lLngWidth = 400;
		lLngHeight = 50;
	}
	else if(lStrLen > 300)
	{
		lLngWidth = 430;
		lLngHeight = 50;
	}

	lStrDisplay = "dialogWidth:" + lLngWidth + "px;dialogHeight:" + lLngHeight + "px;"
	
	return showModalDialog("/Library/ASP/MessageWindow.asp", lObjArgs, lStrDisplay + "center:1;help:0;resizable:0;status:0;");
}


function button_over(eButton)
{
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft   = "buttonhighlight solid 1px";
	eButton.style.borderRight  = "buttonshadow solid 1px";
	eButton.style.borderTop    = "buttonhighlight solid 1px";
}

function button_out(eButton)
{
	eButton.style.borderColor = "#D6DFF7";
}

function button_down(eButton)
{
	eButton.style.borderBottom = "buttonhighlight solid 1px";
	eButton.style.borderLeft   = "buttonshadow solid 1px";
	eButton.style.borderRight  = "buttonhighlight solid 1px";
	eButton.style.borderTop    = "buttonshadow solid 1px";
}

function button_up(eButton)
{
	eButton.style.borderBottom = "buttonshadow solid 1px";
	eButton.style.borderLeft   = "buttonhighlight solid 1px";
	eButton.style.borderRight  = "buttonshadow solid 1px";
	eButton.style.borderTop    = "buttonhighlight solid 1px";
	eButton = null; 
}

function EscapeXML(iStrValue)
{
	if (iStrValue == "")	{	return "";	}
	
	var lStrReturn = iStrValue;
	lStrReturn = ReplaceAll(lStrReturn, "&", "&amp;")
	lStrReturn = ReplaceAll(lStrReturn, "<", "&lt;")
	lStrReturn = ReplaceAll(lStrReturn, ">", "&gt;")

	return lStrReturn;
}

function ExecuteXMLHTTP(iStrFile, iStrXMLCmd)
{
	var lStrResponse;
	var lObjHTTP = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	
	lObjHTTP.Open("POST", iStrFile, false);
	lObjHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-16");
	lObjHTTP.Send(iStrXMLCmd);
	
	if(lObjHTTP.ResponseXML.xml == "")
	{
		lStrResponse = lObjHTTP.ResponseText;
	}
	else
	{
		lStrResponse = lObjHTTP.ResponseXML.xml;
	}
	
	lObjHTTP = null;

	if (lStrResponse.indexOf("<aic_response><error") > 0)
	{
		RaiseGUIError("ExecuteXMLHTTP", lStrResponse);
		return "";
	}
			
	return lStrResponse;
}


function ExportCSV(iName)
{
	var lHeadNode;
	lHeadNode = gXMLDoc.selectSingleNode("//head") // XMLCustomerList.selectSingleNode("//head")
	lHeadNode.setAttribute("sView", "csv");
	lHeadNode.setAttribute("sAddOne", "");
	lHeadNode.setAttribute("sAddTwo", "");
	lHeadNode.setAttribute("bGroup", "");
			
	gXSLProc.transform();

	var lObjDivs;
	lObjDivs = document.all.tags("DIV");
	lObjDivs[lObjDivs.length-1].innerHTML = gXSLProc.output;
	var lFilename = prompt("Enter the CSV export path excluding the filename.\nExample (Valid Folders) : 'C:\\My Documents\\'.", "C:\\");
	
	if(lFilename)
	{	
		if(lFilename.lastIndexOf("\\") != (lFilename.length - 1))
		{
			lFilename += "\\";
		}
		try{
			var lObjFSO = new ActiveXObject("Scripting.FileSystemObject");
			var lFileCSV = lObjFSO.CreateTextFile(lFilename + "Export_"+ iName + ".csv", true);
	
			lFileCSV.WriteLine(lObjDivs[lObjDivs.length-1].innerText);
			lFileCSV.Close();
		}
		catch(iError){
			//Automation server can't create object
			if(iError.number == -2146827859)
			{
				RaiseGUIError("Utilities.js - ExportCSV", "Your browser may not be configured as a trusted site as we cannot create the file on your computer.<br>An error has occured while trying to create output file on your computer.");
			}
			else
			{
				RaiseGUIError("Utilities.js - ExportCSV", "An error has occured while trying to create output file on your computer<br>Error: [" + iError.number + "] " + iError.description);
			}
		}
	}
}


// ***********************************************************************************************************************
// <function name="DisplayHelp">
//	<summary>Displays a story abou help.</summary>
//	<params>
//		<param name="iSource">From where it is comign from (Future)</param>
//	</params>
//	<returns>Nothing.</returns>
// </function>
// ***********************************************************************************************************************
function DisplayHelp(iSource)
{
	atAlert("Help is available in the PDF file on the welcome screen.<br>You can get there by (saving and) closing this item. There will be then be a link to it", 0, 9)
}


// ***********************************************************************************************************************
// <function name="GetDate">
//	<summary>Gets a valid date in the ANSI Standard format.</summary>
//	<params>
//		<param name="iName">Name of input to retreive data from, excl. (Day/Month/etc.)</param>
//		<param name="iBlnHours">Weather to retreive from Hours/Minutes inputs.</param>
//	</params>
//	<returns>Valid date in ansi format</returns>
// </function>
// ***********************************************************************************************************************
function GetDate(iName, iBlnHours)
{
	var lStrDate = "";
	var lValDay, lValMonth, lValYear, lValHour, lValMinute, lValSecond;
			
	lValDay		= eval("document.frmInit.sel" + iName + "Day.value");
	lValMonth	= eval("document.frmInit.sel" + iName + "Month.value");
	lValYear	= eval("document.frmInit.sel" + iName + "Year.value");
	
	if (iBlnHours)
	{
		lValHour	= eval("document.frmInit.sel" + iName + "Hour.value");
		lValMinute	= eval("document.frmInit.sel" + iName + "Minute.value");
		lValSecond	= eval("document.frmInit.sel" + iName + "Second.value");
	}
	else
	{
		lValHour	= "";
		lValMinute	= "";
		lValSecond  = "";
	}
	
	if ((lValDay != "") || (lValMonth != "") || (lValYear != "") || (lValHour != "") || (lValMinute != ""))
	{
		if(iName == "From")
		{
			if(lValYear   != "")	lStrDate += lValYear;			else lStrDate += "1800";
			if(lValMonth  != "")	lStrDate += "-" + lValMonth;	else lStrDate += "-01";
			if(lValDay    != "")	lStrDate += "-" + lValDay;		else lStrDate += "-01";
			if(lValHour   != "")	lStrDate += " " + lValHour;		else lStrDate += " 00";
			if(lValMinute != "")	lStrDate += ":" + lValMinute;	else lStrDate += ":00";
			if(lValSecond != "")	lStrDate += ":" + lValSecond;	else lStrDate += ":00";
		}
		else
		{
			if(lValYear   != "")	lStrDate += lValYear;			else lStrDate += "2010";
			if(lValMonth  != "")	lStrDate += "-" + lValMonth;	else lStrDate += "-12";
			if(lValDay    != "")	lStrDate += "-" + lValDay;		else lStrDate += "-30";
			if(lValHour   != "")	lStrDate += " " + lValHour;		else lStrDate += " 23";
			if(lValMinute != "")	lStrDate += ":" + lValMinute;	else lStrDate += ":59";
			if(lValSecond != "")	lStrDate += ":" + lValSecond;	else lStrDate += ":59";
		}				
	}
	return lStrDate;
}


function RaiseGUIError(iStrSource, iStrDescription)
{
	var lStrError;
	var lStrDetails;
	
	lStrError = "A system error occurred. Further details are available on clicking the details button.\n";
	lStrDetails = "[" + iStrSource + "] " + iStrDescription;

	var lObjArgs = new Array(lStrError, lStrDetails);
	window.showModalDialog("/Library/ASP/ErrorMessageWindow.asp", lObjArgs, "dialogTop:200px; dialogWidth:640px; dialogHeight:205px; resizable:yes; status:no; help:no;");
}	

function RemoveXMLTag(iStrXML)
{
    var lStrTemp
    var lLngLen
    
    lStrTemp = iStrXML;
    
    if (lStrTemp.indexOf("<?xml") = 1)
    {
        lLngLen = lStrTemp.indexOf("?>");
        lStrTemp = lStrTemp.substring(lLngLen, lStrTemp.length)
    }
    
    RemoveXMLTag = lStrTemp
}

// ***********************************************************************************************************************
// <function name="ReplaceAll">
//	<summary>Replaces all occurences of specified substring in a string</summary>
//	<params>
//		<param name="iString">The string to replace in</param>
//		<param name="iStrText">String to replace</param>
//		<param name="iStrBy">Replace by this string</param>
//	</params>
//	<returns/>
// </function>
// ***********************************************************************************************************************
function ReplaceAll(iString, iStrText, iStrBy)
{
	var lIntStrLength = iString.length
	var lIntTxtLength = iStrText.length;
		
	if ((lIntStrLength == 0) || (lIntTxtLength == 0)) return iString;

	var lIndx = String(iString).indexOf(iStrText);
	if ((!lIndx) && (iStrText != iString.substring(0,lIntTxtLength))) return iString;
	if (lIndx == -1) return iString;

	var ReturnValue = iString.substring(0,lIndx) + iStrBy;
	if (lIndx+lIntTxtLength < lIntStrLength)
	{
	    ReturnValue += ReplaceAll(iString.substring(lIndx+lIntTxtLength,lIntStrLength),iStrText,iStrBy);
	}

	return ReturnValue;
}


function WriteDateInputs_nlf(iName, iBlnDisplayHour)
{
	var lCnt;
	document.write ("<table border=0 cellspacing=0 cellpadding=4>");
	document.write ("<tr>");
	document.write ("<td class='content'>Day</td>");
	document.write ("<td class='content'>Month</td>");
	document.write ("<td class='content'>Year</td>");
	if(iBlnDisplayHour) {
		document.write ("<td class='content'>Hour</td>");
		document.write ("<td class='content'>Minute</td>");
		document.write ("<td class='content'>Second</td>");
	}
	document.write ("</tr>");
	document.write ("<tr>");
	document.write ("<td><select name=\"sel" + iName + "Day\"><option value=''>All</option>");
	for(lCnt=1; lCnt<32; lCnt++)
	{
		if(lCnt < 10)
			document.write ("<option value=\"0" + lCnt + "\">" + lCnt + "</option>");
		else
			document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
	}
	document.write ("</select></td>");
	document.write ("<td><select name='sel" + iName + "Month'><option>all</option><option value=\"01\">January</option><option value=\"02\">February</option><option value=\"03\">March</option><option value=\"04\">April</option><option value=\"05\">May</option><option value=\"06\">June</option><option value=\"07\">July</option><option value=\"08\">August</option><option value=\"09\">Sepetember</option><option value=\"10\">October</option><option value=\"11\">November</option><option value=\"12\">December</option></select></td>");
	document.write ("<td><select name=\"sel" + iName + "Year\"><option>all</option>") ;
	var lDt = new Date()  
	for(lCnt=2001; lCnt<=lDt.getFullYear(); lCnt++)
	{
		document.write ("<option value=\""+ lCnt + "\">" + lCnt + "</option>");
	}
	document.write ("</select></td>") ;
	if(iBlnDisplayHour) {
		document.write ("<td><select name=\"sel" + iName + "Hour\"><option>all</option>") ;
		for(lCnt=0; lCnt<24; lCnt++)
		{
			if(lCnt < 10)
			{
				document.write ("<option value=\"0" + lCnt + "\">0" + lCnt + "</option>");
			}
			else
			{
				document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
			}
		}
		document.write("</select></td>") ;
		document.write("<td><select name=\"sel" + iName + "Minute\"><option>all</option>");
		for(lCnt=0; lCnt<60; lCnt++)
		{
			if(lCnt < 10)
			{
				document.write ("<option value=\"0" + lCnt + "\">0" + lCnt + "</option>");
			}
			else
			{
				document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
			}
		}
		document.write ("</select></td>");
		document.write("<td><select name=\"sel" + iName + "Second\"><option>all</option>");
		for(lCnt=0; lCnt<60; lCnt++)
		{
			if(lCnt < 10)
			{
				document.write ("<option value=\"0" + lCnt + "\">0" + lCnt + "</option>");
			}
			else
			{
				document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
			}
		}
		document.write ("</select></td>");		
	}
	document.write("</tr>") ;
	document.write("</table>") ;	

}



function WriteDateInputs(iName, iBlnDisplayHour)
{
	var lCnt;
	document.write ("<table cellpadding=1><tr><td colspan=2>Day</td><td colspan=2>Month</td><td colspan=2>Year</td>");
	if(iBlnDisplayHour) document.write ("<td colspan=2>Hour</td><td>Minute</td>");
	
	document.write ("</tr><tr><td><select name=\"sel" + iName + "Day\"><option value=\"\">All</option>");
	for(lCnt=1; lCnt<32; lCnt++)
	{
		if(lCnt < 10)
			document.write ("<option value=\"0" + lCnt + "\">" + lCnt + "</option>");
		else
			document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
	}
	document.write ("</select></td><td valign='middle'>/</td>");
			
	document.write ("<td><select name=\"sel" + iName + "Month\"><option value=\"\">All</option><option value=\"01\">January</option><option value=\"02\">February</option><option value=\"03\">March</option><option value=\"04\">April</option><option value=\"05\">May</option><option value=\"06\">June</option><option value=\"07\">July</option><option value=\"08\">August</option><option value=\"09\">Sepetember</option><option value=\"10\">October</option><option value=\"11\">November</option><option value=\"12\">December</option></select></td><td valign='middle'>/</td>");
			
	document.write ("<td><select name=\"sel" + iName + "Year\"><option value=\"\">All</option>");
	for(lCnt=2000; lCnt<2010; lCnt++)
	{
		document.write ("<option value=\""+ lCnt + "\">" + lCnt + "</option>");
	}
	document.write ("</select></td><td>&nbsp;</td>");
	if(iBlnDisplayHour) 
	{
		document.write ("<td><select name=\"sel" + iName + "Hour\"><option value=\"\">All</option>");
		for(lCnt=0; lCnt<24; lCnt++)
		{
			if(lCnt < 10)
			{
				document.write ("<option value=\"0" + lCnt + "\">0" + lCnt + "</option>");
			}
			else
			{
				document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
			}
		}
		document.write ("</select></td><td valign='middle'>:</td>");

		document.write ("<td><select name=\"sel" + iName + "Minute\"><option value=\"\">All</option>");
		for(lCnt=0; lCnt<59; lCnt++)
		{
			if(lCnt < 10)
			{
				document.write ("<option value=\"0" + lCnt + "\">0" + lCnt + "</option>");
			}
			else
			{
				document.write ("<option value=\"" + lCnt + "\">" + lCnt + "</option>");
			}
		}
		document.write ("</select></td>");
	}
	document.write ("</tr></table>");
}


function openBrWindow(theURL,winName,features)
{	//v2.0
	window.open(theURL,winName,features);
}