function downloadFile(filename) {
	var download_win = window.open(base_href+'download/?f='+filename,'download','width=600,height=400,top=0,left=0,resizable,scrollbars');
	download_win.focus();
}
function openPage(file_path) {
	var new_page_win = window.open(file_path,'new_page','width=600,height=400,top=0,left=0,resizable=yes,scrollbars=yes');
	new_page_win.focus();
}

function popterms() {
	openPage(base_href+'terms.html');
}

var tourl = '';
function askLeave(theurl) {
	tourl=theurl;
	document.getElementById('leavesite').style.display='block';
}

function killleavealert() {
	document.getElementById('leavesite').style.display='none';
}

function goToURL() {
	document.location=tourl;
}

function validate_form(form_id,val_type) {
	var valid_form = new FormValidator(form_id,val_type);
	if (valid_form.validate()) {
		document.getElementById(form_id).submit();
	} else {
		valid_form.report_errors();
	}
}

function email_manager() {
	if ($('register_email_results').checked) {
		cssSetDisplay('register_manager','block');
	} else {
		cssSetDisplay('register_manager','none');
		$('register_manager_email').value = '';
	}
}

var curr_toggle = '';
var curr_toggle_status = 'closed';
function toggle(id) {
	if (curr_toggle != id) {
		if (curr_toggle != '') {
			cssSetDisplay(curr_toggle,'none');
			$(curr_toggle+'_link').className='';
		}
		
		cssSetDisplay(id,'block');
		$(id+'_link').className='link_on';
		curr_toggle_status = 'open';
	} else if (curr_toggle_status == 'open') {
		cssSetDisplay(id,'none');
		$(id+'_link').className='';
		curr_toggle_status = 'closed'
	} else if (curr_toggle_status == 'closed') {
		cssSetDisplay(id,'block');
		$(id+'_link').className='link_on';
		curr_toggle_status = 'open'
	}
	curr_toggle = id;
}

function login(type) {
	var valid_form = new FormValidator('login_form','form');
	if (valid_form.validate()) {
		if (type == 'form') {
			return true;
		} else {
			document.getElementById('login_form').submit();
		}
	} else {
		valid_form.report_errors();
		if (type == 'form') {
			return false;
		}
	}
}

function register() {
	var valid_form = new FormValidator('register_form','form');

	if (valid_form.validate()) {
		if (document.getElementById('register_email_results').checked == true) {
			
			var valid_form2 = new FormValidator('register_form','register_manager_email');
			valid_form2.initials = 'j2_';
	
			if (valid_form2.validate()) {
				document.getElementById('register_form').submit();
			} else {
				valid_form2.report_errors();
			}

		} else {
			document.getElementById('register_form').submit();
		}
	
	} else {
		valid_form.report_errors();
	}
}

function get_password(type) {
	var valid_form = new FormValidator('password_form','form');
	if (valid_form.validate()) {

		var password_email = document.getElementById('password_email').value;
		var params = '&password_email=' + encodeURIComponent(password_email);
		
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'get_password.php';
		ajax.send_method = 'GET';
		ajax.response_handler = function(resp) {
			document.getElementById('get_password_resp').innerHTML = resp;
			document.getElementById('password_email').value='';
		}
		ajax.sendRequest();
	} else {
		valid_form.report_errors();
	}
}

function email_manager_results() {
	var has_checked = false;
	var f = document.getElementById('training_activity_form');
	var params = '&doit=y';
	for (var i=0;i<f.length;i++) {
		el_type = f.elements[i].type;
		el_name = f.elements[i].name;
		if (el_type == 'checkbox' && f.elements[i].checked) {
			has_checked = true;
			params += '&' + el_name + '=' + encodeURIComponent(f.elements[i].value);
			f.elements[i].checked = false;
		}
	}
	if (has_checked) {
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'email_training_results.php';
		ajax.send_method = 'POST';
		ajax.response_handler = function(resp) {
			document.getElementById('training_results_response').innerHTML = resp;
		}
		ajax.sendRequest();
			
	} else {
		alert('No module is checked.');
	}
	
}

