// JavaScript Document
//temporary function to stop orders
function noOrders()
{
  alert("Thank you for your interest, we value your business.\nUnveiled Bridal Designs will be closed from June 8th-22nd.\nYou can send e-mails to Dorene at unveiledbridaldesigns@gmail.com thank you again.");
  return;
}

function $get(id)
{
  return document.getElementById(id);
}

function createXHR()
{
  if (typeof XMLHttpRequest != "undefined")
  {
    return new XMLHttpRequest();
  }
  else
  {
    var aVersions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
    for (var z=0; z < aVersions.length; z++)
    {
      try
      {
        var oXHR = new ActiveXObject(aVersions[z]);
        return oXHR;
      }
      catch (oError)
      {
      }
    }
  }
  throw new Error("XMLHttpRequest or XMLHttp could not be created");
}

/**
 * Attach a function to an objects event
 * @param obj    the object containing the element to which the function is to be
 *               attached.
 * @param evType event type to which the function is to be attached.
 * @param fn     the function which is to be attached to the event
 */
function addEvent(obj, evType, fn)
{
  if(obj.addEventListener)
  {
    obj.addEventListener(evType, fn, false);
    return true;
  }
  else if(obj.attachEvent)
  {
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  }
  else
  {
    return false;
  }
}

//keyValue class
function keyVal(pKey, pVal)
{
  this.key = pKey;
  this.val = pVal;
}

function keyValArr()
{
  this.keyVals = [];
  this.nextPtr = 0;
  this.add = function(pKeyVal)
             {
               this.keyVals[this.nextPtr] = pKeyVal;
               this.nextPtr++;
             }
             
  this.byKey = function(pKey)
               {
                 var z = 0;
                 while(z < this.nextPtr)
                 {
                   if(this.keyVals[z].key == pKey)
                     return this.keyVals[z].val;

                   z++;
                 }

                 return false;
               }
}

function loadAdvSearch(type)
{
  advSearchHTML = $get('adv_search_-id-').innerHTML;
  valSearchHTML = $get('adv_search_-val-_data').innerHTML;
  dateSearchHTML = $get('adv_search_-date-_data').innerHTML;
  selSearchHTML = $get('adv_search_-sel-_data').innerHTML;

  $get('search_crit').removeChild($get('adv_search_-id-'));
  $get('search_crit').removeChild($get('adv_search_-val-_data'));
  $get('search_crit').removeChild($get('adv_search_-date-_data'));
  $get('search_crit').removeChild($get('adv_search_-sel-_data'));

  advSearchFields(type);
} //function loadAdvSearch()

function addAdvSearch(fieldName, operator, value, value2)
{
  var search_crit = $get('search_crit');
  var crits = search_crit.getElementsByTagName('div');
  var maxID = 0;
  for(var z = 0; z < crits.length; z++)
  {
    if(parseInt(crits[z].id.replace('adv_search_', '')) > maxID)
      maxID = parseInt(crits[z].id.replace('adv_search_', ''));

  }

  maxID++;
  var newDivHTML = '';
  newCrit = document.createElement('div');
  newCrit.id = 'adv_search_'+maxID;
  newCrit.className = 'clearfix';
  newCrit.innerHTML = advSearchHTML.replace(/-data-/g, '<div id="adv_search_-id-_data">'+valSearchHTML+"</div>").replace(/-id-/g, maxID);
  search_crit.appendChild(newCrit);
  advSearchSetOpts($get('field_'+maxID));
  if(fieldName != undefined)
  {
    var fieldSel = $get('field_'+maxID);
    var z = 0;
    while((z < fieldSel.options.length) &&
          (fieldSel.options[z].value != fieldName))
    {
      z++;
    }

    if(z < fieldSel.options.length)
      fieldSel.selectedIndex = z;

    fldSelected(maxID, operator, value, value2);
  }
}

function remAdvSearch(critID)
{
  var search_crit = $get('search_crit');
  var child = $get('adv_search_'+critID);
  search_crit.removeChild(child);
}

