//Popup.js
//	HTC Global Services, Inc.
//	This contains the functions that are called by usageNotes.ascx user control.
//	It opens up a popup window with a text area in it.
//
//	Creation: Oct 15, 2003 - Piyush Bhatt
//	Modifications: 
//


// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
function pageWidth() 
{
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function pageHeight() 
{
	return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function getCenteredPosition(ctlId, width, height)
{
	var ctl = getObject(ctlId);
	var r;
	if(ctl)
	{//get the coordinates of the control in the window
		var coords = getAbsolutePosition(ctl);
		var left = coords.x;
		var top = coords.y;
		left -= (width/2);
		//offset for window position
		left += getLeftWindowCoord(0);
		top  += getTopWindowCoord(0);
		//offset to width and position of screen
		if(width > pageWidth())
			left = getLeftWindowCoord(0);
		else if((left+width) > (getLeftWindowCoord(0) + pageWidth()))
			left = (getLeftWindowCoord(0) + pageWidth()) - width;
		//figure out the top
		if(height > pageHeight())
			top = getTopWindowCoord(0);
		else if((top+height) > (getTopWindowCoord(0) + pageHeight()))
			top = (getTopWindowCoord(0) + pageHeight()) - height;
		r = {x:left, y:top};
	}
	else
	{
		r = {x:width, y:height};
	}
	return r;
}//getCenteredPosition

//popupNotes - PADD! 16-Oct-03
//	@fieldname - fieldname where to return the value
//	@mode - add or update mode
//	
function popupNotes(fieldname, mode, chkfieldname, title, header, enableMn, mnemonic, size, pgmControl)
{
	var url;
	//var lblNotes = getObject(fieldname);
	var pgmId = getObject(pgmControl);
	//lblNotes.value can be more than the max len allowed by a url string
	//in that case this would fail
	//will get this value from parent window with the fieldname parameter
	var description = ''; //lblNotes.value; 
	var pgmCode = '';
	
	var left = 100;
	var top = 100;
	var width = 400;
	var height = 300;
	//get the center coordinates
	var coords = getCenteredPosition(pgmControl,width,height);

	if (pgmId)
		pgmCode = pgmId.value;

	url = "../include/frmpopupNotes.aspx?fieldname=" + fieldname + "&Mode=" + mode + "&chkfieldname=" + chkfieldname + "&Notes=" + escape(description) + "&title=" + title + "&header=" + header + "&size=" + size + "&pgmid=" + pgmCode;

	if(enableMn > 0) 
		url = url + "&enablemn=1&mnemonic=" + mnemonic;
	else
		url = url + "&enablemn=0";
	
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	//window.open(url,'','left=' + coords.x + ',top=' + coords.y + ',width=400,height=300,scrollbars=no,resizable=no');

    //SMCHG : 9/9/09 : set the window name to fieldname to have a single popup to be opened from the specific button
	window.open(url,fieldname,'left=' + coords.x + ',top=' + coords.y + ',width=400,height=300,scrollbars=no,resizable=no');

	return false;
}//popupNotes

//setNotes - PADD! 16-Oct-03
//	@name
//	@description
//
function setNotes(name, chkfieldname, description)
{
	var lblNotes = getObject(name);
	var chkfield = getObject(chkfieldname);
	if (lblNotes)
	{
		lblNotes.value = unescape(description);
	}
	if(chkfield)
	{			
		if(chkfieldname.indexOf("chk") >= 0)
			chkfield.checked = true;
		else
			chkfield.value = 1;
	}//if
}//setNotes

function getFieldVal(name)
{
	//getFieldVal - PADD! 21-Oct-04
	//	@name - field name whose value to get
	var hdnField = getObject(name);
	//alert(hdnField);
	if (hdnField)
		return hdnField.value;
	else
		return ""; 
}//getFieldVal

function setFieldVal(name, chkfieldname, fieldval)
{
	//setFieldVal - PADD! 21-oct-04
	//	@name - the field name
	//	@chkfieldname - the chck box or hidden value to be set if changed
	//	@fieldval - the field value to set
	var hdnField = getObject(name);
	if (hdnField)
	{
		hdnField.value = fieldval;
		//alert(hdnField.value);
		var chkfield = getObject(chkfieldname);
		if (chkfield)
		{	//this field can be either check box or hidden field
			if (chkfieldname.indexOf("chk") >= 0)
				chkfield.checked = true;
			else
				chkfield.value = 1;
		}//if chkfield
	}//if hdnfield
}//setFieldVal

function openScreenHelp()
{
	var tblHeader = getObject("tblHeader");
	var ctlHeaderTop=parseInt(tblHeader.offsetTop)*1;

	if(IE4)	
		ctlHeaderTop = ctlHeaderTop+125;
	else if(NS4)
		ctlHeaderTop = ctlHeaderTop+165;
	else 
		ctlHeaderTop = ctlHeaderTop+125;

	var windowObj;
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	windowObj = window.open('../include/ScreenHelp.aspx?parenturl=' + replaceAll(window.location.href, '&', '@^') + '&pagename=' + getObject("ctlHeader_hdncurrpage").value + '','','height=340,width=380,top=' + ctlHeaderTop + ',left=2,toolbar=no,location=no,directories=no,status=yes,menubar=no,dependent=yes,scrollbars=no,resizable=no', '_blank')
	

	if(windowObj)
	{
		windowObj.focus();
	}
}

function openScrHelpDocs()
{
	//CKK 05-11-07 was not working with documents outside of PDF, waiting screen will have to be redone
	//show waiting message
	//var waiting = window.open('../GRT/HTML/GRT_ReportLoading.html',  '_help', 'toolbar=no,location=no,directories=no,status=yes,menubar=yes,dependent=yes,scrollbars=yes,resizable=yes,width=500,height=500,top=100, left=100', '_blank');
	//show the pdf
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	var doc = window.open('../admin/ViewAppDocs.aspx?app=N&parenturl=' + replaceAll(window.location.href, '&', '@^') + '&pagename=' + getObject("ctlHeader_hdncurrpage").value + '&urlref=' + Request.QueryString("urlref") + '&fnam=' + '' + '', '', 'toolbar=no,location=no,directories=no,status=yes,menubar=yes,dependent=yes,scrollbars=yes,resizable=yes,width=370,height=300,top=100, left=100', '_blank');
	//if (doc == null && waiting != null)
	//	waiting.close();
}//openScrHelpDocs

function openErrorWin(strErrors)
{
	var tblHeader = getObject("tblHeader");
	var ctlHeaderTop = parseInt(tblHeader.offsetTop)*1;
	ctlHeaderTop = ctlHeaderTop+260;

	var leftVal = (screen.width - 700) / 2;
	var topVal = (screen.height - 300) / 2;

	var windowObj;
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	windowObj = window.open('../include/ViewErrors.aspx?error=' + strErrors + '','','height=200,width=450,top=' + topVal + ',left=' + leftVal + ',toolbar=no,location=no,directories=no,status=yes,menubar=no,dependent=yes,scrollbars=no,resizable=no', '_blank')

	if(windowObj)
	{
		windowObj.focus();
	}
}
///---------------------------------------------------------------------------------------------------------
// PADD! 7-Nov-06 
//	Following functions added for the HtmlNotes control
// 
//ACHG:8-1-07:Added one more chkfield
function popupHtmlNotes(fieldname, mode, chkfieldname, chkfieldname1, title, header, size)
{
	var url;

	url = "../include/frmHtmlNotes.aspx?fieldname=" + fieldname 
				+ "&mode=" + mode 
				+ "&chkfieldname=" + chkfieldname 
				+ "&chkfieldname1=" + chkfieldname1 
				+ "&title=" + title 
				+ "&header=" + header 
				+ "&size=" + size;
	
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	window.open(url,'','left=200,top=150,width=610,height=500,scrollbars=no,resizable=yes');

	return false;
}//popupHtmlNotes

//setNotes - PADD! 16-Oct-03
//	@name
//	@description
//
//ACHG:8-1-07:Added one more chkfield
function setHtmlNotes(name, chkfieldname, chkfieldname1, description)
{
	var lblNotes = getObject(name);
	var chkfield = getObject(chkfieldname);
	var chkfield1 = getObject(chkfieldname1);
	if (lblNotes)
	{
		lblNotes.value = unescape(description);
	}
	if(chkfield)
	{
		if(chkfieldname.indexOf("chk") >= 0)
			chkfield.checked = true;
		else
			chkfield.value = 1;
	}//if
	if(chkfield1)
	{
		if(chkfieldname1.indexOf("chk") >= 0)
			chkfield1.checked = true;
		else
			chkfield1.value = 1;
	}//if
}//setNotes

//AADD:3-27-07
function popupAddress(mode, fieldname, chkfieldname, title)
{	
	var url = "../include/frmAddress.aspx?Mode=" + mode + "&fieldname=" + fieldname + "&chkfieldname=" + chkfieldname + "&title=" + title;

	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	window.open(url,'','screenX=400,screenY=100,width=400,height=260,scrollbars=no,resizable=no');

	return false;
}//popupAddress

//setAddressValues
function setAddressValues(fieldname, chkfieldname, address1, address2, city, zip1, zip2, phone, phoneExtn, fax, stateCode)
{
	var indx = fieldname.lastIndexOf("_hdnAddressId");	
	var controlname = fieldname.substring(0, indx);

	var obj = getObject(controlname + '_hdnAddress1');
	if(obj)
		obj.value = address1;

	obj = getObject(controlname + '_hdnAddress2');
	if(obj)
		obj.value = address2;

	obj = getObject(controlname + '_hdnCity');
	if(obj)
		obj.value = city;

	obj = getObject(controlname + '_hdnZip1');
	if(obj)
		obj.value = zip1;

	obj = getObject(controlname + '_hdnZip2');
	if(obj)
		obj.value = zip2;

	obj = getObject(controlname + '_hdnPhone');
	if(obj)
		obj.value = phone;

	obj = getObject(controlname + '_hdnPhoneExt');
	if(obj)
		obj.value = phoneExtn;

	obj = getObject(controlname + '_hdnFax');
	if(obj)
		obj.value = fax;

	obj = getObject(controlname + '_hdnStateCode');
	if(obj)
		obj.value = stateCode;

	var chkfield = getObject(chkfieldname);
	if(chkfield)
	{
		if(chkfieldname.indexOf("chk") >= 0)
			chkfield.checked = true;
		else
			chkfield.value = 'Y';
	}
}//setAddressValues