function notValidEmail(str)
{
    mailRE = new RegExp( );
    mailRE.compile( '^[\._a-z0-9-]+@[\.a-z0-9-]+[\.]{1}[a-z]{2,4}$', 'gi' );
    return !(mailRE.test( str.value ));
} 
function isEmpty(str)
{
    strRE = new RegExp( );
    strRE.compile( '/^[\s ]*$/', 'gi' );
    return strRE.test( str.value );
} 

function verify( form )
{
    //check if form fields are filled
	
	if( isEmpty( form.username ) ){ alert( 'Please enter your desired screen name.' ); form.username.focus();return false; }
	if( form.username.value.length < 4 ){ alert( 'Screen name must be 4 or more characters.' ); form.username.focus();return false; }
	if(!/^[a-zA-Z0-9_\-]+$/.test(form.username.value)) {alert( 'Screen Name can only contain A-Z, a-z, 0-9, - (dash), and _ (underscore).' ); form.username.focus();return false; }
	if(!/^[a-zA-Z]/.test(form.username.value.substring(0, 1))) {alert( 'Screen Name must must start with a letter.' ); form.username.focus();return false; }

	if( isEmpty( form.password ) ){ alert( 'Please enter the password.' ); form.password.focus();return false; }
	if( isEmpty( form.password2 ) ){ alert( 'Please confirm the password.' ); form.password2.focus();return false; }
	if(form.password.value != form.password2.value) { alert( 'Passwords do not match.' ); form.password2.focus();return false; }
	if( (form.password.value.length < 6)  || (form.password.value.length > 16) || (! /^[a-zA-Z0-9_\-.]+$/.test(form.password.value)) || (form.password.value == 'password') || (form.password.value == form.username.value)){ alert( 'Password must be 6-16 characters, must include numbers, cannot equal your chosen username or \'password\', and can only contain A-Z, a-z, 0-9, - (dash), and _ (underscore).' ); form.password.focus();return false; }
	
    if( notValidEmail( form.email ) ){alert( 'Empty or incorrect email address.' ); form.email.focus();return false; }
    if( notValidEmail( form.email2 ) ){alert( 'Empty or incorrect confirmation email address.' ); form.email2.focus();return false; }
    if( form.email.value != form.email2.value ){alert( 'Email addresses do not match.' ); form.email.focus();return false; }

    httpReq();
    return true;
}

function validate( form )
{
    //check if form fields are filled
	
	if( isEmpty( form.screenName1 ) ){ alert( 'Please enter your desired screen name.' ); form.screenName1.focus();return false; }
	if( form.screenName1.value.length < 4 ){ alert( 'Screen name must be 4 or more characters.' ); form.screenName1.focus();return false; }
	if(!/^[a-zA-Z0-9_\-]+$/.test(form.screenName1.value)) {alert( 'Screen Name can only contain A-Z, a-z, 0-9, - (dash), and _ (underscore).' ); form.screenName1.focus();return false; }
	if(!/^[a-zA-Z]/.test(form.screenName1.value.substring(0, 1))) {alert( 'Screen Name must must start with a letter.' ); form.screenName1.focus();return false; }

	if( isEmpty( form.pwword1 ) ){ alert( 'Please enter the password.' ); form.pwword1.focus();return false; }
	if( isEmpty( form.pwword2 ) ){ alert( 'Please confirm the password.' ); form.pwword2.focus();return false; }
	if(form.pwword1.value != form.pwword2.value) { alert( 'Passwords do not match.' ); form.pwword2.focus();return false; }
	if( (form.pwword1.value.length < 6)  || (form.pwword1.value.length > 16) || (! /^[a-zA-Z0-9_\-.]+$/.test(form.pwword1.value)) || (form.pwword1.value == 'password') || (form.pwword1.value == form.screenName1.value)){ alert( 'Password must be 6-16 characters, must include numbers, cannot equal your chosen username or \'password\', and can only contain A-Z, a-z, 0-9, - (dash), and _ (underscore).' ); form.pwword1.focus();return false; }
	
    if( notValidEmail( form.email ) ){alert( 'Empty or incorrect email address.' ); form.email.focus();return false; }
    if( notValidEmail( form.email2 ) ){alert( 'Empty or incorrect confirmation email address.' ); form.email2.focus();return false; }
    if( form.email.value != form.email2.value ){alert( 'Email addresses do not match.' ); form.email.focus();return false; }

    showPage3(form);
    return true;
}

function showPage2(form)
{
	if( form.ageMin.value > form.ageMax.value ){ alert( 'Age range incorrect.' ); form.ageMin.focus();return false; }
	if(!(form.postalCode.value.match(/^\d{5}$/)) && !(form.postalCode.value.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/)))
	{ 
		alert("Please enter a valid postal/zip code.");
		form.postalCode.focus();
		return false;
	}
	if( form.months.value=="" ){ alert( 'Please enter the month of your birth.' ); form.months.focus();return false; }
	if( form.days.value=="" ){ alert( 'Please enter the day of your birth.' ); form.days.focus();return false; }
	if( form.years.value=="" ){ alert( 'Please enter the year of your birth.' ); form.years.focus();return false; }
	
	document.getElementById("page3").style.display ="none";
	document.getElementById("page2").style.display ="inline";
	document.getElementById("page1").style.display ="none";
	document.getElementById("logos").style.visibility ="visible";
	
	return false;
} 

function changeImg(form)
{
	var eighteen = parseInt(form.years.value) + 18;
	var twentythree = parseInt(form.years.value) + 23;
	var month = parseInt(form.months.value) - 1;
	var age18 = new Date();
	age18.setFullYear(eighteen, month, form.days.value);
	var age23 = new Date();
	age23.setFullYear(twentythree, month, form.days.value);
	var today = new Date();
	if (today <= age18)
	{
		document.img_match.src = "images/crush.jpg";	
	}
	else if ((today > age18) && (today < age23))
	{
		document.img_match.src = "images/amateur_match.gif";
	}
	else
	{
		document.img_match.src = "images/true.gif";
	}
}

