/********************************************
* Filename: functions.js
* Version: 1.0.0 (25-03-2009)
* Website: http://www.dcoed.com
* Author: Dan @ DCOE:DESIGNS
* Description: Stage Lighting Services Checkout Functions
*********************************************/
$(function() {
	$("#datepicker").datepicker({dateFormat: 'dd/mm/yy',minDate:+1});
	 $("#datepicker2").datepicker({dateFormat: 'dd/mm/yy',minDate:+1});
});
	
function refresh_basket(){
	document.getElementById("update").value ="true";
	document.update_form.submit();
}
function delete_item(id){
	document.getElementById("remove").value = id;
	document.getElementById("update").value ="true";
	document.update_form.submit();
}
Number.prototype.toDecimals=function(n){
    n=(isNaN(n))?
        2:
        n;
    var
        nT=Math.pow(10,n);
    function pad(s){
            s=s||'.';
            return (s.length>n)?
                s:
                pad(s+'0');
    }
    return (isNaN(this))?
        this:
        (new String(
            Math.round(this*nT)/nT
        )).replace(/(\.\d*)?$/,pad);
}


function calculate_discount(){
	/*
	//- DISCOUNTS
	24 Hour hires will receive a 25% discount.
	3 Day “Weekend” hires will receive 20% discount.
	10 days charged as 7 days – a “theatre week”
	2 Week hires will receive 5% overall discount.
	Each additional week will achieve an extra 5%, up to 6 weeks
	*/
	//- START DATE
	var entry = document.getElementById("datepicker").value.split('/');//splits input value;
	if((entry.length<3)||(entry[0].length!=2)||(entry[1].length!=2)||(entry[2].length!=4)||(Number(entry[0]>31))||(Number(entry[1]>12))){//some limitative conditions
		alert('Please input data in format dd/mm/yyyy!');
		document.getElementById("datepicker").value = '';
		return false;
	}
	y=entry[2]*1;
	m=entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
	d=entry[0]*1;
	
	var startDate = new Date(y,m,d);//sets the new date
	
	//- END DATE
	var entry = document.getElementById("datepicker2").value.split('/');//splits input value;
	if((entry.length<3)||(entry[0].length!=2)||(entry[1].length!=2)||(entry[2].length!=4)||(Number(entry[0]>31))||(Number(entry[1]>12))){//some limitative conditions
		alert('Please input data in format dd/mm/yyyy!');
		document.getElementById("datepicker2").value = '';
		return false;
	}
	y=entry[2]*1;
	m=entry[1]*1-1;//sets months to default values i.e Jan = 0; Feb = 1; etc.
	d=entry[0]*1;
	var endDate = new Date(y,m,d);//sets the new date
	
	dif = endDate-startDate;//difference in milliseconds
	dif_hours = Math.ceil(dif/1000/60/60);//difference in hours
	dif_days = Math.ceil(dif/1000/60/60/24);//difference in days
	dif_weeks = Math.ceil(dif/1000/60/60/24/7);//difference in days
	
	//alert( dif_days+' Days');
	
	
	
	//- WORK OUT DISCOUNT
	var subtotal = document.getElementById("subtotal").value;
	var subtotal_sale_only = document.getElementById("subtotal_sale_only").value;
	
	var subtotal_nonsale = subtotal - subtotal_sale_only;
	
	var discount = 0;
	var grandtotal = subtotal;
	var discount_txt = 'Discount might be available, please contact to discuss your order';
	var multiplier = 1; //for working out the grandtotal
	
	//- 24 Hour hires will receive a 25% discount.
	if(dif_days == 1){
		multiplier = 1;
		discount = (subtotal_nonsale*multiplier) / 4;
		discount_txt = '25% Discount - 24 Hour Hire';
		
	}
	//-3 Day “Weekend” hires will receive 20% discount. 
	else if((dif_days > 1) && (dif_days <= 3)){
		multiplier = 1;
		discount = ((subtotal_nonsale*multiplier) / 100) * 20;
		discount_txt = '20% Discount - 3 Day Hire';
		
	}
	//- 10 days charged as 7 days – a “theatre week” 
	else if((dif_days > 3) && (dif_days <= 6)){
		multiplier = 1;
		discount = ((subtotal_nonsale*multiplier) / 100) * 100;
		discount_txt = '10 Days for the Price of 7';
	}
	else if(dif_days > 6){
		multiplier = 2;
		//- 10 days charged as 7 days – a “theatre week”
		//else if((dif_days >= 7) && (dif_days <= 10)){
		if((dif_days > 6) && (dif_days <= 9)){
			//multiplier = 1.42857143;
			//discount = ((subtotal_nonsale*multiplier) / 100) * 30;
			multiplier = 2;
			discount = ((subtotal_nonsale*multiplier) / 100) * 50;
			discount_txt = '10 Days for the Price of 7';
		}
		//- 2 Week hires will receive 5% overall discount.
		else if(dif_days == 13){
			multiplier = 2;
			discount = ((subtotal_nonsale*multiplier) / 100) * 5;
			discount_txt = '5% Discount - 2 Week Hire';
		}
		//- Each additional week will achieve an extra 5%, up to 6 weeks
		else if(dif_days > 14){
			if(dif_days <= 20){
				multiplier = 3;
				discount = ((subtotal_nonsale*multiplier) / 100) * 10;
				discount_txt = '10% Discount (5% per Week up to 6 weeks)';
			} 
			else if(dif_days <= 27){
				multiplier = 4;
				discount = ((subtotal_nonsale*multiplier) / 100) * 15;
				discount_txt = '15% Discount (5% per Week up to 6 weeks)';
			}
			else if(dif_days <= 34){
				multiplier = 5;
				discount = ((subtotal_nonsale*multiplier) / 100) * 20;
				discount_txt = '20% Discount (5% per Week up to 6 weeks)';
			}
			else if(dif_days > 34){ //- max discount
				multiplier = dif_weeks;
				discount = ((subtotal_nonsale*6) / 100) * 25;
				discount_txt = dif_weeks +' Weeks - 25% Discount (5% per Week up to 6 weeks)';
			}
		
	}
	}
	
	
	var gt = new Number(((subtotal_nonsale * multiplier) + (subtotal_sale_only*1)));// - discount);
	grandtotal = gt.toDecimals(2);
	discount = discount.toDecimals(2);
	
	var st = new Number((subtotal_nonsale * multiplier) + (subtotal_sale_only*1));
	st = st.toDecimals(2);
	
	
	//alert('SUBTOTAL:'+subtotal_nonsale +'\nSALEITEMS:'+subtotal_sale_only +'\nDAYS:'+ (dif_days+1)+'\nMULTIPLIER:'+ multiplier +'\nDISCOUNT:'+ discount +'\nTOTAL:'+ grandtotal+'\n\nTEST:'+(dif_weeks));

	/* THESE ARE WHAT IT SHOULD BE
	document.getElementById("subtotal2").value =  '\243'+ st;
	document.getElementById("discount_txt").value = discount_txt;
	document.getElementById("discount").value = '\243'+discount;
	document.getElementById("grandtotal").value = '\243'+grandtotal;
	
	BUT I'VE CHANGED THEM TO */
	//document.getElementById("subtotal2").value =  '\243'+ st;
	//document.getElementById("discount_txt").value = discount_txt;
	//document.getElementById("discount").value = '\243'+discount;
	document.getElementById("grandtotal").value = '\243'+ grandtotal;
	
	//-UPDATE HIDDEN STUFF FOR FORM
	document.getElementById("start_date").value = document.getElementById("datepicker").value;
	document.getElementById("end_date").value = document.getElementById("datepicker2").value;
	document.getElementById("hidden_total").value = grandtotal;
	document.getElementById("hidden_discount").value = discount;
	
}
		
