
function updateCategoryListing(cat_id) {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('category/ajaxUpdateHomeCatListing')?>",
        url: site_url+"/category/ajaxUpdateHomeCatListing",
        data: "category_id="+cat_id,
        beforeSend: function() {
            $("#cat_"+cat_id).html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#cat_"+cat_id).html(data.responseText); 
        }
    });
}

function updateNIVListing(page_id) {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('article/ajaxUpdateNIVListing')?>",
        url: site_url+"/article/ajaxUpdateNIVListing/",
        data: "page_id="+page_id,
        beforeSend: function() {
            $("#"+page_id).html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#"+page_id).html(data.responseText); 
        }
    });
}

function signin() {
	var isValid = false;
	var email = document.getElementById('user_email').value;
	var pass  = document.getElementById('user_password').value;
	
	var filter = new RegExp("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
	if(filter.test(email))
	{
		isValid = true;
	}
	else
	{
		document.getElementById('user_email').value = "Invalid Email";
		document.getElementById('user_email').style.color = "red";
	}
	
	
	if(isValid == true) {
		$.ajax({
	        type: "POST",
	        //url: "<?=site_url('users/login')?>",
	        url: site_url+"/users/login/",
	        data: $("#loginform").serialize(),
	        beforeSend: function() {
	            $("#memberLogin").html('<img src="im/loading_spinner.gif" />'); 
	        },
	        complete: function(data) {
	        	//alert(data.responseText);
	            top.location.href = location.href;
	            //$("#memberLogin").css('height', 'auto');
	            //$("#memberLogin").html(data.responseText);
	        }
	    });
	}
}

function register() {
	GB_show('', site_url+'/users/registration/', 340,650);
}

function forgetpassword() {
	GB_show('', site_url+'/users/forgetpassword/', 200,518);
}

function registerAction() {
	var isError = false;
	var inputField = new Array(
			'user_firstname', 'user_lastname',
			'user_email', 'user_password', 'user_cfm_password',
			'user_gender', 'birthmth',
			'birthday', 'birthyear'
	);
	
	for(var i = 0; i < inputField.length; i++) {
		var ele = document.getElementById(inputField[i]);
		if(ele.value == "") {
			ele.style.background = "#cc3333";
			isError = true;
		} else {
			ele.style.background = "#ffffff";
		}
	}
	
	if(document.getElementById('user_password').value != document.getElementById('user_cfm_password').value) {
		isError = true;
		document.getElementById('user_password').style.background = '#cc3333';
		document.getElementById('user_cfm_password').style.background = '#cc3333';
	}
	
	if(isError == false) {
		// submit
		document.forms['registrationForm'].action = site_url+'/users/registration';
		document.forms['registrationForm'].submit();
	}
}

function registerSubscriberAction() {
	var isError = false;
	var inputField = new Array(
			'user_firstname', 'user_lastname',
			'user_email', 'user_password', 'user_cfm_password',
			'user_gender', 'birthmth',
			'birthday', 'birthyear'
	);
	
	for(var i = 0; i < inputField.length; i++) {
		var ele = document.getElementById(inputField[i]);
		if(ele.value == "") {
			ele.style.background = "#cc3333";
			isError = true;
		} else {
			ele.style.background = "#ffffff";
		}
	}
	
	if(document.getElementById('user_password').value != document.getElementById('user_cfm_password').value) {
		isError = true;
		document.getElementById('user_password').style.background = '#cc3333';
		document.getElementById('user_cfm_password').style.background = '#cc3333';
	}
	
	if(isError == false) {
		// submit
		document.forms['sub_form'].action = site_url+'/subscription/memberForm';
		document.forms['sub_form'].submit();
	}
}

function signin_fb() {
	FB_RequireFeatures(["XFBML"], function(){
		//FB.Facebook.init("88e4914d2f23006febbee57be27ba2d1", "<?=site_url('xd_receiver.htm')?>");
		FB.Facebook.init("88e4914d2f23006febbee57be27ba2d1", site_url+"/xd_receiver.htm", {'doNotUseCachedConnectState' : true});
	});	
	
	FB.ensureInit(function() { 
		FB.Connect.get_status().waitUntilReady( function(status) { 
			switch (status) { 
			case FB.ConnectState.connected: 					
					 check_registered();						 
					 break; 
				case FB.ConnectState.appNotAuthorized: 
				case FB.ConnectState.userNotLoggedIn: 
					 FB.Connect.requireSession(check_registered);						 
					 break; 
			} 
		}); 
	});
}

function check_registered(){
	var fbid = FB.Connect.get_loggedInUser();
	
	if(fbid != "") {
		$.ajax({
	        type: "POST",
	        //url: "<?=site_url('users/loginfb')?>",
	        url: site_url+"/users/loginfb/",
	        data: "fbid="+fbid,
	        beforeSend: function() {
	            $("#memberLogin").html('<img src="im/loading_spinner.gif" />'); 
	        },
	        complete: function(data) {
				if (data.responseText == "register") {
					// call lightbox
					//GB_show('Register', '<?=site_url('users/register/')?>/' + fbid, 300,600);
					GB_show('Register', site_url+'/users/register/'+fbid, 397,610);
				} else {
					//$("#memberLogin").html(data.responseText);

					top.location.reload();
				}		
	        }
	    });
	} else {
		alert('Error encountered connecting to facebook. Please refresh your browser!');
	}
}

function logout() {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('users/logout')?>",
        url: site_url+"/users/logout/",
        beforeSend: function() {
            $("#memberLogin").html('Logging out&nbsp;<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            top.location.href = location.href;
        	//$("#memberLogin").html(data.responseText);
        }
    });
}

