/* Author: Al Sierra  Version: 12.03.2010  Description: Jquery AJAX for search screens */

/*Create Search variables*/
var $jq = jQuery.noConflict();
search = {};
search.cal = false;
search.inDocumentRef = false;
search.dataURL = false;
search.currentpage = false;
search.msgdata = {};
search.PriorityTags = false;

/*Constructor for search js*/
search.Init = 
	function(inDocumentRef) { 	
		$jq('#search_working').css( 'display', 'none' );
		search.inDocumentRef = inDocumentRef;
		search.dataURL=BitSystem.urls.minisearch+'search.php?';
		var msg_data  = search.msgdata;
		msg_data.category_id = false;
		msg_data.keyword = false;
		msg_data.isPriority = false;
		//msg_data.location = false;
		//msg_data.radius = false;
		msg_data.pagenum = 1;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.audiencetype = false;
		msg_data.interests = false;
		msg_data.skills = false;
		msg_data.date = false;
		//msg_data.OpportunityTypeID = 2;
		search.msgdata = msg_data;
	};

/*Constructor for home page search*/
search.HomeInit = 
	function(oppcategories){
	 	var cmd = search.getCookie('home_cmd');
	 	var isHash = document.location.hash;
		if(oppcategories){
			search.SetPriorityTags(oppcategories);
		}
	 	if (cmd!==null && isHash != ''){
	 		var msg_data = {};
			msg_data.category_id = search.getCookie('home_category_id');
			msg_data.keyword = search.getCookie('home_keyword');
			msg_data.isPriority = search.getCookie('home_isPriority');
			msg_data.pagenum = search.getCookie('home_pagenum');
			msg_data.sort_by = search.getCookie('home_sort_by');
			msg_data.sort_type = search.getCookie('home_sort_type');
			msg_data.cmd = cmd;
			search.CookieHomeSearch(msg_data);
	 	} else {
	 	 	search.InitializeHomeSearch();
	 	}
	 };
	 
/*Constructor for advance page search*/
search.AdvInit = 
	function() { 
		var cmd = search.getCookie('adv_cmd');
		var cmdw = search.getQuerystring('cmd');
		var isHash = document.location.hash;
		search.cal = new CalendarPopup();
		if(cmdw == 'widget' && isHash == ''){
			search.SearchFromWidget();
		} else if (cmd!==null && isHash != '') { 
			var msg_data = {};
			msg_data.category_id = search.getCookie('adv_category_id');
			msg_data.keyword = search.getCookie('adv_keyword');
			msg_data.isPriority = search.getCookie('adv_isPriority');
			//msg_data.location = search.getCookie('adv_location');
			//msg_data.radius = search.getCookie('adv_radius');
			msg_data.pagenum = search.getCookie('adv_pagenum');
			msg_data.sort_by = search.getCookie('adv_sort_by');
			msg_data.sort_type = search.getCookie('adv_sort_type');
			msg_data.audiencetype = search.getCookie('adv_audiencetype');
			msg_data.interests = search.getCookie('adv_interests');
			msg_data.skills = search.getCookie('adv_skills');
			msg_data.date = search.getCookie('adv_date');
			//msg_data.OpportunityTypeID = search.getCookie('adv_OpportunityTypeID');
			msg_data.cmd = cmd;
			if (msg_data.category_id ===null) {msg_data.category_id = false;}
			if (msg_data.keyword ===null) {msg_data.keyword = false;}
			if (msg_data.isPriority ===null) {msg_data.isPriority = false;}
			//if (msg_data.location ===null) {msg_data.location = false;}
			//if (msg_data.radius ===null) {msg_data.radius = false;}
			if (msg_data.pagenum ===null) {msg_data.pagenum = 1;}
			if (msg_data.sort_by ===null) {msg_data.sort_by = false;}
			if (msg_data.sort_type ===null) {msg_data.sort_type = false;}
			if (msg_data.audiencetype ===null) {msg_data.audiencetype = false;}
			if (msg_data.interests ===null) {msg_data.interests = false;}
			if (msg_data.skills ===null) {msg_data.skills = false;}
			if (msg_data.date ===null) {msg_data.date = false;}
			//if (msg_data.OpportunityTypeID ===null) {msg_data.OpportunityTypeID = 2;}
			search.CookieAdvanceSearch(msg_data);
		} else {
			 search.DeleteCookies();
		}
		$jq('#advance_reset_link').click( function( event, anchor ){search.ResetAdvanceForm();});
		$jq('#calendar_window').css( 'display', 'none' );
	};
