//  ==============================================================================
//
//  Name:       Snapshot Archive Price Estimator Logic Modules
//
//  Author:     Mike Porritt,  Emgeep Pty Ltd
//  Copyright:  Mike Porritt,  Emgeep Pty Ltd
//
//  Languages:  HTML and Javascript 
//  
//  Date:       17th April 2002
//
//  Version:    1.0
//
//  ===============================================================================

        var oneScan     = 40;
	var baseCD 	= 6;
	var baseCDdup	= 4 ;
	var compCD 	= 8;
	var compCDdup	= 4 ;
	var dvdCD 	= 7;
	var dvdCDdup	= 4 ;
	var vcdCD 	= 7;
	var vcdCDdup	= 4 ;
	var vhsTP 	= 9;
	var vhsTPdup	= 5 ;
    	ReturnChar      = "\x0D";

//---------------------------------------------------------------
//---------  Launch Field Help pop up boxes
//---------------------------------------------------------------
function helpbox(helpid)
	{
         HelpBox  = window.open("./help/"+helpid+".htm","Helpbox","width=400,height=300,left=100,top=100,scrollbars=yes");
	}

//---------------------------------------------------------------
//---------  Form Refresh
//---------------------------------------------------------------
function FormRefresh()
	{

 	  g_CDDisks = ValidNum("The Number of Computer Slideshows Disks",window.document.CalcForm.cddisks);

	  NewCount();
	  ImageCalc();
	  CDCalc();
	  BaseCalc();
	  if (window.document.CalcForm.titleCheck.checked)   {TitleCalc();}
	  if (window.document.CalcForm.detailsCheck.checked) {DetailsCalc();}
	  DVDCalc();
	  VCDCalc();
	  VHSCalc();
	  postCalc();
	  DoCalc();
	  window.document.CalcForm.gst.checked=true;
	  calcGST();
	  
       	}
//---------------------------------------------------------------
//---------  Form Reset
//---------------------------------------------------------------
function FormReset()
	{
	  window.document.CalcForm.reset();
	  ResetAll();
       	}

//---------------------------------------------------------------
//---------  Reset All Fields
//---------------------------------------------------------------
function ResetAll()
	{
	    g_imageCount   = 0 ;
	    g_scanCost     = 0 ;
	    g_baseCost     = 0 ;
	    g_BaseDisks    = 0 ;
            g_titleCost    = 0 ;
	    g_CDCost       = 0 ;
	    g_CDDisks      = 0 ;
	    g_detailsCost  = 0 ;
	    g_VCDCost      = 0 ;
	    g_VCDDisks     = 0 ;
	    g_DVDCost      = 0 ;
            g_DVDDisks     = 0 ;
	    g_VHSCost      = 0 ;
	    g_VHSTapes     = 0 ;
	    g_PostCost     = 0 ;
            g_TotalCost    = 0 ;
	}

//---------------------------------------------------------------
//---------  Clear Current Calculations
//---------------------------------------------------------------
function ClearCalc()
	{
	   window.document.CalcForm.totalcost.value = "";
	   g_TotalCost =0;
           window.document.CalcForm.gstcost.value = ""; 
	   window.document.CalcForm.plusgst.value = "";
	   window.document.CalcForm.gst.checked = false ;
	}

//------------------------------------------------
//---------  Process a Change to the Image Count
//------------------------------------------------	
function NewCount()
   {	ClearCalc();

    var imageCount = ValidNum("The Number of Images To Be Scanned",window.document.CalcForm.imagecount);

	switch (imageCount)
		{
			case "EMPTY"  : 
				window.document.CalcForm.imagecount.value = "0" ;
				g_imageCount = 0 ;
				break ;
			case "NaN"    :
				break ;
			default :
	 			window.document.CalcForm.imagecount.value = " "+imageCount ;
				g_imageCount = imageCount ;
				ImageCalc() ;
				if (window.document.CalcForm.titleCheck.checked){TitleCalc()} ;
				if (window.document.CalcForm.detailsCheck.checked){DetailsCalc()} ;
		}
   }
//------------------------------------------------
//---------  Calculate Cost of Image Scanning
//------------------------------------------------	
function ImageCalc()
   {
	ClearCalc();

    	if (window.document.CalcForm.scanCheck.checked)
	{
		g_scanCost = oneScan * g_imageCount / 100 ;
		if (g_scanCost < 10) {g_scanCost =10;} 
        	window.document.CalcForm.scancost.value = DisplayFormat(g_scanCost);
	}
	else
	{
		window.document.CalcForm.scancost.value = "";
		g_scanCost = 0 ;
	}
		
   }
