/**
 * @author barbie
 */
var $searchList = new Array();
var $curFaceBox = 0;//the id of the facebox currently shown(if any)
var searchSource = '';//the source of the search. search or ftags
var searchTerm = '';//incase the search source is the ftags, store the tag
			
$(function(){
	$catBox = $('#fs-biz');
	$steps = $('#steps');
	$regForm = $('#regBox');
	$actBox = $('#actBox');
	//$lskards = $('#bzk-container');//listed kards
	$ftKards = $('#fkards');//featured kards
	
	$searchVisible = true;//the search is visible at the onset
	$isUpdate = false;//is the process currently being carried out the update?
	
	//inject styling into the steps, if any
	$('#steps li').each(function(i){
		i = i + 1;
		$(this).addClass('s'+i);
	});
	
	/*$('#createBtn').toggle(
		function(){
			hideSearch();
			$steps.slideUp();
			$actBox.slideUp();
			$ftKards.slideUp();
			$regForm.slideDown();
			
			$isUpdate = false;//becos the regform is currently shown
			
			$(this).val('Search');
		}, 
		function(){
			$regForm.slideUp().find('#regForm').show();
			showSearch();
			$steps.slideDown();
			$ftKards.slideDown();
			$(this).val('Join');
		}
	);*/
	$('#createBtn').click(function(){
		$this = $(this);
		$actionCommand = $this.attr('value');
		if ($actionCommand == 'Join') {
			hideSearch();
			$steps.slideUp();
			$actBox.slideUp();
			$ftKards.slideUp();
			$regForm.slideDown();
			
			$isUpdate = false;//becos the regform is currently shown
			
			$this.val('Search');
		}else if ($actionCommand == 'Search') {
			$regForm.slideUp().find('#regForm').show();
			showSearch();
			$steps.slideDown();
			$ftKards.slideDown();
			$this.val('Join');
		}
	});
	
	/*$('#actBtn').toggle(
		function(){
			showActivationForm();
			$(this).val('Search');
		}, 
		function(){
			hideActivationForm();
			$(this).val('Activate');
		}
	);*/
	
	$('#actBtn').click(function(){
		$this = $(this);
		$actionCommand = $this.attr('value');
		if ($actionCommand == 'Activate') {
			showActivationForm();
			$this.val('Search');
		}else if ($actionCommand == 'Search') {
			hideActivationForm();
			$this.val('Activate');
		}
	});
	
	$('#sLnk').click(function(){
		if($searchVisible){//hide it
			hideSearch();
			return false;
		}else{//show it
			showSearch();
			return false;
		}
	});
		
	var hideSearch = function(){
		$catBox.slideUp('fast');
		$('#sLnk > a').removeClass().addClass('down').attr('title', 'Show Search');
		$searchVisible = false;
	}
	
	var showSearch = function(){
		$catBox.slideDown('fast');
		$('#sLnk > a').removeClass().addClass('up').attr('title', 'Hide Search');
		$searchVisible = true;
	}
	
	$('#cat').autocomplete('mediator.php?sc=AUTO', {width: 230, selectFirst: false}).result(function(evt, item){
		queueBubble($(this), item);
	});
		
	$('span[class=close_sRes]').live('click', function(){
		$this = $(this);
		$parent = $(this).parent();
		
		$parent.fadeIn('slow', function(){
			t = $this.prev().text();
			removeItem(t);
			$parent.remove();
			
			if(isEmpty())					
				$('#sList').hide('fast');
		});
		
		return false;
	});
	
	//hide all the controls for sending mails first
	$('div.acp').css('visibility', 'hidden');
	
	$('div.bizkard').each(function(){
		var $btn = $(this).children('div.acp');
		
		$(this).hover(
			function(){
				$btn.css('visibility', 'visible');
				$(this).css('background', 'url(images/kard-bg-over.jpg) repeat-x');
			}, 
			function(){
				$btn.css('visibility', 'hidden');
				$(this).css('background', 'url(images/kard-bg.jpg) repeat-x');
			}
		);
	});

	$regForm.submit(function(){
		$('div.regMsg').hide('fast').remove();

		if(getTrimmedString($('#c1').val()) == ''){
			displayError("You need to specify your business name");
			return false;
		}
		
		$num = getTrimmedString($('#a4').val());
		if($num == ''){
			displayError("You need to specify your mobile number");
			return false;
		}else{
			//more checks, such as matching for numbers only and 11 digits (but what of non-nigerian residents?)
			if(!isPhoneNoValid($num)){
				displayError("The mobile number specified is either invalid or less than 11 digits");
				return false;
			}
		}
		
		$email = getTrimmedString($('#e1').val());
		if($email == ''){
			displayError("You need to specify your email address");
			return false;
		}else{
			if(!isEmailValid($email)){
				displayError("The email address specified is invalid");
				return false;
			}
		}
			
		if(getTrimmedString($('#a3').val()) == ''){
			displayError("You need to specify your mailing or contact address");
			return false;
		}
			
		if (getTrimmedString($('#g1').val()) == '') {
			displayError("You need to specify your state of operation");
			return false;
		}
			
		if (getTrimmedString($('#v1').val()) == '') {
			displayError("You need to specify your products and services that describe your business");
			return false;
		}
		
		if($('#agree').val() != 'on'){
			displayError("You need to agree to our terms of use");
			return false;
		}
		
		//remove the previous throbber if any and show the new throbber and post the data
		$('div.regThrobber').remove();
		$throbber = $("<div class='regThrobber'>Submitting form...</div>").insertAfter('div#regName').show();
		
		$rForm = $('form#regForm').slideUp('slow');
		
		$.post($rForm.attr('action'), $rForm.serialize(), function(data){
			$throbber.hide('fast', function(){
				$(this).remove();
			});
			
			if(data == 'OK'){
				if($isUpdate){
					displayMsg("Successfully updated your kard");
					showSearch();
					$steps.slideDown();
					$ftKards.slideDown();
					scrollToElement($('#fz-biz'));
					
					setTimeout(hideSuccessfulUpdateUI, 8000);
				}else{
					displayMsg("You have been successfully registered in our database. "+
					"You will receive an activation SMS on your phone shortly, which you'd use to "+
					"activate your account before you can fully utilize our platform. <p>To activate, "+
					" click the <i>Activate</i> button on the Right hand corner of the site. In case you don't get an SMS, you can contact us"+
					" via the feedback link at the bottom of this page. Thank you.</p>");
					//setTimeout(showActivationForm, 10000);
				}
				
				
			}else if (data == 'ERROR1') {
				if(!$isUpdate){
					displayError("An error occurred while registering you on our server. Please try again later, and if problem " +
				"persists, do not hesitate to contact us.");
				}else{
					displayError("An error occurred while update your kard on our server. Please try again later, and if problem " +
				"persists, do not hesitate to contact us.");
				}
				
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR2') {
				if(!$isUpdate){
					displayError("Unable to register your kard on our server because it has been previously registered.");
				}else{
					displayError("Unable to update your kard on our server because it has been previously registered.");
				}
				
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR3') {
				displayError("Unable to activate your kard on our server because it has been previously activated.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR4') {
				displayError("Unable to activate your kard on our server because it is invalid.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR5') {
				displayError("You need to specify your business name.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR6') {
				displayError("You need to specify your mobile number.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR7') {
				displayError("You need to specify your email address.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR8') {
				displayError("You need to specify your mailing address.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR9') {
				displayError("You need to specify your state where your business is operating from.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR10') {
				displayError("You need to specify the categories that describe your business.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR11') {
				displayError("Each of your Service tags should contain at most 2 words (e.g. event management) and each tag must not be longer than 30 characters. Also avoid words such as AND, OR, WILL etc. Be precise in your service tags description.");
				setTimeout(showRegForm, 500);
			}else if (data == 'ERROR12') {
				displayError("Activation Error at service tags.");
				setTimeout(showRegForm, 500);
			}else {
				displayError("An error occurred while registering you on our server. It is either one of the fields you filled " +
				"is incorrect or you have been previously registered before. Please try again later, and if problem " +
				"persists, do not hesitate to contact us.");
				setTimeout(showRegForm, 500);
			}
		});
			
		return false;
	});
	
	$('#actForm').submit(function(){
		$actField = $('#c2');
		$actField.css('disabled', 'disabled');
		$actBtn = $('#actCodeBtn');
		$cancelBtn = $('#actCancelBtn');
		$('div.regMsg').remove();
		
		$cancelBtn.css('visibility', 'hidden');
		$actBtn.css('disabled', 'disabled').val('Activating...');
		
		$actForm = $(this);
		$.post($actForm.attr('action'), $actForm.serialize(), function(data){
			if(data == 'OK'){
				$isError = false;
				$msg = "Successfully activated your account. Thanks for registering.";
				$cls = 'ok';
			}else if(data == 'ERROR3'){
				$isError = true;
				$msg = "An error occurred in activating your account because you've been previously activated.";
				$cls = 'err';
			}else if(data == 'ERROR4'){
				$isError = true;
				$msg = "An error occurred in activating your account because the activation id specified is invalid.";
				$cls = 'err';
			}
			
			injectStr = "<div class='regMsg'><div class='"+$cls+"'>"+$msg+"</div></div>";
			$('div.regMsg').remove();
			$(injectStr).insertBefore($actForm).show('fast');
		
			$actField.css('disabled', '');
			$actBtn.css('disabled', '').val('Ok');
			$cancelBtn.css('visibility', '');
			
			if(!$isError){//if it is successful, hide the activation form and show the search box after some secs
				setTimeout(actTransition, 6000);
			}
		});
		
		return false;
	});
	
	$('#actCancelBtn').click(function(){
		$('#actBtn').attr('value', 'Activate');
		showBaseUI();
	});
	
	$('#rcBtn').click(function(){
		$('#createBtn').attr('value', 'Join');
		showBaseUI();
	});
	
	$('#bizForm').submit(function(){
		$data = $('#cat').val();
		if($data){
			if($data.indexOf(",") > -1){
				searchSource = "new-search";
				searchTerm = $data;
				search(0);
			}else{
				queueBubble($('#cat'), $data);
			}
		}

		return false;
	});
	
	$('#searchBtn').click(function(){
		searchSource = 'search';
		
		search(0);
		
		return false;
	});
	
	$('div.pg a').live('click', function(){
		search($(this).attr('href'));
		
		return false;
	});
	
	$('a.fcat').click(function(){
		searchSource = 'ftags';
		
		$this = $(this);
		$loader = $("<div class='tn'><img src='images/tiny-loader.gif'/></div>").insertAfter($this);
		//get the actual search data within the clicked link
		$data = $(this).find('span').text();
		searchTerm = $data;
		//clear the array and add the data obtained from the link to it and also to the display queue
		//$searchList = [$data];//the data in the clicked link should be the only data in the array
		search(0);
		$loader.hide('fast').remove();
		
		//$searchList = [];//clear the array in preparation for any other operation on it. This is to safeguard having the old queries in the array as well
		return false;
	});
	
	//show the facebox when the mail button is clicked
	$('div.s_mail').live('click', function(){
		$this = $(this);
		$hdr = $this.attr('title');
		$id = $this.parent().attr('id');
		$rec_id = $id.substring(4);//the id of the recipient
		
		$.facebox("<div id-'emBox' class='eBox'><div class='eheader'>"+$hdr+"</div><div class='popupThrobber'>Sending Mail...</div><form id='emailForm' action='mediator.php?sc=MSG'><input id='nt' name='nt' value='n1' type='hidden' /><input id='nk' name='nk' value='"+$rec_id+"' type='hidden' /><div class='dm'><b>Disclaimer:</b> <i>MyBizKards.com</i> and <i>DabarObjects Solutions</i> has no relationship, knowledge or link to the above business or individual and your communication with them. You hereby agree to solely bear any risk that may be involved in dealing with the above individual or business</div><h3>Your Name:</h3><input id='sb_name' name='sb_name' type='text' size='52' /><h3>Your Email Address:</h3><input id='nid' name='nid' type='text' size='52' /><h3>Service required:</h3><div class='mlen'>140</div><textarea id='n4' name='n4' rows='3' cols='40'></textarea><br/><div class='right'><input id='eBtn1' name='eBtn1' type='button' value='Send'></div></form></div>");
		$curFaceBox = '#emBox';
		
		return false;
	});
	
	//show the facebox when the mail button is clicked
	$('div.s_mobile').live('click', function(){
		$this = $(this);
		$hdr = $this.attr('title');
		$id = $this.parent().attr('id');
		$rec_id = $id.substring(4);//the id of the recipient
		
		$.facebox("<div id='smsBox' class='eBox'><div class='eheader'>"+$hdr+"</div><div class='popupThrobber'>Sending SMS...</div><form id='smsForm' action='mediator.php?sc=MSG'><input id='nt' name='nt' value='n2' type='hidden' /><input id='nk' name='nk' value='"+$rec_id+"' type='hidden' /><div class='dm'><b>Disclaimer:</b> <i>MyBizKards.com</i> and <i>DabarObjects Solutions</i> has no relationship, knowledge or link to the above business or individual and your communication with them. You hereby agree to solely bear any risk that may be involved in dealing with the above individual or business</div><h3>Your Name:</h3><input id='sb_name' name='sb_name' type='text' size='52' /><h3>Your Mobile Number:</h3><span class='small'>(Format: 080xxxxxxxx or 070xxxxxxxx)</span><input id='nid' name='nid' type='text' size='52' /><h3>Service required:</h3><div class='mlen'>44</div><textarea id='n4' name='n4' rows='3' cols='40'>I found your information through http://www.mybizkards.com and I'd require your services ASAP.</textarea><br/><div class='right'><input id='eBtn2' name='eBtn2' type='button' value='Send'></div></form></div>");
		$curFaceBox = '#smsBox';
		
		return false;
	});
	
	$(document).bind('close.facebox', function(){//bind a handler to the close event of facebox
		//remove the contents of the closed facebox in preparation for the next call to it
		$($curFaceBox).remove();
	});
	
	var removeFaceBox = function(id){//where id is for the facebox to remove(email or sms facebox)
		jQuery(document).trigger('close.facebox');
	}
	
	$('#eBtn1').live('click', function(){
		$name = $('#sb_name').val();
		$email = $('#nid').val();
		$msg = $('#n4').val();
		$form = $('#emailForm');
		$throbber = $form.parent().find('div.popupThrobber');
		
		//check if the name isn't empty
		if(getTrimmedString($name) == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>You need to specify your name.</div></div>").insertBefore($form).show();
			return false;
		}
		
		if (!isEmailValid($email)) {
			$('div.regMsg').remove();//remove any previous instance, if any
			
			$("<div class='regMsg'><div class='err'>The email address specified is invalid.</div></div>").insertBefore($form).show();
			return false;
		}

		//the email address is valid, so check to see if the message box isn't empty
		if($msg == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>You need to fill in the message you want to send.</div></div>").insertBefore($form).show();
			return false;
		}
		
		if($msg.length > 140){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>The message should be less than 140 characters</div></div>").insertBefore($form).show();
			return false;
		}
		
		$throbber.show();
		$form.slideUp();
		$('div.regMsg').remove();//remove any previous instance, if any

		//ok to proceed with message sending
		$.post($form.attr('action'), $form.serialize(), function(data){
			if(data == 'OK'){
				$("<div class='regMsg'>Successfully sent your message</div>").insertBefore($form).show();
				$throbber.hide();
				setTimeout(removeFaceBox, 5000);
			}else if(data == 'ERROR'){
				$("<div class='regMsg'><div class='err'>An error occurred in sending your message. Please check your email address to see if it's correct and then try again</div></div>").insertBefore($form).show();
				$throbber.hide();
				$form.slideDown();
			}else if(data == 'ERROR13'){
				$("<div class='regMsg'><div class='err'>Your message could not be delivered, please try again later</div></div>").insertBefore($form).show();
				$throbber.hide();
				$form.slideDown();
			}
		});
		
		return false;
	});

	//characters-typed numbering
	$('#n4').live('keypress', function(){
		$max = 140;
		$this = $(this);
		$len = $(this).val().length;

		$size = $max - $len - 1///remove 1 because this happens after this method is exited;
		$('div.mlen').html($size);	
	});
	
	$('#eBtn2').live('click', function(){
		$name = $('#sb_name').val();
		$mobile = $('#nid').val();
		$msg = $('#n4').val();
		$form = $('#smsForm');
		$throbber = $form.parent().find('div.popupThrobber');
		
		//check if the name isn't empty
		if(getTrimmedString($name) == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>You need to specify your name.</div></div>").insertBefore($form).show();
			return false;
		}
		
		if (!$mobile.match(/0{1}[7-8]0{1}[0-9]{8}/)) {
			$('div.regMsg').remove();//remove any previous instance, if any
			
			$("<div class='regMsg'><div class='err'>The mobile number specified is invalid. It should be of the format (080xxxxxxxx or 070xxxxxxxx)</div></div>").insertBefore($form).show();
			return false;
		}

		//the mobile no is valid, so check to see if the message box isn't empty
		if($msg == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>You need to fill in the message you want to send.</div></div>").insertBefore($form).show();
			return false;
		}
		
		if($msg.length > 140){
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>The message should be less than 140 characters</div></div>").insertBefore($form).show();
			return false;
		}
		
		$throbber.show();
		$form.slideUp();
		$('div.regMsg').remove();//remove any previous instance, if any
		
		//ok to proceed with message sending
		$.post($form.attr('action'), $form.serialize(), function(data){
			if(data == 'OK'){
				$("<div class='regMsg'>Successfully sent your message</div>").insertBefore($form).show();
				$throbber.hide();
				setTimeout(removeFaceBox, 5000);
			}else if(data == 'ERROR'){
				$("<div class='regMsg'><div class='err'>An error occurred in sending your message. Please check your mobile number to see if it's correct and then try again</div></div>").insertBefore($form).show();
				$throbber.hide();
				$form.slideDown();
			}else if(data == 'ERROR13'){
				$("<div class='regMsg'><div class='err'>Your message could not be delivered, please try again later</div></div>").insertBefore($form).show();
				$throbber.hide();
				$form.slideDown();
			}
		});
		
		return false;
	});
	
	$('#eBtn3').live('click', function(){
		$name = $('#fdname').val();
		$acct = $('#fdacct').val();
		$email = $('#fdemail').val();
		$mobile = $('#fdmobile').val();
		$msg = $('#fdmsg').val();
		$form = $('#fdbForm');
		
		if(getTrimmedString($name) == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			
			$("<div class='regMsg'><div class='err'>You need to specify your name.</div></div>").insertBefore($form).show();
			return false;
		}
		
		if(getTrimmedString($mobile) == '' && getTrimmedString($email) == ''){
			$('div.regMsg').remove();//remove any previous instance, if any
			
			$("<div class='regMsg'><div class='err'>You need to specify either your email address or mobile number.</div></div>").insertBefore($form).show();
			return false;
		}
		
		//if the email is specified, check to see if it's valid
		if(getTrimmedString($email) != ''){
			if (!isEmailValid($email)) {
				$('div.regMsg').remove();//remove any previous instance, if any
				
				$("<div class='regMsg'><div class='err'>The email address specified is invalid.</div></div>").insertBefore($form).show();
				return false;
			}
		}
		
		if(getTrimmedString($mobile) != ''){
			if (!$mobile.match(/0{1}[7-8]0{1}[0-9]{8}/)) {
				$('div.regMsg').remove();//remove any previous instance, if any
				
				$("<div class='regMsg'><div class='err'>The mobile number specified is invalid. It should be of the format (080xxxxxxxx or 070xxxxxxxx)</div></div>").insertBefore($form).show();
				return false;
			}
		}

		//the email address is valid, so check to see if the message box isn't empty
		if($msg == ''){
			//find out if this form is for feedback or for report abuse, since they contain the same fields
			$val = $form.find('#abuse').val();
			if($val)
				$m = "You need to describe the abuse.";
			else
				$m = "You need to specify the service required.";
			$('div.regMsg').remove();//remove any previous instance, if any
			$("<div class='regMsg'><div class='err'>"+$m+"</div></div>").insertBefore($form).show();
			return false;
		}

		$form.slideUp('fast');
		$('div.regMsg').remove();//remove any previous instance, if any
		$("<div class='regMsg'>Successfully sent your message</div>").insertBefore($form).show('fast');
		setTimeout(removeFaceBox, 2000);
		
		//ok to proceed with message sending
		$.post($form.attr('action'), $form.serialize());
	});
	
	//show the feedback popup when the feedback link is clicked
	$('#fdb').click(function(){
		$.facebox("<div id-'fdbBox' class='eBox'><div class='eheader'>Please provide a feedback</div><form id='fdbForm' action='feedback.php'><h3>Name:</h3><input id='fdname' name='fdname' type='text' size='52' />"+
				"<h3>Account code (if applicable):</h3><input id='fdacct' name='fdacct' type='text' size='52' /><h3>Mobile:</h3><input id='fdmobile' name='fdmobile' type='text' size='52' />"+
				"<h3>Email Address:</h3><input id='fdemail' name='fdemail' type='text' size='52' /><h3>Service Required:</h3><textarea id='fdmsg' name='fdmsg' rows='3' cols='40'></textarea><br/><div class='right'><input id='eBtn3' name='eBtn3' type='button' value='Send'></div></form></div>");
		$curFaceBox = '#fdbBox';
		
		return false;
	});
	
	$('#rab').click(function(){
		$.facebox("<div id-'fdbBox' class='eBox'><div class='eheader'>Report Abuse</div><form id='fdbForm' action='feedback.php'><input id='abuse' name='abuse' type='hidden' value='1' /><h3>Name:</h3><input id='fdname' name='fdname' type='text' size='52' />"+
				"<!--h3>Account code:</h3><input id='fdacct' name='fdacct' type='text' size='52' /--><h3>Mobile:</h3><input id='fdmobile' name='fdmobile' type='text' size='52' />"+
				"<h3>Email Address:</h3><input id='fdemail' name='fdemail' type='text' size='52' /><h3>Description of Abuse:</h3><textarea id='fdmsg' name='fdmsg' rows='3' cols='40'></textarea><br/><div class='right'><input id='eBtn3' name='eBtn3' type='button' value='Report'></div></form></div>");
		$curFaceBox = '#fdbBox';
		
		return false;
	});
	
	$('#ubk').click(function(){
		$.facebox("<div id-'ubkBox' class='eBox'><div class='eheader'>Update Business Kard?</div><div class='popupThrobber'>Fetching Kard...</div><form id='ubkForm' action='mediator.php?sc=UT'><input id='t2' name='t2' type='hidden' value='UT' />"+
				"<h3>Account Code:</h3><input id='c2' name='c2' type='text' size='52' />"+
				"<br/><div class='right'><input id='eBtn4' name='eBtn4' type='button' value='Send'></div></form></div>");
		$curFaceBox = '#ubkBox';
		
		return false;
	});
	
	$('#eBtn4').live('click', function(){
		$form = $('form#ubkForm');
		$acct_code = $form.find('#c2').val();
		
		$('div.regMsg').remove();//remove any previous instance, if any
		//check to see if the account code isn't empty
		if(getTrimmedString($acct_code) == ''){
			$("<div class='regMsg'><div class='err'>You need to specify your account code</div></div>").insertBefore($form).show('fast');
			return;
		}

		$throbber = $form.siblings('div.popupThrobber');
		
		$throbber.show('fast');
		$form.hide('fast');
		
		$.post($form.attr('action'), $form.serialize(), function(data){
			//alert(data);
			$throbber.hide('fast');
			
			//get the name, mobile, email, address, website and state from the data
			//and check to see if it's a valid data or not
			$data = $(data);
			$name = $data.find('name').text();
			
			if($name == 'Not Available'){
				$errMsg = $name = $data.find('error').text();
				$("<div class='regMsg'><div class='err'>"+$errMsg+"</div></div>").insertBefore($form).show('fast');
				
				setTimeout(reshowActivation, 6000);
			}else{
				$mobile = $data.find('mobile').text();
				$email = $data.find('email').text();
				$addr = $data.find('address').text();
				$www = $data.find('website').text();
				$state = $data.find('state').text();
				$tags = $data.find('tags').text();
				
				//update the fields
				$regForm.find('#c1').val($name);
				$regForm.find('#a4').val($mobile);
				$regForm.find('#e1').val($email);
				$regForm.find('#a6').val($www);
				$regForm.find('#a3').val($addr);
				$regForm.find('#g1').val($state);
				$regForm.find('#v1').val($tags);
				
				$regForm.find('#c1').prepend("<input type='hidden' id='c2' name='c2' value='"+$acct_code+"' />");
				
				//hide the facebox and show the regform
				jQuery(document).trigger('close.facebox');
				hideSearch();
				$steps.slideUp();
				$actBox.slideUp();
				$ftKards.slideUp();
				$regForm.slideDown().find('#regForm').show();
				
				$isUpdate = true;//the current action is an update
			}
		});
	});
	
	var hideSuccessfulUpdateUI = function(){
		clearRegFields();
		//$('div.regMsg').hide('fast').remove();
		//$().slideDown();
	}
	
	//clear the registration fields
	var clearRegFields = function(){
		$regForm.find('#c1').val('').end().find('#a4').val('').end().find('#e1').val('').end().find('#a6').val('').end().find('#a3').val('').end().find('#g1').val('').end().find('#v1').val('').end().remove('#c2');
	}
	
	var reshowActivation = function(){
		$('div.regMsg').remove();
		$('form#ubkForm').show('fast');
	}
	
	$('#fac').click(function(){
		$.facebox("<div id-'facBox' class='eBox'><div class='eheader'>Forgot Account Code?</div><div class='popupThrobber'>Retrieving Account code...</div><form id='facForm' action='mediator.php?sc=RM'><input id='t2' name='t2' type='hidden' value='RM' />"+
				"<h3>Mobile:</h3><input id='a4' name='a4' type='text' size='52' />"+
				"<br/><div class='right'><input id='eBtn5' name='eBtn5' type='button' value='Send'></div></form></div>");
		$curFaceBox = '#facBox';
		
		return false;
	});
	
	$('#eBtn5').live('click', function(){
		$form = $('form#facForm');
		$mobile = $form.find('#a4').val();
		$('div.regMsg').remove();//remove any previous instance, if any
		
		//validate the phone number
		if (!getTrimmedString($mobile).match(/0{1}[7-8]0{1}[0-9]{8}/)) {
			$("<div class='regMsg'><div class='err'>The mobile number specified is invalid. It should be of the format (080xxxxxxxx or 070xxxxxxxx)</div></div>").insertBefore($form).show();
			return false;
		}
		
		$throbber = $form.siblings('div.popupThrobber');
		
		$throbber.show('fast');
		$form.hide('fast');
		
		$.post($form.attr('action'), $form.serialize(), function(data){
			$throbber.hide('fast');
			$('div.regMsg').remove();
			
			if (data == 'OK') {
				$("<div class='regMsg'><div class='ok'>Your account code has been resent to your mobile</div></div>").insertBefore($form).show('fast');
				
				setTimeout(hideForgotCodeUI, 6000);
			}
			else {
				$("<div class='regMsg'><div class='err'>Your mobile number was not found in our database. Please try again</div></div>").insertBefore($form).show('fast');
				$form.show();
			}
		});
	});
	
	var hideForgotCodeUI = function(){
		jQuery(document).trigger('close.facebox');
		/*hideSearch();
		$steps.slideUp();
		$ftKards.slideUp();
		$actBox.slideDown();*/
	}
	
	//go back to the base state
	var showBaseUI = function(){
		$regForm.hide('fast');
		$actBox.hide('fast');
		showSearch();
		$steps.slideDown();
		$ftKards.slideDown();
	}
	
	
	var getTrimmedString = function(str){
		return str.replace( /^\s+/g,'').replace(/\s+$/g,'');
	}
	
	var isEmailValid = function(s){
		return  s.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	
	var isPhoneNoValid = function(no){
		return no.match(/\d{11}/);
	}
	
	var displayError = function(err){
		injectStr = "<div class='regMsg'><div class='err'>"+err+"</div></div>";
		$('div.regMsg').remove();
		$(injectStr).insertAfter('div#regName').show('fast', function(){
			scrollToElement($('div.regMsg'));
		});
	}
	
	var displayMsg = function(msg){
		injectStr = "<div class='regMsg'><div class='ok'>"+msg+"</div></div>";
		$('div.regMsg').remove();
		$(injectStr).insertAfter('div#regName').show();
	}
	
	var addItem = function(s){
		$searchList.push(s);
	}
	
	var removeItem = function(s){
		ind = -1;//the index of the string to be searched
		
		//first look thru the array for the item, then store it's index if found
		for(i = 0; i < $searchList.length; i++){
			if($searchList[i] == s){
				ind = i;
				break;
			}
		}
		
		if(ind >= 0){
			$deleted = $searchList.splice(ind, 1);
		}
	}
	
	var isEmpty = function(){
		return $searchList.length == 0;
	}
	
	var getCategories = function(){
		$result = '';
		
		for(i=0; i<$searchList.length;i++){
			$result += $searchList[i]+",";
		}
		
		return $result;
	}
	
	var showActivationForm = function(){
		//$('div.regMsg').hide('fast').remove();
		hideSearch();
		$steps.slideUp();
		$regForm.slideUp();
		$actBox.slideDown();
	}
	
	var hideActivationForm = function(){
		$actBox.slideUp();
		showSearch();
		$steps.slideDown();
	}
	
	var showRegForm = function(){
		hideSearch();
		$steps.slideUp();
		$actBox.slideUp();
		$('form#regForm').slideDown();
	}
	
	//the transition that occurs after a successful activation
	var actTransition = function(){
		$actBox.slideUp();
		showSearch();
	}
	
	var getKardHTML = function($name, $addr, $id, $st, $crdt, $web){
		$response = "<li><div class='bizkard'><div class='bzcontent'>";
		if($web != '#')
			$response = $response += "<div class='bzname'><a title='Click here to go to "+$web+"' href='"+$web+"'>"+$name+"</a></div>";
		else
			$response = $response += "<div class='bzname'>"+$name+"</div>";
		
		$response += "<div class='bzaddr'>"+$addr+" "+$st+"</div></div><div id='acp_"+$id+"' class='acp'><div class='s_mail' title='Send a mail to "+$name+"'></div>";
		
		if($crdt == 'true')
			$response += "<div class='s_mobile' title='Send an SMS to "+$name+"'></div>";
		
		$response += "</div><div class='clearfix'></div></div></li>";
		
		return $response;
				
	}
	
	var getPagingInfo = function(data){
		$total_pages = $(data).find('biz-tags-pages > paging-info > total-pages');
		
		$(data).find('biz-tags-pages > paging-info').each(function(){
			$page_info = $(this);
			$page_no = $page_info.find('page-no').text();
			$page_start_index = $page_info.find('page-start-index').text();
			$av_view_size = $page_info.find('page-available-view-size').text();
		});
	}
	
	//queues up the search term in the search queue
	var queueBubble = function($el, item){
		$('#sHolder').prepend('<div class="sRes"><span>'+item+'</span><span class="close_sRes"><a title="Delete '+item+'" href="#">x</a></span></div>');
		addItem(item);
		$('#sList').show('fast');
		
		if($el){
			$el.val('');
		}
	}
	
	//scroll to the specified element
	var scrollToElement = function($el){
		$.scrollTo($el, 200, {easing: 'easeInQuad'});
	}
	
	var getSelection = function() {
		return (!!document.getSelection) ? document.getSelection() : (!!window.getSelection) ? window.getSelection() :document.selection.createRange().text;
	}
	
	var search = function($paging_url){
		if(searchSource == 'search')
			$cat = getCategories();
		else
			$cat = searchTerm;
		
		if($paging_url === 0)
			$url = "mediator.php?sc=KARDS&q="+$cat;
		else
			$url = $paging_url;//the url to connect to in case the request was initiated from the paging links
		
		//clear the previous search if any
		$('div.regThrobber').remove();//remove any existing throbbers
		$throbber = $("<div class='regThrobber'>Looking up kards...</div>").insertAfter('div#actBox').show();
		
		$.post($url, function(data){
			//console.log(data);
			//alert(data);
			if(searchSource == 'search')
				$searchParam = $cat.substr(0, $cat.length - 1);//remove the trailing comma
			else
				$searchParam = $cat;
			
			$noResults = false;//was No results obtained?

			$kards = "<div id='bzk-container'><div id='bzk-title'>Search Results - "+$searchParam+
					"</div><div id='bzk-info'>Your search query returned the following results, "+
					"roll over any of the listed businesses for more options:</div><ul id='bizkards'>";
			$count = 0;
			$throbber.hide().remove();
			$('div#bzk-container').hide().remove();

			//if no data is obtained
			/*if(data == ''){
				alert('An error occurred while connecting to the server');
			}*/
			
			//alert(data);
			$(data).find('card').each(function(ind){
				$count++;
				var $card = $(this);
				$id = $card.find('id').text();
				
				if($id == 0)
					$noResults = true;
				if(!$noResults){
					$bizname = $card.find('name').text();//alert($bizname);
					$addr = $card.find('address').text();
					
					
					$state = $card.find('state').text();
					$hasCredit = $card.find('has-credit').text();
					$www = $card.find('website').text();
	
					$kards += getKardHTML($bizname, $addr, $id, $state, $hasCredit, $www);
				}
			});
			
			if($noResults){
				alert('There were no results matching your query');
			}else{
				$kards += "</ul><div class='clearfix'></div><div id='pagination'>";
				
				$(data).find('paging-info').each(function(ind){
					var $card = $(this);
					$pn = $card.find('page-no').text();//alert($bizname);
					$psi = $card.find('page-start-index').text();
					$pa = $card.find('page-available-view-size').text();
					
					$kards += "<div class='pg'><a href='mediator.php?sc=PKARDS&q="+$cat+"&pn="+$pn+"&psi="+$psi+"&pa="+$pa+"'>"+$pn+"</a></div>";
				});
				
				$kards += "</div></div>";
	
				if($count > 0){
					hideSearch();
					
					$catBox.after($kards);
					$steps.slideUp();
					$ftKards.slideUp();

					if(searchSource == 'ftags')
						$.scrollTo($('#bzk-container'), 400, {easing: 'easeOutElastic'});
				}
			}
		});
	}
});