﻿$("#contact-order").ready(function(){
    // Select, and deselects all checkboxes
    $("#selectAll").toggle(function(){
        $(".productTypes input").each(function(){
            this.checked = true;
        });
        $("#selectAll").text("נקה הכל >>");
    }, function(){
        $(".productTypes input").each(function(){
            this.checked = false;
        });    
        $("#selectAll").text("בחר הכל >>");
    });
});

function SendOrder()
{
    // Resseting all
    $("#txtFirstName").css("backgroundColor", "");
    $("#txtCity").css("backgroundColor", "");
    $("#txtTelephone").css("backgroundColor", "");
    $("#txtCellular").css("backgroundColor", "");
    $("#txtEmail").css("backgroundColor", "");
    
    var isValid = new Boolean(true);
 
    // Validating form   
    if (!$("#txtFirstName").validate("^.+$"))
    {
        $("#txtFirstName").blink();
        isValid = false;
    }
    if (!$("#txtCity").validate("^.+$"))
    {
        $("#txtCity").blink();
        isValid = false;
    }
    if (!$("#txtTelephone").validate("(\\d{2,3}-\\d{7})|(\\d{9,10})"))
    {
        $("#txtTelephone").blink();
        isValid = false;
    }
    if (!$("#txtCellular").validate("(\\d{2,3}-\\d{7})|(\\d{9,10})*"))
    {
        $("#txtCellular").blink();
        isValid = false;
    }
    if (!$("#txtEmail").validate("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"))
    {
        $("#txtEmail").blink();
        isValid = false;
    }
    
    // If valid, sending the form by AJAX to the server, and saying tnx
    if (isValid)
    {
        ShowBackground();
        
        $("#sendBtn").attr("href", "#");
        $("#sendBtn").attr("disabled", "disabled");
        
        // Getting an array of checked checkboxes
        var arrTypes = new Array();
        var arrInputs = new Array();
        $(".productTypes input").each(function(){
            if (this.checked) arrTypes.push(this.value);
            arrInputs.push($(this).parent().html());
        });
        
        AJAX.ContactOrder($("#txtFirstName").val(), $("#txtLastName").val(), $("#txtCity").val(), $("#txtStreet").val(), $("#ddlRooms").val(), $("#ddlRooms").ddText(), $("#txtTelephone").val(), $("#txtCellular").val(), $("#txtEmail").val(), $("#ddlCustomerType").val(),$("#ddlCustomerType").ddText(), $("#ddlSubject").val(),$("#ddlSubject").ddText(), $("#txtFreeText").val(), arrTypes, arrInputs, SendOrder_Callback);
    }
}

// Gets the selected text from a dropdown
$.fn.ddText = function(){
    return this.get(0).options[this.get(0).selectedIndex].text;
}

function SendOrder_Callback(res)
{
    window.location = "/Pages/Adviser/ThankYou.aspx";
    ShowPopUp(res.value);
}

 
