// <!--
//////////////////////////////////////////////////////////////////////////////////////////////////
// file: locatezips.js
//
// This file has functions to display overlay for locating zips.
// The page that wants to locate the zip has to call: locateZips(textBoxId) function.
//      textBoxId is the "id" of the text input box where the zip code has to be populated.
//
// Uses YUI library
////////////////////////////////////////////////////////////////////////////////////////////////////

// call init() on page load
// YAHOO.util.Event.onDOMReady(init, base);

// this function is called by the main page to locate zip
function locateZips(textBoxId) {
	divTextBox = document.getElementById(textBoxId);
	dispOverlay();
}

// this function is called by the main page to locate zip
function locateZipsOverlay(textBoxId,actionVal) {
	actionValue = actionVal;
	divTextBox = document.getElementById(textBoxId);
	dispOverlay();
}

function cityTextBoxKeyEvent() {
	var charfield=document.getElementById("loczip-city")
	charfield.onkeydown=function(e){
	var e=window.event || e
	if (e.keyCode == 13) callAjaxGetZips();
	}
}

var actionValue=""; 
// baseURL - base url (note: when calling $base to pass value here, place it in quotes like '$base')
var baseURL = "/";

// text box element where the selected zip code has to be populated
var divTextBox;

// dialog footer to show status messages
var diagFooter = document.getElementById("zips-footer");

// ajax success handler - locate 
var handleSuccess = function(o){ 
	dwr.util.setValue('loczip-ziplist', o, { escapeHtml:false });
	if (o == "") {
		myOverlay.setFooter("There are no matching zip codes.");
	} else {
		myOverlay.setFooter("");
	}
} 

// ajax failure handler - locate
var handleFailure = function(o){ 
	myOverlay.setFooter("Status code message: " + o.statusText);
} 

// ajax success handler - states
var succStates = function(o){ 
	var divStates = $('loczip-stateName');
    
    if (divStates.outerHTML == undefined){
    	divStates.innerHTML += o;
    } else{
        var temp = "<select name='stateCode' id='loczip-stateName'><option value=''>--- Select State ---</option>";
    	temp += o;
    	temp += "</select>";
    	divStates.outerHTML = temp;
    }
    
} 

// ajax failure handler - states
var failStates = function(o){ 
    myOverlay.setFooter("Status code message: " + o.statusText);
} 

// ajax callback object - locate
var callback = 
{ 
  success: handleSuccess, 
  failure: handleFailure 
}; 

// ajax callback object - states
var cbStates = 
{ 
  success: succStates, 
  failure: failStates 
}; 

// init() method: creates dialog object with "locateZipsOverlay" div's contents
// base - base url (note: when calling $base to pass value here, place it in quotes like '$base')
function init(base) {

	// Instantiate the Dialog
	myOverlay = new YAHOO.widget.Dialog("locateZipsOverlay", 
							{ 
							  fixedcenter : true,
							  modal : true,
							  visible : false, 
							  // effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25},
							  constraintoviewport : true,
							  draggable:false, 
							  close:false
							});
	// render the dialog
	myOverlay.render();

	// get arguments passed to the handler
	if(arguments.length > 2) { 
	   base = arguments[2]; 
	}

	// set base url used by ajax functions
	baseURL = base;
}

// this is called after user selects the zip code from list
function selectZipAndClose(zip) {
	divTextBox.value = zip;
    if (actionValue != "") {
	  var form = document.forms[0];
	  form.action = actionValue;
	  form.submit();
    }
	myOverlay.cancel();
}

// call ajax for getting zip codes
function callAjaxGetZips() {	

	var jsCity = document.getElementById("loczip-city").value;
    var jsStateCode = document.getElementById("loczip-stateName").value;

	// clear zip list	
	document.getElementById("loczip-ziplist").innerHTML = "";
	
	// set status
	myOverlay.setFooter("Fetching zip codes. Please wait..");

	// url on server	
    var sUrl = baseURL + "/exclude/locateZips.ajax?stateCode=" + jsStateCode + "&city=" + jsCity; 

	// ajax call to url using DWR
    DWRUtil.getData(sUrl, {callback:handleSuccess, errorHandler:handleFailure});
}

// ajax call for getting list of states
function callAjaxGetStates() {
	// clear city text box and zip list
	document.getElementById("loczip-city").value = "";
    document.getElementById("loczip-ziplist").innerHTML = "";
	
	// <select> tag
	var stateNames = document.getElementById("loczip-stateName");
	
	// skip contacting server if states are already in <select> tag
	if(stateNames.options.length > 1) {
		stateNames.options[0].selected = true;
		return;
	}

    // url on server
    var sUrl = baseURL + "/exclude/showStates.ajax"; 
    
	// ajax call to url using DWR
    DWRUtil.getData(sUrl, {callback:succStates, errorHandler:failStates});
}

// this function is called to display overlay
function dispOverlay() {
	callAjaxGetStates();
	myOverlay.setFooter("");
	myOverlay.show();	
}

//-->
