var gender_list = new Array('Select your Gender|', 'male|m', 'female|f');
var position_list = new Array('Select a Position|', 'leader|l', 'student|s');

var loggedIn = '';

function reglog_init(){
	var loggedIn = getCookie("proMember");
	var loginDiv = document.getElementById("logReg");

	var loginDiv_title = document.createElement("Div");
		loginDiv_title.id = 'logReg_title';
		loginDiv_title.className = 'purple_dark bold general reverse clickable';
		loginDiv_title.style.padding = 3 + 'px';

	if (loggedIn == "") {
		loginDiv_title.onclick = function () {build_lrDiv(1);};
		loginDiv_title.innerHTML = 'click here to login / register!';
	} else {
		loginDiv_title.onclick = function () {member_logout();};
		loginDiv_title.innerHTML = 'click here to logout!';		
	}

	loginDiv.appendChild(loginDiv_title);
	loginDiv.style.display = 'block';
}

function member_logout(){
	removeCookie("proMember", "/", ".studylight.org");	
	alert('You have been logged out of the system!');
	window.location.href = '/';
}

function check_infoDiv(type) {
	var email = document.getElementById('lr_email');
	if (email.value == '') {
		alert('Please provide a valid email address');
		email.focus();
		return false;
	}
	
	var password = document.getElementById('lr_password1');
	if (password.value == '') {
		alert('Please provide a password');
		password.focus();
		return false;
	}

	if (type != 1) {
		var password2 = document.getElementById('lr_password2');
		if (password2.value == '') {
			alert('Please confirm your password');
			password2.focus();
			return false;
		}
		if (password.value != password2.value) {
			alert('Passwords do not match.\nPlease try again');
			password.value = '';
			password2.value = '';
			password.focus();
			return false;
		}
		var namefirst = document.getElementById('lr_namefirst');
		if (namefirst.value == '') {
			alert('Please provide a first name');
			namefirst.focus();
			return false;
		}
		var namelast = document.getElementById('lr_namelast');
		if (namelast.value == '') {
			alert('Please provide a last name');
			namelast.focus();
			return false;
		}
		var gender = document.getElementById('lr_gender');
		if (gender.options[gender.selectedIndex].value == '') {
			alert('Please select your gender');
			gender.focus();
			return false;
		}
		var position = document.getElementById('lr_position');
		if (position.options[position.selectedIndex].value == '') {
			alert('Please select the \'Bible study group\' position');
			position.focus();
			return false;
		}
		var city = document.getElementById('lr_city');
		if (city.value == '') {
			alert('Please provide the name of the city where you live');
			city.focus();
			return false;
		}
		var state = document.getElementById('lr_state');
		if (state.options[state.selectedIndex].value == '') {
			alert('Please select the state in which you live');
			state.focus();
			return false;
		}
		var country = document.getElementById('lr_country');
		if (country.options[country.selectedIndex].value == '') {
			alert('Please select the country in which you live');
			country.focus();
			return false;
		}
		var terms = document.getElementById('lr_terms');
		if (terms.checked == false) {
			alert('Please check the box if you agree with the terms!\nYou must agree in order to register!');
			terms.focus();
			return false;
		}
	}

	var date = new Date();
	var timestamp = date.getTime();
	var url = '/cgi-bin/default/log_reg.cgi?type=' + type + '&email=' + email.value  + '&password=' + password.value;
	if (type != 1) {
		url = url + '&namefirst=' + namefirst.value  + '&namelast=' + namelast.value  + '&position=' + position.options[position.selectedIndex].value + '&gender=' + gender.options[gender.selectedIndex].value + '&city=' + city.value + '&state=' + state.options[state.selectedIndex].value  + '&country=' + country.options[country.selectedIndex].value;
	}
	url = url + '&time=' + timestamp;
	parseData = function() {
		var reason = this.req.responseXML.getElementsByTagName('reason').item(0).firstChild.data;
		if (reason == 1) {
			var data = this.req.responseXML.getElementsByTagName('data').item(0).firstChild.data;
			if (type == 1) {
				alert('You have been logged into the system. Enjoy!');
				setCookie("proMember", data, "/", ".studylight.org", 90);
			} else {
				alert('You have been registered. Please check your\ninbox for the confirmation email.  Enjoy!');
			}
			window.location.href = '/';
			return false;
		} else if (reason == 2) {
			alert('Sorry but you have not confirmed your email address.\nPlease check your email box for the confirmation email.');
			cancel_lrDiv(0);
			return false;
		} else if (reason == 3) {
			alert('Sorry but your records show that this email has been banned.');
			cancel_lrDiv(0);
			return false;
		} else {
			alert('There was a problem entering your data into the system.\nPlease try again later.');
			cancel_lrDiv(0);
			return false;
		}
	}
	var http = new net.ContentLoader(url, parseData);
	return true;
}

