


var win=null;

function icDisplayStatusMsg(msgStr)
{
	status=msgStr
	document.MM_returnValue = true;
}

var winFullScreen=null;
function openWinFullScreen(mypage,myname,scrollbar,toolbar,pos,margin){
w=screen.width-125;
h=screen.height-125;
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?((screen.height-h)-20)/2:100;TopPosition=TopPosition-10}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrollbar+',location=no,directories=no,status=no,menubar=no,toolbar='+toolbar+',resizable=yes';
winFullScreen=window.open(mypage,myname,settings);
if(winFullScreen.focus){winFullScreen.focus();}
}

var WinHS=null;
function openHS(mypage,myname,scrollbar,toolbar,pos,w){
h=screen.height-125;
LeftPosition = 65;
TopPosition=(screen.height)?((screen.height-h)-20)/2:100;
TopPosition=TopPosition-10;
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrollbar+',location=no,directories=no,status=no,menubar=no,toolbar='+toolbar+',resizable=yes';
WinHS=window.open(mypage,myname,settings);
if(WinHS.focus){WinHS.focus();}
}

/*
Introduced by  Mahesh on 27th March 2008.
This function was copied from the bodypagefunction.js file in ic/core directory.
Introduced it for defect reporting to launch the tree page.
*/
function showModalDialogIfPossible(theurl, thestring, thedimensions)
{ 
    strAppVersion = navigator.appVersion;
    
    if (strAppVersion.indexOf('MSIE') == -1 && strAppVersion.indexOf('Safari') == -1 )  {
        return window.open(theurl,thestring,thedimensions,true);  
    } 
    else 
    {
        return window.showModalDialog (theurl,thestring,thedimensions);
    }
}

//
var winFullScreen=null;
function openWindowScreen(mypage,myname,scrollbar,toolbar,pos,margin)
{
	w=screen.width-125;
	h=screen.height-125;
	
	if(pos=="center")
		LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?((screen.height-h)-20)/2:100;TopPosition=TopPosition-10
		
		settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scrollbar+',location=no,directories=no,status=no,menubar=no,toolbar='+toolbar+',resizable=yes';
		winFullScreen=window.open(mypage,myname,settings);
		
	if(winFullScreen.focus)
		winFullScreen.focus();
}




function OpenWin(sPageURL, sWinType, bUnique, sWinWidth, sWinHeight, bScrollBars)
{
  var nDefaultWidth  = 650;
  var nDefaultHeight = 550;
  var nDefaultScroll = 1;
  var nScroll;
  var dTime;
  var win;

  if (sPageURL != null) {
    //Default sizes for window
    if (sWinWidth == null) sWinWidth = nDefaultWidth;
    if (sWinHeight == null) sWinHeight = nDefaultHeight;
    if (bScrollBars == null || bScrollBars == true)
       nScroll = nDefaultScroll;
    else
       nScroll = 0;
  
    dTime = new Date();
    if (bUnique == null)
      bUnique = true;
    if (bUnique == true)
      sWinType = sWinType + dTime.getUTCHours() + dTime.getUTCMinutes() + dTime.getUTCSeconds() + dTime.getUTCMilliseconds();
    
    win=window.open(sPageURL, sWinType, 'left=75,top=75,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=' + nScroll + ',resizable=1,width=' + sWinWidth + ',height=' + sWinHeight);
    win.focus();
    return win;
  }
}




function PrintFrame(target)
{	
	if (target.length>0 && target != '_self'){
		if (target != '_parent') {
			parent.frames(target).focus();
		}
		else {
			parent.focus();
		}
	}
	print();
}