function showPage3(form)
{
	document.getElementById("page3").style.display ="inline";
	document.getElementById("page2").style.display ="none";
	document.getElementById("page1").style.display ="none";
	document.getElementById("logos").style.visibility ="visible";
	setTimeout('submitForm()', 4000);
} 

function showPage1()
{
	document.getElementById("page3").style.display ="none";
	document.getElementById("page2").style.display ="none";
	document.getElementById("page1").style.display ="inline";
	document.getElementById("logos").style.visibility ="hidden";
}

function submitForm()
{
//	httpReq();
//	makeHttpRequest('CA', alert);
	document.search.submit();
}

function trueTerms(form)
{
	var birthMonth = form.months.value;
	var birthDay = form.days.value;
	var birthYear = form.years.value;
	if (birthYear == '' || birthMonth == '' || birthDay == '')
	{
		return false;
	}
	var min_age = 23;
	var theirDate = new Date((birthYear*1 + min_age), birthMonth-1, birthDay);
	var today = new Date();
	if ( (today.getTime() - theirDate.getTime()) > 0) {
		document.getElementById('terms').innerHTML = "<div align='center'><p><img src='/images/true_trans.png' border='0' />TRUE will never release personally identifiable information without your express permission. Read more in our <a href='https://www.true.com/privacy.htm' target='_blank'>Privacy and Use Policy.</a></p></div>";  		
		document.getElementById('agree').innerHTML = "<p>By clicking \"Search Now!\" I confirm that I am at least 18 years old, and I have read and agreed to the TRUE.com <a id='_ctl1_secondStep_RemoteWindowAgree' name='RemoteWindowsAgree' href='https://www.true.com/popMoreText.aspx?id=6' target='_blank' onClick=\"javascript:window.open( 'https://www.true.com/popMoreText.aspx?id=6', 'newWindow1297229170', 'menubar=0,toolbar=0,scrollbars=yes,resizable=0,height=350,innerHeight=350,width=500,innerWidth=500'); return false;\">Terms of Use</a> and <a id='_ctl1_secondStep_RemoteWindowEthic' name='RemoteWindowEthic' href='https://www.true.com/popMoreText.aspx?id=56' target='_blank' onClick=\"javascript:window.open( 'https://www.true.com/popMoreText.aspx?id=56', 'newWindow1720521328', 'menubar=0,toolbar=0,scrollbars=yes,resizable=0,height=350,innerHeight=350,width=500,innerWidth=500'); return false;\">Code of Ethics</a>.</p>";
		document.getElementById('chosen').innerHTML = "<img src='/images/true.gif' border='0' />";
		form.buyer.value = "true";
	}
	else
	{
		document.getElementById('chosen').innerHTML = "<img src='/images/fling.gif' border='0' />";
		form.buyer.value = "fling";
	}
		
}

function getXmlHttp()
{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	
	return xmlhttp;
}

function httpReq()
{
	var query = '';
	for (var i = 0 ; i < document.search.elements.length ; i++)
	{
		if (document.search.elements[i].name == 'agree')
		{
			if (document.search.agree.checked)
			{
				query += "&" + document.search.agree.name + "=1";
			}
			else
			{
				query += "&" + document.search.agree.name + "=0";
			}
		}
		else
		{
			query += "&" + document.search.elements[i].name + "=" + document.search.elements[i].value;
		}
	}

	xmlhttp = getXmlHttp();
	
	xmlhttp.onreadystatechange = function()
	{
		if (xmlhttp.readyState == 4) 
		{
			if(/^1/.test(xmlhttp.responseText))
			{
				showPage3();
			}
			else
			{
				if (/zip/.test(xmlhttp.responseText))
				{
					// goto page 1
					showPage1();
				}
				alert(xmlhttp.responseText);
			}
		}
	}
	
	xmlhttp.open("GET", "ajax.php?" + query, true);
	
	xmlhttp.send(null);
}

function goPage2(form)
{
	if( form.ageMin.value > form.ageMax.value ){ alert( 'Age range incorrect.' ); form.ageMin.focus();return false; }
	if(!(form.location_id.value.match(/^\d{5}$/)) && !(form.location_id.value.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/)))
	{ 
		alert("Please enter a valid postal/zip code.");
		form.location_id.focus();
		return false;
	}
	if( form.bmon.value=="" ){ alert( 'Please enter the month of your birth.' ); form.bmon.focus();return false; }
	if( form.bday.value=="" ){ alert( 'Please enter the day of your birth.' ); form.bday.focus();return false; }
	if( form.byear.value=="" ){ alert( 'Please enter the year of your birth.' ); form.byear.focus();return false; }
	document.getElementById("page3").style.display ="none";
	document.getElementById("page2").style.display ="inline";
	document.getElementById("page1").style.display ="none";
	
	return false;	
}

function lookupZip(zip)
{
	if (!zip.value.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/))
	{
		xmlhttp = getXmlHttp();
		
		xmlhttp.onreadystatechange = function()
		{
			if (xmlhttp.readyState == 4) 
			{
				if (xmlhttp.responseText)
				{
					document.getElementById('area').innerHTML = xmlhttp.responseText;
				}
				else
				{
					alert('Zip code is invalid.');
					zip.focus();
				}
			}
		}
		
		xmlhttp.open("GET", "lookupzip.php?zip=" + zip.value, true);
		
		xmlhttp.send(null);
	}

	return false;
}