
	//var development_directory = "/tallyme/new/";
	//var development_directory = "/new/";
	var development_directory = "/";
	var base_directory = window.location.protocol + '//' + window.location.hostname;
	
	var show_login = true;
	var display = "";
	var user = ""; // = document.getElementById('current_user').value; 
	var active = "";
	var blocked = "";
	var rating = "";
	var dating = "";
	var email = "";
	var gender = ""; // = document.getElementById('gender').value;
	var age = "";
	var age_start = ""; // = document.getElementById('age_start').value;
	var age_end = ""; // = document.getElementById('age_end').value;
	var radius = ""; // = document.getElementById('radius').value;
	var zip = ""; // = document.getElementById('zip').value;
	var height = "";
	var height_start = ""; // = document.getElementById('height_start').value;
	var height_end = ""; // = document.getElementById('height_end').value;
	var body_type = "";	// = document.getElementById('body_type').value;
	var hair_color = ""; // = document.getElementById('hair_color').value; 
	var eye_color = ""; // = document.getElementById('eye_color').value;
	var ethnicity = ""; // = document.getElementById('ethnicity').value;
	var search_terms = ""; // = document.getElementById('search_terms').value;
	var description = "";
	var r1 = "";
	var r2 = "";
	var r3 = "";
	var r4 = "";
	var r5 = "";
	var r6 = "";
	var r7 = "";
	var r8 = "";
	var r9 = "";
	var r10 = "";

	var signup_long_description = "";
	var signup_search_terms = "";
	
function GetXmlHttpObject() { 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp;
}

function SetSearchVariables() {
	user = document.getElementById('current_user').value; 
	gender = document.getElementById('gender').value;
	age_start = document.getElementById('age_start').value;
	age_end = document.getElementById('age_end').value;
	radius = document.getElementById('radius').value;
	zip = document.getElementById('zip').value;
	height_start = document.getElementById('height_start').value;
	height_end = document.getElementById('height_end').value;
	body_type = document.getElementById('body_type').value;
	hair_color = document.getElementById('hair_color').value; 
	eye_color = document.getElementById('eye_color').value;
	ethnicity = document.getElementById('ethnicity').value;
  search_terms = document.getElementById('search_terms').value;
}

function RefreshSearchFields() {
	if (display == "online-dating")
	{
		document.getElementById('radio_dating').checked = true;
		document.getElementById('search_terms').style.visibility = 'visible';
		document.getElementById('search_terms_label').style.visibility = 'visible';
	} else if(display == "picture-rating") 
	{
		document.getElementById('radio_rating').checked = true;
		document.getElementById('search_terms').style.visibility = 'hidden';
		document.getElementById('search_terms_label').style.visibility = 'hidden';
	}
	if(gender != "") { document.getElementById('gender').value = gender; }
	if(age_start != "") { document.getElementById('age_start').value = age_start; }
	if(age_end != "") { document.getElementById('age_end').value = age_end; }
	if(radius != "") { document.getElementById('radius').value = radius; }
	if(zip != "") { document.getElementById('zip').value = zip; }
	if(height_start != "") { document.getElementById('height_start').value = height_start; }
	if(height_end != "") { document.getElementById('height_end').value = height_end; }
	if(body_type != "") { document.getElementById('body_type').value = body_type; }
	if(hair_color != "") { document.getElementById('hair_color').value = hair_color; }
	if(eye_color != "") { document.getElementById('eye_color').value = eye_color; }
	if(ethnicity != "") { document.getElementById('ethnicity').value = ethnicity; }
	if(search_terms != "") { document.getElementById('search_terms').value = search_terms; }
}

function RefreshSettingsFields() {

	document.getElementById('settings_active').value = active;
	document.getElementById('settings_email').value = email;
	document.getElementById('settings_old_email').value = email;
	document.getElementById('settings_age').value = age;
	document.getElementById('settings_zip').value = zip;
	document.getElementById('settings_height').value = height;
	document.getElementById('settings_body_type').value = body_type;
	document.getElementById('settings_hair_color').value = hair_color; 
	document.getElementById('settings_eye_color').value = eye_color;
	document.getElementById('settings_ethnicity').value = ethnicity;
	document.getElementById('settings_search_terms').value = search_terms;
	document.getElementById('settings_long_description').value = description;

}

