﻿//********************************************************   HELPER METHODS   ***************************************
//for getting correct td in tr, FF needed
function GetTdFromRow(row, td_id, ctrl_type)
{
    var i=0;
    for(;i < row.childNodes.length; i ++ )
    {
        if( ctrl_type == row.childNodes[i].nodeName )
        {
            if( row.childNodes[i].id == td_id ) return row.childNodes[i];
        }
    }
    return null;
}

//for getting correct control in td, FF needed
function GetControlFromParent(ctrl_parent, ctrl_id, ctrl_type)
{
    var i=0;
    for(;i < ctrl_parent.childNodes.length; i ++ )
    {
        if( ctrl_type == ctrl_parent.childNodes[ i ].nodeName )
        {
            if( ctrl_parent.childNodes[i].id == ctrl_id ) return ctrl_parent.childNodes[i];
        }
    }
    return null;
}

function GetElementPosition(elemID)
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail)
    {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
    {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft,top:offsetTop};
}

function isNonnegativeInteger (s)
{   var secondArg = false;

   if (isNonnegativeInteger.arguments.length > 1)
    secondArg = isNonnegativeInteger.arguments[1];

   // The next line is a bit byzantine.  What it means is:
   // a) s must be a signed integer, AND
   // b) one of the following must be true:
   //    i)  s is empty and we are supposed to return true for
   //        empty strings
   //    ii) this is a number >= 0

   return (isSignedInteger(s, secondArg)
        && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}
 
function isSignedInteger (s)

{   if (isEmpty(s))
  if (isSignedInteger.arguments.length == 1) return false;
  else return (isSignedInteger.arguments[1] == true);

  else {
     var startPos = 0;
     var secondArg = false;

     if (isSignedInteger.arguments.length > 1)
        secondArg = isSignedInteger.arguments[1];

     // skip leading + or -
     if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
        startPos = 1;
     return (isInteger(s.substring(startPos, s.length), secondArg))
  }
}  
function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}

function AddUserToMailingList() 
{
    var emailAddress = document.getElementById('txtMailing').value;
    if (IsValidEmailAddress(emailAddress))
        SendMailingListRequest(OnSucceeded, OnFailed, emailAddress)
    else
        alert('Proverite Vašu email adresu !');
}
function IsValidEmailAddress(emailAddress) 
{
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}
