// JavaScript Document

var xmlHttp;
var responseDiv;

// used to identify the type of form, feedback or owner or send email etc.
// based on this, the response div can be tailored.
var formType;

function createXMLHttpRequest() {

  if(window.ActiveXObject) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } else if(window.XMLHttpRequest) {
    xmlHttp = new XMLHttpRequest();
  }  

  // handleServerResponse is a method in your javascript code.
  xmlHttp.onreadystatechange = handleServerResponse;

}


function handleServerResponse() {
  if(xmlHttp.readyState == 4) { // the state when the request has completed
    if (xmlHttp.status == 200) { // the HTTP status code returned from the server
      if(formType == 'feedback') {
        responseDiv.innerHTML = "Thank You! We will contact you if a response is required.";
      } else if (formType == 'subscribe') {
        responseDiv.innerHTML = "Thank You for signing up. We'll be in touch!";
      } else if (formType == 'advertise') {
        responseDiv.innerHTML = "Thank You. We will contact you shortly!";
      } else {
        responseDiv.innerHTML = xmlHttp.responseText;
      }

      // further, if the form is of type 'rating', then because this was a successful
      // response code from the server, we can assume that the comment was saved
      // so display this comment so that the user doesn't have to reload the page
      if(formType == 'rating') { 
       
        var newReviewStars = document.getElementById("newreviewstars");
        newReviewStars.style.display = "block";
        setStarRating("_newUserRating");

        var newCommentBox = document.getElementById("newcommentbox");
        newCommentBox.style.display = "block";

        var newComment = document.getElementById("newcomment");
        newComment.innerHTML = userComment;

        var newUser = document.getElementById("newcommentuser");
        newUser.innerHTML = 'Posted by - ' + userName;

        var newCommentLine = document.getElementById("newcommentline"); 
        newCommentLine.style.display = "block";
        
        Effect.Pulsate("newcommentbox"); 
        Effect.Fade("noreviewsbox");
      }
    }
  }
}

function subscribe() {

  formType = "subscribe";

  createXMLHttpRequest();

  // the rcpt email(s) - send to administrator
  var rcpt = "admin@mycitybuddy.com";

  // get the subscriber's email
  var subscriberEmail = document.subscribeForm.subscriberEmail.value;

  if(subscriberEmail == "") {
    document.subscribeForm.subscriberEmail.value = 'Invalid email address. Please try again';
    return; 
  }

  if(!checkEmail(subscriberEmail)) {
    document.subscribeForm.subscriberEmail.value = 'Invalid email address. Please try again';
    return; 
  }
 
  var message = 
    "Subscription sign up by " +
    " (" + subscriberEmail + ")\r\n\r\n";

  responseDiv = document.getElementById("subscribeResponse");
  responseDiv.innerHTML = "Sending....";  

  // send this email
  xmlHttp.open(
    "GET", 
    "/ajaxhandler.do?dispatch=sendEmail&sender=" +
    subscriberEmail + "&rcpt=" + rcpt + "&senderName=" + escape('ss') + "&message=" +
    escape(message) + "&subject=" + escape("MCB Subscription Request!"));
  
  xmlHttp.send(null);
}

function checkEmail(emailAddr) { 
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr)) { 
    return (true);
  } else 
    return false;
}

function isIE() {
  var browserName=navigator.appName;
  if (browserName == 'Netscape')
          return false;
  return true;
}

function initSearchWidget(){
  // initialize the autocomplete widget
  var countries = new Array("United States", "Canada", "Mexico", "Russia", "India", "China", "Mongolia", "Romania", "Morocco");
  //var myDataSource = new YAHOO.util.LocalDataSource(countries);

  var myDataSource = new YAHOO.util.XHRDataSource("/ajaxhandler.do");
  myDataSource.responseType = YAHOO.util.XHRDataSource.TYPE_JSON;
  myDataSource.responseSchema = { 
    resultsList : "results", 
    fields : ["areaName"] 
  };


  var myAutoAreaComp = new YAHOO.widget.AutoComplete("areaNameInput", "areaSuggestContainer", myDataSource);

  // Require user to type at least 2 characters before triggering a query
  myAutoAreaComp.minQueryLength = 2;

  // needed to workaround an IE6 bug
  myAutoAreaComp.useIFrame = true;

  myAutoAreaComp.generateRequest = function(sQuery) { 
    return "?dispatch=suggestAreasLike&query=" + sQuery; 
  };

  // Called before display
  myAutoAreaComp.formatResult = function(oResultItem, sQuery, sResultMatch) {
    return oResultItem[0];
  };
}

function advertise() {

  formType = "advertise";

  createXMLHttpRequest();

  // the rcpt email(s) - send to administrator
  var rcpt = "admin@mycitybuddy.com";

  // get the sender's name
  var bizName = document.advtForm.bizName.value;

  if(bizName == "") {
    document.advtForm.bizName.value = 'Please enter business name';
    return; 
  }

  // get the subscriber's email
  var bizEmail = document.advtForm.bizEmail.value;

  if(bizEmail == "") {
    document.advtForm.bizEmail.value = 'Invalid email address. Please try again';
    return; 
  }

  if(!checkEmail(bizEmail)) {
    document.advtForm.bizEmail.value = 'Invalid email address. Please try again';
    return; 
  }
 
  // get the rest of the stuff
  var bizDetails = document.advtForm.bizDetails.value;
  var bizContactNumber = document.advtForm.contactNumber.value;
  var bizAddress = document.advtForm.address.value;

  var message = 
    "Advertisement request by " +
    " ( " + bizEmail + 
    " BUSINESS NAME: " + bizName + 
    " CONTACT NUMBER: " + bizContactNumber + 
    " BUSINESS DETAILS: " + bizDetails +
    " ADDRESS: " + bizAddress + 
    " )\r\n\r\n";

  responseDiv = document.getElementById("advtResponse");
  responseDiv.innerHTML = "Sending....";  

  // send this email
  xmlHttp.open(
    "GET", 
    "/ajaxhandler.do?dispatch=sendEmail&sender=" +
    bizEmail + "&rcpt=" + rcpt + "&senderName=" + escape(bizName) + "&message=" +
    escape(message) + "&subject=" + escape("MCB Advertisement Request!"));
  
  xmlHttp.send(null);
}





