var captchaOk=0;
var emailOk=0;
var emailNotOccupied=0;
var usernameOk=0;

var passwordCorrect=0;

function setPasswordCorrect(data)
{
	//passwordCorrect=val;
	//2/20/2012: code as response to callback. Note that the data passed in must be called data...
	var rslt=parseInt(data);
	if (rslt==0)
	{
		passwordCorrect=false;
	}
	else
	{
		passwordCorrect=true;
	}
}

function getPasswordCorrect()
{
	return passwordCorrect;
}


function setEmailNotOccupiedValue(val)
{
	emailNotOccupied=val;
}

function getEmailNotOccupiedValue()
{
	return emailNotOccupied;
}

function setCaptchaOkValue(val)
{
	captchaOk=val;
	//alert(captchaOk);
}

function getCaptchaOkValue()
{
	return captchaOk;
}

function setEmailOkValue(val)
{
	emailOk=val;
	//alert(emailOk);
}

function getEmailOkValue()
{
	return emailOk;
}

function setUsernameOkValue(val)
{
	usernameOk=val;
	//alert(usernameOk);
}

function getUsernameOkValue()
{
	return usernameOk;
}

/*
	Password Validator 0.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/
function validatePassword (pw, options) {
	// default options (allows any password)
	var o = {
		lower:    0,
		upper:    0,
		alpha:    0, /* lower + upper */
		numeric:  0,
		special:  0,
		length:   [0, Infinity],
		custom:   [ /* regexes and/or functions */ ],
		badWords: [],
		badSequenceLength: 0,
		noQwertySequences: false,
		noSequential:      false
	};

	for (var property in options)
		o[property] = options[property];

	var	re = {
			lower:   /[a-z]/g,
			upper:   /[A-Z]/g,
			alpha:   /[A-Z]/gi,
			numeric: /[0-9]/g,
			special: /[\W_]/g
		},
		rule, i;

	// enforce min/max length
	if (pw.length < o.length[0] || pw.length > o.length[1])
		return false;

	// enforce lower/upper/alpha/numeric/special rules
	for (rule in re) {
		if ((pw.match(re[rule]) || []).length < o[rule])
			return false;
	}

	// enforce word ban (case insensitive)
	for (i = 0; i < o.badWords.length; i++) {
		if (pw.toLowerCase().indexOf(o.badWords[i].toLowerCase()) > -1)
			return false;
	}

	// enforce the no sequential, identical characters rule
	if (o.noSequential && /([\S\s])\1/.test(pw))
		return false;

	// enforce alphanumeric/qwerty sequence ban rules
	if (o.badSequenceLength) {
		var	lower   = "abcdefghijklmnopqrstuvwxyz",
			upper   = lower.toUpperCase(),
			numbers = "0123456789",
			qwerty  = "qwertyuiopasdfghjklzxcvbnm",
			start   = o.badSequenceLength - 1,
			seq     = "_" + pw.slice(0, start);
		for (i = start; i < pw.length; i++) {
			seq = seq.slice(1) + pw.charAt(i);
			if (
				lower.indexOf(seq)   > -1 ||
				upper.indexOf(seq)   > -1 ||
				numbers.indexOf(seq) > -1 ||
				(o.noQwertySequences && qwerty.indexOf(seq) > -1)
			) {
				return false;
			}
		}
	}

	// enforce custom regex/function rules
	for (i = 0; i < o.custom.length; i++) {
		rule = o.custom[i];
		if (rule instanceof RegExp) {
			if (!rule.test(pw))
				return false;
		} else if (rule instanceof Function) {
			if (!rule(pw))
				return false;
		}
	}

	// great success!
	return true;
}

function checkTOSBox()
{
	if (jQuery("#tosagree").attr("checked"))
	{
		toggle_check_cross("tosapproval",-1);
		jQuery('#err_tosagree').html("");
		return true;
	}
	else
	{
		toggle_check_cross("tosapproval",1);
		jQuery('#err_tosagree').html("Please read the Terms of Service and tick this box to indicate agreement.");
		return false;
	}
}

function checkSecurityAnswerNotBlank(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("usersecanswerapproval",1);
		jQuery('#err_secanswer').html("Security answer may not be blank. Please enter an answer for your security question.");
		return false;
	}
	else
	{
		toggle_check_cross("usersecanswerapproval",-1);
		jQuery('#err_secanswer').html("");
		return true;
	}
}

function checkSecurityAnswersMatch(val, val_orig)
{

	if (val!=val_orig)
	{
		toggle_check_cross("userpsswdconfapproval",1);
		jQuery('#err_secanswer_confirmed').html("Security answers do not match. Please re-enter security answer.");
		return false;
	}
	else
	{
		toggle_check_cross("userpsswdconfapproval",-1);
		jQuery('#err_secanswer_confirmed').html("");
		return true;
	}
}

function checkPasswordConfField(val, val_orig)
{
	if (val!=val_orig)
	{
		toggle_check_cross("userpsswdconfapproval",1);
		jQuery('#err_password_confirmed').html("Passwords do not match. Please re-enter password.");
		return false;
	}
	else
	{
		toggle_check_cross("userpsswdconfapproval",-1);
		jQuery('#err_password_confirmed').html("");
		return true;
	}
}

function checkPasswordField(val)
{

	var psswd_passed = validatePassword(val, {
		length:   [8, 20],
		lower:    1,
		upper:    1,
		numeric:  1,
		special:  1,
		badWords: ["password", "apple"],//these bad words are arbitrarily chosen. For a better use, we should pass in values from a dictionary of bad values (I. Ukpo, 11/22/2011)
		badSequenceLength: 0//Alphanumeric sequence length. Just set to zero here. No ban on that.
	});

	if (!psswd_passed)
	{
		toggle_check_cross("userpsswdapproval",1);
		jQuery('#err_password').html("Password must be between 8-20 characters in length with at least one lower case letter, upper case letter, number, and special symbol (!,*, etc.)");
		return false;
	}

	else
	{
		toggle_check_cross("userpsswdapproval",-1);
		jQuery('#err_password').html("");
		return true;
	}
}

function checkPostCode(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("userzipapproval",1);
		jQuery('#err_userzipcode').html("Please enter zip/postcode.");
		return false;
	}
	else
	{
		toggle_check_cross("userzipapproval",-1);
		jQuery('#err_userzipcode').html("");
		return true;
	}
}

function checkUserCountry(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("usercountryapproval",1);
		jQuery('#err_usercountry').html("Please enter country.");
		return false;
	}
	else
	{
		toggle_check_cross("usercountryapproval",-1);
		jQuery('#err_usercountry').html("");
		return true;
	}
}

function checkUserRegion(val)
{
	if ((val.length<=0)&&(jQuery('usercountry').val()=="UNITED STATES"))
	{
		toggle_check_cross("userstateapproval",1);
		jQuery('#err_userstate').html("Please enter state.");
		return false;
	}
	else
	{
		toggle_check_cross("userstateapproval",-1);
		jQuery('#err_userstate').html("");
		return true;
	}
}

function checkUserCity(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("usercityapproval",1);
		jQuery('#err_usercity').html("Please enter city.");
		return false;
	}
	else
	{
		toggle_check_cross("usercityapproval",-1);
		jQuery('#err_usercity').html("");
		return true;
	}
}


//For these two functions, for now, just check that they are not blank.
function checkLastName(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("userlastnameapproval",1);
		jQuery('#err_lastname').html("Please enter last name.");
		return false;
	}
	else
	{
		toggle_check_cross("userlastnameapproval",-1);
		jQuery('#err_lastname').html("");
		return true;
	}
}

function checkFirstName(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("userfirstnameapproval",1);
		jQuery('#err_firstname').html("Please enter first name.");
		return false;
	}
	else
	{
		toggle_check_cross("userfirstnameapproval",-1);
		jQuery('#err_firstname').html("");
		return true;
	}
}