/**********************************************************************************************************************
Initial Search Functions
**********************************************************************************************************************/
/*Automatic initial search on the homepage*/
search.InitializeHomeSearch = 
	function(){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.cmd = 'getinitializesearch';
		search.home_Ajax(msg_data);
	};

/*Stores the priority tags in a js object*/
search.SetPriorityTags=
	function (tags){
		search.PriorityTags = new Array();
		for (var x = 0; x < tags.length; x++){
			search.PriorityTags[tags[x]['content_id']] = tags[x]['title'];
		}
	};
/**********************************************************************************************************************
Home Search Functions
**********************************************************************************************************************/	
/*Search by Keyword submission*/
search.SubmitSearchWord = 
	function(){
		var kw = $jq('#keyword').val();
		var keyword = false;
		if (kw != '') {
			if(!search.ValidateString(kw)){
				alert('Sorry, There are Invalid Characters in your Keyword Search');
				$jq('#keyword').val('');
				return ;
			}
			keyword = kw;
		}		
		search.SubmitKeywordSearchWord(keyword);
		return ;
	};
search.SubmitKeywordSearchWord = 
	function(searchword){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.keyword = searchword;
		if(searchword != ''){
			_gaq.push(['_trackPageview','homepage>help_with>additional_opportunities>'+searchword]);
		} 
		msg_data.isPriority = false;
		msg_data.pagenum = 1;
		msg_data.category_id = false;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.cmd = 'gethomepagesearch';
		search.home_Ajax(msg_data);
	};
	
/*Search by clicking on a priority*/
search.SearchPriority = 
	function(PriorityId){
		$jq('#search_working').css( 'display', 'block' );
		$jq('#main_search_dropdown_tag').html(search.PriorityTags[PriorityId]);
		$jq('#six-ddcontent').css( 'display', 'none' );
		var msg_data = search.msgdata;
		msg_data.category_id = PriorityId;
		msg_data.isPriority = true;
		msg_data.pagenum = 1;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.cmd = 'gethomepagesearch';
		var custom_functions = {};
		custom_functions.complete = new Array();
		custom_functions.complete.push('search.SearchPriority_complete');
		search.home_Ajax(msg_data,custom_functions);
		return ;
	};

/*on ajax complete, set the dropdown menu item*/
search.SearchPriority_complete = 
	function(msg_data){
		setTimeout("ddMenu('six',-1,'big')",100);
	};

/*Pagination for the homepage results*/
search.Pagination = 
	function(pagenum){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		if( pagenum == -1 ){
			pagenum = 1;
			/*This check is part of the highlighted page hack.*/
			if(msg_data.category_id ==false && msg_data.sort_by == false){
				msg_data.cmd = 'getinitializesearch';
			}else{
				msg_data.cmd = 'gethomepagesearch';
			}
		}else{
			msg_data.cmd = 'gethomepagesearch';
		}
		msg_data.pagenum = pagenum;
		search.home_Ajax(msg_data);
	};

/*Sorting for the homepage results*/
search.SortBy_home = 
	function(sort,sort_type){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.cmd = 'gethomepagesearch';
		msg_data.pagenum = 1;
		msg_data.sort_by = sort;
		msg_data.sort_type = sort_type;
		search.home_Ajax(msg_data);
	};

/*Cookie based search for the homepage results*/
search.CookieHomeSearch = 
	function(msg_data){
		$jq('#search_working').css( 'display', 'block' );
		var custom_functions = {};
		custom_functions.complete = new Array();
		custom_functions.complete.push('search.CookieHomeSearch_complete');
		search.home_Ajax(msg_data,custom_functions); 
	};
/*on ajax complete, set the dropdown menu item or keyword*/
search.CookieHomeSearch_complete =
	function(msg_data){
		if(msg_data.cmd == 'gethomepagesearch'){
			if(msg_data.keyword != 'false'){
				search.GetSearchInput();
				$jq('#keyword').val(msg_data.keyword);
			}else if(msg_data.category_id){
				$jq('#main_search_dropdown_tag').html(search.PriorityTags[parseInt(msg_data.category_id)]);
			}
		}
	};

