function companyChange(){
  var companyId = $('companies').value;
  updateCompanyInfo(companyId);
  updateCompanyQuestions(companyId);
  updateCompanyPreferences(companyId);
  
  //since the company has changed
  //save the new state 
  var company_select = new Select();
  company_select.bind('companies');
  var storage = ClientSideStorage('account-init');
  storage.put('selected-company-name',company_select.getSelectedText());
  storage.save();
}
//function attachCompanyChange() {
//    Event.observe($('companies'), 'change', companyChange);
//}
//Event.observe(window, 'load', attachCompanyChange);

//function attachLeadGenChange() {
//    Event.observe($('lead_gen_options'), 'change', leadGenGuiderChange);
//}
//Event.observe(window, 'load', attachLeadGenChange);

function updateCompanyInfo(companyId){
  Ajax_send("/ajax/company/getCompanyInfo",{id: companyId},callbackCompanyInfo);
}
function callbackCompanyInfo(resp){
  cancelEditCompanyInfo();
  $('company-logo-form-ctl').value=resp.info.id;
  $('company-info-name').firstChild.data = resp.info.name;
  $('title-company-name').firstChild.data = resp.info.name;
  $('company-info-offer').firstChild.data = resp.info.offer;
  $('company-info-address1').firstChild.data = resp.info.address1;
  $('company-info-address2').firstChild.data = resp.info.address2;
  $('company-info-city').firstChild.data = resp.info.city;
  $('company-info-state').firstChild.data = resp.info.state;
  $('company-info-zipCode').firstChild.data = resp.info.zipCode;
  $('company-info-phone').firstChild.data = resp.info.phone;
  $('company-info-fax').firstChild.data = resp.info.fax;
  $('company-info-contactName').firstChild.data = resp.info.contactName;
  $('company-info-contactTitle').firstChild.data = resp.info.contactTitle;
  $('company-info-contactEmail').firstChild.data = resp.info.contactEmail;
  
  $('company-info-logoname').src = resp.info.logoName;
  $('company-info-logoname').alt = resp.info.name;
  $('edit-company-info-logoname').src = resp.info.logoName;
  $('edit-company-info-logoname').alt = resp.info.name;
}

function editCompanyInfo(){
  $('edit-company-info-logoname').src = $('company-info-logoname').src;
  $('edit-company-info-logoname').alt = $('company-info-logoname').alt;
  if ($('company-info-name').firstChild){
     $('edit-company-info-name').value = $('company-info-name').firstChild.data;
  }
  if ($('company-info-offer').firstChild){
     $('edit-company-info-offer').value = $('company-info-offer').firstChild.data;
  }
  if ($('company-info-address1').firstChild){
     $('edit-company-info-address1').value = $('company-info-address1').firstChild.data;
  }
  if ($('company-info-address2').firstChild){
     $('edit-company-info-address2').value = $('company-info-address2').firstChild.data;
  }
  if ($('company-info-city').firstChild){
     $('edit-company-info-city').value = $('company-info-city').firstChild.data;
  }
  if ($('company-info-state').firstChild){
     $('edit-company-info-state').value = $('company-info-state').firstChild.data;
  }
  if ($('company-info-zipCode').firstChild){
     $('edit-company-info-zipCode').value = $('company-info-zipCode').firstChild.data;
  }
  if ($('company-info-phone').firstChild){
     $('edit-company-info-phone').value = $('company-info-phone').firstChild.data;
  }
  if ($('company-info-fax').firstChild){
     $('edit-company-info-fax').value = $('company-info-fax').firstChild.data;
  }
  if ($('company-info-contactName').firstChild){
     $('edit-company-info-contactName').value = $('company-info-contactName').firstChild.data;
  }
  if ($('company-info-contactTitle').firstChild){
     $('edit-company-info-contactTitle').value = $('company-info-contactTitle').firstChild.data;
  }
  if ($('company-info-contactEmail').firstChild){
     $('edit-company-info-contactEmail').value = $('company-info-contactEmail').firstChild.data;
  }
  $('company-display').hide();
  $('company-edit').show();
}
function cancelEditCompanyInfo(){
  var company_select = new Select();
  company_select.bind('companies');

  $('title-company-name').firstChild.data = company_select.getSelectedText();
  $('company-logo-form-ctl').value = $('companies').value;
  $('company-edit').hide();
  $('company-display').show();
}
function saveCompanyInfo(){
  var storage = ClientSideStorage('account-init');
  storage.put('selected-company-name',$F('edit-company-info-name'));
  storage.save();

  document.forms['company-info-form'].submit();
}
function deleteCompany(){
  var storage = ClientSideStorage('account-init');
  storage.put('selected-company-name','');  //the selected company will be deleted 
  storage.save(); 
  $('submit-type').value = 'delete-company'
  document.forms['company-info-form'].submit();
  
}
function newCompanyInfo(){
  var inputs = Form.getInputs('company-info-form', 'text');
  inputs.each(function(item) {
                               item.clear();
                             });

  $('company-logo-form-ctl').value = -1;
  $('edit-company-info-logoname').src = "/images/logos/YourLogoHere.png"
  $('edit-company-info-logoname').alt = "Your Logo Here"
  $('title-company-name').firstChild.data = "New Company";
  $('company-display').hide();
  $('company-edit').show();
  
}

function updateCompanyQuestions(companyId){
  Ajax_send("/ajax/company/getCompanyLinks",{company_id: companyId}, callbackCompanyLinks);  
}

