var expires_period=new Date(new Date().getTime() + (365 * 24 * 60 * 60 * 1000));
function setCookie(name, value, expires, path, domain, secure) {
     document.cookie = name + "=" + escape(value) +
     ((expires) ? "; expires=" + expires.toGMTString() : "") +
     ((path) ? "; path=" + path : "") +
     ((domain) ? "; domain=" + domain : "") +
     ((secure) ? "; secure" : "");
}

function getCookie(name) {
     var prefix = name + "=" ;
     var cookieStartIndex = document.cookie.indexOf(prefix) ;
     if (cookieStartIndex == -1)
             return null;
     var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length) ;
     if (cookieEndIndex == -1)
             cookieEndIndex = document.cookie.length;
     return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function getOriginalElement(element, index) {
   var result = null;
   if (element && element[0]!=null && element.tagName==null)
      if (index!=null && index<element.length)
          result = element[index];
      else
          result = element[element.length-1];
   return result;
}

function getElementCount(element) {
   if (!element) return 0;
   if (element[0]!=null && element.tagName==null)
       return element.length;
   return 1;
}

function getElementIndex(collection, element) {
   for (var i=0; i<collection.length; i++)
       if (collection[i]==element)
           return i;
   return null;
}

function getWidth(object) {
    return parseInt(object.offsetWidth);
}

function getHeight(object) {
    return parseInt(object.offsetHeight);
}

function getClientRect(object) { 
    return { left:   parseInt(object.offsetLeft), 
             top:    parseInt(object.offsetTop), 
             right:  parseInt(object.offsetLeft) + parseInt(object.offsetWidth), 
             bottom: parseInt(object.offsetTop) + parseInt(object.offsetHeight), 
             width:  parseInt(object.offsetWidth), 
             height:  parseInt(object.offsetHeight), 
             toString: function () { return "left: "   + this.left + "\n" + 
                                            "top: "    + this.top + "\n" + 
                                            "right: "  + this.right + "\n" + 
                                            "bottom: " + this.bottom + "\n" + 
                                            "width: "  + this.width + "\n" + 
                                            "height: " + this.height 
                                   } 
           }; 
} 

function getBodySize(wnd) {
    wnd = wnd ? wnd : window;
    var width = 0;
    var height = 0;
    //Non-IE
    if (typeof(wnd.innerWidth) == 'number') {
        width = wnd.innerWidth;
        height = wnd.innerHeight;
    } else
    //IE 6+ in 'standards compliant mode'
    if (wnd.document.documentElement &&
       (wnd.document.documentElement.clientWidth || wnd.document.documentElement.clientHeight)) {
        width = wnd.document.documentElement.clientWidth;
        height = wnd.document.documentElement.clientHeight;
    } else
    //IE 4 compatible
    if (wnd.document.body && (wnd.document.body.clientWidth || wnd.document.body.clientHeight)) {
        width = wnd.document.body.clientWidth;
        height = wnd.document.body.clientHeight;
    }
    return { width:  width,
             height: height,
             toString: function () { return "width: "  + this.width + "\n" + 
                                            "height: " + this.height
                                   } 
           }; 

}

function trim(str) {
    if (str == null) return null;
    while (str.indexOf(" ") == 0)
        str = str.substring(1, str.length);
    while (str.lastIndexOf(" ") == str.length-1 && str != '')
        str = str.substring(0, str.length-1);
    return str;
}

function fullTrim(str) {
    if (str == null) return null;
    while ((str.indexOf(" ") == 0) || (str.indexOf("\n") == 0) || (str.indexOf("\r") == 0))
        str = str.substring(1, str.length);
    while (((str.lastIndexOf(" ") == str.length-1) || 
            (str.lastIndexOf("\n") == str.length-1) ||
            (str.lastIndexOf("\r") == str.length-1)) && str != '')
        str = str.substring(0, str.length-1);
    return str;
}

function mailto(address, subject, body) {
    location.replace("mailto:" + address + "?" +
                     (subject ? ("subject=" + subject + (body ? ("&body=" + body) : "")) : ""));
}

function addEvent(obj, evType, fn) { 
   if (obj.addEventListener) { 
       obj.addEventListener(evType, fn, false); 
       return true; 
   } else
   if (obj.attachEvent) { 
       var r = obj.attachEvent("on"+evType, fn); 
       return r; 
   } else
       return false; 
}