//Checks that a) E-mail is not already in system and b) it is of an acceptable form.
function checkEmailIsAcceptableToSystem(val,checkfield,update,usrid)
{
	//jQuery('#err_'+checkfield).html("");

	//Check that email is not already in system.
	var rslt;

	toggle_check_cross("useremailapproval",-1);
	jQuery('#err_'+checkfield).html("");
	
	//Check that e-mail address is not already in the system if this is a new user. Mark it so, then return false.
	if (update=='0')
	{
		$.ajax({
					url : 'http://www.code20.com/form_validation/emailexists.php',
					type : 'post',
					data :  {email: val},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
						rslt=parseInt(data);
						if (rslt==1)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_'+checkfield).html("This e-mail address is already in our system. Please provide a different e-mail address.");
							return false;
						}

						else if (rslt==0)
						{
							//toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							//add_remove_class('error','success','err_email');
							//jQuery('#err_email').html("");
							//return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							/*toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');
							jQuery('#err_email').html("Cannot verify e-mail is in our system at the moment. Please try again later.");
							return;*/
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
						
						}
				});
	}

	else
	{
		//Check that e-mail address is not already in the system.
		$.ajax({
					url : 'http://www.code20.com/form_validation/emailisanotheruser.php',
					type : 'post',
					data :  {email: val, usr: usrid},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) {
						var rslt=parseInt(data);
						if (rslt==1)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_'+checkfield).html("This e-mail address belongs to another user. Please provide a different e-mail address.");
							setEmailNotOccupiedValue(false);
							return false;
						}

						else if (rslt==0)
						{
							toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							//add_remove_class('error','success','err_email');
							//jQuery('#err_email').html(data);
							//setEmailNotOccupiedValue(true);
							//return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							/*toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');
							jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
							return;
							return;*/
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail at the moment. Please try again later.";
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
						
						}
				});
	}
	

	//Else, check if it is of acceptable form.
	$.ajax({
					url : 'http://www.code20.com/form_validation/email.php',
					type : 'post',
					data :  {email: val},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
						var rslt=parseInt(data);
						if (rslt==0)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_'+checkfield).html("This e-mail address is invalid. Please provide a valid e-mail address.");
							setEmailOkValue(false);
							return false;
						}

						else if (rslt==1)
						{
							toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							add_remove_class('error','success','err_email');
							jQuery('#err_'+checkfield).html("");
							setEmailOkValue(true);
							return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							/*toggle_check_cross("usernameapproval",1);
							add_remove_class('success','error','err_username');
							jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
							return;*/
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
						
						}
				});
}

function checkEmailAddressBelongsToAnotherUser(mail,usrid,wait)
{
	//Check that e-mail address is not already in the system.
	$.ajax({
					url : 'http://www.code20.com/form_validation/emailisanotheruser.php',
					type : 'post',
					data :  {email: mail, usr: usrid},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) {
						var rslt=parseInt(data);
						if (rslt==1)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_email').html("This e-mail address belongs to another user. Please provide a different e-mail address.");
							setEmailNotOccupiedValue(false);
							//return false;
						}

						else if (rslt==0)
						{
							toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							add_remove_class('error','success','err_email');
							jQuery('#err_email').html(data);
							setEmailNotOccupiedValue(true);
							//return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail at the moment. Please try again later.";
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
						}
				});

	/*if (wait==0)
	{
		$.post("form_validation/emailisanotheruser.php", {email: mail, usr: usrid }, function(data)
		{
			if(data.length >0) 
			{
				var rslt=parseInt(data);
				if (rslt==1)
				{
					toggle_check_cross("useremailapproval",1);
					add_remove_class('success','error','err_email');						
					jQuery('#err_email').html("This e-mail address belongs to another user. Please provide a different e-mail address.");
					//return false;
				}

				else if (rslt==0)
				{
					toggle_check_cross("useremailapproval",0);

					//Did below just in case wanted to write a debug message or something indicating success in verification.
					add_remove_class('error','success','err_email');
					jQuery('#err_email').html("");
					//return true;
				}
			}
			else
			{
					//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
					toggle_check_cross("useremailapproval",0);
					add_remove_class('success','error','err_email');
					jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
					//return false;
			}
		});
		
	}
	else
	{
					  $.ajax({type: "POST", url:"form_validation/emailisanotheruser.php", async:false, data: {email: mail, usr: usrid},success: function(data){
						//alert(data);
						if(data.length >0) 
						{
							var rslt=parseInt(data);
							if (rslt==1)
							{
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_username');						
								jQuery('#err_email').html("This e-mail address belongs to another user. Please provide a different e-mail address.");
								setEmailNotOccupiedValue(false);
								//return false;
							}

							else if (rslt==0)
							{
								toggle_check_cross("useremailapproval",0);

								//Did below just in case wanted to write a debug message or something indicating success in verification.
								add_remove_class('error','success','err_email');
								jQuery('#err_email').html("");
								setEmailNotOccupiedValue(true);
								//return true;
							}
						}
						else
						{
								//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_email').html("Cannot verify e-mail is in our system at the moment. Please try again later.");
								setEmailNotOccupiedValue(false);
								//return false;
						}

					}});
	}*/
	
}

//Made separate because of clashing with jQuery posts.
function checkEmailExists(val,wait)
{
	//Check that e-mail address is not already in the system.
	$.ajax({
					url : 'http://www.code20.com/form_validation/emailexists.php',
					type : 'post',
					data :  {email: val},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
						var rslt=parseInt(data);
						if (rslt==1)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_email').html("This e-mail address is already in our system. Please provide a different e-mail address.");
							//return false;
						}

						else if (rslt==0)
						{
							toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							add_remove_class('error','success','err_email');
							jQuery('#err_email').html("");
							//return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							/*toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');
							jQuery('#err_email').html("Cannot verify e-mail is in our system at the moment. Please try again later.");
							return;*/
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
						
						}
				});

	/*if (wait==0)
	{
		$.post("form_validation/emailexists.php", {email: val }, function(data)
		{
			if(data.length >0) 
			{
				var rslt=parseInt(data);
				if (rslt==1)
				{
					toggle_check_cross("useremailapproval",1);
					add_remove_class('success','error','err_email');						
					jQuery('#err_email').html("This e-mail address is already in our system. Please provide a different e-mail address.");
					//return false;
				}

				else if (rslt==0)
				{
					toggle_check_cross("useremailapproval",0);

					//Did below just in case wanted to write a debug message or something indicating success in verification.
					add_remove_class('error','success','err_email');
					jQuery('#err_email').html("");
					//return true;
				}
			}
			else
			{
					//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
					toggle_check_cross("useremailapproval",0);
					add_remove_class('success','error','err_email');
					jQuery('#err_email').html("Cannot verify e-mail is in our system at the moment. Please try again later.");
					//return false;
			}
		});
	}
	else
	{
					  $.ajax({type: "POST", url:"form_validation/emailexists.php", async:false, data: {email: val},success: function(data){
						//alert(data);
						if(data.length >0) 
						{
							var rslt=parseInt(data);
							if (rslt==1)
							{
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_username');						
								jQuery('#err_email').html("This e-mail address is already in our system. Please provide a different e-mail address.");
								setEmailOkValue(false);
								//return false;
							}

							else if (rslt==0)
							{
								toggle_check_cross("useremailapproval",0);

								//Did below just in case wanted to write a debug message or something indicating success in verification.
								add_remove_class('error','success','err_email');
								jQuery('#err_email').html("");
								setEmailOkValue(true);
								//return true;
							}
						}
						else
						{
								//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_email').html("Cannot verify e-mail is in our system at the moment. Please try again later.");
								setEmailOkValue(false);
								//return false;
						}

					}});
	}*/
}

