// Preload images
function newImage(arg) {
  if (document.images) {
    rslt = new Image();
    rslt.src = arg;
    return rslt;
  }
}

// Function to change the style on mouse over for the nav menu items
function changeMenuItem(elid, elclass) {
	document.getElementById(elid).className = elclass;
}

// Function to create links for table cells
function gotoLink(linkURL) {
	document.location.href = linkURL;
}

function roundAmount(amount) {
    amount -= 0;
    amount = (Math.round(amount*100))/100;
    return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function setInnerHTML(id, HTML) {
	if (document.getElementById) {
		document.getElementById(id).innerHTML = HTML;
	} else if (document.all) {
		document.all[id].innerHTML = HTML;
	} else if (document.layers) {
		document.layers[id].innerHTML=HTML;
	}
}

function calculateOrder() {
	
	// Set the tax and shipping based on the state selected
	if (document.orderForm.order_state.selectedIndex != 0) {
		
		if (document.orderForm.order_state[document.orderForm.order_state.selectedIndex].value == "NY") {
			//document.orderForm.order_item_tax.value = stateArray[document.orderForm.order_state.selectedIndex-1][0];
			document.orderForm.order_item_tax.value = getNYStateTax(document.orderForm.order_city.value);
		} else {
			document.orderForm.order_item_tax.value = 0;	
		}
		
		if (document.orderForm.order_shipping_method[0].checked == true) {
			document.orderForm.order_item_shipping.value = stateArray[document.orderForm.order_state.selectedIndex-1][1];
		} else {
			document.orderForm.order_item_shipping.value = stateArray[document.orderForm.order_state.selectedIndex-1][2];
		}
	} else {
		document.orderForm.order_item_tax.value = 0;
		document.orderForm.order_item_shipping.value = 0;
	}
	
	// Calculate the order costs based on the quantity selected
	var orderShipping = 0;
	var orderTax = 0;
	var orderTotal = 0;
	var trucomfortTotal = 0;
	var trucomfort2Total = 0;
	var trucomfortDiscountQty = 0;
	var trucomfort2DiscountQty = 0;
	var trucomfortRegQty = 0;
	var trucomfort2RegQty = 0;
	var totalQty = 0;
	var shippingRegQty = 0;
	var shippingDiscountQty = 0;
	
	var trucomfortPrice = document.orderForm.trucomfort_price.value - 0;
	var trucomfort2Price = document.orderForm.trucomfort2_price.value - 0;
	var orderItemShipping = document.orderForm.order_item_shipping.value - 0;
	var orderItemTax = document.orderForm.order_item_tax.value - 0;
	
	// Calculate costs for TruComfort II
	if (document.orderForm.order_trucomfort2_quantity.value != "" && document.orderForm.order_trucomfort2_quantity.value > 0) {
		trucomfort2RegQty = 1;
		trucomfort2DiscountQty = document.orderForm.order_trucomfort2_quantity.value - 1;
		trucomfort2Total = (trucomfort2RegQty * trucomfort2Price) + ((trucomfort2DiscountQty) * (trucomfort2Price) * .9);
	}
	
	// Calcuate costs for TruComfort
	if (document.orderForm.order_trucomfort_quantity.value != "" && document.orderForm.order_trucomfort_quantity.value > 0) {
		trucomfortRegQty = 1;
		trucomfortDiscountQty = document.orderForm.order_trucomfort_quantity.value - 1;
		trucomfortTotal = (trucomfortRegQty * trucomfortPrice) + ((trucomfortDiscountQty) * (trucomfortPrice) * .9);
	}

	// Calculate the shipping
	totalQty = trucomfortRegQty + trucomfort2RegQty + trucomfortDiscountQty + trucomfort2DiscountQty;
	if (totalQty > 0) {
		shippingRegQty = 1;
		shippingDiscountQty = totalQty - 1;
		orderShipping = ((shippingRegQty * orderItemShipping) + ((shippingDiscountQty) * (orderItemShipping) * .5)); 
	}

	// Calculate the tax
	orderTax = (trucomfort2Total + trucomfortTotal + orderShipping)*(orderItemTax/100);
	
	// Calculate the order total
	orderTotal = trucomfort2Total + trucomfortTotal + orderShipping + orderTax;
	
	// Display discount notifications
	if (trucomfortDiscountQty > 0) {
		setInnerHTML('trucomfort_discount', 'You qualified for a 10% quantity discount.');
	} else {
		setInnerHTML('trucomfort_discount', '');
	}
	
	if (trucomfort2DiscountQty > 0) {
		setInnerHTML('trucomfort2_discount', 'You qualified for a 10% quantity discount.');
	} else {
		setInnerHTML('trucomfort2_discount', '');
	}
	
	if (shippingDiscountQty > 0) {
		setInnerHTML('shipping_discount', 'You qualified for a shipping discount.');
	} else {
		setInnerHTML('shipping_discount', '');
	}
	
	// Display the values
	document.orderForm.order_trucomfort_subtotal.value = roundAmount(trucomfortTotal);
	document.orderForm.order_trucomfort2_subtotal.value = roundAmount(trucomfort2Total);
	document.orderForm.order_shipping.value = roundAmount(orderShipping);
	document.orderForm.order_tax.value = roundAmount(orderTax);
	document.orderForm.order_total.value = roundAmount(orderTotal);
}

function validateOrder() {
	
	var errors = false;
	
	if (document.orderForm.order_trucomfort_quantity.value == "" && document.orderForm.order_trucomfort2_quantity.value == "") {
		alert('Please select at least one product to order.');
		document.orderForm.order_trucomfort2_quantity.focus();
		document.orderForm.order_trucomfort2_quantity.select();		errors = true;
	} else if (document.orderForm.order_trucomfort_quantity.value != "" && isNaN(document.orderForm.order_trucomfort_quantity.value)) {
		alert('Please enter a valid quantity for the TruComfort.');
		document.orderForm.order_trucomfort_quantity.focus();
		document.orderForm.order_trucomfort_quantity.select();
		errors = true;
	} else if (document.orderForm.order_trucomfort2_quantity.value != "" && isNaN(document.orderForm.order_trucomfort2_quantity.value)) {
		alert('Please enter a valid quantity for the TruComfort II.');
		document.orderForm.order_trucomfort2_quantity.focus();
		document.orderForm.order_trucomfort2_quantity.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_cc_number.value == "" || document.orderForm.order_cc_number.value.length < 15)) {
		alert('Please enter a valid credit card number.');
		document.orderForm.order_cc_number.focus();
		document.orderForm.order_cc_number.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_cvv2_code.value.length < 3)) {
		alert('Please enter a valid CVV2 code.');
		document.orderForm.order_cvv2_code.focus();
		document.orderForm.order_cvv2_code.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_cc_exp_month.value.length != 2 || isNaN(document.orderForm.order_cc_exp_month.value))) {
		alert('Please enter a valid two digit credit card expiration month.');
		document.orderForm.order_cc_exp_month.focus();
		document.orderForm.order_cc_exp_month.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_cc_exp_year.value.length != 4 || isNaN(document.orderForm.order_cc_exp_year.value))) {
		alert('Please enter a valid four digit credit card expiration year.');
		document.orderForm.order_cc_exp_year.focus();
		document.orderForm.order_cc_exp_year.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_first_name.value == "")) {
		alert('Please enter your first name.');
		document.orderForm.order_first_name.focus();
		document.orderForm.order_first_name.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_last_name.value == "")) {
		alert('Please enter your last name.');
		document.orderForm.order_last_name.focus();
		document.orderForm.order_last_name.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_address_1.value == "")) {
		alert('Please enter your address.');
		document.orderForm.order_address_1.focus();
		document.orderForm.order_address_1.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_city.value == "")) {
		alert('Please enter your city.');
		document.orderForm.order_city.focus();
		document.orderForm.order_city.select();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_state.selectedIndex == 0)) {
		alert('Please select your state.');
		document.orderForm.order_state.focus();
		errors = true;
	} else if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value != "PayPal" && (document.orderForm.order_zip_code.value == "")) {
		alert('Please enter your zip code.');
		document.orderForm.order_zip_code.focus();
		document.orderForm.order_zip_code.select();
		errors = true;
	} else if (document.orderForm.order_phone.value == "") {
		alert('Please enter your phone number.');
		document.orderForm.order_phone.focus();
		document.orderForm.order_phone.select();
		errors = true;
	} else if (document.orderForm.order_email.value == "") {
		alert('Please enter your email address.');
		document.orderForm.order_email.focus();
		document.orderForm.order_email.select();
		errors = true;
	}
	
	if (!errors) {
		calculateOrder();
		document.orderForm.order_cc_number.disabled = false;	
		document.orderForm.order_cvv2_code.disabled = false;
		document.orderForm.order_cc_exp_month.disabled = false;
		document.orderForm.order_cc_exp_year.disabled = false;
		document.orderForm.order_address_1.disabled = false;
		document.orderForm.order_address_2.disabled = false;
		document.orderForm.order_city.disabled = false;
		document.orderForm.order_state.disabled = false;
		document.orderForm.order_zip_code.disabled = false;
		document.orderForm.submit();
	} 
	
}