//------------------------------------------------
//---------  Calculate Cost of Basic Image Disks
//------------------------------------------------	
function BaseCalc()
   {
	ClearCalc();

        g_BaseDisks = ValidNum("The Number of Basic Disks Required",window.document.CalcForm.basedisks);

	switch (g_BaseDisks)
		{
			case "EMPTY"  : 
				window.document.CalcForm.basecost.value = "" ;
				g_baseCost = 0;
				g_BaseDisks = 0;
				break ;
			case "NaN"    :
				window.document.CalcForm.basecost.value = "#####" ;
				break ;
			default :
				if (g_BaseDisks > 0)
				   {
					window.document.CalcForm.basedisks.value = " "+g_BaseDisks ;
		   		     	g_baseCost = baseCD + (baseCDdup * (g_BaseDisks-1)) ;
					window.document.CalcForm.basecost.value = DisplayFormat(g_baseCost);
				   }
				else 
				   {
					window.document.CalcForm.basecost.value = "" ;
					g_baseCost = 0;

				   }
		}
	postCalc();
	
   }
//------------------------------------------------
//---------  Calculate Title Processing Costs
//------------------------------------------------	
function TitleCalc()
   {
	ClearCalc();

	if (window.document.CalcForm.titleCheck.checked)
	   { 
		if (window.document.CalcForm.TitlesRadio[0].checked)
			{g_titleCost = 6 + 30 * (g_imageCount/5)/60;}
		else
			{g_titleCost = 6 + 4 * (g_imageCount/5)/60;}
		window.document.CalcForm.titlecost.value = DisplayFormat(g_titleCost) ;
	   }
	else
	   {
		g_titleCost = 0 ;
		window.document.CalcForm.titlecost.value = "" ;
 		if (window.document.CalcForm.detailsCheck.checked)
		   {
      	 		window.document.CalcForm.detailsCheck.checked = false ;
			DetailsCalc();
		   }
	   }
   }
//------------------------------------------------
//---------  Calculate Cost of CD Slideshows
//------------------------------------------------	
function CDCalc()
   {
	ClearCalc();

        g_CDDisks = ValidNum("The Number of Computer Slideshows Disks",window.document.CalcForm.cddisks);

	switch (g_CDDisks)
		{
			case "EMPTY"  : 
				window.document.CalcForm.cdcost.value = "" ;
				g_CDCost = 0 ;
				g_CDDisks = 0 ;
 				if (window.document.CalcForm.detailsCheck.checked)
		   		  {
      	 				window.document.CalcForm.detailsCheck.checked = false ;
					DetailsCalc();
		   		  }
 				break ;
			case "NaN"    :
				window.document.CalcForm.cdcost.value = "#####" ;
				g_CDDisks = 0 ;
				break ;
			default :
				if (g_CDDisks > 0)
				   {
					window.document.CalcForm.cddisks.value = " "+g_CDDisks ;
	   		     		g_CDCost = compCD + (compCDdup * (g_CDDisks-1)) ;
					window.document.CalcForm.cdcost.value = DisplayFormat(g_CDCost);
				   }
				else
				   {
	   		     		g_CDCost = 0 ;
					window.document.CalcForm.cdcost.value = "" ;
 					if (window.document.CalcForm.detailsCheck.checked)
		   		  	   {
      	 					window.document.CalcForm.detailsCheck.checked = false ;
						DetailsCalc();
		   		  	   }
				   }
		}
	postCalc();	
   }

//------------------------------------------------
//---------  Calculate Details Processing Costs
//------------------------------------------------	
function DetailsCalc()
   {
	ClearCalc();

	if ((!window.document.CalcForm.titleCheck.checked) || (g_CDDisks == 0))
	   {
                alert("Details can only be used if at least one Computer Slideshow is ordered. \n\nTitles must also be selected if you wish to use Details. \n\nPlease select click on the field name for more HELP on using the DETAILS service.");
		window.document.CalcForm.detailsCheck.checked = false ;
	   }

	if (window.document.CalcForm.detailsCheck.checked)
	   { 
		if (window.document.CalcForm.detailsRadio[0].checked)
			{g_detailsCost = 7 + 30 * (g_imageCount/3)/60;}
		else
			{g_detailsCost = 7 + 4 * (g_imageCount/3)/60;}
		window.document.CalcForm.detailscost.value = DisplayFormat(g_detailsCost) ;
	   }
	else
	   {
		g_detailsCost = 0 ;
		window.document.CalcForm.detailscost.value = "" ;
	   }
   }
