// Copyright (c)2004 by Sellathon, Inc.
// All Rights Reserved.

//insertion fees
var reglt1=0.3
var reg1to10=0.35
var reg10to25=0.6
var reg25to50=1.2
var reg50to200=2.4
var reg200to500=3.6
var reggt500=4.8

var motpassenger=40
var motbikesport=30

var reauction10=35
var reauction30=50
var read30=150
var read90=300

var re2auction10=100
var re2auction30=150
var re2ad30=150
var re2ad90=300

//reserve price fees

var reservelt50=1
var reserve50to200=2
var reserve200=0.01
var maxreserve200=100

var motorreservelt25=0.5
var motorreserve25to200=1
var motorreservegt200=2




//options

var homepage1=39.95
var homepage2=79.95
var featuredplus=19.95
var highlight=5
var border=3
var itemsubtitle=0.5
var bolditem=1
var listingdesigner=0.1
var gallery=0.35
var galleryfeatured=19.95
var tenday=0.2
var scheduledlisting=0.1
var butitnow=0.05
var giftservices=0.25

var additionalpic=0.15
var slideshow=0.75
var supersizepicture=0.75
var picturepack6=1
var picturepack12=1.5

var motorgalleryfeaturedvehicle=99.95
var motorgalleryfeaturednonvehicle=24.95
var motorhighlight=5
var motorboldvehicle=4
var motorboldnonvehicle=1
var motorbordervehicle=4
var motorbordernonvehicle=3
var listingdesignernonvehicle=0.1
var motorgalleryvehicle=0
var motorgallerynonvehicle=0.25
var motorfeaturedplus=19.95
var motorlistingiconvehicle=2
var motorlistingiconnonvehicle=1
var motortendaylisting=5
var motorbuyitnowvehicle=1
var motorbuyitnownonvehicle=0.05


//Picture Services
var firstpic=0
var additionalpic=0.15
var previewpic=0
var slideshow=0.75
var supersize=0.75
var picturepack6=1
var picturepack12=1.5

//Final value fees
var finalvalue0to25=0.0525
var finalvalue25to1000=0.0275
var finalvalue1000=	0.015

var transacfeepassenger=40.0
var transacfeemotorcycle=30.0

var finalvaluetimeshareland=35.0
var finalvalueotherre=0


function roundNumber(original_number) {
	var decimals=2;
	
	var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}



function calculatefee()
{
	calculateinsertion()
	calculateoptional()
	calculatereserve()
	calculatefinalvalue()
	calculatetotal()
}

function calculateinsertion()
{
	var insertionfee=0.0;
	var intprice=form1.initialprice.value*1.0;
	var quantity=form1.quantity.value*1.0;
	intprice*=quantity;
	if (form1.type123[0].checked)
	{
		if ((intprice<1.0) & (intprice>0.0))
			insertionfee+=reglt1;
		else if ((intprice<10.0) & (intprice>=1.0)) 
			insertionfee+=reg1to10;
		else if ((intprice<25.0) & (intprice>=10.0))
			insertionfee+=reg10to25;
		else if ((intprice<50.0) & (intprice>=25.0))
			insertionfee+=reg25to50;
		else if ((intprice<200.0) & (intprice>=50.0))
			insertionfee+=reg50to200;
		else if ((intprice<500.0) & (intprice>=200.0))
			insertionfee+=reg200to500;
		else if (intprice>=500.0)
			insertionfee+=reggt500;
		
		if (insertionfee>4.8)
			insertionfee=4.8;
	}
	else if (form1.type123[1].checked)
	{
		if ((form1.cartype.value==1) || (form1.cartype.value==2))
			insertionfee+=motpassenger;
		else if ((form1.cartype.value==3) || (form1.cartype.value==4))
			insertionfee+=motbikesport;
		else if (form1.cartype.value==5)
		{
			if ((intprice<1.0) & (intprice>0.0))
				insertionfee+=reglt1;
			else if ((intprice<10.0) & (intprice>=1.0)) 
				insertionfee+=reg1to10;
			else if ((intprice<25.0) & (intprice>=10.0))
				insertionfee+=reg10to25;
			else if ((intprice<50.0) & (intprice>=25.0))
				insertionfee+=reg25to50;
			else if ((intprice<200.0) & (intprice>=50.0))
				insertionfee+=reg50to200;
			else if ((intprice<500.0) & (intprice>=200.0))
				insertionfee+=reg200to500;
			else if (intprice>=500.0)
				insertionfee+=reggt500;
		}
	}
	else if (form1.type123[2].checked)
	{
		if (form1.format.value==1)
		{
			if (form1.listing.value==1)
				insertionfee+=re2auction10
			else if (form1.listing.value==2)
				insertionfee+=re2auction30
			else if (form1.listing.value==3)
			{
				alert('Invalid listing type selected, you can only select from the first 2 options');
				return;
			}
		}
		else if (form1.format.value==2)
		{
			if (form1.listing.value==2)
				insertionfee+=re2ad30
			else if (form1.listing.value==3)
				insertionfee+=re2ad90
			else if (form1.listing.value==1)
			{
				alert('Invalid listing type selected, you can only select from the last 2 options');
				return;
			}
		}
	}
	form1.insertion.value=roundNumber(insertionfee);
}