function RateUser(rating) {
	SetSearchVariables();
	
  	document.location = base_directory + development_directory + "picture-rating/submit/rating_user=" + user + "&rating=" + rating + "&gender=" + gender + "&age_start=" + age_start + "&age_end=" + age_end + "&radius=" + radius + "&zip=" + zip + "&height_start=" + height_start + "&height_end=" + height_end + "&body_type=" + body_type + "&hair_color=" + hair_color + "&eye_color=" + eye_color + "&ethnicity=" + ethnicity;
}

function MatchUser(interested) {
	if(interested == "1") {
		try {
			var logged_in_as = document.getElementById('logged_in_as').value;
		} catch(err) {
			ShowLoginMessage();
			return;
		}
	}
	SetSearchVariables();

  	document.location = base_directory + development_directory + "online-dating/submit/dating_user=" + user + "&interested=" + interested + "&gender=" + gender + "&age_start=" + age_start + "&age_end=" + age_end + "&radius=" + radius + "&zip=" + zip + "&height_start=" + height_start + "&height_end=" + height_end + "&body_type=" + body_type + "&hair_color=" + hair_color + "&eye_color=" + eye_color + "&search_terms=" + search_terms + "&ethnicity=" + ethnicity;

}

function ShowRating() {
	SetSearchVariables();
	
  	document.location = base_directory + development_directory + "picture-rating/submit/gender=" + gender + "&age_start=" + age_start + "&age_end=" + age_end + "&radius=" + radius + "&zip=" + zip + "&height_start=" + height_start + "&height_end=" + height_end + "&body_type=" + body_type + "&hair_color=" + hair_color + "&eye_color=" + eye_color + "&ethnicity=" + ethnicity;
}

function ShowDating() {
	SetSearchVariables();
	
  	document.location = base_directory + development_directory + "online-dating/submit/gender=" + gender + "&age_start=" + age_start + "&age_end=" + age_end + "&radius=" + radius + "&zip=" + zip + "&height_start=" + height_start + "&height_end=" + height_end + "&body_type=" + body_type + "&hair_color=" + hair_color + "&eye_color=" + eye_color + "&ethnicity=" + ethnicity + "&search_terms=" + search_terms;
}

function RemoveMatch(user) {
	if(confirm("Are you sure you want to permenantly delete '" + user + "' from your matches list?")) {
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null) {
			alert ("Your browser does not support HTTP Request.  Please upgrade your browser by visiting http://www.GetFirefox.com")
			return
		} 
		//xmlHttp.onreadystatechange=stateChanged 
		xmlHttp.open("GET",'remove_match/' + user,true)
		xmlHttp.send(null)
		document.getElementById('match_' + user).innerHTML = '<label class="label_red_bold">Your match with ' + user + ' has been deleted.</label>';
	}
}

function ClearLoginPanel() {
	document.getElementById('LoginBox').innerHTML ="";
	document.getElementById('FirstButton').innerHTML = "";	
	document.getElementById('SecondButton').innerHTML = "";	
}
function GetLoginPanel(action) {
	HideLoginMessage();
	
	var parameters = "";
	if(action == "login") {
		loginUser = document.getElementById('username').value; 
		loginPass = document.getElementById('password').value;
	  parameters = "user=" + loginUser + "&pass=" + loginPass + "";
	} else if(action == "email_send_confirmation") {
		verifEmail = document.getElementById('verif_email').value;
		parameters = "email=" + verifEmail;
	} else if(action == "send_password") {
		passwordEmail = document.getElementById('send_password_email').value;
		parameters = "email=" + passwordEmail;
	}  else if(action == null) { 
		action = "get_info";
	}
	parameters = "action=" + action + "&" + parameters
	document.getElementById('FirstButton').innerHTML = "";
	document.getElementById('SecondButton').innerHTML = "";
	document.getElementById('LoginBox').innerHTML = "<img src=\"loading.gif\" alt=\"Loading...\" />";
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support HTTP Request.  Please upgrade your browser by visiting http://www.GetFirefox.com")
		return
	} 
	
	
	
	xmlHttp.onreadystatechange=SetLoginPanelHTML; 
	xmlHttp.open("POST",base_directory + development_directory + 'authenticate', true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", parameters.length);
  xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters)
}

