Webcallback = function(config) {
	$.extend(this, config);
	$(document).ready(function() { this.init(); } .createDelegate(this));
}

Webcallback.prototype = {
	formId: null
	, inputEl: null
	, errorEl: null
	, submitEl: null
	, sendEl: null
	, url: 'http://v4.viatelecom.com/webcall.php?action=wcb&sid={0}&gid={1}&b={2}&ref={3}'
    , sid: '674'
    , gid: '33842'
    , ref: 'mmfr'
	, confirmUrl: null
	, pauseSec: 15
	, country: null
	, businessHours: { from: '8:30', to: '18:00' }
    , initText: null
    , invalidText: "Invalid phone"
	, validText: ""
    , nightText: "Out of business hours"
    , pauseText: "Your request is already accepted"
	, init: function() {
		if (this.formId) {
			this.inputEl = $(this.formId).children(':input[type!=submit]');
			this.errorEl = $(this.formId).children('.error');
			this.submitEl = $(this.formId).children(':input[type=submit]');
			this.sendEl = $(this.formId).children('iframe');
		}
		if (this.businessHours) {
			var start = new Date('1970/01/01 ' + this.businessHours.from + ':00 UTC+0200');
			this.businessHours.start = { hour: start.getUTCHours(), minute: start.getUTCMinutes() };
			var end = new Date('1970/01/01 ' + this.businessHours.to + ':00 UTC+0200');
			this.businessHours.end = { hour: end.getUTCHours(), minute: end.getUTCMinutes() };
		}
		if (this.isBusinessHour()) {
			this.inputEl.focus(this.inputFocus.createDelegate(this));
			this.inputEl.keypress(this.inputKeypress.createDelegate(this));
			if (this.initText)
				this.inputEl.val(this.initText);
			this.submitEl.click(this.submit.createDelegate(this));
		}
		else {
			this.inputEl.attr('disabled', 'disabled');
			this.submitEl.attr('disabled', 'disabled');
			this.errorEl.html(this.nightText);
		}
	}
	, inputFocus: function() {
		if (this.initText && this.inputEl.val() == this.initText)
			this.inputEl.val('');
	}
	, inputKeypress: function(e) {
		var key = (e.which) ? e.which : e.keyCode;
		if (key == 13) {
			e.preventDefault();
			this.submit();
		}
		else if (key > 57) // not a digit or cmd
			e.preventDefault();
	}
	, validatePhone: function(phone) {
		var phoneOK = true;
		var temp;
		switch (this.country) {
			case 'F':
				if ((temp = phone.match(/^[+]33(.*)$/))) // prepeare if +33 format
					phone = "0" + temp[1];
				if ((temp = phone.match(/^0033(.*)$/))) // prepeare if 0033 format
					phone = "0" + temp[1];
				phone = phone.replace(/\D/g, ''); //extract numbers
				if (!phone.match(/^0[12345689][\d]{8}$/) || phone.match(/^089/)) // check 01 23 45 67 89 format and exclude numbers starting with 089
					phoneOK = false;
				break;
			case 'D':
				if ((temp = phone.match(/^[+]49(.*)$/)))
					phone = "0" + temp[1];
				if ((temp = phone.match(/^0049(.*)$/)))
					phone = "0" + temp[1];
				phone = phone.replace(/\D/g, '');
				if (phone.length < 5)
					phoneOK = false;
				break;
			case 'CH':
				if ((temp = phone.match(/^\s*(?:[+]|00)41(.*)$/)))
					phone = "0" + temp[1];
				phone = phone.replace(/\D/g, '');
				if (!phone.match(/^0(1|2[12467]|3[1234]|4[134]|5[256]|6[12]|7[1689]|81|91)[\d]{7}$/))
					phoneOK = false;
				break;
			default:
				alert('Unkonwn country ' + this.country);
		}
		return phoneOK;//val.match(/^0[12345689][\d]{8}$/) && !val.match(/^089/);
	}
	, submit: function(event) {
		if (event) event.preventDefault();
		var val = this.getNormalizedValue();
		var ok = this.validatePhone(val); // check 01 23 45 67 89 format and exclude numbers starting with 089
		if (!ok)
			this.errorEl.html(this.invalidText);
		else if (this.submitEl.attr('disabled'))
			this.errorEl.html(this.pauseText);
		else {
			this.errorEl.html(this.validText);
			if (this.pauseSec > 0) {
				this.inputEl.attr('disabled', 'disabled');
				this.submitEl.attr('disabled', 'disabled');
				setTimeout(this.enableSubmit.createDelegate(this), this.pauseSec * 1000);
			}
			var url = this.url.format(this.sid, this.gid, this.getNormalizedValue(), this.ref);
			this.sendEl.attr('src', url);
			//alert(url);
		}
	}
	, enableSubmit: function() {
		this.inputEl.removeAttr('disabled');
		this.submitEl.removeAttr('disabled');
	}
	, isBusinessHour: function() {
		if (!this.businessHours)
			return true;
		var now = new Date();
		var hour = now.getUTCHours();
		var minute = now.getUTCMinutes();
		var rslt =
			(hour >= this.businessHours.start.hour || (hour == this.businessHours.start.hour && minute >= this.businessHours.start.minute)) &&
			(hour < this.businessHours.end.hour || (hour == this.businessHours.end.hour && minute < this.businessHours.end.minute));
		return rslt;
	}
	, getValue: function() { return this.inputEl.val(); }
	, getNormalizedValue: function() {
		var val = this.getValue();
		var temp;
		if ((temp = val.match(/^[+]33(.*)$/))) // prepeare if +33 format
			val = "0" + temp[1];
		if ((temp = val.match(/^0033(.*)$/))) // prepeare if 0033 format
			val = "0" + temp[1];
		val = val.replace(/\D/g, ''); //extract numbers
		return val;
	}
};

Function.prototype.createDelegate = function(scope) {
	var fn = this;
	return function() {
		return fn.apply(scope, arguments);
	}
}

String.prototype.format = function() {
	var pattern = /\{\d+\}/g;
	var args = arguments;
	return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}	
