
$(function() {
	//ajax example
	//$.post('?test', {email:'email',user:'user'}, function(response) {
	//	alert(response);
	//});
	
	//ajax example
	//$.get('?get_featured_rant', function(response) {
		//alert(response);
	//});
	
	//each example
	//$("#frm_manage_users input[@type=checkbox]").each(function(){
		//alert($(this).val());
	//});

	
	
	$('#modal_login').click(Login);
	
	$('#modal_login_twitter').click(LoginTwitter);
		
	$('#modal_login_toolbar').click(LoginToolbar);
	
	$('#create_account').click(CreateAccount);
	
	$('#recover_password').click(RecoverPassword);
			
	$('div#search li').click(SearchActiveTab);
	
	$('a#reload_captcha').click(ReloadCaptcha);
	
	$('.add_email_field').click(AddEmailField);
	
	$('a.remove_email').click(RemoveEmail);
	
	$('a.remove_website').click(RemoveWebsite);
	
	$('a.remove_company').click(RemoveCompany);
	
	$('a.remove_im').click(RemoveIm);
	
	//import delicious bookmarks
	$('input#import_delicious').click(ImportDeliciousBookmarks);
	
	//import google bookmarks
	$('input#import_google').click(ImportGoogleBookmarks);
	
	//import stumbleupon bookmarks	
	$('input#import_stumbleupon').click(ImportStumbleUponBookmarks);
	
	//import facebook posted items (bookmark)	
	$('input#import_facebook').click(ImportFacebookBookmarks);
	
	//show browser upload bookmarks
	$('a.browser_bookmarks').click(ShowBrowserUploadBookmarks);
	
	//import hi5 profile
	$('input#import_hi5_profile').click(Import_Hi5_Profile);
	
	//import bebo profile
	$('input#import_bebo_profile').click(Import_Bebo_Profile);
	
	//import orkut profile
	$('input#import_orkut_profile').click(Import_Orkut_Profile);
	
	//import friendster profile
	$('input#import_friendster_profile').click(Import_Friendster_Profile);
	
	//import myspace profile
	$('input#import_myspace_profile').click(Import_MySpace_Profile);
	$('input#import_myspace_profile_account').click(Import_MySpace_Profile);
	
	//import facebook profile
	$('input#import_facebook_profile').click(Import_Facebook_Profile);
	$('input#import_facebook_profile_account').click(Import_Facebook_Profile);
	
	//import twitter profile
	$('input#import_twitter_profile').click(Import_Twitter_Profile);
	

	//submit top search form based on drop down tab selection
	$('a.submit_search_form').click(SubmitSearchForm);
	
	//function for saving in a js variable the key of the result for which bookmark is wanted; see mw-bookmark.js;
	$('a.bookmark_result').click(BookmarkResult);	
	//the same as above for email to friend
	$('a.email_result').click(BookmarkResult);	
	
	//add bookmark; called from DHTML layer; submit button
	$('input#add_bookmark').click(AddBookmark);	
	$('input#add_bookmark_profile').click(AddBookmark);	
	
	//add bookmark; called from window generated by toolbar; submit button
	$('input#add_bookmark_toolbar').click(AddBookmarkToolbar);	
	
	//email to friend; called from DHTML layer; submit button
	$('input#email_to_friend').click(EmailToFriend);
	
	$('a.vote_result').click(VoteResult);
	
	//change items per page for pagination; ajax call -> store in session	
	$('a.change_items_per_page').click(ChangeItemsPerPage);
	
	$('a.changeUserFavorite').click(changeUserFavorite);	
	
	$('a.change_popular_filter').click(ChangePopularFilter);
	
	$('input#claim_websites').click(ClaimWebsites);	
	
	$('input#invite_facebook_friends').click(InviteFacebookFriends);
	
	$('a.copy_to_clipboard').click(CopyToClipboard);
	
	$('input#generate_code').click(GenerateSearchCode);
	
	//sets in a variable the option number of the widget that needs to be previewed
	$('a.preview_widget').click(PreviewWidget);
	
	$('#change_paypal_email').click(ChangePayPalEmail);
	
	$('#change_mailing_address').click(ChangeMailingAddress);
	
	$('#change_ssn_number').click(ChangeSSN);
	
	$('#unclaim').click(Unclaim);
	
	$('#cancel_pending').click(CancelPending);

})

function CancelPending(){
	var claim_ids = new Array();
	$("input.pending").each(function(){
		if($(this).attr("checked") == true)
			claim_ids.push($(this).val());
	});
	
	claim_ids = claim_ids.join(",");
	
	if(claim_ids == '' || claim_ids == null){
		alert("Please select at least one domain!");
		return false;
	}
	
	if(!confirm("Are you sure you want to cancel the claiming process of these domains?"))
		return false;
		
	$.post(HTTP + '/ajax?unclaim', {ids:claim_ids}, function(response) {
		//response will return the ids of the domains that have been deleted; ids will be used to remove html elements from page;
		update = response.split("|||");
		if(update[0] == 1 && update[1] != '' && update[1] != null){
			claim_ids = update[1].split(",");
			for(i = 0; i < claim_ids.length; i++)
				$("#p_row_" + claim_ids[i]).remove();		
		}
	});	
}

function Unclaim(){
	var claim_ids = new Array();
	$("input.completed").each(function(){
		if($(this).attr("checked") == true)
			claim_ids.push($(this).val());
	});
	
	claim_ids = claim_ids.join(",");
	
	if(claim_ids == '' || claim_ids == null){
		alert("Please select at least one domain!");
		return false;
	}
	
	if(!confirm("Are you sure you want to unclaim these domains?"))
		return false;
		
	$.post(HTTP + '/ajax?unclaim', {ids:claim_ids}, function(response) {
		//response will return the ids of the domains that have been deleted; ids will be used to remove html elements from page;
		update = response.split("|||");
		if(update[0] == 1 && update[1] != '' && update[1] != null){
			claim_ids = update[1].split(",");
			for(i = 0; i < claim_ids.length; i++)
				$("#c_row_" + claim_ids[i]).remove();		
		}
	});	
}

