function ShowPopup(login, password){
	var spot;
	var popup = null;
	document.getElementById('flash_detect').style.visibility = 'hidden';
	
    spot = new Ext.Spotlight({
			        easing: 'easeOut',
			        duration: .3
			    });
				
		popup = new Ext.Window({
			id: 'popup',
			width: 640,
			height: 480,
			resizable: false,
			frame: false,
			closable: false,
			shadow: true,
			shadowOffset: 10,
			html: '<div class="Popup">' +
			'<div class="PopupHeader"><div style="padding-top:2px;">Auto Registration</div></div>' +
			'<div class="PopupContent">' +
			'<table cellspacing=0 cellpadding=0 border=0 align="center">' +
			'<tr>' +
			'<td class="PopupColumnLabel">First Name:</td><td><div id="firstName" /></td>' +
			'</tr>' +
			'<tr>' +
			'<td class="PopupColumnLabel">Last Name:</td><td><div id="lastName" /></td>' +
			'</tr>' +
			'</table>' +
			'</div>' +
			'<div class="PopupFooter">' +
			'<input id="btnLogin" class="PopupButton" type="button" value="Register" />' +
			'<input id="btnCancel" class="PopupButton" type="button" value="Cancel" />' +
			'</div>' +
			'</div>'
		
		});
		
		popup.show(document.body);
		
		var firstName = new Ext.form.TextField({
			inputType: 'text',
			renderTo: 'firstName'
		});
		
		var lastName = new Ext.form.TextField({
			inputType: 'text',
			renderTo: 'lastName'
		});
		
		Ext.get('btnLogin').on("click", function(){
			firstName = document.getElementById('firstName').childNodes[0].value;
			firstName = trim(firstName);
			if (firstName == null || firstName == '') {
				alert('Please enter your first name.');
				return;
			}
			
			lastName = document.getElementById('lastName').childNodes[0].value;
			lastName = trim(lastName);
			if (lastName == null || lastName == '') {
				alert('Please enter your last name.');
				return;
			}
			getxmlHTTP();
			xmlHttp.onreadystatechange = registered;
			xmlHttp.open("POST", siteUrl + "AutoRegister?login=" + login + "&password=" + password + "&firstname=" + firstName + "&lastname=" + lastName, true);
			xmlHttp.send(null);
			
			
		});
		
		Ext.get('btnCancel').on("click", function(){
			popup.close();
			spot.destroy();
			document.getElementById('flash_detect').style.visibility = 'visible';
		});
		popup.show(document.body);
    spot.show('popup');	
}


function checkLogin(){
    login = document.getElementById('email').value;
    password = document.getElementById('password').value;
        
    if(login != null && echeck(login)){
        getxmlHTTP();
        xmlHttp.onreadystatechange=loginAction;
        xmlHttp.open("GET", siteUrl + "checkLogin?login=" + login,true);
        xmlHttp.send(null);
    }else{
        alert('Please enter a a valid email address.');
    }
}

function loginAction(){
    if (xmlHttp.readyState!=4){
        return;
    }
    var result = xmlHttp.responseText;
    if(result == 'Successful'){
        document.loginForm.submit();
    }else if(result == 'autoregister.html'){
    		login = document.getElementById('email').value;
    		password = document.getElementById('password').value;
        ShowPopup(login, password);
    }else if(result == 'register.html'){
    		email = document.getElementById('email').value;
        signup(email);
    } else{
				window.location = 'html/' + result;

    }
}

function registered(){
    if (xmlHttp.readyState!=4){
        return;
    }
    var result = xmlHttp.responseText;
    if(result == 1){
        document.loginForm.submit();
    }else{
        alert('can not auto register. Please check your email address and default password');
    }
}

function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
        return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }

    if (str.indexOf(at,(lat+1))!=-1){
        return false
    }

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
    }

    if (str.indexOf(dot,(lat+2))==-1){
        return false
    }

    if (str.indexOf(" ")!=-1){
        return false
    }

    return true
}
