/*
 * This file ONLY provides common and cross-page-reused link functions.
 * 
 * Please add detail description of your function before you created.
 * Also, please add necessary comment when you modified.
 * 
 * Jim.Bi
 */

/**
 * This method will forward to the corresponding page with the specified
 * parameter page.
 */

function _goPage(page){
	window.location.href = page;
}

/**
 * This method will forward to home page.
 */
function gotoHomePage(){	
	chatChangePage(function() {
		_goPage(ctx+"/");
	})
}

function gotoHomePageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	gotoHomePage();
    	},
    	error : function() {		    
	    	gotoHomePage();
		}
	});
}

/**
 * This method will forward to group detail page.
 */
function gotoViewGroup(url){
	_goPage(ctx+"/groups/"+url);
}

/**
 * This method will forward to group index page.
 */
function gotoGroupIndexPage(){
	chatChangePage(function() {
		_goPage(ctx+"/group/index.u");
	})
}

function gotoGroupIndexPageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	gotoGroupIndexPage();
    	},
    	error : function() {		    
	    	gotoGroupIndexPage();
		}
	});
}

/**
 * This method will forward to group home page with the specified parameter
 * groupId.
 */
function gotoGroupHomePage(groupId){
	_goPage(ctx+"/group/home.u?gid="+groupId);
} 

/**
 * This method will forward to group home page with the specified parameter
 * groupId.
 */
function gotoGroupUrlPage(groupUrl){
	_goPage(ctx+"/groups/"+groupUrl);
}

/**
 * This method will forward to shopping cart page.
 */
function gotoShoppingCartPage(){	
	chatChangePage(function() {
		_goPage(ctx+"/viewShoppingCart.u");
	})
}

function gotoShoppingCartPageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	gotoShoppingCartPage();
    	},
    	error : function() {		    
	    	gotoShoppingCartPage();
		}
	});
}

/**
 * This method will forward to account page.
 */
function gotoAccountSummary(){
	chatChangePage(function() {
		_goPage(ctx + "/getAccountSummary.u");
	})
}

function gotoAccountSummaryWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	gotoAccountSummary();
    	},
    	error : function() {		    
	    	gotoAccountSummary();
		}
	});
}

/**
 * This method will forward to user profile page.
 */
function getProfile(userIdParam) {
	getInstructorProfile(userIdParam);
}

/**
 * This method will forward to user profile page.
 */
function getInstructorProfile(userIdParam) {
	window.name = "";
	//_goPage(ctx + "/showPreviewProfilePage.u?userIdName=" + userIdParam);
	_goPage(ctx + "/showPreviewProfile.u?userIdName=" + userIdParam);
}

/**
 * @author Space 2008-9-24 Call Course User Screen
 * @param userIdParam
 * @return
 */

function getCourseProfile(courseIdVar) {
	_goPage(ctx + "/courseprofile.u?courseId=" + courseIdVar);
}

function viewObject(objectId, objectType, isanable, status) {
	var obtype = objectType.toLowerCase(); 
	if (status != null && status != "") {
		if (obtype == "lo") {
			this.location = "file_main_temp.jsp?loId=" + objectId;
		}
		if (obtype == "lesson") {
			this.location = "manage_main.jsp?type=lesson&lessonId="+objectId;
		}

	} else {

		if (obtype == "lesson") {
			viewroom(objectId, isanable);
		}
		if (obtype == "course") {
			getCourseProfile(objectId);
		}
	}
}

function viewObjectWithButtonHistory(page,feature,objectId, objectType, isanable, status) {
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	viewObject(objectId, objectType, isanable, status);
    	},
    	error : function() {		    
	    	viewObject(objectId, objectType, isanable, status);
		}
	});
}

function viewroom(index, isanable, prevPage, fromCourse) {
	_goPage(ctx + "/classroom.u?learningObjectID2=" + index
			+ "&prevPage=" + prevPage + "&from=" + fromCourse);
}

function getWorkProgress() {
	_goPage(ctx + "/getProgressWork.u");
}

function getViewLessons() {
	_goPage(ctx + "/getViewLessons.u");
}

function getPublishLessons() {
	_goPage(ctx + "/getPublishLessons.u");
}

function getBookmarketedLessons() {
	var page = "Learn/My Stuff/recently bookmarked lessons and courses";
	var feature = "# of clicks on 'Show All'";
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	_goPage(ctx + "/getBookmarketedLessons.u");
    	},
    	error : function() {		    
	    	_goPage(ctx + "/getBookmarketedLessons.u");
		}
	});		
}