function oHW(pageKey, sectionName, subSectionName, appRoot)
{	
	settings =  ''
	settings += 'width=400,'
	settings += 'height=570,'
	settings += 'top=50,'
	settings += 'left=50,'
	settings += 'scrollbars=yes,'
	settings += 'status=yes,'
	settings += 'resizable=yes,'
	settings += 'toolbar=no'
	helpURL = appRoot + '/help/ic/context/displayHelpLoading.asp';
	win=window.open(helpURL,'icHelp',settings);
	document.helpHeaderValues.pageKey.value = pageKey
	document.helpHeaderValues.sectionName.value = sectionName
	document.helpHeaderValues.subSectionName.value = subSectionName
	document.helpHeaderValues.submit();
	if(win.focus){win.focus();}
}

var AssocPopUp=null;
function AssociateLaunch(assocType) {
	w = 1000;
	h = 750;
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?((screen.height-h)-20)/2:100;
	TopPosition=TopPosition-10
	
	if (assocType == 0){
		url = "/ic/Costings/AssociateDocumentFrame.asp"
	}
	else if (assocType == 1){
		url = "/ic/Costings/AssociateGenItemFrame.asp";
	}
	else if (assocType == 2){
		url = "/ic/Costings/AssociateGenItemFrame.asp?incoming=-1";
	}
	else { alert("Error: Associate Launch Type Not Recognised"); return; }
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	AssocPopUp=window.open(url,"AssocPopUp",settings);
	
	var tempAction = document.forms[0].action;
	var tempTarget = document.forms[0].target;
	var tempEncType = document.forms[0].encoding;
	document.forms[0].action = url;
	document.forms[0].target = "AssocPopUp";
	document.forms[0].encoding = "application/x-www-form-urlencoded";
	document.forms[0].submit();
	document.forms[0].action = tempAction;
	document.forms[0].target = tempTarget;
	document.forms[0].encoding = tempEncType;
	if(AssocPopUp.focus){AssocPopUp.focus();}
}

