/*

WEBTEC.CZ Common JavaScript Functions version 1.2
      Copyright © 1999 - 2006 Daniel Kejval
             daniel.kejval@webtec.cz

*/

var fpTextSelect, objIDBox, objNameBox;

function filepick(inputobj, mask, lm) {
  fpTextSelect = inputobj;
  
  FilePickDialog = window.open('../lib/WebFilePick/filepick.asp?mask=' + mask + '&lm=' + lm, 'file_pick', 'height=315,resizable=yes,width=500');
  if (FilePickDialog.opener == null) { 
    FilePickDialog.opener = self
  }     
}

function wtcCallBack(FuncName, Params) {
  wtcCallBackIFrame = document.getElementById('wtcCallBackIFrame');
  if (!wtcCallBackIFrame) {
    wtcCallBackIFrame = document.createElement("IFRAME");
    wtcCallBackIFrame.id = 'wtcCallBackIFrame';
    wtcCallBackIFrame.style.display = 'none';
    document.body.appendChild(wtcCallBackIFrame);
  }
  wtcCallBackIFrame.src = '/CallBack.aspx?f=' + FuncName + '&p=' + Params;
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}

function ValidateRequiredValue(source, arguments) {
  arguments.IsValid = (arguments.Value != '');
}

function ValidateBiggerZeroValue(source, arguments) {
  arguments.IsValid = (arguments.Value > 0);
}

function ValidateChecked(source, arguments) {
  arguments.IsValid = arguments.checked;
}

function ConfirmCheckedDelete() {
  return confirm('Are you sure you want to delete the checked records?');
}

function ConfirmDelete(strRecordName) {
  return confirm('Are you sure you want to delete the current record ' + strRecordName + '?');
}

function JG_Click(gridex, button, x, y) {
  if(gridex.HitTest() == "Cell") {
    var row = gridex.getGridEXRow();
    if(row.getRowType() == "Record") {
      var cell = row.getCellSelected();
      if(cell.getColumn().getKey() == "Delete") {
        if(!ConfirmDelete(row.getCellByIndex(1).getText())) {
          window.event.returnValue = false;
          window.event.cancelBubble = true;
        }
      }
    }
  }
}

function JG_DeletingRecords(row) {
  return !ConfirmDelete(row.getCellByIndex(1).getText());
}

function getRadioValue(radioName) {
  var collection;
  collection = document.getElementsByName(radioName);
  for (i = 0; i < collection.length; i++) {
    if (collection[i].checked)
    return(collection[i].value);
  }
}

function Check_Set(check, value) {
  if (check) {
    check.checked = value; // pokud je jen 1 radek
    for (i = 0; i < check.length; i++) 
      check(i).checked = value;
  }
}

function GetCheckIDList(check) {
  if (check) {
    var valueList = '';
    if (check.checked) valueList = check.value; // pokud je jen 1 radek
    for (i = 0; i < check.length; i++) 
      if (check(i).checked) valueList += (valueList.length == 0 ? '' : ', ') + check(i).value;
    return valueList
  }
}

function ReturnIDCheck(check) {
  parent.opener.window.objIDBox.value = GetCheckIDList(check);
  top.close();
}

function ReturnID(intID, strName) {
  parent.opener.window.objIDBox.value = intID;
  parent.opener.window.objNameBox.innerText = strName;
  top.close();
}

function PopupSelect(strRecord, strMode, objID, objName, strIDList, strParams) {
  objIDBox = objID;
  objNameBox = objName;
  if (strParams.search('w2') == -1) strWidth = '300'; else strWidth = '600';
  objPopupSelect = window.open('../Controls/Select' + strRecord + '.aspx?mode=' + strMode + '&amp;idlist=' + strIDList + '&amp;params=' + strParams, 'Select' + strRecord, 'height=350,resizable=yes,width=' + strWidth);
  if (objPopupSelect.opener == null) { 
    objPopupSelect.opener = self
  }
}

function Go(strURI) {
  window.location.href = strURI;
}

function GoP(strURI) {
  parent.location.href = strURI;
}

function GoR(strURI, strFrom) {
  window.location.href = '../public/redirect.asp?from=' + strFrom + '&amp;url=' + strURI;
}

function Open(strURI) {
  window.open(strURI);
}

function OpenR(strURI, strFrom) {
  window.open('../public/redirect.asp?from=' + strFrom + '&amp;url=' + strURI);
}

function article_open(article_key, height, width) {
  window.open('../public/article.asp?fullscreen=1&key=' + article_key, '', 'height=' + height + ',width=' + width);
}

function InvertDisplay(id) {
  if (document.getElementById(id).style.display == 'none') {
    document.getElementById(id).style.display = '';
  } else {
    document.getElementById(id).style.display = 'none';
  }
}

function hideUnder(elmID, objAbove) {
  var objCol = document.getElementsByTagName(elmID);
  for (i = 0; i < objCol.length; i++) {
    obj = objCol[i];
    obj.style.visibility = "hidden";
  }
}

function showUnder(elmID, objAbove) {
  var objCol = document.getElementsByTagName(elmID);
  for (i = 0; i < objCol.length; i++) {
    obj = objCol[i];
//    if (! obj || ! obj.offsetParent)
//      continue;
    obj.style.visibility = "";
  }
}