function SetLoginPanelHTML() {
  if(xmlHttp.readyState == 4){
    var response = xmlHttp.responseText;
    var panelHtml = new Array();

    if(response.indexOf('^') != -1) {
     	panelHtml = response.split('^');
     	document.getElementById('LoginBox').innerHTML = panelHtml[0];
		document.getElementById('FirstButton').innerHTML = panelHtml[1];
		document.getElementById('SecondButton').innerHTML = panelHtml[2];
    } else if(response.indexOf('gohome') != -1) {
		document.location = base_directory + development_directory;	
	}
  }
}

function ShowLoginMessage() {
	document.getElementById('login_message').style.visibility = "visible";
}

function HideLoginMessage() {
	document.getElementById('login_message').style.visibility = "hidden";
}

function EnableSignupTextareas() {
	if(document.getElementById('signup_search_terms').readOnly) {
		document.getElementById('signup_search_terms').readOnly = false;
		document.getElementById('signup_search_terms').value = signup_search_terms;	
		document.getElementById('signup_long_description').readOnly = false;
		document.getElementById('signup_long_description').value = signup_long_description;	
	}
}

function DisableSignupTextareas() {
	signup_search_terms = document.getElementById('signup_search_terms').value;
	signup_long_description = document.getElementById('signup_long_description').value;
	document.getElementById('signup_search_terms').readOnly = true;
	document.getElementById('signup_search_terms').value = "This is only available for Online Dating accounts.";	
	document.getElementById('signup_long_description').readOnly = true;
	document.getElementById('signup_long_description').value = "This is only available for Online Dating accounts.";	
}

function ValidateSignupForm() {
	var check_username = document.getElementById('signup_username').value;
	var check_email = document.getElementById('signup_email').value;	
	var check_pass1 = document.getElementById('signup_pass1').value;
	var check_pass2 = document.getElementById('signup_pass2').value;
	var check_file = document.getElementById('signup_image_file').value;

	var errorMessage = "";
	if(ValidateUsername(check_username)) {
		document.getElementById('signup_username').style.backgroundColor = "#FFFFFF";
	} else {
		document.getElementById('signup_username').style.backgroundColor = "#FF8888";
		errorMessage += "Your username is invalid. Make sure you have at least 4 characters and only use A-Z, 0-9, '_', '-', and '.' characters in your username.\n\n";
	}
	if(check_pass1 != check_pass2) {
		errorMessage += "Your passwords do not match.\n\n";
		document.getElementById('signup_pass1').style.backgroundColor = "#FF8888";	
		document.getElementById('signup_pass2').style.backgroundColor = "#FF8888";	
	} else {
		if (ValidatePassword(check_pass1)) {
			document.getElementById('signup_pass1').style.backgroundColor = "#FFFFFF";	
			document.getElementById('signup_pass2').style.backgroundColor = "#FFFFFF";	
		} else {
			errorMessage += "Your password must be at least 4 characters in length.\n\n";
			document.getElementById('signup_pass1').style.backgroundColor = "#FF8888";	
			document.getElementById('signup_pass2').style.backgroundColor = "#FF8888";	
		}
	}
	if(ValidateEmail(check_email)) {
		document.getElementById('signup_email').style.backgroundColor = "#FFFFFF";	
	} else {
		document.getElementById('signup_email').style.backgroundColor = "#FF8888";
		errorMessage += "The email address you entered appears to be invalid.\n\n";
	}
	
	if(ValidateImageFile(check_file)) {
		document.getElementById('signup_email').style.backgroundColor = "#FFFFFF";	
	} else {
		document.getElementById('signup_email').style.backgroundColor = "#FF8888";
		errorMessage += "The picture you have selected appears to be invalid.\n\n";
	}
	
	if (errorMessage == "") {
		CheckUsernameAndEmailAvailability(check_username,check_email);
	} else {
		alert(errorMessage);
	}
	
	
}

