var message_bad_people_type = "Must be a number";
var req = "All fields with * must be filled";

var vat_215_strings = new Array('Bottled Mineral Water per bottle (250ml)', 
                                'Bottled Mineral Water per bottle (750ml)',
                                'Orange/Apple Juice per Jug Serving 6 persons approximately',
                                'Wine Jean Balmont Syrah or Sauvignon Blanc',
                                'Bottle of Minerals 500ml Coke, Diet Coke, 7 up, Club Orange etc...');
var checkbox_tea_breaks = [];

var checkbox_beverages = [];

var checkbox_finger_foods = [];

var checkbox_receptions = [];





var people = 0;

var vat_135 = 0;
var vat_215 = 0;

function isValueInArray(arr, val) 
{
    inArray = false;
    for (i = 0; i < arr.length; i++)
    {
        if (val == arr[i])
        {
            inArray = true;
        }
    }
    return inArray;
}

function check_people()
{

}

/*-------------------------------------------------------------------------
    IsNumeric(sText)
-------------------------------------------------------------------------*/
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

/*-------------------------------------------------------------------------
    inOrder(obj, text, price)
-------------------------------------------------------------------------*/
function inOrder(obj, text, price)
{
    var inOrd = false;
    for (var i in obj)
    {
        if ( (text == obj[i]['text']) && (price == obj[i]['price']) )
        {
            inOrd = true;
        }
    }
    return inOrd;
}

/*-------------------------------------------------------------------------
    findInOrder(obj, text, price)
-------------------------------------------------------------------------*/
function findInOrder(obj, text, price)
{
    var index = false;
    for (var i in obj)
    {
        if ( (text == obj[i]['text']) && (price == obj[i]['price']) )
        {
            index = i;
        }
    }
    return index;
}

function findInOrderByAmountField(obj, field)
{
    var index = false;
    for (var i in obj)
    {
        if (field == obj[i]['amount_field'])
        {
            index = i;
        }
    }
    return index;
}
/*-------------------------------------------------------------------------
    calc()
-------------------------------------------------------------------------*/
function calc()
{
   var total_price = 0;
   var vat_135 = 0;
   var vat_215 = 0;
   var total_price_vat = 0;
   
    for (var i in checkbox_tea_breaks)
    {
        var local_total_price = checkbox_tea_breaks[i]['price']*checkbox_tea_breaks[i]['amount'];
        total_price += local_total_price;
        if (isValueInArray(vat_215_strings, checkbox_tea_breaks[i]['text']))
        {
            vat_215 += local_total_price*0.215;
        } else {
            vat_135 += local_total_price*0.135;
        }
    }
    
    for (var i in checkbox_beverages)
    {
        var local_total_price = checkbox_beverages[i]['price']*checkbox_beverages[i]['amount'];
        total_price += local_total_price;
        if (isValueInArray(vat_215_strings, checkbox_beverages[i]['text']))
        {
            vat_215 += local_total_price*0.215;
        } else {
            vat_135 += local_total_price*0.135;
        }
    }
    
    for (var i in checkbox_finger_foods)
    {
        var local_total_price = checkbox_finger_foods[i]['price']*checkbox_finger_foods[i]['amount'];
        total_price += local_total_price;
        if (isValueInArray(vat_215_strings, checkbox_finger_foods[i]['text']))
        {
            vat_215 += local_total_price*0.215;
        } else {
            vat_135 += local_total_price*0.135;
        }
    }
    
    for (var i in checkbox_receptions)
    {
        var local_total_price = checkbox_receptions[i]['price']*checkbox_receptions[i]['amount'];
        total_price += local_total_price;
        if (isValueInArray(vat_215_strings, checkbox_receptions[i]['text']))
        {
            vat_215 += local_total_price*0.215;
        } else {
            vat_135 += local_total_price*0.135;
        }
    }
    
    total_price = total_price/100;
    vat_135 = vat_135/100;
    vat_215 = vat_215/100;
    total_price_vat = total_price + vat_135 +vat_215;
    var total_price_fix = total_price.toFixed(2);
    var vat_135_fix = vat_135.toFixed(2);
    var vat_215_fix = vat_215.toFixed(2);
    var total_price_vat_fix = total_price_vat.toFixed(2);
    $("input[name=total_price]").val("€"+total_price_fix);
    $("input[name=vat_135]").val("€"+vat_135_fix);
    $("input[name=vat_215]").val("€"+vat_215_fix);
    $("input[name=total_price_including_vat]").val("€"+total_price_vat_fix);
}


