/**
 * Satelliet Catalog Request
 * 
 * 
 * @category JavaScript/Ext
 * @package MaxJSComponents
 * @copyright Copyright (c) 2009 MaxServ B.V. Netherlands
 *            (http://www.maxserv.com)
 * @license Commercial
 * @author Arno Schoon (arno@maxserv.nl)
 * @description This script contains all JS logics for the form on the satelliet
 *              website
 * @version 1.0
 * @requires ExtJs 3.x library
 */

// reference local blank image
Ext.BLANK_IMAGE_URL = '../../ext/resources/images/default/s.gif';

// create namespace
Ext.namespace('satelliet');

// create application
satelliet.catalogRequest = function() {
	// private variables
	
	// private functions
	
	// public space
	return {
		// public properties, e.g. strings to translate
		btn1Text: 'Button 1',
		
		// public methods
		init: function() {
			Ext.apply(maxServ.slide.options, {
				before: {
					fn: this.validate,
					scope: this
				}
			});
			maxServ.slide.init();
			/*
			 * var elm =
			 * Ext.get('user_satelliet_pi3_whattosend_gehele_boekwerk'); var fe =
			 * new maxServ.formElement(elm, {});
			 * 
			 * fe.on('click', function(e){ fe.toggle(); });
			 */
			var form = new maxServ.form('user_satelliet_pi3_form');
		},
		
		validate: function(o){
			var currentSlideID = o.slide.current.dom.id;
			var errCount = 0;
			var errMsgs = [];
			
			errMsgs.push('An error has occured');
			
			switch(currentSlideID){
				case 'user_satelliet_pi3_slide-1':
					var c = 0;
					
					Ext.select('.productgroups input[type=checkbox]', true, currentSlideID).each(function(e){
						var confirm = Ext.get(e.dom.id + '_confirm');
						confirm.setVisibilityMode(Ext.Element.DISPLAY);
						if(!e.dom.checked){
							confirm.hide();
						} else {
							confirm.show();
							c++;
						}
					});
					
					if(c == 0){
						errCount++;
						errMsgs.push('Choose a section');
					}
				break;
				case 'user_satelliet_pi3_slide-2':
					Ext.select('input[type=text]', false, currentSlideID).each(function(e){
						if(e.is('.required') && e.getValue() == ''){
							errCount++;
							errMsgs.push('The field "' + e.prev('label').dom.innerHTML + '" is not correctly filled in');
						} else {
							Ext.fly(e.dom.id + '_confirm').update(e.getValue());
						}
					});
					
					var email = Ext.get('user_satelliet_pi3_email').getValue();
					var theRegEx_isValid = new RegExp("^.+\@[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})$","");
					if (!theRegEx_isValid.test(email)){
						errCount++;
						errMsgs.push('The entered e-mailaddress is not valid');
					}

					var salutation = Ext.select('.user_satelliet_pi3_salutation .selecttext', true, currentSlideID).first().dom.innerHTML;
					if(salutation == ''){
						errCount++;
						errMsgs.push('You didn\'t select a title');
					}					
					Ext.fly('user_satelliet_pi3_salutation_confirm').update(salutation);

					var country = Ext.select('.user_satelliet_pi3_country .selecttext', true, currentSlideID).first().dom.innerHTML;
					if(country == 'Select your country' || country == '----------'){
						errCount++;
						errMsgs.push('You didn\'t select a country');
					}					
					Ext.fly('user_satelliet_pi3_country_confirm').update(country);
					
					var branche = Ext.select('.user_satelliet_pi3_branche .selecttext', true, currentSlideID).first().dom.innerHTML;
					if(branche == ''){
						errCount++;
						errMsgs.push('You did not select a "Customer type"');
					}	
					
					var referer = Ext.select('.user_satelliet_pi3_referer .selecttext', true, currentSlideID).first().dom.innerHTML;
					if(referer == ''){
						errCount++;
						errMsgs.push('You haven\'t specified how you found us');
					}	
					
				break;
			}
			
			if(errCount){
				this.showMsg(errMsgs);
				return false;
			} else {
				return true
			}
		},
		
		showMsg: function(a){
			var _body = Ext.get('satelliet_body');
			var err = _body.createChild({tag: 'ul'});
			
			err.setVisibilityMode(Ext.Element.DISPLAY);
			err.hide();
			
			for(i = 0; i < a.size(); i++){
				Ext.get(err).createChild({tag: 'li', cls: 'err-msg', html: a[i]}, null, true);
			}
			
			Lightview.show({
				href: Ext.id(err),
				rel: 'inline',
				title: 'Notice',
				options: {
					autosize: true,
					topclose: true
				}
			});
			
		}
	};
}(); // end of form

Ext.onReady(satelliet.catalogRequest.init, satelliet.catalogRequest);