//----------------------------------------------------------------------
// Following used in ctlLookup.ascx
//----------------------------------------------------------------------

//OnLookup - PADD! 17-Jan-05
//	@obj -
function OnLookup(clientid, parentparamid, searchparamid)
{
	//call OkToLookup only if it is defined, otherwise proceed with what this has to do
	if(typeof(OkToLookup) != "undefined")
	{
		if (!OkToLookup(clientid))
		{
			return false;
		}//if
	}//if typeof
		
	var url="../include/frmMultiLookup.aspx";
	url = url + "?clientid=" + clientid;

	var url_params = "";
	//read parent params 
	var parentParams = getObject(parentparamid);
	if (parentParams.value != null && !isEmpty(parentParams.value))
	{
		// parse out name/value pairs separated via &
		var args = parentParams.value.split('&');
		
		for (var i=0;i < args.length; i++)
		{
			var pair = args[i].split('=');
			// Fix broken unescaping
			var temp = unescape(pair[0]).split('+');
			name = temp.join(' ');

			temp = unescape(pair[1]).split('+');
			value = temp.join(' ');
			
			//find the control with name 'value'
			//if the lookup control is part of another user control
			//	then lookup control's id must have 	underscore(_)
			//  and so the object we have to find is also within that
			//	user control we need to generate it's clientid too			
			var obj = lookupSibling(clientid, value);
			
			if(obj)
				url_params = url_params + "&" + name + "=" + escape(obj.value);
			else
			{
				obj = getObject(value);
				if (obj)
					url_params = url_params + "&" + name + "=" + escape(obj.value);
				else
					url_params = url_params + "&" + name + "=" + escape(value);
			}
		}//for
	}//if 
	
	//read search params
	var searchParams = getObject(searchparamid);
	if(searchParams.value != null && !isEmpty(searchParams.value))
	{
		url_params = url_params + "&" + searchParams.value;
	}//if
	url = url + url_params;
	OpenPortalLookupWindow(url);
	return false;
}//OnLookup

function OpenPortalLookupWindow(url)
{
	var oNewWindow;
	//SMCHG : 8/6/07 : set the window name from null to '' to avoid multiple browser to use the same window
	oNewWindow = window.open(url, "", "height=475, width=380, top=100, left=600, resizable=yes, scrollbars=yes, toolbar=no, menubar=no, location=no, status=no", true);
	oNewWindow.focus();
}//OpenLookupWindow

//CopyField
//	@name - database field name
//	@value - database field value
//	This will copy the database field name to the screen field
function CopyField(client_id, name, value)
{	
	var idField = getObject(client_id + "_hdIDField")
	if(idField && idField.value == name)
	{
		var idCode = getObject(client_id + "_hdIDCode");
		if(idCode)
			idCode.value = value;
	}
	var copy_field_str = getObject(client_id + "_hdCopyFieldStr").value;
	
	var nvStr = new Querystring();
	nvStr.process(copy_field_str);
	var fld = nvStr[name];

	if(fld != null && fld != "undefined")
	{	
		//if the lookup control is part of another user control
		//	then lookup control's id must have 	underscore(_)
		//  and so the object we have to find is also within that
		//	user control we need to generate it's user id too
		var objFld = lookupSibling(client_id, fld);
		if (objFld)
			objFld.value = value;
	}//if
}//CopyField

//lookupSibling - PADD! 17-Mar-06
//
function lookupSibling(client_id, fld)
{
	var indx;
	var strid;
	indx = client_id.lastIndexOf("_");
	if (indx > 0)
	{
		strid = client_id.substring(0, indx);
		strid = strid + "_" + fld;
	}
	else
		strid = fld;
		
	return getObject(strid);
}//lookupSibling

function SetAllOrNone(client_id, str)
{
	// set internal idcode field to all or none
	var idCode = getObject(client_id + "_hdIDCode");
	if(idCode && str == 'all')
		idCode.value = 'all';
	else if(idCode && str == 'none')
		idCode.value = '';

	//set all user specified fields to all or none
	var copy_field_str = getObject(client_id + "_hdCopyFieldStr").value;
	
	var args = copy_field_str.split('&');
	for (var i=0; i < args.length; i++)
	{
		var pair = args[i].split('=');
		//pair[0] is dbfield and pair[1] is control id
		var fld = pair[1];
			
		var objFld = lookupSibling(client_id, fld);
		if (objFld && str == 'none')
			objFld.value = '';
		else if(objFld && str == 'all')
			objFld.value = 'All';
	}//for
}//SetAllOrNone

//CopyFields
//	the str should contain 'fieldname=value&fieldname=value' type of string
//	this function copy the values to corresponding fields depending on field names
function CopyFields(clientid, str)
{	
	//if all or none
	if(str == 'all' || str == 'none')
	{
		SetAllOrNone(clientid, str);
		return;
	}	
	
	// parse out name/value pairs separated via &
	var args = str.split('&');
	
	for (var i=0;i < args.length; i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		name = temp.join(' ');

		temp = unescape(pair[1]).split('+');
		value = temp.join(' ');
		
		if (name != "undefined")
			CopyField(clientid, name, value);
	}//for
}//CopyFields


function Querystring()
{
	this.process = Querystring_process;

	this.get=Querystring_get;
	
}//Constructor

function QueryString_old()
{
// get the query string, ignore the ? at the front.
	var querystring=location.search.substring(1,location.search.length);

// parse out name/value pairs separated via &
	var args = querystring.split('&');

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		temp0 = temp.join(' ');
		
		temp = unescape(pair[1]).split('+');
		temp1 = temp.join(' ');
		
		this[temp0]=temp1;
	}

	this.get=Querystring_get;
}

function Querystring_process(strQuery)
{
	var querystring = strQuery;
	
	// parse out name/value pairs separated via &
	var args = querystring.split('&');

// split out each name = value pair
	for (var i=0;i<args.length;i++)
	{
		var pair = args[i].split('=');

		// Fix broken unescaping
		temp = unescape(pair[0]).split('+');
		temp0 = temp.join(' ');
		
		temp = unescape(pair[1]).split('+');
		temp1 = temp.join(' ');
		
		this[temp0]=temp1;
	}
}//process

function Querystring_get(strKey,strDefault)
{
	var value=this[strKey];
	if (value==null){value=strDefault;}
	
	return value;
}