function ChangeMailingAddress(){
	//empty error messages
	$("p.error_message").each(function(){
		$(this).text('');
	});
	
	$("p#change_address_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
			
	address = $("input[@name=address]").val();
	address2 = $("input[@name=address2]").val();
	city = $("input[@name=city]").val();
	state = $("input[@name=state]").val();
	zip = $("input[@name=zip]").val();
	country = $("select[@name=country]").val();
	
	if(address == '' || address == null || city == '' || city == null || state == '' || state == null || zip == '' || zip == null || country == '' || country == null){
		$("p#change_address_message").html('<span class="modal_login_failed">Incomplete fields!</span>');
		return false;
	}
		
	$.post(HTTP + 'ajax/?change_mailing_address', {address:address,address2:address2,city:city,state:state,zip:zip,country:country}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p#change_address_message").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			$("p#change_address_message").html('<span class="confirmMessage">' + update[1] + '</span>');
						
			$("span#address_container").text(city + ', ' + state);
		}
	});	
}

function ChangeSSN(){
	//empty error messages
	$("p.error_message").each(function(){
		$(this).text('');
	});
	
	$("p#change_ssn_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
	
	ssn = $("input[@name=ssn]").val();
	
	if(ssn == '' || ssn == null){
		$("p#change_ssn_message").html('<span class="modal_login_failed">Insert your Social Security Number or Employer Identification Number!</span>');
		return false;
	}
	
	$.post(HTTP + 'ajax/?change_ssn', {ssn:ssn}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p#change_ssn_message").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			$("p#change_ssn_message").html('<span class="confirmMessage">' + update[1] + '</span>');
			$("span#ssn_container").text(ssn);
		}
	});
	
}

function ChangePayPalEmail(){
	//empty error messages
	$("p.error_message").each(function(){
		$(this).text('');
	});
	
	$("p#change_email_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
	
	paypal_email = $("input[@name=paypal_email]").val();
	
	if(paypal_email == '' || paypal_email == null){
		$("p#change_email_message").html('<span class="modal_login_failed">Insert PayPal Email!</span>');
		return false;
	}
		
	$.post(HTTP + 'ajax/?change_paypal_email', {paypal_email:paypal_email}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p#change_email_message").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			$("p#change_email_message").html('<span class="confirmMessage">' + update[1] + '</span>');
			$("span#paypal_email_container").text(paypal_email);
		}
	});
}

var preview_widget = 0;

function PreviewWidget(){
	widget_id = $(this).attr("id").substr(8);
	
	widget_code = $("textarea[@name=option" + widget_id + "]").val();
	
	if(widget_code != '')
		preview_widget = widget_id;
}

function GenerateSearchCode(){
	url = $("input[@name=url]").val();
	
	if(url == '' || url == null || url.substr(0,7).toUpperCase() != 'HTTP://'){
		alert('Please enter a valid URL address!');
		return false;
	}
	
	$.post(HTTP + 'ajax/?generate_search_code', {url:url}, function(response) {
		update = response.split("|||");
		if(update[0] == 0){
			alert(update[1]);
			return false;
		}
		else{
			//put code in textareas
			$("textarea[@name=option1]").val(update[1]);
			$("textarea[@name=option2]").val(update[2]);
			
			//start copy to clipboard script; load trigger events
			var clip = new ZeroClipboard.Client();
		    clip.setText(update[1]);
			clip.glue('copy_1');
			
			var clip2 = new ZeroClipboard.Client();
		    clip2.setText(update[2]);
			clip2.glue('copy_2');
		}
	});
		
}

function CopyToClipboard(){
	textarea_name = 'option' + $(this).attr("id").substr(5);
	
	text_value = $("textarea[@name=" + textarea_name + "]").val();
	
	if(text_value == '' || text_value == null){
		alert("Code has not been generated!");
		return false;
	}
	
	alert("Code has been copied to clipboard!");
}

function InviteFacebookFriends(){
	//each example
	
	var facebook_ids = new Array();
	
	$("input[@type=checkbox]").each(function(){
		if($(this).attr("name") == 'fb_users[]' && $(this).attr("checked") == true)
			facebook_ids.push($(this).attr("value"));
	});
	
	facebook_ids = facebook_ids.join(",");
	if (facebook_ids == '' || facebook_ids == null) {
		alert("Please select at least one Facebook user!");
		return false;
	}
	else {
		//ajax example
		$.post(HTTP + 'ajax/?invite_facebook_friends', {ids:facebook_ids}, function(response) {
			update = response.split("|||");
			if(update[0] == 1 && update[1] != 0){
				//confirmation message
				//remove previous
				$("#facebook_error").remove();
				$("#facebook_friends_container").after('<div id="facebook_error" class="confirmMessage">Invitations have been sent!</div>');
				
				facebook_ids = update[1].split(",");
				for(i = 0; i < facebook_ids.length; i++)
					$("div#fb_" + facebook_ids[i]).remove();//remove friend item
					
				//test if there are any items left; if not remove entire container
				var remaining = 0;
				
				$("input[@type=checkbox]").each(function(){
					if($(this).attr("name") == 'fb_users[]')
						remaining = 1;
				});
				
				if(remaining == 0)
					$("div#facebook_friends_container").remove();
					
			}
			
		});		
	}		
}

function ClaimWebsites(){
	websites = new Array();
			
	$("#frm_claim_websites input[@type=text]").each(function(){
		website = $(this).val();
		if(website != '' && website != null)
			websites.push(website);
	});
	
	websites = websites.join("|||");

	if(websites == '' || websites == 'null'){
		alert("Please add at least one valid URL address!");
		return false;
	}
	
	$.post(HTTP + 'ajax/?claim_websites', {websites:websites}, function(response) {
		update = response.split("|||");
		
		if(update[0] == 1)
			$("input#claim_websites").after('<img src="' + HTTP + LAYOUTS + 'images/websites-saved.png" alt="Websites saved!" /> <input class="button" type="button" onclick="document.location=\'' + HTTP + 'claim/step2/\'" value="Next" />');
		else	alert(update[1]);
	});
}

function ChangePopularFilter(){
	filter_id = $(this).attr("id").substr(7);
	
	$.post(HTTP + 'ajax/?change_popular_filter', {filter:filter_id}, function(response) {
		if(response == 1)
			location.reload(true);
	});
}

function changeUserFavorite(){
	favorite_id = $(this).attr("id").substr(9);
	
	if($(this).attr("class").indexOf("RemoveFavorites") != -1)
		action = 'remove';
	else
		action = 'add';
		
	$.post(HTTP + 'ajax/?change_user_favorite', {id:favorite_id,action:action}, function(response) {
		if (response == 1) {
			if (action == 'remove') {
				$("#favorite_" + favorite_id).attr("class", "AddToFavorites changeUserFavorite");
				$("#favorite_" + favorite_id).text("Add to Favorites");
			}
			else {
				$("#favorite_" + favorite_id).attr("class", "RemoveFavorites changeUserFavorite");
				$("#favorite_" + favorite_id).text("Remove Favorites");
			}
		}
		$('a.changeUserFavorite').click(changeUserFavorite);			
	});		
	
}

