/* *****************
*** GENERAL
******************** */
function trimString(str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function checkemail(str){
	var filter=/^.+@.+\..{2,3}$/
	return (filter.test(str))
}

function getObj(objectId) {
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId);
	} else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
	} else {
		return null;
	}
}
function getStyleObj(objectId) {
	var o = getObj(objectId);
	if (o!=null && (document.getElementById || document.all)) { return o.style; }
	else if (o!=null) { return o; }
}
function getHeight(id) {
	var divMenu = getObj(id);
	var h = null;
	if ( divMenu.offsetHeight ) {
		h = divMenu.offsetHeight;
	} else if ( offsetHeight.pixelHeight ) {
		h = divMenu.pixelHeight;
	}
	return h;
}
/* *****************
*** PRINT
******************** */
function openPrint(URL)
{
	printWindow = window.open(URL,'winPrint','toolbar=yes,directories=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=600');
	printWindow.focus();
}
/* *****************
*** MENU
******************** */
var _startPageMenuStatus = "hidden";
var _startPageIntervalId = null;
var _startPageIntervalTime = 20;
var _startPageEase = .17;
var _startPageCurrentClipHeight = 0;
var _startPageEndClipHeight = 0;
 
function toggleStartpageMenu() {
	_startPageMenuStatus = ( _startPageMenuStatus == "hidden" )? "visible" : "hidden";
	var styleMenu = getStyleObj("navigationPopup");
	if ( _startPageMenuStatus == "hidden" ) {
		_startPageEndClipHeight = 0;
	} else {
		_startPageEndClipHeight = getHeight("navigationPopup");
	}
	window.clearTimeout(_startPageIntervalId);
	_startPageIntervalId = null;
	_startPageIntervalId = window.setTimeout(animateMenuWithClip, _startPageIntervalTime);
}
function animateMenuWithClip( ) {
	var styleMenu = getStyleObj("navigationPopup");
	var step = ( _startPageEndClipHeight - _startPageCurrentClipHeight ) * _startPageEase;
	if ( Math.abs(step) < .2 ) {
		_startPageCurrentClipHeight = _startPageEndClipHeight;
		window.clearTimeout(_startPageIntervalId);
		_startPageIntervalId = null;
	} else {
		_startPageCurrentClipHeight += step;
		_startPageIntervalId = window.setTimeout(animateMenuWithClip, _startPageIntervalTime);
	}
	styleMenu.clip = "rect(auto auto " + _startPageCurrentClipHeight + "px 0px)";
}

function setMenuVisible( visible ) {
	_startPageMenuStatus = ( visible )? "visible" : "hidden";
	var styleMenu = getStyleObj("navigationPopup");
	if ( _startPageMenuStatus == "hidden" ) {
		_startPageEndClipHeight = 0;
	} else {
		_startPageEndClipHeight = getHeight("navigationPopup");
	}
	window.clearTimeout(_startPageIntervalId);
	_startPageIntervalId = null;
	_startPageIntervalId = window.setTimeout(animateMenuWithClip, _startPageIntervalTime);
}

/* *****************
*** SEARCH
******************** */
function sfSearch(urlSearch) {
	var f = document.forms[0];
	f.action = urlSearch;
	f.submit();
}


/* *****************
*** TOOL - DOUBLE DD
******************** */
function sfDDSubmit() {
	var f = document.forms[0];
	var action = f.sfDDUrl.value;
	var cat1 = f.sfDDCat1[f.sfDDCat1.selectedIndex].value;
	var cat2 = f.sfDDCat2[f.sfDDCat2.selectedIndex].value;
	// if ( cat1 != "" || cat2 != "" ) {
		document.location.href = action + ((action.indexOf("?")>-1)? "&" : "?") + "cat1=" + cat1 + "&cat2=" + cat2;
	// }
}


/* *****************
*** RECIPE CHOOSER
******************** */
function sfRCSubmit() {
	var f = document.forms[0];
	var fish = f.sfRCFish[f.sfRCFish.selectedIndex].value;
	var cat = f.sfRCCat[f.sfRCCat.selectedIndex].value;
	var big = (f.sfRCBig.checked)? "1" : "0";
	// if ( fish != "" || cat != "" ) {
		document.location.href = "/1215.aspx?fish=" + fish + "&cat=" + cat + "&big=" + big;
	// }
}

