var imgMinus    = new Image();
var imgPlus     = new Image();
imgMinus.src    = "/graphics/minus.gif";
imgPlus.src     = "/graphics/plus.gif";

function showSection(section,control) {
        var string = eval(section + '_string');
        if (control == 1) {
                document.getElementById(section + '_span').innerHTML = string;
                document.getElementById(section + '_control').innerHTML = '<a style="cursor:hand;" onClick="showSection(\'' + section + '\',0);"><img src="' + imgMinus.src + '" border="0"> hide ' + section + ' results</a>';
        } else {
                document.getElementById(section + '_span').innerHTML = '<br><br>';
                document.getElementById(section + '_control').innerHTML = '<a style="cursor:hand;" onClick="showSection(\'' + section + '\',1);"><img src="' + imgPlus.src + '" border="0"> show ' + section + ' results</a>';
        }
        return true;
}

function refreshChoices(script,qs,lst,inid) {
        var re = '/:'+inid+':/';
	qs = qs.replace(/&lst=([^&]*)/g,"");
        var clct = document.forms['collect'].collected;
        var len = clct.length;
        for (var i=0;i<len;i++) {
                if (clct[i].value == inid && clct[i].checked) {
                        document.getElementById('c' + inid).style['backgroundColor'] = '#eef0bd';
                        document.getElementById('c' + inid).style['fontWeight'] = 'bold';
                        document.getElementById('e' + inid).style['backgroundColor'] = '#eef0bd';
                } else if (clct[i].value == inid) {
                        document.getElementById('c' + inid).style['backgroundColor'] = '#ffffff';
                        document.getElementById('c' + inid).style['fontWeight'] = 'normal';
                        document.getElementById('e' + inid).style['backgroundColor'] = '#ffffff';
		        lst = lst.replace(":"+inid+":","");
                        self.location = 'http://www.internationalrecipes.net'+script+'?'+qs+'&lst='+lst;
                }
        }

}
function makeURL(script,qs,lst,pos) {
        clct = document.forms['collect'].collected;
        len = clct.length;
	qs = qs.replace(/&lst=([^&]*)/g,"");
	qs = qs.replace(/&pos=([^&]*)/g,"");
        for (var i=0;i<len;i++) {
                if (clct[i].checked == 1 && lst.indexOf(":"+clct[i].value+":") == -1) {
                        lst = lst + ":" + clct[i].value + ":";
                }
        }
        self.location = "http://www.internationalrecipes.net" + script + "?" + qs + "&pos="+pos+"&lst="+lst;
}
function makeSort(script,qs,lst,sortval) {
        clct = document.forms['collect'].collected;
	qs = qs.replace(/&sort=([^&]*)/g,"");
	qs = qs.replace(/&pos=([^&]*)/g,"");
	qs = qs.replace(/&lst=([^&]*)/g,"");
        len = clct.length;
        for (var i=0;i<len;i++) {
                if (clct[i].checked == 1 && lst.indexOf(":"+clct[i].value+":") == -1) {
                        lst = lst + ":" + clct[i].value + ":";
                }
        }
        self.location = "http://www.internationalrecipes.net" + script + "?" + qs + "&sort="+sortval+"&pos=1&lst="+lst;
}
function makeShort(lst) {
        clct = document.forms['collect'].collected;
        len = clct.length;
        var cl = 0;
        for (var i=0;i<len;i++) {
                if (clct[i].checked == 1 && lst.indexOf(":"+clct[i].value+":") == -1) {
                        lst = lst + ":" + clct[i].value + ":";
                        cl++;
                }
        }
        if (cl == 0 && lst == "") {
                alert('You must check at least one recipe for your short list before selecting this feature.');
        } else {
                self.location = "http://www.internationalrecipes.net/recipes/list.pl?e=1&lst="+lst;
        }
}
function MM_checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass,URL,altURL) { //v2.0
  var newURL = '', version = parseFloat(navigator.appVersion);
  if (navigator.appName.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) newURL = (NSpass==1)?URL:altURL;}
    else {if (NSnoPass>0) newURL = (NSnoPass==1)?URL:altURL;}
  } else if (navigator.appName.indexOf('Microsoft') != -1) {
    if (version >= IEvers) {if (IEpass>0) newURL = (IEpass==1)?URL:altURL;}
    else {if (IEnoPass>0) newURL = (IEnoPass==1)?URL:altURL;}
  } else if (OBpass>0) newURL = (OBpass==1)?URL:altURL;
  if (newURL) {
    window.location = unescape(newURL);
    document.MM_returnValue = false;
  }
}

function expander(a)
{
                if (a.style.display =='')
                {
                        a.style.display = 'none';
                }
                else
                {
                        a.style.display='';
                }
}

function openAnyWindow(url, name) {
  var l = openAnyWindow.arguments.length;
  var w = "";
  var h = "";
  var features = "";

  for (i=2; i<l; i++) {
    var param = openAnyWindow.arguments[i];
    if ( (parseInt(param) == 0) ||
      (isNaN(parseInt(param))) ) {
      features += param + ',';
    } else {
      (w == "") ? w = "width=" + param + "," :
        h = "height=" + param;
    }
  }
  features += w + h;
  var code = "popupWin = window.open(url, name";
  if (l > 2) code += ", '" + features;
  code += "')";
  eval(code);
}

