YAHOO.namespace("opolis.windows");

function showMe(id){
  document.getElementById(id).style.display="inline";
}
 
function hideMe(id){
  document.getElementById(id).style.display="none";
}

function toggle_view(id) {
  if(document.getElementById(id).style.display == "none") {
    document.getElementById(id).style.display = "";
  }
  else {
    document.getElementById(id).style.display = "none";
  }
}


function toggle_view_edit(id,rid) {
  if(document.getElementById(id).style.display == "none" && document.getElementById(rid).checked == true) {
    document.getElementById(id).style.display = "";
  }
  else if(document.getElementById(id).style.display == "" && document.getElementById(rid).checked == false){
    document.getElementById(id).style.display = "none";
  }
}

function expand(expandIcon, id) {
  if(document.getElementById(id).style.display == "none") {
    document.getElementById(id).style.display = "";
    document.getElementById(expandIcon).src="minus.gif";
  }
  else {
    document.getElementById(id).style.display = "none";
    document.getElementById(expandIcon).src="plus.gif";
  }
}

function expand_zip(zip) {
  ajaxpage('viewcommunities.php?zip=' + encodeURI(zip), 'contentarea');
}


function expand_collection(expandIcon, id, row_id) {
  if(document.getElementById(id).style.display == "none") {
    document.getElementById(id).style.display = "";
    document.getElementById(expandIcon).src="minus.gif";
    document.getElementById(row_id).style.backgroundColor="#DDDEEE";
  }
  else {
    document.getElementById(id).style.display = "none";
    document.getElementById(expandIcon).src="plus.gif";
    document.getElementById(row_id).style.backgroundColor="#FFFFFF";
  }
}


function publish(id, div, checkbox, collection_div) {
	ajaxpage('publish.php?id=' + encodeURI(id), 'message_section');
	if (document.getElementById(checkbox).checked == false) {
		document.getElementById(div).innerHTML = '<b style="font-weight:normal;color: red;">not published</b>';
		if (collection_div != '0') {
			var published_listings = document.getElementById(collection_div).innerHTML*1;
			var published_listings = published_listings-1;
			document.getElementById(collection_div).innerHTML = published_listings;
		}
	}
	else if (document.getElementById(checkbox).checked == true) {
		document.getElementById(div).innerHTML = '<b style="font-weight:normal;color: green;">published</b>';
		if (collection_div != '0') {
			var published_listings = document.getElementById(collection_div).innerHTML*1;
			var published_listings = published_listings+1;
			document.getElementById(collection_div).innerHTML = published_listings;
		}
	}
}


function toggle_submenu(id) {
  if(document.getElementById(id).style.visibility == "visible") {
    document.getElementById(id).style.visibility = "hidden";
  }
  else {
    document.getElementById(id).style.visibility = "visible";
  }
}



function toggle_messages(id) {
  if(document.getElementById(id+".1").style.display == "none") {
    document.getElementById(id+".1").style.display = "";
    document.getElementById(id+".2").style.display = "none";
  }
  else {
    document.getElementById(id+".2").style.display = "";
    document.getElementById(id+".1").style.display = "none";
  }
}

function update_checkboxes(category) {

  ajaxpage('neighborhood_log.php?category='+category, 'neighborhood_empty_div');
  
if(category == "neighborhood_related") {
    document.getElementById("neighborhood_checkbox").checked = true;
  } else {
    document.getElementById("neighborhood_checkbox").checked = false;
  }

  if(category == "recommendations") {
    document.getElementById("recommendations_checkbox").checked = true;
  } else {
    document.getElementById("recommendations_checkbox").checked = false;
  }


  if(category == "for_sale") {
    document.getElementById("for_sale_checkbox").checked = true;
  } else {
    document.getElementById("for_sale_checkbox").checked = false;
  }


  if(category == "looking_for") {
    document.getElementById("looking_for_checkbox").checked = true;
  } else {
    document.getElementById("looking_for_checkbox").checked = false;
  }


  if(category == "real_estate") {
    document.getElementById("real_estate_checkbox").checked = true;
  } else {
    document.getElementById("real_estate_checkbox").checked = false;
  }


  if(category == "miscellaneous") {
    document.getElementById("miscellaneous_checkbox").checked = true;
  } else {
    document.getElementById("miscellaneous_checkbox").checked = false;
  }

  toggle_neighborhood(category);
}

