﻿// Javascript for CreateAccount.aspx
function changeRequired(sCountryID)
{
	if(sCountryID == "1")
	{
		document.getElementById('AreaState').style.display = 'inline';
		document.getElementById('reqZip').style.display = 'inline';
	}
	else
	{
		document.getElementById('AreaState').style.display = 'none';
		document.getElementById('reqZip').style.display = 'none';
	}
}
function rwCancel()
{
	document.getElementById('refworksBib').style.display = 'none';
	document.getElementById('txtRWPass').value = '';
	document.getElementById('txtRWUser').value = '';
}
function fillGroupCode()
{
	var sEmail = document.getElementById("txtEmail").value;
	if(sEmail.indexOf("@") > 0 || sEmail.substring(sEmail.indexOf("@")).indexOf(".") > 0)
	{
		var sDomain = sEmail.substring(sEmail.indexOf("@")+1);
		ret = CreateAccount.getGroupCode(sDomain, OnCompleteGroupCode, OnTimeOut, OnError);
	}
}
function toggleDisplay(element)
{
	var adv = document.getElementById(element);
	if(adv.style.display!='none')
	{
		adv.style.display='none';
	}
	else
	{
		adv.style.display='block';
	}
}
function validate()
{
	if(document.getElementById("txtFirstName").value == "")
	{
		alert("You must enter your First Name.");
		return false;
	}
	if(document.getElementById("txtFirstName").value.length > 100)
	{
		alert("The maximum allowed length for a first name is 100 characters.");
		return false;
	}
	if(document.getElementById("txtLastName").value == "")
	{
		alert("You must enter your Last Name.");
		return false;
	}
	if(document.getElementById("txtLastName").value.length > 100)
	{
		alert("The maximum allowed length for a last name is 100 characters.");
		return false;
	}
	if(document.getElementById("txtInstitution").value == "")
	{
		alert("You must enter your Institution Name.");
		return false;
	}
	if(document.getElementById("txtInstitution").value.length > 100)
	{
		alert("The maximum allowed length for a institution name is 100 characters.");
		return false;
	}
	var sEmail = document.getElementById("txtEmail").value;
	if(sEmail.indexOf("@") < 0 || sEmail.substring(sEmail.indexOf("@")).indexOf(".") < 0)
	{
		alert("You must enter a valid Email Address.");
		return false;
	}
	if(document.getElementById("txtEmail").value.length > 100)
	{
		alert("The maximum allowed length for an email address is 100 characters.");
		return false;
	}
	if(document.getElementById("txtZip").value == "")
	{
		alert("You must enter your Zip Code");
		return false;
	}
	if(document.getElementById("txtZip").value.length > 15)
	{
		alert("The maximum allowed length for a zip code is 15 characters.");
		return false;
	}
	if(document.getElementById("txtPassword").value.length > 100)
	{
		alert("The maximum allowed length for a password is 100 characters.");
		return false;
	}
	if(document.getElementById("txtPassword").value != "")
	{
		if(document.getElementById("txtPassword").value != document.getElementById("txtConfirm").value)
		{
			alert("Password and Confirmation did not match.");
			return false;
		}
	}
	else
	{
		alert("You must enter a Password");
		return false;
	}
	if(document.getElementById("txtPhone").value.length > 20)
	{
		alert("The maximum allowed length for a phone number is 20 characters.");
		return false;
	}
	if(document.getElementById("txtAddress").value.length > 100)
	{
		alert("The maximum allowed length for a address is 100 characters.");
		return false;
	}
	if(document.getElementById("txtCity").value.length > 100)
	{
		alert("The maximum allowed length for a city is 100 characters.");
		return false;
	}
	if(document.getElementById("txtRWUser").value.length > 50)
	{
		alert("The maximum allowed length for a RefWorks User Name is 50 characters.");
		return false;
	}
	if(document.getElementById("txtRWPass").value.length > 50)
	{
		alert("The maximum allowed length for a RefWorks Password is 50 characters.");
		return false;
	}
	if(document.getElementById("txtRWGroup").value.length > 50)
	{
		alert("The maximum allowed length for a RefWorks Group Code is 50 characters.");
		return false;
	}
	return true;
}
function createAccount()
{
	var aUserInfo = new Array();
	if(document.getElementById("txtFirstName").value != "")
		aUserInfo.push("FirstName|"+document.getElementById("txtFirstName").value);
	else
	{
		alert("You must enter your First Name.");
		return;
	}
	if(document.getElementById("txtLastName").value != "")
		aUserInfo.push("LastName|"+document.getElementById("txtLastName").value);
	else
	{
		alert("You must enter your Last Name.");
		return;
	}
	if(document.getElementById("txtInstitution").value != "")
		aUserInfo.push("InstitutionName|"+document.getElementById("txtInstitution").value);
	else
	{
		alert("You must enter your Institution Name.");
		return;
	}
	var position = document.getElementById("ctl00_ContentPlaceHolder1_ddlPosition");
	aUserInfo.push("Position|"+position.options[position.selectedIndex].value);
	var interest = document.getElementById("ctl00_ContentPlaceHolder1_ddlInterest");
	aUserInfo.push("Interest|"+interest.options[interest.selectedIndex].value);
	var sEmail = document.getElementById("txtEmail").value;
	if(sEmail.indexOf("@") >= 0 && sEmail.substring(sEmail.indexOf("@")).indexOf(".") > 0)
		aUserInfo.push("Email|"+document.getElementById("txtEmail").value);
	else
	{
		alert("You must enter a valid Email Address.");
		return;
	}
	if(document.getElementById("txtPhone").value != "")
		aUserInfo.push("Phone|"+document.getElementById("txtPhone").value);
	if(document.getElementById("txtAddress").value != "")
		aUserInfo.push("Address|"+document.getElementById("txtAddress").value);
	if(document.getElementById("txtCity").value != "")
		aUserInfo.push("City|"+document.getElementById("txtCity").value);
	var country = document.getElementById("ctl00_ContentPlaceHolder1_ddlCountry");
	aUserInfo.push("Country|"+country.options[country.selectedIndex].value);
	if(country.options[country.selectedIndex].value == "1")
	{
		var state = document.getElementById("ctl00_ContentPlaceHolder1_ddlState");
		aUserInfo.push("State|"+state.options[state.selectedIndex].value);
	}
	if(document.getElementById("txtZip").value != "")
		aUserInfo.push("Zip|"+document.getElementById("txtZip").value);
	else
	{
		if(country.options[country.selectedIndex].value == "1")
		{
			alert("You must enter your Zip Code");
			return;
		}
	}
	if(document.getElementById("txtPassword").value != "")
	{
		if(document.getElementById("txtPassword").value == document.getElementById("txtConfirm").value)
			aUserInfo.push("Password|"+document.getElementById("txtPassword").value);
		else
		{
			alert("Password and Confirmation did not match.");
			return;
		}
	}
	else
	{
		alert("You must enter a Password");
		return;
	}
	var freq = document.getElementById("ctl00_ContentPlaceHolder1_ddlFrequency");
	aUserInfo.push("EmailAlert|"+freq.options[freq.selectedIndex].value);
	var bib = document.getElementById("ctl00_ContentPlaceHolder1_ddlBibSoftware");
	aUserInfo.push("BibSoftware|"+bib.options[bib.selectedIndex].value);
	var bHasRW = true;
	if(document.getElementById("txtRWUser").value != "")
		aUserInfo.push("RWUser|"+document.getElementById("txtRWUser").value);
	else
		bHasRW = false;
	if(document.getElementById("txtRWPass").value != "")
		aUserInfo.push("RWPass|"+document.getElementById("txtRWPass").value);
	else
		bHasRW = false;
	if(document.getElementById("txtRWGroup").value != "")
		aUserInfo.push("RWGroup|"+document.getElementById("txtRWGroup").value);
	else
		bHasRW = false;
	
	aUserInfo.push("FullWidthLayout|true");
	var sDomain = document.getElementById("txtEmail").value;
	sDomain = sDomain.substring(sDomain.indexOf("@")+1);
	ret = CreateAccount.createUser(aUserInfo, sDomain, bHasRW, OnComplete, OnTimeOut, OnError);
}
function submitEnter(evt)
{
	var code;
	if (evt.keyCode) code = evt.keyCode;
	else if (evt.which) code = evt.which;
	if(code == 13)
	{
		if(confirm("Create Account?")) isCustomer();
	}
}
function OnComplete(arg) {
	document.getElementById("createInfo").className = "";
	if(arg >= 0)
	{
		alert("An activation email has been sent to your email address.\nPlease follow the instructions in the email to complete your account creation.");
		window.location = "rahome.aspx";
	}
	else if(arg == -1)
	{
		alert("That email address already exists.");
	}
	else if(arg == -5)
	{
		alert("Invalid RefWorks Password.");
	}
	else if(arg == -6)
	{
		alert("Invalid RefWorks Group Code.");
	}
	else if(arg == -7)
	{
		alert("Invalid RefWorks User Name.");
	}
	else if(arg == -8)
	{
		if(confirm("An account which has been deleted or never verified already exists with that email address.\nWould you like to restore this account?"))
		{
			CreateAccount.reactivateUser(document.getElementById("txtEmail").value, OnComplete, OnTimeOut, OnError);
		}
		else
		{
			if(confirm("Would you like to permanently delete the existing account and reuse the email address?"))
				CreateAccount.deleteUser(document.getElementById("txtEmail").value, OnCompleteDelete, OnTimeOut, OnError);
		}
	}
	else if(arg == -9)
	{
		alert("An activation email has been sent to your email address.\nPlease follow the instructions in the email to complete your account reactivation.");
		window.location = "rahome.aspx";
	}
	else
	{
		alert("There was a problem processing this request. Please try again later.");
	}
}
function OnCompleteDelete(arg)
{
	if(arg)
		createAccount();
	else
		alert("There was a problem processing this request. Please try again later.");
}
function OnCompleteGroupCode(arg)
{
	if(arg != "")
		document.getElementById("txtRWGroup").value = arg;
}
function OnTimeOut(arg) {
	alert("TimeOut encountered.");
}
function OnError(arg) {
	alert("Error encountered.");
}