//trim function
String.prototype.trim = function() {
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

var WindowDelegate = Class.create();
	WindowDelegate.prototype = {
		initialize: function() {
		},
			
		canClose: function(win) {
			// return false, the window cannot be closed
			return false;
		},
		
		canMove: function(win) {
			return false;
		}
	}

var ContactApp = Class.create();
ContactApp.prototype = {
		initialize: function() {
			this.origPosX = 500;
			this.origPosY = 0;
			
			this.contactWindowWidth = 266;
			this.contactWindowMinHeight = 550;
			
			this.alertWidth = 300;
			this.alertHeight = 100;
			
			this.windowTitle = "Связаться с нами";
			this.successMsg = "<b>Ваше сообщение успешно отправлено</b>";
			this.errorMsg = "<b>Ошибка отправки, попробуйте позже</b>";
			this.validMsg = "<b>Пожалуйста корректно заполните все поля</b>";
			
			this.emailError =  '<img src="fail.gif" width="12" height="12" align="absmiddle"> Ошибка';
			this.sendUrl = "send_form.php";
			
			this.eventLoad = this.loadHandler.bindAsEventListener(this);
			this.eventResize= this.resizeHandler.bindAsEventListener(this)
			this.eventSendForm = this.sendForm.bindAsEventListener(this);
			this.eventCompleted = this.sendCompleted.bindAsEventListener(this);
			
			Event.observe(window, "load", this.eventLoad); 
			Event.observe(window, "resize", this.eventResize);
		},
		
		loadHandler: function(e) {
			this.showContactForm();
			Event.observe($('send_but'), "click", this.eventSendForm);
		},
		
		resizeHandler: function(e) {
			height = Math.max(document.documentElement.clientHeight, this.contactWindowMinHeight)-54;
			win.setSize(this.contactWindowWidth, height);
		},
		
		sendForm: function() {
			if($('email_msg').innerHTML || !$F('username').trim() || !$F('message').trim()) {
				Dialog.alert(this.validMsg, {windowParameters: {width:300, height:100}, okLabel: "Ok"});
				return;
			}
			frmData = "username=" + encodeURIComponent($F('username')) + "&email=" + encodeURIComponent($F('email')) + "&phone="+encodeURIComponent($F('phone')) + "&message=" + encodeURIComponent($F('message'));
			req = new Ajax.Request( this.sendUrl, {method: 'post', postBody: frmData, onComplete: this.eventCompleted });
		},
		
		sendCompleted: function(reqObj) {
			if (reqObj.responseText == '1') {
					Windows.getWindow('contact_us').hide();
					Dialog.alert(this.successMsg, {windowParameters: {width:300, height:100}, okLabel: "Ok"});
			} else {
					Dialog.alert(this.errorMsg + reqObj.responseText, {windowParameters: {width:300, height:100}, okLabel: "Ok"});
			}		
		},
		
		showContactForm: function() {
			height = Math.max(document.documentElement.clientHeight, this.contactWindowMinHeight)-54;
			win = new Window('contact_us', {top: this.origPosY, left: this.origPosX, width: this.contactWindowWidth, height: height, zIndex: 100, resizable: false, title: this.windowTitle, hideEffect: Effect.DropOut})
			win.setContent('contact_form');
			$('contact_form').style.display = 'block'
	
			Validator.register({
	    	"#email" : {
	      	  "/^([^@ ]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/": '',
						"/^$/": '',	      	  
	      	  "/.*/": this.emailError
	    	}
	    });
			
			// Set up a deleagte for win
			delegate = new WindowDelegate();
			win.setDelegate(delegate);
			win.toFront();
			win.show();
			Behaviour.apply();
		}
}

var app = new ContactApp();
//app.showContactForm();