function checkEmail(val,wait)
{
	if (val=="")
	{
		toggle_check_cross("useremailapproval",1);
		add_remove_class('success','error','err_username');						
		jQuery('#err_email').html("This e-mail address is invalid. Please provide a valid e-mail address.");
		return false;
	}

	$.ajax({
					url : 'http://www.code20.com/form_validation/email.php',
					type : 'post',
					data :  {email: val},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
						var rslt=parseInt(data);
						if (rslt==0)
						{
							toggle_check_cross("useremailapproval",1);
							add_remove_class('success','error','err_email');						
							jQuery('#err_email').html("This e-mail address is invalid. Please provide a valid e-mail address.");
							setEmailOkValue(false);
							//return false;
						}

						else if (rslt==1)
						{
							toggle_check_cross("useremailapproval",0);

							//Did below just in case wanted to write a debug message or something indicating success in verification.
							add_remove_class('error','success','err_email');
							jQuery('#err_email').html("");
							setEmailOkValue(true);
							//return true;
						}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_email');
								jQuery('#err_email').html(err_str);
							}
							/*toggle_check_cross("usernameapproval",1);
							add_remove_class('success','error','err_username');
							jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
							return;*/
						
						}
				});

	/*if (wait==0)
	{
		//Check that form is ok.
		$.post("form_validation/email.php", {email: val }, function(data)
		{
			if(data.length >0) 
			{
				var rslt=parseInt(data);
				if (rslt==0)
				{
					toggle_check_cross("useremailapproval",1);
					add_remove_class('success','error','err_username');						
					jQuery('#err_email').html("This e-mail address is invalid. Please provide a valid e-mail address.");
					//return false;
				}

				else if (rslt==1)
				{
					toggle_check_cross("useremailapproval",0);

					//Did below just in case wanted to write a debug message or something indicating success in verification.
					add_remove_class('error','success','err_email');
					jQuery('#err_email').html("");
					//return true;
				}
			}
			else
			{
					//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
					toggle_check_cross("usernameapproval",1);
					add_remove_class('success','error','err_username');
					jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
					//return false;
			}
		});
	}
	

	else
	{
					  $.ajax({type: "POST", url:"form_validation/email.php", async:false, data: {email: val },success: function(data){
						//alert(data);
						if(data.length >0) 
						{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_username');						
								jQuery('#err_email').html("This e-mail address is invalid. Please provide a valid e-mail address.");
								setEmailOkValue(false);
								//return false;
							}

							else if (rslt==1)
							{
								toggle_check_cross("useremailapproval",0);

								//Did below just in case wanted to write a debug message or something indicating success in verification.
								add_remove_class('error','success','err_email');
								jQuery('#err_email').html("");
								setEmailOkValue(true);
								//return true;
							}
						}
						else
						{
								//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_email').html("Cannot verify e-mail at the moment. Please try again later.");
								setEmailOkValue(false);
								//return false;
						}

					}});
	}*/


	
}




