// Menus: (for IE)
function displayMenus () {
	if(!document.body.currentStyle) return;
	var subs = document.getElementsByName('submenu');
	for(var i=0; i<subs.length; i++) {
		var li = subs[i].parentNode;
		if(li && li.lastChild.style) {
			li.onmouseover = function() {
				this.lastChild.style.visibility = 'visible';
			}
			li.onmouseout = function() {
				this.lastChild.style.visibility = 'hidden';
			}
		}
	}
}

function editRow(row,field_types) {
	
	confirmation = true;
			
	var x=0;
	for(field in row) {
		cell_id = field+"_"+row['id'];
//		alert(cell_id);
		cell = document.getElementById(cell_id);
		
//		alert(field+": "+field_types[x]);
		input_field = "";
		if(field_types[x]=="hidden") input_field = cell.innerHTML;
		if(field_types[x]!="select") input_field += "<input class='"+field+"' name='"+field+"[]' type='"+field_types[x]+"' value='"+row[field]+"'>";
		else input_field = "select";
		cell.innerHTML = input_field;
		
		x++;
	}
	
}

function addRow(span_id,table_class,fields,field_types) {
	
	confirmation = true;
	var span = document.getElementById(span_id);
	
	html = "<table class='"+table_class+"'>";
	html += "<tr>";
	
	for(x=0; x<fields.length ;x++) {
		html += "<td class='"+fields[x]+"'><input class='"+fields[x]+"' name='"+fields[x]+"[]' type='"+field_types[x]+"'></td>";
	}
	html += "</tr>";
	html += "</table>";
	
	span.innerHTML = html;
}

function protectPage() {
	window.onbeforeunload = function () {
		if(confirmation) return "Any unsaved changes will be discarded.";
		else confirmation = true;		
	}	
}

function loadUser() {
		
}

function loadLoginForm(current_page) {
	
	var sign_in = document.getElementById("sign_in");
	var username_password = document.getElementById("username_password");
	
	sign_in.href = "#";
	sign_in.onclick = function () { toggle("sign_in"); toggle("username_password"); document.sign_in_form.username.focus(); }
	
	try {
		username_password.style.display = "none";
		html = "<form name='sign_in_form' action='"+site_root+"/user/sign_in.php' method='post'>";
		html += "<a href='#' onclick='toggle(\"username_password\"); toggle(\"sign_in\");'>close<img src='"+image_folder+"/cross.png' alt='' /></a>";
		html += "<a href='"+site_root+"/user/register.php'>Register<img src='"+image_folder+"/user_add.png' alt='' /></a>";
		html += "<input name='username' type='text' size='9' value=''";
//		html += " onfocus='change_default(this);' onblur='reset_default(this,\"username\");'";
		html += ">";
		html += "<input name='password' type='password' size='9' value=''";
//		html += " onfocus='change_default(this);' onblur='reset_default(this,\"password\");";
		html += "'>";
		html += "<input name='url' type='hidden' value='"+current_page+"' />";
		html += "<input name='sign_in' type='submit' value='GO' />";
		html += "</form>";
		username_password.innerHTML = html;
	} catch(e) {
		sign_in.href = site_root+"/user/sign_in.php";
		sign_in.onclick = "";
	}
}

function viewPicture(width, height, picture, title, document_title) {
	
	var top = Math.floor((screen.height - height)/2);
	var left = Math.floor((screen.width - width)/2);
	
	var attr = "top="+top+",left="+left+",toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,width="+(width+20)+",height="+(height+50);
	popup = window.open("","",attr);
	popup_html = "<head><title>"+document_title+"</title><link href='../css/styles.css' rel='stylesheet' type='text/css' /></head><body id='view_picture'><a href='javascript:self.close();'><img src=\""+picture+"\" title='click to close' alt='"+title+"' border=0 /></a><h1>"+title+"</h1></body>";
	
	popup.document.open();
	popup.document.write(popup_html);
	popup.document.close();
	popup.focus();

}
	
