﻿function Logout()
{
    $.post("/Form/Logout", null, function () { });
}

function FormSelectValue(theInput)
{
    var value = $(theInput).val();

    if (value.indexOf("vb.") == 0)
    {
        //$(theInput).select();
        $(theInput).val("");
    }

    $(theInput).removeClass();
}
function FormSetInputClass(theInput)
{
    var value = $(theInput).val();

    $(theInput).removeClass();

    if (value.indexOf("vb.") == 0)
    {
        $(theInput).addClass("formUnFilled");
    }
}
function ValidateFormField(waarde, theFieldId,theFormId)
{
    if (theFieldId == "verify")
    {
        $("#spanValidate_Verify").hide();
        $("#imgValidate_Verify").hide();

        var theImgSource = "/SiteContent/Common/images/success_icon.png";
        var message = "";

        if ($("#txtVerify_" + theFormId).val() != $("#hidVerify_" + theFormId).val())
        {
            theImgSource = "/SiteContent/Common/images/warning_icon.png";
            message = "Beveiligingscode is ongeldig!";
        }

        $("#imgValidate_Verify").height(14);
        $("#imgValidate_Verify").attr("src", theImgSource);
        $("#imgValidate_Verify").attr("title", message);
        $("#imgValidate_Verify").attr("alt", message);


        $("#imgValidate_Verify").show();
        if (message != "" && $("#hidFormShowErrorLabels_" + theFormId).val() == "1")
        {
            $("#spanValidate_Verify").html(message);
            $("#spanValidate_Verify").show();
        }
        
    }
    else
    {
        $("#spanValidate_" + theFieldId).hide();

        var oParams = {};
        oParams["waarde"] = waarde;
        oParams["formFieldId"] = theFieldId;
        oParams["entityId"] = $("#hidFormEntityId_" + theFormId).val();

        if ($("#formfieldrep_" + theFieldId).length > 0)
        {
            oParams["waarderep"] = $("#formfieldrep_" + theFieldId).val();
        }

        $.post("/Form/ValidateField", oParams, function (result)
        {
			
			
            result = $.parseJSON(result);

            $("#imgValidate_" + theFieldId).hide();
            $("#spanValidate_" + theFieldId).hide();

            var theImgSource = "/SiteContent/Common/images/success_icon.png";

            if (result.status == -1)
                theImgSource = "/SiteContent/Common/images/warning_icon.png";



            if (result.message != "" && $("#hidFormShowErrorLabels_" + theFormId).val() == "1")
            {
                $("#spanValidate_" + theFieldId).html(result.message);
                $("#spanValidate_" + theFieldId).show();
            }

            $("#imgValidate_" + theFieldId).height(14);
            $("#imgValidate_" + theFieldId).attr("src", theImgSource);
            $("#imgValidate_" + theFieldId).attr("title", result.message);
            $("#imgValidate_" + theFieldId).attr("alt", result.message);

            if (result.message != "" || waarde != "")
                $("#imgValidate_" + theFieldId).show();

        });
    }



}
function ResetForm(formId)
{
    document.getElementById("form_" + formId).reset();
    $("*[Validation=" + formId + "]").hide();

    $("#form_" + formId + " input").each(function ()
    {
        FormSetInputClass($(this));
    });

}
function SubmitForm(formId)
{
    $("#message_" + formId).html("Even geduld aub...");
    $("#btnSubmit_" + formId).hide();
    $("#btnReset_" + formId).hide();
    
    var oParams={};
    
    oParams["FormId"] = formId;

    oParams["DropdownLists"] = GetFormDropdownListValues(formId);
    oParams["RadioButtons"] = GetFormRadioButtonValues(formId);
    oParams["CheckBoxes"] = GetFormCheckboxValues(formId);
    oParams["TextFields"] = GetFormTextFieldValues(formId);
    oParams["passwords"] = GetFormPasswordValues(formId);
    oParams["TextAreas"] = GetFormTextAreaValues(formId);
    oParams["YesNo"] = GetFormYesNoValues(formId);
    oParams["Verify"] = $("#hidVerify_" + formId).val();
    oParams["Verify2"] = $("#txtVerify_" + formId).val();
    oParams["EntityId"] = $("#hidFormEntityId_" + formId).val();

    $.getJSON("/Form/SubmitUsingjQuery", oParams, function (data)
    {
        if (data.status == '0')
        {
            $("#btnSubmit_" + formId).show();
            $("#btnReset_" + formId).show();

        }
        else
        {
            if (data.status == '2')
            {
                if (typeof OnLogin == 'function')
                {
                    OnLogin();
                }

            }
            else
            {
                if (typeof OnFormSubmit == 'function')
                {
                    OnFormSubmit();
                }
            }
            
            
            $("#btnSubmit_" + formId).show();
            $("#btnReset_" + formId).show();
            $(':input', '#form_' + formId).not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');

            $("#form_" + formId + " .formValidationIcon").hide();
            $("#form_" + formId + " .formValidationMessage").html('');

            
        }

        $("#message_" + formId).html(data.message);
    });

}

