// Set global boolean variables describing browser and version
var Nav4 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 4))
var Nav4plus = ((navigator.appName == "Netscape") && (parseFloat(navigator.appVersion) > 4.04))
var Nav5 = ((navigator.appName == "Netscape") && (parseInt(navigator.appVersion) == 5))
var IE4 = ((navigator.appName.indexOf("Microsoft") > -1)) && (parseInt(navigator.appVersion) == 4);
var IE5 = ((navigator.appName.indexOf("Microsoft") > -1)) && (parseInt(navigator.appVersion) == 5);
var anyIE = (IE4 || IE5)
var anyNav = (Nav4 || Nav4plus || Nav5)

//Directory variables used to construct paths as needed
// var image_dir = opener.image_dir;
var jslib_dir = "jslib/";

//Code to either determine the baseHref based upon this location, or to write whatever has been loaded
//into a variable "baseHref" in the top page
if (top.baseHref) 
{
  var hrefLocation = top.baseHref + "";
}
else
{
  var hrefLocation = document.location + "";
  //if (hrefLocation.indexOf(".") > -1) hrefLocation = hrefLocation.substr(0,hrefLocation.lastIndexOf("/")+1);
  //removing the file name makes href='#' revert to the parent directory, rather than the top of any given page...
  //so, can't be done.  Seems OK though, since a filename in the base HREF appears to be ignored anyway
} 

//special code to write a base-href into any page loaded as a frame in Netscape
if (anyNav && self.location != top.location) document.write("<BASE HREF='"+hrefLocation+"'>")

// Common routines returning source html for empty "spacer" frames - 
function blankframe() {
  return "<HTML><HEAD><BASE HREF='"+hrefLocation+"'><link rel=stylesheet type='text/css' href='" + image_dir + "cabo_styles.css'></HEAD><BODY class=appswindow ></BODY></HTML>" }

function grayframe() {
        return "<HTML><HEAD><BASE HREF='"+hrefLocation+"'><link rel=stylesheet type='text/css' href='" + image_dir + "cabo_styles.css'></HEAD><BODY class=panel onFocus='if (top.checkModal) top.checkModal()'><img src='" + image_dir + "pixel_gray5.gif' width=10></BODY></HTML>" }



// Common routine used by all constructor libraries to parse and trim name:value; pairs
function parsevalues (arg, obj, validargs) {
  while (arg.charAt(0) == " ") { arg = arg.substring(1) }
  while (arg.charAt(arg.length-1) == " ") { arg = arg.substr(0,arg.length-1); }  
  var name_arg = arg.substring(0,arg.indexOf(":"));
  var search_arg = name_arg + ":";
  var value_arg = arg.substring(arg.indexOf(":")+1);
  while (value_arg.charAt(0) == " ") { value_arg = value_arg.substring(1) }
  while (value_arg.charAt(arg.length-1) == " ") { value_arg = value_arg.substr(0,value_arg.length-1); }  
  if (validargs.indexOf(search_arg) > -1) obj[name_arg] = value_arg;
}

var bssDialog;
var bssDialogNumber = -1;
var fillfield;
var namefield;
//Opens a new window in a dialog manner
function bssOpenDialog(dlgNumber,href,width,height,fields,names,paramStr) {
  var openleft=10;
  var opentop=10;
  if ( dlgNumber == bssDialogNumber && typeof(bssDialog) != "undefined"
      && !bssDialog.closed) 
  {
    bssDialog.focus();
  } else {

    if (typeof(bssDialog) != "undefined" && !bssDialog.closed) {
      bssDialog.close();
    }

    var attr = "resizable=yes,scrollbars=yes,modal=yes,width=" + 
        width + ",height=" + height;
    if (Nav4) {
      // center on the main window
      openleft = parseInt(window.screenX + ((window.outerWidth - width)-50 ),10);
      opentop = parseInt(window.screenY + ((window.outerHeight - height)-50),10);
      attr += ",screenX=" + openleft + ",screenY=" + opentop;
    } else {
      // best we can do is center in screen
      openleft = parseInt(((screen.width - width)-((screen.width - width)/2) ),10);
      opentop = parseInt(((screen.height - height)-((screen.height - height)/2) ),10);

      attr += ",left=" + openleft + ",top=" + opentop;
    }

    bssDialog = window.open(href, window.name + 'bss' + dlgNumber, attr);
    fillfield = fields;
    namefield = names;

    if (paramStr) {window.afterfunc = paramStr}
    if (bssDialog.opener == null) {
      bssDialog.opener = self; 
    }

    bssDialogNumber = dlgNumber;
  }
}

