
var processingSale = 0;
var processingRegister = 0;

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

document.onkeypress = stopRKey;

function submitForm(event) {
	if (event.which == "13") {
		return true;
	} else {
		return false;
	}
}

function switchShow(obj) {
	if ($(obj).is(":visible")) {
		$(obj).hide();
	} else {
		$(obj).show();
	}
}

function setLanguage(lang) {
	$.post("ajax/setlanguage.php", { lang: lang }, function(data) {
		window.location.reload();
	})
}

function switchNewsletterLang(lang) {
	$.post("ajax/switchnewsletterlang.php", { lang: lang }, function(data) {
		window.location.reload();
	})
}

function toggleNewsletter(state)
{
	$.post("ajax/togglenewsletter.php", { state: state }, function(data) {
		window.location.reload();
	})
}

function changeshippingaddress()
{
	var address = $("#changeaddress1").val();
	var city = $("#changecity").val();
	var state = $("#changeshippingstate").val();
	var otherstate = $("#shippingotherstate").val();
	var country = $("#shippingcountry").val();
	var zip = $("#changezip").val();
	
	$.post("ajax/changeaddress.php", { address1: address, country: country, state: state, otherstate: otherstate, city: city, zip: zip}, function(data) {
		if (data == "error1") 
		{
			$("#changenameerror").html("Every field is required.");
		}
		else if (data == "error2")
		{
			$("#changenameerror").html("This is not a valid postal code.");
		}
		else
		{
			window.location.reload();
		}
	})
}

function login() {
	var email = $("#login_email").val();
	var pass = $("#login_pass").val();

	$.post("ajax/login.php", { email: email, pass: pass }, function(data) 
	{

		if (data == "1") 
		{
			window.location = "index.php";
		} 
		else 
		{
			$("#login_msg").html("Your username or password is invalid.");
			$("#login_msg").css("color", "red");
		}
	})
}

function sendContactForm() {
	var name = $("#ContactFirstName").val();
	var email = $("#ContactEmail").val();
	var subject = $("#ContactSubjectId").val();
	var msg = $("#ContactMessage").val();
	$.post("ajax/contact.php", { name: name, email: email, subject: subject, msg: msg }, function(data) {
		if (data == "error1") {
			$("#contactus_msg").html("Please enter a name.");
			$("#contactus_msg").css("color", "red");
		} else if (data == "error2") {
			$("#contactus_msg").html("Please enter an email address.");
			$("#contactus_msg").css("color", "red");
		} else if (data == "error3") {
			$("#contactus_msg").html("Please enter a subject.");
			$("#contactus_msg").css("color", "red");
		} else if (data == "error4") {
			$("#contactus_msg").html("Please enter a message.");
			$("#contactus_msg").css("color", "red");
		} else if (data == "error5") {
			$("#contactus_msg").html("This is not a valid email address.");
			$("#contactus_msg").css("color", "red");
		} else if (data == "1") {
			$("#contactus_msg").html("Thank you. We will get back to you within the next 48 hours.");
			$("#contactus_msg").css("color", "green")
			$("#ContactMessage").val("");
		}
	})
}

function subscribe() {
	var email = $("#subscribe_email").val();
	var city = $("#subscribe_city").val();
	var network = $("#subscribe_network").val();
	$.post("ajax/subscribe.php", { email: email, city: city, network: network }, function(data) {
		if (data == "1") {
			$("#subscribe_msg").html("You have been subscribed!");
			$("#subscribe_msg").css("color", "green");
			window.location.href = "/subscribethankyou.php";
		} else if (data == "error1") {
			$("#subscribe_msg").html("Please enter an email address.");
			$("#subscribe_msg").css("color", "red");
		} else if (data == "error2") {
			$("#subscribe_msg").html("This is not a valid email address.");
			$("#subscribe_msg").css("color", "red");
		} else if (data == "error3") {
			$("#subscribe_msg").html("This address is already registered!");
			$("#subscribe_msg").css("color", "red");
		}
	})
}

