function getAngleImg(id,x,y) {
  var img;
  var a = -Math.atan2(y,x)*180/3.141/22.5; 
  if (a > -7 && a <= -5) img = 'NW';
  if (a > -5 && a <= -3) img = 'N';
  if (a > -3 && a <= -1) img = 'NO';
  if (a > -1 && a <= +1) img = 'O';
  if (a > +1 && a <= +3) img = 'SO';
  if (a > +3 && a <= +5) img = 'S';
  if (a > +5 && a <= +7) img = 'SW';
  if (a > +7 || a <= -7) img = 'W';
  $(id).src = 'app_icc/images/icons/customer/'+img+'.gif';
  $(id+'Txt').innerHTML = img
}

function calculateAge(m,y) {
  var now = new Date();
  var ageY = now.getFullYear()-y;
  var ageM = now.getMonth()+1-m
  if (ageM < 0) { 
    ageY = ageY-1; 
    ageM = 12+ageM; 
  }
  return { y: ageY, m: ageM };
}

function setPersonIconAlt(id,m,y) {
  var a = calculateAge(m,y);  
  id = $(id);
  id.title = id.title.replace(/:years:/,a.y);
  id.title = id.title.replace(/:months:/,a.m);
  id.alt = id.alt.replace(/:years:/,a.y);
  id.alt = id.alt.replace(/:months:/,a.m);
}

function setPersonSpan(id,m,y) {
  var a = calculateAge(m,y);  
  id = $(id);
  id.innerHTML = id.innerHTML.replace(/:years:/,a.y);
  id.innerHTML = id.innerHTML.replace(/:months:/,a.m);
}

function getProfileCompleteness(typ,par,chd,abo,n1,n2,o1,o2,mon) {
  var c = 0;
  switch (typ) {
    case '0': 
      break;
    case '1': 
      c += 20;    
//      if (par.length > 1) c += 10;
      if (chd.length > 1) c += 20;
      if (abo != '') c += 10;
      if (n1 != '') c += 30;
      if (n2 != '') c += 20;
      break;
    case '2':
      c += 20;
      if (chd.length > 1) c += 20;
      if (abo != '') c += 10;
      if (o1 != '') c += 30;
      if (o2 != '') c += 20;
      break;
    case '3':
      c += 20;
//      if (par.length > 1) c += 10;
      if (chd.length > 1) c += 20;
      if (abo != '') c += 10;
      if (n1 != '') c += 15;
      if (n2 != '') c += 10;
      if (o1 != '') c += 15;
      if (o2 != '') c += 10;
      break;
    case '6':
      c += 20;
      if (chd.length > 1) c += 20;
      if (abo != '') c += 10;
      if (o1 != '') c += 20;
      if (o2 != '') c += 10;
      if (mon != '') c += 20;
      break;
  }
  return c;
}
function getProfileCompleteness2(typ,par,chd,abo,n1,n2,o1,o2,mon) {
  var c = getProfileCompleteness(typ,par,chd,abo,n1,n2,o1,o2,mon);
  if (c >= 70) return ' *';
  return '';
}
function winHeight () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.body && document.body.offsetHeight) {
    return document.body.offsetHeight;
  } else {
    return 0;
  }
}

var mb = {
  redraw: function() {
    var el = $('maincol');
    if (!el._OriginalHeight) el._OriginalHeight = Element.iccGetDimensions('PubMainWinSB').height;
    if (winHeight()-el._OriginalHeight > 110) {
      if (isIE) {
        if ($('cusFooter')) {
          el.style.height = winHeight()-105-Element.iccGetDimensions('cusFooter').height;
        } else {
          el.style.height = winHeight()-105;
        }
      } else {
        if ($('cusFooter')) {
          el.style.height = winHeight()-100-Element.iccGetDimensions('cusFooter').height;
        } else {
          el.style.height = winHeight()-100;
        }
      }
    }

//    $('cusFooter').style.bottom = 0;
//    var el = $('PubMainWinSB');
//    if (isIE) {
//      if ($('cusFooter')) {
//        el.style.height = winHeight()-130-Element.iccGetDimensions('cusFooter').height;
//      } else {
//        el.style.height = winHeight()-130;
//      }
//    } else {
//      if ($('cusFooter')) {
//        el.style.height = winHeight()-125-Element.iccGetDimensions('cusFooter').height;
//      } else {
//        el.style.height = winHeight()-125;
//      }
//    }
  }
}

var mbRules = {
  'body': function(el) {
    el.onresize = function() {
      mb.redraw();
    };
  },
  '#maincol': function(el) {
    if (!el._OriginalHeight) {
      el._OriginalHeight = Element.iccGetDimensions('maincol').height;
      if (Element.iccGetDimensions('leftcol').height > el._OriginalHeight) el._OriginalHeight = Element.iccGetDimensions('leftcol').height;
      if (Element.iccGetDimensions('rightcol').height > el._OriginalHeight) el._OriginalHeight = Element.iccGetDimensions('rightcol').height;
    }
    if (winHeight()-el._OriginalHeight > 110) {
      if (isIE) {
        if ($('cusFooter')) {
          el.style.height = winHeight()-105-Element.iccGetDimensions('cusFooter').height;
        } else {
          el.style.height = winHeight()-105;
        }
      } else {
        if ($('cusFooter')) {
          el.style.height = winHeight()-100-Element.iccGetDimensions('cusFooter').height;
        } else {
          el.style.height = winHeight()-100;
        }
      }
    }
    $('cusFooter').style.display = 'block';     
  }
}
Behaviour.register(mbRules);