function send_reminder(group) {
	var has_checked = false;
	var f = document.getElementById('group_form');
	var params = '&doit=y&TrainingGroupID='+group;
	for (var i=0;i<f.length;i++) {
		el_type = f.elements[i].type;
		el_name = f.elements[i].name;
		if (el_type == 'checkbox' && f.elements[i].checked) {
			has_checked = true;
			params += '&' + el_name + '=' + encodeURIComponent(f.elements[i].value);
			f.elements[i].checked = false;
		}
	}
	if (has_checked) {
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'email_reminder.php';
		ajax.send_method = 'POST';
		ajax.response_handler = function(resp) {
			document.getElementById('reminder_response').innerHTML = resp;
		}
		ajax.sendRequest();
			
	} else {
		alert('No reminders are checked.');
	}
	
}

function getAllQTries(acct) {
	var params = '&ContactID=' + encodeURIComponent(acct);
	
	var ajax = new Ajax();
	ajax.send_params = params;
	ajax.send_url = ajax_path + 'allquiztries.php';
	ajax.send_method = 'GET';
	ajax.response_handler = function(resp) {
		document.getElementById('allquiztries').innerHTML = resp;
	}
	ajax.sendRequest();
}

function enlarge(filename,width,height) {
	if (document.getElementById('enlarge_div')) {
		remove_id('enlarge_div');
	}
	
	var enlarge_div = document.createElement('div');
	enlarge_div.setAttribute('id','enlarge_div');		
	enlarge_div.setAttribute('width',width+'px');		
	enlarge_div.setAttribute('height',height+'px');		
	
	var img = document.createElement("img");
	img.setAttribute("src","http://mms.flexco.com/images/"+filename);
	var insert_img = enlarge_div.appendChild(img);
	
	var link = document.createElement("a");
	link.setAttribute("href","javascript:remove_id('enlarge_div');");
	var insert_link = enlarge_div.appendChild(link);
	var link_text = document.createTextNode('close');
	var insert_link_text = insert_link.appendChild(link_text);
	
	document.body.appendChild(enlarge_div);
}

function remove_id(id) {
	var remove_item = document.getElementById(id);
	document.body.removeChild(remove_item);
}
// ================== account manager ================== 
function adjust_account(contactid,cmd) {
	switch (cmd) {
	case 'update' :
		var f = document.getElementById('account_form');
		var manager_email_check = true;
		
		if (f.account_email_results.checked == true) {
			 if (f.account_manager_email.value == '') { manager_email_check = false; }
		}
		
		
		if (manager_email_check) {
			var valid_form = new FormValidator('account_form','form');
			if (valid_form.validate()) {
				var params = 'form_display=static&cmd=update';
				if (contactid > 0) { params+='&ContactID='+contactid; }
				
				
				for (var i=0;i<f.length;i++) {
					if (f[i].name == 'account_email_results' || f[i].name == 'account_create_groups' || f[i].name == 'account_root_admin') { // checkboxes
						params += (f[i].checked == true) ? '&' + f[i].name + '=y' : '&' + f[i].name + '=n';
					} else {
						params += '&' + f[i].name + '=' + encodeURIComponent(f[i].value);
					}
				}
				var ajax = new Ajax();
				ajax.send_params = params;
				ajax.send_url = ajax_path + 'manage_account_form.php';
				ajax.send_method = 'GET';
				ajax.response_handler = function(resp) {
					document.getElementById('account_info').innerHTML = resp;
				}
				ajax.sendRequest();
				
				document.getElementById('account_info_button').innerHTML = '<a href="javascript:adjust_account('+contactid+',\'edit\');"><span>Edit</span></a>';
				
			} else {
				valid_form.report_errors();
			}
		} else {
			alert("Please enter a manager's email address.");
		}
		break;
	case 'edit' :
		var params = 'form_display=dynamic';
		if (contactid > 0) { params+='&ContactID='+contactid; }
	
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'manage_account_form.php';
		ajax.send_method = 'GET';
		ajax.response_handler = function(resp) {			
			//clear current html
			var account_info = document.getElementById('account_info');
			account_info.innerHTML = '';
			
			//create form
			var account_form = document.createElement('form');
			account_form.setAttribute('id','account_form');
			account_info.appendChild(account_form);
			document.getElementById('account_form').className = 'form_a';
			
			// add form elements
			account_form.innerHTML = resp;
		}
		ajax.sendRequest();
		
		document.getElementById('account_info_button').innerHTML = '<a href="javascript:adjust_account('+contactid+',\'update\');"><span>Update</span></a>';
		
		break;
	}
}