function ChangeItemsPerPage(){
	items_per_page = $(this).text();
	
	$.post(HTTP + 'ajax/?change_items_per_page', {items_per_page:items_per_page}, function(response) {
		if(response == 1)
			location.reload(true);
	});
	
}

function VoteResult(){
	if($(this).attr("class").indexOf("vote_result") == -1)
		return false; // in case user clicks after he has voted
	
	if($(this).attr("class").indexOf("recent") != -1)
		recent = 1; // in case user votes a result from the recent area
	else	recent = 0;
	
	result_key = $(this).attr("id");
	if(result_key.indexOf("voteup") != -1){ //thumbs up
		result_key = result_key.substr(7);
		vote_type = 1;
	}
	else{ //thumbs down
		result_key = result_key.substr(9);
		vote_type = 0;
	}
	
	action = HTTP + 'ajax/?vote_result';
	db = 0;
	
	if ($(this).attr("class").indexOf("db") != -1) {//vote bookmark already saved in db
		action = HTTP + 'ajax/?vote_result_db';
		db = 1;
	}
	
	if ($(this).attr("class").indexOf("small") != -1) //small icons class		
		small = 1;
	else	
		small = 0;	
		
	if ($(this).attr("class").indexOf("popular") != -1) {//vote bookmark already saved in db
		action = HTTP + 'ajax/?vote_result_cache';
		var urlHalves = String(document.location).toUpperCase();
	
		var popular_type = 'web';
		
		if(urlHalves.indexOf("POPULAR/WEB") != -1)
			popular_type = 'web';
		else if(urlHalves.indexOf("POPULAR/IMAGES") != -1)
			popular_type = 'images';
		else if(urlHalves.indexOf("POPULAR/VIDEOS") != -1)
			popular_type = 'videos';
		else if(urlHalves.indexOf("POPULAR/NEWS") != -1)
			popular_type = 'news';
		else if(urlHalves.indexOf("POPULAR/BLOGS") != -1)
			popular_type = 'blogs';	
		else if(urlHalves.indexOf("POPULAR/BOOKMARKS") != -1)
			popular_type = 'bookmarks';	
	
	}
	else
		popular_type = 0;
	
	$.post(action, {key:result_key,type:vote_type,recent:recent,popular:popular_type}, function(response) {
		if(response == 1){
			if(vote_type == 1){ //thumbs up; darken and disable thumbs down
				if(db == 1 && small == 0)
					selected_class = "iLikeLargeSel";
				else	selected_class = "thumbsUpSel";	
				
				$("#voteup_" + result_key).attr("class", selected_class);
				
				$("#votedown_" + result_key).remove();
			}
			else{ //thumbs down
				if(db == 1 && small == 0)
					selected_class = "iDontLikeLargeSel";
				else	selected_class = "thumbsDownSel";	
				
				$("#votedown_" + result_key).attr("class", selected_class);
				$("#voteup_" + result_key).remove();
			}
		}
		
		$('a.vote_result').click(VoteResult);	
		
	});
	
}

function SendEmailsStep(emails, title, url, bcc, msg, from, thumb, code, description){
	$.post(HTTP + 'ajax/?send_emails', {emails:emails,title:title,url:url,bcc:bcc,msg:msg,from:from,thumb:thumb,code:code,description:description}, function(response) {
		update = response.split("|||");
		if (update[0] == 1)//continue sending
			SendEmailsStep(update[1], title, url, bcc, msg, from, thumb);
		else if(update[0] == 0) 
				$("p.statusMessage").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			//show email step 2
			$.get(HTTP + 'ajax/?get_email_step2', function(response) {
				if (response != 0) {
					$(".mWin").css({
						width: '600px'
					});
					mWin.innerHTML = response;
					
					$('a.changeUserFavorite').click(changeUserFavorite);
					//setTimeout("hide_modal_window()", 2000);		
				}
				//else 
					//hide_modal_window();		
			});
		}
	});	
}

function EmailToFriend(){
	$("p.statusMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
		
	emails = $('#frm_email_to_friend input[@name=to]').val();
	bcc = $('#frm_email_to_friend input[@name=bcc]').val();
	title = $('#frm_email_to_friend input[@name=subject]').val();
	from = $('#frm_email_to_friend input[@name=email]').val();
	msg = $('#frm_email_to_friend textarea[@name=msg]').val();
	url = $('#frm_email_to_friend input[@name=url]').val();
	code = $('#frm_email_to_friend input[@name=code]').val();
	thumb = $('#frm_email_to_friend input[@name=thumb_src]').val();
	description = $('#frm_email_to_friend input[@name=description]').val();
			
	if (emails == '' || emails == null || title == '' || title == null || from == '' || from == null) {
		$("p.statusMessage").html('<span class="modal_login_failed">Incomplete fields!</span>');
		return false;
	}
	
	$("p.statusMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Sending emails...');
	SendEmailsStep(emails, title, url, bcc, msg, from, thumb, code, description);	
}

function AddBookmark(){
			
	$("p.statusMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
		
	if ($("#frm_add_bookmark input[@name=title]").val() == '' || $("#frm_add_bookmark input[@name=url]").val() == null) {
		$("p.statusMessage").html('<span class="modal_login_failed">Incomplete fields!</span>');
		return false;
	}
		
	services = new Array();		
			
	$('#frm_add_bookmark input[@type=checkbox]').each(function(){
		if($(this).attr("checked") == true)
			services.push($(this).val());
			
	});	
	
	title = $('#frm_add_bookmark input[@name=title]').val();
	description = $('#frm_add_bookmark textarea[@name=description]').val();
	tags = $('#frm_add_bookmark input[@name=tags]').val();
	url = $('#frm_add_bookmark input[@name=url]').val();
	
	services = services.join(",");
						
			
	//ajax request; check for duplicates, save in db, sync with external services
	$.post(HTTP + 'ajax/?add_bookmark', {services:services,title:title,description:description,tags:tags,url:url,key:last_bookmark}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p.statusMessage").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			if(update.length == 4 && update[3] == 'redirect'){
				bookmark_id= update[2];
				$("p.statusMessage").html('<span class="confirmMessage">' + update[1] + ' Redirecting to profile page for providing service logins...</span>');
			}else{
				bookmark_id= update[2];
				$("p.statusMessage").html('<span class="confirmMessage">' + update[1] + '</span>');	
				}
				
			//publish to facebook; steps 1-4;
			//1. is facebook checkbox checked? (check var services)
			//2. is the user logged into facebook? (AJAX)
			//3. json_encode array (AJAX response)
			//4. publish to facebook (call facebook_publish_feed_story(template_bundle_id, template_bundle_data);)
			
			//title = JSON.encode(title);
			//description = JSON.encode(description);
														
			var data = {'title': title, 'description': description};
									
			if (services.indexOf("facebook") != -1) { //user checked facebook
				$.get(HTTP + 'ajax/?publish_to_facebook', function(response){
					if (response == 1) 
						facebook_publish_feed_story(61684578816, data);
				//	setTimeout("hide_modal_window()", 2000);	
				});
			}else if(services.indexOf("twitter") !=-1){//user cheched twitter 
				$.get(HTTP + 'ajax/?publish_to_twitter='+bookmark_id,function(response){
					if (response == 1) {
					//hide_modal_window();	
					 self.parent.location.reload();
					}
				});
			}
			else {
				//auto-close window after 2 seconds
			//	setTimeout("hide_modal_window()", 2000);
				if(update.length == 3 && update[2] == 'redirect')
					document.location = HTTP + 'users/account_preferences/';
				
			}
			
			
		}
	});
	
}