// Function to properly set available order form values based on the payment type
function setOrderValues() {
	
	if (document.orderForm.order_card_type[document.orderForm.order_card_type.selectedIndex].value == "PayPal") {
		document.orderForm.order_cc_number.disabled = true;	
		document.getElementById('order_cc_number').className = "disabled";
		document.orderForm.order_cvv2_code.disabled = true;
		document.getElementById('order_cvv2_code').className = "disabled";
		document.orderForm.order_cc_exp_month.disabled = true;
		document.getElementById('order_cc_exp_month').className = "disabled";
		document.orderForm.order_cc_exp_year.disabled = true;
		document.getElementById('order_cc_exp_year').className = "disabled";
		document.orderForm.order_address_1.disabled = true;
		document.getElementById('order_address_1').className = "disabled";
		document.orderForm.order_address_2.disabled = true;
		document.getElementById('order_address_2').className = "disabled";
		document.orderForm.order_city.disabled = true;
		document.getElementById('order_city').className = "disabled";
		document.orderForm.order_state.disabled = true;
		document.getElementById('order_state').className = "disabled";
		document.orderForm.order_zip_code.disabled = true;
		document.getElementById('order_zip_code').className = "disabled";
	} else {
		document.orderForm.order_cc_number.disabled = false;	
		document.getElementById('order_cc_number').className = "text";
		document.orderForm.order_cvv2_code.disabled = false;
		document.getElementById('order_cvv2_code').className = "text";
		document.orderForm.order_cc_exp_month.disabled = false;
		document.getElementById('order_cc_exp_month').className = "text";
		document.orderForm.order_cc_exp_year.disabled = false;
		document.getElementById('order_cc_exp_year').className = "text";
		document.orderForm.order_address_1.disabled = false;
		document.getElementById('order_address_1').className = "text";
		document.orderForm.order_address_2.disabled = false;
		document.getElementById('order_address_2').className = "text";
		document.orderForm.order_city.disabled = false;
		document.getElementById('order_city').className = "text";
		document.orderForm.order_state.disabled = false;
		document.getElementById('order_state').className = "text";
		document.orderForm.order_zip_code.disabled = false;
		document.getElementById('order_zip_code').className = "text";
	}
	
}

