/**
 * Zozzer Common Javascript
 *
 * Copyright (c) 2007-2008, Zozzer Inc <info@zozzer.com>.
 *
 * This software is the confidential and proprietary information of
 * Zozzer. ("Confidential Information").
 *
 * @package    Web
 * @author     Bill Clark <bclark@zozzer.com>
 * @copyright  2007-2008 Zozzer Inc
 * @link       http://www.zozzer.com
*/

function togglediv(d1,d2){
	var div1 = document.getElementById(d1).style.display;
	var div2 = document.getElementById(d2).style.display;
	
	if(div1==''){
		document.getElementById(d1).style.display ='none';
		document.getElementById(d2).style.display ='';
	} else {
		document.getElementById(d1).style.display ='';
		document.getElementById(d2).style.display ='none';
	}
}

/**
 * function to display a div based on a condition
 */
function displayDiv(d1, condition) {
	if (condition) {
		document.getElementById(d1).style.display='';
	} else {
		document.getElementById(d1).style.display='none';
	}
}

/**
 * document.getElementById convenience wrapper
 */
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (document.getElementById)
    		element = document.getElementById(element);
    	else if (document.all)
    		element = document.all[element];
        else if (document.layers)
    		element = document.layers[element];

        if (arguments.length == 1)
        	return element;
        elements.push(element);
    }
    return elements;
}

/**
 * document.createElement convenience wrapper
 */
function $E(data) {
	var el;
	if ('string'==typeof data) {
        el=document.createTextNode(data);
	} else {
	    //create the element
	    el=document.createElement(data.tag);
	    delete(data.tag);
	
	    //append the children
	    if ('undefined'!=typeof data.children) {
	        if ('string'==typeof data.children ||
	            'undefined'==typeof data.children.length) {
                //strings and single elements
                el.appendChild($E(data.children));
	        } else {
                //arrays of elements
                for (var i=0, child=null; 'undefined'!=typeof (child=data.children[i]); i++) {
                        el.appendChild($E(child));
                }
	        }
	        delete(data.children);
	    }
	
	    //any other data is attributes
	    if ('undefined'!=typeof data.events) {
        	for (attr in data.events){
        		el.setAttribute(attr, data.events[attr]);
        	}
	        delete(data.events);
	    }	
	    
	    //any other data is attributes
	    for (attr in data) {
	    	el[attr]=data[attr];
	    }

	}
	
	return el;
}

var handleAjaxFailure = function (ajax) {

}

var handleCountrySelectorChange = function (ajax) {
	if (ajax.responseText !== undefined) {
		var regions = eval('(' + ajax.responseText + ')');

		// get region selector and remove options.
		var selector = $('state');
		while (selector.options.length > 0)
			selector.remove(0);

		var option;
		// create new Options.
		for (var i=0; i < regions.length; i++) {
			option = new Option(regions[i][1], regions[i][0]);
			selector.options[selector.options.length] = option;
		}

		// get city selector and remove options.
		var selector = $('city');
		while (selector.options.length > 0)
			selector.remove(0);

		selector.disable = true;
	}
}

var handleRegionSelectorChange = function (ajax) {
	if (ajax.responseText !== undefined) {
		cities = eval('(' + ajax.responseText + ')');

		// get region selector and remove options.
		var selector = $('city');
		while (selector.options.length > 0)
			selector.remove(0);

		var option;
		// create new Options.
		for (var i=0; i < cities.length; i++) {
			option = new Option(cities[i][1], cities[i][0]);
			selector.options[selector.options.length] = option;
		}

		selector.disable = false;
	}
}

function processCountrySelectorChange(countryCode) {
	
	var url = '/locations/' + countryCode;
	
	var callback = {
		success:handleCountrySelectorChange,
		failure: handleAjaxFailure
	};
	
	var urlParams = null;
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, urlParams);
}

function processRegionSelectorChange(regionCode) {
	
	var countrySelector = $('country');
	var countryCode = countrySelector.options[countrySelector.options.selectedIndex].value;
	var url = '/locations/' + countryCode + '/' + regionCode;
	
	var callback = {
		success:handleRegionSelectorChange,
		failure: handleAjaxFailure
	};
	
	var urlParams = null;
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, urlParams);
}


var openSelectors = 0;
var selectorId='';

function processCategorySelectorChange(category, elementId) {
	
	selectorId = elementId; 

	if(category == ''){
		cleanCategorySelect();
		updateSelectedCategoryValue();
	}
	else{
		var url = '/category/ajax/' + category;
		var callback = {
			success:handleCategorySelectChange,
			failure: handleAjaxFailure
		};
		var urlParams = null;
		YAHOO.util.Connect.asyncRequest('GET', url, callback, urlParams);
	}
}

var handleCategorySelectChange = function (ajax) {
	if (ajax.responseText !== undefined) {
		
		categories = eval('(' + ajax.responseText + ')');

		if(selectorId<openSelectors) {
			cleanCategorySelect();
		}

		// create new select when there are children
		if(categories.decendants.length >0) {
			selectorName = 'category.' + selectorId + 1; 

			var categoriesSpan = $(selectorName);
			
			span = new Object();
			span.tag = "span";
			span.id = 'category.' + (selectorId + 1);
			categoriesSpan = $E(span);
			categoriesDiv = $('category');
			categoriesDiv.appendChild(categoriesSpan);

			// Now create the selector.
			selector = new Object();
			selector.tag = "select";
			selector.id= 'catselect.' + (selectorId + 1);
			selector.events = new Object;
			selector.events.onchange = 'processCategorySelectorChange(this.options[this.selectedIndex].value, ' + (selectorId + 1) + ');';
			selectorElement = $E(selector);
			categoriesSpan.appendChild(selectorElement);
			
			var option;
			// create new Options.
			option = new Option();
			selectorElement.options[0] = option;
			for (var i=0; i < categories.decendants.length; i++) {
				option = new Option(categories.decendants[i].name, categories.decendants[i].id);
				selectorElement.options[selectorElement.options.length] = option;
			}
			openSelectors+=1;
		}
		updateSelectedCategoryValue();

	}
}

