﻿var _theForm;
var _theForm_oldSubmit;
var _theForm_submit;
var _theForm_oldOnSubmit;
var _theForm_onsubmit;
var ___doPostBack;
var jqSettings;

$(document).ready(function() {
    ___doPostBack = __doPostBack;

    _theForm = theForm;
    _theForm_oldSubmit = theForm.oldSubmit;
    _theForm_submit = theForm.submit;
    _theForm_oldOnSubmit = theForm.oldOnSubmit;
    _theForm_onsubmit = theForm.onsubmit;

    installSafetecDoPostback();
    jqSettings = eval("(" + $("#ctl00_divJQSettings").text() + ")");

    $(".submit a").click(function(event) {
        if ($(this).hasClass("submitted")) {
            event.preventDefault();
            return false;
        } else {
            $(this).addClass("submitted");
            return true;
        }
    });
});

function installSafetecDoPostback() {
    __doPostBack = function(eventTarget, eventArgument) { // Overload DoPostBack Function    
        cleanInputs();
        ___doPostBack(eventTarget, eventArgument);
        //PostBack(eventTarget, eventArgument);
    };
}

function RestoreOriginalForm() {
    theForm = _theForm;
    theForm.oldSubmit = _theForm_oldSubmit;
    theForm.submit = _theForm_submit;
    theForm.oldOnSubmit = _theForm_oldOnSubmit;
    theForm.onsubmit = _theForm_onsubmit;
}

/*function PostBack(eventTarget, eventArgument, postBackForm) {
    cleanInputs();

    if (postBackForm) {
        theForm = postBackForm;
    }

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }

    if (postBackForm) { // Reset Original Form
        RestoreOriginalForm();
    } 
}*/

function cleanInputs() {
    $('input[type=text], textarea').each(function() { 
        var $textbox = $(this);
        var value = $textbox.val();
        value = value.replace(/<([^ ])/g, '< $1');
        value = value.replace(/([^ ])>/g, '$1 >');
        $textbox.val(value);
    });
}

function htmlEncode(text) {
    var textneu = text.replace(/&/g, "&amp;");
    textneu = textneu.replace(/</g, "&lt;");
    textneu = textneu.replace(/>/g, "&gt;"); 
    return textneu;
}