function lookupUsername(val,wait)
  {
	  if(val.length == 0) 
	  {
			/*toggle_check_cross("usernameapproval",-1);
			jQuery('#err_username').html("");*/
			toggle_check_cross("usernameapproval",1);
			add_remove_class('success','error','err_username');
			jQuery('#err_username').html("Please enter a username.");
			return false;
	  } 
	  else 
	  {
				jQuery('#err_username').html("Checking availability...");
				$('#usernameapproval').show();
				$.ajax({
					url : 'http://www.code20.com/form_validation/username.php',
					type : 'post',
					data :  {usr: val},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('error','success','err_username');						
								jQuery('#err_username').html("This username is available.");
								return true;
							}

							else if (rslt==1)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username is already taken. Please try another.");
								return false;
							}

							//Pose this as a question in the next meeting...do we display this?
							else if ((rslt==-1)||(rslt==-2)||(rslt==-5)||(rslt==-3)||(rslt==-4))
							{
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								return false;
							}

							//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
							else if (rslt==-7)
							{
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username contains unacceptable characters.");
								return false;
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Cannot verify username at the moment. Please try again later.";
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								return false;
							}
						}
				});
				/*if (wait==0)
				{
						jQuery('#err_username').html("Checking availability...");
						$('#usernameapproval').show();
						$.post("form_validation/username.php", {usr: val }, function(data){
						//alert(data);
						if(data.length >0) 
						{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('error','success','err_username');						
								jQuery('#err_username').html("This username is available.");
								return true;
							}

							else if (rslt==1)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username is already taken. Please try another.");
								return false;
							}

							//Pose this as a question in the next meeting...do we display this?
							else if ((rslt==-1)||(rslt==-2)||(rslt==-5)||(rslt==-3)||(rslt==-4))
							{
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								return false;
							}

							//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
							else if (rslt==-7)
							{
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username contains unacceptable characters.");
								return false;
							}
						}
						else
						{
								//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								return false;
						}

					});
				}
				else
				{
					  var rslt;
					  $.ajax({type: "POST", url:"form_validation/username.php", async:false, data: {usr: val},success: function(data){
						//alert(data);
						if(data.length >0) 
						{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('error','success','err_username');						
								jQuery('#err_username').html("This username is available.");
								setUsernameOkValue(true);
								//return true;
							}

							else if (rslt==1)
							{
								toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username is already taken. Please try another.");
								setUsernameOkValue(false);
								//return false;
							}

							//Pose this as a question in the next meeting...do we display this?
							else if ((rslt==-1)||(rslt==-2)||(rslt==-5)||(rslt==-3)||(rslt==-4))
							{
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								setUsernameOkValue(false);
								//return false;
							}

							//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
							else if (rslt==-7)
							{
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Username contains unacceptable characters.");
								setUsernameOkValue(false);
								//return false;
							}
						}
						else
						{
								//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
								toggle_check_cross("usernameapproval",1);
								add_remove_class('success','error','err_username');
								jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
								setUsernameOkValue(false);
								//return false;
						}

					}});
				}
				
	   }*/
  }
  }

  //Idea: a) Copy code for the first four functions into an AJAX call. In the success clause, call each ensuing function. When all have succeeded, execute code to process registration. Messy, but will ensure that race conditions are eliminated.
   function checkRegistrationInfo(usersecanswer,usersecanswer_confirmed,userpassword,usrzipcode,usrcountry,usrstate,usrcity,usrlastname,usrfirstname,usrname,usremail,usrsecquestion)
  {

	$.ajax({
					url : 'http://www.code20.com/form_validation/captcha.php',
					type : 'post',
					data :  {recaptcha_challenge_field: jQuery("#recaptcha_challenge_field").val(), recaptcha_response_field: jQuery("#recaptcha_response_field").val() },
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("captchaapproval",1);
								add_remove_class('success','error','err_captcha');						
								jQuery('#err_captcha').html("Incorrect words. Please try again.");

								//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
								Recaptcha.reload();

								//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
								//captchaok=false;
								return;
							}

							else if (rslt==1)
							{
								toggle_check_cross("captchaapproval",-1);
								add_remove_class('success','error','err_captcha');
								jQuery('#err_captcha').html("");
								captchaok=true;
								$.ajax({
								url : 'http://www.code20.com/form_validation/username.php',
								type : 'post',
								data :  {usr: usrname},
								dataType : 'text',
								timeout : 20000,
								tryCount : 0,
								retryLimit : 3,
								success : function(data) 
								{
										var rslt=parseInt(data);
										if (rslt==0)
										{
											//alert("here");
											toggle_check_cross("usernameapproval",data);
											add_remove_class('error','success','err_username');						
											jQuery('#err_username').html("This username is available.");
											usernameok=true;
											$.ajax({
											url : 'http://www.code20.com/form_validation/emailexists.php',
											type : 'post',
											data :  {email: usremail},
											dataType : 'text',
											timeout : 20000,
											tryCount : 0,
											retryLimit : 3,
											success : function(data) 
											{
												rslt=parseInt(data);
												if (rslt==1)
												{
													//alert("here7");
													toggle_check_cross("useremailapproval",1);
													add_remove_class('success','error','err_email');						
													jQuery('#err_'+checkfield).html("This e-mail address is already in our system. Please provide a different e-mail address.");
													//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
													//emailok=false;
													return;
												}

												else if (rslt==0)
												{
													emailok=true;
													$.ajax({
													url : 'http://www.code20.com/form_validation/email.php',
													type : 'post',
													data :  {email: usremail},
													dataType : 'text',
													timeout : 20000,
													tryCount : 0,
													retryLimit : 3,
													success : function(data) 
													{
														var rslt=parseInt(data);
														if (rslt==0)
														{
															//alert("here6");
															toggle_check_cross("useremailapproval",1);
															add_remove_class('success','error','err_email');						
															jQuery('#err_'+checkfield).html("This e-mail address is invalid. Please provide a valid e-mail address.");
															//setEmailOkValue(false);
															//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
															//emailok=false;
															return;
														}

														else if (rslt==1)
														{
															toggle_check_cross("useremailapproval",0);
															//alert("here5");
															//Did below just in case wanted to write a debug message or something indicating success in verification.
															add_remove_class('error','success','err_email');
															jQuery('#err_'+checkfield).html("");
															//setEmailOkValue(true);
														}
													},
													error : function(xhr, textStatus, errorThrown ) {
															this.tryCount++;
															if (this.tryCount <= this.retryLimit) {
																//try again
																$.ajax(this);
																return;
															}
															else
															{
																err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
																toggle_check_cross("usernameapproval",1);
																add_remove_class('success','error','err_email');
																jQuery('#err_email').html(err_str);
															}
															//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
															return;
														
														//emailok=false;
														}
												});
												}
											},
											error : function(xhr, textStatus, errorThrown ) {
													this.tryCount++;
													if (this.tryCount <= this.retryLimit) {
														//try again
														$.ajax(this);
														return;
													}
													else
													{
														err_str=xhr.status+" Server internal error. Cannot verify e-mail is in our system at the moment. Please try again later.";
														toggle_check_cross("useremailapproval",1);
														add_remove_class('success','error','err_email');
														jQuery('#err_email').html(err_str);
													}
												}
										});
										}

										else if (rslt==1)
										{
											//alert("here0");
											toggle_check_cross("usernameapproval",data);
											add_remove_class('success','error','err_username');
											jQuery('#err_username').html("Username is already taken. Please try another.");
											//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
											//usernameok=false;
											return;
										}

										//Pose this as a question in the next meeting...do we display this?
										else if ((rslt==-1)||(rslt==-2)||(rslt==-5)||(rslt==-3)||(rslt==-4))
										{
											//alert("here2");
											toggle_check_cross("usernameapproval",1);
											add_remove_class('success','error','err_username');
											jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
											//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
											//usernameok=false;
											return;
										}

										//TODO: Pose this as a question in the next meeting...do we display this? Argue that we should, because everything else forward will fail.
										else if (rslt==-7)
										{
											//alert("here3");
											toggle_check_cross("usernameapproval",1);
											add_remove_class('success','error','err_username');
											jQuery('#err_username').html("Username contains unacceptable characters.");
											//2/16/2012: AJAX absolutely unreliable for setting variables, so instead of setting values, just return on any failure.
											//usernameok=false;
											return;
										}
								},
								error : function(xhr, textStatus, errorThrown ) {
										this.tryCount++;
										if (this.tryCount <= this.retryLimit) {
											//try again
											$.ajax(this);
											return;
										}
										else
										{
											err_str=xhr.status+" Server internal error. Cannot verify username at the moment. Please try again later.";
											toggle_check_cross("usernameapproval",1);
											add_remove_class('success','error','err_username');
											jQuery('#err_username').html("Cannot verify username at the moment. Please try again later.");
										}
										return;
									
									//usernameok=false;
									}
							});
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Please try again later.";
								toggle_check_cross("captchaapproval",1);
								add_remove_class('success','error','err_captcha');
								jQuery('#err_captcha').html("Cannot verify words at the moment. Please try again later.");
								Recaptcha.reload();
							}
							return;
						
						//captchaok=false;
						}
				});

	//alert(getCaptchaOkValue());

	/*alert("usernameok: "+usernameok);

	alert("emailok: "+emailok);

	alert("captchaok: "+captchaok);*/

    var rslt=checkTOSBox() & checkSecurityAnswerNotBlank(usersecanswer) & checkSecurityAnswersMatch(usersecanswer, usersecanswer_confirmed)  & checkPostCode(usrzipcode) & checkUserCountry(usrcountry) & checkUserRegion(usrstate) & checkUserCity(usrcity) &  checkLastName(usrlastname) & checkFirstName(usrfirstname); 

	//alert(rslt);

	if (rslt==true)
	{
		//Continue with processing. Send values of fields to PHP that will CALL stored procedures to add info.
		//alert("All ok");
		/*$.post("form_processing/registration.php", {usr: usrname+"", secquestion: usrsecquestion+"", secanswer: usersecanswer+"", usrpassword: userpassword+"", userzipcode: usrzipcode+"", usercountry: usrcountry+"", userstate: usrstate+"", usercity: usrcity+"", userlastname: usrlastname+"", userfirstname: usrfirstname+"", useremail: usremail+""}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_registration');						
						jQuery('#err_registration').html("There was an error in processing registration-REGA2. Please try again later.");
					}

					else
					{
						//Set AJAX back to async.
						//$.ajaxSetup({async:true});
						window.location.href=data+"&ccode="+jQuery("#usercountrycode").val();
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						//add_remove_class('error','success','err_username');						
						//jQuery('#err_username').html("This username is available.");
						add_remove_class('success','error','err_registration');						
						jQuery('#err_registration').html("There was an error in processing registration-REGA1. Please try again later.");
				}

			});*/
			$.ajax({
					url : 'http://www.code20.com/form_processing/registration.php',
					type : 'post',
					data :  {usr: usrname+"", secquestion: usrsecquestion+"", secanswer: usersecanswer+"", usrpassword: userpassword+"", userzipcode: usrzipcode+"", usercountry: usrcountry+"", userstate: usrstate+"", usercity: usrcity+"", userlastname: usrlastname+"", userfirstname: usrfirstname+"", useremail: usremail+""},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("error");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_registration');						
								jQuery('#err_registration').html("There was an error in processing registration-REGA2. Please try again later.");
							}

							else
							{
								//Set AJAX back to async.
								//$.ajaxSetup({async:true});
								//window.location.href=data+"&ccode="+jQuery("#usercountrycode").val();
								window.location.replace(data+"&ccode="+jQuery("#usercountrycode").val());
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Please try again.  Please try again later. Please try again later.";
								add_remove_class('success','error','err_registration');						
								jQuery('#err_registration').html(err_str);
								return false;
							}
						}
				});
	}

	else
	{
		//Do nothing until fields fixed.
		alert("You have errors in your entry. Please correct your errors.");

		//Set AJAX back to async.
		//$.ajaxSetup({async:true});
	}
}
  /*function checkRegistrationInfo(usersecanswer,usersecanswer_confirmed,userpassword,usrzipcode,usrcountry,usrstate,usrcity,usrlastname,usrfirstname,usrname,usremail,usrsecquestion)
  {
	//Set AJAX to synchronous here with a delay of 7 seconds so that we are forced to wait for results from the captcha, username and e-mail functions.
	//$.ajaxSetup({async:false});
	//alert("here3");

	checkCaptcha(1);

	lookupUsername(usrname,1);

	checkEmailExists(usremail,1);

	checkEmail(usremail,1);

	//alert(getCaptchaOkValue());

    var rslt=checkTOSBox() & checkSecurityAnswerNotBlank(usersecanswer) & checkSecurityAnswersMatch(usersecanswer, usersecanswer_confirmed)  & checkPostCode(usrzipcode) & checkUserCountry(usrcountry) & checkUserRegion(usrstate) & checkUserCity(usrcity) &  checkLastName(usrlastname) & checkFirstName(usrfirstname) & getEmailOkValue() & getUsernameOkValue() & getCaptchaOkValue(); 

	//alert(rslt);

	if (rslt==true)
	{
		//Continue with processing. Send values of fields to PHP that will CALL stored procedures to add info.
		//alert("All ok");
			$.ajax({
					url : 'http://www.code20.com/form_processing/registration.php',
					type : 'post',
					data :  {usr: usrname+"", secquestion: usrsecquestion+"", secanswer: usersecanswer+"", usrpassword: userpassword+"", userzipcode: usrzipcode+"", usercountry: usrcountry+"", userstate: usrstate+"", usercity: usrcity+"", userlastname: usrlastname+"", userfirstname: usrfirstname+"", useremail: usremail+""},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("error");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_registration');						
								jQuery('#err_registration').html("There was an error in processing registration-REGA2. Please try again later.");
							}

							else
							{
								//Set AJAX back to async.
								//$.ajaxSetup({async:true});
								window.location.href=data+"&ccode="+jQuery("#usercountrycode").val();
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
						if (textStatus == 'timeout') {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							
						}
						err_str=xhr.status+" Server internal error. Please try again.  Please try again later. Please try again later.";
						add_remove_class('success','error','err_registration');						
						jQuery('#err_registration').html(err_str);
						return false;
						}
				});
	}

	else
	{
		//Do nothing until fields fixed.
		alert("You have errors in your entry. Please correct your errors.");

		//Set AJAX back to async.
		//$.ajaxSetup({async:true});
	}
}*/