function resetAdvSearch(searchType)
{
  advSearchFields(searchType);
} //function resetAdvSearch(searchType)

function field(table, name, description, type, selTbl, selFld, comment)
{
  this.table = table;
  this.name = name;
  this.description = description;
  this.type = type;
  this.selTbl = selTbl;
  this.selFld = selFld;
  this.comment = comment;
}

function fieldByName(fldName)
{
  var z = 0;
  while((z < fieldArr.length) &&
        (fieldArr[z].name != fldName))
  {
    z++;
  }
  
  if(z < fieldArr.length)
    return fieldArr[z];
  else
    return false;
    
}

function shipToCountryChanged()
{
  var country = $get('shipToCountry').options[$get('shipToCountry').selectedIndex].value;
  getShipOptions(country);
}

function getShipOptions(country)
{
  var currentOption = $get('cartShipOption').value;
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getShipOptions.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var rows = xhr.responseText.split("\n");
                               if(rows[0] == 'OKAY')
                               {
                                 var sel = $get('cartShipOptionSel');
                                 for(var z = sel.options.length - 1; z >= 0; z--)
                                   sel.remove(z);
                                   
                                 var seld = $get('cartShipOption').value;
                                 for(var z = 1; z < rows.length; z++)
                                 {
                                   var flds = rows[z].split('|');
                                   var id = flds[0];
                                   var shortDesc = flds[1];
                                   var price = flds[2];
                                   try
                                   {
                                     sel.add(new Option(shortDesc+' - $'+price, id), null);
                                   }
                                   catch(e)
                                   {
                                     sel.add(new Option(shortDesc+' - $'+price, id));
                                   }
                                 }
                                 
                                 for(var z = 0; z < sel.options.length; z++)
                                 {
                                   if(sel.options[z].value == currentOption)
                                   {
                                     sel.options[z].selected = true;
                                   }
                                 }

                                 cartShipOptionChanged();
                               }
                               else
                               {
                                 alert(xhr.responseText);
                               }
                             }
                           }

  xhr.send('Country='+country);
}

function cartShipOptionChanged()
{
  var shipOption = $get('cartShipOptionSel').options[$get('cartShipOptionSel').selectedIndex].value;
  $get('cartShipOption').value = shipOption;
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getShipOptionPrice.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var rows = xhr.responseText.split("\n");
                               if(rows[0] == 'OKAY')
                               {
                                 var price = $get('cartShipOptionPrice');
                                 if(rows.length > 1)
                                 {
                                   var shipPrice = parseFloat(rows[1]);
                                   if(cartItemCount < 1)
                                     shipPrice = 0;
                                     
                                   price.value = rows[1];
                                   $get('shippingPriceDisplay').innerHTML = '$'+shipPrice.toFixed(2);
                                   var subTotal = parseFloat($get('orderSubTotal').value);
                                   var total = subTotal + shipPrice; 
                                   $get('orderTotalDisplay').innerHTML = '$'+total.toFixed(2);
                                 }
                                 else
                                 {
                                   price.value = '';                                   
                                 }
                               }
                               else
                               {
                                 alert(xhr.responseText);
                               }
                             }
                           }

  xhr.send('ShipOption='+shipOption);
}

function advSearchFields(searchType)
{
  fieldArr.length = 0;
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/advSearchFields.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var fields = xhr.responseText.split("\n");
                               if(fields[0] == 'OKAY')
                               {
                                 for(var z = 1; z < fields.length; z++)
                                 {
                                   var attr = fields[z].split('|');
                                   fieldArr[z - 1] = new field(attr[0],
                                                               attr[1],
                                                               attr[2],
                                                               attr[3],
                                                               attr[4],
                                                               attr[5],
                                                               attr[6]);
                                                               
                                 }
                               }
                               else
                               {
                                 alert(fields[0]);
                               }
                             
                               if($get('search_crit'))
                                 remAllAdvSearches();
                                 
                             }
                           }

  xhr.send('searchType='+searchType);
}

