﻿// JScript File
function checkHtmlTag(allowedHtmlTags)
{
    var tags = allowedHtmlTags.split(";");
    var isNotAllowLink = true;
    var isNotAllowImage = true;
    var isNotAllowMedia = true;
    var isNotAllowLists = true;
    var isNotAllowTextFmt = true;

    if(tags.length > 0)
    {
        for( i = 0 ; i<tags.length ; i++)
        {
            if(tags[i] ==";")
                continue;
            if(tags[i].toLowerCase() == "text formatting")
                isNotAllowTextFmt = false;
            else if(tags[i].toLowerCase() == "links")
                isNotAllowLink = false;
            else if(tags[i].toLowerCase() == "images")
                isNotAllowImage = false;
            else if(tags[i].toLowerCase() == "media")
                isNotAllowMedia = false;
            else if(tags[i].toLowerCase() == "lists")
                isNotAllowLists = false;
        }
    }
    
    var _textArea = UPC_GetElement("InputComment");
    var _match;
    var tagInError = "";
    var showErrorMsg = false;
    if(isNotAllowLink)
    {
        _match =  _textArea.value.toLowerCase().match(/<(a\040.|\/a>)/); //<(a|\/a).*?> old expression saved
    
        if( _match != null &&_match.length > 0)
        {
            tagInError = "link tags aren't allowed";
            showErrorMsg = true;
        }
    }
    else
    {
        _match =  _textArea.value.toLowerCase().match(/<a [-a-zA-Z"':_$@$ 0-9/=.]+>/g);
        if(_match != null && _match.length > 0)
        {
            for(i = 0; i < _match.length; i++)
            {
                var _matches = _match[i].toLowerCase().match(/"/g);
                if( (_matches != null && _matches.length %2) != 0)
                {
                    tagInError = "Illegal Link Formatting";
                    showErrorMsg = true;
                }
                _matches = _match[i].toLowerCase().match(/'/g);
                if( (_matches != null && _matches.length %2) != 0)
                {
                    tagInError = "Illegal Link Formatting";
                    showErrorMsg = true;
                }

            }
        }
    }
    if(isNotAllowTextFmt)
    {
        _match = _textArea.value.toLowerCase().match(/<(color|hl|size|quote|font|b|u|i[^mg]|em|strong|sub|super|strike|del|hr|p|\/color|\/hl|\/size|\/quote|\/font|\/b|\/u|\/i[^mg]|\/em|\/strong|\/sub|\/super|\/strike|\/del|\/hr|\/p).*?>/);
        
        if(_match != null && _match.length > 0)
        {
            tagInError = "Text Formatting tag is not allowed in a comment.";
            showErrorMsg = true;
        }
    }
    if(isNotAllowLists)
    {
        _match = _textArea.value.toLowerCase().match(/<(ul|li|ol|\/ul|\/li|\/ol).*?>/);
        
        if( _match != null && _match.length > 0)
        {
            tagInError = "List tag is not allowed in a comment.";
            showErrorMsg = true;
        }
    }
    
    if(isNotAllowImage)
    {
        _match = _textArea.value.toLowerCase().match(/<(img|\/img).*?>/);
        
        if( _match != null && _match.length > 0)
        {
            tagInError = "Img tag is not allowed in a comment.";
            showErrorMsg = true;
        }
    }
    
    if( isNotAllowMedia )
    {
        _match = _textArea.value.toLowerCase().match(/<(embed|object|param|bgsound|\/embed|\/object|\/param|\/bgsound).*?>/);
        
        if( _match != null && _match.length > 0)
        {
            tagInError = "Media tag is not allowed in a comment. ";
            showErrorMsg = true;
        }
    }
    
    if(showErrorMsg)
    {
        alert(tagInError);
        return false;
    }
    return true;
}