$(document).ready(function(){
    /*-------------------------------------------------------------------------
        get price from checkbox_tea_breaks
    -------------------------------------------------------------------------*/
    $("input[name=checkbox_tea_breaks[]]").click(function(){
        var id = $(this).attr("id");
        id = 'amount_'+id;
        if ($(this).is(':checked'))
        {
            $("#"+id).removeAttr("disabled");  
            $("#"+id).val("1");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[1];
            if (!inOrder(checkbox_tea_breaks, text, price))
            {
                checkbox_tea_break = {  "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_tea_breaks.push(checkbox_tea_break);
            } else {
                index = findInOrder(checkbox_tea_breaks, text, price);
                if (checkbox_tea_breaks.length == 1)
                {
                    checkbox_tea_breaks = [];
                } else { 
                    delete checkbox_tea_breaks[index];
                }
                checkbox_tea_break = {  "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_tea_breaks.push(checkbox_tea_break);
            }
        } else {
            $("#"+id).attr("disabled", "disabled");  
            $("#"+id).val("");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[1];
            if (inOrder(checkbox_tea_breaks, text, price))
            {
                index = findInOrder(checkbox_tea_breaks, text, price);
                if (checkbox_tea_breaks.length == 1)
                {
                    checkbox_tea_breaks = [];
                } else { 
                    delete checkbox_tea_breaks[index];
                }
            }
        }
        calc();
    });
    
    /*-------------------------------------------------------------------------
        get price from checkbox_beverage
    -------------------------------------------------------------------------*/
    $("input[name=checkbox_beverage[]]").click(function(){
        var id = $(this).attr("id");
        id = 'amount_'+id;
        if ($(this).is(':checked'))
        {
            $("#"+id).removeAttr("disabled");  
            $("#"+id).val("1");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[1];
            if (!inOrder(checkbox_beverages, text, price))
            {
                checkbox_beverage  = {  "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_beverages.push(checkbox_beverage);
            } else {
                index = findInOrder(checkbox_beverages, text, price);
                if (checkbox_beverages.length == 1)
                {
                    checkbox_beverages = [];
                } else { 
                    delete checkbox_beverages[index];
                }
                checkbox_beverage  = {  "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_beverages.push(checkbox_beverage);
            }
        } else {
            $("#"+id).attr("disabled", "disabled");  
            $("#"+id).val("");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[1];
            if (inOrder(checkbox_beverages, text, price))
            {
                index = findInOrder(checkbox_beverages, text, price);
                if (checkbox_beverages.length == 1)
                {
                    checkbox_beverages = [];
                } else { 
                    delete checkbox_beverages[index];
                }
            }
        }
        calc();
    });
    
    /*-------------------------------------------------------------------------
        get price from checkbox_finger_food
    -------------------------------------------------------------------------*/
    $("input[name=checkbox_finger_food[]]").click(function(){
        var id = $(this).attr("id");
        id = 'amount_'+id;
        if ($(this).is(':checked'))
        {
            $("#"+id).removeAttr("disabled");  
            $("#"+id).val("1");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[2];
            if (!inOrder(checkbox_finger_foods, text, price))
            {
                checkbox_finger_food  = {   "text"          : text,
                                            "price"         : price,
                                            "amount"        : 1,
                                            "amount_field"  : id};
                checkbox_finger_foods.push(checkbox_finger_food);
            } else {
                index = findInOrder(checkbox_finger_foods, text, price);
                if (checkbox_finger_foods.length == 1)
                {
                    checkbox_finger_foods = [];
                } else { 
                    delete checkbox_finger_foods[index];
                }
                checkbox_finger_food  = {   "text"          : text,
                                            "price"         : price,
                                            "amount"        : 1,
                                            "amount_field"  : id};
                checkbox_finger_foods.push(checkbox_finger_food);
            }
        } else {
            $("#"+id).attr("disabled", "disabled");  
            $("#"+id).val("");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[2];
            if (inOrder(checkbox_finger_foods, text, price))
            {
                index = findInOrder(checkbox_finger_foods, text, price);
                if (checkbox_finger_foods.length == 1)
                {
                    checkbox_finger_foods = [];
                } else { 
                    delete checkbox_finger_foods[index];
                }
            }
        }
        calc();
    });
    
    /*-------------------------------------------------------------------------
        get price from checkbox_reception
    -------------------------------------------------------------------------*/
    $("input[name=checkbox_reception[]]").click(function(){
        var id = $(this).attr("id");
        id = 'amount_'+id;
        if ($(this).is(':checked'))
        {
            $("#"+id).removeAttr("disabled");  
            $("#"+id).val("1");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[2];
            if (!inOrder(checkbox_receptions, text, price))
            {
                checkbox_reception  = { "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_receptions.push(checkbox_reception);
            } else {
                index = findInOrder(checkbox_receptions, text, price);
                if (checkbox_receptions.length == 1)
                {
                    checkbox_receptions = [];
                } else { 
                    delete checkbox_receptions[index];
                }
                checkbox_reception  = { "text"          : text,
                                        "price"         : price,
                                        "amount"        : 1,
                                        "amount_field"  : id};
                checkbox_receptions.push(checkbox_reception);
            }
        } else {
            $("#"+id).attr("disabled", "disabled");  
            $("#"+id).val("");
            var value = $(this).val();
            var value_splited = value.split(':');
            var text = value_splited[0];
            var price = value_splited[2];
            if (inOrder(checkbox_receptions, text, price))
            {
                index = findInOrder(checkbox_receptions, text, price);
                if (checkbox_receptions.length == 1)
                {
                    checkbox_receptions = [];
                } else { 
                    delete checkbox_receptions[index];
                }
            }
        }
        calc();
    });
    
    $("input[name=amount_checkbox_tea_breaks[]]").blur(function(){
        var id = $(this).attr("id");
        var index = findInOrderByAmountField(checkbox_tea_breaks, id);
        var val = $(this).val();
        if ( (false !== index) && IsNumeric(val))
        {
            checkbox_tea_breaks[index]['amount'] = val;
        } else {
            alert(message_bad_people_type);
        }
        calc();
    });
    
    $("input[name=amount_checkbox_beverage[]]").blur(function(){
        var id = $(this).attr("id");
        var index = findInOrderByAmountField(checkbox_beverages, id);
        var val = $(this).val();
        if ( (false !== index) && IsNumeric(val))
        {
            checkbox_beverages[index]['amount'] = val;
        } else {
            alert(message_bad_people_type);
        }
        calc();
    });
    
    $("input[name=amount_checkbox_finger_food[]]").blur(function(){
        var id = $(this).attr("id");
        var index = findInOrderByAmountField(checkbox_finger_foods, id);
        var val = $(this).val();
        if ( (false !== index) && IsNumeric(val))
        {
            checkbox_finger_foods[index]['amount'] = val;
        } else {
            alert(message_bad_people_type);
        }
        calc();
    });
    
    $("input[name=amount_checkbox_reception[]]").blur(function(){
        var id = $(this).attr("id");
        var index = findInOrderByAmountField(checkbox_receptions, id);
        var val = $(this).val();
        if ( (false !== index) && IsNumeric(val))
        {
            checkbox_receptions[index]['amount'] = val;
        } else {
            alert(message_bad_people_type);
        }
        calc();
    });
    
    
    $("#f_submit").click(function(){
        var valid = true;
        if ($("input[name=date]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=location]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=time]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=room_number]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=your_name]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=email]").val() == "")
        {
            valid = false;
        }
        if ($("input[name=telephone_numer]").val() == "")
        {
            valid = false;
        }
        if (!valid)
        {
            alert(req);
        }
        return valid;
    });
    
});