function remAllAdvSearches()
{
  var search_crit = $get('search_crit');
  var crits = search_crit.getElementsByTagName('div');
  for(var z = crits.length - 1; z >= 0 ; z--)
  {
    if((crits[z].id.search('adv_search_') == 0) &&
       (crits[z].id.search('_data') == -1))
      search_crit.removeChild(crits[z]);

  }
  
  if(firstLoad && initFields.length > 0)
  {
    for(var z = 0; z < initFields.length; z++)
    {
      addAdvSearch(initFields[z], initOperators[z], initValues[z], initValue2s[z]);
    }
      
    firstLoad = false;
  }
  else
    addAdvSearch();
    
}

function advSearchSetOpts(fieldSel)
{
  for(var z = fieldSel.options.length - 1; z >= 0; z--)
  {
    fieldSel.remove(z);
  }

  for(var z = 0; z < fieldArr.length; z++)
  {
    try
    {
      fieldSel.add(new Option(fieldArr[z].description, fieldArr[z].name), null);
    }
    catch(e)
    {
      fieldSel.add(new Option(fieldArr[z].description, fieldArr[z].name));
    }
  }
}

function setSel(sel, value)
{
  var z = 0;
  while((z < sel.options.length) &&
        (sel.options[z].value != value))
  {
    z++;
  }

  if(z < sel.options.length)
    sel.selectedIndex = z;

}

function fldSelected(fldID, operator, value, value2)
{
  var sel = $get('field_'+fldID);
  var fldName = sel.options[sel.selectedIndex].value;
  var data = $get('adv_search_'+fldID+'_data');
  switch(fieldArr[sel.selectedIndex].type)
  {
    case 'varchar':
    case 'int':
    case 'decimal':
      var dataHTML  = valSearchHTML.replace(/-id-/g, fldID);
      data.innerHTML = dataHTML;
      if(operator != undefined)
        setSel($get('operator_'+fldID), operator);
        
      if(value != undefined)
        $get('value_'+fldID).value = value;
        
      break;
    case 'date':
      var dataHTML = dateSearchHTML.replace(/-id-/g, fldID);
      data.innerHTML = dataHTML;
      $(function() {
      	$(".date_picker").datepicker();
    	});
    	
      if(value != undefined)
        $get('start_date_'+fldID).value = value;
        
      if(value2 != undefined)
        $get('end_date_'+fldID).value = value2;

      break;
    case 'ptr':
      var dataHTML = selSearchHTML.replace(/-id-/g, fldID);
      data.innerHTML = dataHTML;
      if(operator != undefined)
      {
        setSel($get('operator_'+fldID), operator);
      }

      fillSel(fldID, fieldArr[sel.selectedIndex].selTbl, fieldArr[sel.selectedIndex].selFld, value);
      break;
  }
} //fldSelected(fldID)

function fillSel(fldID, selTable, selField, value)
{
  fldSel = $get('value_'+fldID);
  for(var z = fldSel.options.length - 1; z >= 0; z--)
  {
    fldSel.remove(z);
  }

  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/advSelFields.php", false);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send('table='+selTable+'&field='+selField);
  var fields = xhr.responseText.split("\n");
  if(fields[0] == 'OKAY')
  {
    for(var z = 1; z < fields.length; z++)
    {
      var attr = fields[z].split('|');
      try
      {
        fldSel.add(new Option(attr[1], attr[0]), null);
      }
      catch(e)
      {
        fldSel.add(new Option(attr[1], attr[0]));
      }
    }
    
    if(value != undefined)
      setSel($get('value_'+fldID), value);
      
  }
  else
  {
    alert(fields[0]);
  }
}

function addComment()
{
  var table = $get('table').value;
  var record = $get('record').value;
  var tempID = $get('tempID').value;
  var field = $get('field').value;
  var advisor = $get('commentAdvisor').value;
  var comment = $get('comment').value;
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/addComment.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               alert(xhr.responseText);
                               $.facebox.close();
                             }
                           }
  var post = "table="+table+"&record="+record+"&field="+field+"&advisor="+advisor+"&comment="+comment+"&tempID="+tempID;
  xhr.send(post);
}

