var objForm
var shp = 0
var con = 1
var tpb = 2

function RequireEmail(blnRequireEmail) {
   if (ie4)
      {
      if (blnRequireEmail)
         {
         document.all.ContactEmail.className="formTxtR"
         document.all.ContactEmail.innerText="E-Mail*: "
         }
      else
         {
         document.all.ContactEmail.className="formTxt"
         document.all.ContactEmail.innerText="E-Mail: "
         }
      }
} //end function


function editRequesterAffiliation() {
   if ((!objForm.chkAffShp.checked) && (!objForm.chkAffCon.checked) && (!objForm.chkAffTpb.checked)) {
      return warnInvalid(objForm.chkAffShp,'Please check at least one "Requester Affiliation".')
   }
   if ((objForm.chkAffShp.checked) && (!objForm.chkAffCon.checked) && (!objForm.optPay[0].checked)) {
      return warnInvalid(objForm.optPay[0],'The payment terms must be PREPAID if you are the shipper.')
   }
   if ((!objForm.chkAffShp.checked) && (objForm.chkAffCon.checked) && (!objForm.optPay[1].checked)) {
      return warnInvalid(objForm.optPay[0],'The payment terms must be COLLECT if you are the consignee.')
   }
   return true
}

function editPartyInfo(intParty, strParty, blnAffiliated) {
   var intStrippedNum

   if ((objForm.txtAcctName[intParty].value.length < 1) && (blnAffiliated)){
      return warnInvalid(objForm.txtAcctName[intParty],'A ' + strParty + ' company name is required.')
   }
   if (((objForm.txtAcctCity[intParty].value.length < 1) ||
      (objForm.cboAcctState[intParty].selectedIndex < 1)) &&
      (objForm.txtAcctZip[intParty].value.length < 1)) {
          return warnInvalid(objForm.txtAcctCity[intParty],'A ' + strParty + ' city and state OR zip is required.')
   }
   if (objForm.txtAcctPhone[intParty].value.length > 0) {
      intStrippedNum = stripCharsNotInBag (objForm.txtAcctPhone[intParty].value, reInteger)
      if (isUSPhoneNumber(intStrippedNum)) {
         objForm.txtAcctPhone[intParty].value = reformatUSPhone(intStrippedNum)
      }
      else {
         return warnInvalid(objForm.txtAcctPhone[intParty],'An invalid ' + strParty + ' phone number was entered.\n\nPlease re-enter in the form (111) 222-3333.')
      }
   }
   return true
}

function editShipmentSpecifics()
{
   var blnEquip = false
   if ((objForm.txtLength.value.length < 1) || (!isInteger(objForm.txtLength.value)) || (objForm.txtLength.value < 1)) {
      return warnInvalid(objForm.txtLength, 'A valid length is required.')
   }
   if ((objForm.txtWidth.value.length < 1) || (!isInteger(objForm.txtWidth.value)) || (objForm.txtWidth.value < 1)) {
      return warnInvalid(objForm.txtWidth,'A valid width is required.')
   }
   if ((objForm.txtHeight.value.length < 1) || (!isInteger(objForm.txtHeight.value)) || (objForm.txtHeight.value < 1)) {
      return warnInvalid(objForm.txtHeight,'A valid height is required.')
   }
   for (var i = 0; i < objForm.optEquipDesc.length; i++) {
      if (objForm.optEquipDesc[i].checked) {
         blnEquip = true
      }
   }
   if ((isWhitespace(objForm.txtOther.value)) && (objForm.optEquipDesc[objForm.optEquipDesc.length - 1].checked)) {
      return warnInvalid(objForm.txtOther, 'Please specify the type of equipment you require when selecting "Other" or select a different type.')
   }
   if (!blnEquip) {
      return warnInvalid(objForm.optEquipDesc[0], 'Please select an equipment type.')
   }
   return true
}

function editAdditionalInfo() {
   if ((objForm.txtReqPrice.value.length > 0) && (!objForm.optReqTerms[0].checked) && (!objForm.optReqTerms[1].checked)) {
      return warnInvalid(objForm.txtReqPrice,'Please select either per shipment or per mile.')
   }
   if ((objForm.txtReqPrice.value.length > 0) && (!isFloat(objForm.txtReqPrice.value))) {
      return warnInvalid(objForm.txtReqPrice,'Please enter a whole dollar amount for your requested price.')
   }
   return true
}