//------------------------------------------------
//---------  Calculate Cost of DVD Slideshows
//------------------------------------------------	
function DVDCalc()
   {
	ClearCalc();

        g_DVDDisks = ValidNum("The Number of Mini DVD Slideshows Disks",window.document.CalcForm.dvddisks);

	switch (g_DVDDisks)
		{
			case "EMPTY"  : 
				window.document.CalcForm.dvdcost.value = "" ;
				g_DVDCost = 0 ;
				g_DVDDisks = 0 ;
				break ;
			case "NaN"    :
				window.document.CalcForm.dvdcost.value = "#####" ;
				break ;
			default :
				if (g_DVDDisks > 0)
				   {
					window.document.CalcForm.dvddisks.value = " "+g_DVDDisks ;
	   		     		g_DVDCost = dvdCD + (dvdCDdup * (g_DVDDisks-1)) ;
					window.document.CalcForm.dvdcost.value = DisplayFormat(g_DVDCost);
				   }
				else
				   {
	   		     		g_DVDCost = 0 ;
					window.document.CalcForm.dvdcost.value = "" ;
 				   }
		}
	postCalc();	
   }
//------------------------------------------------
//---------  Calculate Cost of VCD Slideshows
//------------------------------------------------	
function VCDCalc()
   {
	ClearCalc();

        g_VCDDisks = ValidNum("The Number of VCD Slideshows Disks",window.document.CalcForm.vcddisks);

	switch (g_VCDDisks)
		{
			case "EMPTY"  : 
				window.document.CalcForm.vcdcost.value = "" ;
				g_VCDCost = 0 ;
				g_VCDDisks = 0 ;
				break ;
			case "NaN"    :
				window.document.CalcForm.vcdcost.value = "#####" ;
				break ;
			default :
				if (g_VCDDisks > 0)
				   {
					window.document.CalcForm.vcddisks.value = " "+g_VCDDisks ;
	   		     		g_VCDCost = vcdCD + (vcdCDdup * (g_VCDDisks-1)) ;
					window.document.CalcForm.vcdcost.value = DisplayFormat(g_VCDCost);
				   }
				else
				   {
	   		     		g_VCDCost = 0 ;
					window.document.CalcForm.vcdcost.value = "" ;
 				   }
		}

	postCalc();	
   }
//------------------------------------------------
//---------  Calculate Cost of VHS Slideshows
//------------------------------------------------	
function VHSCalc()
   {
	ClearCalc();

        g_VHSTapes = ValidNum("The Number of VHS Tape Slideshows Disks",window.document.CalcForm.vhstapes);

	switch (g_VHSTapes)
		{
			case "EMPTY"  : 
				window.document.CalcForm.vhscost.value = "" ;
				g_VHSCost = 0 ;
				g_VHSTapes = 0 ;
				break ;
			case "NaN"    :
				window.document.CalcForm.vhscost.value = "#####" ;
				break ;
			default :
				if (g_VHSTapes > 0)
				   {
					window.document.CalcForm.vhstapes.value = " "+g_VHSTapes ;
	   		     		g_VHSCost = vhsTP + (vhsTPdup * (g_VHSTapes-1)) ;
					window.document.CalcForm.vhscost.value = DisplayFormat(g_VHSCost);
				   }
				else
				   {
	   		     		g_VHSCost = 0 ;
					window.document.CalcForm.vhscost.value = "" ;
 				   }
		}
	postCalc();	
   }
//------------------------------------------------
//---------  Estimate the Postage Cost
//------------------------------------------------	
function postCalc()
   {
	ClearCalc();

	var handling = 7;
        var packaging = 1.25;
        var localrate = 1.45;
        var interstate = 1.45;
        var overseas = 1.80;
        
	if (window.document.CalcForm.posttype[0].checked)
	   {
     		g_PostCost = 0 ;
		window.document.CalcForm.postcost.value = "" ;
	   }
	else
	   {
                itemCount = g_BaseDisks + g_CDDisks + g_VCDDisks + g_DVDDisks + g_VHSTapes  ;
		
 		if (window.document.CalcForm.posttype[1].checked)
		   {	g_PostCost = handling  + (itemCount * packaging) + localrate + ((itemCount-1)*localrate*0.5) ;  }
		else 
		   {
			if (window.document.CalcForm.posttype[2].checked)
		 	   {	g_PostCost = handling  + (itemCount * packaging) + interstate + ((itemCount-1)*interstate*0.5) ;	}
			else 
		   	   {
				if (window.document.CalcForm.posttype[3].checked)
		   		   {	g_PostCost = handling  + (itemCount * packaging) + overseas + ((itemCount-1)*overseas*0.5) ;}
                   	   }
                   } 

		window.document.CalcForm.postcost.value = DisplayFormat(g_PostCost);
	   }
   }

