/**
 * 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';
	}
}
*/

if (!Array.indexOf) {
  Array.prototype.indexOf = function (obj, start) {
    for (var i = (start || 0); i < this.length; i++) {
      if (this[i] == obj) {
        return i;
      }
    }
  }
}

/**
 * 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';
	}
} 

*/


function loginDecider(response)
{
	location.href = "/facebook"
}

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 = jQuery("#country");
	var countrySelector = document.getElementById('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 handleAjaxFailure = function (ajax) {}
 
var handleCountrySelectorChange = function (ajax) {
         if (ajax.responseText !== undefined) {
                 var regions = eval('(' + ajax.responseText + ')');              // get region selector and remove options.
                 //var selector = jQuery("#state");
				 var selector = document.getElementById('state');
				 if(selector.options)
				 {
                 while (selector.options.length > 0)
                         selector.remove(0);  
					}	 
				var option;
                 // create new Options.
                 for (var i=0; i < regions.length; i++) {
				 		
						for (key in regions[i]) {
							option = new Option(regions[i][key], key);
						}
                         selector.options[selector.options.length] = option;
                 }               // get city selector and remove options.
                 //var selector = jQuery("#city");
				 var selector = document.getElementById('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 = jQuery("#city");
				 var selector = document.getElementById('city');
                 while (selector.options.length > 0)
                         selector.remove(0);             var option;
                 // create new Options.
                 for (var i=0; i < cities.length; i++) {
				 		for(key in cities[i]){
                         option = new Option(cities[i][key], key);
						 }
                         selector.options[selector.options.length] = option;
                 }               selector.disable = false;
         }
 }







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) {
			if (categories.decendants.length > 0) {
				selectorName = 'category.' + selectorId + 1;
				
				var categoriesSpan = jQuery(selectorName);
				
				span = new Object();
				span.tag = "span";
				span.id = 'category.' + (selectorId + 1);
				categoriesSpan = $E(span);
				categoriesDiv = jQuery('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(document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].value==''){
			htmlText="Your selected category is: <b>" + document.getElementById(secondlastSelector).options[document.getElementById(secondlastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = document.getElementById(secondlastSelector).options[document.getElementById(secondlastSelector).selectedIndex].value;
		}
		else {
			htmlText="Your selected category is: <b>" + document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].value;
		}		
	}
	else {
		if(document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].value==''){
			htmlText="Please select a category"; 
			selectedOptionValue = '';
		}
		else{
			htmlText="Your selected category is: <b>" + document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].text + "</b>";
			selectedOptionValue = document.getElementById(lastSelector).options[document.getElementById(lastSelector).selectedIndex].value;
		}
	}
	

	//update hidden input box value
	document.getElementById('categories').value = selectedOptionValue;

	//update visible category text value
	document.getElementById('selected-category').innerHTML= htmlText; 

}

function cleanCategorySelect(){
	// select cleanup
	do{
		selectorName = '#category.' + openSelectors;
		jQuery('#category').removeChild(jQuery(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 = jQuery('#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 = jQuery("#connection_"+id);
		connectionsElement.html(connectionText.html);
	}
}

/////////////////////////////////////////////////////
// added by Mojgan 
// Date: Jan 18th, 2010
////////////////////////////////////////////////////
function processAddUserToListConnectionClick(userId, listId) {
         var url = '/connection/addusertolist';
         var callback = {
                 success:handleAddRemoveListClick,
                 failure: handleAjaxFailure
         };      
         
         var urlParams = 'userId=' + userId + '&listId=' + listId;
         YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
}
function processRemoveUserFromListConnectionClick(userId, listId) {
         var url = '/connection/removeuserfromlist';
         var callback = {
                 success:handleAddRemoveListClick,
                 failure: handleAjaxFailure
         };         var urlParams = 'userId=' + userId + '&listId=' + listId;
		 
		 
		 if (confirm("Do you want to delete this list?")) {
		 	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
		 }
}
var handleAddRemoveListClick = function (ajax) {
         if (ajax.responseText !== undefined) {
             connectionText = eval('(' + ajax.responseText + ')');
             id = connectionText.elementId;
             // get connections span.
             //var connectionsElement = jQuery("#connection_" + id);
             //connectionsElement.html(connectionText.html);
      } 
}

///////////////////////////////////////////////////////
//Added by: Mojgan 
//Date : jan 21th, 2010
//////////////////////////////////////////////////////
function processCreateUserListAndAddToListConnectionClick(userId, listName){ 
      var url = '/connection/addusertolist';
      var callback = {
              success:handleCreateAddListClick,
              failure: handleAjaxFailure
      };
      var urlParams = 'userId=' + userId + '&listName='+ listName;
       YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
 }
 var handleCreateAddListClick = function (ajax) {
      if (ajax.responseText !== undefined) {
          connectionText = eval('(' + ajax.responseText + ')');
          id = connectionText.elementId;
          // get message span.
          //var connectionsElement = jQuery("#popupMsg_" + id);
          //connectionsElement.html(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 = jQuery("#connection_request_" + id);
		connectionsElement.html('');
	}
}//////////////////////////////////////////////////////////////////
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 = jQuery("#connection_" + id);

		connectionsElement.html('');
		// decrement count
		var connectionCountElement = jQuery("#connectionCount");
		var count = connectionCountElement.html();
		connectionCountElement.html(count-1);
	}
}
/////////////////////////////////////////////////////////////////
// added by: mojgan 
// date: Feb 5th, 2010 
// delete all list memebers 
///////////////////////////////////////////////
 function processRemoveUserListConnectionClick(userId, listId , authcode) {
         var url = '/connection/removeuserlist';
         var callback = {
                 success:handleListRemoveClick,
                 failure: handleAjaxFailure
         };
         var urlParams = 'userId=' + userId   +  '&listId=' + listId+   '&authcode=' + authcode;
		 if (confirm("Deleting this list will permanently remove all of the blasts and connections. Are you sure you want to continue?")) {
		 	YAHOO.util.Connect.asyncRequest('POST', url, callback, urlParams);
		 }
 }
 var handleListRemoveClick = function (ajax) {
         if (ajax.responseText !== undefined) {
                connectionText = eval('(' + ajax.responseText + ')');
                id = connectionText.elementId;
                // get list span.
                var connectionsElement = jQuery("#list_" + id);
                connectionsElement.html('');

         }
 }
 //////////////////////////////////////////////////
 function processNetworkSelectorChange(network) {
         var url = '/home/filterByNetwork';
         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) {
          }
 }
 /////////////////////////////////////////////////////