function calculateoptional()
{
	optionalfee=0;
	quantity=form1.quantity.value;
	if (form1.type123[0].checked)
	{	
		var list2categories=false;
		if (form1.homepagefeatured.checked)
		{
			if (quantity==1)
				optionalfee+=homepage1
			else if (quantity>1)
				optionalfee+=homepage2
		}
		if (form1.featuredplus.checked)
			optionalfee+=featuredplus
		if (form1.highlight.checked)
			optionalfee+=highlight
		if (form1.border.checked)
			optionalfee+=border
		if (form1.itemsubtitle.checked)
			optionalfee+=itemsubtitle
		if (form1.bolditem.checked)
			optionalfee+=bolditem
		if (form1.listingdesigner.checked)
			optionalfee+=listingdesigner
		if (form1.gallery.checked)
			optionalfee+=gallery
		if (form1.galleryfeatured.checked)
			optionalfee+=galleryfeatured
		if (form1.list2categories.checked)
			list2categories=true
		if (form1.tendayduration.checked)
			optionalfee+=tenday
		if (form1.scheduledlisting.checked)
			optionalfee+=scheduledlisting
		if (form1.buyitnow.checked)
			optionalfee+=butitnow
		if (form1.giftservices.checked)
			optionalfee+=giftservices
			
		if (list2categories)
		{
			form1.insertion.value=form1.insertion.value*2.0;
			optionalfee*=2;
			if (form1.scheduledlisting.checked)
				optionalfee-=scheduledlisting
			if (form1.homepagefeatured.checked)
			{
				if (quantity==1)
					optionalfee-=homepage1
				else if (quantity>1)
					optionalfee-=homepage2
			}
		}
		
	}
	else if (form1.type123[1].checked)
	{
		if (!form1.cartype[4].selected)		
		{
			if (form1.mmotorsgalleryfeatured.checked)
				optionalfee+=motorgalleryfeaturedvehicle
			if (form1.mhighlight.checked)
				optionalfee+=motorhighlight
			if (form1.mbold.checked)
				optionalfee+=motorboldvehicle
			if (form1.mborder.checked)
				optionalfee+=motorbordervehicle
			if (form1.mgallery.checked)
				optionalfee+=motorgalleryvehicle
			if (form1.mfeaturedplus.checked)
				optionalfee+=motorfeaturedplus
			if (form1.mlistingicon.checked)
				optionalfee+=motorlistingiconvehicle
			if (form1.mtendayduration.checked)
				optionalfee+=motortendaylisting
			if (form1.mbuyitnow.checked)
				optionalfee+=motorbuyitnowvehicle
		}
		else
		{
			if (form1.mmotorsgalleryfeatured.checked)
				optionalfee+=motorgalleryfeaturednonvehicle
			if (form1.mhighlight.checked)
				optionalfee+=motorhighlight
			if (form1.mbold.checked)
				optionalfee+=motorboldnonvehicle
			if (form1.mborder.checked)
				optionalfee+=motorbordernonvehicle
			if (form1.mlistingdesigner.checked)
				optionalfee+=listingdesignernonvehicle
			if (form1.mgallery.checked)
				optionalfee+=motorgallerynonvehicle
			if (form1.mfeaturedplus.checked)
				optionalfee+=motorfeaturedplus
			if (form1.mlistingicon.checked)
				optionalfee+=motorlistingiconnonvehicle
			if (form1.mtendayduration.checked)
				optionalfee+=motortendaylisting
			if (form1.mbuyitnow.checked)
				optionalfee+=motorbuyitnownonvehicle
		}
	}
	else if (form1.type123[2].checked)
	{
		optionalfee=0;
	
	}
	
	
	if (!form1.pictures[0].selected)
	{
		optionalfee+=firstpic;
		if (form1.pictures.value>1.0)
			optionalfee+=additionalpic*(form1.pictures.value-1.0);
		if (form1.previewpic.checked)
			optionalfee+=previewpic*1.0;
		if (form1.slideshow.checked)
			optionalfee+=slideshow*1.0;
		if (form1.supersize.checked)
			optionalfee+=supersize*1.0;
	}
	
	
	if (form1.picturepack6.checked)
		optionalfee+=picturepack6*1.0;
	if (form1.picturepack12.checked)
		optionalfee+=picturepack12*1.0;
	
	form1.optional.value=roundNumber(optionalfee);
	
}

