/**
 * Popup parent helper functions in javascript
 *
 * The contents of this file are subject to the SugarCRM Professional End User
 * License Agreement ("License") which can be viewed at
 * http://www.sugarcrm.com/crm/products/sugar-professional-eula.html
 * By installing or using this file, You have unconditionally agreed to the
 * terms and conditions of the License, and You may not use this file except in
 * compliance with the License.  Under the terms of the license, You shall not,
 * among other things: 1) sublicense, resell, rent, lease, redistribute, assign
 * or otherwise transfer Your rights to the Software, and 2) use the Software
 * for timesharing or service bureau purposes such as hosting the Software for
 * commercial gain and/or for the benefit of a third party.  Use of the Software
 * may be subject to applicable fees and any use of the Software without first
 * paying applicable fees is strictly prohibited.  You do not have the right to
 * remove SugarCRM copyrights from the source code or user interface.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *  (i) the "Powered by SugarCRM" logo and
 *  (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * Your Warranty, Limitations of liability and Indemnity are expressly stated
 * in the License.  Please refer to the License for the specific language
 * governing these rights and limitations under the License.  Portions created
 * by SugarCRM are Copyright (C) 2004-2005 SugarCRM, Inc.; All Rights Reserved.
 */

// $Id: popup_parent_helper.js,v 1.11 2005/12/16 06:01:55 wayne Exp $

var popup_request_data;
var close_popup;

function get_popup_request_data()
{
	return window.document.popup_request_data;
}

function get_close_popup()
{
	return window.document.close_popup;
}

function open_csv(module, report, value1, value2, value3, value4, value5, value6, value7, value8, value9, value10) {
	var width = '600';
	var height = '600';
	window.document.close_popup = close_popup;	
	
	if (module == 'group' && report == 'view_contact_requests') {
	URL = 'modules/module_' + module + '_' + report + '.php?action=export&value1=' + value1 + '&value2=' + value2 + '&value3=' + value3 + '&value4=' + value4 + '&value5=' + value5 + '&value6=' + value6;	
    } else if (module == 'premium' && report == 'main') {
	URL = 'modules/module_' + module + '_' + report + '.php?action=export&value1=' + value1 + '&value2=' + value2 + '&value3=' + value3 + '&value4=' + value4 + '&value5=' + value5 + '&value6=' + value6 + '&value7=' + value7 + '&value8=' + value8 + '&value9=' + value9 + '&value10=' + value10;
	} else {
	URL = 'modules/module_' + module + '_report_' + report + '.php?action=export&value1=' + value1 + '&value2=' + value2 + '&value3=' + value3 + '&value4=' + value4 + '&value5=' + value5 + '&value6=' + value6 + '&value7=' + value7 + '&value8=' + value8 + '&value9=' + value9 + '&value10=' + value10;
	}
	
	windowName = 'popup_window';
	
	windowFeatures = 'width=' + width
		+ ',height=' + height
		+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	win = window.open(URL, windowName, windowFeatures);

	if(window.focus)
	{
		// put the focus on the popup if the browser supports the focus() method
		win.focus();
	}

	return win;


}
function open_popup(type, width, height, id)
{
	// set the variables that the popup will pull from
	//window.document.popup_request_data = popup_request_data;
	window.document.close_popup = close_popup;

	// launch the popup
	if (type == 'newstemplate') 
	{
		URL = 'modules/module_news_preview_template.php?'
			+ 'id=' + id;
	}
	else if (type == 'emailtransaction')
	{
		URL = 'modules/module_reports_email_transaction.php?'
			+ 'id=' + id;
	}
	else if (type == 'printapplication')
	{
		URL = 'modules/module_careers_print_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'pdfapplication')
	{
		URL = 'modules/module_careers_pdf_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'printsapplication')
	{
		URL = 'modules/module_sponsorship_print_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'pdfsapplication')
	{
		URL = 'modules/module_sponsorship_pdf_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'printinventory')
	{
		URL = 'modules/module_store_print_inventory.php?'
			+ 'id=' + id;
	}
	else if (type == 'pdfinventory')
	{
		URL = 'modules/module_store_pdf_inventory.php?'
			+ 'id=' + id;
	}
	else if (type == 'printgapplication')
	{
		URL = 'modules/module_groupdining_print_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'pdfgapplication')
	{
		URL = 'modules/module_groupdining_pdf_application.php?'
			+ 'id=' + id;
	}
	else if (type == 'downloadsubscribers')
	{
		URL = 'modules/module_news_download_subscribers.php?'
			+ 'id=' + id;
	}
	else 
	{
		URL = 'modules/module_email.php?'
			+ 'id=' + id 
			+ '&type=' + type;
	}
	windowName = 'popup_window';
	
	if (type == 'printapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'pdfapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'printsapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'pdfsapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'printinventory')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'pdfinventory')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'printgapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else if (type == 'pdfgapplication')
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1,toolbar=1,menubar=1';
	}
	else
	{
		windowFeatures = 'width=' + width
			+ ',height=' + height
			+ ',resizable=1,scrollbars=1';
	}
		
	win = window.open(URL, windowName, windowFeatures);

	if(window.focus)
	{
		// put the focus on the popup if the browser supports the focus() method
		win.focus();
	}

	return win;
}

/**
 * The reply data must be a JSON array structured with the following information:
 *  1) form name to populate
 *  2) associative array of input names to values for populating the form
 */
var from_popup_return  = false;
function set_return(popup_reply_data)
{
	from_popup_return = true;
	var form_name = popup_reply_data.form_name;
	var name_to_value_array = popup_reply_data.name_to_value_array;
	
	for (var the_key in name_to_value_array)
	{
		if(the_key == 'toJSON')
		{
			/* just ignore */
		}
		else
		{
			var displayValue=name_to_value_array[the_key];
			displayValue=displayValue.replace('&#039;',"'");  //restore escaped single quote.
			displayValue=displayValue.replace( '&amp;',"&");  //restore escaped &.
			displayValue=displayValue.replace( '&gt;',">");  //restore escaped >.
			displayValue=displayValue.replace( '&lt;',"<");  //restore escaped <.
			displayValue=displayValue.replace( '&quot; ',"\"");  //restore escaped ".
			
			window.document.forms[form_name].elements[the_key].value = displayValue;
		}
	}
}

function set_return_and_save(popup_reply_data)
{
	var form_name = popup_reply_data.form_name;
	var name_to_value_array = popup_reply_data.name_to_value_array;
	
	for (var the_key in name_to_value_array)
	{
		if(the_key == 'toJSON')
		{
			/* just ignore */
		}
		else
		{
			window.document.forms[form_name].elements[the_key].value = name_to_value_array[the_key];
		}
	}
	
	window.document.forms[form_name].return_module.value = window.document.forms[form_name].module.value;
	window.document.forms[form_name].return_action.value = 'DetailView';
	window.document.forms[form_name].return_id.value = window.document.forms[form_name].record.value;
	window.document.forms[form_name].action.value = 'Save';
	window.document.forms[form_name].submit();
}

