function setCompanyName(){
	if(dojo.byId('recruiter').checked===true){
		dojo.byId('name').value=dojo.byId('myEmail').value;
		dojo.byId('name').focus();
		dijit.byId('name').setAttribute('disabled',true);
		dojo.byId('name').blur();
	}else{
		dojo.byId('name').value='';
		dijit.byId('name').setAttribute('disabled',false);
	}
}

function checkEmail(userId,uri){
	var email = dojo.byId("myEmail").value;
	if(email.length <5){ return false }
	dojo.xhrPost( {
       	url: uri+"/user/checkEmail.do",
        handleAs: "text",
        preventCache: true,
        load: function(response, ioArgs) {
          if( response.indexOf("Error") != -1){
          		//alert(response);
				dijit.byId('myEmail').invalidMessage = i18n.getMsg("user.error.check.email");
		  }
        },
        error: function(response, ioArgs) {
          //alert(response);
          return response;
        },
        content: {
			userId : userId,
			email : email
		}
    });
}

function registerMessageTransfer(uri,userType){
	dojo.xhrPost( {
       	url: uri+"/user/registerMessageTransfer.do",
       	form: dojo.byId("formRegisterMessage1"),
        handleAs: "text",
        preventCache: true,
        load: function(response, ioArgs) {
          	if( response.indexOf("Succeed") != -1 ) {
				location.href = uri+ "/user/userRegisterNext.jsp?type="+userType;		
			}else if(response.indexOf("Error") != -1){
				$('widget_myEmail').className +=' dijitError';
				$('widget_confirmEmail').className +=' dijitError';
				showError(response);
			}
        },
        error: function(response, ioArgs) {
          return response;
        }
    });
}


function register(uri,userType){
	dijit.byId("registerButton").setAttribute('disabled',true);
	dojo.xhrPost( {
       	url: uri+"/user/register.do",
       	form: dojo.byId("formRegisterMessage2"),
        handleAs: "text",
        preventCache: true,
        load: function(response, ioArgs) {
          if( response.indexOf("Succeed") != -1 ) {
				location.href = uri+ "/user/registerSucceed.jsp?type="+userType;			
		  }else if(response.indexOf("Error") != -1){				
				showError(response);
		  }
        },
        error: function(response, ioArgs) {
          //alert(response);
          return response;
        }
    });
}


function addressCopy(){
	if(dojo.byId("asPeronalAddress").checked===true){
		dojo.byId("companyCity").focus();
		dojo.byId("companyCity").value=dojo.byId("city").value;
		dojo.byId("companyAddress").focus();
		dojo.byId("companyAddress").value=dojo.byId("address").value;
		dojo.byId("companyState").focus();
		dojo.byId("companyState").value=dojo.byId("state").value;	
		dojo.byId("companyAddress2").focus();
		dojo.byId("companyAddress2").value=dojo.byId("address2").value;
		dojo.byId("companyZip").focus();
		dojo.byId("companyZip").value=dojo.byId("zip").value;
		dojo.byId("companyZip").blur();
	}
}

function confirmAgreement(){
	dijit.byId("registerButton").setAttribute('disabled',!dojo.byId("agreeTermsConditions").checked);
}


function getSubUserList(pageNum , pageSize){
	loading.show(i18n.getMsg("loading"));
	var uri = byId("uri");
	dojo.xhrGet( {
		url: uri+"/user/subUserList.jsp", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			loading.hide();
			 dojo.byId("subUserListDiv").innerHTML = response;
			 dojo.parser.parse(dojo.byId('subUserListDiv'));
		},
        error: function(response, ioArgs) {
        	//alert(response);
        	return response;
        },
		content: {
			pageNum : pageNum,
			pageSize : pageSize,
			companyId: byId("companyId")
		}  
	} );
}

function deleteContract( userId ) {
	dojo.byId("contractId"+userId).value="";
	dojo.byId("resumeLink" + resumeId ).parentNode.removeChild(dojo.byId("resumeLink" + resumeId ));
} 

