﻿String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g,"");};

function refresh()
{
	var xhr = createXhr();
	xhr.onreadystatechange = processCheckLoginStatusResponse;
 	var fname = document.getElementById("fullname").innerText;
	xhr.open("GET", "accountGetItems.aspx?action=checkLoginStatus&fieldvalue=" + fname);
	xhr.send(null);

	function processCheckLoginStatusResponse()
	{
		if (xhr.readyState == 4)
		{
			var answer = xhr.responseText;
			xhr.close;
			if (answer === "reload")
			{
				window.location.reload();
			}
		}
	}
}

function setupRefresh()
{
	document.getElementById('mainBody').setAttribute('onunload', "setTimeout('refresh()', 500);");
}

function isDigitKeyPress(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return reg.test(keychar);
}

function isZipKeyPress(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d|-/;
	return reg.test(keychar);
}

function setPageFocus(objName)
{
	var obj = document.getElementById(objName);
	if (obj.getAttribute("disabled") == null)
		setTimeout(obj.focus(), 1);
}

function createXhr()
{
	var xhr;
	try
	{
		xhr = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e1)
	{
		try
		{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e2)
		{
			xhr = false;
		}
	}
	if (!xhr && typeof XMLHttpRequest != "undefined")
	{
		xhr = new XMLHttpRequest()
	}
	return xhr;
}

function login(msg)
{
	var winW = 630, winH = 460;

	if (parseInt(navigator.appVersion) > 3)
	{
		if (navigator.appName=="Netscape")
		{
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft") != -1)
		{
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			if (winH === 0)
				winH = 460;
		}
	}
	
	loginPrompt(msg, winW / 3, winH / 3, false);
}

function logout()
{
	window.location = "accountLogout.aspx?page=" + document.location;
}

function cancelLogin()
{
	document.getElementById("popupDiv").parentNode.removeChild(document.getElementById("popupDiv"));
	if (goBack) history.back();
	var fullUrl = window.location.href;
	var urlParams = window.location.search;
	var noParamUrl = fullUrl.replace(urlParams, "");
	window.location.replace(noParamUrl);
}

function doAction()
{
	if (window.location.toString().indexOf("login=failed") > -1)
	{
		login("Incorrect login and/or password");
	}
}

function createAccount(evt)
{
	if (validateAccountInfo(evt, true))
		document.getElementById("mainForm").submit();
}

function saveAccountEdits(evt)
{
	
	var clickX;
	var clickY;
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		clickX = window.event.clientX + document.documentElement.scrollLeft;
		clickY = window.event.clientY + document.documentElement.scrollTop;
	}
	else
	{
		clickX = evt.pageX;
		clickY = evt.pageY;
	}

	if (validateAccountInfo(evt, true))
	{
		document.getElementById("mainForm").submit();
	}
}

function validateAccountInfo(evt, validate_Email)
{
	var clickX;
	var clickY;
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		clickX = window.event.clientX + document.documentElement.scrollLeft;
		clickY = window.event.clientY + document.documentElement.scrollTop;
	}
	else
	{
		clickX = evt.pageX;
		clickY = evt.pageY;
	}

	var errorStr = "";
	errorStr += validateFirstName(document.getElementById("txtFirstName").value);
	errorStr += validateLastName(document.getElementById("txtLastName").value);
	errorStr += validateOrgType(document.getElementById("lstOrgType").value);
	if (window.location.toString().indexOf("accountLogin.aspx") > -1 || window.location.toString().indexOf("accountCreate.aspx") > -1)
	    errorStr += validateLoginName(document.getElementById("txtLoginName").value);
	if (document.getElementById("txtPassword") !== null)
		errorStr += validatePassword(document.getElementById("txtPassword").value);
	if (validate_Email)
		errorStr += validateEmail(document.getElementById("txtEmail").value);
	errorStr += validatePhoneNumber(document.getElementById("txtPhone").value);
	errorStr += validateFax(document.getElementById("txtFax").value);
	errorStr += validateBillAddress1(document.getElementById("txtBillAddress1").value);
	errorStr += validateBillCity(document.getElementById("txtBillCity").value);
	errorStr += validateBillState(document.getElementById("lstBillState").value);
	errorStr += validateBillZip(document.getElementById("txtBillZip").value);
	//errorStr += validateBillCountry(document.getElementById("txtBillCountry").value);
	errorStr += validateShipAddress1(document.getElementById("txtShipAddress1").value);
	errorStr += validateShipCity(document.getElementById("txtShipCity").value);
	errorStr += validateShipState(document.getElementById("lstShipState").value);
	errorStr += validateShipZip(document.getElementById("txtShipZip").value);
	//errorStr += validateShipCountry(document.getElementById("txtShipCountry").value);

	if (errorStr.length > 0)
	{
		msgBox(errorStr, clickX, clickY);
		return false;
	}
	return true;
}

