<!--Copyright Info-->
<!--The contents of this file are copyrighted by MolecularSoft Technology Inc. -->
<!--All Rights Reserved.  You are not permitted to copy or use this script in any circumstances -->

var g_WhoBuy = "High School Student";
function OnFormLoad(form)
{
  	var dt = "" + new Date().getTime();
	form.refno.value = "";
	for (i=dt.length-1; i>dt.length-9; i--)
	{
	    form.refno.value += dt.substring(i, i+1);
	}
	form.refno.value += '_';
	var num = "" + Math.random()*1000000;
	for (i=0; i<4; i++)
	{
	   form.refno.value += num.substring(i, i+1);
	}   

    CalculateTotalChange(form);
}

function Validate_Fields(form, nType)
{
    CalculateTotalChange(form);

    var fSubTotal = GetFloatNumber(form.subtotal.value);
	if (fSubTotal<0.01)
	{
	   alert("You have to select at aleast one of  titles.");	   
	   form.Chemical_Bond.focus();
	   return false;
	}   

	//7. check the order reference no.
	var strRefNo = form.refno.value;
/*	
	if (strRefNo.length!=13)
	{
	   alert("You input a invalid Order No.");
	   form.refno.focus();
	   form.refno.select();	
	   return false;	
	}
*/
	FillInfomation(form);

	if (nType==1)
	{
	   form.acceptchecks.value="0";
	   form.acceptcards.value="1";
	   form.visaimage.value="1";
	   form.mcimage.value="1";
	   form.ameximage.value="1";	
	   form.discimage.value="1";	
	}
	else if (nType==2)
	{
	   form.acceptcards.value="0";
	   form.visaimage.value="0";
	   form.mcimage.value="0";
	   form.ameximage.value="0";	
	   form.discimage.value="0";	
	   form.acceptchecks.value="1";
    }

	return true;
}

function FillInfomation(form)
{
    for (var i=0;i<form.elements.length;i++)
    {
        var e = form.elements[i];
        if (e.name == '1-cost')
	    {
           var strCharge = '' + GetFloatNumber(form.totalchange.value);
	       var nCopies= GetFloatNumber(form.quantity.value);
	       if (nCopies>1)
           {
	         strCharge = AdjustmentDecimal(''+(GetFloatNumber(form.totalchange.value)/nCopies+0.005));  
	       }

		   var nPos = strCharge.indexOf('.', 0);
		   if (nPos<0)
			  strCharge += ".00";
		   else  if (strCharge.length-nPos<2)
			  strCharge += "00";
		   else  if (strCharge.length-nPos<3)
			  strCharge += "0";
		   else
			 ;
			 
		   e.value = strCharge;    
		}

		if (e.name == '1-qty')
		{
		   e.value = form.quantity.value;
		}
/*		
		if (e.name == '1-desc')
		{
		   e.value = "SoftChemistry 4.20 (" +  form.selectterms.options[form.selectterms.options].text + " term)";
		}
*/		
	}

	var strModules = "";
	strModules += "Periodic_Table";
	strModules += ", ";
	strModules += "Chemical_Bond";
	strModules += ", ";
	strModules += "Gas_Laws";
	strModules += ", ";
	strModules += "Basic_Math";
	strModules += ", ";
	strModules += "Equations";
	strModules += ", ";
	strModules += "Solutions";
	strModules += ", ";
	strModules += "Stoichiometry";
	strModules += ", ";
	strModules += "Acid_and_Base";
	strModules += ", ";
	strModules += "Thermochemistry";

	form.modules.value = strModules;
	form.quantity1.value = form.quantity.value;
	form.unitprice1.value = form.unitprice.value;
	form.subtotal1.value = form.subtotal.value;
	form.tax1.value = form.tax.value;
	form.shiphandle1.value = form.shiphandle.value;
	
    form.email_text.value = "Thank you for online ordering (" + form.refno.value + ").";       
}

function OnCheckProduct(form)
{
	CalculateTotalChange(form);	
	var fSubTotal = GetFloatNumber(form.subtotal.value);
	if (fSubTotal<0.01)
	{
	   alert("You have to select at least one of non-free products.");
	   
	   form.Chemical_Bond.focus();
	}   
}

function OnReferPersonChange(form)
{
	OnSelectWhoBuy("");
}

function IsAuthorizedPerson(refPerson)
{
   if (refPerson.value.length==6)
   { 
       var strPerson = refPerson.value;	   
       for (i=0; i<3; i++)
       {
	       var nonDigit = parseInt(strPerson.substring(i,i+1));
	       var strNaN = "" + nonDigit;
	       if (strNaN!="NaN")
              return (false);
       }

       for (i=3; i<6; i++)
       {
	       var nDigit = parseInt(strPerson.substring(i,i+1));
	       var strDigit = "" + nDigit;
	       if (strDigit=="NaN")
              return (false);
	   }

	   return (true);
    }
	
    return (false);
}