function addProDivBrUnitCCRow()
{
  var tbl = $get('proDivBrUnitCCTbl');
  var maxID = 0;
  for(var z = 0; z < tbl.tBodies[0].rows.length; z++)
  {
    var sels = tbl.tBodies[0].rows[z].cells[0].getElementsByTagName('select');
    if(parseInt(sels[0].id.replace('DeputyID_', '')) > maxID)
      maxID = parseInt(sels[0].id.replace('DeputyID_', ''));

  }
  
  maxID++;
  var newRow = document.createElement('tr');
  for(var z = 0; z < tbl.tBodies[0].rows[0].cells.length; z++)
  {
    var newCell = document.createElement('td');
    var html = tbl.tBodies[0].rows[0].cells[z].innerHTML;
    newCell.innerHTML = html.replace(/_0/g, '_'+maxID);
    var sels = newCell.getElementsByTagName('select');
    for(var z1 = 0; z1 < sels.length; z1++)
    {
      sels[z1].selectedIndex = 0;
      if((sels[z1].id.indexOf('BranchID') == 0) ||
         (sels[z1].id.indexOf('UnitID') == 0))
        remSelOptions(sels[z1]);
        
    }
    
    sels = newCell.getElementsByTagName('input');
    for(var z1 = 0; z1 < sels.length; z1++)
      sels[z1].value = '';

    newRow.appendChild(newCell);
  }

  tbl.tBodies[0].appendChild(newRow);
}

function addConDivBrUnitCCRow(tabID)
{
  var tbl = $get('conDivBrUnitCCTbl_'+tabID);
  var maxID = 0;
  for(var z = 0; z < tbl.tBodies[0].rows.length; z++)
  {
    var sels = tbl.tBodies[0].rows[z].cells[0].getElementsByTagName('select');
    if(parseInt(sels[0].id.replace('ContractDeputyID_'+tabID+'_', '')) > maxID)
      maxID = parseInt(sels[0].id.replace('ContractDeputyID_'+tabID+'_', ''));

  }
  
  maxID++;
  var newRow = document.createElement('tr');
  for(var z = 0; z < tbl.tBodies[0].rows[0].cells.length; z++)
  {
    var newCell = document.createElement('td');
    var html = tbl.tBodies[0].rows[0].cells[z].innerHTML;
    newCell.innerHTML = html.replace(/_0/g, '_'+maxID);
    var sels = newCell.getElementsByTagName('select');
    for(var z1 = 0; z1 < sels.length; z1++)
    {
      sels[z1].selectedIndex = 0;
      if((sels[z1].id.indexOf('ContractBranchID') == 0) ||
         (sels[z1].id.indexOf('ContractUnitID') == 0))
        remSelOptions(sels[z1]);
        
    }
    
    sels = newCell.getElementsByTagName('input');
    for(var z1 = 0; z1 < sels.length; z1++)
      sels[z1].value = '';

    newRow.appendChild(newCell);
  }

  tbl.tBodies[0].appendChild(newRow);
}

function remSelOptions(sel)
{
  for(var z = sel.options.length - 1; z >= 0; z--)
  {
    sel.remove(z);
  }
}

function addSelOptions(sel, optionList)
{
  var options = optionList.split("\n");
  for(var z = 0; z < options.length; z++)
  {
    option = options[z].split('|');
    try
    {
      sel.add(new Option(option[1], option[0]), null);
    }
    catch(e)
    {
      sel.add(new Option(option[1], option[0]));
    }
  }
}

function remProDivBrUnitCCRow(delBtn)
{
  delBtn.parentNode.parentNode.parentNode.removeChild(delBtn.parentNode.parentNode);
}

