<!--
//Object connections for the navigation links
var objectConnections = [];

//Navigation click - Fade out / Fade in
var route_navclick = function(page, page_node, email){
  var contentNode = dojo.byId(page_node);
  var dContentNode = dijit.byId(page_node);

  dojo.xhrGet({
    url: "./content.php?page=" + page,
    handleAs: "text",
    load: function(data,args){
      // fade out the node we're modifying
      dojo.fadeOut({
        node: contentNode,
        onEnd: function(){
          // set the data, fade it back in
          dContentNode.destroyDescendants();
          contentNode.innerHTML = data;
          dojo.parser.parse(contentNode);
 
          //Dijit specific linking of pages
		  if (page == "buy") { linkListings(page); }
		  else if (page.substr(0, 7) == "listing") { linkListingPage(); }
          if (page == "find") { linkBuyForm(); }
          if (page == "sell") { linkSellForm(); }
          if (page == "classifieds") { linkClassifieds(page); }
		  if (page == "addclassified") { linkAddClassifiedForm(); }
          if (page == "franchiselist") { linkFranchiseListPage(); }
          if (page == "addfranchise") { linkFranchiseForm(); }
          if (page.substr(0, 8) == "category") { var id = page.substr(12); linkClassifiedTable(id); }

          //Link the email box if it is applicable to our page
          if(email) { linkUserEmailForm(); }

          dojo.fadeIn({node: contentNode}).play();
        }
      }).play();
    },

    // if any error occurs, it goes here:
    error: function(error,args){
      console.warn("error!",error);
    }
  });
};

function linkBuyForm() {
  var response = dojo.byId("response");
  var form = dojo.byId("route_buyform"); // save ref to form
  dojo.connect(form, "onsubmit", function(e) {
    //Stop the form from submitting
    e.preventDefault();

    //Post the form
    dojo.xhrPost( {
      url: "company.php?p=buy",
      handleAs: "text",
      method: "POST",
      mimetype: "text/plain",
      formNode: "route_buyform",
      form: form,
      content: {city: document.route_buyform.city.value,
                cotype: document.route_buyform.cotype.value,
                price: document.route_buyform.price.value,
                email: document.route_buyform.email.value,
                phone: document.route_buyform.phone.value,
                homebased: document.route_buyform.homebased.value,
                financing: document.route_buyform.financing.value},
      load: function(data) { parseForm(data, "Content"); },
      error: function(data) { parseError(data); }
    });

    response.innerHTML = "Form being sent ...";
  });
}

function linkSellForm() {
  var response = dojo.byId("response");
  var form = dojo.byId("route_sellform"); // save ref to form
  dojo.connect(form, "onsubmit", function(e) {
    //Stop the form from submitting
    e.preventDefault();
    dojo.stopEvent(e);

    //Post the form
    dojo.xhrPost( {
      url: "company.php?p=psell",
      handleAs: "text",
      method: "POST",
      mimetype: "text/plain",
      formNode: 'route_sellform',
      form: form,
      content: {businesslocation: document.route_sellform.businesslocation.value,
                headline: document.route_sellform.headline.value,
                askprice: document.route_sellform.askprice.value,
                general: document.route_sellform.general.value,
                sales: document.route_sellform.sales.value,
                profit: document.route_sellform.profit.value,
                homebased: document.route_sellform.homebased.value,
                franchise: document.route_sellform.franchise.value,
                employees: document.route_sellform.employees.value,
                training: document.route_sellform.training.value,
                reason: document.route_sellform.reason.value,
                firstname: document.route_sellform.firstname.value,
                lastname: document.route_sellform.lastname.value,
                email: document.route_sellform.email.value,
                dayphone: document.route_sellform.dayphone.value},
      load: function(data) { parseForm(data, "Content"); },
      error: function(data) { formError(data); }
    });

    response.innerHTML = "Form being sent ...";
  });
}

