//<![CDATA[

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

rowWrapperOpacity = new Array(6);
rowWrapperOpacity = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0];
displayedStreetCity = false
lastTimeoutId = "";
lastActiveRow = -1;

arrMagazinePrices = new Array(0, 5.95, 10.50, 15.00, 17.50, 19.00, 19.95);

infoDiv = new Array(6);
infoDiv[0] = ['gender;Selecteer uw aanspreekvorm;^(0|1)$', 'birthDay;Vul uw geboortedag in;^[1-9][0-9]?$', 'birthMonth;Vul uw geboortemaand in;^[1-9][0-9]?$', 'birthYear;Vul uw geboortejaar in;^[0-9]{4}$'];
infoDiv[1] = ['firstname;Vul uw voorletter(s) in;^[a-zA-Z\.\\s]+$', 'lastname;Vul uw achternaam in;^[a-zA-Z\'\\s]+$'];
infoDiv[2] = ['zipcode;Vul de cijfers van uw postcode in;^[1-9]{1}[0-9]{3}$', 'zipcodeChars;Vul de letters van uw postcode in;^[a-zA-Z]{2}$', 'houseNr;Vul uw huisnummer in;^[1-9]+[0-9]*$'];
infoDiv[4] = ['phoneNr;Vul uw telefoonnummer in;^[0-9][1-8][0-9]{8}$', 'email;Vul uw e-mailadres in;^[a-z0-9!#$%&\'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}|com|org|net|gov|mil|biz|pro|info|mobi|name|aero|jobs|museum)\\b$'];
infoDiv[5] = ['payment_method;Selecteer een betaalmethode;^(ideal|direct_debit)$'];
//infoDiv[6] = ['ideal_issuer_id;Selecteer uw bankrelatie;^()|([0-9]{4})$'];
//infoDiv[7] = ['accountNumber;Vul uw bankrekeningnummer in;^()|[0-9]{9}$'];

function replaceInfoDivContent(rowNum, fieldName, event)
{
	returnValue = false;
	if(infoDiv[rowNum].length)
	{
		for(i=0;i<infoDiv[rowNum].length;i++)
		{
			infoDivLine = infoDiv[rowNum][i].split(";");
			if(infoDivLine[0] == fieldName)
			{
				var content = infoDivLine[1];
				if(($(fieldName) && $(fieldName).type == 'radio' && $RF('orderMagazinesForm', fieldName) == null) || (!$(fieldName) && $RF('orderMagazinesForm', fieldName) == null) || ($(fieldName) && $(fieldName).type == 'select-one' && $(fieldName).selectedIndex == 0) || ($(fieldName) && ($(fieldName).type == "text" && ($(fieldName).value == "" || !regexTest(infoDivLine[2], $(fieldName).value)))))
				{
					if(event == "blur")
					{
						$$('.infoDiv')[rowNum].replace('<div class="infoDiv nok">'+infoDivLine[1]+'</div>');
						$$('.infoDiv')[rowNum].pulsate();
						fadeFormRowWrapper(i);
						//if($(fieldName)) { $(fieldName).focus(); $(fieldName).select(); }
					}
					else
					{
						$$('.infoDiv')[rowNum].replace('<div class="infoDiv pending">'+infoDivLine[1]+'</div>');
						$$('.infoDiv')[rowNum].highlight();
					}
					return false;
				}
				else if(($(fieldName) && $(fieldName).type == 'radio' && $RF('orderMagazinesForm', fieldName) != null) || (!$(fieldName) && $RF('orderMagazinesForm', fieldName) != null) || ($(fieldName) && $(fieldName).type == 'select-one' && $(fieldName).selectedIndex > 0) || ($(fieldName) && ($(fieldName).type == "text" && $(fieldName).value != "" && regexTest(infoDivLine[2], $(fieldName).value))))
				{
					$$('.infoDiv')[rowNum].replace('<div class="infoDiv ok"><img src="images/check_sign.gif" style="border:0;" /> OK</div>');
					$$('.infoDiv')[rowNum].highlight();
					return true;
				}
			}
		}
	}
	return false;
}

function regexTest(regexp, subject)
{
	var re = new RegExp(regexp);
	var m = re.exec(subject);
	if (m == null) {
		return false;
	} else {
		return true;
	}
}

function fadeFormRowWrapper(active_row)
{
	var i = 0;
	if(lastActiveRow != active_row && !Prototype.Browser.IE)
	{
		$$('.form_row_wrapper').each(function(elem)
		{
			if(i == active_row)
			{
				if(rowWrapperOpacity[i] == 0.4)
				{
					new Effect.Opacity(elem, { from: 0.4, to: 1.0 }); // activate
					rowWrapperOpacity[i] = 1.0;
					lastActiveRow = i;
				}
			}
			else
			{
				if(rowWrapperOpacity[i] == 1.0)
				{
					new Effect.Opacity(elem, { from: 1.0, to: 0.4 }); // deactivate
					rowWrapperOpacity[i] = 0.4;
				}
			}
			i++;
		});
	}
}