function cancel_lrDiv(action) {
	document.body.removeChild(document.getElementById('logregDiv'));
	if (action < 1) {
		document.getElementById('logReg_title').innerHTML = 'click here to login / register!';
	}
}

function registration_checkEmail(obj,action) {
	if (obj.value == '') {
		return false;
	}
	var date = new Date();
	var timestamp = date.getTime();
	var url = '/cgi-bin/default/check_email.cgi?c=1&e=' + obj.value  + '&time=' + timestamp;
	parseData = function() {
		var reason = this.req.responseXML.getElementsByTagName('reason').item(0).firstChild.data;
		if (action == 1) {
			if (reason != 144) {
				alert('This email is not registered in the system.\nPlease click the registration link\nand register.');
				obj.value = '';
				obj.focus();
				return false;
			}
		} else {
			if (reason > 1) {
				if (reason == 2) {      
					alert('There was an error in the format of the email address. Try again!');
					obj.value = '';
				} else if (reason == 3 || reason == 4) {
					alert('The email address seems invalid. Try again!');
					obj.value = '';
				} else if (reason == 7) {
					alert('The email address is being refused for an unknown reason');
				} else if (reason == 144) {
					alert('This email address is already used by a member');
					obj.value = '';
				}
				obj.focus();
				return false;  
			}
		}
		return true;   
	}
	var http = new net.ContentLoader(url, parseData);
}

