// JavaScript Document

var submit = false;

function do_aaron_aff() {

	var str = "Affiliate Signup Request Received\n";
	var the_form = document.getElementById('aff_form');
	var elem = the_form.elements;
	for(var i = 0; i < elem.length; i++) {
		if (elem[i].type == 'button') continue;
		if (elem[i].name == 'wba_action') continue;		
		str += elem[i].name + ": " + elem[i].value ;
		str += "\n";
	}
	newFormElement('wba_message',str,'hidden',the_form);
	the_form.submit();

}
function newFormElement(name,value,type,the_form) {
	if (document.createElement) {
		var inp = document.createElement("input");
		inp.name = name;
		inp.type = type;
		inp.value = value;
		inp.id = name;
		the_form.appendChild(inp);
	} else {
		the_form.innerHTML+='<input type="'+type+'" name="'+name+'" value="'+value+'" />';
	}
}


function add_lead() {
	var ents = new Array("wbw_name", "wbw_email", "wbw_ticket", "wbw_address1", "wbw_address2", "wbw_city", "wbw_state", "wbw_country", "wbw_ticket", "wbw_event");	
	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.wbw_form.submit();
			}
		}
	}
	xhr.open("GET", 'save.php?SAVE=true'+str, true);
	xhr.send(null);
	
}
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") {
					top.location.href = "http://webbusinessworkshops.com/?home";
				} 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("wbw_name", "wbw_email", "wbw_ticket");
	
	var i=0;
	for (i=0;i<=2;i++) {
		document.getElementById(reqs[i])
		if ((document.getElementById(reqs[i]).value).length < 4) {
			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('wbw_email').value)) {
		error = true;	
		document.getElementById('wbw_email_label').style.color = "#FFFFFF";		
		document.getElementById('wbw_email_label').style.backgroundColor = "#CC0000";		
	} else {
		document.getElementById('wbw_email_label').style.backgroundColor = "#FFFFFF";		
		document.getElementById('wbw_email_label').style.color = "#000000";		
	}
	
	// has the user selected an event?
	if (document.getElementById('wbw_event').selectedIndex == 0) {
		error = true;	
		document.getElementById('wbw_event_label').style.color = "#FFFFFF";		
		document.getElementById('wbw_event_label').style.backgroundColor = "#CC0000";		
	} else {
		document.getElementById('wbw_event_label').style.backgroundColor = "#FFFFFF";		
		document.getElementById('wbw_event_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.wbw_form.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 nothanks() {
	var form = document.forms['wbw_form'];
   // form.action = 'put your url here';
   var el = document.createElement("input");
   el.type = "hidden";
   el.name = "nothanks";
   el.value = "nothanks";
   form.appendChild(el);
   form.submit();

}
function previewemail() {
	document.getElementById('preview').style.display = document.getElementById('preview').style.display ? '' : 'none';
}

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


