/**
 * @author Preetham
 */

var startPageForCandidate = "/index.html";
var landingPageForCandidate = "searchReq.html";


var startPageForEmployer = "/index.html";
var landingPageForEmployer = "searchProfile.html";



function login() {
	
	$("span#busyImgCont").html("<img src='image/busy.gif' id='busyImg' />");
	
	var firstElem;
	
	var myElements=new Array("#loginId","#loginPassword");
	$.each(myElements,function(i,n){
		if(!checkRequired($(n))){
			firstElem = !firstElem ? n : firstElem;
		}
	});
	
	if(!firstElem) {
		//all elements are filled, check the email is valid
		var elem = "#loginId";
		if(!checkEmail(elem)) {
			firstElem = elem;
		}
	}
	if(firstElem) {
		$(firstElem).focus();
		$("span#busyImgCont").empty();
	}else {
		//valid entry block ui and call login 
		$.blockUI();
		JOPTDWRGateway.login($(myElements[0]).val(),$(myElements[1]).val(),
		{	callback:function(data){

				$.each(myElements,function(i,n){
					$(n).val("");
				});
				//logged successfully going to forward;
				document.location = landingPageForCandidate;	
			},
			errorHandler:function(errorString, exception){
				$("span#busyImgCont").empty();
				if(errorString.indexOf("900") == 0) {
					$.blockUI({ message:$('#authFailedDlg') });	
				}else if(errorString.indexOf("925") == 0) {
					$.blockUI({
						 message:$('#notActivatedDlg')
					});	
				}else {
					$.blockUI({
						 message:$('#unknownErrDlg')
					});	
				}
				
				//alert(errorString);
				
			}
		}
		
		);
	}
}


function forgotPassword() {
	
	$("span#fpBusyImgCont").html("<img src='image/busy.gif' id='busyImg' />");
	
	var firstElem;
	
	var myElements=new Array("#fpLoginId");
	$.each(myElements,function(i,n){
		if(!checkRequired($(n))){
			firstElem = !firstElem ? n : firstElem;
		}
	});
	
	if(!firstElem) {
		//all elements are filled, check the email is valid
		var elem = "#loginId";
		if(!checkEmail(elem)) {
			firstElem = elem;
		}
	}
	if(firstElem) {
		$(firstElem).focus();
		$("span#fpBusyImgCont").empty();
	}else {
		//valid entry block ui and call login 
		$.blockUI();
		JOPTDWRGateway.forgotPassword($(myElements[0]).val(),
		{	callback:function(data){
				$("span#fpBusyImgCont").empty();
				//Sent mail successfully show the success dialog;
				$.blockUI({ message:$('#fpSuccDlg') });
					
			},errorHandler:function(errorString, exception){
				$("span#fpBusyImgCont").empty();
				if(errorString.indexOf("940") == 0) {
					$.blockUI({ message:$('#fpFailedDlg') });	
				}else {
					$.blockUI({
						 message:$('#unknownErrDlg')
					});	
				}
				
			}
		});
	}//end of outer else
}//end of forgotpassword

function checkLogin(myCallBackFunction){
 	JOPTDWRGateway.checkLogin(function(candidate){
		if(candidate == null) {
			handleInvalidCandidateSession();
		}else {
			//already logged in
			if(myCallBackFunction != null) {
				myCallBackFunction(candidate);	
			} else {
				return candidate;
			}
			
		}
		
	});
 }



function logout(){
	
	JOPTDWRGateway.logout(function(data){
		document.location = startPageForCandidate;
	});
} 

function handleInvalidCandidateSession(){
	alert("Sorry, your session has expired. Please login again.");
	document.location = startPageForCandidate;
}

function recruiterLogin(termOtherSession) {
	
	if(!termOtherSession) {
		termOtherSession = "false";
	}
	var firstElem;
	
	var myElements=new Array("#loginId","#loginPassword");
	$.each(myElements,function(i,n){
		if(!checkRequired($(n))){
			firstElem = !firstElem ? n : firstElem;
		}
	});
	
	if(!firstElem) {
		//all elements are filled, check the email is valid
		var elem = "#loginId";
		if(!checkEmail(elem)) {
			firstElem = elem;
		}
	}
	if(firstElem) {
		$(firstElem).focus();
		$("span#busyImgCont").empty();
	}else {
		//valid entry call login
		JOPTDWRGateway.recruiterLogin($(myElements[0]).val(),$(myElements[1]).val(), termOtherSession,
		{
			callback: function(data){
				//alert("Successfully logged in");
				
				$.each(myElements, function(i, n){
					$(n).val("");
				});
				
				$("span#busyImgCont").empty();
				//alert("logged successfully going to forward");
				document.location = landingPageForEmployer;
			},errorHandler:function(errorString, exception){
				$("span#busyImgCont").empty();
				if(errorString.indexOf("1000")== 0) {
					//show the sess present dialog
					$.blockUI({ message:$('#sessPresDialog') });	
				}else if(errorString.indexOf("900")== 0){
					//auth failed
					$.blockUI({ message:$('#authFailedDlg') });	
				}else {
					//unknown error
					$.blockUI({ message:$('#unknownErrDlg') });
				}
				
				
			}
		});
	}
}

function checkRecruiterLogin(myCallBackFunction){
 	JOPTDWRGateway.checkRecruiterLogin(function(companyLoginAccount){
		if(companyLoginAccount == null) {
			handleInvalidEmployerSession();
		}else {
			//already logged in
			if(myCallBackFunction != null) {
				myCallBackFunction(companyLoginAccount);	
			} else {
				return companyLoginAccount;
			}
			
		}
		
	});
 }

function recruiterLogout(){
	
	JOPTDWRGateway.logout(function(data){
		document.location = startPageForEmployer;
	});
} 

function handleInvalidEmployerSession(){

	alert("Sorry, your session has expired. Please login again");
	document.location = startPageForEmployer;
	
}

