/**
 * @author Preetham
 */

//thickbox dialog setup
var href="#TB_inline?height=80&amp;width=300&amp;inlineId=profCreateWaitDlg&amp;modal=true";

var loginDetail = null;
var candidateProfile = null;
var fileUploadReqd = true;

var landingPageForCandidate = "searchReq.html";

	$(document).ready(function(){
		
		$("#divStep2, #divStep3").hide();

		//hookup the resume upload type select radio
		$("input.resumeTypeSelec").click(toggleFileUploadDiv);

		//select the upload radio as default
		$("input.resumeTypeSelec").filter("[value='upload']").attr('checked', 'checked');
		
		//close the error dlg unblockui when close button is clicked
		$("#profCreateErrDlgCloseBtn").click(function(){
			$.unblockUI();
		});
			
		$("#gts2").click(function(){
			loginDetail = getLoginDetailsFromSignUpForm();
			if(loginDetail != null) {
				$("#divStep1").fadeOut("slow" ,function(){
					$("#divStep2").fadeIn("slow");
				});

			}
			
		});
	
		$("#gts1").click(function(){
			$("#divStep2").fadeOut("slow", function(){
				$("#divStep1").fadeIn("slow");		
			});
			
		});
		
		//enable form validation
		var validator = $("#candProfileForm").validate({
			rules:{
				prof1:{
					required:"#skill1:filled"
				},
				prof2:{
					required:"#skill2:filled"
				}
			}
		});
		$("input.phone").mask("9999999999");
		$("input.zipcode").mask("99999");

		$("#signUp").click(function(){
			handleSignUp(validator);
		});

	});	
	
function handleSignUp(validator){
	//reset reseume error message if any
	$("span#resumeErrMsgSpan").text("");

	var valid = $("#candProfileForm").valid();
	if (valid) {
		var resume = checkResume();
		if(resume) {
			candidateProfile = getCandidateProfileDetailsFromForm();
			//show the dialog
			startProfileCreation();	
		} 
	}else {
		// invalid , focus on the invalid element
		validator.focusInvalid();				
	}			
	
}
function toggleFileUploadDiv(){
	
	//reset error message if any
	$("span#resumeErrMsgSpan").text("");
	
	//clear any previously selected file or copied and pasted text
	$("#fileUploadForm")[0].reset();
	$("textArea#aboutTxtArea").val("");
	
	var selVal = $(this).val();
	
	if(selVal == "upload"){
		$("div#resCopyDiv").hide(function(){
			$("div#resUploadDiv").show("slow");	
		});
	}else if(selVal == "copy"){
		$("div#resUploadDiv").hide(function(){
			$("div#resCopyDiv").show("slow");	
		});
	}
}

function startProfileCreation(){
	//tb_show(false,href,false);
	$.blockUI({ message: $("#profCreateWaitDlg") });
	var proceedFurther = false;
	JOPTDWRGateway.isCandidateEmailIdAvailable(loginDetail.email,
		{ 	callback:function(data){
				if(data) {
					//email Id available
					//alert("Good  proceedFurther ");
					if(fileUploadReqd) {
						//need to upload the resume, update the dialog
						$("#profCreateWaitDlgMsg").text("Email Id is available,Starting file upload ..");
						$("#fileUploadForm").submit();	
					}else {
						//resume cut and paste, so call createProfile, update the dialog 
						$("#profCreateWaitDlgMsg").text("Storing your profile ..");
						createProfile();
					}
				}else {
					//not available
					//tb_remove();
					//$.unblockUI();
					$("#profCreateErrDlgMsg").text("Sorry the email Id already exist. Please enter another Id");
					$.blockUI({ message: $("#profCreateErrDlg") });
					//alert("Sorry the email Id already exist. Please enter another Id");
					$("#divStep2").fadeOut("slow", function(){
						$("#divStep1").fadeIn("slow", function(){
							$("#email").focus();		
						});		
					});
				}
			},errorHandler:function(errorString, exception){
				//tb_remove();
				//$.unblockUI();
				if (errorString.indexOf("800") == 0) {
					$("#profCreateErrDlgMsg").text("Apologies, please try again.If this error persists please call us.");
					$.blockUI({ message: $("#profCreateErrDlg") });
				}else {
					$("#profCreateErrDlgMsg").text("Sorry, an error has occurred, please try again");
					$.blockUI({ message: $("#profCreateErrDlg") });
				}
			}
		});
	
}

function fileUploadCallback(status,message){
	//alert("Status is " + status);
	if(status == "failure") {
		//tb_remove();
		//$.unblockUI();
		//alert("Message " + message);
		$("#profCreateErrDlgMsg").text(message);
		$.blockUI({ message: $("#profCreateErrDlg") });
	}else if(status == "success") {
		//alert("File uploaded, proceed with Creating profile.");
		$("#profCreateWaitDlgMsg").text("File Uploaded, Storing your profile ..");
		createProfile();
	}else {
		//tb_remove();
		//$.unblockUI();
		//alert("Apologies, we have encounterend an error while uploading file, please try again or contact us for assitance.")		
		$("#profCreateErrDlgMsg").text("Apologies, we have encounterend an error while uploading file, please try again or contact us for assitance.");
		$.blockUI({ message: $("#profCreateErrDlg") });
		
	}
}

function createProfile(){
	JOPTDWRGateway.createAccount(loginDetail, candidateProfile,
	{ 	
		callback:function(data){
			//profile create successfully
			$("#divStep2").hide();
			//tb_remove();
			$.unblockUI();
			$("#divStep3").show();
			//document.location = landingPageForCandidate;
		},errorHandler:function(errorString, exception){
			//tb_remove();
			//$.unblockUI();
			if (errorString.indexOf("950") == 0) {
				//alert("Sorry the email Id already exist. Please enter another Id");
				$("#profCreateErrDlgMsg").text("Sorry the email Id already exist. Please enter another Id.");
				$.blockUI({ message: $("#profCreateErrDlg") });
				
				$("#divStep2").fadeOut("slow", function(){
					$("#divStep1").fadeIn("slow", function(){
						$("#email").focus();		
					});		
				});
				
			}else {
				//alert("Sorry An error has occurred " + errorString);
				$("#profCreateErrDlgMsg").text("Sorry An error has occurred. Please try again.");
				$.blockUI({ message: $("#profCreateErrDlg") });
				
			}
		}
	});
	
}	

function checkResume(){
	var status = false;
		//$("input.resumeTypeSelec").click(toggleFileUploadDiv);	
		var selVal = $(":radio").filter("[name='resumeTypeSelec']").filter(":checked").val();
		if(selVal == "upload") {
			fileUploadReqd = true;
			if($("input#file1").val().length == 0) {
				$("#resumeErrMsgSpan").text("Please select a file.");	
			} else {
				status = true;
			}
		}else if (selVal == "copy") {
			fileUploadReqd = false;
			if($("textarea#aboutTxtArea").val().length == 0) {
				$("#resumeErrMsgSpan").text("Please copy and paste your resume.");	
			}else {
				status = true;
			}
		}
	return status;
}