function checkCaptcha(wait)
{
			$.ajax({
					url : 'http://www.code20.com/form_validation/captcha.php',
					type : 'post',
					data :  {recaptcha_challenge_field: jQuery("#recaptcha_challenge_field").val(), recaptcha_response_field: jQuery("#recaptcha_response_field").val() },
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross("captchaapproval",1);
								add_remove_class('success','error','err_captcha');						
								jQuery('#err_captcha').html("Incorrect words. Please try again.");

								//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
								Recaptcha.reload();
								return false;
							}

							else if (rslt==1)
							{
								toggle_check_cross("captchaapproval",-1);
								add_remove_class('success','error','err_captcha');
								jQuery('#err_captcha').html("");
								return true;
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								err_str=xhr.status+" Server internal error. Please try again later.";
								toggle_check_cross("captchaapproval",1);
								add_remove_class('success','error','err_captcha');
								jQuery('#err_captcha').html("Cannot verify words at the moment. Please try again later.");
								Recaptcha.reload();
								return false;
							}
							/*toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_captcha');
							jQuery('#err_captcha').html("Cannot verify words at the moment. Please try again later.");*/

							//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
							/*Recaptcha.reload();
							return false;*/
						
						}
				});
			/*if (wait==0)
			{
					$.post("form_validation/captcha.php", {recaptcha_challenge_field: jQuery("#recaptcha_challenge_field").val(), recaptcha_response_field: jQuery("#recaptcha_response_field").val() }, function(data){
					//alert(data);
					if(data.length >0) 
					{
						var rslt=parseInt(data);
						if (rslt==0)
						{
							toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_captcha');						
							jQuery('#err_captcha').html("Incorrect words. Please try again.");

							//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
							Recaptcha.reload();
							return false;
						}

						else if (rslt==1)
						{
							toggle_check_cross("captchaapproval",-1);
							add_remove_class('success','error','err_captcha');
							jQuery('#err_captcha').html("");
							return true;
						}
					}
					else
					{
							toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_captcha');
							jQuery('#err_captcha').html("Cannot verify words at the moment. Please try again later.");

							//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
							Recaptcha.reload();
							return false;
					}

				});
			}
			else
			{
				  var rslt;
				  $.ajax({type: "POST", url:"form_validation/captcha.php", async:false, data: {recaptcha_challenge_field: jQuery("#recaptcha_challenge_field").val(), recaptcha_response_field: jQuery("#recaptcha_response_field").val()},success: function(data){
					//alert(data);
					if(data.length >0) 
					{
						var rslt=parseInt(data);
						if (rslt==0)
						{
							toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_captcha');						
							jQuery('#err_captcha').html("Incorrect words. Please try again.");

							//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
							Recaptcha.reload();
							//return false;
							rslt=false;
							//echoResult(rslt);
							setCaptchaOkValue(rslt);
						}

						else if (rslt==1)
						{
							toggle_check_cross("captchaapproval",-1);
							add_remove_class('success','error','err_captcha');
							jQuery('#err_captcha').html("");
							//return true;
							rslt=true;
							setCaptchaOkValue(rslt);
							//echoResult(rslt);
						}
					}
					else
					{
							toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_captcha');
							jQuery('#err_captcha').html("Cannot verify words at the moment. Please try again later.");

							//Presumes Recaptcha object loaded on page. Else, this will likely trigger an error.
							Recaptcha.reload();
							//return false;
							rslt=false;
							setCaptchaOkValue(rslt);
							//echoResult(rslt);
					}

				}});
			}*/
}

function checkPayableNameNotBlank(val)
{
	  if(val.length == 0) 
	  {
			toggle_check_cross("userpayablenameapproval",1);
			add_remove_class('success','error','err_userpayablename');
			jQuery('#err_userpayablename').html("Please enter a name for payables.");
			return false;
	  }
	  else
	  {
		  toggle_check_cross("userpayablenameapproval",-1);
		  jQuery('#err_userpayablename').html("");
		  return true;
	  }
}

//Call on blur
function formatPhoneNumber(phone,regioncode,var_name)
{
	//alert(phone.length);
	if ((phone.length==0)&&(var_name=="userphonenumber"))
	{
		toggle_check_cross("userphonenumberapproval",1);
		add_remove_class('success','error','err_userphonenumber');
		jQuery('#err_userphonenumber').html("Please enter your phone number.");
		return false;
		//return true;
	}
	//Check if composed solely of numbers. If not, raise error.
	var regexObj = /^[-\s0-9\(\)]*$/;
	var isnum = regexObj.test(phone);

	//alert(isnum);

	if (!isnum)
	{
		toggle_check_cross("userphonenumberapproval",1);
		add_remove_class('success','error','err_userphonenumber');						
		jQuery('#err_userphonenumber').html("Phone number may contain invalid characters. Please try again.");
		return false;
	}

	//Else, Check if number valid for region.
	else
	{
		var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
		var number = phoneUtil.parseAndKeepRawInput(phone, regioncode);
		var isNumberValid = phoneUtil.isValidNumber(number);
		var isNumberValidForRegion = phoneUtil.isValidNumberForRegion(number, regioncode);

		//alert(isNumberValid);
		//alert(isNumberValidForRegion);

		//alert(var_name);

		//if it is not valid for region, leave the number alone. Else, format it to an out of country format to eliminate guesswork as to what numbers to dial.
		if (isNumberValid && isNumberValidForRegion)
		{
			//alert(phoneUtil.formatOutOfCountryCallingNumber(number, 'US'));//
			var elem_name="#"+var_name;
			jQuery(elem_name).val(phoneUtil.formatOutOfCountryCallingNumber(number, 'US'));
		}

		toggle_check_cross("userphonenumberapproval",-1);
		add_remove_class('error','success','err_userphonenumber');						
		jQuery('#err_userphonenumber').html("");

		return true;
	}
}

function echoResult(val)
{
	alert(val);
}