function build_lrDiv(type, ext_pos){
	_add_rowData = function(objTbl,data,extra){
		var parts = data.split('|');
		var divTR = divTbl.insertRow(objTbl.rows.length);

		var divTD = divTR.insertCell(0);
			divTD.setAttribute('width', 40 + '%');
			divTD.setAttribute('align', 'right');
			var divTD_Text = document.createTextNode(parts[0] + ': ');
				divTD.appendChild(divTD_Text);
			divTR.appendChild(divTD);
					
		var divTD = divTR.insertCell(1);
			divTD.setAttribute('width', 60 + '%');
			divTD.setAttribute('align', 'left');
			var curItem = document.createElement("Input");
				curItem.type = (parts[1].indexOf('password') > 0) ? 'password' : 'text';
				curItem.name = parts[1];
				curItem.id = parts[1];
				curItem.size = parts[2];
				curItem.className = 'smaller';
				if (extra > 0) {
					curItem.onchange = function () {registration_checkEmail(this,extra);};
				}
				divTD.appendChild(curItem);
			divTR.appendChild(divTD);
		objTbl.appendChild(divTR);
	}

	_add_selectType = function(objTbl, txt, id_txt, opt_array){
		_addOption = function(selectbox,text,value ) {
			if (ext_pos != 1 && text == 'leader') {
				return ;
			}
			var optn = document.createElement("OPTION");
				optn.text = text;
				optn.value = value;
			selectbox.options.add(optn);
		}

		var divTR = divTbl.insertRow(objTbl.rows.length);

			var divTD = divTR.insertCell(0);
                	        divTD.setAttribute('width', 40 + '%');
                	        divTD.setAttribute('align', 'right');
                	        var divTD_Text = document.createTextNode(txt + ': ');
                                divTD.appendChild(divTD_Text);
                        divTR.appendChild(divTD);

			var divTD = divTR.insertCell(1);
                	        divTD.setAttribute('width', 60 + '%');
                	        divTD.setAttribute('align', 'left');
                	        var curItem = document.createElement("Select");
                	                curItem.name = txt;
                	                curItem.id = 'lr_' + id_txt;
                	                curItem.className = 'smaller';
               	                divTD.appendChild(curItem);

			for(var i = 0; i < opt_array.length; i++) {
				var parts = opt_array[i].split('|');
				_addOption(curItem, parts[0], parts[1]);
			}

			divTR.appendChild(divTD);
                objTbl.appendChild(divTR);

	}
	
	_add_linedRow = function(objTbl){
		var divTR = divTbl.insertRow(objTbl.rows.length);
		var divTD = divTR.insertCell(0);
			divTD.innerHTML = '<hr width="100%" size="1" noshade>';
		divTR.appendChild(divTD);
		var divTD = divTR.insertCell(1);
			divTD.innerHTML = '<hr width="100%" size="1" noshade>';
		divTR.appendChild(divTD);
		objTbl.appendChild(divTR);
	}

	_get_stateCountry = function(objTbl, type) {
		var dataurl = '/cgi-bin/default/data.cgi?p=lr_';
		var type_text = (type == 1) ? 'state: ' : 'country: ';

		var divTR = divTbl.insertRow(objTbl.rows.length);
		var divTD = divTR.insertCell(0);
			divTD.setAttribute('width', 40 + '%');
			divTD.setAttribute('align', 'right');
			var divTD_Text = document.createTextNode(type_text);
				divTD.appendChild(divTD_Text);
			divTR.appendChild(divTD);

		var divTD = divTR.insertCell(1);
			divTD.setAttribute('width', 60 + '%');
			divTD.setAttribute('align', 'left');

		on_load = function() {
			var xmlDocument = this.req.responseXML;
			divTD.innerHTML = xmlDocument.getElementsByTagName('formdata').item(0).firstChild.data;
		}
		var http = new net.ContentLoader(dataurl + '&t=' + type, on_load);
		divTR.appendChild(divTD);
		objTbl.appendChild(divTR);
	}

	if (document.getElementById('logregDiv') != null){
		cancel_lrDiv(0);
		if (type < 1) {
			return false;
		}
	}
	
	var lrDiv_width = 292;
	var lrDiv_height = (type == 1) ? 130 : (ie) ? 390 : 415;
		
	document.getElementById('logReg_title').innerHTML = (type == 1) ? 'login' : 'register';
	var divTL = getObjectPosition('logReg');
	var lrDiv = document.createElement("Div");
		lrDiv.style.position = 'absolute';
		lrDiv.style.top = ((divTL.y + divTL.z) - 1) + 'px';
		lrDiv.style.left = divTL.x + 'px';
		lrDiv.style.height = lrDiv_height + 'px';
		lrDiv.style.width = lrDiv_width + 'px';
		lrDiv.style.className = 'medium bold';
		lrDiv.style.padding = 5 + 'px';
		lrDiv.id='logregDiv';
		lrDiv.style.border = '1px solid #B5B3CC';
		lrDiv.style.backgroundColor = '#E7E3EF';
	
	var lrDiv_mainDiv = document.createElement("Div");
		lrDiv_mainDiv.style.height = (lrDiv_height - 4) + 'px';
		lrDiv_mainDiv.style.width = (lrDiv_width - 4) + 'px';
		lrDiv_mainDiv.style.border = '1px solid #B5B3CC';
		lrDiv_mainDiv.style.backgroundColor = '#FFFFFF';

	if (type != 1) {
		var mainDiv_Div = document.createElement("Div");
			mainDiv_Div.style.fontSize = 12 + 'px';
			mainDiv_Div.style.margin = '6px 10px 6px';
			var mainDiv_Text = document.createTextNode('The studylight.org tools are completely free.  However, you must register and log in to gain access to the enhanced features available.');
				mainDiv_Div.appendChild(mainDiv_Text);
			lrDiv_mainDiv.appendChild(mainDiv_Div);
	}
		
	var divTb = document.createElement("Table");
                divTb.className = 'medium';
                divTb.setAttribute('width', 100 + '%');
                divTb.setAttribute('cellpadding', 2);
                divTb.setAttribute('cellspacing', 4);
                divTb.setAttribute('border', 0);
	var divTbl = document.createElement("tbody");
	divTb.appendChild(divTbl);

	_add_rowData(divTbl,'email address|lr_email|32|',type);
	_add_rowData(divTbl,'password|lr_password1|15',0);

	if (type != 1) {
		_add_rowData(divTbl,'confirm password|lr_password2|15',0);
		_add_linedRow(divTbl);			
		_add_selectType(divTbl, 'bible study group', 'position', position_list);
		_add_rowData(divTbl,'first name|lr_namefirst|15',0);
		_add_rowData(divTbl,'last name|lr_namelast|20',0);
		_add_selectType(divTbl, 'gender', 'gender', gender_list);
		_add_rowData(divTbl,'city|lr_city|30',0);
		_get_stateCountry(divTbl, 1);
		_get_stateCountry(divTbl, 2);
		
		var divTR = divTbl.insertRow(divTbl.rows.length);

		var divTD = divTR.insertCell(0);
			divTD.colSpan = 2;
			divTD.setAttribute('align', 'center');
			divTD.style.fontSize = 10 + 'px';
			divTD.innerHTML = '<input type="checkbox" name="lr_terms" id="lr_terms"> I agree with the <a href="/info/termsofuse.html" target="_blank">Terms Of Use</a> outlined on this site.';
			divTR.appendChild(divTD);
		divTbl.appendChild(divTR);
	}

	lrDiv_mainDiv.appendChild(divTb);
		
	var btnDiv = document.createElement("Div");
		btnDiv.align = 'center';
		btnDiv.style.paddingTop = 10 + 'px';
		btnDiv.style.paddingBottom = 10 + 'px';
			
	var btn = document.createElement("Button");
		btn.className = 'general purple_dark reverse';
		btn.id = 'submit_button';
		btn.onclick = function () {check_infoDiv(type);};
		btn.innerHTML = (type == 1) ? 'login!' : 'register!';
		btnDiv.appendChild(btn);
	lrDiv_mainDiv.appendChild(btnDiv);

	var txtSpacer = document.createElement("Span");
		txtSpacer.innerHTML = '&nbsp; &nbsp;';
	btnDiv.appendChild(txtSpacer);

	var btn = document.createElement("Button");
		btn.className = 'general purple_dark reverse';
		btn.id = 'cancel_button';
		btn.onclick = function () {cancel_lrDiv(0);};
		btn.innerHTML = 'cancel';
		btnDiv.appendChild(btn);
	lrDiv_mainDiv.appendChild(btnDiv);
		
	if (type == 1) {
		var regDiv = document.createElement("Div");
			regDiv.align = 'center';
			regDiv.className = 'smaller';
			regDiv.style.paddingBottom = 10 + 'px';
			regDiv.innerHTML = 'not registered?  <a href="javascript:void(\'null\');" onClick="build_lrDiv(2);">click here!</a>';
		lrDiv_mainDiv.appendChild(regDiv);
	}

	lrDiv.appendChild(lrDiv_mainDiv);
	document.body.appendChild(lrDiv);
	document.getElementById('lr_email').focus();
}
