// JScript File
//Updated!
var showUS;
var showCA;
var showUK;
var showOE;
var rowcount;
var datadir;
var maxSDK;
var newVar = 0;

function getRowStyle () {
  var check = rowcount % 2;
  rowcount++;
  if (check != 0) {
      return "mainTableAltRow";
  } else {
      return "mainTableRow";
  }
}

function getVariable(varname) {
	newVar++;
	return varname + newVar.toString();
}


function checkSDKVers(data) {
  var reqSDK = 1;
  if (data != null && typeof(data.minVerUS) != "undefined") {
      reqSDK = data.minVerUS;
  }
  return (reqSDK <= maxSDK);
}

function supportsCheckedEditions(supports) {
  if (showUS && supports.US) {
      return true;
  }
  if (showCA && supports.CA) {
      return true;
  }
  if (showUK && supports.UK) {
      return true;
  }
  if (showOE && supports.OE) {
      return true;
  }
  return false;
}

function getMaxDTStr(data) {
  var retval = "";
  if (data.maxDT != null) {
      retval = data.maxDT + ' chars';
  } else {
      retval = "";
  }
  return retval;
}

function getMaxOEStr(data) {
  var retval = "";
  if (data.maxOE != null) {
      retval = data.maxOE + ' chars';
  } else {
      retval = "";
  }
  return retval;
}

function getMaxStr(data) {
  var retval = "";
  var DTStr = "&nbsp";
  var OEStr = "";
  if (showUS || showCA || showUK) {
      DTStr = getMaxDTStr(data);
  }
  if (showOE) {
      OEStr = getMaxOEStr(data);
  }
  if (DTStr == OEStr) {
      retval = DTStr;
  } else {
      retval = DTStr + "," + OEStr;
  }
  return retval;
}

function updateSupports(supports, data) {
  if (data == null || data.supports == null) {
      return supports;
  }
  var tmp = new Object();
  tmp.US = (supports.US && data.supports.US);
  tmp.CA = (supports.CA && data.supports.CA);
  tmp.UK = (supports.UK && data.supports.UK);
  tmp.OE = (supports.OE && data.supports.OE);
  return tmp;
}

function getImplementationStr(data, supports) {
  var retval = "";
  var noUS = '<img src="image/NoUS.png"/>';
  var noCA = '<img src="image/NoCA.png"/>';
  var noUK = '<img src="image/NoUK.png"/>';
  var noOE = '<img src="image/NoOE.png"/>';
  var vers = '<span style="color: #ffffff;">1.0</span>';
  if (showUS && supports.US) {
      noUS = '<img src="image/US.png"/>';
  }
  if (showCA && supports.CA) {
      noCA = '<img src="image/CA.png"/>';
  }
  if (showUK && supports.UK) {
      noUK = '<img src="image/UK.png"/>';
  }
  if (showOE && supports.OE) {
      noOE = '<img src="image/OE.png"/>';
  }
  try {
      if (data.minVerUS != null) {
          if ((data.minVerUS - Math.round(data.minVerUS)) > 0) {
              vers = data.minVerUS;
          } else {
              vers = data.minVerUS + ".0";
          }
      }
  } catch (ex) {
      alert("getImpl caught: " + ex);
  }
  retval = vers + " " + noUS + " " + noOE + " " + noCA + " " + noUK;
  return retval;
}

function getFieldMapAttrs(data) {
    var attrs = "";
    if (typeof(data.MasterImage) != 'undefined' && data.MasterImage != 'None') {
        if (typeof(data.Height) != 'undefined' && data.Height > 0) {
        	data.Width += 3;
        	data.Height += 3;
            attrs += 'b:imageFile="' + data.MasterImage + '" ';
            attrs += 'b:TopLeftX="' + data.TopLeftX + 'px" ';
            attrs += 'b:TopLeftY="' + data.TopLeftY + 'px" ';
            attrs += 'b:Width="' + data.Width + 'px" ';
            attrs += 'b:Height="' + data.Height + 'px" ';
        }
    }
    if (data.xmlName == 'IncludeRetElement') {
        attrs += 'b:IsIncludeRetElement="true"'
    }
    return attrs;
}

function writeQBXMLAttributes(data, parentSupports) {
  var str = "";
  if (data.attributes != null) {
      var wroteattr = false;
      var j;
      for (j=0; j<data.attributes.length; j++) {
          var supports = updateSupports(parentSupports, data.attributes[j]);
          if (supportsCheckedEditions(supports)) {
              if (data.attributes[j].xmlType != "skip") {
                  if (!wroteattr) {
                      str += '(';
                  }
                  hlStart = "";
                  hlEnd = "";
                  if (data.attributes[j].xmlNameHtml) {
                      hlStart = '<a b:behavior="populateDetails" b:url="' + datadir + 'html/' + data.attributes[j].xmlNameHtml +'">';
                      hlEnd = '</a>';
                  }
                  str += hlStart + data.attributes[j].xmlName + hlEnd;
                  if ((j+1)<data.attributes.length) {
                      str += ', ';
                  }
                  wroteattr = true;
              }
          }
      }
      if (wroteattr) {
          str += ')';
      }
  }
  return str;
}

function writeHeader(reqColName) {
  var str = "";
  str += '<b:treelist b:resize="all">\n';
  //str += '<s:event b:on="construct">\n';
  //str += '  <s:super />\n';
  //str += '  <s:task b:action="treelist-open" b:target=".//*"/>\n';
  //str += '</s:event>\n'; 
  str += '<b:treelistrow>\n';
  str += '<b:treelistcell b:type="head">Tag</b:treelistcell>\n';
  str += '<b:treelistcell b:type="head">Type</b:treelistcell>\n';
  str += '<b:treelistcell b:type="head">Max (DT)</b:treelistcell>\n';
  str += '<b:treelistcell b:type="head">Max (OE)</b:treelistcell>\n';
  str += '<b:treelistcell b:type="head">Implementation</b:treelistcell>\n';
  str += '<b:treelistcell b:type="head">' + reqColName + '</b:treelistcell>\n';
  str += '</b:treelistrow>\n';
  return str;
}

function renderOSR(targ) {
      showUS = _vars['showUS'][0];
      showOE = _vars['showOE'][0];
      showCA = _vars['showCA'][0];
      showUK = _vars['showUK'][0];
      datadir = _vars['dataDir'][0];

	  var varname = 'messageDataJSON';
      var msgData = _vars[varname][0];
      var messageDataJSON;
      if (msgData != "") {
          messageDataJSON = eval('(' + _vars[varname][0] + ')');
      }
      //alert("renderOSR");
      //alert(typeof(messageDataJSON));
      var format = _vars['format'][0];
      var content = "";
      if (format == "qbXML") {
          content = renderXMLOSR(messageDataJSON);
      } else {
          content = renderQBFCOSR(messageDataJSON);
      }
      bpc.render(content,'replacechildren', targ);
      bpc.execute('<s:task b:action="treelist-open" b:target=".//*"/>', targ);
}