function showhidepaymentoptions()
{
	if (document.myid.methodpayment.value=="PayPal")
	{
	  document.getElementById("paypalinfo").style.display = "inline";
	  document.getElementById("mailingaddress").style.display = "none";
	  document.getElementById("westernunioninfo").style.display = "none";
	}
	if (document.myid.methodpayment.value=="WesternUnion")
	{
	  document.getElementById("westernunioninfo").style.display = "inline";
	  document.getElementById("paypalinfo").style.display = "none";
	  document.getElementById("mailingaddress").style.display = "none";
	}
	if (document.myid.methodpayment.value=="CheckByMail")
	{
	  document.getElementById("mailingaddress").style.display = "inline";
	  document.getElementById("paypalinfo").style.display = "none";
	  document.getElementById("westernunioninfo").style.display = "none";
	}
}

//12/2/2011: just a quick hack function to hide all boxes except video on page load.
function showOnlyVideoUploadBox()
{
	$("#viduploadbox").show();
	  $("#imguploadbox").hide();
	  $("#audiouploadbox").hide();
	  $("#docuploadbox").hide();
}

function showhideuploadboxes()
{
	if (document.myid.mediatype.value=="Video")
	{
      $("#viduploadbox").show();
	  $("#imguploadbox").hide();
	  $("#audiouploadbox").hide();
	  $("#docuploadbox").hide();
	}
	else if (document.myid.mediatype.value=="Image")
	{
	  $("#viduploadbox").hide();
	  $("#imguploadbox").show();
	  $("#audiouploadbox").hide();
	  $("#docuploadbox").hide();
	}
	else if (document.myid.mediatype.value=="Audio")
	{
	  $("#viduploadbox").hide();
	  $("#imguploadbox").hide();
	  $("#audiouploadbox").show();
	  $("#docuploadbox").hide();
	}
	else
	{
	  $("#viduploadbox").hide();
	  $("#imguploadbox").hide();
	  $("#audiouploadbox").hide();
	  $("#docuploadbox").show();
	}/**/
}


function checkPaymentInformationComplete()
{
	if (document.myid.methodpayment.value=="PayPal")
	{
		var val=jQuery('#userpaypalid').val();
		//Check that PayPal ID is not blank.
		  if(val.length == 0) 
		  {
				toggle_check_cross("userpaypalapproval",1);
				add_remove_class('success','error','err_userpaypalid');
				jQuery('#err_userpaypalid').html("Please enter your PayPal ID.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("userpaypalapproval",-1);
			  jQuery('#err_userpaypalid').html("");
			  return true;
		  }
	}
	else if (document.myid.methodpayment.value=="WesternUnion")
	{
		//Check that name, country, city, state are not blank.
		 var val=jQuery('#wuname').val();

		  if(val.length == 0) 
		  {
				toggle_check_cross("wunameapproval",1);
				add_remove_class('success','error','err_wuname');
				jQuery('#err_wuname').html("Please enter the name of the Western Union recipient.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("wunameapproval",-1);
			  jQuery('#err_wuname').html("");
			  //return true;
		  }

		  val=jQuery('#wucountry').val();
		//Check that PayPal ID is not blank.
		  if(val.length == 0) 
		  {
				toggle_check_cross("wucountryapproval",1);
				add_remove_class('success','error','err_wucountry');
				jQuery('#err_wucountry').html("Please enter the Western Union country.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("wucountryapproval",-1);
			  jQuery('#err_wucountry').html("");
			  //return true;
		  }


		  var val=jQuery('#wustate').val();
		//Check that PayPal ID is not blank.
		  if(val.length == 0) 
		  {
				toggle_check_cross("wustateapproval",1);
				add_remove_class('success','error','err_wustate');
				jQuery('#err_wustate').html("Please enter the Western Union state.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("wustateapproval",-1);
			  jQuery('#err_wustate').html("");
			  //return true;
		  }


		  var val=jQuery('#wucity').val();
		
		  if(val.length == 0) 
		  {
				toggle_check_cross("wucityapproval",1);
				add_remove_class('success','error','err_wucity');
				jQuery('#err_wucity').html("Please enter the Western Union city.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("wucityapproval",-1);
			  jQuery('#err_wucity').html("");
			  return true;
		  }
	}
	else if (document.myid.methodpayment.value=="CheckByMail")
	{
		  var val=jQuery('#mailingaddr').val();
		  if(val.length == 0) 
		  {
				toggle_check_cross("mailingaddrapproval",1);
				add_remove_class('success','error','err_wucity');
				jQuery('#err_mailingaddress').html("Please enter your mailing address.");
				return false;
		  }
		  else
		  {
			  toggle_check_cross("mailingaddrapproval",-1);
			  jQuery('#err_mailingaddress').html("");
			  return true;
		  }
	}
}

function checkPmtAddlInfo()
{
	var rslt=checkPaymentInformationComplete() & formatPhoneNumber(jQuery('#userphonenumber').val(),jQuery('#ccode').val(),'userphonenumber');
	if (rslt==true)
	{
		//Carry SessionID over to next page.
		//alert("All ok");
		$.ajax({
					url : 'http://www.code20.com/form_processing/addlinfo.php',
					type : 'post',
					data :  {sessID: jQuery('#sessid').val()+"", userpayablename: jQuery('#userpayablename').val()+"", userphonenumber: jQuery('#userphonenumber').val()+"", useraltphonenumber: jQuery('#useraltphonenumber').val()+"", pmtmethod: jQuery('#methodpayment').val()+"", paypalid: jQuery('#userpaypalid').val()+"", mailingaddr: jQuery('#mailingaddr').val()+"", wuname: jQuery('#wuname').val()+"", wucountry: jQuery('#wucountry').val()+"", wustate: jQuery('#wustate').val()+"", wucity: jQuery('#wucity').val()+"", facebook: jQuery('#facebook').val()+"", twitter: jQuery('#twitter').val()+"", skype: jQuery('#skype').val()+"" },
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("error");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_addlinfo');						
								jQuery('#err_addlinfo').html(data);
							}

							else
							{
								//window.location.href=data+jQuery('#sessid').val();
								window.location.replace(data+jQuery('#sessid').val());
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							/*toggle_check_cross("captchaapproval",1);
							add_remove_class('success','error','err_addlinfo');						
							jQuery('#err_addlinfo').html(data);*/
							else
							{
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error','err_addlinfo');						
								jQuery('#err_addlinfo').html(err_str);
							}
						
						}
				});
		/*$.post("form_processing/addlinfo.php", {sessID: jQuery('#sessid').val()+"", userpayablename: jQuery('#userpayablename').val()+"", userphonenumber: jQuery('#userphonenumber').val()+"", useraltphonenumber: jQuery('#useraltphonenumber').val()+"", pmtmethod: jQuery('#methodpayment').val()+"", paypalid: jQuery('#userpaypalid').val()+"", mailingaddr: jQuery('#mailingaddr').val()+"", wuname: jQuery('#wuname').val()+"", wucountry: jQuery('#wucountry').val()+"", wustate: jQuery('#wustate').val()+"", wucity: jQuery('#wucity').val()+"", facebook: jQuery('#facebook').val()+"", twitter: jQuery('#twitter').val()+"", skype: jQuery('#skype').val()+""}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_addlinfo');						
						jQuery('#err_addlinfo').html(data);
					}

					else
					{
						window.location.href=data;
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_addlinfo');						
						jQuery('#err_addlinfo').html(data);
				}

			});*/
	}
	else
	{
		//Stay until errors corrected.
	}
}

function checkUploadTitle(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("medtitleapproval",1);
		jQuery('#err_medtitle').html("Please enter the title for your upload.");
		return false;
	}
	else
	{
		toggle_check_cross("medtitleapproval",-1);
		jQuery('#err_medtitle').html("");
		return true;
	}
}

function checkUploadDescription(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("meddescrapproval",1);
		jQuery('#err_meddescr').html("Please enter a description for your upload.");
		return false;
	}
	else
	{
		toggle_check_cross("meddescrapproval",-1);
		jQuery('#err_meddescr').html("");
		return true;
	}
}

function checkUploadCountry(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("uploadcountryapproval",1);
		jQuery('#err_medcountry').html("Please enter country.");
		return false;
	}
	else
	{
		toggle_check_cross("uploadcountryapproval",-1);
		jQuery('#err_medcountry').html("");
		return true;
	}
}

function checkUploadRegion(val)
{
	if ((val.length<=0)&&(jQuery('medstate').val()=="UNITED STATES"))
	{
		toggle_check_cross("uploadstateapproval",1);
		jQuery('#err_medstate').html("Please enter state.");
		return false;
	}
	else
	{
		toggle_check_cross("uploadstateapproval",-1);
		jQuery('#err_medstate').html("");
		return true;
	}
}

function checkUploadCity(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("uploadcityapproval",1);
		jQuery('#err_medcity').html("Please enter city.");
		return false;
	}
	else
	{
		toggle_check_cross("uploadcityapproval",-1);
		jQuery('#err_medcity').html("");
		return true;
	}
}

