﻿
/* edit profile - check for list of nickname words */
function badwords(txt){
    var bad_words_array = new Array("telerik", "admin", "administrator", "sitefinity");
    var alert_arr=new Array;
    var alert_count=0;
    var compare_text=txt;
                
    for(var i=0; i<bad_words_array.length; i++) {
        for(var j=0; j<(compare_text.length); j++) {                                    
                if(bad_words_array[i]==compare_text.substring(j,(j+bad_words_array[i].length)).toLowerCase()) {
                    alert_count++;
                }
        }
    }
    return alert_count;
}

/* Escape Character */
function escapeCharacter(endOfIdToMatch) {
   	$("[id$=" + endOfIdToMatch + "]").each(function() {
   		this.value = this.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');

   	});
}

/* Cookies - Cookies.js */
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function getCookie(c_name) {
       if (document.cookie.length>0)
       {
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1)
          { 
              c_start=c_start + c_name.length+1; 
              c_end=document.cookie.indexOf(";",c_start);
              if (c_end==-1) c_end=document.cookie.length;
              return unescape(document.cookie.substring(c_start,c_end));
           } 
        }
  return "";
} 

/* Fade Effect - fadeFromYellow.js */
FadeEffect = function(elId, sred, sgreen, sblue, ered, egreen, eblue, step, propertyTOFade) {
    this.id = FadeEffect.cnt;
    this.elId = elId;
    this.sred = sred;
    this.sgreen = sgreen;
    this.sblue = sblue;
    this.ered = ered;
    this.egreen = egreen;
    this.eblue = eblue;
    this.step = step;
    this.counter = step;
    this.propertyTOFade = propertyTOFade;

    FadeEffect.objects[FadeEffect.cnt++] = this;

    this.fade = function() {
        if (this.counter > 0) {
            var red = Math.floor(this.ered * this.counter / this.step + this.sred * ((this.step - this.counter) / this.step));
            var green = Math.floor(this.egreen * this.counter / this.step + this.sgreen * ((this.step - this.counter) / this.step));
            var blue = Math.floor(this.eblue * this.counter / this.step + this.sblue * ((this.step - this.counter) / this.step));
            var el = this.elId;

            if (this.propertyTOFade == "backgroundColor")
                el.style.backgroundColor = "#" + this.toHex(red) + this.toHex(green) + this.toHex(blue);
            else
                el.style.color = "#" + this.toHex(red) + this.toHex(green) + this.toHex(blue);

            setTimeout('FadeEffect.objects[' + this.id + '].fade();', (this.step - this.counter));
            this.counter--;
        }
        else {
            var el = this.elId;
            if (this.propertyTOFade == 'backgroundColor')
                el.style.backgroundColor = "transparent";
        }
    }

    this.toHex = function(dec) {
        var hexCharacters = "0123456789ABCDEF"
        if (dec < 0)
            return "00"
        if (dec > 255)
            return "FF"
        var i = Math.floor(dec / 16)
        var j = dec % 16
        return hexCharacters.charAt(i) + hexCharacters.charAt(j)
    }
}

FadeEffect.cnt = 0;
FadeEffect.objects = new Array();

function FadeFromYellow(elId, r, g, b, propertyTOFade) {
    f = new FadeEffect(elId, r, g, b, 204, 255, 0, 35, propertyTOFade);
    f.fade();
}

function FadeProductFeatureFromYellow(elId, r, g, b, propertyTOFade) {
    f = new FadeEffect(elId, r, g, b, 255, 255, 204, 50, propertyTOFade);
    f.fade();
}

/* Web forms - check if Enter Key is pressed */
function keyPress(event, btn) { 
              
   if ((event.which && (event.which == 13)) || (event.keyCode && (event.keyCode == 13))) {     
       $("[id$=" + btn + "]").focus();  
       $("[id$=" + btn + "]").click(function(event){
            event.stopPropagation();
       });       
       return false;
   }
   else return true;
}   