function subscribe2() {
	var email = $("#homeEmail").val();
	var city = $("#homeCityId").val();
	var network = $("#subscribe_network").val();
	$.post("ajax/subscribe.php", { email: email, city: city, network: network}, function(data) {
		if (data == "1") {
			$("#subscribe2_msg").html("You have been subscribed!");
			$("#subscribe2_msg").css("color", "green");
			window.location.href = "/subscribethankyou.php";
		} else if (data == "error1") {
			$("#subscribe2_msg").html("Please enter an email address.");
			$("#subscribe2_msg").css("color", "red");
		} else if (data == "error2") {
			$("#subscribe2_msg").html("This is not a valid email address.");
			$("#subscribe2_msg").css("color", "red");
		} else if (data == "error3") {
			$("#subscribe2_msg").html("This address is already registered!");
			$("#subscribe2_msg").css("color", "red");
		}
	})
}

function hideSubscription() {
	$(".subscriptionbox").slideToggle("slow");
	$.post("ajax/hidesubscription.php");
}

function register() {
	if (processingRegister == 0)
		processingRegister = 1;
	else
		return 0;
		
	$(".register_msg").css("color", "black");
	$.post("ajax/register.php", {
		fname: $("#register_fname").val(),
		lname: $("#register_lname").val(),
		email: $("#register_email").val(),
		email2: $("#register_email2").val(),
		pass: $("#register_pass").val(),
		pass2: $("#register_pass2").val(),
		terms: $("#register_terms").attr("checked")
	}, function(data) {
		processingRegister = 0;
		if (data == "error1")
		{
			$("#registerError").html("Please enter a first name.");
			$("#registerError").css("color", "red");
		} 
		else if (data == "error2") 
		{
			$("#registerError").html("Your first name needs to have between 2 and 50 characters.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error3") 
		{
			$("#registerError").html("This is not a valid first name.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error4") 
		{
			$("#registerError").html("Please enter a last name.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error5") 
		{
			$("#registerError").html("Your last name needs to have between 2 and 50 characters.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error6") 
		{
			$("#registerError").html("This is not a valid last name.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error7") 
		{
			$("#registerError").html("Please enter your email address.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error8") 
		{
			$("#registerError").html("This is not a valid email address.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error9") 
		{
			$("#registerError").html("This email address is already in use.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error10") 
		{
			$("#registerError").html("Make sure both email addresses match.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error11") 
		{
			$("#registerError").html("Please enter a password.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error12") 
		{
			$("#registerError").html("Your password must contain between 6 and 12 characters and can only contain !~#$%^.-_=+ as special characters.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error13") 
		{
			$("#registerError").html("Make sure both passwords are the same.");
			$("#registerError").css("color", "red");
		}
		else if (data == "error14") 
		{
			$("#registerError").html("Make sure you agree to our Terms and Conditions.");
			$("#registerError").css("color", "red");
		}
		else if (data == "1") {
			processingRegister = 1;
			window.location = "registerthankyou.php";
		}
	})
}

function resetPassword() {
	var email = $("#forgot_email").val();
	$.post("ajax/resetpass.php", { email: email }, function(data) {
		if (data == "1") {
			$("#forgot_msg").html("Instructions on how to reset your password have been sent to your email.");
			$("#forgot_msg").css("color", "green");
		} else if (data == "error1") {
			$("#forgot_msg").html("Please enter an email address.");
			$("#forgot_msg").css("color", "red");
		} else if (data == "error2") {
			$("#forgot_msg").html("This email address is not registered.");
			$("#forgot_msg").css("color", "red");
		} else if (data == "error3") {
			$("#forgot_msg").html("Your account has been generated via Facebook.  Please use your Facebook login to connect.");
			$("#forgot_msg").css("color", "red");
		} else if (data == "error4") {
			window.location = "index.php";
		}
	})
}

function newPassword()
{
	var pass1 = $("#pass1").val();
	var pass2 = $("#pass2").val();
	var key = $("#key").val();
	var email = $("#email").val();

	$.post("ajax/newpassword.php", { pass1: pass1, pass2: pass2, key: key, email: email }, function(data) {

		if (data == "1") {
			$("#newpass_msg").html("Your password was changed successfully.");
			$("#newpass_msg").css("color", "green");
		} else if (data == "error1") {
			$("#newpass_msg").html("Enter a password");
			$("#newpass_msg").css("color", "red");
		} else if (data == "error2") {
			$("#newpass_msg").html("Your password must contain between 6 and 12 characters and can only contain !~#$%^.-_=+ as special characters.");
			$("#newpass_msg").css("color", "red");
		} else if (data == "error3") {
			$("#newpass_msg").html("Make sure you enter the same password twice.");
			$("#newpass_msg").css("color", "red");
		} else if (data == "error4") {
			window.location = "index.php";
		}
	})
}

function buy(id) {
	$("#buy_msg").hide();
	$.post("ajax/buy.php", { id: id }, function(data) {
		if (data == "1") {
			window.location = "cart.php";
		} else if (data == "error2") {
			$("#buy_msg").html("This deal is expired.");
			$("#buy_msg").show();
		} else if (data == "error3") {
			$("#buy_msg").html("This deal is full.");
			$("#buy_msg").show();
		} else if (data == "error4") {
			$("#buy_msg").html("You cannot buy more of this deal.");
			$("#buy_msg").show();
		}
	})
}

function sale() {
	if (processingSale == 0)
		processingSale = 1
	else
		return 0;
		
	$("#sale_msg").html("Processing...");
	$("#sale_msg").css("color", "black");
	$.post("ajax/sale.php", {
		id: $("#deal_id").val(),
		fname: $("#DealFirstName").val(),
		lname: $("#DealLastName").val(),
		address1: $("#DealAddress").val(),
		city: $("#DealCity").val(),
		zip: $("#DealZip").val(),
		state: $("#DealState").val(),
		country: $("#cart_country").val(),
		existing_card: $("input:radio[name=existing_card]:checked").val(),
		cctype: $("#DealCreditCardType").val(),
		ccnumber: $("#DealCreditCardNumber").val(),
		expm: $("#DealExpDateMonthMonth").val(),
		expy: $("#DealExpDateYearYear").val(),
		cvv: $("#DealCvv2Number").val(),
		terms: $("#terms").attr("checked"),
		otherstate: $("#DealOtherState").val(),
		qty: $("#qty").val()
	}, function(data) {
		//alert(data);
		processingSale = 0;
		if (data == "error1") {
			$("#sale_msg").html("Every field is required.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error2") {
			$("#sale_msg").html("This is not a valid name.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error10") {
			$("#sale_msg").html("This is not a valid postal code.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error11") {
			$("#sale_msg").html("This is not a valid credit card number.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error12") {
			$("#sale_msg").html("This is not a valid CVV number.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error13") {
			$("#sale_msg").html("You must agree to the Terms and Conditions.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error14") {
			$("#sale_msg").html("There is a problem with your card. Please try another one.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error15") {
			$("#sale_msg").html("The deal is sold out.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error16") {
			$("#sale_msg").html("Province/State is missing.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error18") {
			$("#sale_msg").html("Deal has already ended.");
			$("#sale_msg").css("color", "red");
		} else if (data == "errorcard") {
			$("#sale_msg").html("There is a problem with your card. Please try another one.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error") {
			$("#sale_msg").html("There was an error with your purchase.");
			$("#sale_msg").css("color", "red");
		} else if (data != "error") {
			processingSale = 1;
			$("#sale_msg").html("Please wait while your transaction is being processed...");
			$("#sale_msg").css("color", "green");
			window.location = "receipt.php?a=" + data;
		}
	})
}

function registerThenSale() {
	if (processingSale == 0)
		processingSale = 1;
	else
		return 0;
		
	$("#sale_msg").html("Processing...");
	$("#sale_msg").css("color", "black");
	//alert( $("#cart_country").val());
	//alert( $("#DealState").val());
	$.post("ajax/registersale.php", {
		fname: $("#DealFirstName").val(),
		lname: $("#DealLastName").val(),
		email: $("#register_email").val(),
		email2: $("#register_email2").val(),
		pass: $("#register_pass").val(),
		pass2: $("#register_pass2").val(),
		id: $("#deal_id").val(),
		address1: $("#DealAddress").val(),
		city: $("#DealCity").val(),
		zip: $("#DealZip").val(),
		state: $("#DealState").val(),
		country: $("#cart_country").val(),
		existing_card: $("input:radio[name=existing_card]:checked").val(),
		cctype: $("#DealCreditCardType").val(),
		ccnumber: $("#DealCreditCardNumber").val(),
		expm: $("#DealExpDateMonthMonth").val(),
		expy: $("#DealExpDateYearYear").val(),
		cvv: $("#DealCvv2Number").val(),
		terms: $("#terms").attr("checked"),
		otherstate: $("#DealOtherState").val(),
		qty: $("#qty").val()
	}, function(data) {
		//alert(data);
		processingSale = 0;
		if (data == "error1") {
			$("#sale_msg").html("Every field is required.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error2") {
			$("#sale_msg").html("This is not a valid name.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error3") {
			$("#sale_msg").html("Please enter your email address.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error4") {
			$("#sale_msg").html("This is not a valid email address.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error5") {
			$("#sale_msg").html("This email address is already in use.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error6") {
			$("#sale_msg").html("Please enter a password.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error7") {
			$("#sale_msg").html("Your password must contain between 6 and 12 characters and can only contain !~#$%^.-_=+ as special characters.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error8") {
			$("#sale_msg").html("Please retype your password.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error9") {
			$("#sale_msg").html("The passwords do not match.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error10") {
			$("#sale_msg").html("This is not a valid postal code.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error11") {
			$("#sale_msg").html("This is not a valid credit card number.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error12") {
			$("#sale_msg").html("This is not a valid CVV number.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error13") {
			$("#sale_msg").html("You must agree to the Terms and Conditions.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error14") {
			$("#sale_msg").html("There is a problem with your card. Please try another one.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error15") {
			$("#sale_msg").html("The deal is sold out.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error16") {
			$("#sale_msg").html("Province/State is missing.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error17") {
			$("#sale_msg").html("Email addresses must match.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error18") {
			$("#sale_msg").html("Deal has already ended.");
			$("#sale_msg").css("color", "red");
		} else if (data == "errorcard") {
			$("#sale_msg").html("There is a problem with your card. Please try another one.");
			$("#sale_msg").css("color", "red");
		} else if (data == "error") {
			$("#sale_msg").html("There was an error with your purchase.");
			$("#sale_msg").css("color", "red");
		} else if (data != "error") {
			processingSale = 1;
			$("#sale_msg").html("Please wait while your transaction is being processed...");
			$("#sale_msg").css("color", "green");
			window.location = "receipt.php?a=" + data;
		}
	})
}

function changeName() {
	var fname = $("#changefname").val();
	var lname = $("#changelname").val();
	$.post("ajax/changename.php", { fname: fname, lname: lname }, function(data) {
	data = eval("(" + data + ")");
		if (data.error == "0")	{
			$("#changename_msg").html("Your name has been changed.");
			$("#changename_msg").css("color", "green");
			$("#myaccount_usersfullname").html(data.name);
			setTimeout("switchShow('#editname')", 2000);
			setTimeout("$('#changename_msg').html('')", 2000);
		} else if (data.error == "error1") {
			$("#changename_msg").html("Please enter your name.");
			$("#changename_msg").css("color", "red");
		} else if (data.error == "error2") {
			$("#changename_msg").html("Your first name needs to have between 2 and 50 characters.");
			$("#changename_msg").css("color", "red");
		} else if (data.error == "error3") {
			$("#changename_msg").html("Your last name needs to have between 2 and 50 characters.");
			$("#changename_msg").css("color", "red");
		} else if (data.error == "error4") {
			$("#changename_msg").html("This is not a valid name.");
			$("#changename_msg").css("color", "red");
		}
	})
}

function changePass() {
	$.post("ajax/changepass.php", {
 		oldpass: $("#changepass_oldpass").val(),
		newpass1: $("#changepass_newpass1").val(),
		newpass2: $("#changepass_newpass2").val()
	}, function(data) {
		if (data == "1") {
			$("#changepass_msg").html("Your password has been changed.");
			$("#changepass_msg").css("color", "green");
			setTimeout("$('#changepass_oldpass').val('')", 2000);
			setTimeout("$('#changepass_newpass1').val('')", 2000);
			setTimeout("$('#changepass_newpass2').val('')", 2000);
			setTimeout("switchShow('#editpasss')", 2000);
			setTimeout("$('#changepass_msg').html('')", 2000);
		} else if (data == "error1") {
			$("#changepass_msg").html("This is not your current password.");
			$("#changepass_msg").css("color", "red");
		} else if (data == "error2") {
			$("#changepass_msg").html("Your password must have between 6 and 12 characters and can only contain !~#$%^.-_=+ as special characters..");
			$("#changepass_msg").css("color", "red");
		} else if (data == "error3") {
			$("#changepass_msg").html("Please retype your password correctly.");
			$("#changepass_msg").css("color", "red");
		}
	})
}

function removeCard() {
	var res = confirm("Are you sure you want to remove your credit card?");
	if (res) {
		$.post("ajax/removecard.php", function(data) {
			if (data == "1") {
				alert("Your credit card has been removed.");
				window.location = "myaccount.php";
			}
		})
	}
}
