// render.js : functions that build html strings

function makeLFDisplayPair(o) {
	var s='';
	s += '<div id="' + o.id + '" class="' + o.rowClass + '">';
	s += '<label id="label' + o.id + '" class="' + o.labelClass + '">' + o.labelValue + '</label>';
	s += '<div id="field' + o.id + '" class="' + o.fieldClass + '">' + o.fieldValue + '</div>';
	s += '</div>';
	return s;
}

function makeLFFormPair(o) {
	var s='';
	s += '<div id="' + o.id + '" class="' + o.rowClass + '">';
	s += '<label id="label' + o.id + '" class="' + o.labelClass + '">' + o.labelValue + '</label>';
	s += '<input id="field' + o.id + '" class="' + o.fieldClass + '" value="' + o.fieldValue + '" size="50" />';
	s += '</div>';
	return s;
}

function makeLookupSelect(o)  {
	s='';
	var a = new Array();
	var l = global.lookup[o.lookup];
	for(k in l) {
		a.push(k);
	}
	a.sort(sortNumeric);
	
	s='<select id="' + o.id + '">';
	s+='<option value="0"' + (o.selected==0?' SELECTED':'') + '>...</option>';
	for(var i=0;i<a.length;i++) {
		s+='<option value="' + a[i] + '"' + (a[i]==o.selected ? ' SELECTED' : '') + '>' + l[a[i]] + '</option>';
	}
	s+='</select>';
	return s;
}

function renderProfileMain(o,template) {
	var s = '';
	switch(template) {	
	
		case 'tabDisplay':
		
			// this holds the fields that actually have values
			var fields = new Object();
			
			// associate labels with fields
			var labels = { name: "Name",
						   location: "Location",
						   age: "Age",
						   sex: "Sex",
						   occupation: "Occupation",
						   soehandle: "SOE Handle",
						   registered: "Created" };
			
			// index of fields to look up automatically
			var a=['location','age','sex','occupation','soehandle'];
				
				
			// handle custom fields
			if(o.firstname.length || o.lastname.length) { fields.name = (o.firstname + " " + o.lastname).trim(); }
			fields.registered = formatArchiveMessageDate(o.signupdate) + ' (#' + o.usernumber + ')';
			
			// test for definition
			for(i=0;i<a.length;i++) {
				f=a[i];
				if((o[f] + '').trim().length) {fields[f]=o[f]}
			}			
			
			// add custom fields to the index
			a.unshift('registered');
			a.unshift('name');			
			
			// make Label/Field pairs
			for(i=0;i<a.length;i++) {
				f=a[i];

				args= {	id: "profileMain" + f,
						rowClass: "profileDisplayRow",
						labelClass: "profileDisplayLabel",
						labelValue: labels[f],
						fieldClass: "profileDisplayField",
						fieldValue: (fields[f] ? fields[f] : 'not specified')  };
							
				s+=makeLFDisplayPair(args);

			}
			
		break;
		
		
		case 'tabForm':
		
			s += '<form onSubmit="return editProfileMain();">'
		
			// this holds the fields that actually have values
			var fields = new Object();
			
			// associate labels with fields
			var labels = { firstname: "First Name",
						   lastname: "Last Name",
						   location: "Location",
						   age: "Age",
						   sex: "Sex",
						   occupation: "Occupation",
						   soehandle: "SOE Handle" };
			
			// index of fields to look up automatically
			var a=['firstname','lastname','location','age','sex','occupation','soehandle'];
			
			// test for definition
			for(i=0;i<a.length;i++) {
				f=a[i];
				if((o[f] + '').length) {fields[f]=o[f]}
			}
			
			// make Label/Field pairs
			for(i=0;i<a.length;i++) {
				f=a[i];
				
				args= {	id: "profileMain" + f,
						rowClass: "profileDisplayRow",
						labelClass: "profileFormLabel",
						labelValue: labels[f],
						fieldClass: "profileFormField",
						fieldValue: (fields[f] ? fields[f] : '')  };
						
				if(f=='sex') {
					s += '<div id="profileMainsex" class="profileDisplayRow">';
					s += '<label class="profileFormLabel" id="labelprofileMainsex">Sex</label>';
					s += '<select class="profileFormField" id="fieldprofileMainsex">';
					s += '<option value="not specified"' + (fields[f] == '' ? ' SELECTED' : '') + '>not specified</option>';
					s += '<option value="M"' + (fields[f]=='M' ? ' SELECTED' : '') + '>M</option>';
					s += '<option value="F"' + (fields[f]=='F' ? ' SELECTED' : '') + '>F</option>';
					s += '</select>';
					s += '</div>';
				} else {			
					s+=makeLFFormPair(args);
				}

			}
			
			s += '<label class="profileFormLabel">&nbsp;</label>';
			s += '<div class="profileDisplayField">';
			s += '<input type="submit" class="button" value="submit">';
			s += '&nbsp;<button onClick="pnlProfile.populateUserMainAsDisplay();return false;">cancel</button>';
			s += '</div></form>';
			
		break;		
		
		
	}
	
	return s;

}

function renderProfileContact(o,template) {
	var s = ''
	switch(template) {	
	
		case 'tabDisplay':
		
			// this holds the fields that actually have values
			var fields = new Object();
			
			// associate labels with fields
			var labels = { email: "Email",
						   im_yahoo: "Yahoo",
						   im_msn: "MSN",
						   im_aim: "AIM",
						   im_google: "Google Talk",
						   im_icq: "ICQ" };
			
			// index of fields to look up automatically
			var a=['email','im_yahoo','im_msn','im_aim','im_google','im_icq'];

			// test for definition
			for(i=0;i<a.length;i++) {
				f=a[i];
				if((o[f] + '').length) {fields[f]=o[f]}
			}
			
			// make Label/Field pairs
			for(i=0;i<a.length;i++) {
				f=a[i];

				args= {	id: "profileMain" + f,
						rowClass: "profileDisplayRow",
						labelClass: "profileDisplayLabel",
						labelValue: labels[f],
						fieldClass: "profileDisplayField",
						fieldValue: (fields[f] ? fields[f] : 'not specified')  };
							
				s+=makeLFDisplayPair(args);

			}
			
		break;
		
		case 'tabForm':
		
			s += '<form onSubmit="return editProfileContact();">';	
		
			// this holds the fields that actually have values
			var fields = new Object();
			
			// associate labels with fields
			var labels = { email: "Email",
						   im_yahoo: "Yahoo",
						   im_msn: "MSN",
						   im_aim: "AIM",
						   im_google: "Google Talk",
						   im_icq: "ICQ" };
			
			// index of fields to look up automatically
			var a=['email',,'im_yahoo','im_msn','im_aim','im_google','im_icq'];

			// test for definition
			for(i=0;i<a.length;i++) {
				f=a[i];
				if((o[f] + '').length) {fields[f]=o[f]}
			}
			
			// make Label/Field pairs
			for(i=0;i<a.length;i++) {
				f=a[i];

				args= {	id: "profileContact" + f,
						rowClass: "profileDisplayRow",
						labelClass: "profileFormLabel",
						labelValue: labels[f],
						fieldClass: "profileFormField",
						fieldValue: (fields[f] ? fields[f] : '')  };
							
				s+=makeLFFormPair(args);

			}
			
			s += '<label class="profileFormLabel">&nbsp;</label>';
			s += '<div class="profileDisplayField">';
			s += '<input type="submit" class="button" value="submit">';
			s += '&nbsp;<button onClick="pnlProfile.populateUserContactAsDisplay();return false;">cancel</button>';
			s += '</div></form>';			
			
		break;		
		
		
	}
	
	return s;

}



	