function switchList(script,lcl_list,sort) {
        clct = document.forms['collect'].collected;
        len = clct.length;
        for (var i=0;i<len;i++) {
                if (clct[i].checked == 1 && lcl_list.indexOf(":"+clct[i].value+":") == -1) {
                        lcl_list = lcl_list + ":" + clct[i].value + ":";
                }
        }
        self.location = script + '?d=' + document.forms['changeList'].category.options[document.forms['changeList'].category.selectedIndex].value + '&sort=' + sort + '&pos=0&lst='+lcl_list;
}

function switchList_v2() {
        self.location = '/d/' + document.getElementById('category').value ;
}

function toggleUL(id) {
	$('div#' + id).slideToggle('slow');
}

function validateNewComment() {
	var f = document.forms['new_comment'];
	var count = 0;
	if (f.rating.value == '') {
		document.getElementById('ratingcell').style.borderColor = '#f00';
		document.getElementById('ratingcell').style.backgroundColor = '#fee';
		count++;
	} else {
		document.getElementById('ratingcell').style.borderColor = '';
		document.getElementById('ratingcell').style.backgroundColor = '#fff';
	}

	if (f.name.value == '') {
		document.getElementById('newcomment_name').style.borderColor = '#ff0000';
		document.getElementById('newcomment_name').style.backgroundColor = '#ffeeee';
		count++;
	} else {
		document.getElementById('newcomment_name').style.borderColor = '#999999';
		document.getElementById('newcomment_name').style.backgroundColor = '#ffffee';
	}

	if (f.email.value == '') {
		document.getElementById('newcomment_email').style.borderColor = '#ff0000';
		document.getElementById('newcomment_email').style.backgroundColor = '#ffeeee';
		count++;
	} else {
		document.getElementById('newcomment_email').style.borderColor = '#999999';
		document.getElementById('newcomment_email').style.backgroundColor = '#ffffee';
	}

	if (f.city.value == '') {
		document.getElementById('newcomment_city').style.borderColor = '#ff0000';
		document.getElementById('newcomment_city').style.backgroundColor = '#ffeeee';
		count++;
	} else {
		document.getElementById('newcomment_city').style.borderColor = '#999999';
		document.getElementById('newcomment_city').style.backgroundColor = '#ffffee';
	}

	if (f.comment.value == '') {
		document.getElementById('newcomment_comment').style.borderColor = '#ff0000';
		document.getElementById('newcomment_comment').style.backgroundColor = '#ffeeee';
		count++;
	} else {
		document.getElementById('newcomment_comment').style.borderColor = '#999999';
		document.getElementById('newcomment_comment').style.backgroundColor = '#ffffee';
	}

	if (count == 0) {
		document.getElementById('newcomment_error').innerHTML = '';
		return true;
	} else {
		document.getElementById('newcomment_error').innerHTML = '<font style="color:#ff0000;font-weight:bold;">The highlighted fields below need to be completed before your comment can be contributed.</font><br>';
		return false;
	}
}


function openAnyWindow(url, name) {
  var l = openAnyWindow.arguments.length;
  var w = "";
  var h = "";
  var features = "";

  for (i=2; i<l; i++) {
    var param = openAnyWindow.arguments[i];
    if ( (parseInt(param) == 0) ||
      (isNaN(parseInt(param))) ) {
      features += param + ',';
    } else {
      (w == "") ? w = "width=" + param + "," :
        h = "height=" + param;
    }
  }

  features += w + h;
  var code = "popupWin = window.open(url, name";
  if (l > 2) {
	code += ", '" + features + "'";
  }
  code += ")";
  eval(code);
}

function give_rating(id,num,vote,whichSpan) {
	if (!jQuery("span#" + whichSpan).html().match(/done/)) {
		jQuery.get('/recipes/review_vote.pl','&cid=' + id + '&v=' + vote, function(data){
                        var total = jQuery("span#vote"+vote+"_"+num).text();
                        total = parseInt(total) + 1;
                        jQuery("span#vote"+vote+"_"+num).text(total);
                        jQuery("span#" + whichSpan).html("done");
                });
        } else { 
                alert('You have already provided a vote on this comment.');
        }
}

function focusBox(mode,id) {
  if (mode == "in") {
    jQuery("#"+id).css("filter","alpha(opacity=100)");
    jQuery("#"+id).css("-moz-opacity","1.0");
  } else if (mode == "out") {
    jQuery("#"+id).css("filter","alpha(opacity=50)");
    jQuery("#"+id).css("-moz-opacity","0.5");
  }
}

function TrackPopUp(obj) {
    if(typeof(obj) == "undefined") // just a simple check to avoid runtime errors.
        return;
    var winWidth  = document.body.clientWidth;
    var winHeight = document.body.clientHeight;
    x = winWidth - (obj.offsetWidth + window.event.x);    // see if the menu is not
                                                          // entirely  visible inside
                                                          // the window we shift the
                                                          // origin in the case when
                                                          // the menu is not.

    y = winHeight - (obj.offsetHeight + window.event.y);
    var yPos = y<0 ? window.event.y + y : window.event.y;
    yPos = document.body.scrollTop + window.event.y;
    obj.style.left = window.event.screenX;
    obj.style.top = y<0 ? window.event.y + y : window.event.y;

    obj.style.visibility = 'visible';
    return false;
}

function ClearPopUp(obj) {
    obj.style.visibility = 'hidden';
}

function checkBlank(obj) {
        var re = /^\d+$/;
        if (obj.value == '') {
                alert('Please enter criteria before performing a search. It typically works much better that way.');
                return false;
        } else if (obj.value.length <= 3 && !obj.value.match(re)) {
                alert('Your search criteria must be at least 4 characters in length. Please retry.');
                return false;
        }
}


