
var acct_info;
var is_standalone_login = false;

function populateAccountInfo()
{
	if (acct_info != null) {
		try {
			document.getElementById("tbx_email").value = acct_info.Email;
			document.getElementById("tbx_fname").value = acct_info.NameFirst;
			document.getElementById("tbx_lname").value = acct_info.NameLast.replace("&#039;", "'");
			document.getElementById("tbx_street1").value = acct_info.Street1;
			document.getElementById("tbx_street2").value = acct_info.Street2;
			document.getElementById("tbx_city").value = acct_info.City;
			document.getElementById("tbx_zip").value = acct_info.PostalCode;
			document.getElementById("tbx_phone").value = acct_info.PhoneDay;
			try {
				document.getElementById("tbx_evephone").value = acct_info.PhoneEve;
			}
			catch (e) { }
			document.getElementById("chk_email_ind").checked = acct_info.CheckboxEmail; // this and next 2 added 2007-11-26 PAG
			document.getElementById("chk_mail_ind").checked = acct_info.CheckboxMail;
			document.getElementById("chk_phone_ind").checked = acct_info.CheckboxPhone;
			var menu = document.getElementById("ddl_state");
			if (menu)
				set_selected_value(menu, acct_info.State);
			menu = document.getElementById("ddl_polite");
			if (menu)
				set_selected_value(menu, acct_info.Prefix);
			$('#td_acctinfo1').html(build_customerInfo1());
			$('#td_acctinfo2').html(build_customerInfo2());
		}
		catch (e) { }
	}
	else
		alert("acct_info doesn't exist!");
}

function cancelLogin()
{
	in_submit_order = false;
	toggle_login("hide");
}

function handle_login()
{
	var handle_login_req = getreq();

	this.handler = function () {
		if (handle_login_req.readyState == 4) {
			$('#btn_login_submit').show();
			$('#img_login_wait').hide();
			var rslt = { d: { Success: true} };
			try {
				rslt = JSON.parse(handle_login_req.responseText);
			}
			catch (err) { }
			try {
				rslt = rslt.d;
			}
			catch (e) { }
			if (rslt && rslt.ExceptionType) {
				alert(rslt.Message);
			}
			else if (rslt && rslt.Success) {
				// eval(s.substring(3));
				if (rslt.AccountInfo)
					acct_info = rslt.AccountInfo;
				if (!is_standalone_login) {
					try {
						if (typeof login_success_handler != "undefined")
							login_success_handler();
					}
					catch (e) { /* may not exist for organization */ }

					populateAccountInfo();
					document.getElementById("progress_indicator1").className = "done";
					login_done = true;
					var tbl_login = document.getElementById("tbl_login");
					var change_password_dialog = document.getElementById("change_password_dialog");
					if (typeof change_password != "undefined" && change_password && tbl_login && change_password_dialog) {
						$(tbl_login).slideUp("normal", function () { $(change_password_dialog).slideDown(); });
					}
					else {
						toggle_login("hide");
					}
				}
				else {
					try {
						var tbl_login = document.getElementById("tbl_login");
						var change_password_dialog = document.getElementById("change_password_dialog");
						if (typeof change_password != "undefined" && change_password && tbl_login && change_password_dialog) {
							$(tbl_login).slideUp("normal", function () { $(change_password_dialog).slideDown(); });
						}
						else {
							standalone_login_success(); // may not exist
						}
					}
					catch (e) { }
				}
				if (rslt.CartChanged)
					refreshCart();

			}
			else {
				// some other error: we'd be interested to know!
				try {
					var msg = rslt.ErrorMessage;
					if (typeof handle_login_complete == "function") {
						var m = handle_login_complete(rslt);
						if (m && m.length > 0)
							msg += m;
					}

					Notification(msg, "result");
				}
				catch (err) {
					alert(rslt.ErrorMessage);
				}
			}
		}

	}

	var slogin = document.getElementById("tbx_login").value;
	var spassword = document.getElementById("tbx_password").value;
	if ( slogin.length == 0 || spassword.length == 0 )
	{
		Notification("Entries required in both fields!", "Login error");
		return;
	}

	var remember_me = "false";

	if ($g('chk_remember_me') != null)
		remember_me = $g('chk_remember_me').checked ? "true" : "false";
	
	var sendObj = null;
	var ddl_source_no = document.getElementById("ddl_source_no");
	if (ddl_source_no && ddl_source_no.options.length > 1 && ddl_source_no.selectedIndex < 1 && require_valid_source)
	{
		alert("Please make a selection from the dropdown menu.");
		ddl_source_no.focus();
	}
	else
	{
		if ( login_source_no < 0 )
			login_source_no = 0;
		sendObj = { login: slogin, passwd: spassword , promo_code: login_source_no , "remember_me": remember_me, "autologin":false };
	}
	if (sendObj != null )
	{
		try
		{
			$('#btn_login_submit').hide();
			$('#img_login_wait').show();		
			
			// handle_login_req.open("POST", ( srvr + "/lib/ajax/cart/login.aspx"), true);
			handle_login_req.open("POST", ( srvr + WEBSERVICE + "Login" ), true);
			// handle_login_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			handle_login_req.setRequestHeader( CONTENT_TYPE, APP_JSON);
			handle_login_req.onreadystatechange = this.handler;
			handle_login_req.send(JSON.serialize(sendObj));
		}
		catch(err)
		{
			alert( err.description ? err.description : err );
		}
	}
}

function setLoginSource(source_no) {
	login_source_no = source_no;
}

var remember_no = false;

function do_change_password(oBtn)
{
	var req = getreq();

	this.validate = function()
	{
		// public virtual LoginResp UpdateLogin( string login, string password, string email )
		var retval = { login: "", password: "", email: "" };
		try
		{
			retval.email = document.getElementById("tbx_login").value.trim();
			retval.login = retval.email;
			retval.password = document.getElementById("tbx_chgpassword").value.trim();
			var passwdrepeat = document.getElementById("tbx_chgpassword_repeat").value.trim();
			if (passwdrepeat != retval.password)
			{
				alert("Passwords do not match. Please try again,");
				document.getElementById("tbx_chgpassword").value = "";
				document.getElementById("tbx_chgpassword_repeat").value = ""
				return null;
			}
			else
				return retval;
		}
		catch (e) { return null; }
	}

	this.handler = function () {
		if (req.readyState == 4) {
			document.body.style.cursor = "default";
			if (req.status == 200) {
				rslt = JSON.parse(req.responseText);
				try {
					rslt = rslt.d;
				}
				catch (err) { }
				if (rslt.Success) {
					$(oBtn).show();
					$('#img_chgpassword_wait').hide();
					$('#change_password_dialog').slideUp("normal", function () { $('#change_password_dialog_success').slideDown('normal'); if (is_standalone_login) { standalone_login_success(); } });
				}
				else {
					alert("Sorry, updating you password seems to have failed. Unfortunately, this invalidates your current cart. Please contact our customer service for assistance to accomplish this change.");
					location.href = "cancelOrderAndLogout.aspx";
				}
			}
			else
				alert(req.responseText);
		}

	}


	var sendObj = this.validate();
	
	if (req && sendObj)
	{
		req.open("POST", (srvr + WEBSERVICE + "UpdateLogin"), true);
		req.setRequestHeader(CONTENT_TYPE, APP_JSON);
		req.onreadystatechange = this.handler;
		req.send(JSON.serialize(sendObj));
		$('#img_chgpassword_wait').show();
		$(oBtn).hide();
		document.body.style.cursor = "wait";
	}
	
}

var change_password = false;