//Opens a new window in a dialog manner for grid.xsl
function gridOpenDialog(href,width,height) {
  var openleft=10;
  var opentop=10;
  var dlgNumber = 0; 

//  alert( "href = " + href + "<br>width = " + width + "<br>height = " + height ); 
  
  if ( dlgNumber == bssDialogNumber && typeof(bssDialog) != "undefined"
      && !bssDialog.closed) 
  {
    bssDialog.focus();
  } else {

    if (typeof(bssDialog) != "undefined" && !bssDialog.closed) {
      bssDialog.close();
    }

    var attr = attr = "resizable=yes,scrollbars=yes,modal=yes,width=" + 
        width + ",height=" + height;
    if (Nav4) {
      // center on the main window
      openleft = parseInt(window.screenX + ((window.outerWidth - width)-50 ),10);
      opentop = parseInt(window.screenY + ((window.outerHeight - height)-50),10);
      attr += ",screenX=" + openleft + ",screenY=" + opentop;
    } else {
      // best we can do is center in screen
      openleft = parseInt(((screen.width - width)-((screen.width - width)/2) ),10);
      opentop = parseInt(((screen.height - height)-((screen.height - height)/2) ),10);

      attr += ",left=" + openleft + ",top=" + opentop;
    }

    bssDialog = window.open(href, window.name + 'bss' + dlgNumber, attr);
//    fillfield = fields;
//    namefield = names;

    if (bssDialog.opener == null) {
      bssDialog.opener = self; 
    }

    bssDialogNumber = dlgNumber;
  }
}


// ��������� IFrame, ������� ������� "��������"
function loadIFrame(src, id) {
  var strHTML = "<html><body><p>��������...</p></body></html>";

  if (document.getElementById(id).document) { // for IE
	document.getElementById(id).document.innerHTML = strHTML;
 } else { // for Mozilla Firefox
    document.getElementById(id).innerHTML = strHTML;
  }
  document.getElementById(id).src = src;
}

// ����� � IFrame "��������" � ���
function loadIFrame2(src, id) {
  var strHTML = "<html><body><p>��������...</p></body></html>";

  if (document.getElementById(id).document) { // for IE
    document.getElementById(id).document.innerHTML = strHTML;
  } else { // for Mozilla Firefox
    document.getElementById(id).innerHTML = strHTML;
  }
}

// �������� ������ � ����������� ��������� IFrame
function getIFrameDocument(aID) {
  var rv = null;
  // if contentDocument exists, W3C compliant (Mozilla)
  if (document.getElementById(aID).contentDocument){
    rv = document.getElementById(aID).contentDocument;
  } else {
    // IE
    rv = document.frames[aID].document;
  }
  return rv;
}


// �������� ���
function checkTaxCode(taxcodeField, kppField, juridicalLen) {
   if(taxcodeField.value.length != juridicalLen) {
    kppField.value="";
    kppField.disabled=true;
    kppField.className="readonly";
  }else {
    kppField.className="";
    kppField.disabled=false;
  }
}


function checkKppCodeLenght(kppField, kkpplenght){
    var kppFieldLength = kppField.value.length;
    if(kppFieldLength != kkpplenght){
      kppField.value = "";
      alert("��� ������ " + kppFieldLength  + " ������ �� �������� ���������� ��� ����������� ���.");
      kppField.focus();
    }
}


