
//================================================
// Switch the visibility to block/none
//================================================
function switchVisibility(oElm1, oElm2, bSwitch)
{
	var e1 = oElm1;
	var e2 = oElm2;
	
	if (bSwitch)
	{
		e1 = oElm2;
		e2 = oElm1;
	}
	
	e1.style.display = "block";
	e2.style.display = "none";		
}

//================================================
// Returns the fields/res elements
//================================================
function getResultElement() { return document.getElementById("resultPanel"); }
function getFieldsElement() { return document.getElementById("fieldsPanel"); }
function getInputElement() { return document.getElementById("question-input"); }
function getSearchForm() { return document.getElementById("resultsForm"); }
function getMainForm() { return document.getElementById("mainForm"); }

//================================================
// HACK: Resize results panel
//================================================
function ifrResize() {
   var hi = 1024;
   var wi = parseInt(document.getElementById("resultPanel").offsetWidth);
   if (wi<600) {hi = "1100";}
   if (wi<550) {hi = "1200";}
   if (wi<500) {hi = "1250";}
   if (wi<450) {hi = "1325";}
   if (wi<400) {hi = "1600";}
   if (wi<300) {hi = "1800";}
   if (wi<250) {hi = "2000";}
   if (wi<200) {hi = "2500";}
   if (wi<190) {hi = "2800";}
   document.getElementById("resultsFrame").height = hi; 
 }

//================================================
// Show the results panel
//================================================
function showResults()
{
	switchVisibility(getResultElement(), getFieldsElement());
	openSearch();
}

//================================================
// Show the fields panel
//================================================
function showFields()
{
	switchVisibility(getResultElement(), getFieldsElement(), true);
}

//================================================
// Open the resultspanel and search with the 
// contents of the inputbox
//================================================
function openSearch()
{
	var input = getInputElement();

	if (input != null)
	{
		var question = input.value;
		
		if (question.length > 1)
		{
			question = question.substr(0, 255);
			getSearchForm().question.value = question;
			getSearchForm().submit();
			
			// Do not log this information
			// aladinLog(question, "aladin_vraag_question_form", "");
		}
		else
		{
			alert("Uw vraag is te kort!");
		}
	}
}

//================================================
// Sets focus to the inputbox
//================================================
function focusInputBox()
{
	if (getInputElement() != null)
	{
		getInputElement().focus();
	}
}

//================================================
// Opens the youth chat page
//================================================
function openChat()
{
	window.open("jeugd/aladin_chat.asp?question=" + aladinForm.question.value + "&email=" + aladinForm.email.value, "_self");
}

//================================================
// Change handler for "Anders" options
//================================================
function onAndersChange(oSelectElm, sTrigger, sChangeID)
{
	var o = oSelectElm.options[oSelectElm.selectedIndex];
	var e = document.getElementById(sChangeID);
	if (e != null)
	{
		if (o.value.toLowerCase() == sTrigger.toLowerCase())
		{
			e.style.display = "inline";
			
			// Switch names so form treats this
			// item the same as select box
			if (oSelectElm.name != "")
				e.name = oSelectElm.name;
			oSelectElm.name = "";
		}
		else
		{
			e.style.display = "none";

			// Switch names back
			if (e.name != "")
				oSelectElm.name = e.name;
			e.name = "";
		}
	}
}

//================================================
// Add a check, either function or boolean, 
// to an array
//================================================
function addCheck(aChecks, oCheck, sMsg)
{
	if (Function.prototype.isPrototypeOf(oCheck) == true)
	{
		if (oCheck() != true)
		{
			aChecks.push(sMsg);
		}
		
		return;
	}

	if (oCheck != true)
	{
		aChecks.push(sMsg);
		return;
	}	
}

//================================================
// Walk a check array, returns true if no errors
//================================================
function checkArrayAlert(sPreMessage, aArr)
{
	if (aArr.length > 0)
	{
		var tot = sPreMessage + "\r\n\r\n";
		
		for(var i=0; i < aArr.length; i++)
		{
			if (i > 0) tot += ", ";
			tot += aArr[i];
		}
		
		alert(tot);
		return false;
	}
	
	return true;
}

//================================================
// Submit handler for main form
//================================================
function submitMainForm() 
{
	if (checkMainForm(getMainForm())) 
	{
		getMainForm().submit();
	}
}

