
//	document.goCoreAjax = null;

	if ( 'undefined' != typeof( goCoreWindowOnLoad ) )
	{
		goCoreWindowOnLoad.add('document.goCoreAjax = new coreAjax();');
		goCoreWindowOnLoad.add('document.goCoreAjax.bUseXRequestedWithHeader = true;');
		goCoreWindowOnLoad.add('document.goCoreAjax.setShowErrorMessage( true );');
	}
	else
	{
		document.goCoreAjax = new coreAjax();
		document.goCoreAjax.bUseXRequestedWithHeader = true;
		document.goCoreAjax.setShowErrorMessage( true );
	}

	function coreValidatorZipcode(sValidatorName)
	{

		this.NETHERLANDS					=	121;

		this.oZipcode						=	null;
		this.oZipcodeAdditive				=	null;
		this.oHousenumber					=	null;
		this.oStreet						=	null;
		this.oCity							=	null;
		this.oAdditiveAddress				=	null;
		this.oForeignAddress				=	null;
		this.aSettings						=	new Array();
		this.sBaseUrl						=	document.location.href;
		this.oCountrySelect					=	null;
		this.eCountry						=	121;

		this.oRegExp						=	new RegExp("\[|\]", "g");

		if ("undefined" !== typeof(sValidatorName))
		{
			this.sName						=	sValidatorName;
		}
		else
		{
			this.sName						=	'coreValidatorZipcode';
		}

		this.aFields						=	new Array();

		var oThis = this;

		this.getValidationName				=	function()
												{
													return this.sName;
												}

		this.addField						=	function(oField, aSettings)
												{
													if ("undefined" === typeof(this.aFields[oField.name.replace(this.oRegExp, "_")]))
													{
														this.aFields[oField.name.replace(this.oRegExp, "_")] 				= new Array();
														this.aFields[oField.name.replace(this.oRegExp, "_")]['field'] 		= oField;
														this.aFields[oField.name.replace(this.oRegExp, "_")]['settings'] 	= aSettings;
														if ('undefined' !== typeof(aSettings['base_url']))
														{
															this.sBaseUrl = aSettings['base_url'];
														}
													}
												}

		this.getFields						=	function()
												{
													return this.aFields;
												}

		this.getError						=	function()
												{
													return "Zipcode is not valid";
												}

		this.isValid						=	function(oField, aSettings)
												{
													if (typeof(aSettings['country']) === "undefined")
													{
														eCountry = this.getCountry();
													}
													else
													{
														if ("object" === typeof(aSettings['country']))
														{
															eCountry = aSettings['country'].options[aSettings['country'].selectedIndex].value;
														}
														else
														{
															eCountry = aSettings['country'];
														}
													}

													switch (parseInt(eCountry, 10))
													{
														case this.NETHERLANDS:
														{

															var sResponse = "not found";
															if (this.oZipcode.value != "" && this.oZipcodeAdditive.value != "" && this.oHousenumber.value != "")
															{
																var sChar = '?';
																if (-1 !== this.sBaseUrl.indexOf('?'))
																{
																	sChar = '&';
																}
																var sUrl = this.sBaseUrl+sChar+'zipcode='+this.oZipcode.value+'&zipcode_additive='+this.oZipcodeAdditive.value+'&homenumber='+this.oHousenumber.value+'&country_id='+eCountry;
																sResponse = document.goCoreAjax.requestGetTextSynchrone(sUrl);
															}
															return this.fillZipcode(sResponse)
														}
														break;

														default:
														{
															return true;
														}
														break;
													}
													return false;
												}

		this.setCountrySelect				=	function(oCountrySelect)
												{
													this.oCountrySelect = oCountrySelect;
												}

		this.setCountry						=	function(eCountry)
												{
													this.eCountry = eCountry;
												}

		this.getCountry						=	function()
												{
													if (null !== this.oCountrySelect)
													{
														return this.oCountrySelect.options[this.oCountrySelect.selectedIndex].value;
													}
													return this.eCountry;
												}

		this.setFields						=	function(aFields)
												{
													this.aSettings			=	aFields;
													this.oZipcode			=	aFields['zipcode'];
													this.oZipcodeAdditive	=	aFields['zipcode_additive'];
													this.oHousenumber		=	aFields['housenumber'];
													this.oCountrySelect		=	aFields['country'];
													this.oStreet			=	aFields['street'];
													this.oCity				=	aFields['city'];
													this.oAdditiveAddress	=	aFields['additive_address'];
													this.oForeignAddress	=	aFields['foreign_address'];
												}

		this.init							=	function()
												{
													var oReference					=	this;
													this.oZipcode.onkeydown			=	function(oEvent)
																						{
																							oEvent = (oEvent) ? oEvent : ((event) ? event : null);
																							if (oEvent)
																							{
																								iPosition = oThis.getSelectionStart(this);
																								iKeycode = oEvent.keyCode;
																								if	(	35 === iKeycode	//end key
																									)
																								{
																									oThis.setSelection(oThis.oZipcodeAdditive, 2);
																									oThis.oZipcodeAdditive.focus();
																								}
																								if	(	46 === iKeycode	//del key
																									&&	this.value.length === iPosition
																									&&	oThis.oZipcodeAdditive.value.length > 0
																									)
																								{
																									oThis.oZipcodeAdditive.focus();
																									oThis.setSelection(oThis.oZipcodeAdditive, 0);
																								}
																								if	(	39 === iKeycode	//right
																									&&	this.value.length === iPosition
																									&&	iPosition < 4
																									)
																								{
																									oThis.oZipcodeAdditive.focus();
																									oThis.setSelection(oThis.oZipcodeAdditive, 0);
																								}
																							}
																						}
													this.oZipcode.onkeyup			=	function(oEvent)
																						{
																							oEvent = (oEvent) ? oEvent : ((event) ? event : null);
																							if (oEvent)
																							{
																								iPosition = oThis.getSelectionStart(this);
																								iKeycode = oEvent.keyCode;
																								if	(	37 !== iKeycode	//left
																									&&	38 !== iKeycode	//up
																									&&	39 !== iKeycode	//right
																									&&	40 !== iKeycode	//down
																									&&	4 === iPosition
																									)
																								{
																									oThis.oZipcodeAdditive.focus();
																									oThis.setSelection(oThis.oZipcodeAdditive, 0, 2);
																								}
																								if	(	39 === iKeycode	//right
																									&&	4 === iPosition
																									)
																								{
																									oThis.oZipcodeAdditive.focus();
																									oThis.setSelection(oThis.oZipcodeAdditive, 0);
																								}
																							}
																						}
													this.oZipcodeAdditive.onkeydown	=	function(oEvent)
																						{
																							oEvent = (oEvent) ? oEvent : ((event) ? event : null);
																							if (oEvent)
																							{
																								iPosition = oThis.getSelectionStart(this);
																								iKeycode = oEvent.keyCode;
																								if	(	36 === iKeycode	//home key
																									)
																								{
																									oThis.oZipcode.focus();
																									oThis.setSelection(oThis.oZipcode, 0);
																								}
																								if	(	8 === iKeycode	//backspace
																									&&	0 === iPosition
																									)
																								{
																									oThis.oZipcode.focus();
																									oThis.setSelection(oThis.oZipcode, 4);
																								}
																							}
																						}
													this.oZipcodeAdditive.onkeyup	=	function(oEvent)
																						{
																							oEvent = (oEvent) ? oEvent : ((event) ? event : null);
																							if (oEvent)
																							{
																								iPosition = oThis.getSelectionStart(this);
																								iKeycode = oEvent.keyCode;
																								if	(	(	37 === iKeycode	//left
																										||	8 === iKeycode	//backspace
																										)
																									&&	0 === iPosition
																									)
																								{
																									oThis.oZipcode.focus();
																									oThis.setSelection(oThis.oZipcode, 4);
																								}
																							}
																						}
													this.oZipcode.onchange			=	function()
																						{
																							oReference.isValid(oReference.oZipcode, oReference.aSettings);
																						}
													this.oZipcodeAdditive.onchange	=	function()
																						{
																							oReference.isValid(oReference.oZipcode, oReference.aSettings);
																						}
													this.oHousenumber.onchange		=	function()
																						{
																							oReference.isValid(oReference.oZipcode, oReference.aSettings);
																						}
													this.oCountrySelect.onchange	=	function()
																						{
																							if (this.options[this.selectedIndex].value == 121)
																							{
																								if ( null !== oReference.oForeignAddress )
																								{
																									oFormRowHouseNumber		= getFirstParentByClassName( oReference.oHousenumber, 'formrow' );
																									oFormRowZipcode			= getFirstParentByClassName( oReference.oZipcode, 'formrow' );
																									oFormRowStreet			= getFirstParentByClassName( oReference.oStreet, 'formrow' );
																									oFormRowCity			= getFirstParentByClassName( oReference.oCity, 'formrow' );
																									oFormRowAdditiveAddress	= getFirstParentByClassName( oReference.oAdditiveAddress, 'formrow' );
																									oFormRowForeignAddress	= getFirstParentByClassName( oReference.oForeignAddress, 'formrow' );

																									oFormRowHouseNumber.style.display		= '';
																									oFormRowZipcode.style.display			= '';
																									oFormRowStreet.style.display			= '';
																									oFormRowCity.style.display				= '';
																									oFormRowForeignAddress.style.display	= 'none';
																									if (oFormRowAdditiveAddress)
																									{
																										oFormRowAdditiveAddress.style.display	= '';
																									}
																								}

																								oReference.oZipcode.maxLength							=	4;
																								oReference.oZipcodeAdditive.maxLength					=	2;
																								oReference.oZipcodeAdditive.style.display				=	'';
																								oReference.oStreet.readOnly								=	true;
																								oReference.oCity.readOnly								=	true;
																							}
																							else
																							{
																								if ( null !== oReference.oForeignAddress )
																								{
																									oFormRowHouseNumber		= getFirstParentByClassName( oReference.oHousenumber, 'formrow' );
																									oFormRowZipcode			= getFirstParentByClassName( oReference.oZipcode, 'formrow' );
																									oFormRowStreet			= getFirstParentByClassName( oReference.oStreet, 'formrow' );
																									oFormRowCity			= getFirstParentByClassName( oReference.oCity, 'formrow' );
																									oFormRowAdditiveAddress	= getFirstParentByClassName( oReference.oAdditiveAddress, 'formrow' );
																									oFormRowForeignAddress	= getFirstParentByClassName( oReference.oForeignAddress, 'formrow' );

																									oFormRowHouseNumber.style.display		= 'none';
																									oFormRowZipcode.style.display			= 'none';
																									oFormRowStreet.style.display			= 'none';
																									oFormRowCity.style.display				= 'none';
																									oFormRowForeignAddress.style.display	= '';
																									if (oFormRowAdditiveAddress)
																									{
																										oFormRowAdditiveAddress.style.display	= 'none';
																									}
																								}
																								else
																								{
																									oReference.oZipcode.maxLength						=	10;
																									oReference.oZipcodeAdditive.maxLength				=	10;
																									oReference.oZipcodeAdditive.style.display			=	'none';
																									oReference.oStreet.readOnly							=	false;
																									oReference.oCity.readOnly							=	false;
																								}
																							}
																						}

													if (this.oCountrySelect.options[this.oCountrySelect.selectedIndex].value == 121)
													{
														if ( null !== this.oForeignAddress )
														{
															var oFormRowForeignAddress	= getFirstParentByClassName( this.oForeignAddress, 'formrow' );
															if (null !== oFormRowForeignAddress && false !== oFormRowForeignAddress)
															{
																oFormRowForeignAddress.style.display	= 'none';
															}
														}

														this.oZipcode.maxLength							=	4;
														this.oZipcodeAdditive.maxLength					=	2;
														this.oZipcodeAdditive.style.display				=	'';
														this.oStreet.readOnly							=	true;
														this.oStreet.tabIndex							=	-1;
														this.oCity.readOnly								=	true;
														this.oCity.tabIndex								=	-1;
													}
													else
													{
														if ( null !== this.oForeignAddress )
														{
															oFormRowHouseNumber		= getFirstParentByClassName( this.oHousenumber, 'formrow' );
															oFormRowZipcode			= getFirstParentByClassName( this.oZipcode, 'formrow' );
															oFormRowStreet			= getFirstParentByClassName( this.oStreet, 'formrow' );
															oFormRowCity			= getFirstParentByClassName( this.oCity, 'formrow' );
															oFormRowAdditiveAddress	= getFirstParentByClassName( this.oAdditiveAddress, 'formrow' );
															oFormRowForeignAddress	= getFirstParentByClassName( this.oForeignAddress, 'formrow' );

															oFormRowHouseNumber.style.display		= 'none';
															oFormRowZipcode.style.display			= 'none';
															oFormRowStreet.style.display			= 'none';
															oFormRowCity.style.display				= 'none';
															oFormRowForeignAddress.style.display	= '';
															if (oFormRowAdditiveAddress)
															{
																oFormRowAdditiveAddress.style.display	= 'none';
															}
														}
														else
														{
															this.oZipcode.maxLength							= 10;
															this.oZipcodeAdditive.maxLength					= 10;
															this.oZipcodeAdditive.style.display				= 'none';
															this.oStreet.readOnly							= false;
															this.oCity.readOnly								= false;
														}
													}
												}

		this.fillZipcode					=	function(sResponse)
												{
													if ( -1 !== sResponse.indexOf( "|" ) )
													{
														var aData 					= sResponse.split( "|" );
														this.oStreet.value			= aData[0];
														this.oCity.value			= aData[1];
														this.oZipcodeAdditive.value	= this.oZipcodeAdditive.value.toUpperCase();
														return true;
													}
													else
													{
														var sErrorMessage	= "Invalid combination";
														if ("function" === typeof(Translate))
														{
															sErrorMessage = Translate(sErrorMessage);
														}
														this.oStreet.value	= sErrorMessage;
														this.oCity.value	= "";
														return false;
													}
												}
		this.getSelectionStart				=	function(oInput)
												{
													if (oInput.createTextRange)
													{
														var oRange = document.selection.createRange().duplicate();
														oRange.moveEnd('character', oInput.value.length);
														if ('' === oRange.text)
														{
															return oInput.value.length;
														}
														return oInput.value.lastIndexOf(oRange.text);
													}
													else
													{
														return oInput.selectionStart;
													}
												}
		this.setSelection					=	function(oInput, iStart, iEnd)
												{
													if (!iEnd)
													{
														iEnd = iStart;
													}
													if (oInput.createTextRange)
													{
														var oRange = oInput.createTextRange();
														oRange.collapse(true);
														oRange.moveEnd('character', iEnd);
														oRange.moveStart('character', iStart);
														oRange.select();
													}
													else if (oInput.setSelectionRange)
													{
														oInput.focus();
														oInput.setSelectionRange(iStart, iEnd);
													}
												}
	}
