// Get the HTTP Object (this is for implementation of AJAX)
function getHTTPObject(){
	if (window.ActiveXObject)        
		return new ActiveXObject("Microsoft.XMLHTTP");   
	else if (window.XMLHttpRequest)        
		return new XMLHttpRequest();   
	else {      
		alert("Your browser does not support AJAX.");      
		return null;   
	}
}

function login(email,pword) {
	//alert(pword);
	httpObject = getHTTPObject();    

	if (httpObject != null) {
		httpObject.open("GET", "login_authenticate.php?email=" + email + "&pass=" + pword);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				if(httpObject.responseText=='success') {					
					//sucesss
					location.href="profile.php";
				} else {
					//fail
					alert("Sorry, invalid username or password");
				}
		    }
		};
	}
}

function displayUserMarkers() {

		//start the loop (needed: id, free/premium, firstname, lastname, city, state, latlng
		
		httpObject = getHTTPObject();    
		
		if (httpObject != null) {
			httpObject.open("GET", "getusers_try.php");
			httpObject.send(null);
			//alert(httpObject);
			httpObject.onreadystatechange = function(){
				if (httpObject.readyState == 4) {
					var user = new Array();
					
					
					//var setArrays = new Function(httpObject.responseText);
					//setArrays(); //name of the array is user;
					
					eval(httpObject.responseText);
					/* [0]->id,
					 * [1]->account_type, 
					 * [2]->firstname
					 * [3]->lastname
					 * [4]->city
					 * [5]->state
					 * [6]->lat "(lat"
					 * [7]->lng "lng)"
					 * [8]->opportunity*/
					
					for (var g = 0; g < user.length; g++) {
						us = user[g].split(",");
						//substring to cut off parenthesis
						lat = us[6].substring(1);//"28.162429"
						lng = us[7].substring(0, us[7].length - 1);
						
						var tinyIcon = new GIcon();
						
						tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
						tinyIcon.iconSize = new GSize(12, 20);
						tinyIcon.shadowSize = new GSize(22, 20);
						tinyIcon.iconAnchor = new GPoint(6, 20);
						tinyIcon.infoWindowAnchor = new GPoint(5, 1);
						
						if (us[1] == "premium") {
							tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
						}
						else {
							tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
						}
						
						markerOptions = {
							title: us[2] + " " + us[3] + " (" + us[4] + ", " + us[5] + ")",
							icon: tinyIcon
						};
						
						marker[g] = new GMarker(new GLatLng(lat, lng), markerOptions);
						
						//set the inner html of the window
						//chtml = ''+us[0];
						//alert();
						//chtml += 
						
						GEvent.addListener(marker[g], 'click', function(latlng){
							//map.openInfoWindowHtml(latlng, 'test');
							//turn "lat,lng" into "lat, lng"
							//latlng = latlng.replace(",",", ");
							
							//alert(latlng);
							httpObj2 = getHTTPObject();    
							httpObj2.open("GET", "getuserwindow.php?latlng="+latlng);
							httpObj2.send(null);
							
							
							
							httpObj2.onreadystatechange = function(){
								if (httpObj2.readyState == 4) {
									map.openInfoWindow(latlng,httpObj2.responseText);
								}
							}
							
						});
						
						map.addOverlay(marker[g]);
						marker_id[g] = us[0]; //set the id
						marker_opp[g] = us[8]; //set which opportunity it is
					}
				}
			}
		}


}

function showConfirmation(txt) {
	$("#confirmation").show();
	$("#confirmation").html(txt);
	//alert(txt);
}

function checkAccount(email, callbackFunction) {
	//alert(name + ", " + email);
	
	//use ajax to insert a new account in the database, but first make sure an existing account doesn't exist
	
	httpObject = getHTTPObject();    

	if (httpObject != null) {
		httpObject.open("GET", "checkAccount.php?email=" + email);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				//alert(httpObject.responseText);
				if(httpObject.responseText=='fail') {
					alert("This email address is already registered.");
					//alert(document.getElementsByTagName('iframe').length);
					//document.getElementById('signup_frame').src = "http://www.mlmlocal.com/signup.php";
				} else {
					callbackFunction();
				}
				
		    }
		};
	}
	
	
}

