    function getAjaxBaseURL()
    {
    	var urlArray = location.href.match(/(https?:\/\/[^\/]*\/[^\/]*\/?).*/);
    	var baseURL = RegExp.$1;
    	if (baseURL.substring(baseURL.length-1) != '/')
    	{
    		baseURL += '/';
    	}
			
			if(document.dev)
				{
					return 'index.php';
				}
			else
				{
    			return baseURL;
				}
    }
    
    function getTourBaseURL()
    {
    	var urlArray = location.href.match(/(https?:\/\/.*\/[^\?]*\/?)\??.*/);
    	
    		return RegExp.$1;
    }
    
	var dropdownHandler =
  	{
  		clearDropdown: function(dropdown)
  		{
  	  		var i;
					if(dropdown)
					{
						for(i=dropdown.options.length-1; i>=0; i--)
						{
							dropdown.remove(i);
						}
					}
  		},
		
      	fillDropdown: function(dropdown, options)
      	{
      		var i;
      		for(var i in options)
      		{
      			dropdown.options.add(options[i]);
      		}
      	},

      	setLoading: function(dropdown)
      	{
      		if (!dropdown.options[0])
      		{
      			this.resetDropdown(dropdown, 'Loading...');
	      	  	dropdown.disabled = true;
      		}
      		else
      		{
	    		dropdown.options[0].text = 'Loading...';
	      	  	dropdown.disabled = true;
	      	  	dropdown.selectedIndex = 0;
      		}
      	},
      	
		resetDropdown: function(dropdown, text)
		{
    		var firstOption = document.createElement('option');
    		firstOption.value = '0';
    		firstOption.text = text;
        		
    		this.clearDropdown(dropdown);
				if(dropdown)
				{
    			dropdown.options.add(firstOption);
				}
		}
  	}
  	
  	function getStates(country)
  	{
        dropdownHandler.resetDropdown(document.form.sel_locCity,
                					  'Select a state/prov first');
		dropdownHandler.setLoading(document.form.sel_locState);
		
  	  	$.ajax({ type: "POST", url: getAjaxBaseURL(), 
  	  		     data: "action=ll&sel_locCountry=" + country.value,
				   success: function(xml) {
						var states = [];
	                    $(xml).find('state_list').find('option').each(function(){
	                    state = document.createElement("option");
	                    state.value = $(this).attr('value');
	                    state.text = $(this).text();
						states.push(state);
	                    });
              	        dropdownHandler.resetDropdown(document.form.sel_locState,
													  'Select a state/prov');
						dropdownHandler.fillDropdown(document.form.sel_locState,
													 states);
						document.form.sel_locState.disabled = false;
	                     }
	                    });
  	}

  	function getCities(country, state)
  	{
		dropdownHandler.setLoading(document.form.sel_locCity);
  	  	$.ajax({ type: "POST", url: getAjaxBaseURL(), 
  	  	  	     data: "action=ll&sel_locCountry=" + country.value + 
  	  	  	           "&sel_locState=" + state.value,
				 success: function(xml) {
						var cities = [];
	                    $(xml).find('city_list').find('option').each(function(){
	                    city = document.createElement("option");
	                    city.value = $(this).attr('value');
	                    city.text = $(this).text();
						cities.push(city);
	                    });
              	        dropdownHandler.resetDropdown(document.form.sel_locCity,
						  							  'Select a city');
						dropdownHandler.fillDropdown(document.form.sel_locCity,
								 					 cities);
						document.form.sel_locCity.disabled = false;
	                     }
	                    });
  	}

  	function getSexualOrientations(i_am, looking_for)
  	{
  	  	var len = looking_for.length;
  	  	var preferences = [];
  	  	for (var i in looking_for)
  	  	{
  	  	  	if (looking_for[i].checked)
  	  	  	{
  	  	  	  	preferences.push(looking_for[i].value);
  	  	  	}
  	  	}
  	  	if (preferences.length == 0)
  	  	{
  	  	  	dropdownHandler.resetDropdown(document.form.sel_orientation_1,
										  'Select looking for first');
  	  	  	dropdownHandler.resetDropdown(document.form.sel_orientation_2,
				  						  'Select looking for first');
  	  	}
  	  	else
  	  	{
    		dropdownHandler.setLoading(document.form.sel_orientation_1);
    		dropdownHandler.setLoading(document.form.sel_orientation_2);
      	  	$.ajax({ type: "POST", url: getAjaxBaseURL(), 
    	  	  	     data: "action=ol&sel_gender=" + i_am.value + 
    	  	  	     "&cbx_genderPreference[]=" +
    	  	  	 	 preferences.join('&cbx_genderPreference[]='),
    			 success: function(xml) {
    				   var dropdowns = [document.form.sel_orientation_1,
    				 				    document.form.sel_orientation_2];
    				   var i = 0;
                       $(xml).find('orientation_list').each(function(){
    					   var orientations = [];
                    	   var dropdown = dropdowns[i];
                           $(this).find('option').each(function(){
                       orientation = document.createElement("option");
                       orientation.value = $(this).attr('value');
                       orientation.text = $(this).text();
    				   orientations.push(orientation);
                       });
                        dropdownHandler.clearDropdown(dropdown);
    					dropdownHandler.fillDropdown(dropdown, orientations);
    					i++;
                        })
                        for (var j in dropdowns)
                        {
                        	dropdowns[j].disabled = false;
                        }
      	  		}
      	  	});
  	  	}
  	}
  	
  	function checkCouples(gender)
  	{
  	  	var partnerProfile = document.getElementById('partnerProfile');
  	  	var formDiv = document.getElementById('formDiv');
  	  	if (gender.options[gender.selectedIndex].value > 2)
  	  	{
  	  	  	formDiv.className = 'form h_600';
  	  	  	partnerProfile.style.display = 'block';
  	  	}
  	  	else
  	  	{
  	  	  	formDiv.className = 'form h_564';
  	  	  	partnerProfile.style.display = 'none';
  	  	}
  	}
		
		function createProfile()
		{
			// create profile
			var preferences = [];
			var looking_for = document.getElementsByName('cbx_genderPreference[]');
			for(n=0;n<looking_for.length;n++)
			{
				//alert( n + ': ' + looking_for[n].checked);
				if (looking_for[n].checked)
					{
							preferences.push(looking_for[n].value);
					}
			}
				
			var errors = new Array();
	
			var formData = "";
			formData += "action=pr";
			formData += "&account_id=" + $('#accountId').val();
			formData += "&txt_screenName=" + $('#txt_screenName').val();
			formData += "&txt_emailAddress=" + $('#txt_emailAddress').val();
			
			formData += "&txt_bdayDay_1=" + $('#sel_bdayDay_1').val();
			formData += "&txt_bdayMonth_1=" + $('#sel_bdayMonth_1').val();
			formData += "&txt_bdayYear_1=" + $('#sel_bdayYear_1').val();
			
			formData += "&cbx_genderPreference[]=" + preferences.join('&cbx_genderPreference[]=');
			formData += "&sel_gender=" + document.form.sel_gender.value; //$('#sel_gender').val();
			formData += "&cbx_over18=" + $('#cbx_over18').val();
			formData += "&sel_locCountry=" + document.form.sel_locCountry.value; //$('#sel_locCountry').val();
			formData += "&sel_locState=" + document.form.sel_locState.value; //$('#sel_locState').val();
			formData += "&sel_locCity=" + document.form.sel_locCity.value; //$('#sel_locCity').val();
			formData += "&txar_profileHeadline=" + ''; //$('#txar_profileHeadline').val();
			formData += "&txar_describeYourself=" + ''; //$('#txar_describeYourself').val();
			formData += "&txar_describeLookingFor=" + ''; //$('#txar_describeLookingFor').val();
			
			$.ajax({type: "POST",
						url: location.href,
						data: formData,
						success: function(xml)
						{
							if($(xml).find('success status').attr('code'))
							{
								//loginPopup(document.link_domain);
								
								loginAnimation();
							}
							else
							{
								$(xml).find('error_message').each(function(i)																				 
								{
									errors.push($(this).text());
								});
								
								errorPopup(errors);
							}
						}
					 });
			
			//return false;
		}
		
		function createAccount()
		{
			var errors = new Array();
			
			$.ajax({type: "POST",
						url: location.href,
						data: "action=ca" + "&txt_screenName=" + $('#txt_screenName').val()+ "&txt_emailAddress=" + $('#txt_emailAddress').val()+ "&txt_bdayDay_1=" + $('#sel_bdayDay_1').val()+ "&txt_bdayMonth_1=" + $('#sel_bdayMonth_1').val()+ "&txt_bdayYear_1=" + $('#sel_bdayYear_1').val(),
						success: function(xml)
						{
							if($(xml).find('success status').attr('code'))
							{
								//$('#step1').fadeOut(200,function(){$('#step2').fadeIn(200);});
															
								$(document.form).append('<input id="accountId" name="accountId" type="hidden" value="'+ $(xml).find('account_id').text() +'" />');
								createProfile();
							}
							else
							{
								//alert($(xml).find('error status').attr('code'));
								$(xml).find('error_message').each(function(i)																				 
								{
									errors.push($(this).text());
								});
								
								errorPopup(errors);
							}
						}
					 });
		}
		
		function loginAnimation()
		{
			//$("#loginForm").animate({opacity:"1"},1000,"linear",fadeWithDelay);
			fadeWithDelay();
			
			function fadeWithDelay()
			{
				$("#tBubble").fadeOut(1000);
				$("#onlineContainer").fadeOut(1000);
				$(".footer").css({'border':'none'});
				$("#video_page1").fadeOut(1000,delFlash);
				$("#page1").fadeOut(1000,fadeInPage2);
			}
			
			function delFlash()
			{
				$("#video_page1").remove();
			}
			
			function fadeInPage2()
			{
				var email = $('#txt_emailAddress').val();
				var screenname = $('#txt_screenName').val();
				$("#p2_email").text(email);
				$("#p2_screennmae").val(screenname);
				$("#page2").fadeIn(1000,function(){openEmail(email);});
				$("#video_page2").fadeIn(1000);
			}
		}
		
		function openEmail(domainName){
			if (domainName.indexOf('yahoo') != -1){
				window.open("http://mail.yahoo.com");
			}else if (domainName.indexOf('ameritech.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('bellsouth.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('flash.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('pacbell.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('prodigy.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('sbcglobal.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('snet.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('swbell.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('wans.net') != -1){
				window.open("http://webmail.att.net");
			}else if (domainName.indexOf('hotmail.com') != -1){
				window.open("http://hotmail.com");
			}else if (domainName.indexOf('gmail.com') != -1){
				window.open("http://gmail.com");
			}else if (domainName.indexOf('comcast.net') != -1){
				window.open("http://www.comcast.net");
			}else if (domainName.indexOf('aol.com') != -1){
				window.open("http://mail.aol.com");
			}else if (domainName.indexOf('aol.ca') != -1){
				window.open("http://mail.aol.ca");
			}
		}
		
		$(document).ready(function()
		{
			//alert(getAjaxBaseURL());
			//alert(getTourBaseURL());
			
			//loginAnimation();
			
			$(document.form).submit(function()
			{
				$('#btn_join').css({'display':'none'});
				
				if($('#accountId').val())
				{
					createProfile();
				}
				else
				{
					// check errors then create account
					var preferences = [];
					
					var looking_for = document.getElementsByName('cbx_genderPreference[]');
					//alert(looking_for.length);
					
					for(n=0;n<looking_for.length;n++)
					{
						//alert( n + ': ' + looking_for[n].checked);
						if (looking_for[n].checked)
							{
									preferences.push(looking_for[n].value);
							}
					}
					
					/*for (var i in looking_for)
					{
							//alert( i + ': ' + looking_for[i].checked);
							if (looking_for[i].checked)
							{
									preferences.push(looking_for[i].value);
							}
					}*/
				
					var formData = "";
					formData += "action=er";
					formData += "&txt_screenName=" + $('#txt_screenName').val();
					formData += "&txt_emailAddress=" + $('#txt_emailAddress').val();
					
					formData += "&txt_bdayDay_1=" + $('#sel_bdayDay_1').val();
					formData += "&txt_bdayMonth_1=" + $('#sel_bdayMonth_1').val();
					formData += "&txt_bdayYear_1=" + $('#sel_bdayYear_1').val();
					
					formData += "&cbx_genderPreference=" + preferences.join('&cbx_genderPreference[]=');
					formData += "&cbx_over18=" + $('#cbx_over18').is(':checked');
					formData += "&sel_locCountry=" + document.form.sel_locCountry.value; //$('#sel_locCountry').val();
					formData += "&sel_locState=" + document.form.sel_locState.value; //$('#sel_locState').val();
					formData += "&sel_locCity=" + document.form.sel_locCity.value; //$('#sel_locCity').val();
					
					//$('#sel_locCountry').css({'border':'1px solid red'});
					//alert($('#sel_locCountry').value);
					//alert($('#sel_locCountry').parent().get());
					//alert(document.form.sel_locCountry.value);
					//return false;
					
					$.ajax({type: "POST",
									url: getAjaxBaseURL(),
									dataType: 'json',
									data: formData,
									success: function(data,textStatus)
									{
										if(data[0] == 'ready')
										{
											createAccount();
										}
										else
										{
											errorPopup(data);	
										}
										
									}
								});
					
					//return false;
				}
				
				return false;
				//alert($('form').serialize());
			});
		});
