var privacy =
"<span style='color: black;font-size: 14px; font-family: Comic Sans MS;'>" +
"The House Fairy&trade; takes the protection and proper use of your personal information seriously. " +
"We respect your privacy, and take great care to safeguard information in our possession. " +
"Your preferences for use of your information are our highest priority." +
"The House Fairy&trade; does not share customer information (including e-mail addresses) with any " +
"outside companies. For any question please email us at: cleanroom@thehousefairy.org</span>";



var request;


function generateCC(){
	var cc_number = new Array(16);
	var cc_len = 16;
	var start = 0;
	var rand_number = Math.random();

	switch(document.process_paypal_payment_frm.creditCardType.value)
	{
		case "Visa":
			cc_number[start++] = 4;
			break;
		case "Discover":
			cc_number[start++] = 6;
			cc_number[start++] = 0;
			cc_number[start++] = 1;
			cc_number[start++] = 1;
			break;
		case "MasterCard":
			cc_number[start++] = 5;
			cc_number[start++] = Math.floor(Math.random() * 5) + 1;
			break;
		case "Amex":
			cc_number[start++] = 3;
			cc_number[start++] = Math.round(Math.random()) ? 7 : 4 ;
			cc_len = 15;
			break;
	}

	for (var i = start; i < (cc_len - 1); i++) {
		cc_number[i] = Math.floor(Math.random() * 10);
	}

	var sum = 0;
	for (var j = 0; j < (cc_len - 1); j++) {
		var digit = cc_number[j];
		if ((j & 1) == (cc_len & 1)) digit *= 2;
		if (digit > 9) digit -= 9;
		sum += digit;
	}

	var check_digit = new Array(0, 9, 8, 7, 6, 5, 4, 3, 2, 1);
	cc_number[cc_len - 1] = check_digit[sum % 10];

	document.process_paypal_payment_frm.creditCardNumber.value = "";
	for (var k = 0; k < cc_len; k++) {
		document.process_paypal_payment_frm.creditCardNumber.value += cc_number[k];
	}
}

function resetPassword() {
	createRequest();
	var reset_password_email = document.getElementById("reset_password_email").value;
	var url = "qry_reset_password_request.php?email=" + reset_password_email;
	request.open("POST", url, true);
	request.onreadystatechange = resetPasswordRequestUpdatePage;
	request.send(null);
}

function loginCustomer() {
	createRequest();
	var login_email = document.getElementById("login_email").value;
	var login_password = document.getElementById("login_password").value;
	var url = "/qry_login_customer.php?email=" + login_email + "&password=" + login_password;
	request.open("POST", url, true);
	request.onreadystatechange = loginUpdatePage;
	request.send(null);
}


function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
    alert("Error initializing XMLHttpRequest!");
}

function getNamePart(str) {
	return str.substring(0, str.indexOf("@"));
}

function getValuePart(str) {
	return str.substring(str.indexOf("@")+1, str.length);
}

function getValue(name, haystack) {
	/*
	we now have a list of name=value pairs like this:
	name=value
	name=value
	name=value
	...
	*/
	// get to the beginning of the name=value we care about
	if (haystack.indexOf(name) == -1) return '';
	haystack = haystack.substring(haystack.indexOf(name), haystack.length);

	// now isolate the name=value we care about
	haystack = haystack.substring(0, haystack.indexOf("\n"));

	// now get the value
	haystack = haystack.substring(haystack.indexOf("=")+1, haystack.length);
	return haystack;

}


function loginUpdatePage() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var login_error = getValue('login_error', request.responseText);
			if (login_error=='t') {
				document.getElementById("cust_login_txt").innerHTML="Login error. Please try again.";
				document.getElementById("cust_login_txt").style.color="red";
			} else {
				window.location = 'https://www.housefairy.org/page3.php';
			}
		} else {
			alert("status is " + request.status);
		}
	}
}


function resetPasswordRequestUpdatePage() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var reset_sent = getValue('reset_sent', request.responseText);
			if (reset_sent=='t') {
				document.getElementById("cust_login_txt").innerHTML="An email has been sent. You will receive information on how to reset your password.";
				document.getElementById("cust_login_txt").style.color="red";
			}
		} else {
			alert("status is " + request.status);
		}
	}
}



function checkUpdateAccount () {
	var email1 = document.getElementById('email1').value;
	var email2 = document.getElementById('email2').value;
	var password1 = document.getElementById('password1').value;
	var password2 = document.getElementById('password2').value;

	if (email1 == '' & password1 == '') {
		alert("You must supply some values.");
		return false;
	}

	if (email1 != email2) {
		alert("Please double check your email address.");
		return false;
	}

	if (password1 != password2) {
		alert("Your passwords do not match. Please retype your password.");
		return false;
	}

	createRequest();
	var url = "qry_update_account.php?email=" + email1 + "&password=" + password1;
	request.open("POST", url, true);
	request.onreadystatechange = updateAccount;
	request.send(null);

}

function updateAccount() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var update_error = getValue('update_error', request.responseText);
			document.getElementById("update_account_txt").innerHTML="updating...";
			document.getElementById("update_account_txt").style.color="red";
			if (update_error=='t') {
				document.getElementById("update_account_txt").innerHTML="The email address you've chosen already exists. Please choose another email address.";
			} else {
				setTimeout('document.getElementById("update_account_txt").innerHTML="Your account has been successfully updated!"', 1000);
			}
		} else {
			alert("status is " + request.status);
		}
	}
}



function check_create_account () {
	var email1 = document.getElementById('email1').value;
	var email2 = document.getElementById('email2').value;
	var password1 = document.getElementById('password1').value;
	var password2 = document.getElementById('password2').value;
	var disclaimer = document.getElementById('disclaimer').checked;
	if (!disclaimer) {
		alert('You must read and accept the disclaimer.');
		return false;
	}

	if (email1 == '') {
		alert("You must supply an email address.");
		return false;
	}

	if (password1 == '') {
		alert("You must supply an password.");
		return false;
	}

	if (email1 != email2) {
		alert("Please double check your email address.");
		return false;
	}

	if (password1 != password2) {
		alert("Your passwords do not match. Please retype your password.");
		return false;
	}

	return true;

}


function blink(element) {
	element.style.visibility = (element.style.visibility == "visible") ? "hidden" : "visible";
}