function show (el) {if (e = document.getElementById(el)) {e.style.display = 'block';}}

function hide (el) {if (e = document.getElementById(el)) {e.style.display = 'none';}}

function mask (el) {if (e = document.getElementById(el)) {e.style.visibility = 'hidden';}}

function unmask (el) {if (e = document.getElementById(el)) {e.style.visibility = 'visible';}}

function hideOverlays() {
	hideProfileOverlay();
	hide('registrationOverlay');
	hide('regConfOverlay');
	hideEventOverlay();
	hide('changeLocationOverlay');
	hide('changeFilterOverlay');
	hide('loginOverlay');
	hideProfileOverlay();
	unmask('cat_select');
	setHtml('captchaContainer','');
}

function showLoginOverlay(title,subtitle) {
	hideOverlays();
	if (!title && !subtitle) {
		title = 'Oops!';
		subtitle = 'You need to log in to do that.';
	}
	setHtml('loginTitle',title);
	setHtml('loginSubtitle',subtitle);
	show('loginOverlay');
}

function showProfileOverlay(idx) {
	hideOverlays();
	// clear all fields
	var fields = ['id','revised','visited','screen_name','headline','city','state','country_id','self_desc','orientation','have_children','want_children','music','religion','politics','relationship_status','drinking','smoking','income','occupation','education','ethnicity','height','weight','body_type','gender','seeking_desc','seeking_education','seeking_ethnicity','seeking_height_from','seeking_height_to','seeking_gender','seeking_smoking','seeking_drinking','seeking_body_type','seeking_age_from','seeking_age_to','seeking_distance'];
	for (var i = 0; i < fields.length; i++) {
		if (e = document.getElementById('PROFILE:'+fields[i])) {e.innerHTML = ''};
	}
	window.AJAX.location.href = '/ajax/fetchProfileData.php?idx='+idx;
	document.getElementById('recipient_uidx').value = idx;
	var vOffset = (getScrollOffsetY()*1) + 50;
	document.getElementById('profileOverlay').style.top = vOffset+'px';
	show('profileOverlay');
}

function hideProfileOverlay() {
	hide('profileOverlay');
	hide('PROFILE:photoSection');
	setHtml('PROFILE:profileEventsContainer','');
	setHtml('PROFILE:id','');
	setHtml('PROFILE:revised','');
	setHtml('PROFILE:visited','');
	setHtml('PROFILE:screen_name','');
	setHtml('PROFILE:headline','');
	setHtml('PROFILE:city','');
	setHtml('PROFILE:state','');
	setHtml('PROFILE:country_id','');
	setHtml('PROFILE:self_desc','');
	setHtml('PROFILE:orientation','');
	setHtml('PROFILE:have_children','');
	setHtml('PROFILE:want_children','');
	setHtml('PROFILE:music','');
	setHtml('PROFILE:religion','');
	setHtml('PROFILE:politics','');
	setHtml('PROFILE:relationship_status','');
	setHtml('PROFILE:drinking','');
	setHtml('PROFILE:smoking','');
	setHtml('PROFILE:income','');
	setHtml('PROFILE:occupation','');
	setHtml('PROFILE:education','');
	setHtml('PROFILE:ethnicity','');
	setHtml('PROFILE:height','');
	setHtml('PROFILE:weight','');
	setHtml('PROFILE:body_type','');
	setHtml('PROFILE:gender','');
	setHtml('PROFILE:seeking_desc','');
	setHtml('PROFILE:seeking_education','');
	setHtml('PROFILE:seeking_ethnicity','');
	setHtml('PROFILE:seeking_height_from','');
	setHtml('PROFILE:seeking_height_to','');
	setHtml('PROFILE:seeking_gender','');
	setHtml('PROFILE:seeking_smoking','');
	setHtml('PROFILE:seeking_drinking','');
	setHtml('PROFILE:seeking_body_type','');
	setHtml('PROFILE:seeking_age_from','');
	setHtml('PROFILE:seeking_age_to','');
	setHtml('PROFILE:seeking_distance','');
	if (e = document.getElementById('rating_widget_user_idx_USER')) {e.value = '';}
	if (e = document.getElementById('widget_rating_USER')) {e.value = 0;}
	setWidget('USER',0,''); resetWidget('USER');
	setMainPhoto('/img/clearpix.gif',0,0);
	setHtml('PROFILE:profileThumbContainer','');
}