function remConDivBrUnitCCRow(delBtn)
{
  delBtn.parentNode.parentNode.parentNode.removeChild(delBtn.parentNode.parentNode);
}

function fillProSelect(fldName, filter)
{
  var xhr = createXHR();
  var fldNm;
  if(fldName.lastIndexOf('_') >= 0)
    fldNm = fldName.substr(0, fldName.lastIndexOf('_'));
  else
    fldNm = fldName;

  xhr.open("POST", site_url+"ajax/fillProSelect.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var field = $get(fldName);
                               remSelOptions(field);
                               if(fldNm == 'BranchID')
                               {
                                 remSelOptions($get('UnitID_'+parseInt(fldName.replace('BranchID_', ''))));
                               }
                               
                               if(fldNm == 'CommodityLevel2ID')
                               {
                                 remSelOptions($get('CommodityLevel3ID'));
                               }

                               remSelOptions($get(fldName));
                               addSelOptions($get(fldName), xhr.responseText);
                             }
                           }

  xhr.send("fldName="+fldNm+"&filter="+filter);
}

function fillConSelect(fldName, filter)
{
  var xhr = createXHR();
  var fldNm;
  if(fldName.lastIndexOf('_') >= 0)
    fldNm = fldName.substr(0, fldName.lastIndexOf('_'));
  else
    fldNm = fldName;

  if(fldNm.lastIndexOf('_') >= 0)
    fldNm = fldNm.substr(0, fldNm.lastIndexOf('_'));

  xhr.open("POST", site_url+"ajax/fillProSelect.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var field = $get(fldName);
                               remSelOptions(field);
                               if(fldNm == 'ContractBranchID')
                               {
                                 remSelOptions($get('ContractUnitID_'+fldName.replace('ContractBranchID_', '')));
                               }
                               
                               remSelOptions($get(fldName));
                               addSelOptions($get(fldName), xhr.responseText);
                             }
                           }
                           
  xhr.send("fldName="+fldNm+"&filter="+filter);
}

function addProContactRow()
{
  var tbl = $get('proContactTbl');
  var maxID = 0;
  for(var z = 0; z < tbl.tBodies[0].rows.length; z++)
  {
    var sels = tbl.tBodies[0].rows[z].cells[0].getElementsByTagName('input');
    if(parseInt(sels[0].id.replace('ContactName_', '')) > maxID)
      maxID = parseInt(sels[0].id.replace('ContactName_', ''));

  }

  maxID++;
  var newRow = document.createElement('tr');
  for(var z = 0; z < tbl.tBodies[0].rows[0].cells.length; z++)
  {
    var newCell = document.createElement('td');
    var html = tbl.tBodies[0].rows[0].cells[z].innerHTML;
    newCell.innerHTML = html.replace(/_0/g, '_'+maxID);
    var sels = newCell.getElementsByTagName('select');
    for(var z1 = 0; z1 < sels.length; z1++)
    {
      sels[z1].selectedIndex = 0;
    }

    sels = newCell.getElementsByTagName('input');
    for(var z1 = 0; z1 < sels.length; z1++)
      sels[z1].value = '';

    newRow.appendChild(newCell);
  }

  tbl.tBodies[0].appendChild(newRow);
}

function remProContact(delBtn)
{
  delBtn.parentNode.parentNode.parentNode.removeChild(delBtn.parentNode.parentNode);
}

