function show(newItem) {
	var item = document.getElementById(newItem);
	if (item.style.display=='none') {
		item.style.display='block';
	}
	else {
		item.style.display='none';
	}

	hideOtherToxProducts(newItem);
	genericUpdatePrice();
}

function showThis(newItem) {
	var item = document.getElementById(newItem);
	if (item.style.display=='none') {
		item.style.display='block';
	}
}

function hide(item) {
	var itemToHide = document.getElementById(item);
	itemToHide.style.display='none';
}

function hideOtherToxProducts(item) {
	var radioToxProducts = document.quote_form.radioToxProducts;
	for (var i=0; i < radioToxProducts.length; i++) {
		var value = radioToxProducts[i].value; 
		if (value != item) {
			var itemToClose = document.getElementById(value);
			itemToClose.style.display = 'none';
		}
	}
	
}

function matchEmail() {
	var email1 = document.quote_form.emailaddress.value;
	var email2 = document.quote_form.confirmemailaddress.value;
	if (email1!=email2) {
		alert("The email addresses entered do not match. Please check the information, so that we can send you a product quote. Thank you.");
	}
	
}

function updatePrice(price) {
	var priceElement = document.getElementById("price");
	if (isNaN(price)) {
		price = 0;
	}
	var formatted_price = formatCurrency(price);
	priceElement.firstChild.nodeValue = formatted_price;

	document.quote_form.total.value = price;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function clearHiddenFields() {
	document.quote_form.quantity.value = "";
	document.quote_form.category.value = "";
	document.quote_form.description.value = "";
	document.quote_form.unit_price.value = "";
	document.quote_form.discount.value = "";
	document.quote_form.line_total.value = "";

	document.quote_form.quantity2.value = "";
	document.quote_form.category2.value = "";
	document.quote_form.description2.value = "";
	document.quote_form.unit_price2.value = "";
	document.quote_form.discount2.value = "";
	document.quote_form.line_total2.value = "";

	document.quote_form.total.value = "";

}

function genericUpdatePrice() {
	// CONSTANTS FOR COMPONENT PRICES
	var PRICE_DMO = 52500;
	var PRICE_CERT = 330;
	var PRICE_MECHRPT = 55000;
	var PRICE_XTRA_TREATMENT = 6250;
	var PRICE_SUMRPT = 27500;
	var PRICE_RNA = 90;
	var PRICE_HYB = 630;
	var PRICE_TOXFXCHP = 380;
	var PRICE_RAT230CHP = 530;
	var PRICE_DATAMGMT = 200;

	clearHiddenFields();
	var quote_form = document.quote_form;
	var radioToxProducts = document.quote_form.radioToxProducts;
	var price = 0; 

	var quantity = 0;
	var category = "";
	var description = "";
	var unit_price = 0;
	var discount = 0;
	var line_total = 0;
	var service_margin = 0;
	var array_price = 0;
  
	var quote_terms = "Quoted prices guaranteed for 30 days from quote date.";

	for (var i=0; i < radioToxProducts.length; i++) {
		if (radioToxProducts[i].checked) {
			var value = radioToxProducts[i].value;
			if (value=="drugmatrix") {
				var licenses = document.quote_form.textDrugmatrixLicenses.value;
				licenses = parseInt(licenses);
				if (licenses==1) {
					discount = 0;
				}
				else if (licenses==2) {
					discount = 0.1;
				}
				else if (licenses==3) {
					discount = 0.1666666666666666666666666666666666;
				}
				else if (licenses>=4) {
					discount = 0.25;
				}
				line_total += licenses * PRICE_DMO * (1-discount);

				if (licenses==3) {
					discount = 0.17;
				}

				quantity = licenses;
				category = "DMO";
				description = "DrugMatrixOnline, Single Named User, 1 Year License";
				unit_price = PRICE_DMO;
				price += line_total;
			}
			else if (value=="toxicogenomics") {
				var radioServices = document.quote_form.radioServices;
				for (var j = 0; j < radioServices.length; j++) {
					if(radioServices[j].checked){
						var selectedReport = radioServices[j].value;
/*
						if (selectedReport == "custom_report") {
							description = "Toxicogenomics Custom Report Services";
							category = "RPT-CUST"
						}
						else if (selectedReport == "toxfx_report") {
							description = "ToxFX Services and Report";
							category = "RPT-FX 01";
						}
*/
						category = "RPT-DATA-PREP";
						var number_of_drugs = parseInt(quote_form.report_textToxfxDrugs.value);
						var number_of_doses = parseInt(quote_form.report_textToxfxDoses.value);
						var number_of_treatment_durations = parseInt(quote_form.report_textToxfxDurations.value);
						var number_of_animals = parseInt(quote_form.report_textToxfxAnimals.value);

						var genderMultiplier = 1;
						if (quote_form.report_radioToxfxMaleFemale[0].checked) {
							genderMultiplier = parseInt(quote_form.report_radioToxfxMaleFemale[0].value);
						}
						else if (quote_form.report_radioToxfxMaleFemale[1].checked) {
							genderMultiplier = parseInt(quote_form.report_radioToxfxMaleFemale[1].value);
						}
		
						var certificates = number_of_drugs * (number_of_doses + 1) * number_of_treatment_durations * number_of_animals * genderMultiplier;
		
						quantity = certificates;

						// NOTE: SERVICES PRICE = RNA ISOLATION + TARGET PREP
						var checkboxServices = document.quote_form.checkboxServices;
						var orderedLabServices = false;
						unit_price = 0;
						for (var k=0; k < checkboxServices.length; k++) {
							if (checkboxServices[k].checked) {
								if(checkboxServices[k].value=="rna_isolation") {
									description = "RNA Isolation, Target Preparation and Microarray Data Generation";
									unit_price += PRICE_RNA; // RNA Isolation price
									unit_price += PRICE_HYB; // Target Prep and Microarray Data Generation price
									orderedLabServices = true;
								}
								else if (checkboxServices[k].value=="target_prep") {
									description = "Target Preparation and Microarray Data Generation";
									unit_price += PRICE_HYB; // Target Prep and Microarray Data Generation price
									orderedLabServices = true;
								}
								else if (checkboxServices[k].value=="customer_supplied") {
									description = "Customer-Supplied Microarray Data";
								}
							}

						}

						// NOTE: ARRAY PRICE = PROCESSING MARGIN + ARRAY PRICE
						var radioArrays = document.quote_form.radioArrays;
						for (var m=0; m < radioArrays.length; m++) {
							if (radioArrays[m].checked) {
								if(radioArrays[m].value=="rat_toxfx") {
									description = description + " on " + "Rat ToxFX 1.0 Microarray";
									if (orderedLabServices) {
										unit_price += PRICE_TOXFXCHP;
									} else {
										unit_price += PRICE_DATAMGMT;
									}
								}
								else if (radioArrays[m].value=="rat_230") {
									description = description + " on " + "Rat 230 2.0 Whole Genome Array";
									if (orderedLabServices) {
										unit_price += PRICE_RAT230CHP;
									} else {
										unit_price += PRICE_CERT;
										unit_price += PRICE_DATAMGMT;
									}
								}
								//else if (radioArrays[m].value=="customer_array") {
								//	description = description + " on " + "Customer-Supplied Array";
								//	if (!orderedLabServices) {
								//		unit_price += PRICE_DATAMGMT;
								//	}
								//}
							}
						}
						
						quantity = certificates;
						line_total = quantity * unit_price;
						price += line_total;

						if (selectedReport == "custom_report") {
							quantity = 1;
							unit_price = 0;
							line_total = 0;
							price = 0;
							description = "Toxicogenomics Custom Report Services";
							category = "RPT-CUST";
							quote_terms = "Please contact Entelos at tox_sales@entelos.com or 650-572-5400 for a formal quote.";
						}

						else if (selectedReport == "toxfx_report") {
							description = "ToxFX Services and Report";
							category = "RPT-FX 01";
						}
						
					}
				}

			}
			else if (value=="toxfx") {
				var number_of_drugs = parseInt(quote_form.textToxfxDrugs.value);
				var number_of_doses = parseInt(quote_form.textToxfxDoses.value);
				var number_of_treatment_durations = parseInt(quote_form.textToxfxDurations.value);
				var number_of_animals = parseInt(quote_form.textToxfxAnimals.value);
				
				var genderMultiplier = 1;
				if (quote_form.radioToxfxMaleFemale[0].checked) {
					genderMultiplier = parseInt(quote_form.radioToxfxMaleFemale[0].value);
				}
				else if (quote_form.radioToxfxMaleFemale[1].checked) {
					genderMultiplier = parseInt(quote_form.radioToxfxMaleFemale[1].value);
				}
				
				var certificates = number_of_drugs * (number_of_doses + 1) * number_of_treatment_durations * number_of_animals * genderMultiplier;
				if (certificates>=200) {
					discount = 0.25;
				}
				else if (certificates >= 150) {
					discount = 0.20;
				}
				else if (certificates >= 100) {
					discount = 0.15;
				}
				else if (certificates >= 50) {
					discount = 0.10;
				}
				else {
					discount = 0;
				}


				quantity = certificates;
				category = "ICX-CERT 01";
				description = "ToxFX Analysis Certificate for One Microarray, Expires after 1 Year";
				unit_price = PRICE_CERT;
				line_total += quantity * unit_price * (1-discount);				
				price += line_total;

			} // end - else if (value=="toxfx")

			document.quote_form.quantity.value = quantity;
			document.quote_form.category.value = category;
			document.quote_form.description.value = description;				
			document.quote_form.unit_price.value = unit_price;
			document.quote_form.discount.value = discount;
			document.quote_form.line_total.value = line_total;

			document.quote_form.quote_terms.value = quote_terms;

			updatePrice(price);
			
		}
	}

}

function verify_required_input() {
	var isVerified = true;
	var fullname = document.quote_form.fullname.value;
	var institution = document.quote_form.institution.value;
	var emailaddress = document.quote_form.emailaddress.value;
	var confirmemailaddress = document.quote_form.confirmemailaddress.value;

	if (fullname == null || fullname == "") {
		return badInput();
	}
	else if (institution == null || institution == "") {
		return badInput();
	}
	else if (emailaddress == null || emailaddress == "") {
		return badInput();
	}

	if (emailaddress != confirmemailaddress) {
		alert("The email addresses entered do not match. Please check the information, so that we can send you a product quote.");
		return false;
	}

	if (!verifyProducts()) {
		alert("Please select a Product Offering, so that we can send you a product quote.");
		return false;
	}

	if (!verifyDrugmatrixOnline()) {
		alert("Please check the number of Drugmatrix Named-User Licenses.");
		return false;
	}
	if (!verifyToxFX()) {
		alert("Please check your entries for the ToxFX Analysis Suite Software, so that we can send you a product quote.");
		return false;
	}
	if (!verifyToxicogenomics()) {
		alert("Please check your entries for the Toxicogenomics Services, so that we can send you a product quote.");
		return false;
	}
	if (!verifyToxicogenomics_ToxFX()) {
		alert("Please check your entries for the Toxicogenomics ToxFX Report, so that we can send you a product quote.");
		return false;
	}
	if (!verifyToxicogenomics_Custom()) {
		alert("We apologize but quotes for custom reports are not available online. Please contact tox_sales@entelos.com.");
		return false;
	}


	return isVerified;
}

function badInput() {
	alert("Please check that you have entered the necessary contact information, so that we can send you a product quote.");
	return false;
}

function isValidInteger(quantity) {
	var isVerified = true;
	if (quantity==null || quantity=="" || parseInt(quantity)<1) {
		isVerified = false;
	}
	return isVerified;
}

function verifyToxicogenomics_Custom() {
	var isVerified = true;
	var radio_toxicogenomics = document.getElementById("radio_toxicogenomics");
	var radio_custom_report = document.getElementById("radio_custom_report");
	if (radio_toxicogenomics.checked && radio_custom_report.checked) {
		// no verification allowed
		isVerified = false;
	}

	return isVerified;
}

function verifyToxicogenomics_ToxFX() {
	var isVerified = true;
	var radio_toxicogenomics = document.getElementById("radio_toxicogenomics");
	var radio_toxfx_report = document.getElementById("radio_toxfx_report");
	if (radio_toxicogenomics.checked && radio_toxfx_report.checked) {
		// 1 of 3: verify number fields
		var arrayValues = new Array();
		arrayValues[0] = document.quote_form.report_textToxfxDrugs.value;
		arrayValues[1] = document.quote_form.report_textToxfxDoses.value;
		arrayValues[2] = document.quote_form.report_textToxfxDurations.value;
		arrayValues[3] = document.quote_form.report_textToxfxAnimals.value;
		for (var i=0; i < arrayValues.length; i++) {
			var quantity=arrayValues[i];
			if (!isValidInteger(quantity)) {
				isVerified = false;
			}
		}
		if (!isVerified) {
			return false;
		}
		
		// 2 of 3: verify type of microarray
		isVerified = false;
		var radioArrays = document.quote_form.radioArrays;
		for (var i = 0; i < radioArrays.length; i++) {
			if (radioArrays[i].checked) {
				isVerified = true;
			}
		}
		if (!isVerified) {
			return false;
		}


		// 3 of 3: verify data preparation service
		isVerified = false;
		//var checkboxServices = document.quote_form.checkboxServices;
		var arrayValues = new Array();
		arrayValues[0] = document.quote_form.radio_rna_isolation;
		arrayValues[1] = document.quote_form.radio_target_prep;
		arrayValues[2] = document.quote_form.radio_customer_supplied;
		for (var i = 0; i < arrayValues.length; i++) {
			if (arrayValues[i].checked) {
				isVerified = true;
			}
		}
		if (!isVerified) {
			return false;
		}
	}

	return isVerified;
}

function verifyToxicogenomics() {
	var isVerified = true;
	var radio_toxicogenomics = document.getElementById("radio_toxicogenomics");
	if (radio_toxicogenomics.checked) {
		isVerified = false;
		var radioServices = document.quote_form.radioServices;
		for (var i = 0; i < radioServices.length; i++) {
			if (radioServices[i].checked) {
				isVerified = true;
			}
		}
	}
	return isVerified;
}

function verifyToxFX() {
	var isVerified = true;
	var radio_toxfx= document.getElementById("radio_toxfx");

	if (radio_toxfx.checked) {
		var arrayValues = new Array();
		arrayValues[0] = document.quote_form.textToxfxDrugs.value;
		arrayValues[1] = document.quote_form.textToxfxDoses.value;
		arrayValues[2] = document.quote_form.textToxfxDurations.value;
		arrayValues[3] = document.quote_form.textToxfxAnimals.value;
		for (var i=0; i < arrayValues.length; i++) {
			var quantity=arrayValues[i];
			if (!isValidInteger(quantity)) {
				isVerified = false;
			}
		}
	}
	return isVerified;
}

function verifyDrugmatrixOnline() {
	var isVerified = true;
	var radio_drugmatrix = document.getElementById("radio_drugmatrix");
	if (radio_drugmatrix.checked) {
		var quantity = document.quote_form.textDrugmatrixLicenses.value;
		if (!isValidInteger(quantity)) {
			isVerified = false;
		}
	}
	return isVerified;
}

function verifyProducts() {
	var isVerified = false;

	var radioToxProducts = document.quote_form.radioToxProducts;
	for (var i = 0; i < radioToxProducts.length; i++) {
		if (radioToxProducts[i].checked) {
			isVerified = true;
		}

	}
	return isVerified;
}

	  function makeWin2(url, p_Width, p_Height) {
	
	      agent = navigator.userAgent;
	      windowName = "Sitelet";
	      params  = "";
	      params += "toolbar=0,";
	      params += "location=0,";
	      params += "directories=0,";
	      params += "status=0,";
	      params += "menubar=0,";
	      params += "scrollbars=1,";
	      params += "resizable=1,";
	      params += "top=92,";
	      params += "left=0,";
	      if (p_Width == "null") {
			params += "width=500,";
	        params += "height=350";
	      }
		  else {
			params += "width=" + p_Width+ ",";
	        params += "height=" + p_Height;
	      }

		  // close the window to vary the window size
	   	  if (typeof(win) == "object" && !win.closed){
            win.close();
	      }
	   
	      win = window.open(url, windowName , params);

	      if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) {
	          win = window.open(url, windowName , params);
	      }
	
	      if (!win.opener) {
	          win.opener = window;
	      }
	
		  // bring the window to the front
		  win.focus();	
	  }

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
