
function is_email(str){
	var str = $.trim (str) ;
        if (str.indexOf(" ") != -1){
                return false;
        }else if (str.indexOf("@") == -1){
                return false;
        }else if (str.indexOf("@") == 0){
                return false;
        }else if (str.indexOf("@") == (str.length-1)){
                return false;
        }
        var arrayString = str.split("@");
        if (arrayString[1].indexOf(".") == -1){
                return false;
        }else if (arrayString[1].indexOf(".") == 0){
                return false;
        }else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
                return false;
        }
        return true;
}


function submitLogin(){
	$("#gobtn").unbind("click",submitLogin);
	$.post('loginTest.php',{email:$("#EMAIL").val()},function(data,textStatus){
		if (data.indexOf('newsystem') > -1) {
			if($("#tmp_login_form").length){
				document.body.removeChild(document.getElementById('tmp_login_form'))
			}
			var f = document.createElement('FORM');
			f.action ='http://dmvedu.org/course/content/page/userfront_login'
			f.style.display = 'none';f.method = 'post';f.target="_self" ;f.id='tmp_login_form';
			var input_email = document.createElement('INPUT');
			input_email.type='hidden'; input_email.name='email';input_email.id='tmp_loginemail'
			var input_pass = document.createElement('INPUT');
			input_pass.name = 'password';input_pass.type='hidden';input_pass.id ='tmp_loginpassword';
			var input_faction = document.createElement('INPUT');
			input_faction.name = 'faction'; input_faction.type='hidden'; input_faction.id='tmp_loginfaction'
			f.appendChild(input_pass);f.appendChild(input_email);f.appendChild(input_faction);
			var my_f = document.body.appendChild (f);
			$("#tmp_loginpassword").val($("#PASS").val())
			$("#tmp_loginemail").val($("#EMAIL").val())
			$("#tmp_loginfaction").val('42')
			my_f.submit();
		}else if(data.indexOf('oldsystem') > -1 ){
                        if($("#tmp_login_form").length){
     		   		document.body.removeChild(document.getElementById('tmp_login_form'))
                        }
			var f = document.createElement('FORM');
			f.action ='http://dmvdriverseducation.org/course/login.php'
			f.style.display = 'none';f.method = 'post';f.target="_self" ;f.id='tmp_login_form';
			var input_email = document.createElement('INPUT');
			input_email.type='hidden'; input_email.name='username';input_email.id='tmp_loginemail'
			var input_pass = document.createElement('INPUT');
			input_pass.name = 'password';input_pass.type='hidden';input_pass.id ='tmp_loginpassword';
			var input_faction = document.createElement('INPUT');
			input_faction.name = 'fwd'; input_faction.type='hidden'; input_faction.id='tmp_loginfaction'
			f.appendChild(input_pass);f.appendChild(input_email);f.appendChild(input_faction);
			var my_f = document.body.appendChild (f);
			$("#tmp_loginpassword").val($("#PASS").val())
			$("#tmp_loginemail").val($("#EMAIL").val())
			$("#tmp_loginfaction").val('')
			my_f.submit();
		}else{		
			alert ('There is no such username in the system') ;
		}
		$("#gobtn").bind("click",submitLogin) ;
	})
	return false;
}