/* *****************
*** FISH CHOOSER
******************** */
function sfFCSubmit(sel) {
	var f = sel.form;
	var fishId = sel[sel.selectedIndex].value;
	if ( fishId != "" ) {
		document.location.href = "/" + fishId + ".aspx";
	} else {
		alert("Du måste välja en fisk i listan");
	}
}

function sfFCNutritionSubmit(sel) {
	var f = sel.form;
	var fishId = sel[sel.selectedIndex].value;
	if ( fishId != "" ) {
		document.location.href = window.location.pathname+'?altTemplate=sfFishNutriotionDetail&fish='+fishId;
	} else {
		alert("Du måste välja en fisk i listan");
	}
}

/* *****************
*** NEWSLETTER
******************** */
function sfNewsletterSubmit(f) {
	if ( checkemail(f.email.value) ) {
		f.action = "http://mlm.issi.se/Member?action=register";
		f.method = "post";
		f.submit();
	} else {
		alert("Du måste ange en giltig e-postadress");
	}
}

/* *****************
*** KUNGSFENAN
******************** */
function sfKungsfenanNominate(f) {
	var body = "";
	body += "Nominerad: " + f.nom.value + "<br>\n";
	body += "Motivering: " + f.motivering.value + "<br>\n";
	body += "Namn: " + f.fromName.value + "<br>\n";
	body += "Adress: " + f.fromAdress.value + "<br>\n";
	body += "Postnummer: " + f.fromPostnr.value + "<br>\n";
	body += "Ort: " + f.fromCity.value + "<br>\n";
	body += "Telefon: " + f.fromPhone.value + "<br>\n";
	body += "E-post: " + f.fromEmailClone.value + "<br>\n";

	f.body.value	= body;
	//f.action = "http://www.pixgallery.com/servlet/se.daddy.components.MailForm";
	f.__VIEWSTATE.value = "";
	f.action = "http://client.daddyid.se/[system-settings]/sendmail";
	f.method = "GET";
	f.submit();
}

/* *****************
*** SHOP
******************** */
function sfShopValidate(f) {
	var error = "";

	// Validate product info
	f.sfShopProducts.value = "";
	for ( var i = 0; i < f.length; i++ ) {
		var field = f[i];
		if ( field.name.indexOf("sfProd_") > -1 ) {
			field.value = trimString(field.value);
			if ( field.value.length > 0 && isNaN(field.value) ) {
				error += "* " + field["errorLabel"] + "\n";
			} else if ( field.value.length > 0 && field.value > 0) {
				var arr = field.name.split("_");
				if ( arr.length > 2 && !isNaN(arr[1]) && !isNaN(arr[2]) ) {
					f.sfShopProducts.value += (f.sfShopProducts.value.length>0)? ";" : "";
					f.sfShopProducts.value += arr[1]+","+arr[2]+","+field.value; // id,box number,number ordered
				}
			}
		}
	}
	if ( error.length > 0 ) {
		error = "Kontrollera att följande fält är en siffra:\n" + error;
	} else if ( f.sfShopProducts.value < 1 ) {
		error = "Du har inte beställt någon vara.\n";
	}
	
	// Validate personal info
	var errorPersonal = "";
	if (trimString(f.sfShopName.value).length < 1) errorPersonal += "* Namn\n";
	if (trimString(f.sfShopAdress.value).length < 1) errorPersonal += "* Adress\n";
	if (trimString(f.sfShopPhone.value).length < 1) errorPersonal += "* Telefon\n";
	if (trimString(f.sfShopZip.value).length < 1) errorPersonal += "* Postnummer\n";
	if (trimString(f.sfShopCity.value).length < 1) errorPersonal += "* Ort\n";
	if (trimString(f.sfShopEmail.value).length > 0 && !checkemail(trimString(f.sfShopEmail.value))) errorPersonal += "* E-post\n";
	
	if ( error.length > 0 && errorPersonal.length > 0 ) {
		// error += "\n\n" + errorPersonal;
	} else if ( errorPersonal.length > 0 ) {
		error = "Kontrollera följande fält:\n" + errorPersonal;
	}

	if ( error.length > 0 ) {
		alert(error);
	} else {
		f.action = f.sfShopUrlConfirm.value;
		f.submit();
	}
}
function sfShopConfirm(f,action) {
	if ( action == "order" ) {
		f.action = f.sfShopUrlOrder.value;
	} else if ( action == "change" ) {
		f.action = f.sfShopUrlChangeOrder.value;
	}
	f.submit();
}
