var limit = 500;
var long_limit = 1800;

function check() {
	if(document.f.comments.value.length > limit) {
		alert('Too much data!');
		document.f.comments.focus();
		return false; 
	} else
	return true; 
}
 
function update_counter() {
	var old = document.f.counter.value;
	document.f.counter.value=document.f.comments.value.length;
	if(document.f.counter.value > limit && old <= limit) {
		alert('Too much data in the text box!');
		if(document.styleSheets) {
		 	document.f.counter.style.fontWeight = 'bold';
		   	document.f.counter.style.color = '#ff0000'; 
		}
	} else if (document.f.counter.value <= limit && old > limit && document.styleSheets ) {
	   document.f.counter.style.fontWeight = 'normal';
	   document.f.counter.style.color = '#000000'; 
	} 
}

 
function update_counter_long() {
	var old = document.f.counter.value;
	document.f.counter.value=document.f.comments.value.length;
	if(document.f.counter.value > long_limit && old <= long_limit) {
		alert('Too much data in the text box!');
		if(document.styleSheets) {
		 	document.f.counter.style.fontWeight = 'bold';
		   	document.f.counter.style.color = '#ff0000'; 
		}
	} else if (document.f.counter.value <= long_limit && old > long_limit && document.styleSheets ) {
	   document.f.counter.style.fontWeight = 'normal';
	   document.f.counter.style.color = '#000000'; 
	} 
}

function share_warning(checkval) {
	if(checkval) {
		document.getElementById("share_warning").innerHTML = "When you share this item, it will be reviewed by Lunchtaker. If it meets <a href='/article.php?id=6&Sharing+lunch+foods+on+Lunchtaker.com' target='_blank'>our criteria</a> and is accepted into the library, you will no longer be able to edit the item.";
	} else {
		document.getElementById("share_warning").innerHTML = "";	
	}
}

function neworexisting(p) {
	radios = document.getElementsByName('add_type');
	if (p) {
		radios[1].checked = true;
	} else {
		radios[0].checked = true;			
	}
}

function clearratings() {
	var i=1, m="";
	for (i=1;i<=5;i++) {
		m = i + "star";
		document.getElementById(m).src = "img/0star.gif";
	}
}

function rate(rating) {
	clearratings();
	document.getElementById("1star").src = "img/1star.gif";
	if (rating > 1) document.getElementById("2star").src = "img/1star.gif";
	if (rating > 2) document.getElementById("3star").src = "img/1star.gif";
	if (rating > 3) document.getElementById("4star").src = "img/1star.gif";
	if (rating > 4) document.getElementById("5star").src = "img/1star.gif";
}

function rate_return(rating) {	
	var fl = Math.floor(rating);
	var cl = Math.ceil(rating);
	var fraction = false;
	if (fl != cl) fraction = true;
	clearratings();
	document.getElementById("1star").src = "img/1star.gif";
	if (rating > 1) {
		document.getElementById("2star").src = "img/1star.gif";
		if (rating < 2) document.getElementById("2star").src = "img/star_half.gif"
	}
	if (rating > 2) {
		document.getElementById("3star").src = "img/1star.gif";
		if (rating < 3) document.getElementById("3star").src = "img/star_half.gif"
	}
	if (rating > 3) {
		document.getElementById("4star").src = "img/1star.gif";
		if (rating < 4) document.getElementById("4star").src = "img/star_half.gif"
	} 
	if (rating > 4) {
		document.getElementById("5star").src = "img/1star.gif";
		if (rating < 5) document.getElementById("5star").src = "img/star_half.gif"
	}
}
function paginate(frm, num) {
	frm.pageno.value = num;
	frm.submit();	
}
function switch_az(frm, val) {
	frm.letter.value = val;
	frm.submit();	
}

function isNull(val){return(val==null);}

function update_lunch_qty(frm, boxnumber) {
	var tmpstring = "cal_original_" + boxnumber;
	var tmpstring2 = "cal_calculated_" + boxnumber;
	var calories = document.getElementById(tmpstring).value;
	
	if (calories != "n/a") {
		document.getElementById(tmpstring2).value = calories * frm.options[frm.selectedIndex].value;
	} else {
		document.getElementById(tmpstring2).value = calories;
	}
	
	// now let's get the total of all cells and display
	
	var table = document.getElementById("calorie_table"); 
	var cells = table.getElementsByTagName("input"); 
	var tmp_cal_total = 0;
	for (var i = 0; i < cells.length; i++) { 
		status = cells[i].getAttribute("id");
		status = String(status);
		if (status.search("cal_calculated_") >= 0) { // we've found the cell, so let's add to total
			var tmp_num = cells[i].value.replace(/\,/g,'');
			if (tmp_num != "n/a") {
				tmp_cal_total += Number(tmp_num);
			}
		}
	}
	
	// now set the total value
	document.getElementById("cal_total").value = tmp_cal_total;
	
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