function Check_FieldEmpty(objField)
{
   if (objField.value.length == 0) 
      return(true);
   else
      return(false);
}

function IsValidateString(strIn, strValidate)  
{
   if (strIn.length==0)
      return (false);
	
   strTemp = strValidate;
   for (i=0; i<strIn.length; i++)
   {
      if (strTemp.indexOf(strIn.substring(i, i+1), 0)==-1) 
         return (false);
   }

   return (true);
}

function GetFloatNumber(strFloat)
{
   if (strFloat.length == 0) 
       return 0;
   
   var strTemp;
   if (strFloat.substring(0, 1)=='$')
      return parseFloat(strFloat.substring(1));
   else
      return parseFloat(strFloat);
}

function GetPercentage(nCount)
{
   if (nCount==1)
      return 1.00;
   else if (nCount==2)
      return 0.90;
   else if (nCount==3)
      return 0.80;
   else// if (nCount==3)
      return 0.70;
}

function CalculateSubTotal(form)
{
/*	
	var nGroupCount1 = 0;
	var fGroupTotal1 = 0; 
	if (form.Periodic_Table.checked)
    {
	   nGroupCount1++;
	   fGroupTotal1 += 29.0;
	}	
	if (form.Chemical_Bond.checked)
    {
	   nGroupCount1++;
	   fGroupTotal1 += 29.0;
	}	
	if (form.Gas_Laws.checked)
	{
	   nGroupCount1++;
	   fGroupTotal1 += 29.0;
	}
	if (nGroupCount1==3)
	{
	   fGroupTotal1 = 69.0;
	}

	var nGroupCount2 = 0;
	var fGroupTotal2 = 0; 
	if (form.Basic_Math.checked)
    {
	   nGroupCount2++;
	   fGroupTotal2 += 29.0;
	}	
	if (form.Equations.checked)
	{	
	   nGroupCount2++;
	   fGroupTotal2 += 29.0;
	}
	if (form.Solutions.checked)
	{
	   nGroupCount2++;
	   fGroupTotal2 += 29.0;
	}
	if (nGroupCount2==3)
	{
	   fGroupTotal2 = 69.0;
	}

	var nGroupCount3 = 0;
	var fGroupTotal3 = 0; 
	if (form.Stoichiometry.checked)	
	{
	   nGroupCount3++;
	   fGroupTotal3 += 29.0;
	}
	if (form.Acid_and_Base.checked)
	{
	   nGroupCount3++;
	   fGroupTotal3 += 29.0;
	}
	if (form.Thermochemistry.checked)
	{
	   nGroupCount3++;
	   fGroupTotal3 += 29.0;
	}
	if (nGroupCount3==3)
	{
	   fGroupTotal3 = 69.0;
	}

	var fSubTotal = fGroupTotal1 + fGroupTotal2 + fGroupTotal3;	
	if (nGroupCount1>0 && nGroupCount2>0 && nGroupCount3>0)
	{
	   if (fSubTotal>99.0)
	   {
	      fSubTotal = 99.0;
	   }
	}
	else if (nGroupCount1==0||nGroupCount2==0||nGroupCount3==0)
	{
	  if (fSubTotal>89.0)	 
	     fSubTotal = 89.0;
	}	
*/
//	alert("Term change = " + form.selectterms.options[form.selectterms.selectedIndex].value);
    var fSubTotal = getTermTotal(form.selectterms);
	form.subtotal.value = AdjustmentDecimal('$' + fSubTotal);	   	  		

	return fSubTotal;
}

function CalculateTax(form)
{
	var nTax = 0.;	
	if (nTax>0.)
	   form.tax.value = AdjustmentDecimal('$' + nTax);	   	  		
	else
	   form.tax.value = "0";	   	  		
	
	return nTax;
}

function CalculateTotalChange(form)
{
   	CalculateSubTotal(form);
	CalculateTax(form);

	var nSubTotal = GetFloatNumber(form.subtotal.value);
	var nTax	    = GetFloatNumber(form.tax.value);
	var nShipping = GetFloatNumber(form.shiphandle.value);
	var nTotalCharge = nSubTotal + nTax + nShipping + 0.005;

	var nCopies= GetFloatNumber(form.quantity.value);	
    if (nCopies>=5)
	   nTotalCharge = (nTotalCharge*.75+0.005);	

	form.totalchange.value = AdjustmentDecimal('$' + nTotalCharge);	   	  		
}

function CalculateTotalChangeVAR(form)
{
  var nCopies= GetFloatNumber(form.quantity.value);	
  CalculateTotalChange(form);
}