function send_helpful() {
	var valid_form = new FormValidator('poll_1_form','form');
	if (valid_form.validate()) {
		var params = 'poll_1_choice=';
		if (document.getElementById('poll_1_choice_1').checked) { params+='1'; }
		else if (document.getElementById('poll_1_choice_2').checked) { params+='2'; }
		else if (document.getElementById('poll_1_choice_3').checked) { params+='3'; }
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'site_helpful_vote.php';
		ajax.send_method = 'GET';
		ajax.response_handler = function(resp) {
			document.getElementById('SiteHelpful').innerHTML = resp;
		}
		ajax.sendRequest();
	} else {
		valid_form.report_errors();
	}
}

function send_comments() {
	var valid_form = new FormValidator('comments_form','form');
	if (valid_form.validate()) {
		var params = 'comments=' + encodeURIComponent(document.getElementById('site_comments').value);
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'send_comments.php';
		ajax.send_method = 'GET';
		ajax.response_handler = function(resp) {
			document.getElementById('comments_message').innerHTML = resp;
			document.getElementById('site_comments').value = '';
		}
		ajax.sendRequest();
	} else {
		valid_form.report_errors();
	}
}
// ================== account manager ================== 





// ================== module training pages ================== 
var jumpto_page_num = 0;
function jumpto_training_page(select_id,user_type) {
	jumpto_page_num = document.getElementById(select_id).value;
	goto_training_page('jumpto',user_type);
	if (user_type == 'admin') { sync_pages_lists(); }
}


function goto_training_page(page_type,user_type) { //page_type = prev, next or jumpto
	//TrainingModuleID
	//TrainingPageNum
	//TrainingTotalPages

	switch (page_type) {
	case 'prev' :
		cssSetDisplay('quiz_body_next_pg','block');
		cssSetDisplay('quiz_menu_next_pg','block');
		if (TrainingPageNum - 1 == 1) {
			cssSetDisplay('quiz_body_prev_pg','none');
			cssSetDisplay('quiz_menu_prev_pg','none');
		}
		TrainingPageNum--;
		break;
	case 'next' :
		cssSetDisplay('quiz_body_prev_pg','block');
		cssSetDisplay('quiz_menu_prev_pg','block');
		if (TrainingPageNum + 1 == TrainingTotalPages) {
			cssSetDisplay('quiz_body_next_pg','none');
			cssSetDisplay('quiz_menu_next_pg','none');
		}
		TrainingPageNum++;
		break;
	case 'jumpto' :
		TrainingPageNum = jumpto_page_num;
		if (TrainingPageNum == TrainingTotalPages) {
			cssSetDisplay('quiz_menu_next_pg','none');
			cssSetDisplay('quiz_body_next_pg','none');
		} else {
			cssSetDisplay('quiz_menu_next_pg','block');
			cssSetDisplay('quiz_body_next_pg','block');
		}
		if (TrainingPageNum == 1) {
			cssSetDisplay('quiz_menu_prev_pg','none');
			cssSetDisplay('quiz_body_prev_pg','none');
		} else {
			cssSetDisplay('quiz_menu_prev_pg','block');
			cssSetDisplay('quiz_body_prev_pg','block');
		}
		break;
	}
	if (user_type == 'admin') { sync_pages_lists(); }
	load_training_page();
	if (TrainingPageNum == TrainingTotalPages && TrainingHasQuiz == 'y') {
		cssSetDisplay('take_quiz_button','block');
	} else {
		cssSetDisplay('take_quiz_button','none');
	}
	
	if (user_type != 'admin') {
		$('curr_page_1').innerHTML = TrainingPageNum + ' of ' + TrainingTotalPages;
		$('curr_page_2').innerHTML = TrainingPageNum + ' of ' + TrainingTotalPages;
	}
}