//------------------------------------------------
//---------  Calculate The Total Cost
//------------------------------------------------	
function DoCalc()
   {
	ClearCalc();

	g_TotalCost = g_scanCost + g_baseCost + g_titleCost + g_CDCost + g_detailsCost + g_VCDCost + g_DVDCost + g_VHSCost + g_PostCost;
	
	CostTotal = DisplayFormat(g_TotalCost);
	
	window.document.CalcForm.totalcost.value = CostTotal ;

   }

//------------------------------------------------
//---------  Calculate Cost GST
//------------------------------------------------	
function calcGST()
   {
    	if (window.document.CalcForm.gst.checked)
	{
		gstAmount = g_TotalCost * 10 / 100 ;
		window.document.CalcForm.gstcost.value = DisplayFormat(gstAmount);
		window.document.CalcForm.plusgst.value = DisplayFormat(gstAmount+g_TotalCost);
	}
	else
	{
        	window.document.CalcForm.gstcost.value = ""; 
		window.document.CalcForm.plusgst.value = "";
	}
		
   }

//------------------------------------------------
//---------  Validate Input is all Numbers
//------------------------------------------------
function ValidNum(fieldName,fieldObj)
{
    var fieldVal = fieldObj.value;
        
        if (fieldVal == "          ".substr(0,fieldVal.length))
	{
		return "EMPTY";
        }
             
	if (isNaN(fieldVal))
	{
        	alert("Please Enter A Valid Number For " + fieldName);
                window.document.CalcForm.totalcost.value = "ERROR";
                fieldObj.focus();
                return "NaN" ;
	}
	else
	{
		return parseInt(fieldObj.value);
	}
}  
//------------------------------------------------
//---------  Format Money Values For Display
//------------------------------------------------
function DisplayFormat(inMoney)
{
	var cents = ""
	var part  = ("$"+inMoney).split(".") ;
        if (part[1] == undefined)
	{
		cents="00";
	}
	else 
	{
	        var cents = (part[1]+"00").substr(0,2);
	}
        return part[0]+"."+cents;
}

