// This file is dependant on cerror_box.js

// setup tab handlers
function tabdisplay_discountgeneraldollar_handler_2()
{
	DiscountTypeGeneralDollar_AdminLoadEditDiscountPage();
}
function tabdisplay_discountgeneraldollar_handler_1()
{
	DiscountTypeGeneralDollar_AdminLoadGenerateDiscountNewPage();
}

// process the inputs from the edit page
function DiscountTypeGeneralDollar_AdminEditDiscountCodes()
{
	var filter_cert_range_min = document.getElementById('discount_general_dollar_edit_filter_min_range').value;
	var filter_cert_range_max = document.getElementById('discount_general_dollar_edit_filter_max_range').value;
	var filter_cert_pri_ref = document.getElementById('discount_general_dollar_edit_filter_pri_ref').value;
	var is_edit_dollar_value = document.getElementById('discount_general_dollar_edit_dollar_value_check').checked;
	var change_dollar_value = document.getElementById('discount_general_dollar_edit_dollar_value').value;
	var is_edit_min_subtotal = document.getElementById('discount_general_dollar_edit_min_subtotal_check').checked;
	var change_min_subtotal = document.getElementById('discount_general_dollar_edit_min_subtotal').value;
	var is_edit_activation_date = document.getElementById('discount_general_dollar_edit_activation_date_check').checked;
	var change_activation_date = new String(document.getElementById('discount_general_dollar_edit_activation_date_year').value);
		change_activation_date += String(document.getElementById('discount_general_dollar_edit_activation_date_month').value);
		change_activation_date += String(document.getElementById('discount_general_dollar_edit_activation_date_day').value);
	var is_edit_expiration_date = document.getElementById('discount_general_dollar_edit_expiration_date_check').checked;
	var change_expiration_date = new String(document.getElementById('discount_general_dollar_edit_expiration_date_year').value);
		change_expiration_date += String(document.getElementById('discount_general_dollar_edit_expiration_date_month').value);
		change_expiration_date += String(document.getElementById('discount_general_dollar_edit_expiration_date_day').value);
	var is_edit_is_extended = document.getElementById('discount_general_dollar_edit_is_extended_check').checked;
	var change_is_extended = document.getElementById('discount_general_dollar_edit_is_extended').checked;
	var is_edit_private_reference = document.getElementById('discount_general_dollar_edit_private_reference_check').checked;
	var change_private_reference = document.getElementById('discount_general_dollar_edit_private_reference').value;

	//alert(filter_cert_pri_ref +' '+filter_cert_range_min+'-'+filter_cert_range_max);
	//ShowErrorBox('discount_general_dollar_edit','test');
	
	var error_array = new Array();
	
	// check input
	if(!filter_cert_range_min && !filter_cert_range_max && !filter_cert_pri_ref)
	{
		error_array.push('Invalid certificate filters - You must at least enter a range or a private reference');
	}
	if(!filter_cert_range_min && filter_cert_range_max)
	{
		error_array.push('Invalid certificate filters - You must enter minimum and maximum certificate values for range.');
		document.getElementById('discount_general_dollar_edit_filter_max_range').value = '';
		document.getElementById('discount_general_dollar_edit_filter_min_range').value = '';
	}
	if(filter_cert_range_min && !DiscountTypeGeneralDollar_IsValidCertificateFormat(filter_cert_range_min))
	{
		error_array.push('Invalid certificate filters - Invalid minimum certificate format.');
		document.getElementById('discount_general_dollar_edit_filter_min_range').value = '';
	}
	if(filter_cert_range_max && !DiscountTypeGeneralDollar_IsValidCertificateFormat(filter_cert_range_max))
	{
		error_array.push('Invalid certificate filters - Invalid maximum certificate format.');
		document.getElementById('discount_general_dollar_edit_filter_max_range').value = '';
	}
	if(filter_cert_pri_ref && !DiscountTypeGeneralDollar_IsValidPrivateReference(filter_cert_pri_ref))
	{
		error_array.push('Invalid Private Reference Filter - You may only use combinations of letters and numbers');
		document.getElementById('discount_general_dollar_edit_filter_pri_ref').value = '';
	}
	if(!is_edit_dollar_value && !is_edit_min_subtotal && !is_edit_activation_date && !is_edit_expiration_date && !is_edit_is_extended && !is_edit_private_reference)
	{
		error_array.push('Invalid Values - You must make at least one change to the certificates.');
	}
	if(is_edit_dollar_value && change_dollar_value != '' && !DiscountTypeGeneralDollar_IsValidDollarAmount(change_dollar_value))
	{
		error_array.push('Invalid Dollar Value');
		document.getElementById('discount_general_dollar_edit_dollar_value').value = '';
	}
	if(is_edit_min_subtotal && change_min_subtotal != '' && !DiscountTypeGeneralDollar_IsValidDollarAmount(change_min_subtotal))
	{
		error_array.push('Invalid Minimum Subtotal');
		document.getElementById('discount_general_dollar_edit_min_subtotal').value = '';
	}
	if(is_edit_expiration_date && change_activation_date && change_activation_date < 10000000)
	{
		error_array.push('Invalid Expiration Date - You may leave all month, day, and year blank or you must fill them in completely.');
		document.getElementById('discount_general_dollar_edit_expiration_date_year').value = '';
		document.getElementById('discount_general_dollar_edit_expiration_date_month').value = '';
		document.getElementById('discount_general_dollar_edit_expiration_date_day').value = '';
	}
	if(is_edit_private_reference && change_private_reference && !DiscountTypeGeneralDollar_IsValidPrivateReference(change_private_reference))
	{
		error_array.push('Invalid Private Reference - You may only use combinations of letters and numbers');
		document.getElementById('discount_general_dollar_edit_private_reference').value = '';
	}

	HideErrorBox('discount_general_dollar_edit');
	if(error_array.length > 0)
	{
		ShowErrorBox('discount_general_dollar_edit', error_array);
	}
	else
	{
		var post_parameter_array = Array();
		post_parameter_array[0] = Array('ajax','1');
		post_parameter_array[1] = Array('edit_codes','1');
		post_parameter_array[2] = Array('filter_cert_range_min',filter_cert_range_min);
		post_parameter_array[3] = Array('filter_cert_range_max',filter_cert_range_max);
		post_parameter_array[4] = Array('filter_cert_pri_ref',filter_cert_pri_ref);
		post_parameter_array[5] = Array('is_edit_dollar_value',is_edit_dollar_value.toString());
		post_parameter_array[6] = Array('change_dollar_value',change_dollar_value);
		post_parameter_array[7] = Array('is_edit_min_subtotal',is_edit_min_subtotal.toString());
		post_parameter_array[8] = Array('change_min_subtotal',change_min_subtotal);
		post_parameter_array[9] = Array('is_edit_activation_date',is_edit_activation_date.toString());
		post_parameter_array[10] = Array('change_activation_date',change_activation_date);
		post_parameter_array[11] = Array('is_edit_expiration_date',is_edit_expiration_date.toString());
		post_parameter_array[12] = Array('change_expiration_date',change_expiration_date);
		post_parameter_array[13] = Array('is_edit_is_extended',is_edit_is_extended.toString());
		post_parameter_array[14] = Array('change_is_extended',change_is_extended.toString());
		post_parameter_array[15] = Array('is_edit_private_reference',is_edit_private_reference.toString());
		post_parameter_array[16] = Array('change_private_reference',change_private_reference);

                document.getElementById('discount_general_dollar_edit_area').innerHTML = 'Please wait...';

		DoHTTPRequest("index.php?param=discount_admin&operation=discount_general_dollar&",
				DiscountTypeGeneralDollar_EditCertificateAreaHandler, post_parameter_array);
	}

}