function sync_pages_lists() {
	var pages_lists_ids = new Array('quiz_menu_pages','quiz_body_pages');
	for (var i=0;i<pages_lists_ids.length;i++) {
		var select_ref = document.getElementById(pages_lists_ids[i]);
		for (var j=0;j<select_ref.length;j++) {
			if (select_ref[j].value == TrainingPageNum) {
				select_ref[j].selected = true;
			}
		}
	}
}

function load_training_page(type,ext) {
	var params = 'TrainingModuleID=' + TrainingModuleID + '&TrainingPageNum=' + TrainingPageNum;
	if (type != '') { params+='&EType='+encodeURIComponent(type); }
	if (ext != '') { params+='&Ext='+encodeURIComponent(ext); }
		
	var ajax = new Ajax();
	ajax.send_params = params;
	ajax.send_url = ajax_path + 'load_training_page.php';
	ajax.send_method = 'GET';
	ajax.response_handler = function(resp) {
		document.getElementById('training_page_content').innerHTML = resp;
	}
	ajax.sendRequest();
}

function picselector(module,pageno) {
	var psw=window.open('photo_selector.php?ModuleID='+module+'&PageID='+pageno,'Photos','scrollbars=yes,width=350,height=350,resize=yes');
}

function fileselector(module,pageno) {
	var psw=window.open('file_selector.php?ModuleID='+module+'&PageID='+pageno,'Files','scrollbars=yes,width=450,height=350,resize=yes');
}

function getAllLogins(contactid) {
	var params = 'ContactID=' + contactid;		
	var ajax = new Ajax();
	ajax.send_params = params;
	ajax.send_url = ajax_path + 'contact_all_logins.php';
	ajax.send_method = 'GET';
	ajax.response_handler = function(resp) {
		document.getElementById('all_logins_div').innerHTML = resp;
	}
	ajax.sendRequest();
}

function killpassalert() {
	document.getElementById('alreadypass').style.display='none';
}

function take_quiz() {
	TrainingPageNum = 999;
	cssSetDisplay('train_menu_prev_next','none');
	cssSetDisplay('train_body_prev_next','none');
	cssSetDisplay('take_quiz_button','none');
	cssSetDisplay('train_online_button','block');
	load_training_page();
}

function submit_quiz(page_id) {
	var answer_value,el_type,el_name;
	var params = 'TrainingPageID=' + page_id;
	var valid_form = new FormValidator('train_quiz_form','form');
	if (valid_form.validate()) {
		var f=document.getElementById('train_quiz_form');
		for (var i=0;i<f.length;i++) {
			el_type = f.elements[i].type;
			el_name = f.elements[i].name;
			
			if (el_type == 'radio') {
				for (j=0;j<f[el_name].length;j++) {
					if (f[el_name][j].checked) { 
						answer_value = f[el_name][j].value;
					}
				}
			} else {
				answer_value = f.elements[i].value;
			}
			
			params += '&' + f.elements[i].name + '=' + encodeURIComponent(answer_value);
		}
		
		var ajax = new Ajax();
		ajax.send_params = params;
		ajax.send_url = ajax_path + 'grade_quiz.php';
		ajax.send_method = 'POST';
		ajax.response_handler = function(resp) {
			document.getElementById('training_page_content').innerHTML = resp;
		}
		ajax.sendRequest();

	} else {
		alert('Please answer all the questions.');
	}
	
}