function catchProfileData(user,thumbs,photos,events) {
	var labels = ['','Yuck!','Not&nbsp;really','Not&nbsp;sure','A&nbsp;little','A&nbsp;lot!'];
	var labelIdx = user['uxuRating']; if (labelIdx < 0) {labelIdx = 0;}
	if (e = document.getElementById('rating_widget_user_idx_USER')) {e.value = user['idx'];}
	setWidget('USER',user['uxuRating'],labels[labelIdx]); resetWidget('USER');
	
	setHtml('PROFILE:revised',user['revised']);
	setHtml('PROFILE:visited',user['visited']);
	setHtml('PROFILE:screen_name',user['screen_name']);
	setHtml('PROFILE:headline',user['headline']);
	setHtml('PROFILE:nutshell',user['nutshell']);
	setHtml('PROFILE:physical_nutshell',user['physical_nutshell']);
	setHtml('PROFILE:seeking_nutshell',user['seeking_nutshell']);
	setHtml('PROFILE:city',user['city']);
	setHtml('PROFILE:state',user['state']);
	setHtml('PROFILE:country_id',user['country_id']);
	setHtml('PROFILE:self_desc',user['self_desc']);
	setHtml('PROFILE:music',user['music']);
	setHtml('PROFILE:drinking',user['drinking']);
	setHtml('PROFILE:smoking',user['smoking']);
	setHtml('PROFILE:income',user['income']);
	setHtml('PROFILE:occupation',user['occupation']);
	setHtml('PROFILE:education',user['education']);
	setHtml('PROFILE:ethnicity',user['ethnicity']);
	setHtml('PROFILE:height',user['height']);
	setHtml('PROFILE:weight',user['weight']);
	setHtml('PROFILE:body_type',user['body_type']);
	setHtml('PROFILE:seeking_desc',user['seeking_desc']);
	setHtml('PROFILE:seeking_education',user['seeking_education']);
	setHtml('PROFILE:seeking_ethnicity',user['seeking_ethnicity']);
	setHtml('PROFILE:seeking_height_from',user['seeking_height_from']);
	setHtml('PROFILE:seeking_height_to',user['seeking_height_to']);
	setHtml('PROFILE:seeking_gender',user['seeking_gender']);
	setHtml('PROFILE:seeking_smoking',user['seeking_smoking']);
	setHtml('PROFILE:seeking_drinking',user['seeking_drinking']);
	setHtml('PROFILE:seeking_body_type',user['seeking_body_type']);
	setHtml('PROFILE:seeking_age_from',user['seeking_age_from']);
	setHtml('PROFILE:seeking_age_to',user['seeking_age_to']);
	setHtml('PROFILE:seeking_distance',user['seeking_distance']);
	
	if (photos.length > 0) {
		setMainPhoto(photos[0].src,photos[0].width,photos[0].height)
		show('PROFILE:photoSection');
	} else {
		setMainPhoto('',0,0);
	}
	if (e = document.getElementById('PROFILE:profileThumbContainer')) {
		var thumbHtml = '';
		for (var i=0; i < thumbs.length; i++) {
			thumbHtml += '<img src="'+thumbs[i].src+'" height="75" width="56" class="userThumbnail" onClick="setMainPhoto(\''+photos[i].src+'\','+photos[i].width+','+photos[i].height+')"> ';
		}
		e.innerHTML = thumbHtml;
	}
	if (events.length) {
		var labels = ['','No&nbsp;way!','Doubt&nbsp;it','Maybe','Probably','Yes!'];
		var eventsHtml = '<table class="profileEventTable">';
		for (var i=0; i < events.length; i++) {
			eventsHtml += '<tr>';
			eventsHtml += '<td><img width="84" height="15" src="/img/rating-'+events[i]['rating']+'.gif" alt="'+labels[events[i]['rating']]+'" title="'+labels[events[i]['rating']]+'"></td>';
			eventsHtml += '<td>'+events[i]['date_string']+'</td>';
			eventsHtml += '<td><a href="/events/event.php?idx='+events[i]['idx']+'">'+events[i]['event_name']+'</a></td>';
			eventsHtml += '<td>'+events[i]['venue_name']+'</td>';
			eventsHtml += '</tr>';
		}
		eventsHtml += '</table>';
		if (e = document.getElementById('PROFILE:profileEventsContainer')) {e.innerHTML = eventsHtml;}
	} else {
		if (e = document.getElementById('PROFILE:profileEventsContainer')) {e.innerHTML = '</p>This user has not yet hotlisted any events.</p>';}
	}
}

function setMainPhoto(src,w,h) {
	var maxW = 200;
	var maxH = 300;
	if (e = document.getElementById('PROFILE:mainPhoto')) {
		if (src) {
			e.src = src;
			if (w > maxW) {h = Math.round(h*(maxW/w)); w = maxW;}
			if (h > maxH) {w = Math.round(w*(maxH/h)); h = maxH;}
			e.width = w;
			e.height = h;
			show('PROFILE:photoSection');
		} else {
			hide('PROFILE:photoSection');
			e.src = 'nophoto.gif';
			e.width = 0;
			e.height = 0;
		}
	}
}

