//
// JavaScript functions for CardDuper.
// Michael Benson - 3/16/04
//


var ns4;
var ie4;
var ns6;

// Test for which browser
if (document.layers) ns4 = true;
if (document.all) ie4 = true;
if ((document.getElementById) && (!document.all)) ns6 = true;


function ShowDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "visible";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "visible";
	}
	if (ns4) {
		document.layers[which].visibility = "show";
	}
}

function HideDiv(which)
{
	if (ie4) {
		document.all[which].style.visibility = "hidden";
	}
	if (ns6) {
		document.getElementById(which).style.visibility = "hidden";
	}
	if (ns4) {
		document.layers[which].visibility = "hide";
	}
}

function ShowHideCountry(divName, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "other") {
		ShowDiv(divName);
	} else {
		HideDiv(divName);
	}
}

function ShowHideStates(divUS, divCanada, divOther, countrySelectName)
{
	var selectbox = eval("document." + countrySelectName);
	country = selectbox.options[selectbox.selectedIndex].value;
	if (country == "USA") {
		ShowDiv(divUS);
		HideDiv(divCanada);
		HideDiv(divOther);
	} else if (country == "Canada") {
		HideDiv(divUS);
		ShowDiv(divCanada);
		HideDiv(divOther);
	} else {
		HideDiv(divUS);
		HideDiv(divCanada);
		ShowDiv(divOther);
	}
}

var stateNames = [
	'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
	'Colorado', 'Connecticut', 'Delaware', 'Washington D.C.', 'Florida',
	'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
	'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine',
	'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi',
	'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
	'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
	'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Puerto Rico',
	'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas',
	'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia',
	'Wisconsin', 'Wyoming'
];

var provinceNames = [
	'Alberta', 'British Columbia', 'Manitoba', 'New Brunswick', 'Newfoundland',
	'Nova Scotia', 'Northwest Territories', 'Ontario', 'Prince Edward Island', 'Quebec', 
	'Saskatchewan', 'Yukon Territory'
];

var stateCodes = [
	'AL', 'AK', 'AZ', 'AR', 'CA',
	'CO', 'CT', 'DE', 'DC', 'FL',
	'GA', 'HI', 'ID', 'IL', 'IN',
	'IA', 'KS', 'KY', 'LA', 'ME',
	'MD', 'MA', 'MI', 'MN', 'MS',
	'MO', 'MT', 'NE', 'NV', 'NH',
	'NJ', 'NM', 'NY', 'NC', 'ND',
	'OH', 'OK', 'OR', 'PA', 'PR',
	'RI', 'SC', 'SD', 'TN', 'TX',
	'UT', 'VT', 'VA', 'WA', 'WV',
	'WI', 'WY'
];

var provinceCodes = [
	'AB', 'BC', 'MB', 'NB', 'NF',
	'NS', 'NT', 'ON', 'PE', 'QC',
	'SK', 'YT'
];

function ReFillStates(countrysel, statesel)
{
	var countrybox = eval("document." + countrysel);
	var statebox = eval("document." + statesel);
	country = countrybox.options[countrybox.selectedIndex].value;
	
	statebox.options.length = 0;
	statebox.options[0] = new Option("(whole country)", "all");
	if (country == "USA") {
		for (i = 0;  i < stateNames.length;  i++) {
			statebox.options[i+1] = new Option(stateNames[i] + " (" + stateCodes[i] + ")", stateCodes[i]);
		}
	} else if (country == "Canada") {
		for (i = 0;  i < provinceNames.length;  i++) {
			statebox.options[i+1] = new Option(provinceNames[i] + " (" + provinceCodes[i] + ")", provinceCodes[i]);
		}
	}
}

function CheckRangeRadio(name)
{
	var radioBtn = eval("document." + name);
	radioBtn[0].checked = false;
	radioBtn[1].checked = true;
}

function DeleteCategories(num_cats)
{
	// Check if at least one category is selected:
	atleast1 = false;
	for (i = 0;  i < num_cats;  i++) {
		obj = eval("document.categories.cat_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one category to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected categories? All products in this category will also be deleted.")) {
		return false;
	}

	document.categories.action = "delete_categories.php";
	document.categories.submit();
	return true;
}

function AddCategory()
{
	document.categories.action = "add_category.php";
	document.categories.submit();
	return true;
}