function checkIDNotBlank(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("loginidapproval",1);
		jQuery('#err_loginid').html("Please enter login ID.");
		return false;
	}
	else
	{
		toggle_check_cross("loginidapproval",-1);
		jQuery('#err_loginid').html("");
		return true;
	}
}

/*function checkPasswordField(val)
{
	if (val.length<=0)
	{
		toggle_check_cross("loginpsswdapproval",1);
		jQuery('#err_loginpassword').html("Please enter password.");
		return false;
	}
	else
	{
		toggle_check_cross("loginpsswdapproval",-1);
		jQuery('#err_loginpassword').html("");
		return true;
	}
}*/

function updateRegistrationInfo(usrzipcode,usrcountry,usrstate,usrcity,usrlastname,usrfirstname,usrname,usremail,sessid)
  {

	//checkEmailExists(usremail,1);
	//checkEmailAddressBelongsToAnotherUser(usremail,usrname,1);

	//checkEmail(usremail,1);

	//alert(getCaptchaOkValue());

	//1/30/2012: don't check email vals using AJAX...still experiencing race conditions. Instead, merge check into PHP. If bad values, return error message.
    //var rslt=checkPostCode(usrzipcode) & checkUserCountry(usrcountry) & checkUserRegion(usrstate) & checkUserCity(usrcity) &  checkLastName(usrlastname) & checkFirstName(usrfirstname) & getEmailOkValue() & getEmailNotOccupiedValue();
	
	var rslt=checkPostCode(usrzipcode) & checkUserCountry(usrcountry) & checkUserRegion(usrstate) & checkUserCity(usrcity) &  checkLastName(usrlastname) & checkFirstName(usrfirstname); 

	//alert(rslt);

	if (rslt==true)
	{
		//Continue with processing. Send values of fields to PHP that will CALL stored procedures to add info.
		//alert("All ok");

		$.ajax({
					url : 'http://www.code20.com/user_mgmt/updateuserregistration.php',
					type : 'post',
					data :  {usr: usrname+"", userzipcode: usrzipcode+"", usercountry: usrcountry+"", userstate: usrstate+"", usercity: usrcity+"", userlastname: usrlastname+"", userfirstname: usrfirstname+"", useremail: usremail+"", sessionid: sessid },
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var noerror=0;
							var rslt=data.indexOf("There was an error in updating user information-REGU2");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								alert("You have errors in your entry. Please correct your errors.");
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_username');						
								jQuery('#err_email').html("This e-mail address is invalid. Please provide a valid e-mail address.");
								noerror=1;
							}

							rslt=data.indexOf("There was an error in updating user information-REGU3");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								alert("You have errors in your entry. Please correct your errors.");
								toggle_check_cross("useremailapproval",1);
								add_remove_class('success','error','err_username');						
								jQuery('#err_email').html("This e-mail address is in use by another user. Please provide another e-mail address.");
								noerror=1;
							}

							if (noerror==0)
							{
								//Set AJAX back to async.
								//$.ajaxSetup({async:true});
								//window.location.href=data+sessid;
								window.location.replace(data+sessid);
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								/*add_remove_class('success','error','err_registration');						
								jQuery('#err_registration').html("There was an error in processing registration-REGA1. Please try again later.");*/
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error','err_registration');						
								jQuery('#err_registration').html(err_str);
							}
							
						}
				});

		/*$.post("user_mgmt/updateuserregistration.php", {usr: usrname+"", userzipcode: usrzipcode+"", usercountry: usrcountry+"", userstate: usrstate+"", usercity: usrcity+"", userlastname: usrlastname+"", userfirstname: usrfirstname+"", useremail: usremail+"", sessionid: sessid}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_registration');						
						jQuery('#err_registration').html(data);
					}

					else
					{
						//Set AJAX back to async.
						//$.ajaxSetup({async:true});
						window.location.href=data+"?&sessid="+sessid;
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						//add_remove_class('error','success','err_username');						
						//jQuery('#err_username').html("This username is available.");
						add_remove_class('success','error','err_registration');						
						jQuery('#err_registration').html("There was an error in processing registration-REGA1. Please try again later.");
				}

			});*/
	}

	else
	{
		//Do nothing until fields fixed.
		alert("You have errors in your entry. Please correct your errors.");

		//Set AJAX back to async.
		//$.ajaxSetup({async:true});
	}
}


function updatePaymentAndAddlInfo(sessid)
{
	/*document.getElementById('userzipcode').value,document.getElementById('usercountry').value,document.getElementById('userstate').value,document.getElementById('usercity').value,document.getElementById('userlastname').value,document.getElementById('userfirstname').value,document.getElementById('username').value,document.getElementById('useremail').value,document.getElementById('sessid').value*/
	var rslt=checkPaymentInformationComplete() & formatPhoneNumber(jQuery('#userphonenumber').val(),jQuery('#ccode').val(),'userphonenumber');
	if (rslt==true)
	{
		//Carry SessionID over to next page.
		//alert("All ok");
		$.ajax({
					url : 'http://www.code20.com/user_mgmt/updateaddlinfo.php',
					type : 'post',
					data :  {sessID: jQuery('#sessid').val()+"", userpayablename: jQuery('#userpayablename').val()+"", userphonenumber: jQuery('#userphonenumber').val()+"", useraltphonenumber: jQuery('#useraltphonenumber').val()+"", pmtmethod: jQuery('#methodpayment').val()+"", paypalid: jQuery('#userpaypalid').val()+"", mailingaddr: jQuery('#mailingaddr').val()+"", wuname: jQuery('#wuname').val()+"", wucountry: jQuery('#wucountry').val()+"", wustate: jQuery('#wustate').val()+"", wucity: jQuery('#wucity').val()+"", facebook: jQuery('#facebook').val()+"", twitter: jQuery('#twitter').val()+"", skype: jQuery('#skype').val()+""},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("error");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','err_addlinfo');						
								jQuery('#err_addlinfo').html(data);
							}

							else
							{
								if(data.length>0)
								{
									//window.location.href=data+sessid;
									window.location.replace(data+sessid);
								}
								else
								{
									add_remove_class('success','error','err_addlinfo');
									jQuery('#err_addlinfo').html("Cannot redirect to main page. Please try again later.");
								}
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								/*add_remove_class('success','error','err_addlinfo');						
								jQuery('#err_addlinfo').html(" Server internal error. Please try again.  Please try again later. Please try again later.");*/
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error','err_addlinfo');						
								jQuery('#err_addlinfo').html(err_str);
							}
							
						}
				});

		/*$.post("user_mgmt/updateaddlinfo.php", {sessID: jQuery('#sessid').val()+"", userpayablename: jQuery('#userpayablename').val()+"", userphonenumber: jQuery('#userphonenumber').val()+"", useraltphonenumber: jQuery('#useraltphonenumber').val()+"", pmtmethod: jQuery('#methodpayment').val()+"", paypalid: jQuery('#userpaypalid').val()+"", mailingaddr: jQuery('#mailingaddr').val()+"", wuname: jQuery('#wuname').val()+"", wucountry: jQuery('#wucountry').val()+"", wustate: jQuery('#wustate').val()+"", wucity: jQuery('#wucity').val()+"", facebook: jQuery('#facebook').val()+"", twitter: jQuery('#twitter').val()+"", skype: jQuery('#skype').val()+""}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_addlinfo');						
						jQuery('#err_addlinfo').html(data);
					}

					else
					{
						if(data.length>0)
						{
							window.location.href=data+"?&sessid="+sessid;
						}
						else
						{
							add_remove_class('success','error','err_addlinfo');
							jQuery('#err_addlinfo').html("Cannot redirect to login page. Please try again later.");
						}
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','err_addlinfo');						
						jQuery('#err_addlinfo').html(data);
				}

			});*/
	}
	else
	{
		//Stay until errors corrected.
	}
}

