/********************************************************
 * Created by Shirokovskiy D. jimmy.webstudio@gmail.com
 *
 */

/** ********** Created by Jimmy 2008.02.07 17:11 **************************
* Определить браузер
**************************************************************************/
var isDOM = document.getElementById; //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
var isOpera = isOpera5 = window.opera && isDOM; //Opera 5+
var isOpera6 = isOpera && window.print; //Opera 6+
var isOpera7 = isOpera && document.readyState; //Opera 7+
var isMSIE4 = document.all && document.all.item && !isOpera; //Microsoft Internet Explorer 4+
var isMSIE = isDOM && isMSIE4; //MSIE 5+
var isNetscape4 = document.layers; //Netscape 4.*
var isMozilla = isDOM && navigator.appName == "Netscape"; //Mozilla или Netscape 6.*


/**
 * Проверка корректности Email-адреса
 */
function isEmail(str) {
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

/** ********** Created by Jimmy 2006.04.23 16:35 **************************
* Подтверждение на удаление, с переходом по ссылке
**************************************************************************/
function confirmDelete(url, msgtxt) {
  if( !msgtxt ) {
    msgtxt = '';
  }
  if( confirm("Вы действительно хотите УДАЛИТЬ запись?"+"\n"+msgtxt) ) {
    if ( url != '' ) {
      location.href = ''+url;
    }
  }
}

/** ********** Created by Jimmy 2008.05.06 15:23 **************************
* Подтверждение действия, с переходом по ссылке
**************************************************************************/
function confirmAction(url, msgtxt) {
  if( !msgtxt ) {
    msgtxt = '';
  }
  if( confirm( msgtxt+"\n"+"Вы уверены?" ) ) {
    if ( url != '' ) {
      location.href = ''+url;
    }
  }
}


/**
 * Открыть окно
 */
function jsShowModal(strPagePath, intWidth, intHeight, stScroll, stResize, strWinName) {
  var scroll = stScroll;
  var resize = stResize;

  if ( strWinName == '' )
    strWinName = 'fcmModal';

  if ( resize == '' )
    resize = 'yes';

  var top=0, left=0;
  if(intWidth > screen.width-10 || intHeight > screen.height-28) {
    scroll = 'yes';
  }

  if(intHeight < screen.height-28)
    top = Math.floor((screen.height - intHeight)/2-14);

  if(intWidth < screen.width-10)
    left = Math.floor((screen.width - intWidth)/2);

  intWidth = Math.min(intWidth, screen.width-10);
  intHeight = Math.min(intHeight, screen.height-28);
  window.open(strPagePath, strWinName, 'menubar=no, scrollbars='+scroll+', resizable='+resize+', width='+intWidth+', height='+intHeight+', left='+left+', top='+top);
}

/** ********** Created by Jimmy 2008.02.06 15:43 **************************
* Скрыть объект. Показать объект
**************************************************************************/
function doHideObj( objID ) {
  document.getElementById( objID ).style.display = 'none';
}

function doShowObj( objID, mode ) {
  if ( typeof(mode) == 'undefined' || mode == null || mode == '' ) mode = 'block';
  document.getElementById( objID ).style.display = '' + mode;
}

function hideBlock( obj_id ) {
  if ( document.getElementById( obj_id ) ) {
    document.getElementById( obj_id ).style.display = 'none';
  }
}

function showBlock( obj_id, type ) {
  if (type == undefined) type = 'block';
  if ( document.getElementById( obj_id ) ) {
    document.getElementById( obj_id ).style.display = type;
  }
}

function HideShow ( el ) {
  if (document.getElementById(el).style.display != 'none') {
    document.getElementById(el).style.display = 'none';
  } else {
    document.getElementById(el).style.display = 'block';
  }
}

function reloadPage() {
  window.location.reload();
}

function setValue( id, val ) {
  document.getElementById( id ).value = val;
}

function ElementDisabled( obj, status ) {
  obj.disabled = status;
}

function ReturnValueCbx( obj ) {
  obj.checked = !obj.checked;
}

function _getTime() {
  now = new Date();
  hours = now.getHours();
  minuts = now.getMinutes();
  if (hours<10) hours="0"+hours;
  if (minuts<10) minuts="0"+minuts;
  curTime = hours+':'+minuts;
  return curTime;
}

function _getDate() {
  now = new Date();
  day = now.getDate();
  month = now.getMonth()+1;
  year = now.getYear();
  if (day<10) day="0"+day;
  if (month<10) month="0"+month;
  curDate = day+'-'+month+'-'+year;
  return curDate;
}

function getSelectBoxValue ( tagID ) {
  if (document.getElementById( tagID ))
    return document.getElementById( tagID ).options[document.getElementById( tagID ).selectedIndex].value;
  else
    alert ('Объект не найден!');
  return false;
}

function getValue( tagID ) {
  if (document.getElementById( tagID ))
    return document.getElementById( tagID ).value;
  else
    alert ('Объект не найден!');
  return false;
}

function okExec( obj, evt ) {
  evt = (evt) ? evt : ((window.event) ? window.event : "")
  if (evt) {
    if (evt.keyCode) strKeyCode = evt.keyCode;
    else
    if (evt.which) strKeyCode = evt.which;

    if (strKeyCode) {
      if (strKeyCode == 13 || strKeyCode == 27 || strKeyCode == 16 || strKeyCode == 17 || strKeyCode == 18 ||
          strKeyCode == 37 || strKeyCode == 38 || strKeyCode == 39 || strKeyCode == 40 ) {
        return false;
      }
    }
  }

  return true;
}

function clearBlock( name ) {
  if ( document.getElementById(name) ) {
    var obj = document.getElementById(name);
    obj.innerHTML = '';
    obj.style.display = 'none';
  }
}

function clearTextA( id ) {
  document.getElementById(id).innerText =
  document.getElementById(id).innerHTML = '';
}

function checkUncheck( theElements, tagControllerID ) {
  var formElements = theElements.split(',');
  if ( tagControllerID != '' && tagControllerID != undefined ) {
    var theController = document.getElementById(tagControllerID);
  }

  for( var z=0; z<formElements.length; z++ ) {
    theItem = document.getElementById(formElements[z]);

    if ( theItem.type ) {
      if ( theItem.type == 'checkbox' ) {
        if ( theController && theController.type == 'checkbox') {
          theItem.checked = theController.checked;
        } else {
          theItem.checked = true;
        }
      }
    } else {
      theInputs = theItem.getElementsByTagName('input');

      for( var y=0; y<theInputs.length; y++) {
        if( theInputs[y].type == 'checkbox' && theInputs[y].id != theController.id ) {
          if ( theController && theController.type == 'checkbox') {
            theInputs[y].checked = theController.checked;
          } else {
            theInputs[y].checked = true;
          }
        }
      }
    }
  }
}
