//********** global constants and settings **********
var FILE_HOST               = 'http://www.myecoville.com/';
var LOCATION_OF_FILES       = 'custom-ecoville-templates/us/ga/';
var JS_FILE                 = 'mini-search.js';
var CSS_FILE                = 'mini-search.css';

var SEARCH_HOST             = 'http://www.myecoville.com/';
var LOCATION_OF_SEARCH      = 'us/ga/recycling-resources';
var SEARCH_TARGET           = '_blank';

var FORCESEARCH_DEF_VALUE   = '1';
var MAXDISTANCE_DEF_VALUE   = '5';
var TOPLOCATION_DEF_VALUE   = 'ou=Georgia,ou=United States,ou=North America,ou=Earth,dc=ecoville,dc=com';

var TITLE_VALUE             = 'How Can I Recycle in My Community?';
var INIT_INPUT_VALUE        = 'Enter Your ZIP Code';
var BUTTON_VALUE            = 'GO!';
//***************************************************

//load the helper files
function ecoInitilize() {
	var cssFile		= document.createElement('link');
	cssFile.type	= 'text/css';
	cssFile.rel		= 'stylesheet';
	cssFile.href	= FILE_HOST + LOCATION_OF_FILES + CSS_FILE;
	document.getElementsByTagName('head')[0].appendChild(cssFile);
}

//Main javascript the creates the search widget
function ecoCreateWidget() {
	//get the div for the search widget and setup the style
	var miniSearchDiv	= document.getElementById('mini-ecoville-search');
	
	//form
	var widgetForm		= document.createElement('form');
	widgetForm.action	= SEARCH_HOST + LOCATION_OF_SEARCH;
	widgetForm.target	= SEARCH_TARGET;
	widgetForm.id       = 'eco-search-form';
	widgetForm.onsubmit = function() { return ecoFormValidation(); }
	miniSearchDiv.appendChild(widgetForm);
	
	//title bar div
    var titleDiv    = document.createElement('div');
    titleDiv.id     = 'eco-title-div';
    widgetForm.appendChild(titleDiv);
    
    //input area div
    var contentDiv  = document.createElement('div');
    contentDiv.id   = 'eco-content-div';
    widgetForm.appendChild(contentDiv);
    var inputDiv    = document.createElement('div');
    inputDiv.id     = 'eco-input-div';
    contentDiv.appendChild(inputDiv);

	//hidden but required values for the search
	var forceSearch		= document.createElement('input');
	forceSearch.type	= 'hidden';
	forceSearch.name	= 'forceSearch';
	forceSearch.value	= FORCESEARCH_DEF_VALUE;
	contentDiv.appendChild(forceSearch);
	var maxDistance		= document.createElement('input');
	maxDistance.type	= 'hidden';
	maxDistance.name	= 'maxDistance';
	maxDistance.value	= MAXDISTANCE_DEF_VALUE;
	contentDiv.appendChild(maxDistance);
	var topLocation		= document.createElement('input');
	topLocation.type	= 'hidden';
	topLocation.name	= 'top';
	topLocation.value	= TOPLOCATION_DEF_VALUE;
	contentDiv.appendChild(topLocation);
	var mailCode		= document.createElement('input');
	mailCode.type   	= 'hidden';
	mailCode.name   	= 'locQuery';
	mailCode.id         = 'locQuery';
	contentDiv.appendChild(mailCode);

	//title
    var titleText  = document.createTextNode(TITLE_VALUE);
    titleDiv.appendChild(titleText);

    //table for inputs
    var table	= document.createElement('table');
	table.id	= 'eco-input-table';
	var tbody   = document.createElement('tbody');
	tbody.id    = 'eco-input-table-body';
	var tr1		= document.createElement('tr');
	var td1		= document.createElement('td');
	var	td2		= document.createElement('td');
    inputDiv.appendChild(table);
    table.appendChild(tbody);
	tbody.appendChild(tr1);
	tr1.appendChild(td1);
	tr1.appendChild(td2);

	//input and button
    var postalInput		= document.createElement('input');
    postalInput.id		= 'eco-postal-input';
    postalInput.type	= 'text';
    postalInput.value   = INIT_INPUT_VALUE;
    postalInput.onfocus = function() {
        var input = document.getElementById('eco-postal-input');
        
        if(input.value == INIT_INPUT_VALUE) {
            input.value = '';
        }
    }
	var button 		    = document.createElement('input');
	button.id 		    = 'eco-submit-search';
	button.type		    = 'submit';
	button.value	    = BUTTON_VALUE;
    td1.appendChild(postalInput);
    td2.appendChild(button);
}

function ecoFormValidation() {
    var isValid = true;
    
    var postalInput         = document.getElementById('eco-postal-input');
    var postalInputValue    = postalInput.value;
    
    if(!postalInputValue || (postalInputValue == INIT_INPUT_VALUE)) {
        postalInput.value = INIT_INPUT_VALUE;
        isValid = false;
    }
    
    if(postalInputValue) {
        postalHidden        = document.getElementById('locQuery');
        postalHidden.value  = postalInputValue;
    }
    
    return isValid;
}

ecoInitilize();
ecoCreateWidget();