function DiscountTypeGeneralDollar_AdminReportPage_ExportByFilter()
{
	var filter_cert_range_min = document.getElementById('discount_general_dollar_report_filter_min_range').value;
	var filter_cert_range_max = document.getElementById('discount_general_dollar_report_filter_max_range').value;
	var filter_cert_pri_ref = document.getElementById('discount_general_dollar_report_filter_pri_ref').value;

	var error_array = new Array();

	// check input
	if(!filter_cert_range_min && filter_cert_range_max)
	{
		error_array.push('Invalid certificate filters - You must enter minimum and maximum certificate values for range.');
		document.getElementById('discount_general_dollar_report_filter_max_range').value = '';
		document.getElementById('discount_general_dollar_report_filter_min_range').value = '';
	}
	if(filter_cert_range_min && !DiscountTypeGeneralDollar_IsValidCertificateFormat(filter_cert_range_min))
	{
		error_array.push('Invalid certificate filters - Invalid minimum certificate format.');
		document.getElementById('discount_general_dollar_report_filter_min_range').value = '';
	}
	if(filter_cert_range_max && !DiscountTypeGeneralDollar_IsValidCertificateFormat(filter_cert_range_max))
	{
		error_array.push('Invalid certificate filters - Invalid maximum certificate format.');
		document.getElementById('discount_general_dollar_report_filter_max_range').value = '';
	}
	if(filter_cert_pri_ref && !DiscountTypeGeneralDollar_IsValidPrivateReference(filter_cert_pri_ref))
	{
		error_array.push('Invalid Private Reference Filter - You may only use combinations of letters and numbers');
		document.getElementById('discount_general_dollar_report_filter_pri_ref').value = '';
	}

	HideErrorBox('discount_general_dollar_report');
	if(error_array.length > 0)
	{
		ShowErrorBox('discount_general_dollar_report', error_array);
	}
	else
	{
        	window.location = 'index.php?param=discount_admin&operation=discount_general_dollar&ajax=1&export_csv_cert_by_filter=1&filter_cert_range_min='+filter_cert_range_min+'&filter_cert_range_max='+filter_cert_range_max+'&filter_cert_pri_ref='+filter_cert_pri_ref+'&';
	}
}

