﻿function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function Hide(element){
    var hide = document.getElementById(element);
    if (hide == null)
        return; 
         
    hide.style.display = 'none';
}

function Show(element){
    var show = document.getElementById(element);
    if (show == null)
        return; 
         
    show.style.display = '';
}

function HideShow(element){
    var hideshow = document.getElementById(element);
    if (hideshow == null)
        return;
         
    if (hideshow.style.display == ''){
        hideshow.style.display = 'none';
    }else{
        hideshow.style.display = '';
    }
}

function VisibleHideShow(element){
     var hideshow = document.getElementById(element);
    if (hideshow == null)
        return;
         
    if (hideshow.style.visibility == 'hidden'){
        hideshow.style.visibility = 'visible';
    }else{
        hideshow.style.visibility = 'hidden';
    }
}

function toggleVisibility(id)
{
	var id = document.getElementById(id);
	$(id).toggleClass("displayOff");
}
	
function VisibleHide(element){
    var hideshow = document.getElementById(element);
    if (hideshow == null)
        return;
         
    hideshow.style.visibility = 'hidden';
}

function VisibleShow(element){
    var hideshow = document.getElementById(element);
    if (hideshow == null)
        return;
         
    hideshow.style.visibility = 'visible';
}


function SetFocus(element){
    var txtBox = document.getElementById(element);
    if (txtBox!=null)
        txtBox.focus();
}

function Mid(str, start, len){
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}

function validateURL(url){
    var v = new RegExp(); 
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.@=#]+$"); 
    return (v.test(url));
}

function validateUsernameCharacters(username) {
    var arrru = new Array("А", "а", "Б", "б", "В", "в", "Г", "г", "Д", "д", "Е", "е", "Ё", "ё", "Ж", "ж", "З", "з", "И", "и", "Й", "й", "К", "к", "Л", "л", "М", "м", "Н", "н", "О", "о", "П", "п", "Р", "р", "С", "с", "Т", "т", "У", "у", "Ф", "ф", "Х", "х", "Ц", "ц", "Ч", "ч", "Ш", "ш", "Щ", "щ", "Ъ", "ъ", "Ы", "ы", "Ь", "ь", "Э", "э", "Ю", "ю", "Я", "я");
    var arr = new Array("A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z","_","1","2","3","4","5","6","7","8","9","0",
                        "à", "è", "ì", "ò", "ù", "À", "È", "Ì", "Ò", "Ù", "á", "é", "í", "ó", "ú", "ý", "Á", "É", "Í", "Ó", "Ú", "Ý", "â", "ê", "î", "ô", "û", "Â", "Ê", "Î", "Ô", "Û", "ã", "ñ", "õ", "Ã", "Ñ", "Õ", "ä", "ë", "ï", "ö", "ü", "ÿ", "Ä", "Ë", "Ï", "Ö", "Ü", "Ÿ", "å", "Å", "æ", "Æ", "œ", "Œ", "ç", "Ç", "ð", "Ð", "ø", "Ø", "¿", "¡", "ß");
    
    for(var i = 0;i < username.length; i++){
        var validChar = false;

        for (var j = 0; j < arrru.length; j++) {
           
            if (username.charAt(i) == arrru[j]) {
                
                
                validChar = true;
                break;
            }            
        }
        
        if(validChar)
            continue;

        for (var j = 0; j < arr.length; j++) {
            if (username.charAt(i) == arr[j]) {
                validChar = true;
                break;
            }
        }

        if (!validChar)
            return false;

    }
    
    return true;
}

function StripComments(value) {
    var regex_sc1 = /<(?:!(?:--[\s\S]*?--\s*)?(>)\s*|(?:script|style|SCRIPT|STYLE)[\s\S]*?<\/(?:script|style|SCRIPT|STYLE)>)/g;
    var nocomments = value.replace(regex_sc1, "");
    nocomments = nocomments.replace("<!--", "");
    return nocomments; 
}

function slideUp(){
   $($(this)).slideUp("slow");
}

$(document).ready(function() {
	$("#closeNotificationBox").click(function () {
		$("#notificationBox").slideUp("slow");
    });

	$("#showNotificationBox").click(function () {
		$("#notificationBox").show();
    });

	$("#showFileNotificationBox").click(function () {
		$("#fileNotificationBox").show();
    });

    // Toggle the internet services dropdown
	$("#selectService").click(function() {
		$('ul#services').slideToggle('fast');
		$('img#serviceArrowDown').toggleClass('displayOff');
		$('img#serviceArrowUp').toggleClass('displayOff');
		$("#selectService").toggleClass('shadow');
    });    

    // Toggle the language dropdown
    $('#selectLanguage').click(function() {
        $('ul#languages').slideToggle('fast');
        $('img#arrowDown').toggleClass('displayOff');
        $('img#arrowUp').toggleClass('displayOff');
        $('#selectLanguage').toggleClass('shadow');
    });

    // Toggle the login dropdown
    $('.login').click(function() {
        $('#signindropdownHolder').toggleClass('displayOff');
        $('.mainMenu').toggle();
        return false;
    });

});

function ReplaceHTMLTags(html) {
    var replacement = html.replace(/>/g, '&gt;');
    replacement = replacement.replace(/</g, '&lt;');
    replacement = replacement.replace(/&/g, '&amp;');
    return replacement;
}

function StripTags(text) {
    return text.replace(/(<([^>]+)>)/ig, "");
}

function NewLine2BR(text) {
    return text.replace(/\n/gi, '&lt;br&gt;');
}