function validateFirstName(value)
{
	if (value.trim() === "")
		return "\"First Name\" is required<br/>";
	else
		return "";
}

function validateLastName(value)
{
	if (value.trim() === "")
		return "\"Last Name\" is required<br/>";
	else
		return "";
}

function validateOrgType(value)
{
	if (value.trim() === "")
	{
		return "\"Type\" is required<br/>";
	}
	else
	{
		if (value.toLowerCase() !== "individual" && value.toLowerCase() !== "other...")
		{
			var retval = "";
			retval += validateOrganization(document.getElementById("txtOrg").value);
//			retval += validateLowestGrade(document.getElementById("lstLowestGrade").value);
//			retval += validateHighestGrade(document.getElementById("lstHighestGrade").value);
			return retval;
		}
		else
			return "";
	}
}

function validateOrganization(value)
{
	if (value.trim() === "")
		return "\"Organization/School\" is required<br/>";
	else
		return "";
}

function validateEnrollment(value)
{
	if (value == 0)
		return "\"Enrollment\" cannot be zero (0)<br/>";
	else
		return "";
}

function validateLowestGrade(value)
{
	if (value.trim() === "")
		return "\"Lowest Grade\" is required<br/>";
	else
	{
		var lstLowestGrade = document.getElementById("lstLowestGrade");
		var grades = "";
		for (var i = 0; i < lstLowestGrade.options.length; i++)
			grades += "|" + lstLowestGrade.options[i].text + "|";
		if (grades.indexOf(value) == -1)
			return "\"Lowest Grade\" is invalid<br/>";
		else
			return "";
	}
}

function validateHighestGrade(value)
{
	if (value.trim() === "")
		return "\"Highest Grade\" is required<br/>";
	else
	{
		var lstHighestGrade = document.getElementById("lstHighestGrade");
		var grades = "";
		for (var i = 0; i < lstHighestGrade.options.length; i++)
			grades += "|" + lstHighestGrade.options[i].text + "|";
		if (grades.indexOf(value) == -1)
			return "\"Highest Grade\" is invalid<br/>";
		else
			return "";
	}
}

function validateLastName(value)
{
	if (value.trim() === "")
		return "\"Last Name\" is required<br/>";
	else
		return "";
}

function validateLoginName(value)
{
	if (value.trim() === "")
		return "\"Login Name is required<br/>";
	else
	{
		var xhr = createXhr();
		xhr.open("GET", "accountGetItems.aspx?action=checkLoginDupes&fieldvalue=" + value, true); //asynchronous ajax
		xhr.send(null);
		if (xhr.responseText.length > 0)
		{
			xhr.close;
			return "\"Login Name\" already exists<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please select a different \"Login Name\"<br/>";
		}
		xhr.close;
		return "";
	}
}

function validatePassword(value)
{
	if (value.trim() === "")
	{
		return "\"Password\" is required<br/>";
	}
	else
	{
		if (value !== document.getElementById("txtConfirmPassword").value)
		{
			return "Password and its confirmation do not match<br/>";
		}
		return "";
	}
}

function validateEmail(value)
{
	if (value.trim() !== "")
	{
		var regx = /^([\w]+)(([-\.][\w]+)?)*@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!regx.test(value))
			return "Invalid Email address<br/>";
		else
		{
			var xhr = createXhr();
			xhr.open("GET", "accountGetItems.aspx?action=checkEmailDupes&fieldvalue=" + value, true); //asynchronous ajax
			xhr.send(null);
			if (xhr.responseText.length > 0)
			{
				xhr.close;
				return "\"Email Address\" already exists<br/>&nbsp;&nbsp;&nbsp;&nbsp;Please select a different \"Email Address\"<br/>";
			}
			xhr.close;
			return "";
		}
	}
	else
		return "\"Email address\" is required<br/>";
}

function validatePhoneNumber(value)
{
	if (value.trim() === "")
	{
		return "\"Phone Number\" is required<br/>";
	}
	else
	{
		var regx = /^((\+\d{1,3}(-| |\.)?\(?\d\)?(-| |\.)?\d{1,5})|(\(?\d{2,6}\)?))(-| |\.)?(\d{3,4})(-| |\.)?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
		if (!regx.test(value))
			return "Invalid Phone Number<br/>";
		else
			return "";
	}
}

