/*
	frs-index.js
	Demo Script for Free Interactive Services BV
	
	Revision 1
	(c) 2010 MasterObjects, Inc.
*/

if (MO.env.mobile) {
	document.location.replace("http://f.questobjects.com");
}

var frsField = {questletVersion: 200, technology: 'dhtml'},
	frsFieldId = "FrsProductFinder";
var useQuestField = QO.allowField(frsField);

function insertFrsQuestField(theWidth) {
	if (useQuestField) {
		MO.loader.loadCss(null,".mo .frsList {margin:8px 0} .mo .frsList li {display:list-item;margin:2px 0 0 16px}");
		
		frsField.id					= frsFieldId;
		frsField.license			= "0000001";
		frsField.defaultDomain		= "questobjects.com";
		frsField.questletVersion	= 202;
		frsField.connectString		= "/questobjects-nl-frs/server";
		frsField.contentChannel		= "frs";
		frsField.listLayout			= "/questobjects-demo-resources/frs.nl/questfield/layouts/NlFrsDemo";
		frsField.autoSelect			= "match";
		frsField.showResultArrows	= false;
		frsField.hoverSelect		= true;
		frsField.autoComplete		= "right";
		frsField.questFieldWidth	= theWidth;
		frsField.popUpWidth			= theWidth;
		frsField.questFieldHeight	= 20;
		frsField.errorHandler		= "demoErrorHandler";
		frsField.valueInput			= "searchinput";
		frsField.valueForm			= "searchform";
		frsField.helpText			=
				'Welkom bij deze "ProductFinder QuestField" demo voor Free Interactive Services. Met het veld hierboven vind je snel wat je zoekt. Hieronder enkele tips die je daarbij helpen:' +
				'<ul class="frsList">' +
					'<li>begin met het invullen van slechts enkele letters<\/li>' +
					'<li>de meest populaire producten vind je bovenaan de lijst<\/li>' +
					'<li>je kan zoeken op titel, artiesten en uitvoerenden, categorie, genre, artikelnummer, etc.<\/li>' +
					'<li>je mag ook beginletters van meerdere woorden invullen, met spatie ertussen<\/li>' +
					'<li>je kan ook op "zoeken" klikken om op de traditionele manier te zoeken<\/li>' +
				'<\/ul>' +
				'Deze preview is op 25 januari bijgewerkt.';
		frsField.footerHeight		= 75;
		frsField.footerHTML		= '<div style="padding:0px 4px;font-size:11px">This demo for Free Interactive Services BV was created by <a href="http://www.masterobjects.com" style="font-size:11px">MasterObjects BV</a>. List layout is identical to the iPhone Preview at <a href="http://f.questobjects.com">f.questobjects.com</a>. This simple test page submits the current QuestField value when doing a traditional submit (clicking "zoeken" or pressing the Return key).</div>';
		frsField.popUpRows			= 6;
		frsField.placeholder		= "Demo-QuestField (type beginletters...)";
		frsField.errorHandler		= frsErrorHandler;
		
		QO.field.insert(frsField);
		MO.util.addOnload(focusFrs);
	
		MO.d.write('<input name="fh_search" id="searchinput" type="hidden" value="" />');
	} else {
		MO.d.write('<input style="width:349px;font-size:12px;" name="fh_search" id="searchinput" type="text" onfocus="removeStartText(document.getElementById(\'searchinput\'));" onblur="setStartText(document.getElementById(\'fh_search\'));" value="" />');
	}
}

function frsErrorHandler(id,number,text,recoverable) {
	var errorText =
		"Oops! It seems that an error occurred in this FRS test page." + MO.N + MO.N +
		"We or our partner Crimsonwing are either updating the server, or the internet connection may be down temporarily. In either case, thank you for calling us so that we can rectify the situation!" + MO.N + MO.N + "U.S./Canada: +1-800-518-6001 (toll-free)" + MO.N + "Rest of world: +31-346-285-144" + MO.N + MO.N + "Thank you on behalf of the MasterObjects team!";
	if (recoverable) {
		errorText += MO.N + MO.N + '(You can try your query again by clicking on the "triangle" on the right side of the QuestField.)';
	}
	alert(errorText);
}

function focusFrs() {
	QO.util.setFocus(frsFieldId);
}

function selectProductCategory(theSelect) {
	var theVal = theSelect.value;
	if (theVal.indexOf('01') > -1) {
		theVal = "music";
	}
	else if (theVal.indexOf('02') > -1) {
		theVal = "movies";
	}
	else if (theVal.indexOf('03') > -1) {
		theVal = "games";
	}
	else if (theVal.indexOf('05') > -1) {
		theVal = "extras";
	}
	else {
		theVal = '';
	}
	QO.field.query(frsFieldId, theVal, null, true); // Allow the results list to open if results are found
	theSelect.blur();
}


/* Metadata functions also used in the frs list layout */

function frsImageUrl(metadata,subdirectory) {
	if (metadata && metadata.meta40 == "Y") {
		var theUrl = "http://images.frs.nl" + metadata.meta6;
		if (subdirectory !== '') {
			var i = theUrl.lastIndexOf('/');
			if (i > -1) {
				theUrl = theUrl.substr(0,i) + "/" + subdirectory + theUrl.substr(i);
			}
		}
		return theUrl;
	}
	return "http://www.freerecordshop.nl/is-bin/intershop.static/WFS/FreeRecordShop-FRS_B2C_NL-Site/-/nl_NL/images/placeholders/CD_productdetail.jpg";
}

function frsCategory(prevValue,theValue) {
	return (theValue && theValue != prevValue) ? ("&raquo;" + unescape(theValue)) : "";
}

var theOtherText;
function frsAppend(theValue,theLabel) {
	if (theValue) {
		theOtherText += ((theOtherText.length > 0) ? ", " : "") + (theLabel ? '<div class="frsLabel">' + theLabel + "<\/div>" : "") + unescape(theValue);
	}
}

function frsBlock(theValue,theLabel) {
	return theValue ? ((theLabel ? '<div class="frsLabel">' + theLabel + "<\/div>" : "<br /><br />") + unescape(theValue)) : "";
}

function frsDelivery(meta) {
	switch (meta.meta4) {
		case "":
		case "0": 
			return "1 werkdag";
		case "1": 
			return "1 werkdag";
		default:
			return (meta.meta4 + " werkdagen");
	}
}

/*
	End of frs-index.js
*/