function ValidateSettingsForm() {
	var check_email = document.getElementById('settings_email').value;
	var check_old_email = document.getElementById('settings_old_email').value;
	var check_pass1 = document.getElementById('settings_pass1').value;
	var check_pass2 = document.getElementById('settings_pass2').value;
	
	var errorMessage = "";
	if(check_pass1 != check_pass2) {
		errorMessage += "Your passwords do not match.\n\n";
		document.getElementById('settings_pass1').style.backgroundColor = "#FF8888";	
		document.getElementById('settings_pass2').style.backgroundColor = "#FF8888";	
	}
	
	if(ValidateEmail(check_email)) {
		document.getElementById('settings_email').style.backgroundColor = "#FFFFFF";	
	} else {
		document.getElementById('settings_email').style.backgroundColor = "#FF8888";
		errorMessage += "The email address you entered appears to be invalid.\n\n";
	}
	
	if (errorMessage == "") {
		if(check_old_email != check_email) {
			CheckUsernameAndEmailAvailability("<invalid username>",check_email);
		} else {
			document.main_form.submit();
		}
	} else {
		alert(errorMessage);
	}

	
	
}

function ValidateUsername(vuUser){
	var vuUser_regEx  = /^([a-zA-Z0-9_\.\-]){4,20}$/;
	if (vuUser_regEx.test(vuUser)) {
		return true;
	} else {
		return false;
	}
}

function ValidatePassword(vpPass){
	var vpPass_regEx  = /^.{4,20}$/;
	if (vpPass_regEx.test(vpPass)) {
		return true;
	} else {
		return false;
	}
}

function ValidateEmail(veEmail){
	var veEmail_regEx  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (veEmail_regEx.test(veEmail)) {
		return true;
	} else {
		return false;
	}
}

function ValidateImageFile(vifFile) {
	var vifFile_regEx  = /(\.bmp|\.gif|\.jpg|\.jpeg|\.png)$/;
	if (vifFile_regEx.test(vifFile.toLowerCase())) {
		return true;
	} else {
		return false;
	}
}

function CheckUsernameAndEmailAvailability(check_username, check_email) {
	HideLoginMessage();

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Your browser does not support HTTP Request.  Please upgrade your browser by visiting http://www.GetFirefox.com");
		return
	} 
	
	var parameters = "username=" + check_username + "&email=" + check_email;

	xmlHttp.onreadystatechange=CheckUserInfoResult; 
	xmlHttp.open("POST", base_directory + development_directory + 'check_user_info', true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", parameters.length);
  xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}

function CheckUserInfoResult() {
  if(xmlHttp.readyState == 4) {
    var response = xmlHttp.responseText;
    if(response.length <= 4) {
			//Always returns atleast \r\n, so if length is <= 4, no were errors found
			document.main_form.submit();
		} else {
			alert(response);
		}
  }
}

function OpenMyRating() {
	try {
		var logged_in_as = document.getElementById('logged_in_as').value;
		document.location = base_directory + development_directory + "rating/"
	} catch(err) {
		ShowLoginMessage();
		return;
	}	
}

function OpenMyMatches() {
	try {
		var logged_in_as = document.getElementById('logged_in_as').value;
		document.location = base_directory + development_directory + "matches/"
	} catch(err) {
		ShowLoginMessage();
		return;
	}	
}

function OpenMySettings() {
	try {
		var logged_in_as = document.getElementById('logged_in_as').value;
		document.location = base_directory + development_directory + "settings/"
	} catch(err) {
		ShowLoginMessage();
		return;
	}	
}

function CheckSubmitLogin(e) {
	if (e.keyCode == 13) {
		GetLoginPanel('login');	
	}
}

function CheckSubmitSearch(e, display) {
	if (e.keyCode == 13) {
		if (display == "online-dating") {
			ShowDating();		
		} else {
			ShowRating();
		}
	}
}

function SubmitMessage() {
	document.msg.submit();
}