function ModifyCategory(num_cats)
{
	// Check that exactly one category is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_cats;  i++) {
		obj = eval("document.categories.cat_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.categories.cat_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a category to modify (or just click the category name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one category.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.categories.action = "modify_category.php?id=" + id;
	document.categories.submit();
	
	return true;
}

function UpdateCategory(id)
{
	if (document.category.catname.value == "") {
		alert("Please supply a category name.");
		return false;
	}
	
	document.category.action = "update_category.php?id=" + id;
	document.category.submit();
	return true;
}

function AddFAQCategory()
{
	document.faqcategories.action = "add_faq_category.php";
	document.faqcategories.submit();
	return true;
}

function DeleteFAQCategories(num_cats)
{
	// Check if at least one category is selected:
	atleast1 = false;
	for (i = 0;  i < num_cats;  i++) {
		obj = eval("document.faqcategories.cat_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one FAQ category to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected FAQ categories? All FAQs in this category will also be deleted.")) {
		return false;
	}

	document.faqcategories.action = "delete_faq_categories.php";
	document.faqcategories.submit();
	return true;
}

function ModifyFAQCategory(num_cats)
{
	// Check that exactly one category is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_cats;  i++) {
		obj = eval("document.faqcategories.cat_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.faqcategories.cat_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select an FAQ category to modify (or just click the category name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one FAQ category.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.faqcategories.action = "modify_faq_category.php?id=" + id;
	document.faqcategories.submit();
	
	return true;
}

function UpdateFAQCategory(id)
{
	if (document.faqcategory.catname.value == "") {
		alert("Please supply an FAQ category name.");
		return false;
	}
	
	document.faqcategory.action = "update_faq_category.php?id=" + id;
	document.faqcategory.submit();
	return true;
}

function DisplayFAQs(cat_id)
{
	document.faqcategory.action = "faqs.php?cat_id=" + cat_id;
	document.faqcategory.submit();
	return true;
}

function BackToFAQs(cat_id)
{
	document.faq.action = "faqs.php?cat_id=" + cat_id;
	document.faq.submit();
	return true;
}

function DeleteAttributes(num_attrs)
{
	// Check if at least one attribute is selected:
	atleast1 = false;
	for (i = 0;  i < num_attrs;  i++) {
		obj = eval("document.attributes.attr_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one attribute to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected attributes?")) {
		return false;
	}

	document.attributes.action = "delete_attributes.php";
	document.attributes.submit();
	return true;
}

function AddAttribute()
{
	document.attributes.action = "add_attribute.php";
	document.attributes.submit();
	return true;
}

function UpdateAttribute(id)
{
	if (document.attribute.attrname.value == "") {
		alert("Please supply an attribute name.");
		return false;
	}
	
	document.attribute.action = "update_attribute.php?id=" + id;
	document.attribute.submit();
	return true;
}

function ModifyAttribute(num_attrs)
{
	// Check that exactly one attribute is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_attrs;  i++) {
		obj = eval("document.attributes.attr_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.attributes.attr_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select an attribute to modify (or just click the attribute name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one attribute.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.attributes.action = "modify_attribute.php?id=" + id;
	document.attributes.submit();
	
	return true;
}

function AddSerials()
{
	document.serialnums.action = "add_serials.php";
	document.serialnums.submit();
	return true;
}

function DeleteSerials(num_serials)
{
	// Check if at least one serial number is selected:
	atleast1 = false;
	for (i = 0;  i < num_serials;  i++) {
		obj = eval("document.serialnums.serial_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one serial number to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected serial numbers?")) {
		return false;
	}

	document.serialnums.action = "delete_serials.php";
	document.serialnums.submit();
	return true;
}

function PageSerials(pagenum)
{
	document.serialnums.pagenum.value = pagenum;
	document.serialnums.submit();
	return true;
}

function AddDevice()
{
	document.devices.action = "add_device.php";
	document.devices.submit();
	return true;
}

function DeleteDevices(num_devs)
{
	// Check if at least one device is selected:
	atleast1 = false;
	for (i = 0;  i < num_devs;  i++) {
		obj = eval("document.devices.dev_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one device to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected devices?")) {
		return false;
	}

	document.devices.action = "delete_devices.php";
	document.devices.submit();
	return true;
}

function ModifyDevice(num_devs)
{
	// Check that exactly one device is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_devs;  i++) {
		obj = eval("document.devices.dev_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.devices.dev_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a device to modify (or just click the device name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one device.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.devices.action = "modify_device.php?id=" + id;
	document.devices.submit();
	
	return true;
}

function UpdateDevice(id)
{
	if (document.device.devname.value == "") {
		alert("Please supply a device name.");
		return false;
	}
	
	document.device.action = "update_device.php?id=" + id;
	document.device.submit();
	return true;
}

function AddFunction()
{
	document.functions.action = "add_function.php";
	document.functions.submit();
	return true;
}

function DeleteFunctions(num_funcs)
{
	// Check if at least one device function is selected:
	atleast1 = false;
	for (i = 0;  i < num_funcs;  i++) {
		obj = eval("document.functions.func_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one device function to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected device functions?")) {
		return false;
	}

	document.functions.action = "delete_functions.php";
	document.functions.submit();
	return true;
}

function ModifyFunction(num_funcs)
{
	// Check that exactly one device function is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_funcs;  i++) {
		obj = eval("document.functions.func_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.functions.func_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a device function to modify (or just click the function name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one device function.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.functions.action = "modify_function.php?id=" + id;
	document.functions.submit();
	
	return true;
}

function UpdateFunction(id)
{
	if (document.func.funcname.value == "") {
		alert("Please supply a device function name.");
		return false;
	}
	
	document.func.action = "update_function.php?id=" + id;
	document.func.submit();
	return true;
}

function DisplayProducts(cat_id)
{
	document.category.action = "products.php?cat_id=" + cat_id;
	document.category.submit();
	return true;
}

function DeleteProducts(num_prods, cat_id)
{
	// Check if at least one category is selected:
	atleast1 = false;
	for (i = 0;  i < num_prods;  i++) {
		obj = eval("document.products.prod_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one product to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected product?")) {
		return false;
	}

	document.products.action = "delete_products.php?cat_id=" + cat_id;
	document.products.submit();
	return true;
}

function AddProduct(cat_id)
{
	document.products.action = "add_product.php?cat_id=" + cat_id;
	document.products.submit();
	return true;
}

function ModifyProduct(num_prods)
{
	// Check that exactly one category is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_prods;  i++) {
		obj = eval("document.products.prod_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.products.prod_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a product to modify (or just click the product name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one product.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.products.action = "modify_product.php?id=" + id;
	document.products.submit();
	
	return true;
}

function UpdateProduct(id)
{
	if (document.product.prodname.value == "") {
		alert("Please supply a product name.");
		return false;
	}
	
	document.product.action = "update_product.php?id=" + id;
	document.product.submit();
	return true;
}

function BackToProducts(cat_id)
{
	document.product.action = "products.php?cat_id=" + cat_id;
	document.product.submit();
	return true;
}

function FindUsers()
{
	document.users.action = "user_results.php";
	document.users.submit();
	return true;
}

function AddUser()
{
	document.users.action = "add_user.php";
	document.users.submit();
	return true;
}

function UpdateUser(id)
{
	if (document.user.firstname.value == "") {
		alert("Please supply a first name.");
		return false;
	}
	if (document.user.lastname.value == "") {
		alert("Please supply a last name.");
		return false;
	}
	if (document.user.email.value == "") {
		alert("Please supply an email address.");
		return false;
	}

	document.user.thefunction.value = "update";
	document.user.action = "modify_user.php?id=" + id;
	document.user.submit();
	return true;
}

function UserRegistrations(id)
{
	document.user.action = "user_registrations.php?id=" + id;
	document.user.submit();
	return true;
}

function PageUsers(pagenum)
{
	document.usersfound.pagenum.value = pagenum;
	document.usersfound.submit();
	return true;
}

function DeleteUsers(num_users)
{
	// Check if at least one user is selected:
	atleast1 = false;
	for (i = 0;  i < num_users;  i++) {
		obj = eval("document.usersfound.user_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one user to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected users?")) {
		return false;
	}

	document.usersfound.thefunction.value = "delete";
	document.usersfound.submit();
	return true;
}

function ModifyUser(num_users)
{
	// Check that exactly one user is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_users;  i++) {
		obj = eval("document.usersfound.user_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.usersfound.user_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a user to modify (or just click the user name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one user.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.usersfound.action = "modify_user.php?id=" + id;
	document.usersfound.submit();
	
	return true;
}

function ModifyUserLink(id)
{
	document.usersfound.action = "modify_user.php?id=" + id;
	document.usersfound.submit();
	return true;
}

function BackToQuery()
{
	document.usersfound.action = "users.php";
	document.usersfound.submit();
	return true;
}

function BackToResults()
{
	document.user.action = "user_results.php";
	document.user.submit();
	return true;
}

function BackToUser(user_id)
{
	document.registrations.action = "modify_user.php?id=" + user_id;
	document.registrations.submit();
	return true;
}

function AddFAQ(cat_id)
{
	document.faqs.action = "add_faq.php?cat_id=" + cat_id;
	document.faqs.submit();
	return true;
}

function UpdateFAQ(id)
{
	if (document.faq.question.value == "") {
		alert("Please supply a question.");
		return false;
	}
	if (document.faq.answer.value == "") {
		alert("Please supply an answer.");
		return false;
	}

	document.faq.action = "update_faq.php?id=" + id;
	document.faq.submit();
	return true;
}

function ModifyFAQ(num_faqs)
{
	// Check that exactly one FAQ is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_faqs;  i++) {
		obj = eval("document.faqs.faq_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.faqs.faq_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select an FAQ to modify (or just click the question).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one FAQ.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.faqs.action = "modify_faq.php?id=" + id;
	document.faqs.submit();
	
	return true;
}

function DeleteFAQs(num_faqs, cat_id)
{
	// Check if at least one FAQ is selected:
	atleast1 = false;
	for (i = 0;  i < num_faqs;  i++) {
		obj = eval("document.faqs.faq_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one FAQ to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected FAQs?")) {
		return false;
	}

	document.faqs.action = "delete_faqs.php?cat_id=" + cat_id;
	document.faqs.submit();
	return true;
}

function AddContent()
{
	document.contents.action = "add_content.php";
	document.contents.submit();
	return true;
}

function UpdateContent(id, lang)
{
	if (document.content_form.name.value == "") {
		alert("Please supply a name.");
		return false;
	}
  lang = lang;
	document.content_form.action = "modify_content.php?id=" + id + "&lang=" + lang;
	document.content_form.submit();
	return true;
}

function ModifyContent(num_contents)
{
	// Check that exactly one content is selected:
	numchecked = 0;
	id = "";
	for (i = 0;  i < num_contents;  i++) {
		obj = eval("document.contents.content_" + i);
		if (obj && obj.checked) {
			if (numchecked == 0) {
				obj2 = eval("document.contents.content_id_" + i);
				id = obj2.value;
			}
			numchecked++;
		}
	}
	if (numchecked == 0) {
		alert("Please select a content to modify (or just click the name).");
		return false;
	}
	if (numchecked > 1 && !confirm("You have selected more than one content.  Do you want to modify the first one selected?")) {
		return false;
	}

	document.contents.action = "modify_content.php?id=" + id;
	document.contents.submit();
	
	return true;
}

function DeleteContents(num_contents)
{
	// Check if at least one content is selected:
	atleast1 = false;
	for (i = 0;  i < num_contents;  i++) {
		obj = eval("document.contents.content_" + i);
		if (obj && obj.checked) {
			atleast1 = true;
			break;
		}
	}
	if (!atleast1) {
		alert("Please select at least one content to delete.");
		return false;
	}
	if (!confirm("Are you sure you want to delete the selected contents?")) {
		return false;
	}

	document.contents.action = "delete_contents.php";
	document.contents.submit();
	return true;
}

function InitializeDatabase()
{
	if (confirm("Are you sure you want to initialize the database?")) {
		document.initdbform.submit();
	}
}

function Warranty_Update(id)
{
	if (document.war_modify.war_exp_io.value == "") {
		alert("Please supply a warranty expiration date.");
		return false;
	}
	
	document.war_modify.action = "war_update.php?id=" + id;
	document.war_modify.submit();
	return true;
}

function Warranty_Open(num_regs)
{
	for (i = 0;  i < num_regs;  i++) {
		obj = eval("document.registrations.user_reg_" + i);
		if (obj && obj.checked) {
			obj = eval("document.registrations.user_reg_id_" + i);
			reg_id = obj.value;

			document.registrations.action = "../admin_warranty/war_modify.php?rec=" + reg_id;
			document.registrations.submit();

			break;
		}
	}


	return true;
}