// Function to ensure no duplicate orders
function disableButton() {
	document.placeOrderForm.placeOrder.disabled = true;
	return true;
}

function goNextPrev(start_row) {
	document.adminForm.action = "index.php";
	document.adminForm.start_row.value = start_row;
	document.adminForm.submit();
}

function setFilter() {
	document.adminForm.action = "index.php";
	if (document.adminForm.start_row) {
		document.adminForm.start_row.value = "0";
	}
	document.adminForm.submit();
}

function setOrderBy(order_by) {
	document.adminForm.action = "index.php";
	if (document.adminForm.start_row) {
		document.adminForm.start_row.value = "0";
	}
	document.adminForm.order_by.value = order_by;
	document.adminForm.submit();
}

// Function to add or modify a provider
function addModProvider(provider_id) {
	
	document.adminForm.provider_id.value = provider_id;
	document.adminForm.submit();
	
}

// Function to delete a provider
function deleteProvider(provider_id, provider_name) {
		
	if (confirm("Are you sure you want to delete the professional '" + provider_name + "'")) {
		document.adminForm.provider_id.value = provider_id;
		document.adminForm.action = "professional_delete.php";
		document.adminForm.submit();
	}
	
}

// Function to save a provider
function applyProvider() {
	
	var errors = false;
	
	if (document.adminForm.provider_first_name.value == "") {
		alert('Please enter a first name for this professional.');
		document.adminForm.provider_first_name.focus();
		document.adminForm.provider_first_name.select();
		errors = true;
	} else if (document.adminForm.provider_last_name.value == "") {
		alert('Please enter a last name for this professional.');
		document.adminForm.provider_last_name.focus();
		document.adminForm.provider_last_name.select();
		errors = true;
	} else if (document.adminForm.provider_city.value == "") {
		alert('Please enter a city for this professional.');
		document.adminForm.provider_city.focus();
		document.adminForm.provider_city.select();
		errors = true;
	} else if (document.adminForm.provider_state.value == "") {
		alert('Please enter a state for this professional.');
		document.adminForm.provider_state.focus();
		document.adminForm.provider_state.select();
		errors = true;
	}
	
	if (!errors) {
		document.adminForm.submit();
	}
	
}