function DiscountTypeGeneralDollar_AdminEditPage_ExportByFilter()
{
	var filter_cert_range_min = document.getElementById('discount_general_dollar_edit_filter_min_range').value;
	var filter_cert_range_max = document.getElementById('discount_general_dollar_edit_filter_max_range').value;
	var filter_cert_pri_ref = document.getElementById('discount_general_dollar_edit_filter_pri_ref').value;

	var error_array = new Array();

	// check input
	if(!filter_cert_range_min && !filter_cert_range_max && !filter_cert_pri_ref)
	{
		error_array.push('Invalid certificate filters - You must at least enter a range or a private reference');
	}
	if(!filter_cert_range_min && filter_cert_range_max)
	{
		error_array.push('Invalid certificate filters - You must enter minimum and maximum certificate values for range.');
		document.getElementById('discount_general_dollar_edit_filter_max_range').value = '';
		document.getElementById('discount_general_dollar_edit_filter_min_range').value = '';
	}
	if(filter_cert_pri_ref && !DiscountTypeGeneralDollar_IsValidPrivateReference(filter_cert_pri_ref))
	{
		error_array.push('Invalid Private Reference Filter - You may only use combinations of letters and numbers');
		document.getElementById('discount_general_dollar_edit_filter_pri_ref').value = '';
	}

	HideErrorBox('discount_general_dollar_edit');
	if(error_array.length > 0)
	{
		ShowErrorBox('discount_general_dollar_edit', error_array);
	}
	else
	{
        	window.location = 'index.php?param=discount_admin&operation=discount_general_dollar&ajax=1&export_csv_cert_by_filter=1&filter_cert_range_min='+filter_cert_range_min+'&filter_cert_range_max='+filter_cert_range_max+'&filter_cert_pri_ref='+filter_cert_pri_ref+'&';
	}
}