function GetFormTextFieldValues(formId)
{
    var values = {};
    var teller = 0;
    $("#form_" + formId + " input[type=text]").each(function ()
    {

        if ($(this).attr("id").indexOf("formfield") == 0)
        {

            values[teller] = "[FieldName]" + $(this).attr("id") + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll($(this).val(), ">", "&gt;"), "<", "&lt;") + "[/FieldValue]";
            teller = teller + 1;
        }


    });

    return values;
}
function GetFormPasswordValues(formId)
{
    var values = {};
    var teller = 0;
    $("#form_" + formId + " input[type=password]").each(function ()
    {

        if ($(this).attr("id").indexOf("formfield") == 0)
        {

            values[teller] = "[FieldName]" + $(this).attr("id") + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll($(this).val(), ">", "&gt;"), "<", "&lt;") + "[/FieldValue]";
            teller = teller + 1;
        }


    });

    return values;
}
function GetFormDropdownListValues(formId)
{
    var values = {};
    var teller = 0;
    $("#form_" + formId + " select").each(function ()
    {

        

        if ($(this).attr("id").indexOf("formfield") == 0)
        {

            values[teller] = "[FieldName]" + $(this).attr("id") + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll($(this).val(), ">", "&gt;"), "<", "&lt;") + "[/FieldValue]";
            
            teller = teller + 1;
        }


    });

    return values;
}
function GetFormRadioButtonValues(formId)
{
    var values = {};
    var teller = 0;
    $("#form_" + formId + "  input[radioList=1]").each(function ()
    {

        var theId = $(this).val();
        var theFieldName = "formfield_" + theId;

        var theValue = "";
        $("input[name=" + theFieldName + "]").each(function ()
        {

            if ($(this).attr("checked"))
                theValue = $(this).val();
        });


        values[teller] = "[FieldName]" + theFieldName + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll(theValue, ">", "&gt;"), "<", "&lt;") + "[/FieldValue]";
        
        teller = teller + 1;



    });

    return values;
}
function GetFormCheckboxValues(formId)
{
    var values = {};
    var teller = 0;
    $("#form_" + formId + "  input[checkboxList=1]").each(function ()
    {

        var theId = $(this).val();
        var theFieldName = "formfield_" + theId;

        var theValue = "";
        $("input[name=" + theFieldName + "]").each(function ()
        {
            

            if ($(this).attr("checked"))
            {
                if (theValue != "")
                    theValue = theValue + ", ";

                theValue += $(this).val();
            }
        });


        values[teller] = "[FieldName]" + theFieldName + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll(theValue, ">", "&gt;"), "<", "&lt;") + "[/FieldValue]";
       

        teller = teller + 1;



    });

    return values;
}

function GetFormTextAreaValues(formId)
{
    var values = {};
    var teller=0;
    $("#form_" + formId + " textarea").each(function(){
    
       if($(this).attr("id").indexOf("formfield")==0)
       {
        
            values[teller]="[FieldName]" + $(this).attr("id") + "[/FieldName][FieldValue]" + ReplaceAll(ReplaceAll($(this).val(),">","&gt;"),"<","&lt;") + "[/FieldValue]";
            teller=teller+1;
       }    
    
    });
    return values;
}
function GetFormYesNoValues(formId)
{
    var values = {};
    var teller=0;
    $("#form_" + formId + " input[yesOrNo=1]").each(function ()
    {
    
       if($(this).attr("id").indexOf("formfield")==0)
       {
            var theValue="0";
            if($(this).attr("checked"))
            {
                theValue="1";
            }
            
            values[teller]="[FieldName]" + $(this).attr("id") + "[/FieldName][FieldValue]" + theValue + "[/FieldValue]";
            teller=teller+1;
       }    
    
    });
    return values;

}


function ReplaceAll(theString,toReplace,replaceWith)
{     
    var myString=theString;
    
    while(myString.indexOf(toReplace)>-1)
    {
        myString=myString.replace(toReplace,replaceWith);
    }

    return myString;
}
function ValidateNumber(evt,textbox)
{
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    
    
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=44 && charCode !=46)
        return false;
    
    
    if(charCode==44 || charCode==46)
    {
        if(textbox.value.indexOf(".")>-1)
            return false;

        if(textbox.value.indexOf(",")>-1)
            return false;
    }
    
   

    return true;

}
function OpenTextAreaWindow(theFieldId, theTitel)
{
    var exValue = $("#formfield_" + theFieldId).val();

    var dialogContent = "<div id='memoEditor_" + theFieldId + "' style='display:none;' title=\"" + theTitel+ "\"><textarea name='bigMemo_" + theFieldId + "' id='bigMemo_" + theFieldId + "' style='width:470px;height:300px;' onKeyUp=\"UpdateMemoValue('" + theFieldId + "');\">" + exValue + "</textarea><input type='submit' class='submitbutton' onClick=\"CloseDialog('" + theFieldId + "');return false;\" value='Sluiten'></div>";
    $("#formFieldContainer_" + theFieldId).append(dialogContent);

    $("#memoEditor_" + theFieldId).dialog({ modal: true, height: 400, width: 500, draggable: true });
}
function UpdateMemoValue(theFieldId)
{
    $("#formfield_" + theFieldId).val($("#bigMemo_" + theFieldId).val());
}
function CloseDialog(theFieldId)
{
    $("#memoEditor_" + theFieldId).dialog("destroy");
    $("#memoEditor_" + theFieldId).remove();
}