function checkPasswordIsCorrect(usrname,psswd,errfield,checkfield)
{
	$.ajax({
					url : 'http://www.code20.com/login_processing/verifypassword.php',
					type : 'post',
					data :  {username: usrname, password: psswd},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					/*success : function(data) 
					{
							var rslt=parseInt(data);
							if (rslt==0)
							{
								toggle_check_cross(checkfield,1);
								add_remove_class('success','error',errfield);						
								jQuery('#'+errfield).html("Incorrect password.");
								//2/19/2012: Don't use because of unpredictability of AJAX value setting. Just return value instead.
								setPasswordCorrect(rslt);
								//return false;
							}

							else if (rslt>=1)
							{
								toggle_check_cross(checkfield,-1);
								add_remove_class('success','error',errfield);
								jQuery('#'+errfield).html("");
								//return true;
								rslt=1;
								//return true;

								//2/19/2012: Don't use because of unpredictability of AJAX value setting. Just return value instead.
								setPasswordCorrect(rslt);
								//echoResult(rslt);
							}
					},*/
					//2/20/2012: new method for setting values-define external function as success callback.
					success : setPasswordCorrect,
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								/*add_remove_class('success','error',errfield);
								jQuery('#'+errfield).html("Cannot verify password at the moment. Please try again later.");*/
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error',errfield);
								jQuery('#'+errfield).html(err_str);
								//return false;
							}
							
						}
				});
	/*$.ajax({type: "POST", url:"login_processing/verifypassword.php", async:false, data: {username: usrname, password: psswd},success: function(data){
					//alert(data);
					if(data.length >0) 
					{
						var rslt=parseInt(data);
						if (rslt==0)
						{
							toggle_check_cross(checkfield,1);
							add_remove_class('success','error',errfield);						
							jQuery('#'+errfield).html("Incorrect password.");
							setPasswordCorrect(rslt);
						}

						else if (rslt>=1)
						{
							toggle_check_cross(checkfield,-1);
							add_remove_class('success','error',errfield);
							jQuery('#'+errfield).html("");
							//return true;
							rslt=1;
							setPasswordCorrect(rslt);
							//echoResult(rslt);
						}
					}
					else
					{
							toggle_check_cross(checkfield,1);
							add_remove_class('success','error',errfield);
							jQuery('#'+errfield).html("Cannot verify password at the moment. Please try again later.");
					}

				}});*/
				//alert("Down here");
}

function changePassword(usrid,psswd,new_psswd,psswd_conf,errfield,checkfield,sessid)
{
	checkPasswordIsCorrect(usrid,psswd,errfield,checkfield);
	//alert(getPasswordCorrect());
	var rslt=getPasswordCorrect() & checkPasswordConfField(new_psswd,psswd_conf);
	//alert(rslt);
	if (rslt==true)
	{
		$.ajax({
					url : 'http://www.code20.com/user_mgmt/updatepassword.php',
					type : 'post',
					data :  {userid: usrid, password: new_psswd, sessID: sessid},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("error");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html(data);
							}

							else
							{
								if(data.length>0)
								{
									//window.location.href=data+sessid;
									alert("Password reset successful.");
									window.location.replace(data+sessid);
								}
								else
								{
									add_remove_class('success','error','#err_'+checkfield);
									jQuery('#err_'+errfield).html("Cannot redirect to main page. Please try again later.");
								}
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								/*add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html("Cannot redirect to main page. Please try again later.");*/
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html(err_str);
							}
							
						}
				});
		/*$.post("user_mgmt/updatepassword.php", {userid: usrid, password: new_psswd}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','#err_'+checkfield);						
						jQuery('#err_'+errfield).html(data);
					}

					else
					{
						if(data.length>0)
						{
							window.location.href=data+"?&sessid="+sessid;
						}
						else
						{
							add_remove_class('success','error','#err_'+checkfield);
							jQuery('#err_'+errfield).html("Cannot redirect to login page. Please try again later.");
						}
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','#err_'+checkfield);						
						jQuery('#err_'+errfield).html(data);
				}

			});*/
	}
}


//2/1/2012: update to deny change in PHP if password is not correct. Don't use AJAX for verification because of race conditions (so, drop it out of setting value of rslt)
function changeSQA(usrid,psswd,secq,seca,errfield,checkfield,sessid)
{
	checkPasswordIsCorrect(usrid,psswd,errfield,checkfield);
	
	var rslt=checkSecurityAnswerNotBlank(seca);

	if (rslt==true)
	{
		$.ajax({
					url : 'http://www.code20.com/user_mgmt/updatesqa.php',
					type : 'post',
					data :  {userid: usrid, password: psswd, secquestion: secq, secanswer: seca},
					dataType : 'text',
					timeout : 20000,
					tryCount : 0,
					retryLimit : 3,
					success : function(data) 
					{
							var rslt=data.indexOf("ERRUSQA1");
							if (rslt!=-1)
							{
								//State that there was a general error in processing and that they should try again later.
								//toggle_check_cross("usernameapproval",data);
								add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html(data);
							}

							else
							{
								if(data.length>0)
								{
									//window.location.href=data+sessid;
									window.location.replace(data+sessid);
								}
								else
								{
									add_remove_class('success','error','#err_'+checkfield);
									jQuery('#err_'+errfield).html("Cannot redirect to main page. Please try again later.");
								}
							}
					},
					error : function(xhr, textStatus, errorThrown ) {
							this.tryCount++;
							if (this.tryCount <= this.retryLimit) {
								//try again
								$.ajax(this);
								return;
							}
							else
							{
								/*add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html("Cannot redirect to main page. Please try again later.");*/
								err_str=xhr.status+" Server internal error. Please try again later.";
								add_remove_class('success','error','#err_'+checkfield);						
								jQuery('#err_'+errfield).html(err_str);
							}
							
						}
				});
		/*$.post("user_mgmt/updatesqa.php", {userid: usrid, secquestion: secq, secanswer: seca}, function(data){
				//alert(data);
				if(data.length >0) 
				{
					var rslt=data.indexOf("error");
					if (rslt!=-1)
					{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','#err_'+checkfield);						
						jQuery('#err_'+errfield).html(data);
					}

					else
					{
						if(data.length>0)
						{
							window.location.href=data+"?&sessid="+sessid;
						}
						else
						{
							add_remove_class('success','error','#err_'+checkfield);
							jQuery('#err_'+errfield).html("Cannot redirect to login page. Please try again later.");
						}
					}
				}
				else
				{
						//State that there was a general error in processing and that they should try again later.
						//toggle_check_cross("usernameapproval",data);
						add_remove_class('success','error','#err_'+checkfield);						
						jQuery('#err_'+errfield).html(data);
				}

			});*/
	}
}