function DiscountTypeGeneralDollar_AdminLoadEditDiscountPage()
{
	document.getElementById('discount_general_dollar_edit_area').innerHTML = 'Please Wait...';
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_general_dollar&ajax=1&show_edit_page=1&",
			DiscountTypeGeneralDollar_EditCertificateAreaHandler);
}
function DiscountTypeGeneralDollar_AdminLoadGenerateDiscountNewPage()
{
        document.getElementById('discount_general_dollar_generate_area').innerHTML = 'Please Wait...';
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_general_dollar&ajax=1&show_generate_discount_page=1&",
			DiscountTypeGeneralDollar_GenerateCertificateAreaHandler);
}
function DiscountTypeGeneralDollar_AdminLoadGenerateDiscountShowLastPage()
{
	document.getElementById('discount_general_dollar_generate_area').innerHTML = 'Please Wait...';
	DoHTTPRequest("index.php?param=discount_admin&operation=discount_general_dollar&ajax=1&view_last_codes_this_session=1&",
			DiscountTypeGeneralDollar_GenerateCertificateAreaHandler);
}
function DiscountTypeGeneralDollar_AdminGenerateDiscountCodes()
{

	var qty = document.getElementById('discount_general_dollar_generate_qty').value;
	var dollar_value = document.getElementById('discount_general_dollar_generate_dollar_value').value;
	var min_subtotal = document.getElementById('discount_general_dollar_generate_min_subtotal').value;
	var activation_date = new String(document.getElementById('discount_general_dollar_generate_activation_date_year').value);
		activation_date += String(document.getElementById('discount_general_dollar_generate_activation_date_month').value);
		activation_date += String(document.getElementById('discount_general_dollar_generate_activation_date_day').value);
	var expiration_date = new String(document.getElementById('discount_general_dollar_generate_expiration_date_year').value);
		expiration_date += String(document.getElementById('discount_general_dollar_generate_expiration_date_month').value);
		expiration_date += String(document.getElementById('discount_general_dollar_generate_expiration_date_day').value);
	var is_extended = document.getElementById('discount_general_dollar_generate_is_extended').checked;
	var private_reference = document.getElementById('discount_general_dollar_generate_private_reference').value;

/*
alert('qty:' + qty + "\n"
	+ 'dollar_value:' + dollar_value + "\n"
	+ 'min_subtotal:' + min_subtotal + "\n"
	+ 'activation_date:' + activation_date + "\n"
	+ 'expiration_date:' + expiration_date + "\n"
	+ 'is_extended:' + is_extended + "\n"
	+ 'private_reference:' + private_reference + "\n"
	);
*/



	var error_array = new Array();

	// check input
	if(!DiscountTypeGeneralDollar_IsValidGenerateQuantity(qty))
	{
		error_array.push('Invalid Quantity to generate');
		document.getElementById('discount_general_dollar_generate_qty').value = '';
	}
	if(dollar_value != '' && !DiscountTypeGeneralDollar_IsValidDollarAmount(dollar_value))
	{
		error_array.push('Invalid Dollar Value');
		document.getElementById('discount_general_dollar_generate_dollar_value').value = '';
	}
	if(min_subtotal != '' && !DiscountTypeGeneralDollar_IsValidDollarAmount(min_subtotal))
	{
		error_array.push('Invalid Minimum Subtotal');
		document.getElementById('discount_general_dollar_generate_min_subtotal').value = '';
	}
	if(expiration_date && expiration_date < 10000000)
	{
		error_array.push('Invalid Expiration Date - You may leave all month, day, and year blank or you must fill them in completely.');
		document.getElementById('discount_general_dollar_generate_expiration_date_year').value = '';
		document.getElementById('discount_general_dollar_generate_expiration_date_month').value = '';
		document.getElementById('discount_general_dollar_generate_expiration_date_day').value = '';
	}
	if(private_reference && !DiscountTypeGeneralDollar_IsValidPrivateReference(private_reference))
	{
		error_array.push('Invalid Private Reference - You may only use combinations of letters and numbers');
		document.getElementById('discount_general_dollar_generate_private_reference').value = '';
	}
	
	HideErrorBox('discount_general_dollar');
	if(error_array.length > 0)
	{
		ShowErrorBox('discount_general_dollar', error_array);
	}
	else
	{
		var post_parameter_array = Array();
		post_parameter_array[0] = Array('ajax','1');
		post_parameter_array[1] = Array('generate_codes','1');
		post_parameter_array[2] = Array('qty',qty);
		post_parameter_array[3] = Array('dollar_value',dollar_value);
		post_parameter_array[4] = Array('min_subtotal',min_subtotal);
		post_parameter_array[5] = Array('activation_date',activation_date);
		post_parameter_array[6] = Array('expiration_date',expiration_date);
		post_parameter_array[7] = Array('is_extended',is_extended.toString());
		post_parameter_array[8] = Array('private_reference',private_reference);

                document.getElementById('discount_general_dollar_generate_area').innerHTML = 'Please wait...';

		DoHTTPRequest("index.php?param=discount_admin&operation=discount_general_dollar&",
				DiscountTypeGeneralDollar_GenerateCertificateAreaHandler, post_parameter_array);
	}

}