function addProParticipantRow()
{
  var tbl = $get('proParticipantTbl');
  var maxID = 0;
  for(var z = 0; z < tbl.tBodies[0].rows.length; z++)
  {
    var sels = tbl.tBodies[0].rows[z].cells[0].getElementsByTagName('input');
    if(parseInt(sels[0].id.replace('ParticipantName_', '')) > maxID)
      maxID = parseInt(sels[0].id.replace('ParticipantName_', ''));

  }

  maxID++;
  var newRow = document.createElement('tr');
  for(var z = 0; z < tbl.tBodies[0].rows[0].cells.length; z++)
  {
    var newCell = document.createElement('td');
    var html = tbl.tBodies[0].rows[0].cells[z].innerHTML;
    html = html.replace(/_0/g, '_'+maxID);
    newCell.innerHTML = html.replace(/\[0\]/g, '['+maxID+']');
    var sels = newCell.getElementsByTagName('select');
    for(var z1 = 0; z1 < sels.length; z1++)
    {
      sels[z1].selectedIndex = 0;
    }

    sels = newCell.getElementsByTagName('input');
    for(var z1 = 0; z1 < sels.length; z1++)
      sels[z1].value = '';

    newRow.appendChild(newCell);
  }

  tbl.tBodies[0].appendChild(newRow);
}

function remProParticipant(delBtn)
{
  delBtn.parentNode.parentNode.parentNode.removeChild(delBtn.parentNode.parentNode);
}

function getNextProConID(divCode)
{
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getNextProConID.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               $get('ProConIDNum').value = xhr.responseText;
                             }
                           }

  xhr.send('divCode='+divCode);
}

function setFieldsByProMethod(methodID)
{
  var allFields = ['VORArrangement', 'NoncompetitveExceptionID',
                   'AntLegalReview', 'LegalAdvisor', 'LegalReviewStart',
                   'LegalReviewComplete', 'ApprovalPosted', 'IssueDate',
                   'ProponentInfoSession', 'SiteVisit', 'ClosingDate'];
  var cinFields = ['AntLegalReview', 'LegalAdvisor', 'LegalReviewStart',
                   'LegalReviewComplete', 'ApprovalPosted', 'IssueDate',
                   'ProponentInfoSession', 'SiteVisit', 'ClosingDate'];
  var copFields = ['AntLegalReview', 'LegalAdvisor', 'LegalReviewStart',
                   'LegalReviewComplete', 'ApprovalPosted', 'IssueDate',
                   'ProponentInfoSession', 'SiteVisit', 'ClosingDate'];
  var nonFields = ['NoncompetitveExceptionID'];
  var vorFields = ['VORArrangement',
                   'AntLegalReview', 'LegalAdvisor', 'LegalReviewStart',
                   'LegalReviewComplete', 'ApprovalPosted', 'IssueDate',
                   'ProponentInfoSession', 'SiteVisit', 'ClosingDate'];
                   
  for(var z = 0; z < allFields.length; z++)
  {
    $get(allFields[z]).disabled = true;
    if(fieldByName(allFields[z]).comment)
      $get(allFields[z]).setAttribute("class", "disabled float_left");
    else
      $get(allFields[z]).setAttribute("class", "disabled");
      
  }

  switch(methodID)
  {
    case '2': //Competitive - Invitational
      for(var z = 0; z < cinFields.length; z++)
      {
        $get(cinFields[z]).disabled = false;
        if(fieldByName(cinFields[z]).comment)
          $get(cinFields[z]).setAttribute("class", "float_left");
        else
          $get(cinFields[z]).setAttribute("class", "");
          
      }
  
      break;
    case '1': //Competitive - Open
      for(var z = 0; z < copFields.length; z++)
      {
        $get(copFields[z]).disabled = false;
        if(fieldByName(copFields[z]).comment)
          $get(copFields[z]).setAttribute("class", "float_left");
        else
          $get(copFields[z]).setAttribute("class", "");
          
      }
  
      break;
    case '6': //Competitive Quotation
      for(var z = 0; z < copFields.length; z++)
      {
        $get(copFields[z]).disabled = false;
        if(fieldByName(copFields[z]).comment)
          $get(copFields[z]).setAttribute("class", "float_left");
        else
          $get(copFields[z]).setAttribute("class", "");
          
      }
  
      break;
    case '4': //Non-competitive - Single Source
      for(var z = 0; z < nonFields.length; z++)
      {
        $get(nonFields[z]).disabled = false;
        if(fieldByName(nonFields[z]).comment)
          $get(nonFields[z]).setAttribute("class", "float_left");
        else
          $get(nonFields[z]).setAttribute("class", "");
      }
  
      break;
    case '5': //Non-Competitive - Sole Source
      for(var z = 0; z < nonFields.length; z++)
      {
        $get(nonFields[z]).disabled = false;
        if(fieldByName(nonFields[z]).comment)
          $get(nonFields[z]).setAttribute("class", "float_left");
        else
          $get(nonFields[z]).setAttribute("class", "");
      }
  
      break;
    case '3': //VOR Arrangement
      for(var z = 0; z < vorFields.length; z++)
      {
        $get(vorFields[z]).disabled = false;
        if(fieldByName(vorFields[z]).comment)
          $get(vorFields[z]).setAttribute("class", "float_left");
        else
          $get(vorFields[z]).setAttribute("class", "");
      }
  
      break;
  }
}