function calculatereserve()
{
	var reservepricefee=0;
	var reserveprice=form1.reserveprice.value*1.0;
	if (form1.type123[0].checked)	
	{
		if ((reserveprice<50) && (reserveprice>0))
			reservepricefee=reservelt50;
		else if ((reserveprice<200) && (reserveprice>=50))
			reservepricefee=reserve50to200;
		else if (reserveprice>=200)
		{
			reservepricefee=reserve200*reserveprice
			if (reservepricefee>maxreserve200) 
				reservepricefee=maxreserve200;
		}
	}
	else if (form1.type123[1].checked)
	{
		if ((reserveprice<25) && (reserveprice>0))
			reservepricefee=motorreservelt25;
		else if ((reserveprice<200) && (reserveprice>=25))
			reservepricefee=motorreserve25to200;
		else if (reserveprice>=200)
			reservepricefee=motorreservegt200;
	}
	form1.reserve.value=roundNumber(reservepricefee);
}

function calculatefinalvalue()
{
	finalvaluefee=0;
	approxfinalvalue=form1.approxfinalvalue.value*1.0;
	if (form1.type123[0].checked) 
	{
		if (approxfinalvalue<=25)
			finalvaluefee+=finalvalue0to25*approxfinalvalue;
		else if (approxfinalvalue>25)
		{
			if (approxfinalvalue<=1000.0)
				finalvaluefee=finalvalue0to25*25.0+finalvalue25to1000*1.0*(approxfinalvalue*1.0-25.0)
			else if (approxfinalvalue>1000)
				finalvaluefee=finalvalue0to25*25.0+finalvalue25to1000*975.0+finalvalue1000*(approxfinalvalue*1.0-1000.0)
		}
	
	}
	else if (form1.type123[1].checked)
	{
		if (!form1.cartype[4].selected)	
		{
			if (form1.cartype[0].selected || form1.cartype[1].selected)
				finalvaluefee=transacfeepassenger
			else if (form1.cartype[2].selected || form1.cartype[3].selected)
				finalvaluefee=transacfeemotorcycle
		}
		else
		{
			if (approxfinalvalue<=25.0)
				finalvaluefee=finalvalue0to25*1.0*approxfinalvalue*1.0;
			else if (approxfinalvalue>25.0)
			{
				if (approxfinalvalue<=1000.0)
					finalvaluefee=finalvalue0to25*25.0+finalvalue25to1000*1.0*(approxfinalvalue*1.0-25.0)
				else if (approxfinalvalue>1000)
					finalvaluefee=finalvalue0to25*25.0+finalvalue25to1000*975.0+finalvalue1000*(approxfinalvalue*1.0-1000.0)
			}
		}
	
	}
	else if (form1.type123[2].checked)
	{	
		if (form1.timeshare.checked)
			finalvaluefee=finalvaluetimeshareland;
		else 
			finalvaluefee=finalvalueotherre;
	}
	
	form1.finalvalue.value=roundNumber(finalvaluefee);
}

function calculatetotal()
{
	form1.total.value=roundNumber(form1.insertion.value*1.0+form1.optional.value*1.0+form1.finalvalue.value*1.0+form1.reserve.value*1.0);

}