function getPurchasedLessons() {
	var page = "Learn/My Stuff/Recently Acquired Lessons and Courses";
	var feature = "# of clicks on 'Show All'";
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	_goPage(ctx + "/getPurchasedLessons.u");
    	},
    	error : function() {		    
	    	_goPage(ctx + "/getPurchasedLessons.u");
		}
	});	
}

function getViewedLessons() {
	var page = "Learn/My Stuff/Recently Viewed Lessons and Courses";
	var feature = "# of clicks on 'Show All'";
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	_goPage(ctx + "/getViewedLessons.u");
    	},
    	error : function() {		    
	    	_goPage(ctx + "/getViewedLessons.u");
		}
	});	
	
}

function getRatingPage(varLessonId, varBackPage) {
	_goPage(ctx + "/rating_summary.jsp?lessonId=" + varLessonId
			+ "&backUrl=" + varBackPage);
}

function goHelpPage(page,feature) {
	var helpPage = ctx+"/tour/howto_before.jsp"; 
	/*
	if ("myStuff" == currentPage)
		helpPage = helpPath + "My_Stuff.htm";
	else if ("createPage" == currentPage)
		helpPage = helpPath + "Create.htm";
	else if ("learnPage" == currentPage)
		helpPage = helpPath + "Learn.htm";
	else if ("managePage" == currentPage)
		helpPage = helpPath + "Manage.htm";
	else if ("groupPage" == currentPage)
		helpPage = helpPath + "Groups.htm";
	else
		helpPage = ctx + "/footer/help.jsp";
		window.open(helpPage, 'newwindow');
	*/
	
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	chatChangePage(function() {
				_goPage(helpPage);
			});
    	},
    	error : function() {		    
	    	chatChangePage(function() {
				_goPage(helpPage);
			});
		}
	});
	
	
}

function GotoEditProfile(){
	var page = "Learn/My Stuff/Dashboard/User Profile";
	var feature = "Edit Profile";
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	window.location.href = ctx+"/editUserProfile.u";
    	},
    	error : function() {		    
	    	window.location.href = ctx+"/editUserProfile.u";
		}
	});	
}

function getMaillingPage() {
	chatChangePage( function() {
		_goPage(ctx + "/showMailGroups.u");
	});
}

function getPlayListPage() {
	chatChangePage( function() {
		_goPage(ctx + "/playlist.u");
	});
}

function getPlayListPageWithButtonHistory(page,feature) {
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getPlayListPage();
    	},
    	error : function() {		    
	    	getPlayListPage();
		}
	});
}

function getShoppingCartPage() {
	chatChangePage( function() {
		_goPage(ctx + "/viewShoppingCart.u");
	});
}

function getManagePage(type) {
	chatChangePage( function() {
		_goPage(ctx + "/manage_main.jsp?type=" + type);
	});
}

function getFileMainPage() {
	chatChangePage( function() {
		_goPage(ctx + "/file_main_quick.jsp");
	});
}

function getAboutUsPage() {
	chatChangePage( function() {
		_goPage(ctx + "/footer/aboutUs.jsp");
	});
}

function getAboutUsPageWithButtonHistory(page,feature) {
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getAboutUsPage();
    	},
    	error : function() {		    
	    	getAboutUsPage();
		}
	});
}

function getGettingStartedPage() {
	/*chatChangePage( function() {
		_goPage(ctx + "/footer/gettingStarted.jsp");
	});*/
	window.open(ctx + "/footer/gettingStarted.jsp");
}

function getLegalPage() {
	chatChangePage( function() {
		_goPage(ctx + "/footer/termsOfService.jsp");
	});
}

function getLegalPageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getLegalPage();
    	},
    	error : function() {		    
	    	getLegalPage();
		}
	});
}

function getPressPage() {
	chatChangePage( function() {
		_goPage(ctx + "/footer/press.jsp");
	});
}

function getPressPageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getPressPage();
    	},
    	error : function() {		    
	    	getPressPage();
		}
	});
}

function getMyLearningPage() {
	chatChangePage( function() {
		_goPage(ctx + "/my_learning_experience.u");
	});
}

function getMyLearningPageWithButtonHistory(page,feature){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getMyLearningPage();
    	},
    	error : function() {		    
	    	getMyLearningPage();
		}
	});
}

function getMyTeachingPage() {
	chatChangePage( function() {
		if (isLogin()) {
			_goPage(ctx + "/my_teaching_experience.u");
		} else {
			_goPage(ctx + "/jit.u?r=/my_teaching_experience.u");
		}
	});
}

function getMyTeachingPageWithButtonHistory(page,feature) {
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	getMyTeachingPage();
    	},
    	error : function() {		    
	    	getMyTeachingPage();
		}
	});
}