//------------------------------------------------
//---------  Produce Order Form
//------------------------------------------------
function OrderForm()
{
	order = window.open("","order","menubar=yes,scrollbars=yes");
	
	AddLine("<html><head>");
	AddLine("<title>Snapshot-Archive Order Estimate</title>");
        AddLine("<script language=\"JavaScript\">");
		AddLine("window.moveTo(0,0);");
        	AddLine("window.resizeTo(screen.width,screen.height);");
	AddLine("</script>");
	AddLine("</head>");

	AddLine('<body><div STYLE="position:absolute; LEFT:50; width:600">');
	AddLine('<h1><font color="#800000">Snapshot-Archive Order Estimate</font></h1>');
	AddLine("Please check your details and the estimate summary shown below.<br>")
	AddLine("If you wish to place an order please print two copies of this page.  One copy for inclusion with your order and one copy for your own records.");
	AddLine("<br><br><b>Close this window to return to price estimator.</b><br>");

	AddLine("<h2>Customer Details</h2>");

	AddLine('<hr><table border="0" width="600">');
		AddCRow("Estimate ID.",window.document.CalcForm.EstimateID.value);
		AddCRow("Name",window.document.CalcForm.CustomerName.value);
		AddCRow("*","");
		AddCRow("Address",window.document.CalcForm.Address1.value);
		if (window.document.CalcForm.Address2.value != "") {AddCRow("",window.document.CalcForm.Address2.value);}
		if (window.document.CalcForm.Address3.value != "") {AddCRow("",window.document.CalcForm.Address3.value);}
  		AddCRow("State",window.document.CalcForm.State.value);
		AddCRow("Post Code",window.document.CalcForm.PostCode.value);
		AddCRow("*","");
		AddCRow("Email",window.document.CalcForm.CustomerEmail.value);
		AddCRow("Phone Numbers",window.document.CalcForm.PhoneNumbers.value);
		AddCRow("Marketing","Contact By Snapshot-Archive is "+(window.document.CalcForm.contact.checked?"Allowed":"Not Permitted"));

	AddLine('</table><hr>');

	cd=new Date();
	ViewDate = cd.getDate()+"-"+cd.getMonth()+"-"+cd.getFullYear();
	AddLine("<h2>Order Details - As At "+ViewDate+"</h2>");

	AddLine('<table border="1" width="600">');
		AddORow("Description","<b>Quantity</b>","<b>Price</b>");

		AddORow("Total Number of Images",window.document.CalcForm.imagecount.value,"*");
		if (g_scanCost  > 0) {AddORow("Perform Basic Scanning",(window.document.CalcForm.scanCheck.checked?"Yes":"Not Required"),window.document.CalcForm.scancost.value);}
		if (g_baseCost  > 0) {AddORow("Basic Scan Disks",window.document.CalcForm.basedisks.value,window.document.CalcForm.basecost.value);}
	    	if (g_titleCost > 0) {AddORow("Picture Titles, Supplied To Us "+(window.document.CalcForm.TitlesRadio[0].checked?"On Paper":"Electronically"),"*",window.document.CalcForm.titlecost.value);}
		if (g_CDCost > 0) {AddORow("Computer Slideshows Disks",window.document.CalcForm.cddisks.value,window.document.CalcForm.cdcost.value);}
	    	if (g_detailsCost > 0) {AddORow("Picture Details, Supplied To Us "+(window.document.CalcForm.TitlesRadio[0].checked?"On Paper":"Electronically"),"*",window.document.CalcForm.detailscost.value);}
		if (g_VCDCost > 0) {AddORow("VCD Slideshow Disks",window.document.CalcForm.vcddisks.value,window.document.CalcForm.vcdcost.value);}
		if (g_DVDCost > 0) {AddORow("DVD Slideshow Disks",window.document.CalcForm.dvddisks.value,window.document.CalcForm.dvdcost.value);}
		if (g_VHSCost > 0) {AddORow("VHS Slideshow Tapes",window.document.CalcForm.vhstapes.value,window.document.CalcForm.vhscost.value);}
		if (g_PostCost > 0) {AddORow("Postage Costs For "+(window.document.CalcForm.posttype[1].checked?"Local":"Interstate")+" Mail","*",window.document.CalcForm.postcost.value);}
            
		AddORow("*","","");

		AddORow("Total Cost Of This Order ","*",window.document.CalcForm.totalcost.value);
		AddORow("GST on This Order ","*",window.document.CalcForm.gstcost.value);
		AddORow("Total Cost Including GST ","*",window.document.CalcForm.plusgst.value);


	AddLine('</table>');

	AddLine("</div></body></html>");
	
	order.document.close();

	order.focus();
        window.document.CalcForm.action = "http://"+cserver+"/cgi-bin/CalcForm.pl";
	window.document.CalcForm.onSubmit = "";
	window.document.CalcForm.submit();
	window.document.CalcForm.onSubmit="return false;"
	
} 
//------------------------------------------------
//---------  Add Order Line
//------------------------------------------------
function AddLine(lineText)
{
	order.document.write(lineText+ReturnChar)
}  

//------------------------------------------------
//---------  Add Customer Details Table Row
//------------------------------------------------
function AddCRow(FieldId,FieldVal)
{	
	if (FieldId == "*") 
	   {AddLine('<tr><td width="150">&nbsp;</td><td></td></tr>');}
	else
	   if (FieldId != "" ) 
		{AddLine('<tr><td width="150"><b>'+FieldId+':</b></td><td>'+FieldVal+'</td></tr>');}
	   else 
		{AddLine('<tr><td width="150"><b>'+FieldId+'</b></td><td>'+FieldVal+'</td></tr>');}
} 

//------------------------------------------------
//---------  Add Order Details Table Row
//------------------------------------------------
function AddORow(FieldId,Quantity,Amount)
{
	if (Quantity == "*") {Quantity = "&nbsp;"}
	if (Amount == "*") {Amount = "&nbsp;"}
	if (FieldId == "*") 
	   {AddLine('<tr><td width="350">&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>');}
	else
	   {AddLine('<tr><td width="350"><b>'+FieldId+':</b></td><td align="right">'+Quantity+'</td><td align="right">'+Amount+'</td></tr>');}
}    
   