/* RadEditor OnClientPasteHtml */
function OnClientPasteHtml(sender, args) {
   var commandName = args.get_commandName();
   var value = args.get_value();

   if (commandName == "FormatCodeBlock") {
        var div = document.createElement("DIV");

        Telerik.Web.UI.Editor.Utils.setElementInnerHtml(div, value);

        div.firstChild.className = "tFormatCodeBlock supportThreadCodeBlock";

        var randomnumber = Math.floor(Math.random() * 100000);
        var genId = 'highlighter_' + randomnumber;
        div.firstChild.setAttribute("id", genId);

        args.set_value(div.innerHTML);
  }  
}

/* Hide real button */
function hideBtn(validationGroupName,realBtnName,fakeBtnName) {    
    if (Page_ClientValidate(validationGroupName)) { 
            $("[id$=" + realBtnName + "]").css("display","none");
            $('#'+fakeBtnName).css("display","inline");                       
                      
    }
}

/* search autocomplete */
function hideLabel(obj, flag) {
    var lbl = obj.parentNode.getElementsByTagName('label')[0];
    if (flag) {
        lbl.style.textIndent = "-99999px";
    } else {
        if (obj.value === '')
            lbl.style.textIndent = "0px";
        else
            lbl.style.textIndent = "-99999px";
    }

}
function focusSearchField(obj) {
    var id, field;
    id = obj.getAttribute('for');
    if (id && (field = document.getElementById(id))) {
        field.focus();
    }
}

function autocomplete(control, suggestUrl) {
    control = '#' + control;
    $(control).autocomplete(suggestUrl, {
        dataType: "json",
        max: 10,
        scroll: false,
        selectFirst: false, 
        parse: function (data) {
            var array = new Array();

            for (var i = 0; i < data.results.length; i++) {
                array[i] = { data: data.results[i], value: data.results[i].name, result: data.results[i].name };
            }

            return array;
        },
        formatItem: function (row) {
            return row.name;
        }
    });
}

function searchProxy(text,productId,searchPageUrl) {
    $("#page-wrap").empty();
    var count = 0;
    $(document).ready(function () {
        $.ajax({
            type: "GET",
            url: searchPageUrl,
            data: "q=" + text + "&requiredfields=ProductId%3a" + productId,
            dataType: "xml",
            success: function (xml) {
                $(xml).find('RES').find('R').each(function () {
                    var title = $(this).find('T').text();
                    var url = $(this).find('U').text();
                    var content = $(this).find('S').text();
                    $('<div id="link_' + count + '"></div>')
                        .html('<a target="_blank" href="' + url + '">' + title + '</a><div>' + content + '</div>')
                        .appendTo('#page-wrap');
                    count++;
                });
                if (count != 0) {
                    $('#mailNoteTooltip').addClass('asTooltipBoxShow');
                }
            }
        });
    });
}

function submitAutoSuggest(controlID, buttonID) {
    controlID = '#' + controlID;
    buttonID = '#' + buttonID;
    $(controlID).keyup(function (event) {
        var code = event.keyCode;
        // handling of pressing the enter key             
        if (code == '13') {            
            $(buttonID).click();
        }
    });
}
function sortBy(divID) {
    if (divID == null) {
        divID = 'tSortBy';
    }

    divIDLinks = '#' + divID + ' a';
    $(divIDLinks).each(function () {
        var _aHref = $(this).attr('href');
        if (_aHref == '' || _aHref == null) {

            $(this).parent().addClass('selected');
        }
    });
}

/*Get URL on page*/
var docUrl = document.location.href;

function getURLItem(url, number) {
    var urlparts = url.split('/');
    var item = urlparts[number];
    return item;
}
/*Match current URL with href*/
function UrlMatch(url, matchTxt) {
    var subStr = url.substring(0, matchTxt.length);
    if (subStr == matchTxt)
        return true;
    else
        return false;
}