function changeActiveStatus(userId){
	comfirmMsg(i18n.getMsg("user.confirm.are.you.sure.to.operate.this.data") , function(){
		var uri = byId("uri");
		var pageNum = byId("pageNum");
		var pageSize = byId("pageSize");
		if(pageNum===""){
			pageNum = 1;
			pageSize = 20;
		}
		dojo.xhrGet( {
			url: uri+"/user/changeActiveStatus.do", 
			handleAs: "text",
			preventCache: true,
			load: function(response, ioArgs) {
				if( response.indexOf("Error") != -1 ) {
					showError(response);
				} else {
					window.location.reload();
				}
			},
	        error: function(response, ioArgs) {
	          //alert(response);
	          return response;
	        },
			content: {
				userId : userId
			}      
		} );
	});
}

function saveEditedUser(userId){
	loading.show(i18n.getMsg("saving"));
	var uri = byId("uri");
	dojo.xhrPost( {
		url: uri+"/user/saveEditedUser.do", 
		handleAs: "text",
		preventCache: true,
		form: dojo.byId("formRegisterMessage1"),
		load: function(response, ioArgs) {
			if( response.indexOf("Error") != -1 ) {
				showError(response);
				dojo.byId("confirmEmail").value="";
			} else {
				location.href = uri+ "/user/userProfile.jsp?userId="+userId;
			}
		},
		error: function(response, ioArgs) {
          //alert(response);
          return response;
        }
	} );
}

function changeUserPwd(uri){
	var userId = byId("userId");
	dojo.xhrPost( {
		url: uri+"/user/changeUserPwd.do", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			if( response.indexOf("Succeed") != -1 ){
				loading.showMsg( i18n.getMsg("user.password.edit.successfully") );
				//console.debug( i18n.getMsg("user.password.edit.successfully") );
				//pause();
				delayGoUrl(uri+ "/user/userProfile.jsp?userId="+userId);
				
			}else if(response.indexOf("Error1") != -1){				
				showError(i18n.getMsg("user.error.password.not.correct"));
				dojo.byId("oldPassword").value="";
			}
		},
		error: function(response, ioArgs) {
          //alert(response);
          return response;
        },
		content: {
			userId : userId ,
			oldPassword : dojo.byId("oldPassword").value,
			password : dojo.byId("password").value
		}     
	} );
}

function userLoginInHeader(){
	var uri = byId("uri");
	var email = byId("email");
	if( email == "" ) {
		location.href=uri +"/login.jsp?errorCode=1";
	}
	var password = byId("password");
	if( password == "" ) {
		location.href=uri +"/login.jsp?errorCode=1";
	}
	var remeberMe= dojo.byId("remeberMe");
	//alert(email);
	dojo.xhrPost( {
		url: uri+"/user/login.do",
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			try{
				if( remeberMe.checked ) {
					setCookie("teremeberme","true");
					setCookie("teemail",email);
					setCookie("tepassword",password);
				} else {
					setCookie("teremeberme","false");
					setCookie("teemail","");
					setCookie("tepassword","");
				}
			}catch(e){}
			
			if( response.indexOf("Success") != -1)  {
				//alert("123");
				location.href=uri +"/dashboard/dashboard.jsp";
			}else if( response.indexOf("Failed1") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=1";
			}else if( response.indexOf("Failed2") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=2";
			}else if( response.indexOf("Failed3") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=3";
			}
		},
		content: {
			email : email,
			password: password
		}     
	} );
}

