ICL.ui.Signup = function() {
	
	return {
		
		validator: {},
		
		initNormal: function() {
			this.init();
		},
		
		initNews: function() {
			jQuery("#t_company_desc").keyup(function(){
				ICL.limitChars(jQuery(this), 500, 'cd_info');
			});
			
			this.init();
		},
		
		initSecurity: function() {
			jQuery("#t_company_desc").keyup(function(){
				ICL.limitChars(jQuery(this), 500, 'cd_info');
			});
			
			this.init();
		},
		
		checkemail: function (str) {
			var testresults = false;
			var filter = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (filter.test(str)) {
				testresults = true
			}
			delete filter;
			return testresults;
		},
		
		init: function() {
			this.validator = jQuery("#signup_form").validate({
				submitHandler: function(form){
					var continueSubmit = true;
					var companyEmail = jQuery('#companyemail').val();
					var email = jQuery('#tEmail').val();
					
					if (companyEmail != null) {
						if (!ICL.ui.Signup.checkemail(companyEmail)) {
							continueSubmit = false;
							ICL.ui.Signup.validator.showErrors({tCompanyEmail: 'Please input a valid company e-mail address'});
						}
					}
					
					if (email != null) {
						if (!ICL.ui.Signup.checkemail(email)) {
							continueSubmit = false;
							ICL.ui.Signup.validator.showErrors({tEmail: 'Please input a valid e-mail address'});
						}
					}
					
					if (continueSubmit) {
						form.submit();
					}
					
					delete continueSubmit;
					delete companyEmail;
					delete email;
					
					return false;
				},
				errorPlacement: function(error, element) {
					switch(element.attr('name')) {
						case 'tFirstStreetAddress':
							error.appendTo( element.next().next().next(".sys_error_field") );
						break;
						case 'tTOS':
							error.appendTo( element.parent().parent().next(".sys_error_field") );
						break;
						default:
							error.appendTo( element.next(".sys_error_field") );
						break;
					}
				},
				messages: {
					tCompany: {
						required: 'Please input your company name'
					},
					tCompanyDesc: {
						required: 'Please input your company description'
					},
					tCompanyContactNumber: {
						required: 'Please input your company contact number'
					},
					tCompanyEmail: {
						required: 'Please input your company e-mail address',
						email: 'Please input a valid company e-mail address'
					},
					tCompanyWebsite: {
						required: 'Please input your company website'
					},
					tFirstName: {
						required: 'Please input your first name'
					},
					tSurname: {
						required: 'Please input your surname'
					},
					tEmail: {
						required: 'Please input your e-mail address',
						email: 'Please input a valid e-mail address'
					},
					tFirstStreetAddress: {
						required: 'Please input your street address (held private)'
					},
					tCity: {
						required: 'Please input your city or town'
					},
					tTOS: {
						required: 'Please indicate that you understand the terms and condition'
					}
				}
			});
			
			/*jQuery('#t_tos').click(function(){
				if (this.checked) {
					jQuery('#sys_submit')[0].disabled = false;
				} else {
					jQuery('#sys_submit')[0].disabled = true;
				}
			});*/
			
			jQuery('#sys_view_radius').click(function(){
				ICL.ui.Signup.drawCircle();
			});
			
			jQuery('#sys_radius').change(function(){
				ICL.ui.Signup.drawCircle();
			});
			
			jQuery('#sys_radius_label').change(function(){
				ICL.ui.Signup.drawCircle();
			});
			
			/* INITIATE MAP CREATION WITH BUNCH OF OPTIONS */
			var countryContainer =  jQuery('#sys_country');
			this.drawMap(DEFAULT_LATITUDE, DEFAULT_LONGITUDE, countryContainer);
			
			countryContainer.change(function(){
				var country = this.value.split('-');
				var countryText = this.options[this.options.selectedIndex].text;
				
				ICL.ui.Map.setAddress({
					countryiso3: country[1],
					country: countryText,
					zoomSize: 8,
					addSelectMarker: true,
					callback: function(){
						if (ICL.ui.Map.newMarker != null) {
							ICL.ui.Map.markerClick(ICL.ui.Map.newMarker, {
								object: $('<div>' + jQuery('#sys_address').val() + '<br/>' + jQuery('#sys_city').val() + '<br/>' + countryText + '</div>')
							});
							ICL.ui.Map.drawCircle(new GLatLng(jQuery('#sys_latitude').val(), jQuery('#sys_longitude').val()), jQuery('#sys_radius').val());
						}
					}
				});
				
				delete country;
				delete countryText;
			});
			
			jQuery('#sys_address').blur(function(){
				var country = countryContainer.val().split('-');
				var tempFAddress = jQuery('#sys_address_temp');
				
				if (this.value != tempFAddress.val()) {
					tempFAddress.val(this.value);
				
					ICL.ui.Map.setAddress({
						countryiso3: country[1],
						country: countryContainer[0].options[countryContainer[0].options.selectedIndex].text,
						city: jQuery('#sys_city').val(),
						address: jQuery('#sys_address').val(),
						zoomSize: 13,
						addSelectMarker: true,
						callback: function(){
							if (ICL.ui.Map.newMarker != null) {
								ICL.ui.Map.markerClick(ICL.ui.Map.newMarker, {
									object: $('<div>' + jQuery('#sys_address').val() + '<br/>' + jQuery('#sys_city').val() + '<br/>' + countryContainer[0].options[countryContainer[0].options.selectedIndex].text + '</div>')
								});
							}
						}
					});
				}
				
				delete country;
				delete tempFAddress;
			});
			
			jQuery('#sys_city').blur(function(){
				var country = countryContainer.val().split('-');
				var tempCity = jQuery('#sys_city_temp');
				
				if (this.value != tempCity.val()) {
					tempCity.val(this.value);
				
					ICL.ui.Map.setAddress({
						countryiso3: country[1],
						country: countryContainer[0].options[countryContainer[0].options.selectedIndex].text,
						city: this.value,
						address: jQuery('#sys_address').val(),
						zoomSize: 13,
						addSelectMarker: true,
						callback: function(){
							if (ICL.ui.Map.newMarker != null) {
								ICL.ui.Map.markerClick(ICL.ui.Map.newMarker, {
									object: $('<div>' + jQuery('#sys_address').val() + '<br/>' + jQuery('#sys_city').val() + '<br/>' + countryContainer[0].options[countryContainer[0].options.selectedIndex].text + '</div>')
								});
							}
						}
					});
				}
				
				delete country;
				delete tempCity;
			});
			
			jQuery('#sys_fix_marker').click(function(){
				var latitude = jQuery('#sys_latitude').val();
				var longitude = jQuery('#sys_longitude').val();
				
				ICL.ui.Map.init({
					defaultLocationLatLng: [latitude, longitude],
					defaultZoomSize: 15,
					container: 'detail_map_canvas',
					width: 600,
					height: 350,
					mapControl: 'large',
					enableOverviewMapControl: true,
					icons: {
						pin_point: IMG_PATH + 'pin_point.png'
					},
					addEventClick: true,
					selectMarkerDraggable: true,
					markerDragend: function(GLatLng) {
						ICL.ui.Signup.drawCircle(GLatLng);
					},
					form: {
						enable: true,
						useInfoWindow: false,
						field: {
							latitude: [{id: 'sys_fix_latitude', type: 'text'}],
							longitude: [{id: 'sys_fix_longitude', type: 'text'}]
						}
					},
					objReport: jQuery('#map_message')
				});
				
				ICL.ui.Map.createSelectMarker(ICL.ui.Map.getPoint(latitude, longitude), ICL.ui.Map.setIcon(IMG_PATH + 'pin_point.png'));
				
				ICL.ui.Signup.drawCircle();
				
				delete countryContainer;
				delete latitude;
				delete longitude;
			});
		},
		
		saveFix: function() {
			var latitude = jQuery('#sys_fix_latitude');
			var longitude = jQuery('#sys_fix_longitude');
			
			jQuery('#sys_latitude').val(latitude.val());
			jQuery('#sys_longitude').val(longitude.val());
			
			this.drawMap(latitude.val(), longitude.val());
			
			latitude.val('');
			longitude.val('');
			
			delete latitude;
			delete longitude;
		},
		
		drawMap: function(latitude, longitude, countryContainer) {
			ICL.ui.Map.init({
				defaultLocationLatLng: [latitude, longitude],
				defaultZoomSize: 13,
				container: 'map_canvas',
				width: 330,
				height: 300,
				mapControl: 'small',
				enableOverviewMapControl: false,
				icons: {
					pin_point: IMG_PATH + 'pin_point.png'
				},
				addEventClick: false,
				selectMarkerDraggable: false,
				form: {
					enable: true,
					useInfoWindow: false,
					field: {
						fullAddress: [{id: 'sys_full_address', type: 'text'}],
						country: [{id: 'sys_country_iso3', type: 'select'}],
						latitude: [{id: 'sys_latitude', type: 'text'}],
						longitude: [{id: 'sys_longitude', type: 'text'}]
					}
				},
				objReport: jQuery('#map_message')
			});
			
			ICL.ui.Map.createSelectMarker(ICL.ui.Map.getPoint(latitude, longitude), ICL.ui.Map.setIcon(IMG_PATH + 'pin_point.png'));
			var countryContainer =  jQuery('#sys_country');
			ICL.ui.Map.markerClick(ICL.ui.Map.newMarker, {object: $('<div>' + jQuery('#sys_address').val() + '<br/>' + jQuery('#sys_city').val() + '<br/>' + countryContainer[0].options[countryContainer[0].options.selectedIndex].text + '</div>')});
			delete countryContainer;
			
			ICL.ui.Signup.drawCircle();
		},
		
		drawCircle: function(LatLng) {
			var radius = jQuery('#sys_radius').val();
			if (jQuery('#sys_radius_label').val() == 2) {
				radius = new Number( radius * new Number(0.6214) );
			}
			
			if (LatLng == null) {
				var latitude = jQuery('#sys_fix_latitude').val();
				if (latitude == '') {
					latitude = jQuery('#sys_latitude').val();
				}
				var longitude = jQuery('#sys_fix_longitude').val();
				if (longitude == '') {
					longitude = jQuery('#sys_longitude').val();
				}
				
				LatLng = new GLatLng(latitude, longitude);
				
				delete latitude;
				delete longitude;
			}
			
			ICL.ui.Map.drawCircle(LatLng, radius);
		}
		
	}
	
}();