function calculateMagazines()
{
	theForm = $('orderMagazinesForm');
	magazineCounter = 0;
	for (i=0,n=theForm.elements.length;i<n;i++) {
		if (theForm.elements[i].name.indexOf('mag') !=-1) {
			if(theForm.elements[i].checked == true) {
				magazineCounter++;
			}
		}
	}
	var strShowprice = ''+cent(arrMagazinePrices[magazineCounter]+2.95);
	var bladen = 'magazines';
	if(magazineCounter == 0)
	{
		strShowprice = '0.00';
		for (i=0,n=theForm.elements.length;i<n;i++) 
		{
			if (theForm.elements[i].name.indexOf('mag') == -1) 
			{
				theForm.elements[i].disabled = true;
			}
		}

	}
	else
	{
		for (i=0,n=theForm.elements.length;i<n;i++) 
		{
			theForm.elements[i].disabled = false;
		}
		if(magazineCounter == 1)
			bladen = 'magazine';
	}
	$('showPrice').update(magazineCounter + ' ' + bladen + ' = &euro;&nbsp;' + strShowprice.replace('.',','));
	$('showPrice').highlight();
	$('price').setValue(arrMagazinePrices[magazineCounter]+2.95);
}

function cent(amount) 
{
	amount -= 0;
	return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}


function checkForm(form)
{
	for(n=0;n<infoDiv.length;n++)
	{
		if(!infoDiv[n]) continue;
		for(j=0;j<infoDiv[n].length;j++)
		{
			infoDivLine = infoDiv[n][j].split(";");
			if(!replaceInfoDivContent(n, infoDivLine[0], 'blur'))
			{
				//if($(infoDivLine[0])) $(infoDivLine[0]).focus();
				fadeFormRowWrapper(n);
				return false;
				break;
			}
		}
	}
	if($('direct_debit').checked && ($('accountNumber').value == '' || ($('accountNumber').value != '' && !regexTest('^[0-9]{1}[0-9]{5,}$', $('accountNumber').value))))
	{
		$$('.infoDiv')[7].replace('<div class="infoDiv nok">Vul uw rekeningnummer in</div>');
		$$('.infoDiv')[7].pulsate();
		fadeFormRowWrapper(7);
		return false;
	}
	else if($('privacy').checked == false)
	{
		alert('Om te bestellen dient u akkoord te gaan met onze privacy voorwaarden');
		return false;
	}
	else
	{
		form.submit();
	}
	return true;
}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }
    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

Event.observe(window, 'load', function()
{
	Event.observe.delay(2, $('houseNr'), 'keyup', function(event)
	{
		window.clearTimeout(lastTimeoutId);
		lastTimeoutId = window.setTimeout(function()
		{
			if($('houseNr').value != "")
			{
				new Ajax.Request("includes/script/check_zip_wrapper.php?zip="+$('zipcode').value+''+$('zipcodeChars').value+"&houseNumber="+$('houseNr').value, {
					method: 'get',
					onSuccess: function(transport)
					{
						if(transport.responseText != "error;error")
						{
							address = transport.responseText.split(";");
							$('address').setValue(address[0]);
							$('city').setValue(address[1]);
							if(!displayedStreetCity)
							{
								$('address_city').appear();
								displayedStreetCity = true;
							}
						}
						else
						{
							$('address_city').fade();
							displayedStreetCity = false;
							$$('.infoDiv')[2].replace('<div class="infoDiv nok">Onjuist adres</div>');
							$$('.infoDiv')[2].pulsate();
							fadeFormRowWrapper(2);
						}
					}
				});
			}
		}, 1500);
	});

	[$('gender_mr'), $('gender_mrs')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(0); replaceInfoDivContent(0, 'gender', ''); } ) });
	[$('gender_mr'), $('gender_mrs')].each(function(e){Event.observe(e,'click', function() { replaceInfoDivContent(0, 'gender', ''); } ) });
	[$('gender_mr'), $('gender_mrs'), $('birthDay'), $('birthMonth'), $('birthYear')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(0); replaceInfoDivContent(0, e.id, ''); } ) });
	[$('gender_mr'), $('gender_mrs'), $('birthDay'), $('birthMonth'), $('birthYear')].each(function(e){Event.observe(e,'click', function() { replaceInfoDivContent(0, e.id, ''); } ) });

	[$('firstname'), $('prefix'), $('lastname')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(1); replaceInfoDivContent(1, e.id, ''); } ) });
	[$('firstname'), $('prefix'), $('lastname')].each(function(e){Event.observe(e,'blur', function() { replaceInfoDivContent(1, e.id, 'blur'); } ) });

	[$('zipcode'), $('zipcodeChars'), $('houseNr'), $('houseNrExtra')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(2); replaceInfoDivContent(2, e.id, ''); } ) });
	[$('zipcode'), $('zipcodeChars'), $('houseNr'), $('houseNrExtra')].each(function(e){Event.observe(e,'blur', function() { replaceInfoDivContent(2, e.id, 'blur'); } ) });

	[$('phoneNr'), $('email')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(4); replaceInfoDivContent(4, e.id, ''); } ) });
	[$('phoneNr'), $('email')].each(function(e){Event.observe(e,'blur', function() { replaceInfoDivContent(4, e.id, 'blur'); } ) });

	[$('accountNumber')].each(function(e){Event.observe(e,'focus', function() { fadeFormRowWrapper(7); replaceInfoDivContent(7, e.id, ''); } ) });
	[$('accountNumber')].each(function(e){Event.observe(e,'blur', function() { replaceInfoDivContent(7, e.id, 'blur'); } ) });

	fadeFormRowWrapper(0);
});

Element.observe(window, 'load', calculateMagazines);

//]]>