function tabChange(ClickTab, FormName) {
//  try {
    strGroupIDArray = document.getElementById('GroupIDArray' + FormName).value;
    if (strGroupIDArray) {
      eval('var GroupIDArray = new Array(' + strGroupIDArray + ');')
    }
    ActiveTab = document.getElementById('ActiveTab' + FormName).value;
    if (ActiveTab != ClickTab) {
      if (ActiveTab == '0') {
        for (i = 0; i < GroupIDArray.length; i++) {
          document.getElementById('TabBody' + FormName + GroupIDArray[i]).style.display = 'none';
        }
      } else {
        document.getElementById('TabBody' + FormName + ActiveTab).style.display = 'none';
      }
      document.getElementById('Tab' + FormName + ActiveTab).className = 'tableBorderTabB';
      document.getElementById('AboveTab' + FormName + ActiveTab).className = 'tableBorderB';
      document.getElementById('ActiveTab' + FormName).value = ClickTab;
    }
    if (ClickTab == '0') {
      for (i = 0; i < GroupIDArray.length; i++) {
        document.getElementById('TabBody' + FormName + GroupIDArray[i]).style.display = '';
      }
    } else {
      document.getElementById('TabBody' + FormName + ClickTab).style.display = '';
    }
    document.getElementById('Tab' + FormName + ClickTab).className = 'tableBorderTabBA';
    document.getElementById('AboveTab' + FormName + ClickTab).className = 'tableBorder';
//  } catch(e) {} finally {}
}

function sheetAction(strRecordAction, strCopyValue) {
  if (strCopyValue != '') frmSheet.elements(strCopyValue).value = frmSelected.elements(strCopyValue).value;
  frmSheet.RecordAction.value = strRecordAction;
  frmSheet.submit();
}

function FormatNumber(Expression, NumDigitsAfterDecimal, GroupDigits) {
  Expression = parseFloat(Expression.toString().replace(',', '.'));
  if (NumDigitsAfterDecimal > 0) {
    Expression *= Math.pow(10, NumDigitsAfterDecimal);
  }
  Expression = Math.round(Expression);
  if (NumDigitsAfterDecimal > 0) {
    Expression /= Math.pow(10, NumDigitsAfterDecimal);
  }
  return Expression.toString().replace('.', ',');
}

function IsValidEmail(email){
  return email.search(/^[^.]+(\.[^.]+)*@([^.]+[.])+[a-z]{2,3}$/) == 0;
}

function ErrorAlert(strText) {
  alert(strText);
  return true;
}

function wtcCharCount(string1, string2) {
  intCharCount = 0;
  for (i = 0; i < string1.length; i++) {
    if(string2.indexOf(string1.charAt(i)) > -1)
      intCharCount++;
  }
  return intCharCount;
}

function wtcInStr(string1, string2) {
  blnInStr = true;
  for(i = 0; blnInStr && i < string2.length; i++) {
    blnInStr = false;
    for(j = 0; !blnInStr && j < string1.length; j++) {
      blnInStr = blnInStr || string2.charAt(i) == string1.charAt(j);
    }
  }
  return blnInStr;
}

function wtcRound(numeric_expression, length) {
  tmpNum = Math.round(numeric_expression * 100) / 100;
  tmpStr = '' + tmpNum + '';
  if (tmpStr.indexOf(".", 0) == -1) {
    tmpNum = tmpNum + ".00"
  }
  if (tmpStr.length - (tmpStr.indexOf(".", 0)) == 2) {
    tmpNum = tmpNum + "0"
  }
  return tmpNum;
}

function ValidateControl(objControl, strName, strType) {
  var strValue = objControl.value;
  var blnError = false;
  switch (strType) {
    case 'number' :
      if(!(wtcInStr('0123456789', strValue) && wtcCharCount(strValue, ',') <= 1 && wtcCharCount(strValue, '.') <= 1 && (parseInt(strValue) >= 0 && strValue != ''))) {
        blnError = ErrorAlert('Položka může obsahovat jen číslice bez mezer');
      }
      break;
    case 'text' :
      if (strValue.length == 0 || wtcCharCount(strValue, ' ') == strValue.length) {
        blnError = ErrorAlert('Není zadaná položka: ' + strName);
      }
      break;
    case 'email' :
      if (!IsValidEmail(objControl.value)) {
        blnError = ErrorAlert('Chybně zadaná položka ' + strName);
      }
      break;
  }
  if (blnError) {
    objControl.focus();
    return false;
  } else {
    return true;
  }
}

function ValidateControlSelect(objControl, strName, objItem) {
  if(objControl.selectedIndex < 0) {
    alert('Nebyla vybrána žádná položka v nabídce ' + strName);
    objControl.focus();
    return false;
  }
  if(objItem > -1) {
    if(objItem == objControl.selectedIndex) {
      alert('Nebyla vybrána žádná položka v nabídce ' + strName);
      objControl.focus();
      return false;
    }
  }
  return true;
}

//pridani stranky do oblibenych
function siteToFavorites(){
  if (IE4 && !NS4)
    window.external.AddFavorite("http://www.webtec.cz/","www.webtec.cz");
  else
    alert('Tato služba vyžaduje alespon\n Internet Explorer 5.0');
}

//nastaveni stranky jako homepage
function siteAsHomepage(){
  if (IE4 && !NS4)
    oHomePage.setHomePage('www.webtec.cz');
  else
    alert('Tato služba vyžaduje alespon\n Internet Explorer 5.0');
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}
