//----------------------------------------------------------------------
//-
//-		arts & crafts functions
//-
//----------------------------------------------------------------------
function updatePayBtns(frm, sListingType){
  var sPayBtnIds = frm["paybtnids"].value;
  var sFreeBtnId = "paybtnFree";
  var aPayBtnIds = null;
  if(sPayBtnIds != ""){
    aPayBtnIds = sPayBtnIds.split(",");
  }

  switch(sListingType.toLowerCase()){  
    case "free":
      showInline(sFreeBtnId);
      if(aPayBtnIds != null){
        for(var i=0; i<aPayBtnIds.length; i++){
          hide(aPayBtnIds[i]);
        }
      }
      break;
      
    case "basic":
	  case "premium":
	    hide(sFreeBtnId);
      if(aPayBtnIds != null){
        for(var i=0; i<aPayBtnIds.length; i++){
          showInline(aPayBtnIds[i]);
        }
      }
      break;
  }
  return true;
}

function alertTooManyCategories(){
  alert("Only 1 category is permitted for a free listing.\n\nIf you would like to be listed in multiple categories,\nplease upgrade to a basic or premium listing.\n ");
}

function clearForm(frm){
  var oElement;
  for(var i=0; i<frm.elements.length; i++){
    oElement = frm.elements[i];
    if(oElement.type == "text" || oElement.type == "textarea"){
      oElement.value = "";
    }
    if(oElement.type == "select" || oElement.type == "select-multiple"){
      oElement.selectedIndex = -1;
    }
  }
}

function validateForm(frm){
  var bIsValid = true;
  
  // validate company info 
  var sCompanyMsgs = "";
  var oListOptions = frm["listopt"];
  var oSelect = frm["category"];
  if(isAllSpaces(frm["company"].value)){ sCompanyMsgs += "\t - please enter a name\n"; }
  if(isAllSpaces(frm["address"].value)){ sCompanyMsgs += "\t - please enter an address\n"; }
  if(isAllSpaces(frm["phone"].value)){ sCompanyMsgs += "\t - please enter a phone number\n"; }
  
  var bIsPaid = false;
  for(var i=0; i<oListOptions.length; i++){
    if(oListOptions[i].checked && oListOptions[i].value.toLowerCase() != "free"){ bIsPaid = true; }
  }
  if(bIsPaid){
    if(!isAllSpaces(frm["hlemail"].value) && frm["hlemail"].value.search(/\w@\w/) == -1){ sCompanyMsgs += "\t - please enter a valid email address\n"; }
  }
  
  if(oSelect.selectedIndex == -1 || oSelect.options[oSelect.selectedIndex].value == ""){ sCompanyMsgs += "\t - please select a category\n"; }
  else if(!bIsPaid && !validateCategory(frm)){ sCompanyMsgs += "\t - only 1 category is permitted for a free listing\n"; }
  if(isAllSpaces(frm["description"].value)){ sCompanyMsgs += "\t - please enter a company description\n"; }
  if(!isAllSpaces(frm["description"].value) && trim(frm["description"].value).length > 360){ sCompanyMsgs += "\t - please enter briefer description\n"; }
  if(sCompanyMsgs != ""){ 
    bIsValid = false;
    sCompanyMsgs = "Please correct the following Company Information:\n" + sCompanyMsgs + "\n";
  }
  
  // validate contact info
  var sContactMsgs = "";
  if(isAllSpaces(frm["fullname"].value)){ sContactMsgs += "\t - please enter a name\n"; }
  if(isAllSpaces(frm["email"].value)){ sContactMsgs += "\t - please enter an email address\n"; }
  else if(frm["email"].value.search(/\w@\w/) == -1){ sContactMsgs += "\t - please enter a valid email address\n"; }
  if(isAllSpaces(frm["cphone"].value)){ sContactMsgs += "\t - please enter a phone number\n"; }
  if(sContactMsgs != ""){ 
    bIsValid = false;
    sContactMsgs = "Please correct the following Contact Information:\n" + sContactMsgs + "\n";
  }
  
  if(!bIsValid){
    var sMsg;
    if(sCompanyMsgs != "" && sContactMsgs != ""){ sMsg = sCompanyMsgs + sContactMsgs; }
    else if(sCompanyMsgs != ""){ sMsg = sCompanyMsgs; }
    else if(sContactMsgs != ""){ sMsg = sContactMsgs; }
    alert(sMsg);
  }
  
  return bIsValid;
}


function validateCategory(frm){
  var oListOptions = frm["listopt"];
  var oSelect = frm["category"];
  var bIsFree = false;
  for(var i=0; i<oListOptions.length; i++){
    if(oListOptions[i].checked && oListOptions[i].value.toLowerCase() == "free"){ bIsFree = true; }
  }
  var iNumSelected = 0;
  if(bIsFree){
    for(var i=0; i<oSelect.length; i++){
      if(oSelect.options[i].selected){ iNumSelected++; }
    }
  }
  return (!bIsFree || (bIsFree && iNumSelected < 2));
}