function checkTaxCodeLenght(taxcodeField, juridical, natural) {
  var taxcodeFieldLength = taxcodeField.value.length;
  if(taxcodeFieldLength  != juridical && taxcodeFieldLength  != natural){
      taxcodeField.value = "";
      alert("��� ������ " + taxcodeFieldLength  + " ������ �� �������� ���������� �� ��� ����������, �� ��� ����������� ���.");
      }
  }


// ������� �������� ����. ���� ���� �� ����� �������� ����� ���� � ������ � 20 ��������,
// �� �� ��������� "����������". � ��������� ������ - "�� ����������"
// ���������:
// accountField - input ��� �������� ������������ ��������
// fieldsArray - ������ ����� �� ������� ��������� ���������� ������� ����� ��������
//              (���������, �������� � �.�.) � ������ ���� ���� �������� "�� ����������"
//              ������ ������������ ������� �� ���������� ������� ������� ������ ����������
//              ��������� ���������� ������������ � �������
// action - ������ ���� ����� 'disable' �� �������� disable � ��� ��������� ����������� � true
//          � ��������� ������ value ������� �������� �������� �������� ���������� � action
// warning - ����� ���������������� ��������� ���� ���� �������� �� ����������
// accountType - ������� ���� � ������� �������� ��� ����� (���������������� "�� �����������
// �����" = '2', '4', '' ��� ��������� �������� ����� ���� - "���������� �����"



function checkAccount(accountField, fieldsArray, action, warning, accountType) {
  var accountLenght = parseInt(accountField.value.length);
  if(accountLenght == 0){
    accountType.value='2';
    return true;
  }

  if(accountLenght == 20) {
    for(j=0; j <= accountLenght; j++){
      if(accountField.value.charCodeAt(j) > 57 || accountField.value.charCodeAt(j) < 48){
                  if(!isEmptyFields(fieldsArray)){
/*                  var c = confirm(warning);
                  if(c){*/
                    peformAction(fieldsArray, action);
                    accountType.value='3';
                    return false;
/*                    }
                  else{
                    accountField.focus();
                    accountType.value='2';

                    return false;
                    }*/
                 }
                 else{
                    accountType.value='3';
                    return false;
                 }
        }
    }
    accountType.value='2';
    return true;
   }else {
                if(!isEmptyFields(fieldsArray)){
                  /*var c = confirm(warning);
                  if(c){*/
                    peformAction(fieldsArray, action);
                    accountType.value='3';
                    return false;
                    /*}
                    else{
                    accountField.focus();
                    accountType.value='2';
                    return false;
                    } */
                 }
                 else{
                    accountType.value='3';
                    return false;
                 }
  }
}

function peformAction(fieldsArray, action){
  var fieldsCount = parseInt(fieldsArray[0]);
  for(i=1; i <= fieldsCount; i++){
    if(action != "disable")
      fieldsArray[i].value = '';
    else
      fieldsArray[i].disabled = true;
  }
}

function isEmptyFields(fieldsArray){
  var fieldsCount = parseInt(fieldsArray[0]);
  for(i=1; i <= fieldsCount; i++){
    if(fieldsArray[i].value){
      if(fieldsArray[i].value != '')
          return false;
    }
  }
  return true;
}

function validate2dates(date1, date2) {
  var oDate1, oDate2;
  
  if(date1) {
	  if(!checkDate(date1)) return false;
	  oDate1 = strToDate(date1);
  }
  
  if(date2) {
	  if(!checkDate(date2)) return false;
	  oDate2 = strToDate(date2);
  }

  if(oDate1 && oDate2 && oDate1 > oDate2) {
    return false;
  } else {
    return true;
  }
}

function checkDate(inputStr) {
	var reg=/\d\d\.\d\d\.\d\d\d\d/;
	var rez = reg.test(inputStr);
	return rez ;
}

function strToDate(s) {
    var a = s.split(".")
    return new Date(a[2], a[1], a[0]);
}
