//**********************************************************************************************************
function editUserAccount(user_id, account_id){
	$('#mainUserListDiv').slideUp(300, function(){
		getUserAccountFrm('editUserFrmContent', user_id, account_id);
		$('#editUserFrmContent').slideDown(300, function(){
			$('.backToListLink').show();
			 return false;
		});
		return false;
	});
	return false;
}
//**********************************************************************************************************
function returnToUserAccountList(){
	$('.backToListLink').hide();
	$('#editUserFrmContent').slideUp(300, function(){
		$('#userListDiv').hide();
		$('#mainUserListDiv').slideDown(300, function(){
			pager($('#srt').val(), $('#rpp').val(), $('#page').val());
			return false;
		});
		return false;
	});
	return false;
}
//**********************************************************************************************************
function getUserAccountFrm(containerDiv, user_id, account_id){
	if(account_id == 'AdminAdd'){ user_id = account_id; }
	$('#'+containerDiv).empty();
	$.post('../include/users.php', { mtd: 'getUserAccountFrm', user_id: user_id, account_id: account_id },
	function(data){
		switch(data){
			case 'AccessDenied':
			case 'NoAccountRequested':
			case 'UserNotFound':
			case '':
				alert(data);
			break;
			default:
				if(user_id=='Add'){
					$('#'+containerDiv).hide();
					$('#'+containerDiv).append(data);
					$('#'+containerDiv).width(1);
					$('#'+containerDiv).css('opacity', 0.0);
					$('#'+containerDiv).animate({ 
						width: '805px',
						opacity: 0.9
					  });
					initUserAccountFrm();
				}else{
					$('#'+containerDiv).append(data);
					initUserAccountFrm();
				}
				$('#'+containerDiv).append('<input name="userAccountContainerDivID" type="hidden" id="userAccountContainerDivID" value="'+containerDiv+'" />');
			break;
		}
		return false;
	});
	return false;
}
//**********************************************************************************************************
function initUserAccountFrm(){
	if(parseInt($('#ulvl').val()) > parseInt($('#user_level').val()) && $('#user_level').val() != '-10'){
		if($('#auid').val() != $('#account_id').val()){
			$('#userAccountContainerDivID').empty();
			showResponse('<span class="smallred">You\'re Account does not have Permission to Edit this Account.</span>');
			return false;
		}
		$('#userAccountStatus').val('Revised');
		$('div.userAccountStatusLightDiv').flashembed(
			{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
			{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
		);
		$('#userMsgTd').empty();
		$('#userMsgTd').append('<span class="smallred">You\'re Account does not have Permission to Edit this Account.</span>');
		return false;
	}
	$('div.userAccountStatusLightDiv').flashembed(
		{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
		{CurrentStatus: $('#userAccountStatus').val(), doBlink: 'no' }
	);
	if($('#accountStatus').val() != undefined){
		$('#accountStatusDiv').flashembed(
			{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
			{CurrentStatus: $('#accountStatus').val(), doBlink: 'no' }
		);
	}
	var flds = String($('#userAccountFields').val()).split(':');
	for(var i=1; i < flds.length; i = i + 5){
		if(flds[(i+3)] == 'true'){
			$('#'+flds[i]).before('<span id="'+flds[i]+'Req" class="smallred">* </span>');
		}else{
			switch(flds[(i+1)]){
				case 'country':
				case 'website':
					//do nothing
				break;
				default:
					$('#'+flds[i]).before('<span id="'+flds[i]+'Req" class="smallwhite">* </span>');
				break;
			}
		}
		switch(flds[(i+2)]){
			case 'textbox':
			case 'numeric':
			case 'password':
				$('#'+flds[i]).bind('keyup', function(){ validateUserAccount('yes'); });
			break;
			case 'dropdown':
			case 'dropdownVal':
				$('#'+flds[i]).bind('change', function(){ validateUserAccount('yes'); });
				switch(flds[i]){
					case 'user_level':
					case 'accountStatus':
						if($('#user_id').val()==$('#auid').val()){
							$('#'+flds[i]).attr('disabled', true);
							$('#'+flds[i]).attr('title', 'You can not edit your own '+flds[(i-1)]);
						}
					break;
				}
			break;
			case 'checkbox':
				$('#'+flds[i]).bind('click', function(){ validateUserAccount('yes'); });
			break;
			case 'hidden':
			default:
				//do nothing
			break;
		}
	}
	if($('#user_id').val() != 'Add'){ validateUserAccount('no'); }
	return false;
}
//**********************************************************************************************************
function updateAccountStatus(){
	$('#accountStatusDiv').flashembed(
		{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
		{CurrentStatus: $('#accountStatus').val(), doBlink: 'yes' }
	);
	return false;
}
//**********************************************************************************************************
function validateUserAccount(doBlink){
	if($('#userAccountFields').val()==undefined){ return false; }
	var missingInfo = false;
	var flds = String($('#userAccountFields').val()).split(':');
	for(var i=1; i < flds.length; i = i + 5){
		$('#'+flds[i]+'Msg').remove();
		if(flds[(i+3)]=='true'){
			switch(flds[(i+2)]){
				case 'hidden':
				case 'textbox':
					if($('#'+flds[i]).val()==''){
						missingInfo = true;
					}
				break;
				case 'password':
					if($('#'+flds[i]).val() != ':NOPASSWORD:'){
						if($('#'+flds[i]).val()=='' || $('#'+flds[i]).val() != $('#password2').val()){
							appendError(flds[(i+1)], '<em>Passwords don\'t match.</em>', false);
							$('#'+flds[i]+'Msg').show();
							missingInfo = true;
						}else{
							//$('#'+flds[i]+'Msg').hide();
						}
					}
				break;
				case 'numeric':
					if(parseFloat($('#'+flds[i]).val())<=0 || parseFloat($('#'+flds[i]).val()).toString()=='NaN'){
						missingInfo = true;
					}
				break;
				case 'dropdown':
				case 'dropdownVal':
					if($('#'+flds[i]).val()=='' || $('#'+flds[i]).val()=='-10'){
						missingInfo = true;
					}
				break;
				case 'checkbox':
					if($('#'+flds[i]).attr('checked')==false){
						missingInfo = true;
					}
				break;
			}
		}
	}
	/************************/
	if(missingInfo == false){
		switch(doBlink){
			case 'yes':
				if($('#userAccountInEditMode').val() == 'no' || $('#userAccountStatus').val() != 'Ready'){
					$('#saveAccountBtn').attr('disabled', '');
					$('#userAccountInEditMode').val('yes');
					$('#userAccountStatus').val('Ready');
					$('div.userAccountStatusLightDiv').flashembed(
						{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
						{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
					);
				}
			break;
			case 'no':
			default:
				$('#saveAccountBtn').attr('disabled', 'diabled');
				$('#userAccountInEditMode').val('no');
				$('#userAccountStatus').val('Active');
				$('div.userAccountStatusLightDiv').flashembed(
					{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
					{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
				);
			break;
		}
	}else{
		switch(doBlink){
			case 'yes':
				if($('#userAccountInEditMode').val() == 'no' || $('#userAccountStatus').val() != 'NotReady'){
					$('#saveAccountBtn').attr('disabled', 'diabled');
					$('#userAccountInEditMode').val('yes');
					$('#userAccountStatus').val('NotReady');
					$('div.userAccountStatusLightDiv').flashembed(
						{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
						{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
					);
				}
			break;
			case 'no':
			default:
				$('#saveAccountBtn').attr('disabled', 'diabled');
				$('#userAccountInEditMode').val('no');
				$('#userAccountStatus').val('NotReady');
				$('div.userAccountStatusLightDiv').flashembed(
					{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
					{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
				);
			break;
		}
	}
	return false;
}
//***************************************************************************
function saveUserAccount(){
	if($('#saveAccountBtn').attr('disabled')==false){
		$('#saveAccountBtn').attr('disabled', 'disabled');
		addLoader('div.userAccountLoadingStatus');
		$('#userAccountInEditMode').val('no');
		var flds = String($('#userAccountFields').val()).split(':');
		var c = Array();
		var v = Array();
		for(var i=1; i < flds.length; i = i + 5){
			switch(flds[(i+2)]){
				case 'hidden':
				case 'textbox':
				case 'numeric':
				case 'dropdown':
				case 'dropdownVal':
					if(flds[i] == 'usrTZ'){
						var tz = String($('#'+flds[i]).val()).split('|');
						c.push('tz_id');
					  	v.push(tz[0]);
						c.push('tz_abbreviation');
					  	v.push(tz[1]);
						c.push('tz_offset');
					  	v.push(tz[2]);
					}else{
					  	c.push(flds[(i+1)]);
					  	v.push($('#'+flds[i]).val());
					}
				break;
				case 'dropdownText':
					c.push(flds[(i+1)]);
					v.push($('#'+flds[i]+' :selected').text());
				break;
				case 'password':
					if(flds[i] != 'password2'){
						if($('#'+flds[i]).val() != ':NOPASSWORD:'){
							c.push(flds[(i+1)]);
							v.push($('#'+flds[i]).val());
						}
					}
				break;
			}
		}
		/*************/
		$.post('../include/users.php', { mtd: 'saveUserAccount', user_id: $('#user_id').val(), account_id: $('#account_id').val(), c: c.join(':'), v: v.join(':||:') },
		function(data){
			var doBlink = 'no';
			var dat = data.split(':');
			switch(dat[0]){
				case 'Success':
					switch(dat[1]){
						case 'Added':
							switch(dat[2]){
								case 'Added':
									var usrFrmContainer = $('#userAccountContainerDivID').val();
									$('#'+usrFrmContainer).fadeOut(2000, function(){
										$('#'+usrFrmContainer).empty();
										$('#'+usrFrmContainer).append('<span class="heading">Thank you for registering with shopbuilder.ca A confirmation Email has been sent to you.</span>');
										$('#'+usrFrmContainer).fadeIn(2000);
									});
									return false;
								break;
								default:
									$('#user_id').val(dat[2]);
									$('#userAccountStatus').val('Active');
									showResponse('User Account Added.');
								break;
							}
						break;
						case 'Updated':
							$('#userAccountStatus').val('Active');
							showResponse('User Account Updated.');
						break;
					}
				break;
				case 'Failure':
					switch(dat[1]){
						case 'AccessDenied':
							$('#userAccountContainerDivID').fadeOut(500, function(){
								$('#userAccountContainerDivID').empty();
								$('#userAccountContainerDivID').append('<span class="heading2red">'+dat[3]+'</span>');
								$('#userAccountContainerDivID').fadeIn(2000);
							});
							return false;
						break;
						case 'NoMethodRequested':
							alert(dat[3]);
						break;
						case 'UserNameAlreadyExists':
						case 'InvalidEmail':
						case 'EmailAlreadyExists':
							appendError(dat[2], dat[3], true);
							$('#userAccountStatus').val('NotReady');
							doBlink = 'yes';
						break;
						default:
							$('#userAccountStatus').val('NotReady');
							doBlink = 'yes';
							alert('An error occured while saving:'+data);
						break;
					}
				break;
				default:
					alert('An error occured:'+data);
				break;
			}
			$('div.userAccountStatusLightDiv').flashembed(
				{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
				{CurrentStatus: $('#userAccountStatus').val(), doBlink: doBlink }
			);
			fadeOutLoader('div.userAccountLoadingStatus');
			return false;
		});
	}
	return false;
}
//**********************************************************************************************************
function appendError(c, msg, selectFld){
	var flds = $('#userAccountFields').val().split(':');
	var fld = '';
	for(var i = 1; i < flds.length; i++){
		if(c == flds[i]){
			if(flds[i]==flds[(i+1)]){
				fld = flds[i];
			}else{
				fld = flds[(i-1)];
			}
			$('#'+fld+'Msg').remove();
			$('#'+fld).after('<span id="'+fld+'Msg" class="smallred">'+msg+'</span>');
			if(selectFld==true){
				$('#'+fld).focus();
				$('#'+fld).select();
			}
			return false;
		}
	}
	return false;
}
//**********************************************************************************************************
function pager(srt, rpp, page){
	addLoader('#searchstatus');
	$('#spacer').attr('height',$('#userstbl').height()+2);
	if(rpp != $('#rp').val()){
		page = 1;
	}
	$('#rp').val(rpp);
	$('#rpp').val(rpp);
	$('#page').val(page);
	$('#srt').val(srt);
	$.post('../include/users.php', { mtd: 'pager', srt: srt, rpp: rpp, page: page, srch: $('#srch').val() },
	function(data){
		$('#userListDiv').fadeOut(200, function(){
			$('#userstbl').remove();
			$('#userListDiv').prepend(data).fadeIn(200, function(){
				$('#spacer').attr('height','1');
				fadeOutLoader('#searchstatus');
				return false;
			});
			return false;
		});
		return false;
	});
	return false;
}
//**********************************************************************************************************
function remUserAccount(row){
	var rem = confirm('Are you sure that you want to remove this user and all related data?');
	if(rem != true){
		return false;
	}
	if($('#user_id'+row).val() == $('#account_id'+row).val()){
		rem = confirm('You do realize that you clicked on remove user, right?');
		if(rem != true){
			return false;
		}
	}
	addLoader('#userRowLoadingStatusDiv'+row);
	$.post('../include/users.php', { mtd: 'remUserAccount', user_id: $('#user_id'+row).val(), account_id: $('#account_id'+row).val() },
	function(data){
		var doBlink = 'no';
		var dat = data.split(':');
		switch(dat[0]){
			case 'Success':
				switch(dat[1]){
					case 'Removed':
						fadeOutLoader('#userRowLoadingStatusDiv'+row);
						pager($('#srt').val(), $('#rpp').val(), $('#page').val());
						return false;
					break;
				}
			break;
			case 'Failure':
			default:
				$('#userRowEditStatus'+row).val('NotReady');
				doBlink = 'yes';
				switch(dat[1]){
					case 'AccessDenied':
						alert(dat[2]);
					break;
					default:
						alert('An error occured while saving: '+data);
					break;
				}
				$('#userRowEditStatusDiv'+row).flashembed(
					{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
					{CurrentStatus: $('#userRowEditStatus'+row).val(), doBlink: doBlink }
				);
			break;
		}
		fadeOutLoader('#userRowLoadingStatusDiv'+row);
		return false;
	});
	return false;
}
//**********************************************************************************************************
function onUserListUserLevelChanged(row){
	$('#ulvlImg'+row).attr('src', '../graphics/userLevelImage'+$('#user_level'+row).val()+'.png');
	return false;
}
//**********************************************************************************************************
function validateUserListRow(row, doBlink){
	if($('#userRowFields').val() == undefined){ return false; }
	var missingInfo = false;
	var flds = String($('#userRowFields').val()).split(':');
	for(var i = 1; i < flds.length; i = i + 5){
		if(flds[(i+3)] == 'true'){
			switch(flds[(i+2)]){
				case 'hidden':
				case 'textbox':
					if($('#'+flds[i]+row).val() == ''){
						missingInfo = true;
					}
				break;
				case 'numeric':
					if(parseFloat($('#'+flds[i]+row).val()) <= 0 || parseFloat($('#'+flds[i]+row).val()).toString() == 'NaN'){
						missingInfo = true;
					}
				break;
				case 'dropdown':
				case 'dropdownVal':
					if($('#'+flds[i]+row).val() == '' || $('#'+flds[i]+row).val() == '-10'){
						missingInfo = true;
					}
				break;
				case 'dropdownText':
					if($('#'+flds[i]+row+' :selected').text() == ''){
						missingInfo = true;
					}
				break;
			}
		}
	}
	/***********************/
	if(missingInfo == false){

		var newStatus = '';
		if(doBlink=='no'){
			newStatus = 'Active';
		}else{
			newStatus = 'InActive';
			$('#userRowSaveBtn'+row).attr('disabled', '');
		}
		if($('#userRowEditStatus'+row).val()!=newStatus){
			$('#userRowEditStatus'+row).val(newStatus);
			$('#userRowEditStatusDiv'+row).flashembed(
				{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
				{CurrentStatus: $('#userRowEditStatus'+row).val(), doBlink: doBlink }
			);
			$('#accountStatusDiv'+row).flashembed(
				{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
				{CurrentStatus: $('#accountStatus'+row).val(), doBlink: doBlink }
			);
		}
	}else{
		if($('#userRowEditStatus'+row).val()!='NotReady'){
			$('#userRowEditStatus'+row).val('NotReady');
			$('#userRowEditStatusDiv'+row).flashembed(
				{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
				{CurrentStatus: $('#userRowEditStatus'+row).val(), doBlink: doBlink }
			);
			$('#accountStatusDiv'+row).flashembed(
				{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
				{CurrentStatus: $('#accountStatus'+row).val(), doBlink: doBlink }
			);
		}
		$('#userRowSaveBtn'+row).attr('disabled', 'diabled');
	}
	return false;
}
//***************************************************************************
function saveUserListRow(row){
	if($('#userRowSaveBtn'+row).attr('disabled')!=false){ return false; }
	$('#userRowSaveBtn'+row).attr('disabled', 'disabled');
	addLoader('#userRowLoadingStatusDiv'+row);
	var flds = String($('#userRowFields').val()).split(':');
	var v = Array();
	var c = Array();
	for(var i = 1; i < flds.length; i = i + 5){
		switch(flds[(i+2)]){
			case 'hidden':
			case 'textbox':
			case 'numeric':
			case 'dropdown':
			case 'dropdownVal':
				c.push(flds[(i+1)]);
				v.push($('#'+flds[i]+row).val());
			break;
			case 'dropdownText':
				c.push(flds[(i+1)]);
				v.push($('#'+flds[i]+row+' :selected').text());
			break;
		}
	}
	/****************************/
	$.post('../include/users.php', { mtd: 'saveUserListRow', user_id: $('#user_id'+row).val(), account_id: $('#account_id'+row).val(), c: c.join(':'), v: v.join(':||:') },
	function(data){
		var doBlink = 'no';
		var dat = data.split(':');
		switch(dat[0]){
			case 'Success':
				switch(dat[1]){
					case 'Added':
						alert('That is strange! You should not be able to add users from the user list?');
					break;
					case 'Updated':
						$('#userRowEditStatus'+row).val('Active');
					break;
				}
			break;
			case 'Failure':
				switch(dat[1]){
					case 'AccessDenied':
					case 'NoMethodRequested':
					case 'UserNameAlreadyExists':
					case 'InvalidEmail':
					case 'EmailAlreadyExists':
						$('#userRowEditStatus'+row).val('NotReady');
						doBlink = 'yes';
						alert(dat[3]);
					break;
					default:
						$('#userRowEditStatus'+row).val('NotReady');
						doBlink = 'yes';
						alert('An error occured while saving: '+data);
					break;
				}
			break;
		}
		$('#userRowEditStatusDiv'+row).flashembed(
			{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
			{CurrentStatus: $('#userRowEditStatus'+row).val(), doBlink: doBlink }
		);
		$('#accountStatusDiv'+row).flashembed(
			{src: '../flex/StatusLight/StatusLight.swf', wmode: 'transparent'},
			{CurrentStatus: $('#accountStatus'+row).val(), doBlink: doBlink }
		);
		fadeOutLoader('#userRowLoadingStatusDiv'+row);
		return false;
	});
	return false;
}
//***************************************************************************