function userLogin(){
	var email = byId("myemail");
	if( email == "" ) {
		//showError(i18n.getMsg("user.error.please.enter.your.email.address"));
		showLoginErr(i18n.getMsg("user.error.please.enter.your.email.address"));
		return;
	}
	var password = byId("mypassword");
	if( password == "" ) {
		//showError(i18n.getMsg("user.please.enter.your.password"));
		showLoginErr(i18n.getMsg("user.please.enter.your.password"));
		return;
	}
	var remeberMe= dojo.byId("remeberMe");
	var uri = byId("uri");
	//alert(email);
	dojo.xhrPost( {
		url: uri+"/user/login.do",
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			try{
				if( remeberMe.checked ) {
					setCookie("teremeberme","true");
					setCookie("teemail",email);
					setCookie("tepassword",password);
				} else {
					setCookie("teremeberme","false");
					setCookie("teemail","");
					setCookie("tepassword","");
				}
			}catch(e){}
			
			if( response.indexOf("Success") != -1)  {
				location.href=uri +"/dashboard/dashboard.jsp";
			}else if( response.indexOf("Failed1") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=1";
			}else if( response.indexOf("Failed2") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=2";
			}else if( response.indexOf("Failed3") != -1 ) {
				location.href=uri +"/login.jsp?errorCode=3";
			}
		},
		content: {
			email : email,
			password: password
		}     
	} );
}


function showLoginErr(errMsg){
	var msgDiv =$('loginerr');
	toggle.show(msgDiv);
	msgDiv.innerHTML =errMsg;
}

function quit(position){
	var uri = byId("uri");
	dojo.xhrGet( {
		url: uri+"/user/quit.do",
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			if(position=="te"){
				location.href=uri+"/login.jsp";
			}else{
				location.href=uri+"/admin/login.jsp";
			}
		}
	} );
}

function sendReferral(){
	var uri = byId("uri");
	loading.show(i18n.getMsg("loading"));
	dojo.xhrPost( {
	   	url: uri+"/user/sendReferral.do",
	   	form: dojo.byId("inviteForm"),
	    handleAs: "text",
	    preventCache: true,
	    load: function(response, ioArgs) {
			if(response.indexOf("Error") != -1){				
				showError(response);
			} else {
				window.location.href= uri +"/referral/referral.jsp";
			}
	    },
	    error: function(response, ioArgs) {
	      //alert(response);
	      return response;
	    }
    });
}

function getReferralList( pageNum , pageSize ) {
	var uri = byId("uri");
	var orderBy = byId("sortByValue");
	loading.show(i18n.getMsg("loading"));
	dojo.xhrGet( {
		url: uri+"/referral/referralList.jsp", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			loading.hide();
			 dojo.byId("referralListDiv").innerHTML = response;
			 dojo.parser.parse(dojo.byId('referralListDiv'));
		},
        error: function(response, ioArgs) {
        	//alert(response);
        	return response;
        },
		content: {
			pageNum : pageNum,
			pageSize : pageSize,
			orderBy : orderBy
		}  
	} );
}

function referralListSortBy(description, fieldName ){
	sortBy(description,fieldName);
	getReferralList(1,20);
}
//click column sort by
function referralListColumnSortBy(obj, fieldName){
	obj = $(obj);
	columnSortByAction(obj,fieldName);
	getReferralList(1,20);
}

function getContactList( pageNum , pageSize ) {
	var uri = byId("uri");
	var orderBy = byId("sortByValue");
	loading.show(i18n.getMsg("loading"));
	dojo.xhrGet( {
		url: uri+"/contact/contactList.jsp", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			loading.hide();
			 dojo.byId("contactListDiv").innerHTML = response;
			 dojo.parser.parse(dojo.byId('contactListDiv'));
		},
        error: function(response, ioArgs) {
        	//alert(response);
        	return response;
        },
		content: {
			pageNum : pageNum,
			pageSize : pageSize,
			orderBy : orderBy
		}  
	} );
}

function contactListSortBy(description, fieldName ){
	sortBy(description,fieldName);
	getContactList(1,20);
}

//click column sort by
function contactListColumnSortBy(obj, fieldName){
	obj = $(obj);
	columnSortByAction(obj,fieldName);
	getContactList(1,20);
}