function toggle_neighborhood(category) {
  if(document.getElementById("neigh.1").style.display == "none") {
    document.getElementById("neigh.1").style.display = "";
    document.getElementById("neigh.2").style.display = "none";
    document.getElementById("neighscoop").src="images/neighminus.jpg";
    document.getElementById("allneigh").src ="images/neighplus.jpg";
  }
  else {
      if(category != "") {
        ajaxpage('neighborhood_log.php?category=0', 'neighborhood_empty_div');
        ajaxpagepost('view_neighborhood.php?div=neighborhoodall&'+category+'=1','neighborhoodall','nviewpost');
      } else {
        ajaxpage('neighborhood_log.php?category=0', 'neighborhood_empty_div');
        ajaxpagepost('view_neighborhood.php?div=neighborhoodall','neighborhoodall','nviewpost');
      }
    document.getElementById("neigh.2").style.display = "";
    document.getElementById("neigh.1").style.display = "none";
    document.getElementById("neighscoop").src="images/neighplus.jpg";
    document.getElementById("allneigh").src ="images/neighminus.jpg";
  }
}

//javascript:toggle_messages('neigh');ajaxpagepost('view_neighborhood.php?div=neighborhoodall','neighborhoodall','nviewpost');

function removeMe(id){
  var parent = document.getElementById(id).parentNode;
  parent.removeChild(document.getElementById(id));
}



function respond(id) {
  var x = document.getElementById('replyButton').value;

  if(x == 'reply') {
  showMe(id);
  document.getElementById('replyButton').value="discard reply";
  }
  else {
  hideMe(id);
  document.getElementById('replyButton').value="reply";
  }
}

function msgPost(id) {
  var x = document.getElementById('msgPostButton').value;

  if(x == 'Post to List') {
  showMe(id);
  document.getElementById('msgPostButton').value="Discard Post";
  }
  else {
  hideMe(id);
  document.getElementById('msgPostButton').value="Post to List";
  }
}

function forward(id) {
  var x = document.getElementById('forwardButton').value;

  if(x == 'forward') {
  showMe(id);
  document.getElementById('forwardButton').value="discard forward";
  }
  else {
  hideMe(id);
  document.getElementById('forwardButton').value="forward";
  }
}


function search() {
  var query = document.getElementById('search_field').value;
  ajaxpage('search.php?query=' + encodeURI(query), 'contentarea');
}

function search2() {
  var query = document.getElementById('search_field2').value;
  var name = document.getElementById('var_name').value;
  var value = document.getElementById('var_value').value;
  ajaxpage('search.php?div=contentarea&query=' + encodeURI(query) + '&' + encodeURI(name) + '=' + encodeURI(value), 'contentarea');
}

function publicsearch() {
  var query = document.getElementById('public_search_field').value;
  ajaxpage('publicsearch.php?div=publiccontentarea&query=' + encodeURI(query), 'publiccontentarea');
}


function setMessage(message) {
  if(document.getElementById('message_section')) {

    if(message == "") {
      document.getElementById('message_section').style.display="none";
    }
    else {
      document.getElementById('message_section').style.display="";
    }
    document.getElementById('message_section').innerHTML = '<b style="color: #CF4545;">' + message + '</b><br><br>';
  }
}

function setMessage2(message, divID) {
  if(document.getElementById(divID)) {
    document.getElementById(divID).innerHTML = '<b style="display: inline; font-size: 14px; color: #CF4545;">' + message + '</b>';
  }
}


function ajaxpagepost(url, containerid, form_id) {
  new Ajax.Updater(containerid, url, {parameters: $(form_id).serialize(true), evalScripts: 'true', method: 'post', asynchronous: true}); 
}