//================================================
// Check function for main form
//================================================
function checkMainForm(form) 
{
	var checks = new Array();
	
	addCheck(
		checks,
		function() {
			var val = form.email.value;			
			try { return emailCheck(val) && val.length > 1; } catch(e) { }
			return true;
		},
		"E-mailadres (e-mail niet correct)"
	);
	addCheck(
		checks,
		function() {
			var val = form.email2.value;
			try { return emailCheck(val) && val.length > 1; } catch(e) { }
			return true;
		},
		"E-mailadres nogmaals (e-mail niet correct)"
	);	
	addCheck(
		checks,
		form.email.value == form.email2.value,
		"Uw e-mail adressen zijn niet gelijk"
	);	
	addCheck(
		checks, 
		form.vraag.value.length > 1, 
		"Vraag"
	);	
	addCheck(
		checks, 
		form.postcode.value.length >= 4, 
		"Postcode (4 cijfers)"
	);	
	addCheck(
		checks, 
		form.postcodeext.value.length >= 2, 
		"Postcode (2 letters)"
	);	
	addCheck(
		checks,
		form.leeftijd[0].checked || form.leeftijd[1].checked || form.leeftijd[2].checked || form.leeftijd[3].checked || form.leeftijd[4].checked || form.leeftijd[5].checked,
		"Leeftijd"
	);
	addCheck(
		checks,
		form.kennisniveau[0].checked || form.kennisniveau[1].checked || form.kennisniveau[2].checked || form.kennisniveau[3].checked || form.kennisniveau[4].checked || form.kennisniveau[5].checked,
		"Kennisniveau"
	);
	addCheck(
		checks,
		form.lid_van_bieb[0].checked || form.lid_van_bieb[1].checked,
		"Lid van de bibliotheek"
	);
	addCheck(
		checks,
		form.geslacht[0].checked || form.geslacht[1].checked,
		"Geslacht"
	);
	addCheck(
		checks,
		form.taal_bronnen[0].checked || form.taal_bronnen[1].checked,
		"Taal bronnen"
	);

		
	return checkArrayAlert(
		"Een of meer van de volgende velden zijn nog niet juist ingevuld:",
		checks
	);
}

//================================================
// Log information
//================================================
function aladinLog(sVal, sType, sReserved)
{
}

//================================================
// check email
//================================================
function emailCheck (emailStr) 
{
   emailStr = trim(emailStr);
   
	/* The following pattern is used to check if the entered e-mail address
		fits the user@domain format.  It also is used to separate the username
		from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
		characters.  We don't want to allow special characters in the address. 
		These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
		username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
		which case, there are no rules about which characters are allowed
		and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
		rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
		non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
		For example, in john.doe@somewhere.com, john and doe are words.
		Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
		domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is
		valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
		different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) 
	{
		/* Too many/few @'s or something; basically, this address doesn't
			even fit the general mould of a valid e-mail address. */
		//alert("Uw e-mail adres lijkt niet correct (controleer de @ en .)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid 
	if (user.match(userPat)==null) 
	{
		// user is not valid
		//alert("Uw gebruikersnaam van uw e-mail lijkt niet correct.")
		return false
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
		host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) 
	{
		// this is an IP address
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				//alert("Het IP adres van uw email is niet geldig!")
				return false
			}
		}
		
		return true
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) 
	{
		//alert("Het domein naam van uw e-mail adres is niet correct.")
		return false
	}

	/* domain name seems valid, but now make sure that it ends in a
		three-letter word (like com, edu, gov) or a two-letter word,
		representing country (uk, nl), and that there's a hostname preceding 
		the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
		it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length

	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>4) 
	{
		// the address must end in a two, three or four letter word.
			if (true) 
		//alert("Het e-mailadres moet eindigen met een 3 of 4 letterige domeinnaam of twee letterige landnaam.")
		return false
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) 
	{
		var errStr="Dit e-mailadres mist een hostnaam!"
		//alert(errStr)
		return false
	}

	// If we've gotten this far, everything's valid!
	return true;
}

//================================================
// Trim string
//================================================
function trim(strParm) {
       var str = strParm;
   while(''+str.charAt(0)==' ')
      str=str.substring(1,str.length);
   while(''+str.charAt(str.length-1)==' ')
      str=str.substring(0,str.length-1);
   

       // alert("**"+str+"**");
        return str;
}


