var xmlHttp;
var divTarget;

var password_status;
var email_status;
var captcha_status;

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function NextStep() {
	var city = document.getElementById('City').value;
	var username_status = document.getElementById('username_result').innerHTML;
	var captcha_status = document.getElementById('captcha_result').innerHTML;
	if (username_status == 'Username is good!'){
		if (password_status == 1){
			if (city != ''){
				if (email_status == 1){
					if (captcha_status == 'Verified!'){
						if (document.getElementById('Agree').checked == 1){
							document.getElementById('Step_1').style.display = 'none';
							document.getElementById('Step_2').style.display = '';
						} else {
							alert('You must agree to the terms of use in order to continue.');
						}
					} else {
						alert('Invalid verification code.');
					}
				} else {
					alert('The email address you have entered is invalid.');
				}
			} else {
				alert('Your city cannot be left blank.');
			}
		} else {
			alert('The password you have entered is invalid.');
		}
	} else {
		alert('The username you have entered is invalid.');
	}
}

function Register() {
	var about_me = window.frames['Step_2'].document.getElementById('about_me').value;
	var looking_for = window.frames['Step_2'].document.getElementById('looking_for').value;
	var first_date = window.frames['Step_2'].document.getElementById('first_date').value;
	var photo = window.frames['Step_2'].document.getElementById('imgfile').value;
	var spam = 0;
	
	if (about_me != ''){
		if (looking_for.length > 0 || first_date.length > 0){
			if (Left(looking_for, 12) == Left(first_date, 12) || Left(about_me, 12) == Left(first_date, 12) || Left(about_me, 12) == Left(looking_for, 12)) {
				spam = 1;
			}
		}
	}

	if (about_me != ''){
		if (spam == 0){
			if (photo != ''){
				var url = 'http://www.freeflirtdating.com/join/step2.php?';
				var username = document.getElementById('Username').value;
				var password = document.getElementById('Password').value;
				var region = document.getElementById('region').value;
				var city = document.getElementById('City').value;
				city = city.replace(/ /g, '-');
				username = username.replace(/ /g, '-');
				password = password.replace(/ /g, '-');

				url=url+'username='+username;
				url=url+'&password='+password;
				url=url+'&gender='+document.getElementById('Gender').value;
				url=url+'&year='+document.getElementById('bdateyear').value+'&month='+document.getElementById('bdatemonth').value+'&day='+document.getElementById('bdateday').value;
				url=url+'&email='+document.getElementById('Email').value;
				url=url+'&country='+region.substr(0, region.indexOf('_'));
				url=url+'&state='+region.substr((region.indexOf('_')+1));
				url=url+'&city='+city;

				window.frames['Step_2'].document.Finish_Form.action = url;
				window.frames['Step_2'].document.Finish_Form.submit();
			} else {
				alert('You must upload a photo to register.');
			}
		} else {
			alert('Please do not spam your profile.');
		}
	} else {
		alert('Please enter in a description of yourself in the about me section.');
	}
}

function ValidateCaptcha() {
	var code = document.getElementById('Captcha').value;
	GetPage('validate_captcha.php?code='+code, 'captcha_result');
}

function ValidateUsername() {
	var username = document.getElementById('Username').value;
	if (username.length == 0){
		document.getElementById('username_result').innerHTML = '&nbsp;';
	} else if (username.length > 12){
		document.getElementById('username_result').style.color = '#FF0000';
		document.getElementById('username_result').innerHTML = 'Username too long.';
	} else if (username.length < 8){
		document.getElementById('username_result').style.color = '#FF0000';
		document.getElementById('username_result').innerHTML = 'Username too short.';
	} else if (username.length > 7 && username.length < 13){
		GetPage('validate_username.php?username='+username, 'username_result');
	}
}
function CheckPasswords() {
	var pwda = document.getElementById('Password').value;
	var pwdb = document.getElementById('PasswordAgain').value;
	if (pwda.length == 0 && pwdb.length == 0){
		document.getElementById('password_result').innerHTML = '&nbsp;';
		password_status = 0;
	} else if (pwda == pwdb){
		if (pwda.length > 7){
			document.getElementById('password_result').style.color = '#00FF00';
			document.getElementById('password_result').innerHTML = 'Password is strong!';
			password_status = 1;
		} else {
			document.getElementById('password_result').style.color = '#FF0000';
			document.getElementById('password_result').innerHTML = 'Password too weak.';
			password_status = 0;
		}
	} else {
		document.getElementById('password_result').style.color = '#FF0000';
		document.getElementById('password_result').innerHTML = 'Passwords don\'t match.';
		password_status = 0;
	}
}
function CheckEmail() {
	var email = document.getElementById('Email').value;
	var emailformat = /^.+@.+\..{2,3}$/

	if (email.length == 0){
		document.getElementById('email_result').innerHTML = '&nbsp;';
		email_status = 0;
	} else {
		if (emailformat.test(email)){
			document.getElementById('email_result').style.color = '#00FF00';
			document.getElementById('email_result').innerHTML = 'Valid e-mail address!';
			email_status = 1;
		} else {
			document.getElementById('email_result').style.color = '#FF0000';
			document.getElementById('email_result').innerHTML = 'Invalid e-mail address.';
			email_status = 0;
		}

	}
}

function GetPage(url, target) {
	divTarget = target;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
  		alert ("Please upgrade to Internet Explorer 5 or later.");
 		return;
  	} 
	
	isbusy = 1;
	xmlHttp.open("GET","http://www.freeflirtdating.com/join/"+url,true);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.send(null);
}

function stateChanged(){ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		isbusy = 0;
		if (divTarget == 'username_result'){
			if (xmlHttp.responseText == 1){
				document.getElementById('username_result').style.color = '#00FF00';
				document.getElementById('username_result').innerHTML = 'Username is good!';
			} else {
				document.getElementById('username_result').style.color = '#FF0000';
				document.getElementById('username_result').innerHTML = 'Username is taken.';
			}
		} else if (divTarget == 'captcha_result'){
			if (xmlHttp.responseText == 1){
				document.getElementById('captcha_result').style.color = '#00FF00';
				document.getElementById('captcha_result').innerHTML = 'Verified!';
			} else {
				document.getElementById('captcha_result').style.color = '#FF0000';
				document.getElementById('captcha_result').innerHTML = 'Not verified.';
			}
		}
 	}
}

function GetXmlHttpObject()
{
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}