var dom = Boolean(document.getElementById) ? true : false;
var ns4 = Boolean(document.layers) ? true : false;
var ie = Boolean(document.all) ? true : false;
var ie4 = ie && !dom;

function objGet(id) {
   if (dom)
      return document.getElementById(id);
   else if (ie4)
      return eval('document.all.'+id);
   else
      return eval('document.'+id);
}

function showAttributes(whose){
  var test = "";
  var objWhose = objGet(whose);
  
  for(arg in objWhose){
    test += arg+"\n";
  }

  alert(test);
}

function openWindow(url, width, height) {
  newWindow = window.open(url,"Map","width="+width+",height="+height+",left=0,top=0,directories=no,status=yes,scrollbars=no,resizable=no,menubar=no");
  if(newWindow.focus != null) newWindow.focus();
}

function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
  return blankWin;
}

function inputChanged(){
  var i;
  var count = 0;
  var used = 0;
  var regForm = objGet("registration");
  var oneName;
  
  for (i = 0; i < regForm.elements.length; i++) {
  	oneName = regForm.elements[i].id;
    if (oneName.substr(0,6) == 'paper_') {
	    count++;
	    if (regForm.elements[i].value != "") used++;
  	}
  }

  if (used >= count-1) {
	  var last = objGet('paper_'+(count-1));
  	if (last) {
	    count++;
	    var newBox = document.createElement('div');
	    newBox.id = 'div_paper_'+(count-1);
	    var inputBox = document.createElement('input');
	    inputBox.type = 'text';
	    inputBox.className = 'inputBox';
	    inputBox.id = 'paper_'+(count-1);
	    inputBox.name = 'paper[]';
	    inputBox.onchange = inputChanged;
	    newBox.appendChild(inputBox);
	    last.parentNode.insertBefore(newBox, last.nextSibling);
  	}
  }
}

function addInputBox(howMany){
  var last = objGet('paper_1');
  for(var i=2; i<2+howMany; i++){
    var newBox = document.createElement('div');
    newBox.id = 'div_paper_'+i;
    var inputBox = document.createElement('input');
    inputBox.type = 'text';
    inputBox.className = 'inputBox';
    inputBox.id = 'paper_'+i;
    inputBox.name = 'paper[]';
    inputBox.onchange = inputChanged;
    newBox.appendChild(inputBox);
    last.parentNode.insertBefore(newBox, last.nextSibling);
  }
}

function fillPapers(){
  var extraPaper;
  for(i=0; i<arguments.length; i++){
    extraPaper = objGet("paper_"+(i+2));
    if(extraPaper){
      extraPaper.value = arguments[i];
    }
  }
}

function changePersonsCount(){
  var selectBox = objGet("othersCount");
  
  if(selectBox){
    var count = parseInt(selectBox.value);
    var fieldset;
    var i;
    
    //showing count select boxes
    for(i=0; i<count; i++){
      fieldset = objGet("othersFieldset"+i);
      if(fieldset){
        fieldset.className = "show registration";
      }
    }
    
    if(count==0) count = -1;
    //hiding other select boxes
    for(i=count; i<5; i++){
      fieldset = objGet("othersFieldset"+i);
      if(fieldset){
        fieldset.className = "hide registration";
      }
    }
     
  }
}

function changePapers(which){
  var selectBox = objGet("papersCount"+which);
  if(selectBox){
    var count = parseInt(selectBox.value);
    
    //showing count input boxes
    for(i=1; i<=count; i++){
      fieldset = objGet("paper_"+i+"Part_"+which);
      if(fieldset){
        fieldset.className = "show";
      }
    }
    
    //hiding other input boxes
    for(i=count+1; i<=2; i++){
      fieldset = objGet("paper_"+i+"Part_"+which);
      if(fieldset){
        fieldset.className = "hide";
      }
    }
  }
}

function changePapersVisibility(){
  try{
    var student = objGet("student");
    var accomp = objGet("accompanyingPerson");
    var author = objGet("isauthor");
  
    if(student && (accomp || author)){
      var papers1 = objGet("publishingDetails1");
      var papers2 = objGet("publishingDetails2");
      var isStudent = student.checked;
      if(accomp)
        var isAccomp = accomp.checked;
      else{
        var isAccomp = author.checked;
        isAccomp = !isAccomp;
      }
      //alert(isAccomp);  
      if(isAccomp){
        papers1.className = "hide";
        papers2.className = "hide";
      }
      else{
        papers1.className = "show";
        papers2.className = "show";
      }
    }
  }
  catch(e){
    //an exception is thrown in admin area when changing info - the objects don't exist 
  }
}

function checkStudent(){
  var student = objGet("student");
  var author = objGet("isauthor");
  
  if(student && author.checked){
    student.checked = false;
  }
  changePapersVisibility();
}

function checkAuthor(){
  var student = objGet("student");
  var author = objGet("isauthor");
  
  if(author && student.checked){
    author.checked = false;
  }
}

/*
function changeIsAuthor(){
  var isauthor = objGet("isauthor");
  var papers1 = objGet("publishingDetails1");
  var papers2 = objGet("publishingDetails2");
      
  if(isauthor){
    if(isauthor.checked){
      papers1.className = "show";
      papers2.className = "show";      
    }
    else{
      papers1.className = "hide";
      papers2.className = "hide";      
    }
  }
}
*/
function changeOthersPapersVis(which, type){
  var row = objGet("othersPapersCountRow"+which);
  var article1 = objGet("paper_1Part_"+which);
  var article2 = objGet("paper_2Part_"+which);
  
  if(row){   
    if(type==1){
      row.className = "show";
      changePapers(which);
    }
    else{
      row.className = "hide";
      article1.className = "hide";
      article2.className = "hide";
    }
  }
}

function showInvoiceTable(){
  var table = objGet("invoiceTable");
  var chkInvSame = objGet("chkInvoiceSame");
  var how = (chkInvSame.checked) ? 0 : 1;
  
  if(table){
    if(how)
      table.className = "show registration";
    else
      table.className = "hide registration";
  }
}

function showInvoiceAll(){
  var chkObj = objGet("chkAttachInv");
  var invAll = objGet("invoiceAll");
  var parentIco = objGet("parentIcoTable");
  var redHash = objGet("redHash");
  
  var how = (chkObj.checked) ? 0 : 1;

  if(how){
    if(redHash){
      redHash.className = "show";  
    }
    invAll.className = "show";
    parentIco.className = "registration hide";
  }
  else{
    if(redHash){
      redHash.className = "hide";
    }
    invAll.className = "hide";
    parentIco.className = "registration show";
  }
}

function showAccommodation(how){
  var accommRow = objGet("accommRow");
  if(accommRow){
    if(how){
      accommRow.className = "show";
    }
    else{
      accommRow.className = "hide";
    }
  }
}

function showAccommodationPart(how, which){
  var accommRow = objGet("accommRow"+which);

  if(accommRow){
    if(how){
      accommRow.className = "show";
    }
    else{
      accommRow.className = "hide";
    }
  }
}