function AddBookmarkToolbar(){
			
	$("p.statusMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
		
	if ($("#frm_add_bookmark input[@name=title]").val() == '' || $("#frm_add_bookmark input[@name=url]").val() == null) {
		$("p.statusMessage").html('<span class="modal_login_failed">Incomplete fields!</span>');
		return false;
	}
	
	services = new Array();		
			
	$('#frm_add_bookmark input[@type=checkbox]').each(function(){
		if($(this).attr("checked") == true)
			services.push($(this).val());
			
	});	
	
	title = $('#frm_add_bookmark input[@name=title]').val();
	description = $('#frm_add_bookmark textarea[@name=description]').val();
	tags = $('#frm_add_bookmark input[@name=tags]').val();
	url = $('#frm_add_bookmark input[@name=url]').val();
	
	services = services.join(",");
						
	//ajax request; check for duplicates, save in db, sync with external services
	$.post(HTTP + 'ajax/?add_bookmark_toolbar', {services:services,title:title,description:description,tags:tags,url:url,key:last_bookmark}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p.statusMessage").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			$("p.statusMessage").html('<span class="confirmMessage">' + update[1] + ' This window will now close...</span>');	
			
			//publish to facebook; steps 1-4;
			//1. is facebook checkbox checked? (check var services)
			//2. is the user logged into facebook? (AJAX)
			//3. json_encode array (AJAX response)
			//4. publish to facebook (call facebook_publish_feed_story(template_bundle_id, template_bundle_data);)
			
			title = JSON.encode(title);
			description = JSON.encode(description);
														
			var data = {'title': title, 'description': description};
									
			if (services.indexOf("facebook") != -1) { //user checked facebook
				$.get(HTTP + 'ajax/?publish_to_facebook', function(response){
					if (response == 1) 
						facebook_publish_feed_story(61684578816, data);
					setTimeout("window.close()", 2000);	
				});
			}
			else {
				//auto-close window after 2 seconds
				setTimeout("window.close()", 2000);	
			}
		}
	});
}

