// Account creation / editing functions

TOOLKITS.enableSubmitButton = function() {
	$("buttonSubmit").disabled = false;
}

TOOLKITS.disableSubmitButton = function() {
	$("buttonSubmit").disabled = true;
}

TOOLKITS.checkEmailValidity = function(email) {
	var pattern = "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*";
	pattern += "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*";
	pattern += "\.[a-z]+$";
	var regexpr = new RegExp(pattern, "i");
	if (regexpr.match(email)) {
		return true;
	} else {
		return false;	
	}
}

TOOLKITS.checkEmail = function(sourceFieldNumber) {
	// clear any old error messages
	var errorText1 = $("erruserName").innerHTML;
	var errorText2 = $("erruserNameConfirm").innerHTML;
	if (errorText1 != "") {
		$("erruserName").update();
	}
	if (errorText2 != "") {
		$("erruserNameConfirm").update();
	}
	
	// get current content of fields
	var email1 = $F("userName").strip();
	var email2 = $F("userNameConfirm").strip();
	
	// whether coming from field 1 or field 2, field 1 must not be empty
	if (email1 == "") {
		$("erruserName").update("You must enter an email address.");
		return;
	} else {
		var isValidEmail = TOOLKITS.checkEmailValidity(email1);
		if (!isValidEmail) {
			$("erruserName").update("Please enter a valid email address.");
			return;
		}
	}
	
	// if field 1 has content, field 2 cannot be empty
	if (sourceFieldNumber == 2) {
		if (email2 == "" && email1 != "") {
			$("erruserNameConfirm").update("To confirm, please re-enter your email.");
		}
	}
	
	// if both fields have content, the content must match
	if (email1 != "" && email2 != "" && email1 != email2) {
		$("erruserName").update("The email addresses you entered do not match.");
		$("erruserNameConfirm").update("The email addresses you entered do not match.");
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkPassword = function(sourceFieldNumber) {
	// clear any old error messages
	var errorText1 = $("errpassword").innerHTML;
	var errorText2 = $("errpasswordConfirm").innerHTML;
	if (errorText1 != "") {
		$("errpassword").update();
	}
	if (errorText2 != "") {
		$("errpasswordConfirm").update();
	}
	
	// get current content of fields
	var pw1 = $F("password").strip();
	var pw2 = $F("passwordConfirm").strip();
	
	// whether coming from field 1 or field 2, we expect field 1 to have a password filled in
	if (pw1 == "") {
		$("errpassword").update("You must create a password.");
		return;
	}
	
	// if not blank, password must meet criteria
	if (pw1 != "") {
		if (pw1.length < 8) {
			$("errpassword").update("Too short: Your password must be at least eight (8) characters long.");
			$("passwordConfirm").value = "";
			return;
		}
	}
	
	// if field 1 has content, field 2 cannot be empty
	if (sourceFieldNumber == 2) {
		if (pw2 == "" && pw1 != "") {
			$("errpasswordConfirm").update("To confirm, please re-enter your password.");
		}
	}
	
	// if both fields have content, the content must match
	if (pw1.length != "" && pw2.length != "" && pw1 != pw2) {
		$("errpassword").innerHTML = "The passwords you entered do not match.";
		$("errpasswordConfirm").innerHTML = "The passwords you entered do not match.";
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkFirstName = function() {
	// clear any old error messages
	var errorText = $("errfirstName").innerHTML;
	if (errorText != "") {
		$("errfirstName").update();
	}
	
	// get current content of field
	var content = $F("firstName").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errfirstName").update("Please enter your first name.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkLastName = function() {
	// clear any old error messages
	var errorText = $("errlastName").innerHTML;
	if (errorText != "") {
		$("errlastName").update();
	}
	
	// get current content of field
	var content = $F("lastName").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errlastName").update("Please enter your last name (surname or family name).");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkPhone = function() {
	// clear any old error messages
	var errorText = $("errphone").innerHTML;
	if (errorText != "") {
		$("errphone").update();
	}
	
	// get current content of field
	var content = $F("phone").strip();
	
	var hasPhone = (content == "") ? false : true;
	if (!hasPhone) {
		$("errphone").update("Please enter your office phone number.");
		return;
	} else if (content.length < 10) {
		$("errphone").update("That does not appear to be a valid phone number. Please make sure you include area code.");
		return;
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkCompany = function() {
	// clear any old error messages
	var errorId = "errcompany";
	var errorText = $(errorId).innerHTML;
	if (errorText != "") {
		$(errorId).update();
	}
	
	// get current content of field
	var selex = $("company").selectedIndex;
	selex = parseInt(selex);
	
	// field cannot be empty
	if (selex < 1) {
		$(errorId).update("Please select your company from the dropdown menu.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkJobTitle = function() {
	// clear any old error messages
	var errorText = $("errTitle").innerHTML;
	if (errorText != "") {
		$("errTitle").update();
	}
	
	// get current content of field
	var content = $F("Title").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errTitle").update("Please enter your job title/position.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkDepartment = function() {
	// clear any old error messages
	var errorText = $("errDepartment").innerHTML;
	if (errorText != "") {
		$("errDepartment").update();
	}
	
	// get current content of field
	var content = $F("Department").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errDepartment").update("Please enter the name of your department, school, or division.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkInstitution = function() {
	// clear any old error messages
	var errorText = $("errInstitution").innerHTML;
	if (errorText != "") {
		$("errInstitution").update();
	}
	
	// get current content of field
	var content = $F("Institution").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errInstitution").update("To use this site, you must be affiliated with an educational institution.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkSelexFall = function() {
	// clear any old error messages
	var errorText = $("errFallChoice").innerHTML;
	if (errorText != "") {
		$("errFallChoice").update();
	}
	
	// get current content of field
	var selex = $("FallChoice").selectedIndex;
	selex = parseInt(selex);
	
	// field cannot be empty
	if (selex < 1) {
		$("errFallChoice").update("Please select a month above.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkSelexSpring = function() {
	// clear any old error messages
	var errorText = $("errSpringChoice").innerHTML;
	if (errorText != "") {
		$("errSpringChoice").update();
	}
	
	// get current content of field
	var selex = $("SpringChoice").selectedIndex;
	selex = parseInt(selex);
	
	// field cannot be empty
	if (selex < 1) {
		$("errSpringChoice").update("Please select a month above.");
	}
	
	TOOLKITS.checkFormInfo();
	
}


TOOLKITS.checkStoreName = function() {
	// clear any old error messages
	var errorText = $("errStoreName").innerHTML;
	if (errorText != "") {
		$("errStoreName").update();
	}
	
	// get current content of field
	var content = $F("StoreName").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errStoreName").update("Please enter the name of the university or college bookstore that stocks your course texts.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.checkLoginEmail = function() {
	if ($("erruserName").innerHTML != "") {
		$("erruserName").update();
	}
	var userName = $F("userName");
	var hasUserName = (userName == "") ? false : true;
	var isValidEmail = TOOLKITS.checkEmailValidity(userName);
	if (!hasUserName) {
		$("erruserName").update("Please enter your email.");
	} else if (!isValidEmail) {
		$("erruserName").update("Please enter a valid email address.");
	}
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkLoginPw = function() {
	if ($("errpassword").innerHTML != "") {
		$("errpassword").update();
	}
	var password = $F("password");
	var hasPassword = (password == "") ? false : true;
	if (!hasPassword) {
		$("errpassword").update("Please enter your password.");
		return;
	} else if (password.length < 8) {
		$("errpassword").update("Too short: Your password must be at least eight (8) characters long.");
		return;
	}
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkCourseName = function() {
	// clear any old error messages
	var errorText = $("errCourseName").innerHTML;
	if (errorText != "") {
		$("errCourseName").update();
	}
	
	// get current content of field
	var content = $F("CourseName").strip();
	
	// field cannot be empty
	if (content == "") {
		if (pageName == "request_exam") {
			$("errCourseName").update("Please enter the name of the course for which you&rsquo;re considering this book.");
		} else {
			$("errCourseName").update("Please enter the name of the course in which you&rsquo;re using this book.");
		}
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkCourseEnrollment = function() {
	// clear any old error messages
	var errorText = $("errCourseEnrollment").innerHTML;
	if (errorText != "") {
		$("errCourseEnrollment").update();
	}
	
	// get current content of field
	var content = $F("CourseEnrollment").strip();
	
	// field cannot be empty
	if (content == "") {
		$("errCourseEnrollment").update("Please enter the course enrollment number.");
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkPO = function() {
	// clear any old error messages
	var errorText = $("errPO_Number").innerHTML;
	if (errorText != "") {
		$("errPO_Number").update();
	}
	
	// get current content of field
	var content = $F("PO_Number").strip();
	var testContent = content.toLowerCase();
	
	// field cannot be empty
	if (content == "") {
		$("errPO_Number").update("Please enter the bookstore purchase order number.");
	} else if (content.length < 3) {
		$("errPO_Number").update("That does not appear to be a valid PO number.");
	} else if (content == "none" || content == "unknown") {
		$("errPO_Number").update("That does not appear to be a valid PO number.");
	}
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkBlank = function(field, errorContainer) {
	// clear any old error messages
	var errorText = $(errorContainer).innerHTML;
	if (errorText != "") {
		$(errorContainer).update();
	}
	
	// get current content of field
	var content = $F(field).strip();
	
	// field cannot be empty
	if (content == "") {
		$(errorContainer).update("This field cannot be blank.");
	}
	
	TOOLKITS.checkFormInfo();
}


TOOLKITS.checkEmailAddress = function(field, errorContainer) {
	if ($(errorContainer).innerHTML != "") {
		$(errorContainer).update();
	}
	var userName = $F(field);
	var hasUserName = (userName == "") ? false : true;
	var isValidEmail = TOOLKITS.checkEmailValidity(userName);
	if (!hasUserName) {
		$(errorContainer).update("Please enter an email address.");
	} else if (!isValidEmail) {
		$(errorContainer).update("Please enter a valid email address.");
	}
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkState = function(selectId) {
	// clear any old error messages
	var errorId = "err" + selectId;
	var errorText = $(errorId).innerHTML;
	if (errorText != "") {
		$(errorId).update();
	}
	
	// get current content of field
	var selex = $(selectId).selectedIndex;
	selex = parseInt(selex);
	
	// field cannot be empty
	if (selex < 1) {
		$(errorId).update("Please select your state, province, or territory.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.setCountry = function(selectId) {
	// which form is this coming from -- shipping or billing info?
	switch (selectId.id) {
		case "ShipState":
			var field = "ShipCountry";
			var stateField = "ShipState";
			var changeBilling = false;
			break;
		case "BillState":
			var field = "BillCountry";
			var stateField = "BillState";
			var changeBilling = true;
			break;
	}
	// set country only if a state has been selected
	if ($(selectId.selectedIndex > 0)) {
		var state = $F(selectId);
		var country = getCountry(state);
		var radioButtonToCheck = field + "_" + country;
		$(radioButtonToCheck).checked = true;
	}
	// check that a state has been selected (to clear error messages if it has)
	TOOLKITS.checkState(stateField);
	// update billing info to match shipping
	if (changeBilling == true) {
		TOOLKITS.changeBillingInfo();
	}
}

TOOLKITS.changeBillingInfo = function() {
	if ($("useShippingInfo")) {
		if ($("useShippingInfo").checked == true) {
			$("BillName").value = $F("ShipName");
			$("BillAddress1").value = $F("ShipAddress1");
			$("BillAddress2").value = $F("ShipAddress2");
			$("BillAddress3").value = $F("ShipAddress3");
			$("BillCity").value = $F("ShipCity");
			$("BillState").value = $F("ShipState");
			$("BillZip").value = $F("ShipZip");
			if ($F("ShipCountry") == "USA") {
				$("BillCountry_USA").checked = true;
			} else if ($F("ShipCountry") == "CAN") {
				$("BillCountry_CAN").checked = true;
			}
			$("BillPhone").value = $F("ShipPhone");
			$("BillEmail").value = $F("ShipEmail");
		}
	}
}

TOOLKITS.toggleShippingInfo = function() {
	if ($("useShippingInfo")) {
		if ($("useShippingInfo").checked == true) {
			$("BillName").value = $F("ShipName");
			$("BillAddress1").value = $F("ShipAddress1");
			$("BillAddress2").value = $F("ShipAddress2");
			$("BillAddress3").value = $F("ShipAddress3");
			$("BillCity").value = $F("ShipCity");
			$("BillState").value = $F("ShipState");
			$("BillZip").value = $F("ShipZip");
			if ($F("ShipCountry") == "USA") {
				$("BillCountry_USA").checked = true;
			} else if ($F("ShipCountry") == "CAN") {
				$("BillCountry_CAN").checked = true;
			}
			$("BillPhone").value = $F("ShipPhone");
			$("BillEmail").value = $F("ShipEmail");
			$("BillName").disabled = true;
			$("BillAddress1").disabled = true;
			$("BillAddress2").disabled = true;
			$("BillAddress3").disabled = true;
			$("BillCity").disabled = true;
			$("BillState").disabled = true;
			$("BillZip").disabled = true;
			$("BillCountry_USA").disabled = true;
			$("BillCountry_CAN").disabled = true;
			$("BillPhone").disabled = true;
			$("BillEmail").disabled = true;
		} else {
			$("BillName").value = "";
			$("BillAddress1").value = "";
			$("BillAddress2").value = "";
			$("BillAddress3").value = "";
			$("BillCity").value = "";
			$("BillState").value = "";
			$("BillZip").value = "";
			//$("BillCountry_USA").checked = false;
			//$("BillCountry_CAN").checked = false;
			$("BillPhone").value = "";
			$("BillEmail").value = "";
			$("BillName").disabled = false;
			$("BillAddress1").disabled = false;
			$("BillAddress2").disabled = false;
			$("BillAddress3").disabled = false;
			$("BillCity").disabled = false;
			$("BillState").disabled = false;
			$("BillZip").disabled = false;
			$("BillCountry_USA").disabled = false;
			$("BillCountry_CAN").disabled = false;
			$("BillPhone").disabled = false;
			$("BillEmail").disabled = false;
		}
		// clear any error messages by checking all fields
		TOOLKITS.checkOrderInfo();
	}
}

TOOLKITS.checkFormInfo = function() {
	
	// loop through all error message containers and check for text
	var errorMessages = $$('div.error');
	var errors = 0;
	
	// increment error count for each message
	errorMessages.each(function(i){
		if ($(i).innerHTML != "") {
			errors++;
		}					   
	});
	
	// if no errors, turn button on; else turn off
	if (errors == 0) {
		TOOLKITS.enableSubmitButton();
	} else {
		TOOLKITS.disableSubmitButton();
	}
	
}

TOOLKITS.checkNewClientAccountInfo = function(checkId) {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkEmail(1);
	TOOLKITS.checkEmail(2);
	TOOLKITS.checkPassword(1);
	TOOLKITS.checkPassword(2);
	TOOLKITS.checkFirstName();
	TOOLKITS.checkLastName();
	TOOLKITS.checkPhone();
	if (checkId == true) {
		TOOLKITS.checkCompany();
	}
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkNewAccountInfo = function() {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkEmail(1);
	TOOLKITS.checkEmail(2);
	TOOLKITS.checkPassword(1);
	TOOLKITS.checkPassword(2);
	TOOLKITS.checkFirstName();
	TOOLKITS.checkLastName();
	TOOLKITS.checkJobTitle();
	TOOLKITS.checkDepartment();
	TOOLKITS.checkInstitution();
	TOOLKITS.checkSelexFall();
	TOOLKITS.checkSelexSpring();
	TOOLKITS.checkStoreName();
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkLoginInfo = function() {
	/*if ($F("userName") || $F("password")) {
		TOOLKITS.disableSubmitButton();
		TOOLKITS.checkLoginEmail();
		TOOLKITS.checkLoginPw();
		TOOLKITS.checkFormInfo();
	}*/
}

TOOLKITS.checkReminderInfo = function() {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkLoginEmail();
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkExamInfo = function() {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkCourseName();
	TOOLKITS.checkCourseEnrollment();
	TOOLKITS.checkStoreName();
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkDeskInfo = function() {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkCourseName();
	TOOLKITS.checkCourseEnrollment();
	TOOLKITS.checkPO();
	TOOLKITS.checkStoreName();
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkOrderInfo = function() {
	TOOLKITS.disableSubmitButton();
	TOOLKITS.checkCardType();
	TOOLKITS.checkCardNo();
	TOOLKITS.checkBlankExpiration();
	var noBlanks = [];
	noBlanks.push("BillName");
	noBlanks.push("BillAddress1");
	noBlanks.push("BillCity");
	noBlanks.push("BillZip");
	noBlanks.push("BillPhone");
	noBlanks.each(function(i){
		var field = i;
		var errorContainer = "err" + i;
		TOOLKITS.checkBlank(field, errorContainer);
	});
	TOOLKITS.setCountry($("BillState"));
	TOOLKITS.checkEmailAddress("BillEmail", "errBillEmail");
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkShipInfo = function() {
	TOOLKITS.disableSubmitButton();
	var noBlanks = [];
	noBlanks.push("ShipName");
	noBlanks.push("ShipAddress1");
	noBlanks.push("ShipCity");
	noBlanks.push("ShipZip");
	noBlanks.push("ShipPhone");
	noBlanks.each(function(i){
		var field = i;
		var errorContainer = "err" + i;
		TOOLKITS.checkBlank(field, errorContainer);
	});
	TOOLKITS.setCountry($("ShipState"));
	TOOLKITS.checkEmailAddress("ShipEmail", "errShipEmail");
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkCardType = function() {
	// clear any old error messages
	var errorText = $("errCCType").innerHTML;
	if (errorText != "") {
		$("errCCType").update();
	}
	
	// get current content of field
	var selex = $("CCType").selectedIndex;
	selex = parseInt(selex);
	
	// field cannot be empty
	if (selex < 1) {
		$("errCCType").update("This field cannot be left blank.");
	}
	
	TOOLKITS.checkFormInfo();
	
}

TOOLKITS.cleanCardNo = function() {
	var ccNo = $F("CCNo");
	var pattern1 = " ";
	var regexp1 = new RegExp(pattern1, "gi");
	ccNo = ccNo.replace(regexp1, "");
	var pattern2 = "-";
	var regexp2 = new RegExp(pattern2, "gi");
	ccNo = ccNo.replace(regexp2, "");
	$("CCNo").value = ccNo;
	//TOOLKITS.checkFormInfo();
}

TOOLKITS.checkCardNo = function() {
	// clear any old error messages
	var errorText = $("errCCNo").innerHTML;
	if (errorText != "") {
		$("errCCNo").update();
	}
	
	TOOLKITS.cleanCardNo();
	
	// get current content of field
	var content = $F("CCNo").strip();
	var len = content.length;
	
	// default error message
	var msg = "";
	
	// field cannot be empty
	if (len == 0) {
		msg = "This field cannot be blank.";
	} else if (len < 10) {
		msg = "That does not appear to be a valid credit card number.";
	}
	$("errCCNo").update(msg);
	
	TOOLKITS.checkFormInfo();
}

TOOLKITS.checkBlankExpiration = function() {
	dateSet = true;
	TOOLKITS.checkExpiration();
	
}

TOOLKITS.checkExpiration = function() {
	var monthVal = $F("CCExpMonth");
	var yearVal = $F("CCExpYear");
	var pattern = new RegExp("^0", "i");
	monthVal = monthVal.replace(pattern, "");
	yearVal = yearVal.replace(pattern, "");
	monthVal = parseInt(monthVal);
	yearVal = parseInt(yearVal);
	if (monthVal > 0 && yearVal > 0) {
		if (yearVal == curYear && monthVal < curMonth) {
			var monthIsOK = false;
		} else if (yearVal == curYear && monthVal >= curMonth) {
			var monthIsOK = true;
		} else if (yearVal > curYear || yearVal < curYear) {
			var monthIsOK = true;
		}
		if (yearVal >= curYear) {
			var yearIsOK = true;
		} else {
			var yearIsOK = false;
		}
		if (yearIsOK && monthIsOK) {
			$("errCCExp").update();
		} else {
			$("errCCExp").update("That credit card has expired. Please use another.");
		}
	} else {
		/* if user has previously posted the form, check that they have not unset
		 * the expiration date since (don't check this the first time they fill out
		 * the form because doing so would produce the error message as soon as they select
		 * the first dropdown (whether month or year) -- before they'd even had a chance to 
		 * make a valid selection on the the second dropdown
		 */
		if (dateSet == true) {
			$("errCCExp").update("Please select a valid expiration date.");
		}
	}
	TOOLKITS.checkFormInfo();
}