function raiseContent(frame, content) {
    var doc = frame.document;
    doc.clear();
    doc.open ("text/html");
    doc.writeln ('<html>' +
                 '<head>' +
                   '<meta http-equiv="content-type" content="text/html; charset=Windows-1251">' +
                   '<link rel="stylesheet" type="text/css" href="../styles/content.css">' +
                 '</head>' +
                 '<body>' +
                   '<div class="div">' +
                     '<table cellspacing="0" cellpadding="0" border="0" width="100%">' + 
                       '<tr>' +
                         '<td width="100%" style="padding:10px">' +
                           content +
                         '<td>' +
                       '</tr>' +
                     '</table>' +
                   '<div>' +
                 '</body>' +
                 '</html>');
    doc.close ();
}

function clickLink(event, anchor, width, height) {
    event = window.event ? window.event : event;
    event.returnValue = false;
    if (event.preventDefault)
        event.preventDefault();
    
    if (width > 0 && height > 0)
        showWindow(anchor.href, width, height);
    else
        showWindow(anchor.href);
    return false;
}

function skipEvent(event) {
    event = window.event ? window.event : event;
    event.returnValue = false;
    if (event && event.preventDefault)
        event.preventDefault();
    return false;
}

function exist(element, message, noFocus) {
    var result = false;
    var list = element.tagName==null;
    if (list && element.length) {
        for (var i=0; i<element.length; i++)
            if (result = element[i].checked)
               break;
    } else
        result = element.checked;

    if ((!result && list && element.length) || ((!list || element.length==null) && trim(element.value)=="")) {
        alert(message);
        if (!noFocus)
            if (element.length==null) {
                if (element.focus)
                    element.focus();
            } else
                if (element[0].focus)
                    element[0].focus();
    } else
        result = true;
    return result;
}

function getCollection(element) {
    var result = element;
    if (element.length==null)
        result = new Array(element)
    return result;
}


function checkEmail(element, message, noFocus) {
    var result = true;
    if (element.value!="")
        if (element.value!=trim(element.value) || !validateEmail(trim(element.value))) {
            if (!noFocus && element.focus)
                element.focus();
            alert(message)
            result = false;
        }
    return result;
}

function _keyDown(event) {
    return event.keyCode == 35 || event.keyCode == 36 ||
           event.keyCode == 37 || event.keyCode == 39 ||
           event.keyCode == 9 || event.keyCode == 13 ||
           event.keyCode == 27;
}

function clearEvent(event, func) {
    var evt = event ? event : window.event;            
    if (func) {
       value = func(evt);
       evt.returnValue = value; 
       if (!value) {
           if(evt.preventDefault) evt.preventDefault();
           if(evt.stopPropagation) evt.stopPropagation();
       }
    } else { 
        evt.returnValue = false;
        if(evt.preventDefault) evt.preventDefault();
        if(evt.stopPropagation) evt.stopPropagation();
    }
}

function extractFileName(name) {
    var result = null;
    var linux = new RegExp("^(?:/[^/]+)*/(.*)$", "g");
    var windows = new RegExp("^(?:.:\\\\)(?:[^\\\\]+\\\\)*(.*)$", "g");
    if (name.charAt(0)=="/") {
        linux.exec(name);
        result = RegExp.$1;
    } else
    if (name.charAt(1)==":") {
        windows.exec(name);
        result = RegExp.$1;
    } else 
        result = name;
    return result;
}

function extractFileExtention(name) {
    return name.substring(Math.min(name.lastIndexOf(".")+1, name.length), name.length);;
}

function convertUserName(firstName, lastName, midName){
    if(lastName == "") return;

    if (firstName != "") 
        lastName += " " + firstName.substr(0,1) + ".";
    else
        return lastName;

    if (midName != "") lastName += " " + midName.substr(0,1) + ".";

    return lastName;
}


if (!Array.prototype.remove) {
    Array.prototype.remove = 
        function (item) {
            for (var i=0; i<this.length; i++)
                if (item == this[i]) this.splice(i, 1);
        }
}

if (!Array.prototype.contains) {
    Array.prototype.contains = 
        function (item) {
            for (var i=0; i<this.length; i++)
                if (item == this[i]) return true;
            return false;
        }
}

if (!Array.prototype.removeAll) {
    Array.prototype.removeAll = 
        function () {
            for (var i=this.length; i>=0; i--)
                this.remove(this[i]);
        }
}