function updateSelectedCategoryValue(){

	var lastSelector = 'catselect.' + (openSelectors);
	var secondlastSelector = 'catselect.' + (openSelectors-1);

	var htmltext='';
	var selectedOptionValue='';

	if(openSelectors > 0){
		if($(lastSelector).options[$(lastSelector).selectedIndex].value==''){
			htmlText="Your selected category is: <b>" + $(secondlastSelector).options[$(secondlastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = $(secondlastSelector).options[$(secondlastSelector).selectedIndex].value;
		}
		else {
			htmlText="Your selected category is: <b>" + $(lastSelector).options[$(lastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = $(lastSelector).options[$(lastSelector).selectedIndex].value;
		}		
	}
	else {
		if($(lastSelector).options[$(lastSelector).selectedIndex].value==''){
			htmlText="Please select a category"; 
			selectedOptionValue = '';
		}
		else{
			htmlText="Your selected category is: <b>" + $(lastSelector).options[$(lastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = $(lastSelector).options[$(lastSelector).selectedIndex].value;
		}
	}
	

	//update hidden input box value
	$('categories').value= selectedOptionValue;

	//update visible category text value
	$('selected-category').innerHTML= htmlText; 

}

function cleanCategorySelect(){
	// select cleanup
	do{
		selectorName = 'category.' + openSelectors;
		$('category').removeChild($(selectorName));
		openSelectors=openSelectors - 1;
	}
	while(selectorId<openSelectors);
}


function processCategoryClick(category) {
	
	var url = '/category/' + category;
	
	var callback = {
		success:handleCategoryClick,
		failure: handleAjaxFailure
	};
	
	var urlParams = null;
	
	YAHOO.util.Connect.asyncRequest('GET', url, callback, urlParams);
}

var handleCategoryClick = function (ajax) {
	if (ajax.responseText !== undefined) {
		categories = eval('(' + ajax.responseText + ')');
		
		// get categories span.
		var categoriesElement = $('categories');
		categoriesElement.innerHTML = categories.html;
	}
}

function processAddUserConnectionClick(userId) {
	var url = '/connection/add';
	var callback = {
		success:handleConnectionClick,
		failure: handleAjaxFailure
	};
	
	var urlParams = 'userId=' + userId;
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
} var handleConnectionClick = function (ajax) {
	if (ajax.responseText !== undefined) {
		connectionText = eval('(' + ajax.responseText + ')');
                id = connectionText.elementId;
                // get connections span.
                var connectionsElement = $('connection_'+id);
		connectionsElement.innerHTML = connectionText.html;
	}
}






function processAcceptUserConnectionClick(userId, authcode) {
	
	var url = '/connection/accept';
	
	var callback = {
		success:handleConnectionApproveRejectClick,
		failure: handleAjaxFailure
	};
	
	var urlParams = 'userId=' + userId + '&authcode=' + authcode;
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
} 
function processRejectUserConnectionClick(userId, authcode) {
	
	var url = '/connection/reject';
	
	var callback = {
		success:handleConnectionApproveRejectClick,
		failure: handleAjaxFailure
	};
	
	var urlParams = 'userId=' + userId + '&authcode=' + authcode;
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
}
var handleConnectionApproveRejectClick = function (ajax) {
	if (ajax.responseText !== undefined) {
		connectionText = eval('(' + ajax.responseText + ')');
		id = connectionText.elementId;
		// get connections span.
		var connectionsElement = $('connection_request_' + id);
		connectionsElement.innerHTML = '';
	}
}

function processRemoveUserConnectionClick(userId, authcode) {
	
	var url = '/connection/remove';
	
	var callback = {
		success:handleConnectionRemoveClick,
		failure: handleAjaxFailure
	};
	
	var urlParams = 'userId=' + userId + '&authcode=' + authcode;
	
	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
}

var handleConnectionRemoveClick = function (ajax) {
	if (ajax.responseText !== undefined) {
		connectionText = eval('(' + ajax.responseText + ')');
		id = connectionText.elementId;
		// get connections span.
		var connectionsElement = $('connection_' + id);
		connectionsElement.innerHTML = '';
		// decrement count
		var connectionCountElement = $('connectionCount');
		var count = connectionCountElement.innerHTML;
		connectionCountElement.innerHTML = count -1;
	}
}






 //////////////////////////////////////////////////
 function processNetworkSelectorChange(network) {
         var url = '/home/filterByNetwork';
 alert(network);
         var callback = {
                         success:handleNetworkSelectChange,
                         failure: handleAjaxFailure
                 };
         var urlParams = 'network=' + netword;
         YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
 } var handleNetworkSelectChange = function (ajax) {
          if (ajax.responseText !== undefined) {
          }
  }
 /////////////////////////////////////////////////////