function AssociateProcessLaunch(assocType,GID,BaseGID) {
	w = 1000;
	h = 750;
	LeftPosition=(screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?((screen.height-h)-20)/2:100;
	TopPosition=TopPosition-10;
	if (assocType == 1)
		url = "/ic/GenAdmin/ItemNewAssociateGenItemFrame.asp?ListAssociatedGen=-1&BatchRespond=1&IDGenItemGroup=" + GID + "&BaseGID=" + BaseGID + "";
	else if (assocType == 2)
		url = "/ic/GenAdmin/WorkPackageAssociationFrame.asp?BatchRespond=1&IDGenItemGroup=" + GID + "&IDGenItemType=7&BaseGID=" + BaseGID + "";
	else if (assocType == 3)
		url = "/ic/GenAdmin/ItemNewAssociateDocumentFrame.asp?BatchRespond=1&IDGenItemGroup=" + GID + "&IDGenItemType=0&Skip=0&Response=0&BaseGID=" + BaseGID + "";


	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	AssocPopUp=window.open(url,"AssocPopUp",settings);
	
	var tempAction = document.forms[0].action;
	var tempTarget = document.forms[0].target;
	var tempEncType = document.forms[0].encoding;
	document.forms[0].action = url;
	document.forms[0].target = "AssocPopUp";
	document.forms[0].encoding = "application/x-www-form-urlencoded";
	document.forms[0].submit();
	document.forms[0].action = tempAction;
	document.forms[0].target = tempTarget;
	document.forms[0].encoding = tempEncType;
	if(AssocPopUp.focus){AssocPopUp.focus();}
}

var Calendar;	

// old, deprecated call to use the native .net calendar
function pickDate(Src, SetAll, Func)
{
	var target = "";
	var newLeft = (window.screenLeft + 325 - 100);
	var newTop = (window.screenTop + 275 - 80);	
	
	if (SetAll == null)
	{
		target = "/BIW/Utils/CalendarPopUp.aspx?src=" + Src;
		winSetting = 'dialogHeight:170px;dialogWidth:210px;center:yes;help:no;resizable:yes;scroll:no;status:no;';
	}
	else
	{
		target = "/BIW/Utils/CalendarPopUp.aspx?src=" + Src + "&SetAll=" + SetAll + "&Func=" + Func;
		winSetting = 'dialogHeight:190px;dialogWidth:210px;center:yes;help:no;resizable:no;scroll:no;status:no;';
	}
	
	if (Calendar) 
	{
		if (! Calendar.closed) 
		{ 
			Calendar.close();
		}
	}
	Calender = window.showModalDialog(target ,window, winSetting);
	
}

// uses the new client-only calendar
function pickDateNew(Src, SetAll, Func)
{
	var target = "";
	var newLeft = (window.screenLeft + 325 - 100);
	var newTop = (window.screenTop + 275 - 80);	
	
	if (SetAll == null)
	{
		target = "/BIW/Utils/ClientCalendar.aspx?src=" + Src;
		winSetting = 'dialogHeight:210px;dialogWidth:210px;center:yes;help:no;resizable:yes;scroll:no;status:no;';
	}
	else
	{
		target = "/BIW/Utils/ClientCalendar.aspx?src=" + Src + "&SetAll=" + SetAll + "&Func=" + Func;
		winSetting = 'dialogHeight:230px;dialogWidth:210px;center:yes;help:no;resizable:no;scroll:no;status:no;';
	}
	
	if (Calendar) 
	{
		if (! Calendar.closed) 
		{ 
			Calendar.close();
		}
	}
	Calender = window.showModalDialog(target ,window, winSetting);

}

// an all-new function to use the popup holding the Ajax calendar control. Note that this control relies upon the calling
// page having a function called setDateFromAjaxCalendarPopup, which is the recipient of the selected date. This function will
// have to know what it wants to do with the data it receives.
function pickDateAjaxNew(selectMonths)
{
	var target = "";
	var newLeft = (window.screenLeft + 325 - 100);
	var newTop = (window.screenTop + 275 - 80);	
	
	target = '/BIW/Utils/AjaxCalendarPopup.aspx?selectMonths=' + selectMonths;
	winSetting = 'dialogHeight:240px;dialogWidth:200px;center:yes;help:no;resizable:yes;scroll:no;status:no;';
	
	if (Calendar) 
	{
		if (! Calendar.closed) 
		{ 
			Calendar.close();
		}
	}
	Calender = window.showModalDialog(target ,window, winSetting);
}


// the window manager object
function windowManager()
{
	var topWin = getTopmostWindow(window);
	if (!topWin.windowManagerStore)
	{
		topWin.windowManagerStore = new windowManagerStore();
	}
	this.windowManagerStore = topWin.windowManagerStore;
}

function windowManager_Add(strName, obj)
{
	this.windowManagerStore['window'+strName] = obj;

}
windowManager.prototype.Add = windowManager_Add;

function windowManager_Exists(strName)
{
	try{
	if (this.windowManagerStore['window'+strName] && this.windowManagerStore['window'+strName].self)
		return true;
	return false;
	}
	catch(er){};
	return false;
}
windowManager.prototype.Exists = windowManager_Exists;

function windowManager_Get(strName)
{
	return this.windowManagerStore['window'+strName];
}
windowManager.prototype.Get = windowManager_Get;

// the store object that gets stuck on the top page
function windowManagerStore()
{}

function getTopmostWindow(win)
{
	while ((win.parent != null) && (win.parent != win))
	{
		win = win.parent;
	}
	return win;
}

function KillEnterAndJump()
{
	if (window.event.keyCode == 13)
	{
	window.event.keyCode = 0;
	goToAnchor();
	}
}

function ToggleDivVis(whichDiv, imageRef){
	var OpenMenuSrc = "/skins/BIW/images/menuimages/menu/win/plusonlyraw.gif";
	var CloseMenuSrc = "/skins/BIW/images/menuimages/menu/win/minusonlyraw.gif";
	imageActual = eval('document.' + imageRef);	
    theDiv=eval(whichDiv);
	if (theDiv.style.display == 'block') {
		theDiv.style.display = 'none';
		imageActual.src = OpenMenuSrc;
	} 
	else {
		theDiv.style.display = 'block';
		imageActual.src = CloseMenuSrc;
	}
}


function C1ToggleAll(id, open, level)
{
	var table = document.getElementById(id);
	
	if (table == null) return;
	
	var rows = table.getElementsByTagName("tr");
	var count = 0;
	var style = open ? "" : "none";
	var nodeLevel = NaN;
	var imgSrc = open ? "/BIW/Core/JSLibrary/C1WebGrid/minus.gif" : "/BIW/Core/JSLibrary/C1WebGrid/plus.gif";

	for (var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		var isHeading = row.id.indexOf(id + "_GH") > -1;
		var isRow = row.id.indexOf(id + "_R") > -1;
		
		var showLevel;
		
		if (isHeading)
		{
			nodeLevel = parseInt(row.getAttribute("nodeLevel"));

			if (level == -1 || (open && (nodeLevel < level + 1)) || !open)
			{
				var img = row.getElementsByTagName("td")[0].getElementsByTagName("img")[0];
				img.src = imgSrc;
			}
		}

		if (open)
		{
			showLevel = level == -1 || (isRow && nodeLevel <= level) || (isHeading && nodeLevel <= level + 1);
		}
		else
		{
			showLevel = (!isNaN(nodeLevel) && nodeLevel > 1) || isRow;
		}
		
		if (!isNaN(nodeLevel) && (isHeading || isRow) && showLevel)
		{
			row.style.display = style;
		}
	}
}

function C1OpenFirstGroup(id)
{
	var table = document.getElementById(id);
	
	if (table == null) return;
	var rows = table.getElementsByTagName("tr");

	var headingCount = 0;
	for (var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		var isHeading = row.id.indexOf(id + "_GH") > -1;
		var isRow = row.id.indexOf(id + "_R") > -1;

		if (isHeading)
		{
			headingCount = headingCount+1;
			if (headingCount == 2){
				break;
			}
			var img = row.getElementsByTagName("td")[0].getElementsByTagName("img")[0];
			img.src = "/BIW/Core/JSLibrary/C1WebGrid/minus.gif";
		}
		
		if (isRow){
			row.style.display = "";
		}
	}
}



function SortListItemFuncAsc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(1);
    if (value1 < value2) return(-1);
    return(0);
} 