function DiscountTypeGeneralDollar_IsValidGenerateQuantity(quantity_value)
{
	var valid_char_string = "1234567890";
	
	
	if(quantity_value.length < 1 || quantity_value.length > 6)
		return false;
	else
	{
		// make sure there are only numbers
		for(var i=0; i<quantity_value.length; i++)
		{
			if(valid_char_string.indexOf(quantity_value.charAt(i)) == -1)
			{
				return false;
			}
		}
	}
	
	// make sure the value is actually greater than zero
	if(Number(quantity_value) < 1)
		return false;
	
	return true;
}

function DiscountTypeGeneralDollar_IsValidCertificateFormat(cert_num)
{
	var valid_char_string = "1234567890";


	if(cert_num.length != 11)
		return false;
	else
	{
		// make sure there are only numbers
		for(var i=0; i<cert_num.length; i++)
		{
			if(valid_char_string.indexOf(cert_num.charAt(i)) == -1)
			{
				return false;
			}
		}
	}
	return true;
}

function DiscountTypeGeneralDollar_IsValidPrivateReference(pr_value)
{
	var valid_char_string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";


	if(pr_value.length < 1 || pr_value.length > 255)
		return false;
	else
	{
		// make sure there are only numbers
		for(var i=0; i<pr_value.length; i++)
		{
			if(valid_char_string.indexOf(pr_value.charAt(i)) == -1)
			{
				return false;
			}
		}
	}
	return true;
}

function DiscountTypeGeneralDollar_IsValidDollarAmount(dollar_value)
{
	var valid_char_string = "1234567890.";
	
	
	if(dollar_value.length < 1 || dollar_value.length > 10)
		return false;
	else
	{
		// make sure there are only numbers and decimal points
		for(var i=0; i<dollar_value.length; i++)
		{
			if(valid_char_string.indexOf(dollar_value.charAt(i)) == -1)
			{
				return false;
			}
		}
	
		// make sure there is only one decimal place
		if(dollar_value.indexOf('.') != dollar_value.lastIndexOf('.'))
			return false;
	}
	return true;
}

function DiscountTypeGeneralDollar_GenerateCertificateAreaHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			//alert(http_output);
			document.getElementById('discount_general_dollar_generate_area').innerHTML = http_output;
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}
}


function DiscountTypeGeneralDollar_EditCertificateAreaHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			//alert(http_output);
			document.getElementById('discount_general_dollar_edit_area').innerHTML = http_output;
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}
}

function DiscountTypeGeneralDollar_DownloadCertImg(cert_num)
{
	window.location = 'index.php?param=discount_admin&operation=discount_general_dollar&sub_op=generate_cert_img&cert_number='+JSFeature.Base64.encode(cert_num)+'&';
}