function ajaxpage(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true' });
}


function ajaxpage_home(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=block', 'contentarea');} });
}

function ajaxpage_home2(url, containerid){
  //new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&refresh=1&case=block', 'contentarea');} });
}

function ajaxpage_building(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=building', 'contentarea');} });
}

function ajaxpage_neighborhood(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=neighborhood', 'contentarea');} });
}


function ajaxpage_replies(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=myReplies', 'contentarea');} });
}

function ajaxpage_listings(url, containerid){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('listing_management.php', 'listing_management_area')} });
}

function ajaxpage_listings_window(url, getParameter, containerid, loaderPage, loaderArea) {
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', parameters: {type: getParameter}, onSuccess: function(transport) {ajaxpage(loaderPage, loaderArea)} });
}


function ajaxpage2(url, containerid, loaderPage, loaderArea){
  new Ajax.Updater(containerid, url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage(loaderPage, loaderArea)} });
}


function ajaxpage_tagsearch(tag_name, div_name) {
  var safe_tag_name = encodeURI(tag_name);
  new Ajax.Updater('contentarea', 'search.php', { method: 'get', evalScripts: 'true', parameters: {tag_search: 'y', query: safe_tag_name , div: div_name} });
}

function ajaxpage_publictagsearch(tag_name, div_name) {
  var safe_tag_name = encodeURI(tag_name);
  new Ajax.Updater('publiccontentarea', 'publicsearch.php', { method: 'get', evalScripts: 'true', parameters: {tag_search: 'y', query: safe_tag_name , div: div_name} });
}

function showWindow(url) {
  new Ajax.Updater('windowBody', url, { method: 'get', evalScripts: 'true', onSuccess: function(transport) {
     var myPanel = YAHOO.opolis.windows.myPanel;

     myPanel.setHeader("www.opolis.com");
     myPanel.setBody(transport.responseText); 
     myPanel.render();
     myPanel.show();
   } });
}

function clearMsgText(id) {
  if(document.getElementById(id).value == 'Type your response here') {
    document.getElementById(id).value = '';
  }
}

function showWindowPublic(url) {
new Ajax.Request(url,
  {
    method:'get',
    evalScripts: 'true',
    onSuccess: function(transport){

     var myPanel = YAHOO.opolis.windows.myPanelPublic;


     myPanel.setHeader("Public Listing Window");
     myPanel.setBody(transport.responseText); 
     myPanel.setFooter("<b>Reply to this listing | Send to friend</b>");
     myPanel.render();
     myPanel.show();
     }
  });

}

function closeWindow() {
  var myPanel = YAHOO.opolis.windows.myPanel;
  myPanel.setHeader("");
  myPanel.setBody("");
  myPanel.setFooter("");
  myPanel.hide();
}

function navigateTo(target, stateName) {
  setMessage("");
  YAHOO.util.History.navigate(target, stateName);
}


function clear_content(containerid) {
  document.getElementById(containerid).innerHTML="";
}

function sendMessage(form_id, target) {
    new Ajax.Updater(target, 'save_reply.php', {method: 'post', parameters: $(form_id).serialize(true)});
}


//this is for the tags on postlisting

var counter1 = 0;
var counter2 = 0;
var counter3 = 0;

function clearTags() {
  counter1 = 0;
  counter2 = 0;
  counter3 = 0;
  removeElement('tagID1');
  removeElement('tagID2');
  removeElement('tagID3');
}

function addElement(value) {
  var ni = document.getElementById('selectedTags');
  sum = counter1 + counter2 + counter3;
  if(sum < 3) {
    if(counter1 == 0) {
      myCounter = 1;
      counter1 = 1;
    }
    else if (counter2 == 0) {
      myCounter = 2;
      counter2 = 1;
    }
    else {
      myCounter = 3;
      counter3 = 1;
    }

    var newdiv = document.createElement('div');
    newdiv.style.display = "inline";
    newdiv.setAttribute('id','tagID'+myCounter);
    newdiv.innerHTML = '<b>[<a href="javascript:removeElement(\'tagID'+myCounter+'\');"><font style="font-weight: strong;" color="#D52525"><b>x </b></font></a><input type="hidden" name="tag' + myCounter + '" value="' + value + '">' + value + ']</b>&nbsp;';
    ni.appendChild(newdiv);
  }
}