/* Site-wide functions */
function toggleSubmitButtons(form) {
	
	var submitButtons = form.getElementsByTagName("input");
	
	for(x=0; x<submitButtons.length ;x++) {
		if(submitButtons[x].type == "submit") {
			if(submitButtons[x].style.visibility=="hidden") submitButtons[x].style.visibility = "visible";
			else submitButtons[x].style.visibility = "hidden";
//			// Changing the class doesn't actually prevent users from clicking buttons multiple times
//			if(submitButtons[x].className=="button") submitButtons[x].className = "button_disabled";
//			else submitButtons[x].className = "button";
//			// Disabling the buttons makes them lose their value - problem when distinguishing between multiple submit buttons
//			if(submitButtons[x].disabled==true) submitButtons[x].disabled = false;
//				else submitButtons[x].disabled = true;
		}
	}
	
}
function toggle(id) {
	var obj = document.getElementById(id);
	obj.style.display = obj.style.display == 'none' ? 'inline' : 'none';
}

/* Confirmations */
function confirmReload() {
	confirmation=false;
	var agree = confirm('Are you sure you want to reset all changes?');
	if(agree) window.location.reload(false);
	else return false;
}
function confirmReset() {
	return confirm('Are you sure you want to reset all changes?');
}
function confirmResetPassword(username) {
	return confirm("Are you sure you want to reset the password for '"+username+"'?");
}
function confirmDelete(name) {
	return confirm('Are you sure you want to delete "'+name+'"?');
}
function confirmSearch(form){
	var form_name = form.name;
	var valid = true;
	var first_field;
	var message = "Please fill in the following field(s):\n\n";
	
	// Test fields
	if(form.query.value=="" && form.user.value=="" && form.submitted_from.value=="" && form.submitted_to.value=="" && form.modified_from.value=="" && form.modified_to.value=="") {
		message += "-at least one text field (search term, user, submitted from, submitted to, modified from, or modified to)\n";
		if(!first_field) first_field = form.query;
		valid = false;	
	}
	if(!form.title_field.checked && !form.description_field.checked) {
		message += "-title or description\n";
		if(!first_field) first_field = form.title_field;
		valid = false;
	}
		
	// Check validity
	if(!valid) {
		alert(message);
		first_field.focus();
		return false;
	} else return true;
}
function confirmSubmitRecipe(form) {
	var valid = true;
	var first_field = "";
	var message = "Please fill in the following field(s):\n\n";
	
	// Test fields
	if(form.title.value=="") {
		message += "-title\n";
		if(first_field=="") first_field = form.title;
		valid = false;	
	}
	if(form.directions.value=="") {
		message += "-directions\n";
		if(first_field=="") first_field = form.directions;
		valid = false;
	}
	
	// Check validity
	if(!valid) {
		alert(message);
		first_field.focus();
		confirmation = true;
		toggleSubmitButtons(form);
		return false;
	} else return true;	
}
function confirmProfile(form) {
	var form_name = form.name;
	var valid = true;
	var first_field;
	var message = "Please fill in the following field(s):\n\n";
	
	// Test fields
	if(form.username.value=="") {
		message += "-username\n";
		if(!first_field) first_field = form.username;
		valid = false;	
	}
	if(form_name=='edit_profile') {
		if(form.old_password.value=="") {
			message += "-password\n";
			if(!first_field) first_field = form.old_password;
			valid = false;
		}
	} else {
		if(form.new_password.value=="") {
			message += "-password\n";
			if(!first_field) first_field = form.new_password;
			valid = false;
		}
	}
	if(form.email.value=="") {
		message += "-email address\n";
		if(!first_field) first_field = form.email;
		valid = false;
	}
	
	if(valid) {
		if(form.new_password.value!=form.confirm_password.value) {
			message = "The passwords do not match.";
			first_field = form.new_password;
			valid = false;
		} else if(form.email.value.indexOf("@")==-1) {
			message = "Please enter a valid email address.";
			first_field = form.email;
			valid = false;
		}
	}
	
	// Check validity
	if(!valid) {
		alert(message);
		first_field.focus();
		return false;
	} else return true;
}
function filterJump(page,var_name,selObj) {
	if(page.indexOf("?")==-1) page += "?";
	else page += "&";
  eval("parent.location='"+page+var_name+"="+selObj.options[selObj.selectedIndex].value+"'");
}
function selectAll(id) {
	var element = document.getElementById(id);
	element.focus();
	element.select();
}
function htmlSpecialChars(value) {
	value = value.replace(/\"/g,"&quot;");
	return value;
}
function jsSpecialChars(value) {
	value = value.replace(/&quot;/g,"&rdquo;");
	value = value.replace(/\"/g,"&rdquo;");
	value = value.replace(/\'/g,"&rsquo;");	
	return value;
}

/* Dreamweaver functions */
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}