function deleteContact(contactId) {
	comfirmMsg(i18n.getMsg("job.confirm.msg") , function(){
		var uri=byId("uri");
		loading.show(i18n.getMsg("loading"));
		dojo.xhrPost( {
			url: uri+"/user/deleteContact.do", 
			handleAs: "text",
			preventCache: true,
			load: function(response, ioArgs) {
				if( response.indexOf("Succeed") != -1 ) {
					 loading.show(response,1500);
					 location.href= uri + '/contact/contact.jsp';
					 loading.hide();
				}
				if( response.indexOf("Error") != -1 ) {
					 showError(response);
				}
			},
			content : {
				contactId:contactId
			}
		} );
	});
}

//modify by Amilim 2008-11-21
function onkeydownTotal(min,max,strValue){
   var maxl=max;
   var min1=min;
   	   strValue = formatHTML(strValue.clearHTML());
   var s=strValue.length +1;
   if(s>maxl){
   		dojo.byId("companyDescriptionTextarea").style.backgroundColor =errBgColor;
   		dojo.byId("num").innerHTML="<span class='red'>" + s+"</span>/"+maxl+" " + i18n.getMsg("user.characters");
   	}else{
   		dojo.byId("num").innerHTML=s+"/"+maxl+" " + i18n.getMsg("user.characters");
   		dojo.byId("companyDescriptionTextarea").style.backgroundColor ="#fff";
   	}
}

/**
 * set user locale saveUserLocal(local)
 * @param String local
 * @verson 20081125 By Amilim
 */
function saveUserLocal(local){
	var uri = byId("uri");
	dojo.xhrGet( {
		url: uri+"/user/saveUserLocal.do", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			if( response.indexOf("Succeed") != -1 ) {
				location.reload(); 
			}
			if( response.indexOf("Error") != -1 ) {
				showError(response);
			}
		},
		content : {
			local:local
		}
	} );
}

/**
 * To create a set of sub-users
 * */
function createSubUser(){
	var uri=byId("uri");
	loading.show(i18n.getMsg("saving"));
	dojo.xhrPost( {
		url: uri+"/user/createSubUser.do", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			if( response.indexOf("Error") != -1 ) {
				 showError(response);
			} else {
				loading.show(response,1500);
				location.href= uri + '/user/user.jsp';
				loading.hide();
			}
		},
		form: dojo.byId("subUserForm"),
		content : {
		}
	} );
}

//click sort by
function userSortBy(description, fieldName ){
	sortBy(description,fieldName);
	getMsgInboxList(1,20);
	getUserListByCompany(1,20)
}

//click column sort by
function userColumnSortBy(obj,fieldName){
	obj = $(obj);
	columnSortByAction(obj,fieldName);
	getUserListByCompany(1,20)
}

function getUserListByCompany(pageNum,pageSize){
	var uri = byId("uri");
	loading.show(i18n.getMsg("loading"));
	dojo.xhrGet( {
		url: uri+"/user/userList.jsp", 
		handleAs: "text",
		preventCache: true,
		load: function(response, ioArgs) {
			 dojo.byId("displayUserDiv").innerHTML = response;
			 dojo.parser.parse(dojo.byId('displayUserDiv'));
			 loading.hide();
		},
        error: function(response, ioArgs) {
        	//alert(response);
        	return response;
        },
		content: {
			pageNum : pageNum,
			pageSize : pageSize,
			orderby:byId("sortByValue"),
			companyId:byId("companyId")
		}  
	} );
}


/**
 * Reset user password with random 6 bytes character and send Email to notice.
 * @param userId who will reset password
 * */
function resetPassword(userId) {
	comfirmMsg(i18n.getMsg("user.reset.password.confirm") , function(){
		loading.show(i18n.getMsg("saving"));
		var uri = byId("uri");
		dojo.xhrGet( {
			url: uri+"/user/resetPassword.do", 
			handleAs: "text",
			preventCache: true,
			load: function(response, ioArgs) {
				if( response.indexOf("Error") != -1 ) {
					showError(response);
				} else {
					loading.show(response,1500);
					loading.hide();
				}
			},
			content: {
				userId:userId
			}  
		} );
	})
}