function CalculateTotalChangeCoupon(form)
{
	CalculateSubTotal(form);
	CalculateTax(form);

	var nSubTotal = GetFloatNumber(form.subtotal.value);
	var nTax      = GetFloatNumber(form.tax.value);
	var nShipping = GetFloatNumber(form.shiphandle.value);
	var nTotalChange = nSubTotal + nTax + nShipping + 0.005;
	form.totalchange.value = AdjustmentDecimal('$' + nTotalChange);	   	  		
}

function AdjustmentDecimal(strTemp)
{
	var nPos = strTemp.indexOf('.', 0);
	if (nPos>=0 && strTemp.length-nPos>2)
	   return strTemp.substring(0, nPos+3);
	else
	   return strTemp;
}

function OnSelectWhoBuy(objWho)
{
	g_WhoBuy = objWho.value;
}

function OnPaypalFormLoad(form)
{
	var dt = "" + new Date().getTime();
	form.invoice.value = "";
	for (i=dt.length-1; i>dt.length-9; i--)
	{
	 form.invoice.value += dt.substring(i, i+1);
	}
	form.invoice.value += '_';
	var num = "" + Math.random()*1000000;
	for (i=0; i<4; i++)
	{
	 form.invoice.value += num.substring(i, i+1);
	}
}

function prePostToPayPal(form)
{
	var oform = document.OrderForm;
	if (!Validate_Fields(oform, 3))
	   return false;
	
    form.item_number.value = oform.modules.value;	
	form.amount.value = oform.totalchange.value;

	var custom =  oform.refno.value;
	custom +='|'+oform.product.value;
	custom +='|'+oform.modules.value;
	custom +='|'+oform.version.value;
	custom +='|'+g_WhoBuy;
	custom +='|'+oform.referperson.value;
	custom +='|'+oform.quantity.value;
	custom +='|'+oform.unitprice.value;
	custom +='|'+oform.totalchange.value.substring(1);		
	custom +='|selectterms:'+oform.selectterms.value;		
	
	form.custom.value = custom;
//alert("Term=" + form.custom.value);

	return true;
}

function onCheckVolume(form, obj)
{
	if (obj.name=="volume_a")
	{
		form.Periodic_Table.checked = obj.checked; 
		form.Chemical_Bond.checked = obj.checked; 
		form.Gas_Laws.checked = obj.checked; 	
	}
	else if (obj.name=="volume_b")
	{
		form.Basic_Math.checked = obj.checked; 
		form.Equations.checked = obj.checked; 
		form.Solutions.checked = obj.checked; 		
	}
	else if (obj.name=="volume_c")
	{
		form.Stoichiometry.checked = obj.checked; 
		form.Acid_and_Base.checked = obj.checked; 
		form.Thermochemistry.checked = obj.checked; 		
	}
	else if (obj.name=="volume_abc")
	{
		form.Periodic_Table.checked = obj.checked; 
		form.Chemical_Bond.checked = obj.checked; 
		form.Gas_Laws.checked = obj.checked; 	
		form.Basic_Math.checked = obj.checked; 
		form.Equations.checked = obj.checked; 
		form.Solutions.checked = obj.checked; 		
		form.Stoichiometry.checked = obj.checked; 
		form.Acid_and_Base.checked = obj.checked; 
		form.Thermochemistry.checked = obj.checked; 	
		
		form.volume_a.checked = obj.checked;
		form.volume_b.checked = obj.checked;
		form.volume_c.checked = obj.checked;
	}

	CalculateTotalChange(form);
}

function setValueByElementId(sIdName, sValue)
{
   if (document.getElementById(sIdName)!=null)
   {
       if (document.all)
       {
         document.getElementById(sIdName).innerText = sValue;
       }
       else
       {
         document.getElementById(sIdName).textContent = sValue;
       }
   }
}

function OnTermChange(form)
{	
    var nTotalCharge = getTermTotal(form.selectterms);
	setValueByElementId("totaltermcharges", '$'+nTotalCharge);
	
	CalculateTotalChange(form);
}

function getTermTotal(termObject)
{
	var nCharges = 99.99;
    var nValue = termObject.options[termObject.selectedIndex].value;
	if (nValue==-7)
	   nCharges = 9.99;
	else if (nValue==1)
	   nCharges = 29.99;
	else if (nValue==3)
	   nCharges = 59.99;
	else if (nValue==6)
	   nCharges = 79.99;
	else if (nValue==12)
	   nCharges = 99.99;
	else if (nValue==24)
	   nCharges = 149.99;
	else //if (nValue==24)
	   nCharges = 199.99;	
//	alert(termObject.options[termObject.selectedIndex].text);
	
	return nCharges; 
}
