//Opens a window at a particular position (or tries to!)
function openWindowAtEventAppendField (URL, windowName, evt, field) 
{  
	//alert("open window at event called");
	params = 'width=230,height=200,toolbar=no,location=no,titlebar=no,directories=no,menubar=no,status=no,scrollbars=no,resizable=no,fullscreen=no';  
	params = params + ",left=" + (FindPosX(document.getElementById(field)) ) + ",top=" + (FindPosY(document.getElementById(field)) )
	URL += document.getElementById(field).value; 
	
	//alert("URL" + URL);  
   
	popupWindow = window.open(URL,windowName,params);
    //Ensure opener exists
	if(!popupWindow.opener)
	{
		popupWindow.opener = self;
	 }
      
	popupWindow.focus();         
}

function FindPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)//Most Browsers
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)//Netscape
	{
		//alert("x:" + obj.offsetLeft);
		curleft += obj.x;
	}
		
	if(window.screenLeft)//Internet Explorer	
	{
		curleft += window.screenLeft;
		curleft -= 23;
		
	}
	else
	{
		if(window.screenX)//Netscape/Firefox
		{
			//alert("screenx:" + window.screenX);
			curleft += window.screenX;
			curleft += 175;
		}
		
		if(window.pageXOffset)//Netscape
		{
			//alert("pageoffset: " + window.pageXOffset);
			curleft += window.pageXOffset;
		}
	}
	
	return curleft;
}

function FindPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
		
	if(window.screenTop)	
	{
		curtop += window.screenTop;
		curtop += 15;
	}
	else
	{
		if(window.screenY)
		{
			curtop += window.screenY;
			curtop += 128;
		}
		
		if(window.pageYOffset)
		{
			curtop += window.pageYOffset;
		}	
	}
	return curtop;
}

//Updates a field in the opener form, from the field value of a field in 
//the child form
function updateParentFromField(parentFieldName, field)
{
	//alert("update parent from field called:" + field.name);
	if ( field.name == 'SelectedDate' )
	{
		// this is the Calendar calling so we format the value
		updateParentFieldDate(parentFieldName, field.value);
	}
	else
	{
		// this could be any other forms calling not requiring formatting
		updateParentField(parentFieldName, field.value);
	}
	return true;	
}

function updateParentField(fieldName, value)
{
	//alert("updating parent field : " + fieldName + " : " + value);
	if ( fieldName != "" )
	{
		//alert("Field Name not null : " + fieldName);
		if (opener)
		{
			//alert("Opener document form exists");	
			if(opener.document.getElementById(fieldName))
			{
				opener.document.getElementById(fieldName).value = value;
				opener.document.getElementById(fieldName).title = value;
				opener.focus();
			}
			else
			{
				alert("Parent Document does not have a field named : " + fieldName);
			}
		}
		else{
			alert ("document has no opener");
		}
	}
	else
	{
		alert("no field specified for update");	
	}
	
	return true;	
}

//Updates a date field from a child form
function updateParentFieldDate(fieldName, value)
{
	//alert("updating parent field : " + fieldName + " : " + value);
	if ( fieldName != "" )
	{
		//alert("Field Name not null : " + fieldName);
		if (opener)
		{
			//alert("Opener document form exists");	
			if(opener.document.getElementById(fieldName))
			{
				//alert("Parent Field Exists : " + fieldName);
				//opener.document.Detail[fieldName].value = FormatDateISO8061(value);
				opener.document.getElementById(fieldName).value = value;					
					
				opener.document.getElementById(fieldName).title = GetLocaleDateFromString( value );
				opener.focus();
			}
			else
			{
				alert("Parent Document does not have a field named : " + fieldName);
			}
		}
		else{
			alert ("document has no opener");
		}
	}
	else
	{
		alert("no field specified for update");	
	}
	
	return true;	
}

function GetLocaleDateFromString( strDate )
{
	// used to create a tooltip from the server calendar SetDate feature
	var DateParts = GetDatePartsFromString( strDate )
	intYear = new Number( parseInt( DateParts[2],10 ) );
	intMonth = new Number( parseInt( DateParts[1], 10 ) - 1 );
	intDay = new Number( parseInt( DateParts[0], 10 ) );

	// compute date from its parts and return its locale format
	var datComputedDate = new Date( intYear, intMonth, intDay );

	return RemoveTime( datComputedDate.toLocaleString() );
}

// return an Array of parts (Year, Month, Day)
// can be used to create a date from int values
function GetDatePartsFromString( strDate )
{
	//Added regular expression matching - Paul
	var RegEx1 = /[-]/;
	var RegEx2 = /[\/]/;
	var DateParts = new Array();
	
    if (RegEx1.test(strDate))
	{
		DateParts = strDate.split( "-" );
	}
	
	if (RegEx2.test(strDate))
	{
		DateParts = strDate.split( "/" );
	}

	return DateParts;
}