function validateFax(value)
{
	if (value.trim() !== "")
	{
		var regx = /^((\+\d{1,3}(-| |\.)?\(?\d\)?(-| |\.)?\d{1,5})|(\(?\d{2,6}\)?))(-| |\.)?(\d{3,4})(-| |\.)?(\d{4})$/;
		if (!regx.test(value))
			return "Invalid Fax Number<br/>";
		else
			return "";
	}
	else
		return "";
}

function validateBillAddress1(value)
{
	var errStr = validateAddress1(value);
	if (errStr.trim() === "empty")
		return "\"Billing Address 1\" is required<br/>";
	else
		return "";
}

function validateBillCity(value)
{
	var errStr = validateCity(value);
	if (errStr.trim() === "empty")
		return "\"Billing City\" is required<br/>";
	else
		return "";
}

function validateBillState(value)
{
	var errStr = validateState(value);
	if (errStr.trim() === "empty")
		return "\"Billing State\" is required<br/>";
	else
		return "";
}

function validateBillZip(value)
{
	var errStr = validateZip(value);
	if (errStr.trim() === "empty")
	{
		return "\"Billing Zip\" is required<br/>";
	}
	else
	{
		if (errStr.trim() === "invalid")
			return "\"Billing Zip\" is invalid<br/>";
		else
			return "";
	}
}

function validateBillCountry(value)
{
	var errStr = validateCountry(value);
	if (errStr.trim() === "empty")
	{
		return "\"Billing Country\" is required<br/>";
	}
	else
	{
		if (errStr.trim() === "invalid")
			return "\"Billing Country\" is invalid<br/>";
		else
			return "";
	}
}

function validateShipAddress1(value)
{
	var errStr = validateAddress1(value);
	if (errStr.trim() === "empty")
	{
		var shipSameAsBill = document.getElementById("chkSameAsBillingInfo").checked;
		var billAddress1 = document.getElementById("txtBillAddress1").value;
		if (shipSameAsBill)
		{
			if (validateAddress1(billAddress1).trim() === "empty")
				return "\"Shipping Address 1\" is required<br/>";
			else
				return "";
		}
		else
			return "\"Shipping Address 1\" is required<br/>";
	}
	else
		return "";
}

function validateShipCity(value)
{
	var errStr = validateCity(value);
	if (errStr.trim() === "empty")
	{
		var shipSameAsBill = document.getElementById("chkSameAsBillingInfo").checked;
		var billCity = document.getElementById("txtBillCity").value;
		if (shipSameAsBill)
		{
			if (validateCity(billCity).trim() === "empty")
				return "\"Shipping City\" is required<br/>";
			else
				return "";
		}
		else
			return "\"Shipping City\" is required<br/>";
	}
	else
		return "";
}

function validateShipState(value)
{
	var errStr = validateState(document.getElementById("lstBillState").value);
	if (errStr.trim() === "empty")
	{
		var shipSameAsBill = document.getElementById("chkSameAsBillingInfo").checked;
		var billState = document.getElementById("lstBillState").value;
		if (shipSameAsBill)
		{
			if (validateBillState(billState).trim() === "empty")
				return "\"Shipping State\" is required<br/>";
			else
				return "";
		}
		else
			return "\"Shipping State\" is required<br/>";
	}
	else
	{
		if (errStr.trim() === "invalid")
			return "\"Shipping State\" is invalid<br/>";
		else
			return "";
	}
}

function validateShipZip(value)
{
	var errStr = validateZip(value);
	if (errStr.trim() === "empty")
	{
		var shipSameAsBill = document.getElementById("chkSameAsBillingInfo").checked;
		var billZip = document.getElementById("txtBillZip").value;
		if (shipSameAsBill)
		{
			if (validateBillZip(billZip).trim() === "empty")
				return "\"Shipping Zip\" is required<br/>";
			else
				return "";
		}
		else
			return "\"Shipping Zip\" is required<br/>";
	}
	else
	{
		if (errStr.trim() === "invalid")
			return "\"Shipping Zip\" is invalid<br/>";
		else
			return "";
	}
}

function validateShipCountry(value)
{
	var errStr = validateCountry(value);
	if (errStr.trim() === "empty")
	{
		var shipSameAsBill = document.getElementById("chkSameAsBillingInfo").checked;
		var billCountry = document.getElementById("txtBillCountry").value;
		if (shipSameAsBill)
		{
			if (validateBillCountry(billCountry).trim() === "empty")
				return "\"Shipping Country\" is required<br/>";
			else
				return "";
		}
		else
			return "\"Shipping Country\" is required<br/>";
	}
	else
	{
		if (errStr.trim() === "invalid")
			return "\"Shipping Country\" is invalid<br/>";
		else
			return "";
	}
}

