var KNVHTML = new Object();
KNVHTML.GetBodyPrint = function( ID)
{
    return RSURLCst("KNVWS.asp?%5Ffunction=GetBodyPrint&ID=") + ID;
}

function KNVOpenPopup( URL, Height, Width) 
{
    open( URL, "_new", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,height=" + Height + ",width=" + Width + ",left="+(200 + screenLeft)+",top="+(150 + screenTop));
}

function KNVOpenPopupMiddle( URL, Height, Width) 
{
    open( URL, "_new", "toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=no,height=" + Height + ",width=" + Width);
}

function KNVOpenNormal( URL, Frame)
{
    open( URL, Frame);
}

// RSChange(41,MU,2007.08.27,"Refactoring the templates")
function KNVGoToItemByName( Sel, TargetFrame)
{
    // RSChange( 101,PR,2007.04.24,"Setting the target frame")
    KNVOpenNormal( RSURLCst( "KNVPrint.asp") + "?Name=" + encodeURIComponent( Sel), TargetFrame);
    // RSChangeEnd(101)
}

// RSChange(42,MU,2007.08.30,"Fix the Bugs in the Translation")
function ShowHelpPage( Sel) 
// RSChangeEnd(42)
{
    // This method cannot be moved within the template KNVHelpButton
    // This KNVHelpButton will be always called within another template
    // But, We have a constraint that KNVOnce tag can be used only once in a template
    // RSChange(42,MU,2007.08.30,"Fix the Bugs in the Translation")
    var URL = RSURLCst( 'KNVStatic/KNVHelp.htm') + '#' + encodeURIComponent( Sel);
    KNVOpenPopup( URL, 100, 200);
}

function ShowAddCommentPage( Sel) 
{
    // I tried to move this function in to the KNVAddComment Template
    // But, The toolbar does'nt show the comment Icon If I use KNVOnce
    // Need to confirm
    var URL = RSURLCst( 'KNVAdd.asp') + '?TypeName=KNVCommentType' + '&SuperID=' + encodeURIComponent( Sel) + '&Popup=1';
    KNVOpenPopup(URL, 300, 450);
    // RSChangeEnd(42)
}

function KNVEventManagerCreate()
{
    var obj = new Object();
    obj.url = window.location.href;
    obj.onKNVEvent = function(name) 
    {
	alert("Manager called");
        var args = [];
        for (var i = 1; i < arguments.length; i++)
            args.push(arguments[i]);
        if (typeof obj["on" + name] == "function")
            obj["on" + name].apply(null, args);
        if (name == "FatalError")
            obj.raiseEvent("Finalization", obj);
    }
    return obj;
}

function KNVEvent( EventType, EventArg)
{
    if (window.parent.frames.length !=0)
    {
	for ( i = 0; i < window.parent.frames.length; i++)
	{
	    if ( window.parent.frames( i) != window)
	    {
		if (window.parent.frames( i))
		{
		    // Accessing frame 0 will fail on self: due to security reason, see PARSE_SECURITY_URL
		    if (document.location.protocol != "self:")
		    {
			var Doc = window.parent.frames(i).document;
			if (Doc)
			{
			    var Mgr = Doc.getElementById("KNVQueueManager");
			    if (Mgr)
			    {
				//alert("Sending Event " + EventType + "(" + EventArg + ")" + "to frame " + i);
				var eventObj = document.createEventObject();
				//eventObj.type = 'click';
				//eventObj.x = 15;
				Mgr.href = EventArg;
				Mgr.alt = EventType;
				Mgr.fireEvent( "onClick", eventObj);
			    }
			    else
			    {
				// alert("No managerFound");
			    }
			}
		    }
		}
	    }
	}
    }
}
// RSChangeEnd( 103)

function PrintTodayOrYesterdayWithTag( DayToPrint, Day)
{
    return (' <html:abbr title="' + DayToPrint + '">' + Day + '</html:abbr> ');
}
function DocWriteLocalDateDayOnly( d)
{
    // display today and yesterday instead of date
    // Today and Yesterday must be computed in User time
    // recent = less than 2 days
    var DayToPrint = d.toLocaleDateString();
    // Test for today must be done in local time
    var Today = new Date();
    // All arithmetic on Date are only possibleof Time in milliseconds, no helper fcts in API see RI729
    // Being Today in Local time means being between Midnight and next Midnight
    // So we reset Today Hours
    // This is 0 AM
    Today.setHours( 0,0,0,0);
    var OneDayinMilliSecond = 24*60*60*1000;
    if (( d.getTime() >= Today.getTime()) && ( d.getTime() <= Today.getTime() + OneDayinMilliSecond))
    {
        // RSChange(42,MU,2007.08.30,"Fix the Bugs in the Translation")
        // RSChange(44,MU,2007.09.12,"Residual update in External Type")
        DayToPrint = PrintTodayOrYesterdayWithTag( DayToPrint, RSLNGCst( 'Today'));
        // RSChangeEnd(44)
        // RSChangeEnd(42)
    }
    else
    if (( d.getTime() >= Today.getTime() - OneDayinMilliSecond) && ( d.getTime() <= Today.getTime()))
    {
        // RSChange(42,MU,2007.08.30,"Fix the Bugs in the Translation")
        // RSChange(44,MU,2007.09.12,"Residual update in External Type")
        DayToPrint = PrintTodayOrYesterdayWithTag( DayToPrint, RSLNGCst( 'Yesterday'));
        // RSChangeEnd(44)
        // RSChangeEnd(42)
    }
    else
    {
        // Prevent date to be splitted
        var re;
        re = / /g;
        // Tue Oct 23 2009 1:53 PM
        DayToPrint = d.toDateString();
        DayToPrint = DayToPrint.replace( re, "&nbsp;");
    }
    // I want to highlight recent elements. Not asked by Bruno
    // highlight = use of css class KNVRecentItemsDate
    var Recent = ( ( Today.getTime() + OneDayinMilliSecond - d.getTime()) <= 2*OneDayinMilliSecond);
    if(Recent)
    {
        document.write('<span class="KNVRecentItemsDate">' + DayToPrint + '</span>');
    }
    else
    {
        document.write( DayToPrint);
    }
}

function DocWriteLocalHourInDay( d)
{
    // The toLocaleTimeString returns the time with seconds as a String
    // There is no API to get the local time without seconds
    // I am using the split method to manipulate the result of toLocaleTimeString
    var TimeString = d.toLocaleTimeString().split(":");
    document.write( "&nbsp;" + TimeString[0] + ":" + TimeString[1]);
    // TimeString[2] is splitted again to get AM/PM
    TimeString = TimeString[2].toString().split(" ");
    document.write( "&nbsp;" + TimeString[1]);
    // RSChangeEnd(44)
}

// RSChange( 103,PR,2007.05.04,"Adding a new feature to display and set user experience")
// RSChange(42,MU,2007.08.30,"Fix the Bugs in the Translation")
// KNVExpSetValue and KNVExpChangeValue are not specific to a Single template
// It is used in Templates of all the Experience Criteria
// It is not possible to move this function from KNV.js
// Unless we combine all the User Criteria in to a single template
// RSChangeEnd(42)
function KNVExpSetValue( ExpValue, SpanElem)
{
    var obj = SpanElem.childNodes;  
    // access child table element from parent span entity
    for( var i = 0 ; i < obj.length ; i++)
    {
	if (obj.item(i).tagName == "table")
	{
	    var userlevel = obj.item( i ).getElementsByTagName("img");
	    // sets the image src according to the user level selected 
	    for( var j = 0 ; j < userlevel.length ; j++ )
	    {	   
		if( ( ExpValue / 25 ) >= j )
		    userlevel.item(j).src = RSURLCst("KNVImages/KNVBlueBar.gif");
		else
		    userlevel.item(j).src = RSURLCst("KNVImages/KNVGrayBar.gif");
	    }
	}
    }
}
function KNVExpChangeValue( ID, ExpTypeNum, NewValue, SpanElem )
{
    // Set user experience according to the selected type (like User Evaluation, User Agreement, User Conformance )
            
    // Changes the level of experience in display of blue and gray bars
    KNVExpSetValue( NewValue, SpanElem);            
    // Navigates to KNVSetUserExperience page with the new values to set user experience 
    // RSChange(41,MU,2007.08.27,"Refactoring the templates")
    navigate( RSURLCst( "KNVSetUserExperience.asp") + "?ID=" + encodeURIComponent( ID) + "&ExperienceType=" + encodeURIComponent( ExpTypeNum) + "&ExperienceValue=" + encodeURIComponent( NewValue) );
    // RSChangeEnd(41)
}
function KNVReplace( elem, Pattern, Value)
{
    if ( elem == null)
    {
        alert("D1");
        return;
    }
    // regexp style
    // a pattern constant is declared as var g = /aa/g, not ethat there is no quote around
    // var a = "/"+Pattern+"/g";
    // this just declare a string, not a pattern
    var Pattern1 = Pattern; // /+Pattern+/g;
    if (elem.nodeType == 3)
    {
        // TextNode
        // tested
        elem.data = elem.data.replace( Pattern1, Value);
        return;
    }
    // yes uppercase event in XHTML, remember XHTML is loaded as HTML
    if (elem.tagName == "A")
    {
        // tested
        elem.href = elem.href.replace( Pattern1, Value);
    }
    var aNodeList = elem.childNodes;
    if ( aNodeList == null)
    {
        // never occurs
        return;
    }
    var i;
    for (i = 0; i < aNodeList.length; i++) 
    {
        // tested
        var sunb = aNodeList.item(i);
        KNVReplace( sunb, Pattern, Value)
    }
}
function KNVGetElemValue( ID, Def)
{
    var Frm = window.parent;
    // you get an error when accessing any slot of Frm
    var Doc = Frm.document;
    if (Doc == null)
    {
        return Def;
    }
    var elem = Doc.getElementById( ID);
    if (elem == null)
    {
        alert("F");
        return "";
    }
    return elem.value;
}