function setBrkStat(key)
{
  var brk = $get('break_'+key+'_');
  srtVal = parseInt($get('sortOrder_'+key+'_').value[$get('sortOrder_'+key+'_').selectedIndex]);
  if((srtVal == 'NaN') || (srtVal < 1))
  {
    brk.disabled = true;
    brk.checked = false;
  }
  else
  {
    brk.disabled = false;
  }
}

function printReport()
{
  window.print();
  window.close();
}

function showSaveForm()
{
  $get('saveForm').style.visibility = 'visible';
}

function verifyDelete(what)
{
  return confirm('Are you sure you want to delete this '+what+'?');
}

function calcContractDates(tab)
{
  var conStartStr = $get('ContractStart_'+tab).value;
  var conStartDate = new Date(conStartStr);
  var conEndStr = $get('ContractEnd_'+tab).value;
  var conEndDate = new Date(conEndStr);
  $get('ContractTerm_'+tab).value = calcMonths(conStartDate, conEndDate);
  setFinalConEnd(tab);
}

function calcMonths(startDate, endDate)
{
  var days = (endDate - startDate) / (24 * 60 * 60 * 1000);
  var months = Math.round(days / 30);
/*  var startYear = startDate.getFullYear();
  var startMonth = startDate.getMonth();
  var startDay = startDate.getDay();
  var endYear = endDate.getFullYear();
  var endMonth = endDate.getMonth();
  var endDay = endDate.getDay();
  var months = parseInt((12 * (endYear - startYear)) + (endMonth - startMonth));
  if(isNaN(months))
    months = '';
*/
    
  return months;
}

function calcExtDates(tab)
{
  var extStartStr = $get('ExtensionStart_'+tab).value;
  var extStartDate = new Date(extStartStr);
  var extMonths = $get('ExtensionMonths_'+tab).value;
  if($get('ExtensionStatusID_'+tab).options[$get('ExtensionStatusID_'+tab).selectedIndex].value == 5)
  {
    $get('ExtensionStart_'+tab).value = '';
    $get('ExtensionEnd_'+tab).value = '';
    $get('ExtensionMonths_'+tab).value = '';
  }
  else
  {
    $get('ExtensionEnd_'+tab).value = calcEnd(extStartDate, extMonths);
  }
  
  setFinalConEnd(tab);
}

function calcEnd(startDate, months)
{
  var mons = months % 12;
  var yrs = (months - mons) / 12;
  var newYear = startDate.getFullYear() + yrs;
  var newMon = startDate.getMonth() + mons;
  if(newMon < 12)
  {
    newYear++;
    newMon -= 12;
  }
  
  var endDate = new Date();
  endDate.setFullYear(newYear);
  endDate.setMonth(newMon);
  endDate.setDate(startDate.getDate());
  return ((endDate.getMonth()+1)+'/'+endDate.getDate()+'/'+endDate.getFullYear());
}

function setFinalConEnd(tab)
{
  if($get('ExtensionStatusID_'+tab).options[$get('ExtensionStatusID_'+tab).selectedIndex].value == 2)
    $get('FinalContractEnd_'+tab).value = $get('ExtensionEnd_'+tab).value;
  else
    $get('FinalContractEnd_'+tab).value = $get('ContractEnd_'+tab).value;
  
}

