try {
    document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

document.cookie = 'killme=' + escape('nothing');

function cc() {
    if (document.cookie == "") {
        window.location = "/holiwast/cookie/";
    } else if($('cookie')) {
        $('cookie').value ="true";
    }
}

function getLetterPos(let) {
    var letters = "0abcdefghijklmnopqrstuvxyz";
    return letters.split(let)[0].length;
}

function getLetter(pos) {
    var letters = Array('0','a','b','c','d','e');
    return letters[pos];
}

function sum(input,target,target2) {
    if(arguments.length == 2) { target2 = '0'; }
    i = 0;
    res = 0;
    var input = input.split(';');
    while (i < input.length) {
        res += parseFloat(formatNumberRemove($(input[i]).value));
        i += 1; 
    }
    changeCell(res,target,1.5);
    $(target).className = '';
    if(target2 != '0') { changeCell(res,target2,1.5); $(target2).className = ''; }
    return res;
}

function distribute(sum,dist,target) {
    changeCell((Math.round(sum*dist/10)/10),target,1);
    $(target).className = '';
}

function changeCell(output,target,dur,num) {
    if(arguments.length == 3) { num = 1; }
    if(num) { output = formatNumber(output); }
    $(target).value = (output != 0) ? output : '';
    new Effect.Highlight('c'+target,{duration:dur});
}

function moveFocus(id,evt,w,h,last) {
    var keyCode = document.layers ? evt.which : document.all ? event.keyCode : document.getElementById ? evt.keyCode : 0;
    col = pF(getLetterPos(id.substr(0,1)));
    row = pF(id.substr(1,id.length-1));
    if(keyCode == '37' && col > 1) { changeFocus(getLetter(col-1)+row); }
    if(keyCode == '38' && row > 1) { changeFocus(getLetter(col)+(row-1)); }
    if(keyCode == '39' && col < w) { changeFocus(getLetter(col+1)+row); }
    if(keyCode == '40' || keyCode == '13') {
        if(row < h) { changeFocus(getLetter(col)+(row+1)); }
        if(row == h && keyCode == '13' && last) { $(id).blur(); }
    }
}

function changeFocus(id) {
    if($(id)) { $(id).focus(); }
}

function cellFocus(id,type) {
    inp = $(id);
    value = inp.value;
    if(type == 'p') { value = value.replace(/%/g, ''); }
    else if(type == 'n') { value = value.replace(/,/g, ''); }
    else { value = formatNumberRemove(value); }
    inp.className = 'sel';
    $('cv').value = value;
    inp.value = value; 
    setTimeout("inp.select()",1);
}

function cellBlur(id,type,remove_zero,allow_negative) {
    if(arguments.length < 3) { remove_zero = 0; }
    if(arguments.length < 4) { allow_negative = 0; }
    inp = $(id);
    value = inp.value;
    if(value.length) {
        if(type == 'p') { value = formatPercent(value,inp,remove_zero); }
        else if(type == 'n') { 
            value = value.replace(/,/g, ''); 
            if(isNaN(value) || (!allow_negative && value < 0)) { 
                inp.className = 'err';
            } else {
                value = formatNumber(formatNumberRemove(value));
                inp.className = 'aft';
            }}
        else { value = formatNumber(formatNumberRemove(value)); }
        //inp.value = (remove_zero && num == 0) ? '' : value;
    } else if(type = 'n') { inp.className = 'aft'; }
    inp.value = value;
}

function formatNumberRemove(num) {
    num = num + '';
    num = num.replace(',', '');
    if(num.length == 0 || isNaN(num)) { num = '0'; }
    return num;
}

function formatPercent(num,inp,remove_zero) {
    if(arguments.length == 2) { remove_zero = 0; }
    num = num + '';
    num = num.replace(/%/g, '');
    if(isNaN(num) || num < 0 || num > 100) { inp.className = 'err'; } else { inp.className = ''; }
    if(!isNaN(num) && num.length) { num = pF(num) + '%'; }
    if(remove_zero && num == '0%') { num = ''; }
    return num;
}

function formatNumber(num) {
  sep = ",";
  decpoint = ".";
  neg = 0;

  num = pF(num).toString();
  
  if(num.substr(0,1) == '-') { 
      neg = 1;
      num = num.substr(1,num.length);
  }
  
  a = num.split(decpoint);
  x = a[0];
  y = a[1];
  z = "";

  if (typeof(x) != "undefined") {
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return (neg) ? '-'+ x : x;
}

function pF(num) {
    return parseFloat(num);
}

function submitForm(target,lang) {
  if(typeof lang === 'undefined') { lang = ''; }
  if($('form')) {
    if(err = document.getElementsByClassName('err','input').length) {
      $('cv').blur();
      if(lang === 'da/') {
        alert('Denne side indeholder '+ err +' fejl. Du kan ikke fortsætte før dette er rettet.');
      } else {
        alert('The page currently contains '+ err +' error(s).\nYou cannot proceed before this is corrected.');
      }
      return false;
    }
    $('target').value = target;
    $('form').submit();
    return false;
  }
}

function chScen(target,form,scenario,lang) {
  if(typeof scenario === 'undefined') { scenario = 1; }
  if(typeof lang === 'undefined') { lang = ''; }
  if($('form')) {
    if(err = document.getElementsByClassName('err','input').length) {
      $('cv').blur();
      if(lang === 'da/') {
        if(confirm('Siden kan ikke genmmes på grund af fejl i siden.\nFortsæt uden at gemme?')) { return true; }
      } else {
        if(confirm('The page cannot be saved due to errors.\nProceed without saving?')) { return true; }
      }
      return false;
    } 
    if(form == 'submitForm2') {
      submitForm2(target,scenario,lang);
    } else {
      submitForm(target,scenario,lang);
    }
    return false;
  }
}

function ajax(page,params,target) {
    var url = '/holiwast/ajax/'+ page +'.php';
    var myAjax = new Ajax.Updater(target, url, { method: 'post', parameters: params});
}

function stopSpinner(name) {
    $(name+'_spinner').style.display = 'none';
    $('blank').remove();
}

function classChange(ids,className) {
    i = 0;
    var ids = ids.split(';');
    while (i < ids.length) {
        $(ids[i]).className = className;
        i += 1;
    }
}

function checkSum(ids,sum,positive) {
    sum = formatNumberRemove(sum);
    if(arguments.length == 2) { positive = 1; }
    i = 0;
    total = 0;
    var ids = ids.split(';');
    while (i < ids.length) {
        val = removeNP($(ids[i]).value);
        //if(!val.length || (positive && val < 0)) { val = 0; }
        if(!val.length) { val = 0; }
        total += (isNaN(val)) ? val : pF(val);
        i += 1;
    }
    if(isNaN(total) || total != sum) {
        return false;
    } else {
        return true;
    }
}

function checkLess(ids,sum) {
    i = 0;
    total = 0;
    var ids = ids.split(';');
    while (i < ids.length) {
        val = removeNP($(ids[i]).value);
        //if(!val.length || val < 0) { val = 0; }
        if(!val.length) { val = 0; }
        total += (isNaN(val)) ? val : pF(val);
        i += 1; 
    }
    if(isNaN(total) || total > sum) {
        return false;
    } else {
        return true;
    }
}

function classChangeCheck(ids, sum, positive) {
    if(arguments.length == 2) { positive = 1; }
    if(!checkSum(ids,sum,positive)) {
        classChange(ids,'err');
    } else {
        classChange(ids,'');
    }
}

function classChangeLess(ids, sum) {
    if(!checkLess(ids,sum)) {
        classChange(ids,'err');
    } else {
        classChange(ids,'');
    }
}

function removeNP(val) {
    val = val + '';
    val = val.replace(/,/g, '');
    return val.replace(/%/g, '');
}