function linkUserEmailForm() {
  //Get the email objects
  var response = dojo.byId("email_response");
  var form = dojo.byId("route_emailform"); // save ref to form

  //Link the classifieds
  dojo.connect(dojo.byId("classified"),"onclick",function() { route_navclick("classifieds", "Content", false); });
  dojo.connect(dojo.byId("classified_img"),"onclick",function() { route_navclick("classifieds", "Content", false); });

  //Link the email objects
  dojo.connect( dojo.byId( 'route_emailform'), 'onsubmit', function(e){
    //Stop the form from submitting
    e.preventDefault();

    //Post the form
    dojo.xhrPost( {
      url: "user.php?p=addemail",
      handleAs: "text",
      method: "POST",
      mimetype: "text/plain",
      formNode: 'route_emailform',
      form: form,
      content: {user_email: document.route_emailform.user_email.value},
      load: function(data) { parseForm(data, "email_response"); },
      error: function(data) { formError(data); }
    });

    response.innerHTML = "Form being sent ...";
    return false;
  });
}

function linkAddClassifiedForm() {
  //Get the email objects
  var response = dojo.byId("classified_response");
  var form = dojo.byId("route_form"); // save ref to form

  //Link the email objects
  dojo.connect( dojo.byId( 'route_form'), 'onsubmit', function(e){
    //Stop the form from submitting
    e.preventDefault();

    //Post the form
    dojo.xhrPost( {
      url: "classified.php?p=paddclassified",
      handleAs: "text",
      method: "POST",
      mimetype: "text/plain",
      formNode: 'route_form',
      form: form,
      content: {category: document.route_form.category.value,
                title: document.route_form.title.value,
                description: document.route_form.description.value,
                start_date: document.route_form.start_date.value,
                end_date: document.route_form.end_date.value,
                firstname: document.route_form.firstname.value,
                lastname: document.route_form.lastname.value,
                email: document.route_form.email.value,
                dayphone: document.route_form.dayphone.value},
      load: function(data) { parseForm(data, "ClassifiedContent"); },
      error: function(data) { formError(data); }
    });

    //response.innerHTML = "Form being sent ...";
    return false;
  });
}

function linkListings() {
  var jsonURL = "listing.php?p=jlistings";

  //Get the categories
  dojo.xhrGet({
    url: "listing.php?p=jlistings",
    handleAs: "json",
    load: function(data) { linkListingTableByID(data); },
    error: function(data) { alert("error"); }
  });
}

function linkListingTableByID(data) {
  //Get the JSON object
  var json = eval(data);
  var id = 0;

  for(var i = 0; i < json['size']; i++) {
    id = json[i + "-id"];
    dojo.connect(dojo.byId("ilisting" + id),"onclick",function(id) { route_navclick("listing&id=" + this.id.substr(8), "Content", false); }); 
  }
}

function linkListingPage() {
  dojo.connect(dojo.byId("listings"),"onclick",function(id) { route_navclick("buy", "Content", false); }); 
}

function linkClassifieds(page) {
  //Connect the add classified form
  dojo.connect(dojo.byId("addclassified"),"onclick",function(){ route_navclick("addclassified", "ClassifiedContent", false); }); //linkAddClassifiedForm(); });

  //Get the categories
  dojo.xhrGet({
    url: "classified.php?p=jcategories",
    handleAs: "json",
    load: function(data) { linkClassifiedCategories(data); },
    error: function(data) { alert("error"); }
  });

  linkClassifiedTable(-1);
}

function linkClassifiedCategories(data) {
  //Get the JSON object
  var json = eval(data);
  var id = 0;

  for(var i = 0; i < json['size']; i++) {
    id = json[i + "-id"];
    dojo.connect(dojo.byId("ccategory" + id),"onclick",function(id) { route_navclick("category&id=" + this.id.substr(9), "ClassifiedContent", false); }); 
  }
}