function SortListItemFuncDesc(record1, record2)
{
	return SortListItemFuncAsc(record2,record1);
}

function SortListItems(selectToSort, useAscending)
{
	if (useAscending == null)
		useAscending = true;

    // copy options into an array
    var myOptions = [];
    for (var loop=0; loop<selectToSort.options.length; loop++) 
    {
        myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
    }

    // sort array
    if (useAscending)
    {
		myOptions.sort(SortListItemFuncAsc);
	}
	else
	{
		myOptions.sort(SortListItemFuncDesc);
	}	

    // copy sorted options from array back to select box
    selectToSort.options.length = 0;
    for (var loop=0; loop<myOptions.length; loop++) 
    {
        selectToSort.options[loop] = new Option(myOptions[loop].optText, myOptions[loop].optValue);
    }
}

function SelectListItems(list)
{
	for (var i = 0; i < list.options.length; i++)
	{
		list.options[i].selected = true;
	}
}

function CheckTime(time, validationControl)
{
	var bflag = false;
	
	if (time != null && time != "")
	{
		var validTimeFormat=/^\d{2}\:\d{2}$/ //Basic check for correct format

		if (!validTimeFormat.test(time))
			validationControl.errormessage = "Invalid Time Format. Please correct and submit again.";
		else
		{ 
			var hourfield = time.split(":")[0];
			var minfield = time.split(":")[1];
		
			if ((hourfield > 23 || hourfield < 0) || (minfield > 59 || minfield < 0))
				validationControl.errormessage = "Invalid time detected. Please correct and submit again.";
			else
				bflag = true;
		}
	}
	return bflag
}

