function SetFocus() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i=0; i<field.length; i++) {
      if ( (field.elements[i].type != "image") &&
           (field.elements[i].type != "hidden") &&
           (field.elements[i].type != "reset") &&
           (field.elements[i].type != "submit") ) {

        document.forms[0].elements[i].focus();

        if ( (field.elements[i].type == "text") ||
             (field.elements[i].type == "password") )
          document.forms[0].elements[i].select();

        break;
      }
    }
  }
}

function confirmLink(theLink, confirmMsg)
{
    var is_confirmed = confirm(confirmMsg);

    return is_confirmed;
}

function CheckAll(id, checked) {
    count = document.all.length;
    for (i=0; i<count;i++) {
        if (document.all[i].id == id)
            document.all[i].checked = checked;
    }
}

function CheckAllSub(id, checked) {
    var str = "";
    var count = document.all.length;
    var len = id.length;

    for (i=0; i<count;i++) {
        str = document.all[i].id;

        if (document.all[i].id)
        if (document.all[i].id != 'undefined')
        if (str.substring(0,len) == id)
            document.all[i].checked = checked;
    }
}

function ShowAll(id, bShow) {
    count = document.all.length;
    for (i=0; i<count;i++) {
        if (document.all[i].id == id)
            document.all[i].style.display = (bShow ? 'block' : 'none');
    }
}

function object_show(name, id)
{
    count = document.all.length;
    for (i=0; i<count;i++) {
        if (document.all[name+i]) {
            document.all[name+i].style.display='none';
        }
    }
    if (document.all[name+id]) {
        document.all[name+id].style.display='block';
    }
}

function checkFields(formobj, $req, $desc){
    // name of mandatory fields
    var fieldRequired = $req;
    // field description to appear in the dialog box
    var fieldDescription = $desc;
    // dialog message
    var alertMsg = "Пожалуйста заполните следующие поля:\n";

    var l_Msg = alertMsg.length;

    for (var i = 0; i < fieldRequired.length; i++){
        var obj = formobj.elements[fieldRequired[i]];
        if (obj){
            switch(obj.type){
            case "select-one":
                if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "select-multiple":
                if (obj.selectedIndex == -1){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            case "text":
            case "textarea":
                if (obj.value == "" || obj.value == null){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
                break;
            default:
                if (obj.value == "" || obj.value == null){
                    alertMsg += " - " + fieldDescription[i] + "\n";
                }
            }
        }
    }

    if (alertMsg.length == l_Msg){
        return true;
    }else{
        alert(alertMsg);
        return false;
    }
}

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}

/* Открывает окно для визуального редактирования текста */
var currentTextArea = null;
function openEditor(textarea) {

    // location of edit.php file:
    var editFile = '/spaw2/scripts/edit.php';

    currentTextArea = textarea;

    var edit = window.open(editFile, 'editorWindow', 'width=720, height=470, status=yes, scrollbars=yes, resizable=yes');
    edit.focus();
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


$(document).ready(function(){
    $(".border").removeAttr("width");
    $(".border").removeAttr("height");

    var width = $(".border").width();
    var height = $(".border").height();
        if(width > '150') {
            width = '150';
        }
        if(height > '150') {
            height = '150';
        }

        if(width > height) {
            $(".border").attr("width", width);
        } else {
            $(".border").attr("height", height);
        }
    console.log(width);
});