function linkClassifiedTable(id) {
  var jsonURL = "classified.php?p=jclassifieds";
  if(id > -1) { jsonURL += "&id=" + id; }

  //Get the categories
  dojo.xhrGet({
    url: "classified.php?p=jclassifieds",
    handleAs: "json",
    load: function(data) { linkClassifiedTableByID(data); },
    error: function(data) { alert("error"); }
  });
}

function linkClassifiedTableByID(data) {
  //Get the JSON object
  var json = eval(data);
  var id = 0;

  for(var i = 0; i < json['size']; i++) {
    id = json[i + "-id"];
    dojo.connect(dojo.byId("iclassified" + id),"onclick",function(id) { route_navclick("classified&id=" + this.id.substr(11), "ClassifiedContent", false); }); 
  }
}

function linkFranchiseListPage() {
  dojo.connect(dojo.byId("addfranchise"),"onclick",function(){ route_navclick("addfranchise", "Content", false); })
}

function linkFranchiseForm() {
  //Get the email objects
  var response = dojo.byId("franchise_response");
  var form = dojo.byId("route_form"); // save ref to form
  var fileMask = [["Jpeg File", "*.jpg;*.jpeg"],
                  ["GIF File", "*.gif"],
                  ["PNG File", "*.png"],
                  ["All Images", "*.jpg;*.jpeg;*.gif;*.png"]
                 ];

  dojo.connect(dojo.byId("franchiseFormSubmitBtn"),"onclick",function(){ processFranchiseForm(1); }); 
  dojo.byId("fTypes").innerHTML+=fileMask[fileMask.length-1][1];
}

function processFranchiseForm(x){
  var td = dojo.io.iframe.send({
             url: "company.php?p=paddfranchise",
             form: "route_form",
             method: "post",
             content: {fnx:x},
             timeoutSeconds: 5,
             preventCache: true,
             handleAs: "json",
             handle: function(res, ioArgs){
                       if(res.status == "Success") {
                         dojo.xhrGet({
                           url: "company.php?p=caddfranchise",
                           handleAs: "text",
                           load: function(data) { parseForm(data, "Content"); },
                           error: function(data) { alert("There was a error processing your request"); }
                         });
					     //	linkUserEmailForm();
					   }
					   else if(res.status == "Fail") {
                         dojo.xhrGet({
                           url: "company.php?p=franchiseform",
                           handleAs: "text",
                           load: function(data) { franchiseFormError(data, "Content", res.textdata); },
                           error: function(data) { alert("Error1. There was a error processing your request"); }
                         });
					   }
					   else {
                         alert("Status: " + res.status + ". There was a error processing your request");
					   }
                     },
             error: function (res,ioArgs) {alert('Error2. There was a error processing your request')}
           });
}

function franchiseFormError(data, node, textdata) {
  var dContentNode = dijit.byId(node);
  var contentNode = dojo.byId(node);

  // fade out the node we're modifying
  dojo.fadeOut({
    node: contentNode,
    onEnd: function(){
      dContentNode.destroyDescendants();
      // set the data, fade it back in
      contentNode.innerHTML = data;
      dojo.parser.parse(contentNode);
      dojo.byId("FranchiseError").innerHTML = textdata;
      dojo.byId("FranchiseError").style.display = "";
      dojo.fadeIn({node: contentNode}).play();
    }
  }).play();
}

function parseForm(data, node) {
  var dContentNode = dijit.byId(node);
  var contentNode = dojo.byId(node);

  // fade out the node we're modifying
  dojo.fadeOut({
    node: contentNode,
    onEnd: function(){
      dContentNode.destroyDescendants();
      // set the data, fade it back in
      contentNode.innerHTML = data;
      dojo.parser.parse(contentNode);
      dojo.fadeIn({node: contentNode}).play();
    }
  }).play();
}

function buyFormError(data) {
  alert("Error! Please try again");
}

// -->
