// Specific functions for NoteBar
var na_xmlhttpreq;
var connectMessageNewCount=0;
var connectMessagesAdded='';
var discussMessageNewCount=0;
var discussMessagesAdded='';
var notebar_lid=0;
var nbfeatureType=0;
var rgstrBoxPanel;
var sCurrNotePrefix="nw";

var featActivValues = [ { "featureType" : "-2130706432", "values" : ["work","spend money", "play", "hang out", "study"] },
			{ "featureType" : "10", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
			{ "featureType" : "20", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
			{ "featureType" : "30", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
			{ "featureType" : "40", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
			{ "featureType" : "41", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
			{ "featureType" : "70", "values" : ["live", "work", "spend money", "play", "hang out", "study"] },
                        { "featureType" : "96", "values" : ["travel from/to", "live", "work", "play", "hang out", "study"] },
                        { "featureType" : "97", "values" : ["live", "work", "play", "hang out", "study" ]},
                        { "featureType" : "98", "values" : ["work", "live", "play","hang out","study"] },
                        { "featureType" : "99", "values" : ["live", "work","play","hang out","study" ] },
                        { "featureType" : "100", "values" : ["study", "teach", "work", "play", "hang out" ]},
                        { "featureType" : "101", "values" : ["ride", "live", "work", "play", "hang out", "study" ]},
                        { "featureType" : "102", "values" : ["play", "work", "live", "work", "hang out", "study"]},
                        { "featureType" : "103", "values" : ["work", "live","play", "hang out", "study"]},
                        { "featureType" : "104", "values" : ["get treatment", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "105", "values" : ["live", "work", "play",  "hang out", "study" ]},
                        { "featureType" : "106", "values" : ["ski", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "107", "values" : ["play", "work", "live", "hang out", "study"]},
                        { "featureType" : "109", "values" : ["hang out", "live",  "work", "play", "study" ] },
                        { "featureType" : "110", "values" : ["study", "teach", "work", "live", "play", "hang out"] },
                        { "featureType" : "113", "values" : ["travel from/to", "live", "work", "play", "hang out", "study" ] },
                        { "featureType" : "114", "values" : ["live", "work","play","hang out","study"] },
                        { "featureType" : "115", "values" : ["live","work","play","hang out"] },
                        { "featureType" : "116", "values" : ["shop","work","play","hang out"] },
                        { "featureType" : "117", "values" : ["work","train","study"] },
                        { "featureType" : "118", "values" : ["play","work","hang out"] },
                        { "featureType" : "119", "values" : ["hang out","study","play"] },
                        { "featureType" : "120", "values" : ["visit","work","study","play"] },
                        { "featureType" : "121", "values" : ["live","hang out","play","work"] },
                        { "featureType" : "160", "values" : ["live", "work", "hang out"] },
                        { "featureType" : "161", "values" : ["own a plot", "have a loved one", "work", "hang out"] },
                        { "featureType" : "162", "values" : ["golf","work","hang out","take lessons"] },
                        { "featureType" : "163", "values" : ["live", "work", "hang out", "play", "study"]},
                        { "featureType" : "164", "values" : ["play", "hang out", "work", "live", "study"]},
                        { "featureType" : "165", "values" : ["hang out","spend money", "work", "live", "play", "study"  ]},
                        { "featureType" : "166", "values" : ["hang out",  "work", "live", "study", "play" ]},
                        { "featureType" : "168", "values" : ["spend money", "work", "hang out", "live", "play", "study" ]},
                        { "featureType" : "169", "values" : ["spend money", "work", "hang out", "live", "play", "study" ]},  
                        { "featureType" : "170", "values" : ["live", "hang out", "work", "play", "study"] },
                        { "featureType" : "171", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "172", "values" : ["hang out", "play", "work", "live", "study"]},
                        { "featureType" : "173", "values" : ["spend money", "work", "hang out", "live", "play", "study" ]},
                        { "featureType" : "174", "values" : ["live", "work", "hang out", "play", "study"]},
                        { "featureType" : "175", "values" : ["eat","work","play","hang out","supply"]},
                        { "featureType" : "176", "values" : ["worship", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "177", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "178", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "179", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "180", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "181", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "182", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "183", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "184", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "185", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "186", "values" : ["study", "work", "live", "play", "hang out"]},
                        { "featureType" : "187", "values" : ["study", "work", "live", "play", "hang out"]},
                        { "featureType" : "188", "values" : ["live", "work", "play", "hang out", "study"]},
                        { "featureType" : "189", "values" : ["work out","work","train","play"]},
                        { "featureType" : "190", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "191", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "192", "values" : ["travel from/to", "live", "work", "play", "hang out", "study"]},
                        { "featureType" : "193", "values" : ["travel from/to", "live", "work", "play", "hang out", "study"]},
                        { "featureType" : "194", "values" : ["travel from/to", "live", "work", "play", "hang out", "study"]},
                        { "featureType" : "195", "values" : ["study", "teach", "work", "play", "hang out"]},
                        { "featureType" : "196", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "197", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "198", "values" : ["serve time", "have a loved one", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "199", "values" : ["get treatment","work", "live", "play", "hang out", "study"]},
                        { "featureType" : "200", "values" : ["bank", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "201", "values" : ["live", "work", "play", "hang out", "study"]},
                        { "featureType" : "202", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "203", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "204", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "205", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "206", "values" : ["hang out", "travel from/to", "live", "work", "play", "study"]},
                        { "featureType" : "207", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "208", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "209", "values" : ["camp", "hang out", "live", "work", "play", "study"]},
                        { "featureType" : "210", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "211", "values" : ["stay", "hang out", "live", "work", "play", "study"]},
                        { "featureType" : "212", "values" : ["bowl", "hang out", "live", "work", "play", "study"]},
                        { "featureType" : "213", "values" : ["skate", "hang out", "live", "work", "play", "study"]},
                        { "featureType" : "214", "values" : ["play", "work", "live", "hang out", "study"]},
                        { "featureType" : "215", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "216", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "217", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "218", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "219", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "220", "values" : ["work", "live", "play", "hang out","serve time", "study"]},
                        { "featureType" : "221", "values" : ["work", "live", "play", "hang out","serve time", "study"]},
                        { "featureType" : "222", "values" : ["work", "live", "play", "hang out", "study"]},
                        { "featureType" : "223", "values" : ["dump", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "224", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "225", "values" : ["spend money", "work", "live", "play", "hang out", "study"]},
                        { "featureType" : "226", "values" : ["attend","work","play","hang out","study"]},
                        { "featureType" : "227", "values" : ["play","work","hang out","watch"]},
                        { "featureType" : "228", "values" : ["go","work","play","hang out"]},
                        { "featureType" : "229", "values" : ["work","visit","study"]},
                        { "featureType" : "230", "values" : ["work","spend money"]},
                        { "featureType" : "231", "values" : ["live","play","hang out"]},
                        { "featureType" : "232", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "233", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "234", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "235", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "236", "values" : ["go","teach","work","hang out"]},
                        { "featureType" : "237", "values" : ["hang out", "live", "work", "play", "study"]},
                        { "featureType" : "238", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "239", "values" : ["spend money","work","hang out"]},
                        { "featureType" : "240", "values" : ["spend money","work","hang out"]}
		];

                                       
function initNoteBar(locationID, fType, sPrefix)
{
    notebar_lid = locationID;
    nbfeatureType = fType;

    initFromToYear(sPrefix + "_from_yr", sPrefix + "_to_yr");
    initFromToMonth(sPrefix + "_from_month", sPrefix + "_to_month");

    if (notebar_lid == 0) {
        InitQueryCode('locationInput', 'http://www.zoomatlas.com/mediawiki/skins/za_skin/DoTypeAhead.php?q=');
    } else {
        setFeatureValues(nbfeatureType);
    }
}

function setFeatureValues(featureType)
{
    return setFeatureValuesByWidget(featureType,"activityInput");
}

function setFeatureValuesByWidget(featureType, widgetName)
{
    var activValues;
    var activitySelect;

    var activityElements = document.getElementsByName(widgetName);
    for(c = 0; c < activityElements.length; c++)
    {
       activitySelect = activityElements.item(c);
       var featFound = false;
       for(var i = 0; (i < featActivValues.length && !featFound ); i++)
       {
          if (featureType == featActivValues[i].featureType)
          {
             activValues = featActivValues[i].values;
             activitySelect.options.length = activValues.length;
             for (var  j = 0; j < activValues.length; j++)
             {
                activitySelect.options[j].text = activValues[j];
                activitySelect.options[j].value = activValues[j];
             }
             featFound = true;
          }
       }
       if (!featFound)
       {
             activitySelect.options.length = 1;
             activitySelect.options[0].text = "hang out";
             activitySelect.options[0].value = "hang out";    
       }
    }
}

function nbSetPeriod(sPrefix)
{
   var current = document.getElementById(sPrefix + '_nbcurrent').checked;
   if (current)
   {
	document.getElementById('pre').innerHTML = "Since";
        document.getElementById(sPrefix + '_to_yr').disabled = "disabled";
        document.getElementById(sPrefix + '_to_month').disabled = "disabled";
   }
   else
   {
       document.getElementById(sPrefix + '_to_yr').disabled = "";
       document.getElementById(sPrefix + '_to_month').disabled = "";
       document.getElementById('pre').innerHTML = "From";
   }
}

// User has submitted a new note for posting
function postNote(sPrefix)
{
    if (!loggedIn())
    {
	// User not logged in...cannot post
        askToRegister();
    }
    else
    {
	// User IS logged in...but is he FB user?
	if (isFacebookUser())
	{
	    // Yes. FB users must opt-in for email
	    // Ask FB to call us back with the answer
	    FB.Facebook.apiClient.users_hasAppPermission('email', 
		function(result)
		{
		// The result is 0 for users who have not yet opted in, and 1 for those who have 
		if (result==0) 
		    { 
			// We will not post the note yet. Ask for opt-in
			fbconnect_optin_showdiv();
		    } 
		else
		    {
			// User HAS opted in...pop up the note widget immediately
			postNoteNow(sPrefix);
		    }
		} );
	    // Do nothing now. Wait for above callback to fire.
	    return;
	}
	// Not a facebook user. Just post the note
	postNoteNow(sPrefix);
    }
}

// This is the actual work routine for postNote(). We have already determined that the user
// is either a ZA user or an opt-in FB user.
function postNoteNow(sPrefix)
{
    var nbdetails="";
    var unencodedDetails="";
    var activity = "";
    var fromYear="";
    var toYear="";
    var fromMonth="";
    var toMonth="";

    sCurrNotePrefix = sPrefix;

    var detailsElement = document.getElementById(sPrefix + "_details");
    if (detailsElement != null)
    {
        unencodedDetails = detailsElement.value;
        nbdetails  = encodeURIComponent(unencodedDetails);
    }

    var activityElement = document.getElementById(sPrefix + "_activityInput");
    if (activityElement != null)
    {
        activity = activityElement.value; 
    }

    var fromYearElement = document.getElementById(sPrefix + "_from_yr");
    if (fromYearElement != null)
    {
        fromYear = fromYearElement.value;
    }

    var current = document.getElementById(sPrefix + "_nbcurrent");
    if (current.checked == false)
    {
        var toYearElement = document.getElementById(sPrefix + "_to_yr");
        if (toYearElement != null)
        {
            toYear = toYearElement.value;
        }
    }
    else
    {
        toYear = 9999;
    }

    var fromMonthElement = document.getElementById(sPrefix + "_from_month");
    if (fromMonthElement != null)
    {
        fromMonth = fromMonthElement.value;
    }

    var toMonthElement = document.getElementById(sPrefix + "_to_month");
    if (toMonthElement != null)
    {
        toMonth = toMonthElement.value;
    }

    if (notebar_lid == 0)
    {
        // No LID, don't bother making the call
        alert("No location was specified.  Please enter the location that you wish to add.");
        return false;       
    }

    na_xmlhttpreq = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
    	    na_xmlhttpreq = new XMLHttpRequest();
    	    if (na_xmlhttpreq.overrideMimeType) {
                na_xmlhttpreq.overrideMimeType('text/html');
    	    }
    } else if (window.ActiveXObject) { // IE
        try {
            na_xmlhttpreq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                na_xmlhttpreq = new ActiveXObject("Microsoft.XMLHTTP");
    	    } catch (e) {}
        }
    }

    if (!na_xmlhttpreq) {
        alert('Cannot create XMLHTTP instance');
        return false;
    }

    // Above code doesn't work for FF. Use InfoBox xmlhttp object
    //na_xmlhttpreq = xmlhttp;

    na_xmlhttpreq.onreadystatechange = processPostedNote;

    var iZoom = getZoomLevel();

    var url="http://www.zoomatlas.com/NoteMan/NoteBar?op=addloc&lid=" + notebar_lid + 
        "&fy=" + fromYear + "&ty=" + toYear + "&fm=" + fromMonth + "&tm=" + toMonth + "&at=" + activity + "&dt=" + escape(nbdetails) +
        "&long=" + ibFloatPanelLon + "&lat=" + ibFloatPanelLat + "&zoom=" + iZoom + "&ft=" + nbfeatureType;

    na_xmlhttpreq.open('GET', url, true);
    na_xmlhttpreq.send(null);

    return false;
}

function processPostedNote()
{
   if (na_xmlhttpreq.readyState==4) {
	if (na_xmlhttpreq.status==200) {
            var xmlDoc = na_xmlhttpreq.responseXML;
            //var resultArray = xmlDoc.getElementsByTagName('Result');
            //if (resultArray[0].firstChild.nodeValue == "OK") {
            if (true) {
                na_xmlhttpreq.close;
                clearForm();
                updateClient();
		doRedrawMap(xmlDoc);
            } else {
                alert("Error: Could not post your location");
            } 
        } else {
            alert("processPostedNote: problem with request");
        }
    }
}

function doRedrawMap(response)
{
    RedrawMap();
    return;
    
//    // In javascript, parse out the bounding box like this:
//    var bbNorthResp = response.getElementsByTagName('bbnorth')[0];
//    var bbSouthResp = response.getElementsByTagName('bbsouth')[0];
//    var bbEastResp = response.getElementsByTagName('bbeast')[0];
//    var bbWestResp = response.getElementsByTagName('bbwest')[0];

//    // ...and then call this global function to force a repaint of the map:
//    if ((bbNorthResp != null) && (bbSouthResp != null) && (bbEastResp != null) && (bbWestResp != null))
//    {
//        // Ask the map to redraw.
//        RedrawMap(bbWestResp.firstChild.data, bbSouthResp.firstChild.data,
//                  bbEastResp.firstChild.data, bbNorthResp.firstChild.data);
//    }
}

function clearForm()
{
    var locationInput = document.getElementById("locationInput");
    if (locationInput != null && locationInput.disabled != "disabled")
    {
        locationInput.value = "";
        notebar_lid = 0;
        nbfeatureType = 0;
        setFeatureValues(0);
    }

    if (msgBoxPanel == null) {
        clearNoteFormElement("details");
        clearNoteFormElement("from_yr");
        clearNoteFormElement("to_yr");
        clearNoteFormElement("from_month");
        clearNoteFormElement("to_month");
        resetNoteSelectElement("activityInput");
    } else {
        var successDisplay = "Your note was posted successfully.<br/><input type='button' value='OK' onclick='cancelPvtMsg();'>";
        msgBoxPanel.setBody(successDisplay);
    }
}

function clearNoteFormElement(sId)
{
    try {
        document.getElementById(sCurrNotePrefix + "_" + sId).value = "";
    } catch (e) {}
}

function resetNoteSelectElement(sId)
{
    try {
        var selectElement = document.getElementById(sCurrNotePrefix + "_" + sId);
        selectElement.selectedIndex = 0;
    } catch (e) {}
}

function getBrowserWindowHeight() {
  myHeight = 0;
  if (typeof(window.innerWidth) == 'number') {
    //Non-IE
    myHeight = window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}
