/*
 *login script
 *sends user into a new window with all the vars required for ma index pasge
*/

function goToLogin(){	
	//check screen resolution
	var thaBrowser=navigator.userAgent;
	var myHeight=screen.height;
	var myWidth=screen.width;
	if(thaBrowser.indexOf("MSIE 6")>-1){
		myHeight = screen.height - 50;
		myWidth = screen.width - 6;
	}
	if(thaBrowser.indexOf("MSIE 7")>-1){
		myWidth = screen.width - 8;
		if(thaBrowser.indexOf("MSIE 7.0.5")>-1){
			myHeight = screen.height - 80;
		}else{
			myHeight = screen.height - 65;
		}
	}
	
	//load to page
	if(screen.width>=1024){
		window.open(JSmainPath+'login/logincheck.asp', 'popup', 'toolbar=0,top=0,left=0,target=_blank,width='+myWidth+',height='+myHeight+',scrollbars=no');
	}else{
		top.location.href = JSmainPath+"resolution_warning.asp";
	}	
}
function launchTB(myh,myw){	
	tb_show("My Agent System Login", JSmainPath+"login/system_login.asp?myheight="+myh+"&mywidth="+myw+"&TB_iframe=true&height=210&width=250&modal=true");
}
function validateuser(myh,myw){	
	$("#loginchk").hide();
	$("#loginloader").show();
	var mustrun = true;
	if($('#usr').val() == ""){
		alert("Warning!\nYou have not entered your username. Please enter your username.");
		$("#loginchk").show();
		$("#loginloader").hide();
		mustrun = false;
	}
	if($('#pwd').val() == ""){
		alert("Warning!\nYou have not entered your password. Please enter your password.");
		$("#loginchk").show();
		$("#loginloader").hide();
		mustrun = false;
	}
	if(mustrun == true){
		$.post(JSmainPath+"login/validate.asp",{usr:$('#usr').val(),pwd:$('#pwd').val()},function(retval){
				
				switch(retval){						
					case "1":						
						top.location.href = JSmainPath+"private/index.asp?myHeight="+myh+"&mywidth="+myw;
						parent.tb_remove();
					break;
					case "6":
						top.location.href = JSmainPath+"private/demoexpired.asp?myHeight="+myh+"&mywidth="+myw;
						parent.tb_remove();
					break;
					case "2":
						alert(":::Warning::: Login Failed \n Username and password are incorrect \n Make sure you are using the correct case");
						$("#loginchk").show();
						$("#loginloader").hide();
					break;
					case "3":
						alert(":::Warning::: Your account is locked for at least 30 minutes \n Only one user is allowed to use this account at a time");
						$("#loginchk").show();
						$("#loginloader").hide();
					break;
					case "4":
						alert(":::Warning::: There was another user currently logged in \n Only one user is allowed to use this account at a time \n Your account is locked for 30 minutes");
						$("#loginchk").show();
						$("#loginloader").hide();
					break;
					case "5":
						alert(":::Warning::: Your user account has been deactivated \n Contact your administrator to activate your account");
						$("#loginchk").show();
						$("#loginloader").hide();
					break;					
					default:
						alert(":::Warning::: Problem logging in \n If this problem continues please contact My Agent 0860 69 2468");
						$("#loginchk").show();
						$("#loginloader").hide();
					break
				}			
			}
		);
	}
}
function forgot(){
	$("#passwordchk").hide();
	$("#passwordloader").show();
	var mustrun = true;
	if($('#fusr').val() == ""){
		alert("Warning!\nYou have not entered your username. Please enter your username.");
		$("#passwordchk").show();
		$("#passwordloader").hide();
		mustrun = false;
	}
	if($('#femail').val() == ""){
		alert("Warning!\nYou have not entered your email correctly. Please enter your email.");
		$("#passwordchk").show();
		$("#passwordloader").hide();
		mustrun = false;
	}
	if(mustrun == true){
		$.post(JSmainPath+"login/resetpassword.asp",{usr:$('#fusr').val(),email:$('#femail').val(),stopSpider:"ok"},function(retval){
				var arr  =new Array(2);
				arr = retval.split(",");			
				if(arr[0]=="0"){
					
					$.post(JSmainPath+"send.asp",{s:$('#femail').val(),f:"My Agent" ,e:"info@myagent.co.za",subj:"My Agent Password Reset" ,b:"Your My Agent password has been reset.<br><br> To log into the system please use the following password: "+arr[1]+" <br><br><a href='http://www.myagent.co.za'>www.myagent.co.za</a><br><br>Please contact info@myagent if you have any questions<br><br>Regards<br>My Agent<br>Tel: 0860 MY AGNT" },function(data){
						alert("Your password has been reset and emailed to the following address \n "+$('#femail').val()+"");
						$("#passwordchk").show();
						$("#passwordloader").hide();
					});
				}else{					
					alert(":::Warning::: incorrect details \n Username and email do not exist ");
					$("#passwordchk").show();
					$("#passwordloader").hide();
				}
				
			}
		);
	}
}
function frmChkr2(){
	if(document.forgotpwd.usr.value == ""){
		alert("Warning!\nYou have not entered your username. Please enter your username.");
		document.forgotpwd.usr.focus();
		return false;
	}else{
		if (((document.forgotpwd.email.value != "")&&document.forgotpwd.email.value.length > 0 && (document.forgotpwd.email.value.indexOf("@",0) == - 1 || document.forgotpwd.email.value.indexOf(".",0) == - 1))) { 
			alert("Warning!\nYou have not entered a valid email address.\nPlease enter a valid email address.");
			document.forgotpwd.email.focus();		
		}
		else{
			document.forgotpwd.stopSpider.value = "ok";
			return true;
		}
	}	
}
function show(id){
	var i;
	var nm=2;
	for(i=1;i<=nm;i++){
		var tag = document.getElementById(i);
		if(i==id){
			tag.style.display = '';
		}
		else{
			if(tag.style.display == ''){
				tag.style.display = 'none';
			}
		}
	}
}
function recommendFireFox(){
	top.parent.location.href = "http://www.mozilla.com/";
	parent.tb_remove();
}