function updateAccountStep2(id, name, email, password, opportunity, phone, address, city, state, zip, point, aff, callbackFunction) {
	//alert(email + ", " + phone + ", " + address);
	//alert(arguments.join());
	//
	
	//use ajax to update information in the account to include the email, phone, address, etc.
	//make sure to calculate the longitude and latitude though
	
	httpObject = getHTTPObject();    

	if (httpObject != null) {
		ll=escape(point);
		//alert(ll);
		//alert(password);
		
		httpObject.open("GET", "updateAccountStep2.php?id=" + id + "&name=" + name + "&email=" + email + "&password=" + password + "&opportunity=" + opportunity + "&phone=" + phone + "&address=" + address + "&city=" + city + "&state=" + state + "&zip=" + zip + "&latlng=" + ll + "&aff=" + aff);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				//alert(httpObject.responseText);
				if(httpObject.responseText=='fail') {
					alert("This phone number was already registered");
				} else {
					callbackFunction();
					showConfirmation("Account Creation was Successful <br/><span class=\"link\" onclick=\"login('" + email + "','" + password + "');\">View Account Information</span> | <span onclick=\"$('#confirmation').hide();\" class=\"link\">Hide</span>");
				}
				
		    }
		};
	}
}

function updateAccountFinal(id, cc_type, cc_num, cc_cvn, cc_expdate, callbackFunction) {
	//alert(arguments.length);
	
	//use ajax to validate the credit card, if valid, upgrade the account to a premium version
	httpObject = getHTTPObject();    

	if (httpObject != null) {
		httpObject.open("GET", "updateAccountFinal.php?id=" + id + "&account_type=premium&cc_type=" + cc_type + "&cc_num=" + cc_num + "&cc_cvn=" + cc_cvn + "&cc_expdate=" + cc_expdate);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				//alert(httpObject.responseText);
				callbackFunction();
		    }
		};
	}
}

function updateAccountProfile(id,full_name, email, opportunity, pword, phone, address, city, state, zip, latlng, website, twitter, facebook, myspace, linkedin, hideshowvals, callbackFunction) {
	//val="";for(i=0;i<arguments.length;i++) {val+=arguments[i] + "\n";} alert(val);
	
	httpObject = getHTTPObject();    
	//alert(myspace);
	if (httpObject != null) {
		httpObject.open("GET", "updateAccountProfile.php?id=" + id + "&full_name=" + full_name + "&email=" + email + "&opportunity=" + opportunity + "&pword=" + pword + "&phone=" + phone + "&address=" + address + "&city=" + city + "&state=" + state + "&zip=" + zip + "&latlng=" + latlng + "&website=" + website + "&twitter=" + twitter + "&facebook=" + facebook + "&myspace=" + myspace + "&linkedin=" + linkedin + hideshowvals);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
//				alert(httpObject.responseText);
				callbackFunction();
				showConfirmation("Account Update was Successful <br/><a href=\"index.php\">Back to the Map</a> | <a href=\"profile.php\">Reload Page</a> | <span onclick=\"$('#confirmation').hide();\" class=\'link\'>Close</span>");
		    }
		};
	}
	
}

function deleteAccount(id) {
	$('#confirmation').hide();
	//alert(id);
	
	httpObject = getHTTPObject();    
	//alert(myspace);
	if (httpObject != null) {
		httpObject.open("GET", "deleteaccount.php?id=" + id);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				showConfirmation("Your Account was Successfully Deleted <br/><a href=\"index.php?signout\">Back to the Map</a>");
		    }
		};
	}
	
}

function retrievePassword(email) {

	httpObject = getHTTPObject();    

	if (httpObject != null) {
		httpObject.open("GET", "retrievepassword.php?email=" + email);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				if (httpObject.responseText != "fail") {
					closeLightBox();
					showConfirmation("Your Password was Sent to " + $("#forgot_password_email").val() + ", Please Check Your Email <br/><span onclick=\"$('#confirmation').hide();\" class=\'link\'>Close</span>");
				} else {
					showConfirmation("Sorry, we don't seem to have that email on file.  Please try again with another one.<br/><span onclick=\"$('#confirmation').hide();\" class=\'link\'>[OK]</span>");
				}
			}
		};
	}
	
}