function validateAddress1(value)
{
	if (value.trim() === "")
		return "empty";
	else
		return "";
}

function validateCity(value)
{
	if (value.trim() === "")
		return "empty";
	else
		return "";
}

function validateState(value)
{
	if (value.trim() === "")
		return "empty";
		
	var states = "";
	var lstStates = document.getElementById("lstBillState");
	
	for (var i = 0; i < lstStates.options.length; i++)
		states += "|" + lstStates.options[i].text + "|";
	
	if (states.indexOf("|" + value + "|") == -1)
		return "invalid";
	else
		return "";
}

function validateZip(value)
{
	if (value.trim() === "")
		return "empty";
	else
	{
		var regx = /^(\d{5})(-\d{4})?$/;
		if (!regx.test(value))
			return "invalid";
		else
			return "";
	}
}

function validateCountry(value)
{
	if (value.trim() === "empty")
	{
		return "empty";
	}
	else
	{
		value = value.toLowerCase();
		if (value !== "usa" && value !== "us" && value !== "u.s.a." && value !== "u.s." && value !== "u.s.a" && value !== "canada" && value !== "united states" && value !== "united states of america")
			return "invalid";
		else
			return "";
	}
}

function msgBox(msg, x, y)
{
	var popupDiv;
	if (document.getElementById("popupDiv") !== null)
		document.getElementById("popupDiv").parentNode.removeChild(document.getElementById("popupDiv"));
	popupDiv = document.createElement("div");
	popupDiv.setAttribute("id", "popupDiv");
	document.getElementById("mainForm").appendChild(popupDiv);
	popupDiv.style.position = "absolute";
	var msgNoLineBreaks = msg;
	while (msgNoLineBreaks.indexOf("<br/>") > -1)
		msgNoLineBreaks = msgNoLineBreaks.replace("<br/>", "");
	popupDiv.style.height = (((((msg.length - msgNoLineBreaks.length) / 5) + 1) * 18) + 40) + "px";
	popupDiv.style.width = "251px";
	popupDiv.style.backgroundColor = "White";
	popupDiv.style.border = "solid 5px #5588DD";
	popupDiv.style.textAlign = "center";
	popupDiv.style.verticalAlign = "middle";
	popupDiv.style.fontSize = "9pt";
	popupDiv.setAttribute("onmousemove", "this.style.display='';");
	popupDiv.setAttribute("onmouseout", "this.style.display='none';");
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		popupDiv.attachEvent("onmousemove", function(){document.getElementById("popupDiv").style.display = "";});
		popupDiv.attachEvent("onmouseout", function(){document.getElementById("popupDiv").style.display = "none";});
	}
	var spanText = document.createElement("span");
	popupDiv.appendChild(spanText);
	spanText.style.position = "relative";
	spanText.style.top = "3px";
	spanText.style.cursor = "default";
	spanText.innerHTML = "<p style=\"position:relative; text-align:left; left:10px; top:5px;\">" + msg + "</p>";
	popupDiv.style.display = "";
	var divButtons = document.createElement("div");
	popupDiv.appendChild(divButtons);
	divButtons.style.position = "relative";
	divButtons.style.TextAlign = "right";
	divButtons.style.height = "20px";
	var btnOK = document.createElement("input");
	btnOK.id = "btnOK";
	btnOK.type = "button";
	btnOK.value = "OK";
	btnOK.style.width = "60px";
	divButtons.appendChild(btnOK);
	if (navigator.appName == "Microsoft Internet Explorer")
		btnOK.attachEvent("onclick", function(){popupDiv.style.display = 'none';});
	else
		btnOK.onclick = function(){popupDiv.style.display='none';};
	
	y -= 5;
	x -= 5;
	var divWidth = +popupDiv.style.width.replace("px", "");
	var divHeight = +popupDiv.style.height.replace("px", "");
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		x = ((x + divWidth) > document.body.offsetWidth ? document.body.offsetWidth - divWidth : x) - 30;
		y = ((y + divHeight) > document.body.offsetHeight ? document.body.offsetHeight - divHeight : y) - 30;
	}	
	else
	{
		x = ((x + divWidth + 20) > window.innerWidth ? window.innerWidth - divWidth - 20 : x) - 30;
		y = ((y + divHeight + 20) > window.innerHeight ? window.innerHeight - divHeight - 20 : y) - 30;
	}
	popupDiv.style.top = y + "px";
	popupDiv.style.left = x + "px";
}
