var totalprice = 0;
var km = 0;
var kmprice = 0;
var maintenance_price = 0;
var rentalprice = 0;

function getRadioValue(radioName) {
	for(var i = 0; i < document.quotation[radioName].length; i++) {
		if(document.quotation[radioName][i].checked) {
			return document.quotation[radioName][i].value;
		}
	}
	
	return '';
}

function check_delivery() {
	var object = document.getElementById('rent_trailer');
	var object2 = document.getElementById('different_delivery_address');
	var object2_box = document.quotation.different_delivery_address;
	var object3 = document.getElementById('deliver_fields');
	var object4 = document.quotation.maintenance;
	var object5 = document.getElementById('maintenance_fields');
	
	if(getRadioValue('deliver') == 'ophalen Oosterland') {
		object.style.display = '';
		object2.style.display = 'none';
		object3.style.display = 'none';
	} else if(getRadioValue('deliver') == 'ophalen Goes') {
		object.style.display = '';
		object2.style.display = 'none';
		object3.style.display = 'none';
	} else if(getRadioValue('deliver') == 'ophalen Oosterland met aanhangwagen'){
		object.style.display = '';
		object2.style.display = 'none';
		object3.style.display = 'none';
	} else if(getRadioValue('deliver') == 'ophalen Goes met aanhangwagen'){
		object.style.display = '';
		object2.style.display = 'none';
		object3.style.display = 'none';
	} else {
		object.style.display = 'none';
		object2.style.display = '';
		
		if(object2_box.checked) {
			object3.style.display = '';
		} else {
			object3.style.display = 'none';
		}
		
		if(object4.checked) {
			object5.style.display = '';
		} else {
			object5.style.display = 'none';
		}
	}
}

function validate_form() {
	check_delivery();
	calcTransportPrice();
	calcMaintenancePrice();
	calcTrailerPrice();
	calcProductsTotalPrice();
	updatePrice();
}

function calcDistance() {
	makeLocation(document.edit.postalcode, document.edit.deliver_postalcode, document.edit.city, document.edit.deliver_city);
	
	document.getElementById('distance').value = km;
}

function calcUpdateDistance() {
	var address = document.edit.address.value;
	var postalcode = document.edit.postalcode.value;
	var city = document.edit.city.value;
	var deliverAddress = document.edit.deliverAddress.value;
	var deliverPostalcode = document.edit.deliverPostalcode.value;
	var deliverCity = document.edit.deliverCity.value;
	
	if(address == deliverAddress && postalcode == deliverPostalcode && city == deliverCity){
		makeLocation(postalcode, '', city, '');
		
		document.getElementById('distance').value = km;
	}
	
	else if(deliverAddress == '' || deliverPostalcode == '' || deliverCity == ''){
		makeLocation(postalcode, '', city, '');
		
		document.getElementById('distance').value = km;
	}
	
	else{
		makeLocation(deliverPostalcode, '', deliverCity, '');
		
		document.getElementById('distance').value = km;
	}
}

function calcDistanceBasic(){
	var postalcode = document.edit.postalcode.value;
	var city = document.edit.city.value;
	
	makeLocation(postalcode, '', city, '');
	
	document.getElementById('totalDistance').innerHTML = (Math.round(km * 10) / 10);
	document.getElementById('totalPrice').innerHTML = km2price(km);
}

function setFocusValue(element,originalValue){
	if(element.value == originalValue){
		element.value = '';
	}
}

function setBlurValue(element,originalValue){
	if(element.value == '' || element.value == originalValue){
		element.value = originalValue;
	}
}

function calcAdminUI(id, postalcode, city) {
	makeLocation(postalcode, '', city, '');
	setTimeout("updateAdminUI('" + id + "')", 2000);
	calcTotalPrice(id);
}

function calcTrailerPrice(){
	if(getRadioValue('deliver') == 'ophalen Oosterland met aanhangwagen'){
		trailer_price = 35;
	} else if(getRadioValue('deliver') == 'ophalen Goes met aanhangwagen'){
		trailer_price = 35;
	} else{
		trailer_price = 0;
	}
}

function calcTotalPrice(id) {
	if(getElementById(id+'_kmprice')) {
		
	}
}

function calcTransportPrice() {
	if (getRadioValue('deliver') == 'bezorgen') {
		makeLocation(document.quotation.postalcode, document.quotation.deliver_postalcode, document.quotation.city, document.quotation.deliver_city);
	} else {
		kmprice = 0;
	}
}

function calcDays(startDate, endDate) {
	var day = 1000 * 60 * 60 * 24;
	
	var startDateSplit = startDate.split('-');
	var endDateSplit = endDate.split('-');
	var newStartDate = startDateSplit[2]+'-'+startDateSplit[1]+'-'+startDateSplit[0];
	var newEndDate = endDateSplit[2]+'-'+endDateSplit[1]+'-'+endDateSplit[0];
	
	return Math.ceil((new Date(newEndDate).getTime() - new Date(newStartDate).getTime()) / day) + 1;
}