function addNewElement(ID) {
  value = document.getElementById(ID).value;

  if(value == "") {} else {

  var ni = document.getElementById('selectedTags');
  sum = counter1 + counter2 + counter3;
  if(sum < 3) {
    if(counter1 == 0) {
      myCounter = 1;
      counter1 = 1;
    }
    else if (counter2 == 0) {
      myCounter = 2;
      counter2 = 1;
    }
    else {
      myCounter = 3;
      counter3 = 1;
    }


    var newdiv = document.createElement('div');
    newdiv.style.display = "inline";
    newdiv.setAttribute('id','tagID'+myCounter);
    newdiv.innerHTML = '<b>[<a href="javascript:removeElement(\'tagID'+myCounter+'\');"><font style="font-weight: strong;" color="#D52525"><b>x </b></font></a><input type="hidden" name="tag' + myCounter + '" value="' + value + '">' + value + ']</b>&nbsp;';
    ni.appendChild(newdiv);
    }
  }
}

function removeElement(divNum) {
  if(divNum == 'tagID1') {
    counter1 = 0;
  }
  else if(divNum == 'tagID2') {
    counter2 = 0;
  }
  else {
    counter3 = 0;
  }
  var d = document.getElementById('selectedTags');
  var olddiv = document.getElementById(divNum);
  if(olddiv) {    
    d.removeChild(olddiv);
  }
}



function spawnWindow(url, name) {
  myRef = window.open(url, name);
}

function refresh() {
    var myModuleCurrentState = YAHOO.util.History.getCurrentState("main_body");
    updateState(myModuleCurrentState);
}



function updateState(state) {
  switch(state) {
    case "home":
      if(window["tmpv"] == 1) {} else {
      ajaxpage_home("home_content.php","main_body");
      }
      window["tmpv"] = 0;
    break;
    case "home2":
      ajaxpage_home2("home_content.php","main_body");
    break;
    case "neighborhood":
      if (!document.getElementById('tabarea')) {
        new Ajax.Updater("main_body", "home_content.php", { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('tabs.php?case=neighborhood', 'tabarea');ajaxpage('showThreads.php?div=contentarea&case=neighborhood', 'contentarea');} });
      } else{
        new Ajax.Updater("tabarea", "tabs.php?case=neighborhood", { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=neighborhood', 'contentarea');} });
      }
      window["tmpv"] = 0;
    break;
    case "neighborhood_init":   
      new Ajax.Updater("main_body", "home_content.php", { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('tabs.php?case=neighborhood', 'tabarea');ajaxpage('showThreads.php?div=contentarea&case=neighborhood', 'contentarea');} });
      window["tmpv"] = 0;
    break;
    case "listing_management":
      ajaxpage_listings("view_messages.php","main_body");
    break;
    case "listing_management_rep":
      ajaxpage_listings_window('view_messages.php','repliedListings', 'main_body', 'myResponded.php', 'listing_management_area');
    break;
    case "edit_account":
	ajaxpage("edit_account.php", "main_body");
    break;
    case "fs_t":
      if (!document.getElementById('tabarea')) {
        new Ajax.Updater("main_body", "home_content.php", { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('tabs.php?case=messages', 'tabarea');ajaxpage('showThreads.php?div=contentarea&case=myReplies', 'contentarea');} });
      } else {
	new Ajax.Updater("tabarea", "tabs.php?case=messages", { method: 'get', evalScripts: 'true', onSuccess: function(transport) {ajaxpage('showThreads.php?div=contentarea&case=myReplies', 'contentarea');} });      
      }
    break;    
    case "def":
    //alert(window["tmpv"]);
    break;
    default:
  }
}





 


