
	
	function newajax(){
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		try
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		catch (e)
		  {
		  alert("Your browser does not support AJAX!");
		  return false;
		  }
		}
	  }
		return xmlHttp;
	}
	
	function ajax_login(){
		
		get_userid();
		
	}

	function get_userid(){

	var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			 pw_hash(res);
				
			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var un = document.getElementById("email").value;
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=getUserIdbyLoginName&p1=' + un + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}

	function pw_hash(userid){

	var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			check_login(res,userid);
				
			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=getPasswordHash&p1=' + userid + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}

	function check_login(hash,userid){

	var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			if (res == 'true'){
				//var choosesession = document.getElementById("session").value;
				set_session(userid);

			}else{
				var error = 'User Name or Password was incorrect, please try again';
				document.getElementById("errors").innerHTML = error;
				document.getElementById("email").value = '';
				document.getElementById("pwd").value = '';
			}
			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var plainpwd = document.getElementById("pwd").value;
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=checkLogin&p1=' + hash + '&p2=' + plainpwd + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}
	
	function set_session(userid){

	var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			if(res == 'true'){
				window.location = 'join2.php';
			}else{
				var error = 'Catastrophic Failure, Please contact administrator';
				document.getElementById("errors").innerHTML = error;
				document.getElementById("email").value = '';
				document.getElementById("pwd").value = '';
			}
				
			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=setUserSession&p1=' + userid + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}

	function get_billing_info(userid){
		

		var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			if(res != 'false'){
				var adarray = new Array();
				adarray = res.split(',');
				
				document.getElementById("address1").value = adarray[0];
				document.getElementById("address2").value = adarray[1];
				document.getElementById("city").value = adarray[2];
				document.getElementById("zipcode").value = adarray[4];
				//document.getElementById("city").value = adarray[2];
				document.getElementById("cust_first").value = adarray[5];
				document.getElementById("cust_last").value = adarray[6];
				document.getElementById("cust_email").value = adarray[7];

				var selstate = adarray[3];
				var selobj = document.getElementById("states");
				for (i=0;i<selobj.length;i++){
					selobj.selectedIndex = i;
					if (selobj.options[i].value == selstate){
						var thisindex = i;
					}
				}
				selobj.selectedIndex = thisindex;
				get_phone(userid);
				
				
			}else{
				alert('Contact Administrator, Catastrophic Failure');
			}			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=getUserPrimaryAddressExt&p1=' + userid + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}

	function get_phone(userid){

	var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			document.getElementById("phone").value = res;			
			
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=getUserPhoneNumber&p1=' + userid + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		
	}

	function forgot_password(){

		document.getElementById("forgot_password").style.display = 'block';

	}

	function do_forgot_password(){

		

		var xmlhttp = newajax()
		
		xmlhttp.onreadystatechange=function()
		{
		if(xmlhttp.readyState==4)
		  {
			var res = xmlhttp.responseText;
			if (res == 'true'){
				var loc = 'join-login.php?error=Thanks. We emailed a new password to you';
			}else{
				var loc = 'join-login.php?error=' + res;
			}		
			window.location = loc;
		  }
		}
		var da = new Date();
		var timestamp = da.getTime();
		var email = document.getElementById("forgot_email").value;
		var posturl = 'api.php?u=e68cbded83e915a748a8af30d9542577&c=User&f=resetUserPassword&p1=' + email + '&uq=' + timestamp;
		xmlhttp.open("POST",posturl,true);
		xmlhttp.send(null);
		

	}
	