var prices = Array();
prices[1] = Array( 930, 780, 720, 690);
prices[2] = Array( 960, 810, 750, 720);
prices[3] = Array( 930, 780, 720, 690);
prices[4] = Array( 960, 810, 750, 720);
prices[5] = Array( 1050, 900, 840, 810);
prices[6] = Array( 1080, 930, 870, 840);
prices[7] = Array( 1080, 930, 870, 840);
prices[8] = Array( 1500, 1410,1350, 1290);
prices[9] = Array( 1500, 1410, 1350, 1290);
prices[10] = Array( 1500, 1410, 1350, 1290);
prices[11] = Array( 1710, 1560, 1500, 1470);
prices[12] = Array( 1710, 1560, 1500, 1470);
prices[13] = Array( 1710, 1560, 1500, 1470);
prices[14] = Array( 1560, 1440, 1380, 1320);
prices[15] = Array( 6000, 6000, 6000, 6000);

var complexity12_array = Array( 90, 150, 210);
var complexity_array = Array('', 750, 900, 900, 300, 900, 35, 210, 7.5, 75);
var add_array = Array( '', 240, 450, 600, 50, 450, 600, 900, 900, 300);

function validate(obj) {
	if (isNaN(obj.value)) {
		obj.value = 0;
	} else {
		obj.value=parseFloat(parseInt(obj.value*100)/100);
	}
}	

function toInt(arg){
	if (isNaN(arg) || "" == arg) {
		return 0;
	} 
	return parseInt(arg);
}	

function toFloat(arg){
	var a = parseFloat(arg);
	if (isNaN(a)) { return 0; } 
	return a;
}

function Calculate() {
    var squarage = toFloat( document.getElementById( 'square').value);
    var result = 0;
    var persent = 1;
    
    result += getOnePrice() * squarage + getCompPrice() + getCornerPrice();
    
    if (document.getElementById( 'complexity_check1').checked) {
    	persent += 0.15;
    }
    if (document.getElementById( 'complexity_check2').checked) {
    	persent += 0.15;
    }
    result = parseFloat(parseInt(result * persent*100)/100);
    
    result += getAddPrice();
    
    document.getElementById( 'outsum').value = result;
    //getOnePrice();
}

function getOnePrice() {
    var facture = document.getElementById( 'factura_options').options[document.getElementById( 'factura_options').selectedIndex].value;
    var squarage = toFloat( document.getElementById( 'square').value);
    var second_index = 0;
    if (squarage < 10) {
        second_index = 0;
    } else if (squarage < 20) {
        second_index = 1;
    } else if (squarage < 50) {
        second_index = 2;
    } else {
        second_index = 3;
    }
    return prices[facture][second_index];
}

function getCompPrice() {
	var result1 = 0;
	var value = 0;
	for (var i =1; i < 10; i++){
		value = toFloat( document.getElementById( 'complexity' + i).value);
		result1 += value * complexity_array[i];
	}
	return result1;
}

function getCornerPrice() {
	var corners = toInt( document.getElementById( 'complexity10').value);
	if (corners < 10) {
		return corners * 90;
	} else if (corners < 20) {
		return corners * 150;
	} else {
		return corners * 210;
	}
}

function getAddPrice() {
	var result1 = 0;
	var value = 0;
	for (var i =1; i < 10; i++){
		value = toFloat( document.getElementById( 'add' + i).value);
		result1 += value * add_array[i];
	}
	return result1;	
}