function updateLatestPost() {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('article/ajaxUpdateLatestPost')?>",
        url: site_url+"/article/ajaxUpdateLatestPost/",
        beforeSend: function() {
            $("#latestpost").html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            //$("#latestpost").css('height', 'auto');
            $("#latestpost").html(data.responseText); 
        }
    });
}

function updatePopularPost() {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('article/ajaxUpdatePopularPost')?>",
        url: site_url+"/article/ajaxUpdatePopularPost/",
        beforeSend: function() {
            $("#popularpost").html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            //$("#popularpost").css('height', 'auto');
            $("#popularpost").html(data.responseText); 
        }
    });
}

function updateGalleries(category_id) {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('article/ajaxUpdateGalleries')?>/"+category_id,
        url: site_url+"/article/ajaxUpdateGalleries/"+category_id,
        beforeSend: function() {
            $("#galleries").html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#galleries").css('height', 'auto'); 
            $("#galleries").html(data.responseText);
        }
    });
}

function updateVideos(category_id) {
    $.ajax({
        type: "POST",
        //url: "<?=site_url('article/ajaxUpdateVidoes')?>/"+category_id,
        url: site_url+"/article/ajaxUpdateVidoes/"+category_id,
        beforeSend: function() {
            $("#videos").html('<img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#videos").css('height', 'auto'); 
            $("#videos").html(data.responseText);
        }
    });
}

function loadDesignerList(alphabet) {
    $.ajax({
        type: "POST",
        url: site_url+"article/ajaxLoadDesignerList",
        data: "alphabet="+alphabet,
        beforeSend: function() {
            $("#directory_content").html('<br /><img src="im/loading_spinner.gif" />'); 
        },
        complete: function(data) {
            //alert(data.responseText);
        	$("#directory_content").html(data.responseText);
        }
    });
}

function changeTab(tab, category_id) {
	if(tab == 'commented') {
		$("#tagged").removeClass('current');
		$("#commented").addClass('current');

		displayMostCommented(category_id);
	} else if(tab == 'tagged') {
		$("#commented").removeClass('current');
		$("#tagged").addClass('current');

		displayMostTagged(category_id);
	} else {
		// default to 'commented' tab
		$("#tagged").removeClass('current');
		$("#commented").addClass('current');

		displayMostCommented(category_id);
	}
}

function displayMostCommented(category_id) {
	$.ajax({
        type: "POST",
        //url: "<?=site_url('article/mostCommentedArticles')?>",
		url: site_url+"/article/mostCommentedArticles/",
        data: "category_id="+category_id,
        beforeSend: function() {
            $("#mostpopular").html('<img src="im/loading_spinner.gif" />');
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#mostpopular").html(data.responseText);
        }
    });
}

function displayMostTagged(category_id) {
	$.ajax({
        type: "POST",
        //url: "<?=site_url('article/mostTaggedArticles')?>",
		url: site_url+"/article/mostTaggedArticles/",
        data: "category_id="+category_id,
        beforeSend: function() {
            $("#mostpopular").html('<img src="im/loading_spinner.gif" />');
        },
        complete: function(data) {
            //alert(data.responseText);
            $("#mostpopular").html(data.responseText);
        }
    });
}

function shareToAM() {
	var link = document.location.href;
	$.ajax({
		type: "POST",
		url: site_url+"/article/ajaxShareToAM/",
		data: "link="+link,
		complete: function(data) {
			var id = data.responseText;
			GB_show('', site_url+'/article/shareToAM/'+id, 300,718);
		}
	});
}

function swap() {
	document.getElementById('passwordLabelOne').style.display = "none";
	document.getElementById('passwordLabelTwo').style.display = "block";
	document.getElementById('user_password').focus();
}

function nextPage(cat_id, page_id, dest) {
	$.ajax({
		type: "POST",
		url: site_url+"/article/ajaxNextPage/",
		data: "cat_id="+cat_id+"&page_id="+page_id+"&dest="+dest,
        beforeSend: function() {
            $("#main").html('<img src="im/loading_spinner.gif" />');
        },
		complete: function(data) {
			$("#main").html(data.responseText);
		}
	});
}

function changePage(section, id, page, divTag) {
	$.ajax({
		type: "POST",
		url: site_url+"/article/ajaxChangePage/",
		data: "section="+section+"&id="+id+"&page="+page,
        beforeSend: function() {
            $("#"+divTag).html('<img src="im/loading_spinner.gif" />');
        },
		complete: function(data) {
			$("#"+divTag).html(data.responseText);
		}
	});
}