function AddBookmarkPopular(){
			
	$("p.statusMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Validating fields...');
		
	if ($("#frm_add_bookmark input[@name=title]").val() == '' || $("#frm_add_bookmark input[@name=url]").val() == null) {
		$("p.statusMessage").html('<span class="modal_login_failed">Incomplete fields!</span>');
		return false;
	}
	
	services = new Array();		
			
	$('#frm_add_bookmark input[@type=checkbox]').each(function(){
		if($(this).attr("checked") == true)
			services.push($(this).val());
			
	});	
	
	title = $('#frm_add_bookmark input[@name=title]').val();
	description = $('#frm_add_bookmark textarea[@name=description]').val();
	tags = $('#frm_add_bookmark input[@name=tags]').val();
	url = $('#frm_add_bookmark input[@name=url]').val();
	
	services = services.join(",");
	
	var urlHalves = String(document.location).toUpperCase();
	
	var popular_type = 'web';
	
	if(urlHalves.indexOf("POPULAR/WEB") != -1)
		popular_type = 'web';
	else if(urlHalves.indexOf("POPULAR/IMAGES") != -1)
		popular_type = 'images';
	else if(urlHalves.indexOf("POPULAR/VIDEOS") != -1)
		popular_type = 'videos';
	else if(urlHalves.indexOf("POPULAR/NEWS") != -1)
		popular_type = 'news';
	else if(urlHalves.indexOf("POPULAR/BLOGS") != -1)
		popular_type = 'blogs';	
	else if(urlHalves.indexOf("POPULAR/BOOKMARKS") != -1)
		popular_type = 'bookmarks';	
	else
		popular_type = 'bookmarks';			
						
	//ajax request; check for duplicates, save in db, sync with external services
	$.post(HTTP + 'ajax/?add_bookmark_popular', {services:services,title:title,description:description,tags:tags,url:url,key:last_bookmark,popular:popular_type}, function(response) {
		update = response.split("|||");
		if (update[0] == 0) 
			$("p.statusMessage").html('<span class="modal_login_failed">' + update[1] + '</span>');
		else {
			if(update.length == 3 && update[2] == 'redirect')
				$("p.statusMessage").html('<span class="confirmMessage">' + update[1] + ' Redirecting to profile page for providing service logins...</span>');
			else
				$("p.statusMessage").html('<span class="confirmMessage">' + update[1] + '</span>');	
				
			//publish to facebook; steps 1-4;
			//1. is facebook checkbox checked? (check var services)
			//2. is the user logged into facebook? (AJAX)
			//3. json_encode array (AJAX response)
			//4. publish to facebook (call facebook_publish_feed_story(template_bundle_id, template_bundle_data);)
			
			title = JSON.encode(title);
			description = JSON.encode(description);
														
			var data = {'title': title, 'description': description};
									
			if (services.indexOf("facebook") != -1) { //user checked facebook
				$.get(HTTP + 'ajax/?publish_to_facebook', function(response){
					if (response == 1) 
						facebook_publish_feed_story(61684578816, data);
					setTimeout("hide_modal_window()", 2000);	
				});
			}
			else {
				//auto-close window after 2 seconds
				setTimeout("hide_modal_window()", 2000);
				if(update.length == 3 && update[2] == 'redirect')
					document.location = HTTP + 'users/account_preferences/';
				
			}
		}
	});
	
}

var last_bookmark;

function facebook_publish_feed_story(form_bundle_id, template_data) {
  // Load the feed form
  FB.ensureInit(function() {
          FB.Connect.showFeedDialog(form_bundle_id, template_data);
  });
}

function BookmarkResult(){
	last_bookmark = $(this).attr("id").substr(7);
}

function SubmitSearchForm(){
	type_id = $(this).attr("id").substr(7);
	
	switch(type_id){
		case 'images':
			$('#form_search').attr("action", HTTP + 'images');
			break;
		case 'videos':
			$('#form_search').attr("action", HTTP + 'videos');
			break;
		case 'news':
			$('#form_search').attr("action", HTTP + 'news');
			break;
		case 'blogs':
			$('#form_search').attr("action", HTTP + 'blogs');
			break;
		case 'bookmarks':
			$('#form_search').attr("action", HTTP + 'bookmarks');
			break;	
		default:
			$('#form_search').attr("action", HTTP + 'search');				
					
	}
	
	document.form_search.submit();	
}

function RemoveIm(){
	id= $(this).attr("id").substr(3);
	
	$.post(HTTP + 'ajax/?remove_im', {id:id}, function(response) {
		if(response == 1){
			//remove div container
			$('div#div_im_' + id).remove();
		}
				
	});
		
}

function RemoveEmail(){
	id= $(this).attr("id").substr(6);
	
	$.post(HTTP + 'ajax/?remove_email', {email:id}, function(response) {
		if(response == 1){
			//remove div container
			$('div#div_email_' + id).remove();
		}
				
	});
		
}

function RemoveCompany(){
	id= $(this).attr("id").substr(8);
	
	$.post(HTTP + 'ajax/?remove_company', {id:id}, function(response) {
		if(response == 1){
			//remove div container
			$('div#div_company_' + id).remove();
		}
				
	});
		
}

function RemoveWebsite(){
	id= $(this).attr("id").substr(8);
	
	$.post(HTTP + 'ajax/?remove_website', {id:id}, function(response) {
		if(response == 1){
			//remove div container
			$('div#div_website_' + id).remove();
		}
				
	});
		
}

var facebook_form_submitted = 0;

function Import_Facebook_Profile(){
	if(facebook_form_submitted == 1) //prevent multiple submissions
		return false;
	
	facebook_form_submitted = 1;
	
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.get(HTTP + 'ajax/?import_facebook_profile', function(response) {
		if (response == 1) {
			if ($(this).attr("id") == 'import_facebook_profile_account') 
				$("p#profile_facebook").text('Facebook profile imported');
			else {
				$("p.confirmMessage").text('Facebook profile imported');
				$("a.skip").attr("href", HTTP + 'registration/step6/');
			}
		}
		else {
			update = response.split("|||");
			if($(this).attr("id") == 'import_facebook_profile_account')
				$("p#profile_facebook").html('<span style="color: red">' + update[1] + '</span>');
			else
				$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
			
		}
		
		facebook_form_submitted = 0;	
	});	
}

var myspace_form_submitted = 0;

function Import_MySpace_Profile(){
	if(myspace_form_submitted == 1) //prevent multiple submissions
		return false;
	
	email = $('input#myspace_user').val();
	pass = $('input#myspace_pass').val();
	
	if(email == '' || pass == ''){
		alert('Please fill in your login information!');
		return false;
	}
	
	myspace_form_submitted = 1;
	
	button_id = $(this).attr("id");
	
	if (button_id == 'import_myspace_profile_account')
		$("#profile_myspace").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	else
		$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.post(HTTP + 'ajax/?import_myspace_profile', {email:email,pass:pass}, function(response) {
				
		if (response == 1) {
			if (button_id == 'import_myspace_profile_account') 
				$("#profile_myspace").text('MySpace profile imported');
			else {
				$("p.confirmMessage").text('MySpace profile imported');
				$("a.skip").attr("href", HTTP + 'registration/step6/');
			}	
		}
		else {
			update = response.split("|||");
			if (button_id == 'import_myspace_profile_account')
				$("#profile_myspace").html('<span style="color: red">' + update[1] + '</span>');
			else 
				$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
				
		}
		
		myspace_form_submitted = 0;	
	});
}

var bebo_form_submitted = 0;

function Import_Bebo_Profile(){
	if(bebo_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#bebo_user').val();
	pass = $('input#bebo_pass').val();
	
	if(user == '' || pass == ''){
		alert('Please fill in your login information!');
		return false;
	}
	
	bebo_form_submitted = 1;
	
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.post(HTTP + 'ajax/?import_bebo_profile', {user:user,pass:pass}, function(response) {
		if (response == 1) {
			$("p.confirmMessage").text('Bebo profile imported');
			$("a.skip").attr("href", HTTP + 'registration/step6/');
		}
		else {
			update = response.split("|||");
			$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
		}
		
		bebo_form_submitted = 1;	
	});
}

var orkut_form_submitted = 0;

function Import_Orkut_Profile(){
	if(orkut_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#orkut_user').val();
	pass = $('input#orkut_pass').val();
	
	if(user == '' || pass == ''){
		alert('Please fill in your login information!');
		return false;
	}
	
	orkut_form_submitted = 1;
	
	$("#orkut_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.post(HTTP + 'ajax/?import_orkut_profile', {user:user,pass:pass}, function(response) {
		if(response == 1)
			$("#orkut_message").text('Orkut profile imported');
		else{
			update = response.split("|||");
			$("#orkut_message").html('<span style="color: red">' + update[1] + '</span>');
		}
		
		orkut_form_submitted = 0;	
	});
}
var twitter_form_submitted=0;

	function Import_Twitter_Profile(){
	
	if(twitter_form_submitted == 1) //prevent multiple submissions
			return false;
			
				
	//user = $('input#twitter_user').val();
	//pass = $('input#twitter_pass').val();
	
	/*if(user == '' || pass == ''){
		$("#twitter_message").html('<span style="color: red">Please fill in your login information!</span>');
		return false;
	}*/
	
	
		twitter_form_submitted = 1;
	
	
		$("#twitter_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
		$.get(HTTP + 'ajax/?import_twitter_profile', function(response) {
		update = response.split("|||");
			if(update[0] == 1){
				$("#twitter_message").text('Twitter profile imported');
				}
			else{
				
				$("#twitter_message").html('<span style="color: red">' + update[1] + '</span>');
			}
			
			twitter_form_submitted = 0;	
		});

		
	/*$.get(HTTP + 'ajax/?import_facebook_profile', function(response) {
		if (response == 1) {
			if ($(this).attr("id") == 'import_facebook_profile_account') 
				$("p#profile_facebook").text('Facebook profile imported');
			else {
				$("p.confirmMessage").text('Facebook profile imported');
				$("a.skip").attr("href", HTTP + 'registration/step6/');
			}
		}
		else {
			update = response.split("|||");
			if($(this).attr("id") == 'import_facebook_profile_account')
				$("p#profile_facebook").html('<span style="color: red">' + update[1] + '</span>');
			else
				$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
			
		}
		
		facebook_form_submitted = 0;	
	});	*/
		
		
		

	}





var friendster_form_submitted = 0;

function Import_Friendster_Profile(){
	if(friendster_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#friendster_user').val();
	pass = $('input#friendster_pass').val();
	
	if(user == '' || pass == ''){
		alert('Please fill in your login information!');
		return false;
	}
	
	friendster_form_submitted = 1;
	
	$("#friendster_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.post(HTTP + 'ajax/?import_friendster_profile', {user:user,pass:pass}, function(response) {
		if(response == 1)
			$("#friendster_message").text('Friendster profile imported');
		else{
			update = response.split("|||");
			$("#friendster_message").html('<span style="color: red">' + update[1] + '</span>');
		}
		
		friendster_form_submitted = 0;	
	});
}

var hi5_form_submitted = 0;

function Import_Hi5_Profile(){
	if(hi5_form_submitted == 1) //prevent multiple submissions
		return false;
	
	email = $('input#hi5_email').val();
	pass = $('input#hi5_pass').val();
	
	if(email == '' || pass == ''){
		alert('Please fill in your login information!');
		return false;
	}
	
	hi5_form_submitted = 1;
	
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
		
	$.post(HTTP + 'ajax/?import_hi5_profile', {email:email,pass:pass}, function(response) {
		if (response == 1) {
			$("p.confirmMessage").text('Hi5 profile imported');
			$("a.skip").attr("href", HTTP + 'registration/step6/');
		}
		else {
			update = response.split("|||");
			$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
		}
		
		hi5_form_submitted = 0;
			
	});
}

function ShowBrowserUploadBookmarks(){
	//disable all first
	$("li#li_firefox").addClass('li_hidden');	
	$("li#li_safari").addClass('li_hidden');	
	$("li#li_ie").addClass('li_hidden');	
	
	id = $(this).attr("id").substr(7);
	
	//show active list
	$("li#li_" + id).removeClass('li_hidden');			
}

var stumble_form_submitted = 0;

function ImportStumbleUponBookmarks(){
	//check for login and pass
	
	if(stumble_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#stumbleupon_user').val();
	pass = $('input#stumbleupon_pass').val();	
	
	if(user == '' || pass == ''){
		alert('Please complete your username and password!');
		return false;
	}
	
	stumble_form_submitted = 1;
	
	$("#stumbleupon_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	
	$.post(HTTP + 'ajax/?import_stumbleupon_bookmarks', {user:user,pass:pass}, function(response) {
		if(response == 1){
			//$('input#stumbleupon_user').val('');
			//$('input#stumbleupon_pass').val('');
			$('#stumbleupon_message').attr("class", "confirmMessage");
			$('#stumbleupon_message').text('StumbleUpon bookmarks added');
		}
		else{
			update = response.split('|||');
			
			$('#stumbleupon_message').attr("class", "errorMessage");
			$('#stumbleupon_message').text(update[1]);
		}
		
		stumble_form_submitted = 0;
	});
}

var google_form_submitted = 0;

function ImportGoogleBookmarks(){
	if(google_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#google_user').val();
	pass = $('input#google_pass').val();	
	
	if(user == '' || pass == ''){
		alert('Please complete your username and password!');
		return false;
	}
	
	google_form_submitted = 1;
	
	$("#google_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	
	$.post(HTTP + 'ajax/?import_google_bookmarks', {user:user,pass:pass}, function(response) {
		if(response == 1){
			//$('input#google_user').val('');
			//$('input#google_pass').val('');
			$('#google_message').attr("class", "confirmMessage");
			$('#google_message').text('Google bookmarks added');
		}
		else{
			update = response.split('|||');
			
			$('#google_message').attr("class", "errorMessage");
			$('#google_message').text(update[1]);
		}
		
		google_form_submitted = 0;
	});
}

var delicious_form_submitted = 0;

function ImportDeliciousBookmarks(){
	if(delicious_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input#del_user').val();
	pass = $('input#del_pass').val();	
	
	if(user == '' || pass == ''){
		alert('Please complete your username and password!');
		return false;
	}
	
	delicious_form_submitted = 1;
	
	$("#delicious_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	
	$.post(HTTP + 'ajax/?import_delicious_bookmarks', {user:user,pass:pass}, function(response) {
		if(response == 1){
			//$('input#del_user').val('');
			//$('input#del_pass').val('');
			$('#delicious_message').attr("class", "confirmMessage");
			$('#delicious_message').text('del.icio.us bookmarks added');
		}
		else{
			update = response.split('|||');
			
			$('#delicious_message').attr("class", "errorMessage");
			$('#delicious_message').text(update[1]);
		}
		
		delicious_form_submitted = 0;
		
	});
}

var facebook_form_submitted = 0;

function ImportFacebookBookmarks(){
	if(facebook_form_submitted == 1) //prevent multiple submissions
		return false;
	
	url = $('input#facebook_url').val();
		
	if(url == ''){
		alert('Please complete the feed URL address!');
		return false;
	}
	
	facebook_form_submitted = 1;
	
	$("#facebook_message").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	
	$.post(HTTP + 'ajax/?import_facebook_bookmarks', {url:url}, function(response) {
		if(response == 1){
			//$('input#facebook_url').val('');
			$('#facebook_message').attr("class", "confirmMessage");
			$('#facebook_message').text('Facebook bookmarks added');
		}
		else{
			$('#facebook_message').attr("class", "errorMessage");
			$('#facebook_message').text('Import failed!');
		}
		
		facebook_form_submitted = 0;
		
	});
}

function AddEmailField(){
			
	$('.addresses').append('<input type="text" name="emails[]" />');

}

function ReloadCaptcha(){
			
	$('#captcha_code').html('<img name="captcha' + Math.random() + '" id="captcha' + Math.random() + '" src="'+ HTTP + 'ajax/?captcha&sid=' + Math.random() + '" alt="captcha" />');
}

var recover_form_submitted = 0;

function RecoverPassword(){
	if(recover_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('input[@name=user]').val();
	email = $('input[@name=email]').val();
			
	if( (user == null && email == null) || (user == '' && email == '') ){
		alert('Insert Username or Email address!');
		return false;
	}
	
	recover_form_submitted = 1;
			
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" />');
	
	$.post(HTTP + 'ajax/?recover_password', {email:email,user:user}, function(response) {
		if(response == 1)
			$("p.confirmMessage").text('Password successfully sent');
		else{
			update = response.split("|||");
			$("p.confirmMessage").html('<span style="color: red">' + update[1] + '</span>');
		}
		
		recover_form_submitted = 0;
	});
}

var create_account_form_submitted = 0;

function CreateAccount(){
	
	if(create_account_form_submitted == 1) //prevent multiple submissions
		return false;
	
	//remove mandatory field class if previously added
	$('span,label').each(function(){
		$(this).removeClass('mandatory_field');
	});	
	
	valid_form = 1;
	
	$("form#registration_form input,select").each(function(){
				
		field_type = $(this).attr("type");
		if(field_type != 'image'){
									
			//for checkbox type fields see if it's checked
			if(field_type == 'checkbox'){
				if(this.checked == false)
					field_value = 0;
				else
					field_value = 1;	
			}
			else	
				field_value = $(this).val();
								
			if(field_value == '' || field_value == 0){
				if($(this).attr("name") == 'zip'){
					$('label#zip_city_label').addClass('mandatory_field');
				}
				else{
					$('label#label_' + $(this).attr('name')).addClass('mandatory_field');
					//day and year do not have labels; used spans instead
					$('span#label_' + $(this).attr('name')).addClass('mandatory_field');
				}
				valid_form = 0;
			}
		}
	});
				
	//other verifications
	user = $("form#registration_form input[@name=user]").val();
	pass = $("form#registration_form input[@name=pass]").val();
	pass2 = $("form#registration_form input[@name=pass2]").val();
	email = $("form#registration_form input[@name=email]").val();	
	day = $("form#registration_form input[@name=day]").val();	
	year = $("form#registration_form input[@name=year]").val();
	
	fname = $("form#registration_form input[@name=fname]").val();
	lname = $("form#registration_form input[@name=lname]").val();
	
	city = $("form#registration_form input[@name=zip]").val();
	
	month = $("form#registration_form select[@name=month]").val();
	
	country = $("form#registration_form select[@name=country]").val();
	
	code = $("form#registration_form input[@name=code]").val();	
		
	//check for password correspondence
	if(pass != pass2){
		$('label#label_pass').addClass('mandatory_field');
		$('label#label_pass2').addClass('mandatory_field');
		//return false;
	}
	
	//check for email validity
	if(!emailValid(email)){
		$('label#label_email').addClass('mandatory_field');
		//return false;
	}
	
	//check for day and year validity
	//day
	if(!IsNumeric(day) || day < 1 || day > 31){
		$('span#label_day').addClass('mandatory_field');
		//return false;
	}
	
	//year
	if(!IsNumeric(year) || year > 1995 || year < 1930){// at least 13 years old
		$('span#label_year').addClass('mandatory_field');
		//return false;
	}
	
	//check if form is valid
	if(valid_form == 0)
		return false;
		
	//check for captcha code and existance of user or email address
	$.post(HTTP + 'ajax/?check_account_info', {user:user,pass:pass,email:email,fname:fname,lname:lname,month:month,day:day,year:year,country:country,city:city,code:code}, function(response) {
		update = response.split('|||');
		if(update[0] == 0){
			if(update[1] == 1)//invalid code
				$('label#label_code').addClass('mandatory_field');
			else if(update[1] == 2) //user
				$('label#label_user').addClass('mandatory_field');	
			else
				$('label#label_email').addClass('mandatory_field');		
		}
		else{ //valid data; insert in database
			//remove trigger button id; prevent duplicate submitting
			create_account_form_submitted = 1;
			document.location = HTTP + 'registration/step1-1/';
		}
	});
				
}

function SearchActiveTab(){
	$('div#search li').each(
		function(){
			//remove active class
			$(this).removeAttr('class');
			//link all tabs
			$(this).html('<a href="javascript: void(0);">' + $(this).text() + '</a>');
		}
	);
	
	//add active class to selected tab
	$(this).attr('class', 'active');
	
	//unlink active tab
	$(this).html('<span>' + $(this).text() + '</span>');
	
	//change form action based on selected tab
	active_tab = $(this).text();
	
	if(active_tab.indexOf("Web") != -1)
		$('#search_form').attr("action", HTTP + 'search');
	else if(active_tab.indexOf("Images") != -1)
		$('#search_form').attr("action", HTTP + 'images');
	else if(active_tab.indexOf("Video") != -1)
		$('#search_form').attr("action", HTTP + 'videos');
	else if(active_tab.indexOf("News") != -1)
		$('#search_form').attr("action", HTTP + 'news');
	else if(active_tab.indexOf("Blogs") != -1)
		$('#search_form').attr("action", HTTP + 'blogs');
	else
		$('#search_form').attr("action", HTTP + 'bookmarks');								
}

var login_form_submitted = 0;

function Login(){
	if(login_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('#frm_login input[@name=user]').val();
	pass = $('#frm_login input[@name=pass]').val();
	
	if($('input[@type=checkbox]').attr('checked') == true)
		remember = 1;
	else
		remember = 0;	
					
	if(user == null || pass == null || user == '' || pass == ''){
		alert('Insert Username and Password!');
		return false;
	}
	
	login_form_submitted = 1;
				
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Validating login...</span>');
		
	$.post(HTTP + 'ajax/?modal_login', {user:user,pass:pass,remember:remember}, function(response) {
		update = response.split("|||");
		
		if (update[0] == 1) {
			//see if redirect is needed after process is finished
			if(update.length == 2 && update[0] == 1) //valid login with redirect
				redirect_url = update[1];
			else redirect_url = HTTP;
			
			$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Synching bookmarks...</span>');
			//ajax request; sync bookmarks; import + export
			$.get(HTTP + 'ajax/?sync_bookmarks', function(response) {
				document.location = redirect_url;
			});
		
		}
		else
			$("p.confirmMessage").html('<span class="modal_login_failed">Failed</span>');
					
		login_form_submitted = 0; //reload trigger button		
	});
}
var login_form_submitted = 0;
function LoginTwitter(){
	
	if(login_form_submitted == 1) //prevent multiple submissions
		return false;
		var option;

		option= $('#frm_login_twitter input[name=twitter_sign_in]:checked').val();
	
	if(option == 1){
		//new gravee account
		//get fields
		$('div#div_gravee_credentials').css('display','none');
		
		username = 	$('#frm_login_twitter input[name=new_user]').val();
		email =  	$('#frm_login_twitter input[name=new_email]').val();
		pass = 		$('#frm_login_twitter input[name=new_pass]').val();
		confirm_pass = $('#frm_login_twitter input[name=new_pass2]').val();
		followers = $('#tell_twitter_followers').attr('checked')?1:0;
		termsofuse = $('#terms_of_use').attr('checked')?1:0;
	
		login_form_submitted = 1;
		
			$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Validating login...</span>');
		
		$.post(HTTP+'ajax/?new_acount_with_twitter',{username:username,email:email,pass:pass,confirm_pass:confirm_pass,followers:followers,termsofuse:termsofuse},function(response){
			info  = response.split('|||');
			if(info[0]  == 0){
				//error case
				$("p.confirmMessage").html('<span class="modal_login_failed">'+info[1]+'</span>');
				login_form_submitted = 0;
			}else if(info[0] == 1){
				login_form_submitted = 1;
				if(info.length == 5 && info[2]=='followers' ){
					user_id = info[3];
					twitter_id = info[4];
					$("p.confirmMessage").html('<span class="confirmMessage">'+info[1]+'</span>');
					$.get(HTTP+'ajax/?invite_twitter_followers',{user_id:user_id,twitter_id:twitter_id},function(response_1){	
						if (response_1 == 1) 
						self.document.location = HTTP + 'registration/step1-1/';
					});
					
				}else {
					$("p.confirmMessage").html('<span class="confirmMessage">'+info[1]+'</span>');
					self.document.location = HTTP + 'registration/step1-1/';
				}
				}
			});
	}else if(option == 2) {
		//merge with gravee profile
		email = $('#frm_login_twitter input[@name=gravee_email]').val();
		pass = $('#frm_login_twitter input[@name=gravee_pass]').val();
		followers = $('#tell_twitter_followers').attr('checked')?1:0;
		termsofuse = $('#terms_of_use').attr('checked')?1:0;
		
		$('div#div_gravee_new_account').css('display','none');
		
		if(email == null || pass == null || email == '' || pass == '')
			$("p.confirmMessage").html('<span class="modal_login_failed">Insert email and password   ...</span>');
			
			login_form_submitted = 1;	
			
			$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Validating login...</span>');		
				
			$.post(HTTP+'ajax/?login_merge_with_twitter',{email:email,pass:pass,followers:followers,termsofuse:termsofuse},function(response){
				info = response.split('|||');
				if(info[0] == 0){
					//error case
					$("p.confirmMessage").html('<span class="modal_login_failed">'+info[1]+'</span>');
					login_form_submitted = 0;
				}if(info[0] == 1){
					login_form_submitted = 1;
					if(info.length == 5 && info[2]=='followers' ){
						user_id = info[3];
						twitter_id = info[4];
						$("p.confirmMessage").html('<span class="confirmMessage">'+info[1]+'</span>');
						$.get(HTTP+'ajax/?invite_twitter_followers',{user_id:user_id,twitter_id:twitter_id},function(response_1){	
							if (response_1 == 1) 
						self.document.location = HTTP ;
						});
				}else {
					$("p.confirmMessage").html('<span class="confirmMessage">'+info[1]+'</span>');
					self.document.location = HTTP ;
				}
				}
			});
				
	}else  if(option == 'undefined' || option == '' || option == null){
		alert('Please choose an option to login!'); 
		return;
	}
	
	
}



function LoginToolbar(){
	if(login_form_submitted == 1) //prevent multiple submissions
		return false;
	
	user = $('#frm_login input[@name=user]').val();
	pass = $('#frm_login input[@name=pass]').val();
	
	if($('input[@type=checkbox]').attr('checked') == true)
		remember = 1;
	else
		remember = 0;	
					
	if(user == null || pass == null || user == '' || pass == ''){
		alert('Insert Username and Password!');
		return false;
	}
	
	login_form_submitted = 1;
				
	$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Validating login...</span>');
		
	$.post(HTTP + 'ajax/?modal_login', {user:user,pass:pass,remember:remember}, function(response) {
		update = response.split("|||");
		
		if (update[0] == 1) {
			//see if redirect is needed after process is finished
			if(update.length == 2 && update[0] == 1) //valid login with redirect
				redirect_url = update[1];
			else redirect_url = HTTP;
			
			$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Synching bookmarks...</span>');
			//ajax request; sync bookmarks; import + export
			$.get(HTTP + 'ajax/?sync_bookmarks', function(response) {
				$("p.confirmMessage").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> <span class="statusMessage">Success. This window will now close...</span>');
				setTimeout("window.close()", 2000);
			});
		
		}
		else
			$("p.confirmMessage").html('<span class="modal_login_failed">Failed</span>');
					
		login_form_submitted = 0; //reload trigger button		
	});
}

function MetaDescription(bookmark_id){
	if(bookmark_id == null || bookmark_id == 0 || bookmark_id == '')
		return false;
	
	$("#meta_description").html('<img src="' + HTTP + LAYOUTS + 'images/load.gif" border="0" /> Loading website description...<br /><br style="line-height: 10px;" />');

	$.post(HTTP + 'ajax/?meta_description', {bookmark_id:bookmark_id}, function(response) {
		update = response.split("|||");
		if(update[0] == 1)
			$("#meta_description").html(update[1] + '<br /><br style="line-height: 10px;" />');
		else	$("#meta_description").html('');	
		
	});
 
}


	//get order and sort from url needed to sort
	function existsURLParam(strParamName) {
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > - 1 ) {
			var strQueryString = strHref.substr(strHref.indexOf("?")+1).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
				if (aQueryString[iParam] == strParamName.toLowerCase()) return true;
			}
		}
		return false;
	} 
 
	//get order and sort from url needed to sort
	function getURLParam(strParamName) {
		var strReturn = "";
		var strHref = window.location.href;
		if ( strHref.indexOf("?") > - 1 ) {
			var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
			var aQueryString = strQueryString.split("&");
			for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
				if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > - 1 ) {
					var aParam = aQueryString[iParam].split("=");
					strReturn = aParam[1];
					break;
					}
				}
			}
		return unescape(strReturn);
	} 
	
	
function ToggleVisibility(targetID){
	var target=document.getElementById(targetID);
	if(target.style.display!='none'){
		target.style.display='none';
		
	}else{
		target.style.display='';
	
	}
}	

	
	