// Function to cancel an add/modify action
function cancelAddMod() {
	document.adminForm.action = "index.php";
	document.adminForm.submit();
}

// Function to view a contact form
function viewContact(contact_id) {
	
	document.adminForm.contact_id.value = contact_id;
	document.adminForm.submit();
	
}

// Function to view an order
function viewOrder(order_id) {
	
	document.adminForm.order_id.value = order_id;
	document.adminForm.submit();
	
}

// Function to send email
function sendEmail() {
	
	var errors = false;
	
	if (document.adminForm.send_providers.checked != true && document.adminForm.send_contacts.checked != true && document.adminForm.send_customers.checked != true) {
		alert('Please select at least one set of recipients.');
		document.adminForm.send_providers.focus();
		errors = true;
	} else if (document.adminForm.email_from.value == "") {
		alert('Please enter the from address for this email.');
		document.adminForm.email_from.focus();
		document.adminForm.email_from.select();
		errors = true;
	} else if (document.adminForm.email_subject.value == "") {
		alert('Please enter the subject for this email.');
		document.adminForm.email_subject.focus();
		document.adminForm.email_subject.select();
		errors = true;
	} else if (document.adminForm.email_body.value == "") {
		alert('Please enter the body for this email.');
		document.adminForm.email_body.focus();
		document.adminForm.email_body.select();
		errors = true;
	}
	
	if (!errors) {
		document.adminForm.submit();
	}
	
}

function getNYStateTax(cityNameVar) {
	
	var cityName = new String(cityNameVar);
	cityName = cityName.replace(/ /g,"_");
	cityName = cityName.toUpperCase();
	
	var ra = /(.+)(,)(.+_County)(,\d\.\d\d\d?,0\.\d\d\d?,\d\.\d\d)(,)(\d\.?\d?\d?\d?)/
	var ri = /(,)(\d\.\d\d\d?)(,)(0\.\d\d\d?)(,)(\d\.\d\d)(,)(\d\.?\d?\d?\d?)/
	
	var foundCity = false;
	var city = "";
	var county = "";
	var rates = "";
	var localRate = "";
	var transitRate = "";
	var stateRate = "";
	var totalTaxRate = 8.625;
	var i = 0;
	
	while (foundCity == false && i < cityRate.length) {
	
		currentCity = cityRate[i];

		if (ra.test(currentCity)) {
			city = currentCity.replace(ra,"$1")
			county = currentCity.replace(ra,"$3")
			rates = currentCity.replace(ra,"$4$5$6")
		}
		
		if (city == cityName) {
	
			if (ri.test(rates)) {
				localRate = rates.replace(ri,"$2")
				transitRate = rates.replace(ri,"$4")
				stateRate = rates.replace(ri,"$6")
				totalTaxRate = rates.replace(ri,"$8")
			}
	
			var ro = /^(\d)$/
	
			if (ro.test(totalTaxRate)) {
				totalTaxRate=totalTaxRate.replace(ro,"$1\.00")
			}
			
			foundCity = true;
			
		}
		
		i = i + 1;
		
	}
	
	return totalTaxRate;
	
}	