// ****************************************************************************
// used to validate text boxes
// ****************************************************************************
function RemoveAllSpecialCharsHandler() {
    RemoveAllSpecialChars(this);
}

function RemoveAllSpecialChars(element) {
    //var element = event.element();
    element = $j(element);
    var regEx = /[;\'><]/g;
    if (element.val().search(regEx) > -1) {
        element.val(element.val().replace(/\'/g, '').replace(/[;><]/g, ' '));
        if ($j(element[0].name + 'SA').length == 0) {
            element.after('<span id="' + element[0].name + 'SA" class="SecurityAlert" title="We had to remove some of your input for security reasons.">!!<span>');
        }
    }
}

function RemoveSpecialCharsHandler() {
    RemoveSpecialChars(this);
}

function RemoveSpecialChars(element) {
    //var element = event.element();
    element = $j(element);
    var regEx = /[;><]/g;
    if (element.val().search(regEx) > -1) {
        element.val(element.val().replace(regEx, ' '));
        if ($j(element[0].name + 'SA').length == 0) {
            element.after('<span id="' + element[0].name + 'SA" class="SecurityAlert" title="We had to remove some of your input for security reasons.">!!<span>');
        }
    }
}

function loadCleanupScript() {
    if ((typeof allowAll != 'undefined') && (typeof lessSecure != 'undefined')) {
        $j('form').each(
            function(index2, ele2) {
                $j(ele2).submit(function() {
                    $j('input[type="text"], textarea').each(
                        function(index, ele) {
                            if (allowAll.indexOf(ele.name.toLowerCase()) >= 0) {
                                //Don't do anything
                            }
                            else if (lessSecure.indexOf(ele.name.toLowerCase()) >= 0) {
                                RemoveSpecialChars($j(ele));
                            }
                            else {
                                RemoveAllSpecialChars($j(ele));
                            }
                        }
                    );
                });
            }
        )

        $j('input[type="text"], textarea').each(
            function(index, ele) {
                if (allowAll.indexOf(ele.name.toLowerCase()) >= 0) {
                    //Don't do anything
                }
                else if (lessSecure.indexOf(ele.name.toLowerCase()) >= 0) {
                    $j(ele).blur(RemoveSpecialCharsHandler);
                }
                else {
                    $j(ele).blur(RemoveAllSpecialCharsHandler);
                }
            }
        );
    }
}

$j(document).ready(function() {
    loadCleanupScript();
    if (typeof Sys != 'undefined') {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(loadCleanupScript);
    }
});


// ref : http://techpatterns.com/downloads/javascript_cookies.php
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

// ****************************************************************************
// Internal Campaign Tracking Helper.  Will add the value of the icid attribute to the href of an
// anchor element.
// ****************************************************************************
var ICIDHelper = {
    init: function() {
        $j('a[icid]').each(
			function(index, anchor) {
			    var sep = (anchor.href.indexOf('?') > 0) ? '&' : '?';
			    anchor.href = anchor.href + sep + 'icid=' + encodeURIComponent($j(anchor).attr('icid').value);
			}
		);
    }
};

$j(document).ready(ICIDHelper.init);

// ****************************************************************************
// cleans up AJAX response from RenderControls
// ****************************************************************************
var CleanAjaxResponse = function(responseText) {
    return responseText.replace(/<\/?form.*?>/g, '').replace(/<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" .*?>/g, '');
}

var RemoveScriptFromAjaxResponse = function(responseText) {
    return responseText.replace(/<\/?script.*?>/g, '');
}