function UpdateRestrictionLabel(textObj, maxCharacters, label)
{	
    var objLabel = document.getElementById(label);
	var x = maxCharacters - textObj.innerText.length
	if (x < 0)	x = 0;
	objLabel.innerText = x +  " Characters left";
}

function LimitText(textObj, maxCharacters)
{

	if (textObj.innerText.length >= maxCharacters){
		textObj.innerText = textObj.innerText.substring(0,maxCharacters);
		if ((event.keyCode >= 32 && event.keyCode <= 126) || (event.keyCode >= 128 && event.keyCode <= 254)){
			event.returnValue = false;
		}
	}	
}

function GridViewChildToggle(obj, img)
{
	if (obj.style.display == "none")
	{
		obj.style.display = "";
		img.src = "/BIW/Core/JSLibrary/C1WebCommand/af_ind_up_light.gif";
		img.alt = "Collapse";
	}
	else
	{
		obj.style.display = "none";
		img.src = "/BIW/Core/JSLibrary/C1WebCommand/af_ind_down_light.gif";
		img.alt = "Expand";
	}
}

/*
<summary>
Generic function to check / uncheck all check boxes within a control
</summary>
<param name=checkBoxId> single checkbox, checking/unchecking this, will check/uncheck all checkboxes in the control.</param>
<param name=controlId> Control containing the child checkboxes</param>
<revisions>
	<revision author="Shahzad Nawaz" date="01/06/07">function to check and uncheck all checkboxes within a control.</revision>
</revisions>
*/
function selectAll(controlId, checkBoxId)
{
	var i = 0;
	var check = false;
	
	if ($get(checkBoxId) != null)
	{
		if ($get(checkBoxId).checked)
			check = true;
		
		//get all the input boxes from the control
		var checkBoxList = $get(controlId).getElementsByTagName('input');
		
		for (i=0; i<checkBoxList.length; i++)
			checkBoxList[i].checked = check;
	}
}

/*
<summary>
Generic function to check / uncheck all check boxes within a control
</summary>
<param name=checkBoxId> single checkbox, checking/unchecking this, will check/uncheck all checkboxes in the control.</param>
<param name=controlId> Control containing the child checkboxes</param>
<param name=columnIndex>Column index for checkbox</param>
<revisions>
	<revision author="Shahzad Nawaz" date="12/03/08">function to check and uncheck all checkboxes within a grid control can specfiy column.</revision>
</revisions>
*/
function SelectAllCheckBoxes(checkBoxID, controlID, columnIndex) 
{
	var check = false;
	
	if ($get(checkBoxID.id) != null)
	{
		if ($get(checkBoxID.id).checked)
			check = true;
	}
		
	for (i=1; i < $get(controlID.id).rows.length; i++)
	{
		var checkBox = $get(controlID.id).rows[i].cells[columnIndex].getElementsByTagName('input');
		
		if (checkBox.length > 0)
		{
			if (checkBox[0].type == "checkbox")
				checkBox[0].checked = check;
		}
	}
}

var DELAY = 1200;
var i = 0;
var pendingFunc;
var pendingFuncID;

function FormatCurrencyValue(Id, myFunction)
{
	ChangeValue(Id, myFunction);
}


function ChangeValue(Id, myFunction)
{
	// shouldn't be a pending function, but does no harm to check
	i++;
	var v=i;	
	pendingFunc = function() {ChangeValueDelayed(v, Id, myFunction)};
	pendingFuncID = setTimeout(pendingFunc, DELAY);
}

function ChangeValueDelayed(x, Id, myFunction)
{
	if (i==x)
	{
		pendingFunc = null;
		
		if (Id != null)
			myFunction(Id);
		else
			myFunction();
	}
}

function FormatCurrencyById(id)
{
	$get(id).value = AddThousandSepPrecision($get(id).value);
}

function FormatCurrencyByValue(val)
{
	return AddThousandSepPrecision(val);
}