function newQuizQuestion() {
	f=document.TrainingForm;
	var qc=parseInt(f.NumQuestions.value)+1; var newqq='';
	newqq+='<input type="text" name="QP'+qc+'" size="3" value="'+qc+'"><input type="text" name="Question'+qc+'" size="50" value="">';
	newqq+='<input type="hidden" name="QID'+qc+'" value="0">';
	newqq+='<input type="hidden" name="NumOptions'+qc+'" value="2">';
	newqq+='<div id="qq'+qc+'o1"><input type="radio" name="Q'+qc+'C" value="1" checked>';
	newqq+='<input type="text" name="Q'+qc+'A1T" size="20" value="True"> ';
	newqq+='Priority:<input type="text" name="Q'+qc+'A1P" size="3" value="1"> ';
	newqq+='Points:<input type="text" name="Q'+qc+'A1S" size="3" value="50"> ';
	newqq+="</div>\n";
	newqq+='<div id="qq'+qc+'o2"><input type="radio" name="Q'+qc+'C" value="2">';
	newqq+='<input type="text" name="Q'+qc+'A2T" size="20" value="False"> ';
	newqq+='Priority:<input type="text" name="Q'+qc+'A2P" size="3" value="2"> ';
	newqq+='Points:<input type="text" name="Q'+qc+'A2S" size="3" value="0"> ';
	newqq+="</div>\n";
	newqq+='<div id="NewQO'+qc+'"><a href="javascript:newQuizOption('+qc+')">+New Option</a></div>';
	var ne = document.createElement('DIV');
	ne.innerHTML=newqq;
	ne.id='qq'+qc;
	document.getElementById('quizdiv').appendChild(ne);
	f.NumQuestions.value=''+qc;
}

function newQuizOption(qq) {
	f=document.TrainingForm;
	var qo=parseInt(eval('f.NumOptions'+qq+'.value'))+1; var newqq='';
	newqq+='<input type="hidden" name="Q'+qq+'A'+qo+'ID" value="0">';
	newqq+='<input type="radio" name="Q'+qq+'C" value="'+qo+'">';
	newqq+='<input type="text" name="Q'+qq+'A'+qo+'T" size="20" value=""> ';
	newqq+='Priority:<input type="text" name="Q'+qq+'A'+qo+'P" size="3" value="'+qo+'"> ';
	newqq+='Points:<input type="text" name="Q'+qq+'A'+qo+'S" size="3" value="0"> ';
	var ne = document.createElement('DIV');
	ne.innerHTML=newqq;
	ne.id='qq'+qq+'0'+qo;
	var qqd=document.getElementById('qq'+qq);
	qqd.insertBefore(ne,qqd.lastChild);
	eval('f.NumOptions'+qq+'.value=\''+qo+'\'');
}
// ================== module training pages ================== 



function showinactive() {
	document.getElementById('nonactive').style.display='block';
	document.getElementById('shownactive').innerHTML='<a href="javascript:hideinactive();">Hide Inactive Modules</a>';
}
function hideinactive() {
	document.getElementById('nonactive').style.display='none';
	document.getElementById('shownactive').innerHTML='<a href="javascript:showinactive();">Show Inactive Modules</a>';
}

function newModuleCategory() {
	var nmc='New Module Category<br>Name:<input type="text" name="NewCatName" size="15" value=""><br>Priority:<input type="text" name="NewCatPri" size="4" value="0"><br>Pic<input type="file" id="newmodcatpic" name="newmodcatpic"><br><br>';
	document.getElementById('newmodcat').innerHTML=nmc;
}

function editModuleCategory(id,name,pri) {
	var nmc='Edit Module Category<br><input type="hidden" name="ModCatID" value="'+id+'">Name:<input type="text" name="NewCatName" size="15" value="'+name+'"><br>Priority:<input type="text" name="NewCatPri" size="4" value="'+pri+'"><br>Pic<input type="file" id="newmodcatpic" name="newmodcatpic"><br><input type="checkbox" name="DelModCat" value="Y"> - Delete This Category<br><br>';
	document.getElementById('newmodcat').innerHTML=nmc;
}