function showRegistrationOverlay() {
	hideOverlays();
	document.getElementById('regFormSubmit').value='Go >';
	setHtml('email_primary_error_span','');
	setHtml('email_secondary_error_span','');
	setHtml('screen_name_error_span','');
	setHtml('email_primary_error_span','');
	setHtml('email_primary_error_span','');
	setHtml('email_secondary_error_span','');
	setHtml('passwd1_error_span','');
	setHtml('passwd2_error_span','');
	setHtml('first_name_error_span','');
	setHtml('last_name_error_span','');
	setHtml('postal_code_error_span','');
	setHtml('relationship_status_error_span','');
	setHtml('birthday_error_span','');
	setHtml('seeking_gender_error_span','');
	setHtml('seeking_age_error_span','');
	setHtml('seeking_distance_error_span','');
	mask('cat_select');
	loadCaptcha();
	show('registrationOverlay');
}

function loadCaptcha() {
	window.AJAX.location.href = '/ajax/loadCaptcha.php';
}

function catchCaptchaHtml(enccap,html) {
	document.getElementById('enccap').value = enccap;
	document.getElementById('captchaContainer').innerHTML = html;
}

function showChangeLocationOverlay() {
	hideOverlays();
	show('changeLocationOverlay');
}

function showChangeFilterOverlay() {
	hideOverlays();
	show('changeFilterOverlay');
}
function getSelectBoxValue(selectObj) {
	return selectObj.options[selectObj.selectedIndex].value;
}

function setSelectBoxValue(selectObj,value) {
	for (i=0;i<selectObj.options.length;i++) {
		if (selectObj.options[i].value == value) {
			selectObj.selectedIndex = i;
			return;
		}
	}
}

function getHashLength(assoc) {var ret=0;for (var k in assoc) {ret++;} return ret;}

function setHtml(elId,html) {if (e = document.getElementById(elId)) {e.innerHTML = html;}}

function userChooserCatcher(idx,html) {
	setHtml('userContainer'+idx,html);
}

function setCountry(c) { // only works for US & Canada
	if (c=='CA') {
		document.getElementById('zip_label').innerHTML = 'postal';
		document.getElementById('rad_label').innerHTML = 'kilometers';
		document.getElementById('state_label').innerHTML = 'province';
		document.getElementById('state_select').style.display = 'none';
		document.getElementById('province_select').style.display = 'inline';
		document.getElementById('us_label').style.fontWeight = 'normal';
		document.getElementById('ca_label').style.fontWeight = 'bold';
		document.getElementById('ca_radio_button').checked = true;
		document.getElementById('us_radio_button').checked = false;
	} else {
		document.getElementById('zip_label').innerHTML = 'zip';
		document.getElementById('rad_label').innerHTML = 'miles';
		document.getElementById('state_label').innerHTML = 'state';
		document.getElementById('state_select').style.display = 'inline';
		document.getElementById('province_select').style.display = 'none';
		document.getElementById('us_label').style.fontWeight = 'bold';
		document.getElementById('ca_label').style.fontWeight = 'normal';
		document.getElementById('ca_radio_button').checked = false;
		document.getElementById('us_radio_button').checked = true;
	}
}

function hideEventOverlay() {
	hide('eventOverlay');
	setHtml('EVENT:what','');
	setHtml('EVENT:when','');
	setHtml('EVENT:where','');
	setHtml('EVENT:info','');
}

function showEventOverlay(eidx) {
	hideOverlays();
	window.AJAX.location.href = '/ajax/fetchEventData.php?eidx='+eidx;
	var vOffset = (getScrollOffsetY()*1) + 50;
	document.getElementById('eventOverlay').style.top = vOffset+'px';
	show('eventOverlay');
}

function catchEventData(eidx,what,when,where,info) {
	setHtml('EVENT:what',what);
	setHtml('EVENT:when',when);
	setHtml('EVENT:where',where);
	setHtml('EVENT:info',info);
	if (e = document.getElementById('EVENT:printable')) {e.href = '/events/event.php?event='+eidx;}
	show('eventOverlay');
}

function getScrollOffsetX() {
	var x;
	if (self.pageYOffset) {x = self.pageXOffset;} // all except Explorer
	else if (document.documentElement && document.documentElement.scrollTop) {x = document.documentElement.scrollLeft;} // Explorer 6 Strict
	else if (document.body) {x = document.body.scrollLeft;} // all other Explorers
	return x;
}

function getScrollOffsetY() {
	var y;
	if (self.pageYOffset) {y = self.pageYOffset;} // all except Explorer
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;} // Explorer 6 Strict
	else if (document.body) {y = document.body.scrollTop;} // all other Explorers
	return y;
}
