function openwin(url, width, height)
{
	//center the window on the screen
	var w = 480, h = 340;
	
	w = screen.availWidth; //document.body.clientWidth;
	h = screen.availHeight; //document.body.clientHeight;

	var leftPos = (w-width)/2;
	var topPos = (h-height)/2;
	
	msgWindow = open(url,'WinName','menubar=0,status=0,scrollbars=0,resizable=1,toolbar=0,location=0,width=' + width + ',height=' + height + ',top=' + topPos + ',left=' + leftPos);
	msgWindow.focus;
	return msgWindow;
}

function openWin(strURL, strName, strFeatures) {
  window.open(strURL, strName, strFeatures).focus();
}

function isEmail(sEmail)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/;
	if(!re.test(sEmail))
	{
		alert("E-postadressen är felaktig!\nVar god ange en korrekt e-postadress.");
	}
	return(re.test(sEmail));
}

function isNumeric(i, sField)
{
	if(i.length)
	{
		var re = /\d/;
		if(!re.test(i))
		{
			alert("Texten du angivit i fältet \"" + sField + "\" innehåller otillåtna tecken!\nEndast siffror är tillåtna.\n\nVar god ange en korrekt text.");
		}
		return(re.test(i));
	}
	else
		return(true);
}

// A utility function that returns true if a string contains only 
// whitespace characters.
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
	

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];

        if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + fieldnames[i];
                continue;
            }

            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(v) || 
                    ((e.min != null) && (v < e.min)) || 
                    ((e.max != null) && (v > e.max))) {
                    errors += "- Fältet " + fieldnames[i] + " kan bara innehålla tal";
                    if (e.min != null) 
                        errors += " som är större än " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " och mindre än " + e.max;
                    else if (e.max != null)
                        errors += " som är mindre än " + e.max;
                    errors += ".\n";
                }
            }
        }
    }

	
    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields) return true;

    msg  = "______________________________________________________\n\n"
    msg += "Formuläret kunde inte skickas pga nedanstående fel.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- Följande obligatoriska fält har inte fyllts i:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    alert(msg);
    return false;
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		butSearchOver = newImage("/img/recept/butSearchOver.gif");
		butOrtguideOver = newImage("/img/recept/butOrtguideOver.gif");
		butPotatisguideOver = newImage("/img/recept/butPotatisguideOver.gif");
		butSallatsguideOver = newImage("/img/recept/butSallatsguideOver.gif");
		preloadFlag = true;
	}
}

function printFriendly(sURL) {
	if (sURL.length) {
		window.open("/lib/scr/print.asp?page="+sURL,"","width=600,height=480,scrollbars=yes");
	}
}

function printRecipe(iRecipeID) {
	if (iRecipeID.length) {
		window.open("/lib/scr/printRecipe.asp?id="+iRecipeID,"","width=600,height=480,scrollbars=yes");
	}
}

function printWin() {
	if (window.print) {
		window.print();
	}
}

function openFaq(id)
{
	var d = document.getElementById(id);
	
	if(d.style.display == "none")
		d.style.display = "block";
	else
		d.style.display = "none";
}