function checkIfOppExists(oppname, callbackFunction) {
	httpObject = getHTTPObject();    

	if (httpObject != null) {
		httpObject.open("GET", "checkIfOppExists.php?oppname=" + oppname);
		httpObject.send(null);
		
        httpObject.onreadystatechange = function() {
			if (httpObject.readyState == 4){
				if(httpObject.responseText=="fail") {
					callbackFunction();
					response="fail";
				} else {
					response="success";
				}
			}
		};
	}
}

	var ctrl=false;
	function phone_keydown(o,e) {
		var k;
		if (!e) var e = window.event
		if (e.keyCode) k = e.keyCode;
			else if (e.which) k = e.which;        
		if(!o.value.match(/[!@#$%^&*()]+/) && (k >= 48 && k <= 57) && o.value.replace(/\-/, '').length <= 10) {
		   if(o.value.length == 3 || o.value.length == 7) {
		           o.value+='-';
		   }
		return true;
		} else if(k==8 || k==9 || k==46 || (k >= 35 && k <= 40) || (ctrl && (k==86 || k==67))) {
		   return true;
		} else if(k==17){
		   ctrl=true;
		}
		return false;
	}
	
	function phone_keyup(o,e) {
		var k;
		if (!e) var e = window.event
		if (e.keyCode) k = e.keyCode;
		else if (e.which) k = e.which;
		
		if(k==17)
		   ctrl=false;
		   
		o.value = o.value.replace(/[^0-9\-]/g, '');
	}
	 
	function checkEnter(o,e) {
		var k;
		if (!e) var e = window.event
		if (e.keyCode) k = e.keyCode;
		else if (e.which) k = e.which;
		if(k==13) {
			$('#login').click();
		}
		//if(window.event.keyCode==13) 
	}
	 
function checkRefresh()
{
	// Get the time now and convert to UTC seconds
	var today = new Date();
	var now = today.getUTCSeconds();

	// Get the cookie
	var cookie = document.cookie;
	var cookieArray = cookie.split('; ');

	// Parse the cookies: get the stored time
	for(var loop=0; loop < cookieArray.length; loop++)
	{
		var nameValue = cookieArray[loop].split('=');
		// Get the cookie time stamp
		if( nameValue[0].toString() == 'SHTS' )
		{
			var cookieTime = parseInt( nameValue[1] );
		}
		// Get the cookie page
		else if( nameValue[0].toString() == 'SHTSP' )
		{
			var cookieName = nameValue[1];
		}
	}

	if( cookieName &&
		cookieTime &&
		cookieName == escape(location.href) &&
		Math.abs(now - cookieTime) < 5 )
	{
		// Refresh detected

		// Insert code here representing what to do on
		// a refresh
		$("#confirmation").hide();
		// If you would like to toggle so this refresh code
		// is executed on every OTHER refresh, then 
		// uncomment the following line
		// refresh_prepare = 0; 
	}	

	// You may want to add code in an else here special 
	// for fresh page loads
}

function prepareForRefresh()
{
	if( refresh_prepare > 0 )
	{
		// Turn refresh detection on so that if this
		// page gets quickly loaded, we know it's a refresh
		var today = new Date();
		var now = today.getUTCSeconds();
		document.cookie = 'SHTS=' + now + ';';
		document.cookie = 'SHTSP=' + escape(location.href) + ';';
	}
	else
	{
		// Refresh detection has been disabled
		document.cookie = 'SHTS=;';
		document.cookie = 'SHTSP=;';
	}
}

function disableRefreshDetection()
{
	// The next page will look like a refresh but it actually
	// won't be, so turn refresh detection off.
	refresh_prepare = 0;

	// Also return true so this can be placed in onSubmits
	// without fear of any problems.
	return true;
} 

// By default, turn refresh detection on
var refresh_prepare = 1;

function updatetofinal(id) {
	//alert(id);
	//update account to elite account
	updateAccountFinal(id, "", "", "", "", function() {
		document.forms.frm.submit();	
	});
}
		-->