function callbackCompanyLinks(resp){
  var links_table = "<table><thead><tr><th>Guider</th><th>Question</th><th>Lead Gen Guider</th></tr></thead><tbody>";
  questions = resp.attached_questions;
  for(q in questions){
    if(questions.hasOwnProperty(q)){
        link = questions[q];
        links_table += createLinkRow(link["origin_guider_slug"], link["origin_question_slug"], link["lgg_name"]);
    }
  }
  $('company-links').innerHTML = links_table;
  return true;
}
function createLinkRow(guider, question, lgg_name){
   return "<tr><td>" + guider + "</td><td><a href='/guider/" + guider + "/" + question + "'>" + question + "</a></td><td>" + lgg_name + "</td></tr>";
} 
function updateCompanyPreferences(companyId){
   $('pref-company-id').value = companyId;
   leadGenGuiderChange();
   return true;
}

function leadGenGuiderChange(){
  var lggSlug = $('lead_gen_options').value;
  var companyId = $('companies').value;
  $("gq_slug").value = lggSlug;
  Ajax_send("/ajax/company/getCompanyPreferences",{company_id: companyId, lgg_slug: lggSlug}, callbackLeadGen);
} 

function callbackLeadGen(resp){
  var pref_table = "<table>";
  var prefs = resp.prefs;
  for(question in prefs){
    gq_slug = question;
    question_text = prefs[question][0][1];
    pref_table += createPrefHeaderRow(question_text);
    for(var i = 0; i < prefs[question].length; i++){
        answer = prefs[question][i];
        answer_slug = answer[0];
        answer_text = answer[2];
        selected = answer[3];
        pref_table += createPrefRow(gq_slug, answer_slug, answer_text, selected);
    }
  }
  pref_table += "</table>";
  $('company-prefs').innerHTML = pref_table;
  return true;
}
function createPrefHeaderRow(question_text){
  row = "";
  row += "<tr><th colspan='2'>"+ question_text + "</th></tr>";
  return row;
}
function createPrefRow(gq_slug, answer_slug, answer_text, selected){
  row = "";
  row += "<tr><td>";
  row += "<input type='checkbox'";
  row += " name='prefs[]' value='"+ gq_slug +":"+ answer_slug +"' ";
  if(selected){
    row += " checked='checked' ";
  }
  row += "/></td><td>";
  row += answer_text;
  row += "</td></tr>";
  return row;
}

function deleteLead(lead_id){
  Ajax_send('/ajax/leads/delete',{id: lead_id},callbackDeleteLead);
}
function unDeleteLead(lead_id){
  Ajax_send('/ajax/leads/undelete',{id: lead_id},callbackunDeleteLead);
}
function hideLead(lead_id){
  Ajax_send('/ajax/leads/hide',{id: lead_id},callbackHideLead);
}
function unHideLead(lead_id){
  Ajax_send('/ajax/leads/unhide',{id: lead_id},callbackUnhideLead);
}
function callbackDeleteLead(response){
  $("lead-info-"+response.request.parameters.id).style.backgroundColor="pink";
  $("lead-info-"+response.request.parameters.id).style.color="gray";
  $("delete-ctl-"+response.request.parameters.id).style.display="none";
  $("undelete-ctl-"+response.request.parameters.id).style.display="";  
}
function callbackunDeleteLead(response){
  $("lead-info-"+response.request.parameters.id).style.backgroundColor="";
  $("lead-info-"+response.request.parameters.id).style.color="";
  $("delete-ctl-"+response.request.parameters.id).style.display="";
  $("undelete-ctl-"+response.request.parameters.id).style.display="none";
}
function callbackHideLead(response){
  $("lead-info-"+response.request.parameters.id).style.backgroundColor="lightblue";
  $("lead-info-"+response.request.parameters.id).style.color="gray";  
  $("hide-ctl-"+response.request.parameters.id).style.display="none";
  $("unhide-ctl-"+response.request.parameters.id).style.display="";
}
function callbackUnhideLead(response){
  $("lead-info-"+response.request.parameters.id).style.backgroundColor="";
  $("lead-info-"+response.request.parameters.id).style.color="";  
  $("hide-ctl-"+response.request.parameters.id).style.display="";
  $("unhide-ctl-"+response.request.parameters.id).style.display="none";
}

function checkProfileLinks(){
    var link_elements = $$("input.profile-link");
    var filled_elements = 0;
    for(var i = 0; i < link_elements.length; i++){
        if(link_elements[i].value != ""){
            filled_elements += 1;
        }
    }
    if(filled_elements > 3){
        alert("Max of 3 profile links.\nPlease remove one before saving.");
        return false;
    }else{
        return true;
    }
}

// Multiple contributors
function formMultipleContributors(guider_id){
	Modalbox.show("/forms/multiple_contributors.php?guider_id=" + guider_id,
	  {
	  	title: "Additional Contributors"
	  });
}

function multiple_contributors_add(){
  Ajax_send(
    "/ajax/guiders/addContributor",
    {
	     user_id:    $('contrib-user').value,
	     guider_id:  $('guider-id').value
	  },
	  multiple_contributors_update_users
	);
}
function multiple_contributors_update_users(resp){
    $("g" + resp.guider_id + "-contrib").innerHTML = "<span style='font-weight:bold;color:#C11;'>" + resp.contrib_summary + "</span>";
    return true;
}
function multiple_contributors_remove(user_id){
  Ajax_send(
    "/ajax/guiders/removeContributor",
    {
	     user_id:    user_id,
	     guider_id:  $('guider-id').value
	  },
	  multiple_contributors_update_users
	);
}