/*Display the priority dropdown on the homepage*/
search.ShowDropDown = 
	function(){
		$jq('#six-ddcontent').css( 'display', 'block' );
	};

/*Sets the keyword input field on the homepage*/
search.GetSearchInput = 
	function(){
		$jq('#main_search_input').css( 'display', 'block' );
		$jq('#main_search_dropdown').css( 'display', 'none' );
		$jq('#keyword').focus();
	};

/*Sets the priority dropdown on the homepage*/
search.GetSearchDropdown = 
	function(){
		$jq('#main_search_dropdown_tag').html("");
		$jq('#main_search_dropdown').css( 'display', 'block' );
		$jq('#main_search_input').css( 'display', 'none' );
		$jq('#keyword').val("");
	};

/*Generalized Ajax call for the homepage*/
search.home_Ajax = 
	function(msg_data,custom_functions){
		custom_functions = typeof(custom_functions) != 'undefined' ? custom_functions : false;
		$jq.ajax({
		   type: "POST",
		   dataType: 'json',
		   url: search.dataURL,
		   data: msg_data,
		   success: function(JSON){
			try {
				if (JSON.emsg) {
					alert(JSON.emsg);
					return false;
				}
				$jq('#search_results').html(JSON.html);
				search.currentpage = msg_data.pagenum;
				search.msgdata = msg_data;
				if(custom_functions.success){
					for(var x = 0 ; x <  custom_functions.success.length ; x++){
						eval(custom_functions.success[x]+"(msg_data);");
					}
				}
				
			}catch(e) {
				//alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
		   },
		   complete: function(){
				$jq('#search_working').css( 'display', 'none' );
				search.SetCookiesHome();
				parent.location.hash = "s";
				
				if(custom_functions.complete){
					for(var x = 0 ; x <  custom_functions.complete.length ; x++){
						eval(custom_functions.complete[x]+"(msg_data);");
					}
				}
		   }
		 });
	};
	
/*Set Cookies for the homepage*/
search.SetCookiesHome = 
	function (){
	 	var msg_data = search.msgdata;
	 	search.setCookie('home_cmd',msg_data.cmd,0,'/',document.domain,false);
	 	search.setCookie('home_category_id',msg_data.category_id,0,'/',document.domain,false);
	 	search.setCookie('home_keyword',msg_data.keyword,0,'/',document.domain,false);
	 	search.setCookie('home_isPriority',msg_data.isPriority,0,'/',document.domain,false);
	 	search.setCookie('home_pagenum',msg_data.pagenum,0,'/',document.domain,false);
	 	search.setCookie('home_sort_by',msg_data.sort_by,0,'/',document.domain,false);
	 	search.setCookie('home_sort_type',msg_data.sort_type,0,'/',document.domain,false);
	 };
	 
/*Delete Cookies for the homepage*/
search.DeleteCookiesHome = 
	function (){
		search.deleteCookie('home_category_id','/',document.domain);
		search.deleteCookie('home_cmd','/',document.domain);
		search.deleteCookie('home_keyword','/',document.domain);
		search.deleteCookie('home_isPriority','/',document.domain);
		search.deleteCookie('home_pagenum','/',document.domain);
		search.deleteCookie('home_sort_by','/',document.domain);
		search.deleteCookie('home_sort_type','/',document.domain);
	};
/**********************************************************************************************************************
Advance Search Functions
**********************************************************************************************************************/
/*Submit the advance page form and retrieve the results*/
search.SubmitAdvanceSearch = 
	function(){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		/*		
		if($jq('#category_id').val() != '0' && $jq('#category_id').val() != ""){
			msg_data.category_id = $jq('category_id').val();
			 //pageTracker._trackPageview('volunteers>find_opportunity>go>'+search.GetPriorityTagForGoogleTracking(msg_data.category_id));
		} else {
			msg_data.category_id = false;
		}
		*/
		msg_data.category_id = false;
		if ($jq('#adv_keyword').val()!='') {
			if(!search.ValidateString($jq('#adv_keyword').val())){
				alert('Sorry, There are Invalid Characters in your Keyword Search');
				$jq('#search_working').css( 'display', 'none' );
				return ;
			}
			msg_data.keyword = $jq('#adv_keyword').val();
			 //pageTracker._trackPageview('advance_search>keyword>go>'+$jq('adv_keyword').value);
		} else {
			msg_data.keyword = false;
		}
/* Temporarily Removed for COS
		if ($jq('#adv_zip').val()!=''){
			if($jq('#adv_zip').val() != 'Enter zip code'){
				if(!search.ValidateNumber($jq('#adv_zip').val()) || $jq('#adv_zip').val().length != 5){
					alert('Sorry, The Zip Code you entered is invalid.');
					$jq('#search_working').css( 'display', 'none' );
					return ;
				}
				msg_data.location = $jq('#adv_zip').val();
				msg_data.radius = $jq('#adv_radius').val();
			} else {
				msg_data.location = false;
				msg_data.radius = false;
			}
		} else {
			msg_data.location = false;
			msg_data.radius = false;
		}
*/
		if($jq('#adv_date').val() != ''){
			msg_data.date = $jq('#adv_date').val();
			//msg_data.OpportunityTypeID = 1;
			//$jq('#adv_search_tab-2').click(function() {});
		} else {
			msg_data.date = false;
			//msg_data.OpportunityTypeID = 2;
			//$jq('#adv_search_tab-2').click(function() {search.ToogleOpportunityType(2);});	
		}
		msg_data.category_id = $jq('#category_id').val();
		msg_data.pagenum = 1;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.audiencetype = search.GetSelectorValues('adv_audiencetype_multiform','adv_audiencetype');
		msg_data.interests = search.GetSelectorValues('adv_interest_multiform','adv_interest');
		msg_data.skills = search.GetSelectorValues('adv_skill_multiform','adv_skill');
		// msg_data.location == false
		if(msg_data.keyword == false && msg_data.category_id == false && msg_data.date == false && msg_data.audiencetype == false && msg_data.interests == false && msg_data.skills == false){
			 //pageTracker._trackPageview('volunteers>find_opportunity>go');
		}
		msg_data.cmd = 'getadvancesearch';
		//search.OpportunityTypeToggle(msg_data.OpportunityTypeID);
		search.adv_Ajax(msg_data);
		search.activateSearchTab();
	};

/*Cookie based searched for the advance page*/
search.CookieAdvanceSearch = 
	function(msg_data){
		msg_data.cmd = 'getadvancesearch';
		$jq('#search_working').css( 'display', 'block' );
		var custom_functions = {};
		custom_functions.success = new Array();
		custom_functions.success.push('search.SetSelectors');		
		//search.OpportunityTypeToggle(msg_data.OpportunityTypeID);
		search.adv_Ajax(msg_data,custom_functions);
		search.activateSearchTab();
	};
	
/*DEPRECATED*/
/*One-time vs Ongoing tab search for the advance page*/
search.ToogleOpportunityType = 
	function(OpportunityTypeID){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.pagenum = 1;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.OpportunityTypeID = OpportunityTypeID;
		msg_data.cmd = 'getadvancesearch';
		search.OpportunityTypeToggle(msg_data.OpportunityTypeID);
		search.adv_Ajax(msg_data);
	};

/*Pagination for the advance page*/
search.Pagination_adv = 
	function(pagenum){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.cmd = 'getadvancesearch';
		msg_data.pagenum = pagenum;
		search.adv_Ajax(msg_data);
	};

/*Sorting for the advance page*/
search.SortBy_adv = 
	function(sort,sort_type){
		$jq('#search_working').css( 'display', 'block' );
		var msg_data = search.msgdata;
		msg_data.cmd = 'getadvancesearch';
		msg_data.pagenum = 1;
		msg_data.sort_by = sort;
		msg_data.sort_type = sort_type;
		search.adv_Ajax(msg_data);
	};

/*Generalized Ajax call for the advance page*/
search.adv_Ajax = 
	function(msg_data,custom_functions){
		custom_functions = typeof(custom_functions) != 'undefined' ? custom_functions : false;
		$jq.ajax({
		   type: "POST",
		   dataType: 'json',
		   url: search.dataURL,
		   data: msg_data,
		   success: function(JSON){
			try {
				if (JSON.emsg) {
					alert(JSON.emsg);
					return false;
				}
				$jq('#search_results').html(JSON.html);
				search.currentpage = msg_data.pagenum;
				search.msgdata = msg_data;
				if(custom_functions.success){
					for(var x = 0 ; x <  custom_functions.success.length ; x++){
						eval(custom_functions.success[x]+"(msg_data);");
					}
				}
				
			}catch(e) {
				//alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
		   },
		   complete: function(){
				$jq('#search_working').css( 'display', 'none' );
				search.SetCookies();
				parent.location.hash = "s";
				
				if(custom_functions.complete){
					for(var x = 0 ; x <  custom_functions.complete.length ; x++){
						eval(custom_functions.complete[x]+"(msg_data);");
					}
				}
		   }
		 });
	};

/*Activates the tab*/
search.activateSearchTab =
	function (){
		$jq('#search_results_tab').css( 'display', 'block' );		
	};

/*DEPRECATED*/
/*UI for toggling the tab*/
search.OpportunityTypeToggle = 
	function (OpportunityTypeID){
		if(OpportunityTypeID == 1){
			$jq('#adv_search_tab-2').css('color', "#ffffff");
			$jq('#adv_search_tab-2').css('background', "#D1D1D1");
			$jq('#adv_search_tab-1').css('color', "#ffffff");
			$jq('#adv_search_tab-1').css('background', "#4f4c55");
		} else {
			$jq('#adv_search_tab-2').css('color', "#ffffff");
			$jq('#adv_search_tab-2').css('background', "#4f4c55");
			$jq('#adv_search_tab-1').css('color', "#ffffff");
			$jq('#adv_search_tab-1').css('background', "#D1D1D1");
		}
	};
	
/*Gets the date pop up module*/	
search.getDateCal = 
	function (){
		var inputObj = $jq('#adv_date');
		search.cal.select(inputObj[0],'anchor1','yyyy-MM-dd');
		return ;
	};
/**********************************************************************************************************************
Common Search Functions
**********************************************************************************************************************/	
/*Common sort function for the UI to use. Let JS decifer what search page it is for*/	
search.SortBy =
	function (sort,sort_type,form){
		if(form=='home'){
			search.SortBy_home(sort,sort_type);
		}else if(form=='advance'){
			search.SortBy_adv(sort,sort_type);
		}
	};


/**********************************************************************************************************************
Advance Calendar Search Functions
**********************************************************************************************************************/	
/*Intitialize and get the calendar that displayes one time opportunities*/	
search.GetOneTimeCalendar= 
	function(){
		var msg_data = {};
		msg_data.cmd = 'getcalendar';
		var custom_functions = {};
		custom_functions.complete = new Array();
		custom_functions.complete.push('search.GetOneTimeCalendar_complete');	
		search.cal_Ajax(msg_data,custom_functions);
	};

/*Get the previous month calendar that displayes one time opportunities*/	
search.CalPrevious= 
	function(m,y,aPrevious){
		if(!aPrevious){
			return ;
		}
		var msg_data = {};
		msg_data.cmd = 'getcalendar';
		msg_data.cmonth = m;
		msg_data.cyear = y;
		search.cal_Ajax(msg_data);
	};

/*Get the next month calendar that displayes one time opportunities*/	
search.CalNext= 
	function(m,y){
		var msg_data = {};
		msg_data.cmd = 'getcalendar';
		msg_data.cmonth = m;
		msg_data.cyear = y;
		search.cal_Ajax(msg_data);
	};

/*Generalized Ajax call for the calendar*/	
search.cal_Ajax = 
	function(msg_data,custom_functions){
		custom_functions = typeof(custom_functions) != 'undefined' ? custom_functions : false;
		$jq.ajax({
		   type: "POST",
		   dataType: 'json',
		   url: search.dataURL,
		   data: msg_data,
		   success: function(JSON){
			try {
				if (JSON.emsg) {
					alert(JSON.emsg);
					return false;
				}
				$jq('#calendar_window').html(JSON.html);
				if(custom_functions.success){
					for(var x = 0 ; x <  custom_functions.success.length ; x++){
						eval(custom_functions.success[x]+"(msg_data);");
					}
				}
				
			}catch(e) {
				//alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
		   },
		   complete: function(){
				if(custom_functions.complete){
					for(var x = 0 ; x <  custom_functions.complete.length ; x++){
						eval(custom_functions.complete[x]+"(msg_data);");
					}
				}
		   }
		 });
	};

/*Hide the calendar*/
search.FadeScreen = 
	function(){
		$jq('#calendar_window').css( 'display', 'none' );
	};

/*Display the calendar*/
search.GetOneTimeCalendar_complete = 
	function(msg_data){
		$jq('#calendar_window').center();
		$jq('#calendar_window').show();		
	};
/**********************************************************************************************************************
Search Widget Functions: Temporarily Removed for COS  
**********************************************************************************************************************/	
/*NO WIDGET IN COS
search.SearchWidgetEmbed = 
	function(popup){
		var category_id = $jq('#sw_category').val();
		var keyword = $jq('#sw_keyword').val();
		var zip = $jq('#sw_zip').val();
		var isZip = false;
		
		if(search.ValidateNumber(zip)){
			if(zip.length != 5){
				alert('Invalid Zip Code Format');
				return;
			}
			isZip = true;
		} else{
			if(zip == 'Enter zip code' || zip == ''){
				zip = false;
			}
		}
		
		if (!keyword =='') {
			if(keyword != 'Enter skill, passion or interest'){
				keyword = keyword.replace(' ','+');
				if(!search.ValidateString(keyword)){
					alert('Sorry, There are Invalid Characters in your Keyword Search');
					return ;
				}	 
			} else {
				keyword = false;
			}
		} else {
			keyword = false;
		}
		var url = BitSystem.urls.minisearch+"index.php?cmd=widget&category_id="+category_id+"&keyword="+keyword+"&zip="+zip+"&isZip="+isZip;
		if(popup){
			var day = new Date();
			var id = day.getTime();
			eval("page" + id + " = window.open('" + url + "', '" + id + "', '');");
		} else {
			window.location = url;
		}
	};

search.SearchFromWidget = 
	function(){
		var msg_data = search.msgdata;
		msg_data.cmd = 'getadvancesearch';
		//Get Mayor Priority
		var category_id = search.getQuerystring('category_id');
		if(category_id != '0'){
			msg_data.category_id = category_id;
		} else {
			msg_data.category_id = false;
		}
		$jq('#category_id').val(category_id);
		
		//Get Keyword
		var keyword = search.getQuerystring('keyword');
		keyword = keyword.replace('+',' ');
		msg_data.keyword = keyword;
		if(keyword != 'false'){
			$jq('adv_keyword').val(keyword);
		}
		//Get Zip or Borough
		var zip = search.getQuerystring('zip');
		var isZip = search.getQuerystring('isZip');			
		if(isZip == 'true'){
			$jq('#adv_zip').val(zip);
			msg_data.location = zip;
			msg_data.radius = '1';
		}
		msg_data.pagenum = 1;
		msg_data.sort_by = false;
		msg_data.sort_type = false;
		msg_data.audiencetype = search.GetSelectorValues('adv_audiencetype_multiform','adv_audiencetype');
		msg_data.interests = search.GetSelectorValues('adv_interest_multiform','adv_interest');
		msg_data.skills = search.GetSelectorValues('adv_skill_multiform','adv_skill');
		msg_data.OpportunityTypeID = 2;
		$jq.ajax({
		   type: "POST",
		   dataType: 'json',
		   url: search.dataURL,
		   data: msg_data,
		   success: function(JSON){
			try {
				if (JSON.emsg) {
					alert(JSON.emsg);
					return false;
				}
				$jq('#search_results').html(JSON.html);
					if(msg_data.OpportunityTypeID == 1){
						$jq('#adv_search_tab-2').css('color', "#ff9a00");
						$jq('#adv_search_tab-2').css('background', "#ce7c00");
						$jq('#adv_search_tab-1').css('color', "#fff");
						$jq('#adv_search_tab-1').css('background', "#4f4c55");
					} else {
						$jq('#adv_search_tab-2').css('color', "#fff");
						$jq('#adv_search_tab-2').css('background', "#4f4c55");
						$jq('#adv_search_tab-1').css('color', "#ff9a00");
						$jq('#adv_search_tab-1').css('background', "#ce7c00");
					}
					$jq('#search_results_tab').css( 'display', 'block' );
					search.currentpage = 1;
					search.msgdata = msg_data;
			}catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
		   },
		   complete: function(){
				$jq('#search_working').css( 'display', 'none' );
				search.SetCookies();
				parent.location.hash = "s";
		   }
		 });
	};
*/	
/**********************************************************************************************************************
Advance Search DOM Functions
**********************************************************************************************************************/	
/*Set Cookie function for advance page*/
search.SetCookies = 
	function(){
		var msg_data  = search.msgdata;
		search.setCookie('adv_category_id',msg_data.category_id,0,'/',document.domain,false);
		search.setCookie('adv_keyword',msg_data.keyword,0,'/',document.domain,false);
		search.setCookie('adv_isPriority',msg_data.isPriority,0,'/',document.domain,false);
		//search.setCookie('adv_location',msg_data.location,0,'/',document.domain,false);
		//search.setCookie('adv_radius',msg_data.radius,0,'/',document.domain,false);
		search.setCookie('adv_pagenum',msg_data.pagenum,0,'/',document.domain,false);
		search.setCookie('adv_sort_by',msg_data.sort_by,0,'/',document.domain,false);
		search.setCookie('adv_audiencetype',msg_data.audiencetype,0,'/',document.domain,false);
		search.setCookie('adv_interests',msg_data.interests,0,'/',document.domain,false);
		search.setCookie('adv_skills',msg_data.skills,0,'/',document.domain,false);
		//search.setCookie('adv_OpportunityTypeID',msg_data.OpportunityTypeID,0,'/',document.domain,false);
		search.setCookie('adv_cmd',msg_data.cmd,0,'/',document.domain,false);
		search.setCookie('adv_sort_type',msg_data.sort_type,0,'/',document.domain,false);
		search.setCookie('adv_date',msg_data.date,0,'/',document.domain,false);
	};
/*Delete Cookie function for advance page*/
search.DeleteCookies = 
	function(){
		search.deleteCookie('adv_category_id','/',document.domain);
		search.deleteCookie('adv_keyword','/',document.domain);
		search.deleteCookie('adv_isPriority','/',document.domain);
		//search.deleteCookie('adv_location','/',document.domain);
		//search.deleteCookie('adv_radius','/',document.domain);
		search.deleteCookie('adv_pagenum','/',document.domain);
		search.deleteCookie('adv_sort_by','/',document.domain);
		search.deleteCookie('adv_audiencetype','/',document.domain);
		search.deleteCookie('adv_interests','/',document.domain);
		search.deleteCookie('adv_skills','/',document.domain);
		//search.deleteCookie('adv_OpportunityTypeID','/',document.domain);
		search.deleteCookie('adv_cmd','/',document.domain);
		search.deleteCookie('adv_sort_type','/',document.domain);
		search.deleteCookie('adv_date','/',document.domain);
	};

/* Reset the entire form on the advance page */
search.ResetAdvanceForm = 
	function(){
		$jq('#category_id').val('');
		$jq('#adv_keyword').val('');
		$jq('#adv_zip').val('');
		$jq('#adv_radius').val('1');
		$jq('#adv_date').val('');
		$jq('#adv_interest').val('');
		$jq('#adv_skill').val('');
		$jq('#adv_audiencetype').val('');
		search.RemoveSelectors('adv_audiencetype_multiform','advance_audiencetype');
		search.RemoveSelectors('adv_interest_multiform','advance_interest');
		search.RemoveSelectors('adv_skill_multiform','advance_skill');
		search.DeleteCookies();
	};
	
/* Dynamic sets the multiform selectors. This is specifically when searching by cookies when user returns to the page */
search.SetSelectors = 
	function (msg_data){
		/*Audience Types DropDowns*/
		var pointer = -1;
		if(msg_data.audiencetype != 'false'){
			var audiencetypes = $jq.parseJSON(msg_data.audiencetype);
			if(audiencetypes.length > 0){
				$jq('#adv_audiencetype').val(audiencetypes[0]);
				if(audiencetypes.length > 1){	
					for(var x = 1 ; x <  audiencetypes.length ; x++){
						BitMultiForm.addForm('advance_audiencetype_temp', 'adv_audiencetype_multiform');
						pointer = pointer+1;
						$jq('#adv_audiencetype_'+pointer).val(audiencetypes[x]);
					}
				}
			}
		}
		/*Interests Drop Downs*/
		pointer = -1;
		if(msg_data.interests != 'false'){
			var interests = $jq.parseJSON(msg_data.interests);
			if(interests.length > 0){
				$jq('#adv_interest').val(interests[0]);
				if(interests.length > 1){
					for(x = 1 ; x <  interests.length ; x++){
						BitMultiForm.addForm('advance_interest_temp', 'adv_interest_multiform');
						pointer = pointer+1;
						$jq('#adv_interest_'+pointer).val(interests[x]);
					}
				}
			}
		}
		/*Skill Drop Downs*/
		pointer = -1;
		if(msg_data.skills != 'false'){
			var skills = $jq.parseJSON(msg_data.skills);
			if(skills.length > 0){
				$jq('#adv_skill').val(skills[0]);
				if(skills.length > 1){
					for(x = 1 ; x <  skills.length ; x++){
						BitMultiForm.addForm('advance_skill_temp', 'adv_skill_multiform');
						pointer = pointer+1;
						$jq('#adv_skill_'+pointer).val(skills[x]);
					}
				}
			}
		}
	};

/*Removes All Selectors and resets the first one*/
search.RemoveSelectors = 
	function (selector_div,selector_type){
		$jq("#"+selector_div+" div").each(function(){
			if($jq(this).hasClass('row') && $jq(this).attr('id') != selector_type){
				BitMultiForm.removeForm($jq(this).attr('id'));
			}
		});
	};

/*Gets the Values of the Multiform Selectors*/
search.GetSelectorValues = 
	function (selector_div){
		var selector_id = new Array();
		var selval;
		$jq("#"+selector_div+" select").each(function(){
			if($jq(this).hasClass('selectInput')){
				selval = $jq(this).val();
				if(selval != '0' && selval != 'e' && selval != ''){
					selector_id.push(selval);
				}
			}
		});
		if(selector_id.length < 1){
			return false;
		}else {
			return $jq.toJSON(selector_id);
		}
	};
/**********************************************************************************************************************
Default Functions
**********************************************************************************************************************/		
/*Function to get a querysting value from the URL*/
search.getQuerystring = 
	function (key, default_){
		if (default_==null) default_="";
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);
		if(qs == null)
			return default_;
		else
			return qs[1];
	};

/*Custom submit code for the homepage*/	
search.submit = 
	function(myfield,e){
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
		{
			
			return false;
	   }
		else
		return true;
	};

/*String validator function*/	
search.ValidateString =
	 function (s){
        if(s==undefined || s==null || s.length==0) {
            return false;
        }
        
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(s)) {
            return false;
        }
     
        //Check for html tags
        reHtmlTags = new RegExp(/<(.|\n)/);
        if (reHtmlTags.test(s)) {
            return false;
        }
        return true;
    };

/*Number validator function*/
search.ValidateNumber = 
	function (number) {
	 	reNumeric = new RegExp(/^[0-9]+$/);
        if (!reNumeric.test(number)) {
        	return false;
        }
        return true;
    };

/*Zip code validator: Not in use aslong as zip code is not part of the advance form*/
search.ValidateZip = 
	function (number) {
		if(number.length != 5){
			return false;
		}
	 	reNumeric = new RegExp(/^[0-9]+$/);
        if (!reNumeric.test(number)) {
        	return false;
        }
        return true;
    };

/*Custom get, set and delete cookie functionaly. Bitweaver version was not giving me accurate results*/	
/*** Start Cookie Code (Public Domain) ***/
search.getCookie = 
	function ( name ) {
		var start = document.cookie.indexOf( name + "=" );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ";", len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	};

search.setCookie = 
	function ( name, value, expires, path, domain, secure ) {
		var today = new Date();
		today.setTime( today.getTime() );
		if ( expires ) {
		  expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+"="+escape( value ) +
		  ( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		  ( ( path ) ? ";path=" + path : "" ) +
		  ( ( domain ) ? ";domain=" + domain : "" ) +
		  ( ( secure ) ? ";secure" : "" );
	};

search.deleteCookie = 
	function ( name, path, domain ) {
		if ( search.getCookie( name ) ) document.cookie = name + "=" +
		  ( ( path ) ? ";path=" + path : "") +
		  ( ( domain ) ? ";domain=" + domain : "" ) +
		  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	};
/*** End Cookie Code (Public Domain) ***/

/**********************************************************************************************************************
 jQuery Functions
**********************************************************************************************************************/

/*Custom jquery function to position a object in the center of the screen*/
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $jq(window).height() - this.height() ) / 2+$jq(window).scrollTop() + "px");
    this.css("left", ( $jq(window).width() - this.width() ) / 2+$jq(window).scrollLeft() + "px");
    return this;
};