function editCommodityInfo(i,txtHandled,cboHandledType,txtPackages,cboPackagesType,txtWeight,txtItem,txtSub,cboClass,txtDescription)
{
   if ((i > 0) && ((txtItem.value.length == 0) && (txtSub.value.length == 0)) &&
      ((cboClass.selectedIndex < 1) && (txtDescription.value.length == 0))) {
      return true
   }
   if ((txtHandled.value.length < 1) || (!isInteger(txtHandled.value))) {
      return warnInvalid(txtHandled,'Please enter a valid handling units amount for this commodity.')
   }
   if (cboHandledType.selectedIndex < 1) {
      return warnInvalid("",'Please enter a valid handling units type for this commodity.')
   }
   if (((txtPackages.value.length > 0) && (!isInteger(txtPackages.value))) ||
      ((txtPackages.value.length == 0) && (cboPackagesType.selectedIndex > 0)))  {
          return warnInvalid(txtPackages,'Please enter a valid number of packages for this commodity.')
   }
   if ((txtPackages.value.length > 0) && (cboPackagesType.selectedIndex < 1))  {
      return warnInvalid(txtPackages,'Please enter a valid package type for this commodity.')
   }
   if ((txtWeight.value.length < 1) || (!isInteger(txtWeight.value)))  {
      return warnInvalid(txtWeight,'Valid shipment weight is required.')
   }
   if ((txtItem.value.length == 0) && ((cboClass.selectedIndex < 1) || (txtDescription.value.length == 0)))  {
      return warnInvalid(txtDescription,'A freight description and class or a valid NMFC Item and Sub are required for this commodity.')
   }
   return true
}

function editRequesterContactInfo() {
   if (objForm.txtContactName.value.length < 1) {
      return warnInvalid(objForm.txtContactName,'A contact name is required.')
   }
   return true
}

function editRequesterEmailInfo() {
   if (objForm.txtContactEmail.value.length < 1) {
      return warnInvalid(objForm.txtContactEmail,'A contact email address is required.')
   }
   else if (!ValidEMail(objForm.txtContactEmail.value)) {
      return warnInvalid(objForm.txtContactEmail,'An invalid contact e-mail address was entered. Please re-enter it now.')
   }
   return true
}

function editRequesterPhoneInfo() {
   var intStrippedNum
   if (objForm.txtContactPhone.value.length < 1) {
      return warnInvalid(objForm.txtContactPhone,'A contact phone number is required.')
   }
   else {
      intStrippedNum = stripCharsNotInBag (objForm.txtContactPhone.value, reInteger)
      if (isUSPhoneNumber(intStrippedNum)) {
         objForm.txtContactPhone.value = reformatUSPhone(intStrippedNum)
      }
      else {
         return warnInvalid(objForm.txtContactPhone,'An invalid contact phone number was entered. Please re-enter itnow.')
      }
   }
   return true
}

function validate() {
   if (!editRequesterAffiliation()) {
      return false
   }
   if (!editRequesterContactInfo()) {
      return false
   }
   if (!editRequesterEmailInfo()) {
      return false
   }
   if (!editRequesterPhoneInfo()) {
      return false
   }
   if (!editPartyInfo(0, 'Shipper', objForm.chkAffShp.checked)) {
      return false
   }
   if (!editPartyInfo(1, 'Consignee', objForm.chkAffCon.checked)) {
      return false
   }
   if (objForm.chkAffTpb.checked) {
      if (!editPartyInfo(2, 'Third Party', objForm.chkAffTpb.checked)) {
         return false
      }
   }
   if ((!objForm.chkAffTpb.checked) && (objForm.txtAcctCity[2].value.length != 0)) {
      return warnInvalid(objForm.chkAffTpb,'Please check that you are the Third Party.')
   }
   if (!editShipmentSpecifics()) {
      return false
   }
   if (objForm.txtWeight.length == null) {
      if (!editCommodityInfo(0,
                        objForm.txtHandled,
                        objForm.cboHandledType,
                        objForm.txtPackages,
                        objForm.cboPackagesType,
                        objForm.txtWeight,
                        objForm.txtItem,
                        objForm.txtSub,
                        objForm.cboClass,
                        objForm.txtDescription)) {
         return false
      }
   }
   else {
      for (var i = 0; i < objForm.txtWeight.length; i++) {
         if (!editCommodityInfo(i,
                           objForm.txtHandled[i],
                           objForm.cboHandledType[i],
                           objForm.txtPackages[i],
                           objForm.cboPackagesType[i],
                           objForm.txtWeight[i],
                           objForm.txtItem[i],
                           objForm.txtSub[i],
                           objForm.cboClass[i],
                           objForm.txtDescription[i])) {
            return false
         }
      }
   }
   if (!editAdditionalInfo()) {
      return false
   }

   return true
}                                                     