// Function for client side validation of running surveys. Server side validation
// is always used as a backup and there should be nothing in here that prevents
// a survey from running if client-side JavaScript is disabled.

// generally there are two types of function, on for validating input chars (_chr fns)
// and others for when the value changes. (_ch fns).


// valText_chr
// check that we have not exceeded the length of this entry field. Return 
// 'true' if we're OK, false otherwise.
function valText_chr(x) {
  var length = parseInt(x.value.length);
  var max    = parseInt(x.getAttribute('maxlength'));

  if (length>max) {
    x.value=(new String(x.value)).substr(0,max);
  }
  return true;
}

function openResults(surveyID, resultsID) {
  // Open a results intermediate window and then close this window (if possible)
  window.open('/survey/report_simple.rhtm?useComp=yes&surveyID='+surveyID+'&resgroup='+resultsID,'_blank');
  try {
    window.close();
  }
  catch (ev) {
  }
}

function openWindow(pathName,winName) {
  // Open a results intermediate window and then close this window (if possible)
  window.open(pathName,winName,'width=850,height=650,resizable=yes,scrollbars=yes').focus();
}

// Return the size of the current client area. Should work cross-browser.
function calcWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } 
  else if (document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } 
  else if (document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return {width:myWidth, height:myHeight};
}

function procOnLoadList(form,panel,maxWidth) {
  var winSize = calcWinSize();

  if (maxWidth!=null && winSize.width>maxWidth)
    winSize.width = maxWidth;

  // Find the bodyContent and resize according to the size of the screen.
  var elem = document.getElementById(form.n);

  if (elem!=null) {
    var newHeight = winSize.height-form.b;
    var newWidth  = winSize.width -form.r;
  
    elem.style.height=''+(newHeight)+'px';
    elem.style.width=''+(newWidth)+'px';
  }

  var elem = document.getElementById(panel.n);

  if (elem!=null) {
    var newHeight = winSize.height-panel.b;
    var newWidth  = winSize.width-panel.r;

    elem.style.height=''+(newHeight)+'px';
    elem.style.width=''+(newWidth)+'px';
    window.status = ""+elem.style.width+", "+(elem.style.height);
  }
}

function checkPref(page,q,row,col) {
  var fieldID = ''+page+'.'+q+'.';

  // Work out old row to re-enable other columns
  var tcol = 1;
  var item;
  while ((itemlst = document.getElementsByName(fieldID+tcol))!=null && itemlst.length>0) {
    if (tcol!=col && itemlst[row]!=null) {
      itemlst[row].checked = false;
    }
    tcol++;
  }
}

function checkCsum(page,q,row,totalPoints) {
  var fieldID = ''+page+'_id.'+q+'.';

  // Work out old row to re-enable other columns
  var workTotal=0;
  
  tcol = 0;
  var item;
  var thisElem;
  var vlist=[];

  while ((itemlst = document.getElementById(fieldID+tcol))!=null) {
    var pt=itemlst.attributes.id.value.split('.');
    if (pt.length>=3 && pt[2]==row)
      thisElem=itemlst;
    vlist.push(itemlst);
    tcol++;
  }
  // Make sure the new value is a number
  var thisNum=0;

  if (thisElem!=null) {
    var thisNum=parseInt(thisElem.value,10);
    if (isNaN(thisNum)||thisNum<0||thisNum>totalPoints)
      thisNum=0;
    thisElem.value=thisNum.toString();
  }
  // Work out total;
  for (var i=0;i<vlist.length;i++) {
    var t=parseInt(vlist[i].value,10);
    if (!isNaN(t))
      workTotal+=t;
  }

  if (workTotal>totalPoints) {
    workTotal-=thisNum;
    thisNum=totalPoints-workTotal;
    workTotal=totalPoints;
    if (thisNum<0) {
      // Impossible!
      for (var i=0;i<vlist.length;i++)
        vlist[i].value=0;
      workTotal=0;
      vlist[0].focus();
      thisNum=0;
    }
    thisElem.value = thisNum.toString();
    thisElem.focus();
  }
  // Deal with the prompt
  function subStr(s) {
    var ns=subTable[s.substring(1).toLowerCase()];
    if (ns!=null)
      return ns;
    return s;
  }
  var prompt=window['prompt_'+q];

  subTable={
    total:totalPoints,
    avail:totalPoints-workTotal,
    used:workTotal
  }
  prompt = prompt.replace(/\$\w+/gi,subStr);

  // Find prompt for this element
  var promptElem=document.getElementById('prompt_'+q);
  if (promptElem!=null)
    promptElem.innerHTML=prompt;

}

