function mozes_conf() {
	repCodeArray = new Array();	
	uncoded = false;
	coded = false;
	if (navigator.appName == 'Microsoft Internet Explorer')
		$("#rpwzd select").hide();
	else if (navigator.appName == 'Netscape') {
		$("#mozes_prefs")
		.css('position','fixed')
		.css('left','30%')
		.css('top','20%');	
	}	
	$('#coded-rep, #uncoded-rep').empty();
	$('fieldset').hide();
	$.get("index.php/user/getMobileStatus", function(data) {
		if (data.match(/Active/)) {
			$.getJSON("index.php/user/sessionState", function(respObj) {
				var reports = respObj.reports;
				if (reports) {
					for (x in reports) {
						rep_id = reports[x].rpt_id;
						rep_name = reports[x].rep_name;
						rep_code = reports[x].rep_code;
						repCodeArray.push(rep_code);
						if (rep_code) {
							coded = true;
							$('#coded-rep').append(
								"<span class='rep-names'>" + rep_name + "</span>" +
								"<span class='rep-codes'>" + rep_code + "</span><br>"
								
							);
						}
						else {
							uncoded = true;
							$('#uncoded-rep').append(
								"<span class='rep-names'>" + rep_name + "</span>" +
								"<input type='text' class='rep-codes'" +
								" name='rep-code-" + rep_id + "'></input><br>"
							);						
						}
					}
					$('#coded-rep, #uncoded-rep').prepend(
						"<div class='rep-list-hdr'>" + 
						"<span class='rep-names'>Report Name</span>" + 
						"<span class='rep-codes'>Report Code</span>" +
						"</div>"
					);
					if (coded && uncoded)
						$('#step-3, #step-4').show();
					else if (!coded) //step 3 only
						$('#step-3').show();
					else if (!uncoded)//step 4 only
						$('#step-4').show();	

					$('#rep-code-submit').click(function() {
						repCodesSubmit();
					});	
				}
			});	
		}
		else if (data.match(/Not activated/)){//initated & not activated
			$('#step-2').show();
			if (data.match(/\[([a-zA-Z0-9]+)\]/)) {
				act_code = RegExp.lastParen;
				$('#act-code').text(act_code);
			}	
		}
		else{ //not initated
			$('#step-1').show();			
		}

		$('#mozes_prefs').slideDown();		
	});	
		
	
	
	$('#act-code-gen').click(function() {
		$('#mozes_prefs').slideUp();
		$.get("index.php/user/genMozActCode", function(data) {
			if (data.match(/success/))
				mozes_conf();
		});				
	});
	
	function repCodesSubmit() {
		newCodeArray = new Array();
		$('#error-txt').empty();
		valid = true;
		$('input', '#uncoded-rep').each(function() {
			val = $(this).val();
			if (!val) {
				$('#error-txt').text('Report code cannot be blank. Edit code');
				$(this).focus();
				valid = false;
				return false;
			}
			else if (exists_in_array(repCodeArray, val) || exists_in_array(newCodeArray, val)) {
				$('#error-txt').text('Report code not unique. Edit code');
				$(this).focus();
				valid = false;
				return false;
			}
			else if (val.length > 10) {
				$('#error-txt').text('Report code too long. Edit code');
				$(this).focus();
				valid = false;
				return false;	
			}
			else if (!val.match(/^[A-Za-z0-9]+$/)) {
				$('#error-txt').text('Report code should be of alphanumeric characters only. Edit code');
				$(this).focus();
				valid = false;
				return false;
			}
			else {
				newCodeArray.push(val);
				valid = true;
				return true;
			}	
		});
		
		if (valid) {
			count = $('input', '#uncoded-rep').length;
			data = {};
			$('input', '#uncoded-rep').each(function() {
				data[$(this).attr('name')] = $(this).val();
			});			
			$.post("index.php/user/saveReportCodes", data, function(resp) {
				if (resp.match(/success/)) {
					$('#mozes_prefs').slideUp();
					if (navigator.appName == 'Microsoft Internet Explorer')
						$("#rpwzd select").show();
					$("#alerts").text("Your report codes have been saved");
					setTimeout("$('#alerts').text('');", 5000);
				}	
			});			
		}		
	}	
}