// JavaScript Document

var submit = false;

function add_lead() {
	var ents = new Array("name", "email", "country");	
	var str = "";
	var i=0;
	for (i=0;i<=ents.length-1;i++) {
		str = str + "&" + ents[i] + "=" + (document.getElementById(ents[i]).value).replace(/&/, "And");
	}
	if(window.XMLHttpRequest){
		var xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {
			if (xhr.status == 200) {
				//alert(xhr.responseText);
				// Submit the form when we get a reply back
				document.form1.submit();
			}
		}
	}
	xhr.open("GET", '/save.php?SAVE=true'+str, true);
	xhr.send(null);
	
}

function linkclick() {
	alert('Fill in the form to Receive INSTANT Information');
}

function checkemail() {
	var ents = new Array("ar_name", "ar_email");	
	var str = "";
	var i=0;
	for (i=0;i<=ents.length-1;i++) {
		if (document.getElementById(ents[i]).value == ""){
			alert("Please enter all the required fields");
			return;
		}
	str = str + "&" + ents[i] + "=" + (document.getElementById(ents[i]).value).replace(/&/, "And");
	}
	
	if(window.XMLHttpRequest){
		var xhr = new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		var xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4) {
			if (xhr.status == 200) {
				//alert(xhr.responseText);
				if (xhr.responseText != "notfound") {
					document.test_form.submit();
					//top.location.href = "http://womensepublishers.com/index.php";
				} else {
					alert('Sorry, That email address is not in our database');
				}
			}
		}
	}
	xhr.open("GET", '/checkemail.php?SAVE=true'+str, true);
	xhr.send(null);
}


function checkform(al) {
	var error = false;
	
	if ((al) && (!submit)) { // Don't want to fire this until we've hit the submit button once
		return false;
	} else if ((!al)&& (!submit)) {
		submit = true;
	}
	
	// First up loop all the fields listed as required and check for some data (at least 5 char)
	var reqs = new Array("name", "email", "country");	
	
	var i=0;
	for (i=0;i<=2;i++) {
		document.getElementById(reqs[i])
		if ((document.getElementById(reqs[i]).value).length < 3) {
			error = true;
			document.getElementById(reqs[i]+'_label').style.color = "#FFFFFF";
			document.getElementById(reqs[i]+'_label').style.backgroundColor = "#CC0000";		
		} else { //We need to set it back to black in case it's the second time around
			document.getElementById(reqs[i]+'_label').style.color = "#000000";
			document.getElementById(reqs[i]+'_label').style.backgroundColor = "#FFFFFF";
		}	
	}
	
	// Next we see if the email address has all the bits it needs to be real
	if (!echeck(document.getElementById('email').value)) {
		error = true;	
		document.getElementById('email_label').style.color = "#FFFFFF";		
		document.getElementById('email_label').style.backgroundColor = "#CC0000";		
	} else {
		document.getElementById('email_label').style.backgroundColor = "#FFFFFF";		
		document.getElementById('email_label').style.color = "#000000";		
	}
	
	// Check for errors and go from there
	if (error) {
		if (!al) {
			alert('I\m Sorry, but we\'re missing some information from you.\nPlease complete the Required Fields and try again');
		}
		return false;
	} else if (!al) {
		add_lead();
		setTimeout('document.form1.submit()', 1500);
		return true;
	}
}


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					
}

function swapform(bob) {
	if (bob == 'swap') {
		document.getElementById('alreadyregistered').style.display = document.getElementById('alreadyregistered').style.display ? '' : 'none';
		document.getElementById('freetickets').style.display = document.getElementById('freetickets').style.display ? '' : 'none';
	} else {
		document.getElementById('alreadyregistered').style.display = bob ? '' : 'none';
		document.getElementById('freetickets').style.display = bob ? 'none' : '';
	}	
}