function calcMaintenancePrice() {
	if(document.quotation.maintenance.checked) {
		maintenance_price = document.quotation.maintenance_hours.value * 35;
	} else {
		maintenance_price = 0;
	}
}

function calcProductsTotalPrice() {
	var i = 0;
	var finalproductprice = 0;
	
	while(true) {
		if(!document.getElementById('productpriceperday_' + i) || !document.getElementById('producttotalprice_' + i)) {
			break;
		}
		
		document.getElementById('producttotalprice_' + i).innerHTML = price2string(calcProductTotalPrice(string2price(document.getElementById('productpriceperday_' + i).innerHTML)));
		finalproductprice += string2price(document.getElementById('producttotalprice_' + i).innerHTML);
		
		i++;
	}
	
	if(finalproductprice > 0) {
		totalprice = finalproductprice;
	}
}

function calcProductTotalPrice(price) {
	var days = calcDays(document.quotation.rental_start.value, document.quotation.rental_end.value);
	
	var producttotalprice = 0;
	
	if(days >= 3) {
		producttotalprice += (days - 2) * (price * 0.3);
	}
	
	if(days >= 2) {
		producttotalprice += price * 0.5;
	}
	
	if(days >= 1) {
		producttotalprice += price;
	}
	
	if(days < 1) {
		producttotalprice += price;
	}
	
	if(isNaN(days)){
		producttotalprice += price;
	}
	
	return producttotalprice;
}

function updatePrice() {
	document.getElementById('transport').innerHTML = price2string(kmprice);
	document.getElementById('maintenance_price').innerHTML = price2string(maintenance_price);
	document.getElementById('trailer_price').innerHTML = price2string(trailer_price);
	document.getElementById('total').innerHTML = price2string(totalprice + kmprice + maintenance_price + trailer_price);
	document.getElementById('distance').value = km;
}

function updateAdminUI(id) {
	if(document.getElementById(id + '_km') && document.getElementById(id + '_kmprice')) {
		document.getElementById(id + '_km').innerHTML = km;
		document.getElementById(id + '_kmprice').innerHTML = price2string(kmprice);
	}
}

function setProductTotal(val) {
	totalprice = val;
}

function toggleTab(self_id, list) {
	var self = document.getElementById(self_id);
	self.style.display = '';
	
	var children = self.parentNode.childNodes;
	
	for(var c = 0; c < children.length; c++) {
		if(children[c].id != self_id && children[c].tagName !== undefined && (children[c].tagName == 'P' || children[c].tagName == 'SPAN')) { 
			children[c].style.display = 'none';
		}
	}
	
	var listItems = list.parentNode.childNodes;
	
	for(var l = 0; l < listItems.length; l++) {				
		if(listItems[l].tagName == 'LI' && listItems[l].getAttribute('class') !== undefined) {
			listItems[l].removeAttribute('class');
		}
	}
	
	list.setAttribute('class', 'active');
}

function price2string(price) {
	var str = '';
	
	if(Math.floor(price) < price) {
		dec = Math.ceil((price - Math.floor(price)) * 100);
		str = Math.floor(price) +','+ dec;
	} else {
		str = price +',-';
	}
	
	return '&euro;' + str;
}

function string2price(input) {
	var price = 0;
	var arr = input.split('');
	
	str = '';
	
	for(var chr in arr) {
		if(isNumeric(arr[chr])) {
			if(arr[chr] == '-') {
				arr[chr] = '00';
			}
			
			str += arr[chr];
		}
	}
	
	price = parseFloat(str.replace(',', '.'));
	return price;
}

function productToggleList(pid, to) {
	var destination = document.getElementById(to);
	var currElement = document.getElementById('products_' + pid);
	
	var newInnerHTML = currElement.innerHTML;
	
	var arrow_up_regex = /arrow-up/gi;
	
	if(arrow_up_regex.test(newInnerHTML)) {
		newInnerHTML = newInnerHTML.replace(arrow_up_regex, 'delete');
		newInnerHTML = newInnerHTML.replace(/selected_products/gi, 'remaining_products');
		document.edit.products.value += '|' + pid;
	} else {
		newInnerHTML = newInnerHTML.replace(/delete/gi, 'arrow-up');
		newInnerHTML = newInnerHTML.replace(/remaining_products/gi, 'selected_products');
		
		var oldproducts = document.edit.products.value;
		oldproducts = oldproducts.split("|");
		
		var newproducts = [];
		
		for(var product in oldproducts) {
			if(oldproducts[product] != pid) {
				newproducts.push(oldproducts[product]);
			}
		}
		
		document.edit.products.value = newproducts.join("|");
	}
	
	currElement.parentNode.removeChild(currElement);
	destination.innerHTML += '<li id="products_' + pid + '">' + newInnerHTML + '</li>';
}

function isNumeric(chr) {
	var validChars = '0123456789,.-';
	
	if(validChars.indexOf(chr) == -1) {
		return false;
	}
	
	return true;
}

function km2price(km) {
	return (Math.ceil((km * 100) * 2) / 100) + 35;
}