function getInstructorReport() {
	chatChangePage( function() {
		if (isLogin()) {
			_goPage(ctx + "/getInstructorReport.u");
		} else {
			_goPage(ctx + "/jit.u?r=/getInstructorReport.u");
		}
	});
}


function goFileMain() {
	chatChangePage( function() {
		if (isLogin()) {
			_goPage(ctx + "/file_main.jsp");
		} else {
			_goPage(ctx + "/jit.u?r=/file_main.jsp");
		}
	});
}

function goManage() {
	chatChangePage( function() {
		if (isLogin()) {
			_goPage(ctx + "/manage_main.jsp?r="
					+ Math.floor(Math.random() * 100));
		} else {
			_goPage(ctx + "/jit.u?r=/manage_main.jsp");
		}
	});
}

function showAccount(type) {
	chatChangePage( function() {
		_goPage(ctx + "/show.u?isUserAccount=" + type);
	});
}

function getAccountSummary(varTeachPage) {
	if(varTeachPage=='Y'){
		_goPage(ctx + "/my_teaching_experience.u");
	}else{
		chatChangePage( function() {
			_goPage(ctx + "/getAccountSummary.u");
		})
	}
}

function goCategory(code, searchType) {
	chatChangePage( function() {
		_goPage(ctx + "/search.u?category=" + code + "&searchType=" + searchType);
	})
}

function goCategoryWithButtonHistory(page,feature,code,searchType){
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	goCategory(code, searchType)
    	},
    	error : function() {		    
	    	goCategory(code, searchType)
		}
	});
}


function getOutstandingBalance() {
	_goPage(ctx + "/getOutstandBalance.u");
}

function getOverdueBalance() {
	_goPage(ctx + "/getOverdueBalance.u");
}

function getPurchaseActivity() {
	_goPage(ctx + "/getPurchaseActivity.u");
}

function getSalesActivity() {
	_goPage(ctx + "/getSalesActivity.u");
}

function getDisbursementStatement() {
	_goPage(ctx + "/getDisbursementStatement.u");
}

function giftLesson(itemId, iId, responseUrl) {
	if(giftOnClick==false){
		giftOnClick=true;
		chatChangePage( function() {
			_goPage(ctx + "/add2Cart.u?isGift=1&orderType=0&itemId="
					+ itemId + "&iId=" + iId + "&responseUrl=" + responseUrl);
		});
	}
}

function giftCourse(itemId, iId, responseUrl) {
	if(giftOnClick==false){
		giftOnClick=true;
		chatChangePage( function() {
			_goPage(ctx + "/add2Cart.u?isGift=1&orderType=1&itemId="
					+ itemId + "&iId=" + iId + "&responseUrl=" + responseUrl);
		});
	}
}

function goInstructorRating(userId) {
	_goPage(ctx + "/lessonRatingsPage.u?userId=" + userId);
}


function forwardViewPage(type,expirationDate,itemId) {	
	if(expirationDate==null || expirationDate=="") {
		if(type=='lesson') {
			window.location.href=ctx +"/classroom.u?learningObjectID2="+itemId;
		}
		if(type == 'course') {
			getCourseProfile(itemId);
		}
		if(type == 'group') {
			window.location.href = ctx+"/group/home.u?gid="+itemId;
		}
		return;
	}
	
    var times = new Array;
	var datearray = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05','Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'};
	if(expirationDate == 'Unlimited'){
		expirationDate = 'Jan 01,2030';
	}
	if(expirationDate.length == 11){
		 times[0] = datearray[expirationDate.substring(0,3)];
		 times[1] = expirationDate.substring(4,5);
		 times[2] = expirationDate.substring(7,11);
	} else if (expirationDate.length == 12){
		 times[0] = datearray[expirationDate.substring(0,3)];
		 times[1] = expirationDate.substring(4,6);
		 times[2] = expirationDate.substring(8,12);
	}
	var expirate = new Date(times[2]+"/"+times[0]+"/"+times[1]);
	if(Date.parse(expirate) >= Date.parse(new Date())) {
		if(type=='lesson') {
			window.location.href=ctx +"/classroom.u?learningObjectID2="+itemId;
		}
		if(type == 'course') {
			getCourseProfile(itemId);
		}
		if(type == 'group') {
			window.location.href = ctx+"/group/home.u?gid="+itemId;
		}
	}else {
		if(type == 'lesson') {
			//alert("The Lesson is expired");
			alertMessageInfo("The lesson has expired.",'');
			return;
		}
		if(type == 'course') {
			// alert("The Course is expired");
			alertMessageInfo("The course has expired.",'');     
			return;
		}
		if(type == 'group') {
			//alert("The Group is expired");
			alertMessageInfo("The group access has expired.",'');     
			return;
		}
	}	
	
}