function resetProcurement(proID)
{
  if(confirm('Are you sure you want to cancel changes?'))
  {
    if(proID == undefined)
      window.location='procurement.php';
    else
      window.location='procurement.php?ID='+proID;
      
  }
  else
    return false;
    
}

function getScorecard()
{
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getScorecard.php", true);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.onreadystatechange = function()
                           {
                             if (xhr.readyState == 4)
                             {
                               var fields = xhr.responseText.split("|");
                               if(fields[0] == 'OKAY')
                               {
                                 for(var z = 1; z < fields.length; z++)
                                 {
                                   var parts = fields[z].split("=");
                                   $get(parts[0]).innerHTML = parts[1];
                                 }
                               }
                               else
                               {
                                 alert(fields[0]);
                               }
                             }
                           }

  var startDate = $get('start_date').value;
  var endDate = $get('end_date').value;
  xhr.send('startDate='+startDate+'&endDate='+endDate);
}

function postUpdateToCart(form)
{
  var postStr = '';
  for(var z = 0; z < form.length; z++)
  {
    if(form.elements[z].tagName != 'FIELDSET')
    {
      if(postStr != '')
        postStr += '&';
        
      postStr += form.elements[z].name+'='+form.elements[z].value;
    }   
  }
  
  var xhr = createXHR();
  xhr.open("POST", form.action, false);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send(postStr);
}

function openOptionHelp(helpButton, optionID)
{
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getOptionHelp.php", false);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send('optionID='+optionID);
  var rows = xhr.responseText.split("\n");
  if(rows[0] == 'OKAY')
  {                    
    if(rows.length > 1)
    {
      var flds = rows[1].split('|');
      var title = flds[0];
      var helpText = flds[1];
      $get('optionHelpTitle').innerHTML = title;
      $get('optionHelpText').innerHTML = helpText;
      var helpOptionDiv = $get('optionHelp');
      helpOptionDiv.style.top = (getElementPos(helpButton).top) + 'px';
      helpOptionDiv.style.left = (getElementPos(helpButton).left - 310) + 'px';
      helpOptionDiv.style.display = 'block';   
    }
  }
  else
  {
    alert(xhr.responseText);
  }
}
  
function openAddOnHelp(helpButton, addOnID)
{
  var xhr = createXHR();
  xhr.open("POST", site_url+"ajax/getAddOnHelp.php", false);
  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xhr.send('addOnID='+addOnID);
  var rows = xhr.responseText.split("\n");
  if(rows[0] == 'OKAY')
  {                    
    if(rows.length > 1)
    {
      var flds = rows[1].split('|');
      var title = flds[0];
      var helpText = flds[1];
      $get('optionHelpTitle').innerHTML = title;
      $get('optionHelpText').innerHTML = helpText;
      var helpOptionDiv = $get('optionHelp');
      helpOptionDiv.style.top = (getElementPos(helpButton).top) + 'px';
      helpOptionDiv.style.left = (getElementPos(helpButton).left - 310) + 'px';
      helpOptionDiv.style.display = 'block';   
    }
  }
  else
  {
    alert(xhr.responseText);
  }
}
  
function closeOptionHelp()
{
  $get('optionHelp').style.display = 'none';
}

function getElementPos(elem)
{
  var offsetTrail = elem;
  var offsetLeft = 0;
  var offsetTop = 0;
  while(offsetTrail)
  {
    offsetLeft += offsetTrail.offsetLeft;
    offsetTop += offsetTrail.offsetTop;
    offsetTrail = offsetTrail.offsetParent;
  }
  
  if(navigator.userAgent.indexOf('Mac') != -1 &&
     typeof document.body.leftMargin != 'undefined')
  {
    offsetLeft += document.body.leftMargin;
    offsetTop += document.body.topMargin;
  }
  
  return {left:offsetLeft,top:offsetTop};
}


