/**
 * Sidebar Elements,
 * written by b:dreizehn, Stuttgart
 */

function switchBox(el, counterpart) { return ServiceMessage.switchSidebarBox(el, counterpart); }
function createBookmark() { return ServiceMessage.addBookmark(); }

var ServiceMessage = {
	init: function() {

		// check if we should fill out the login box
		if ($('sidebar-login') || $('form-sidebar-login')) {
			this.requestPermanentLogin();
		}

		var pos = 0;
		// see if we need to scroll
		$$('.sidebar-box-content-with-scrollbar-left').each(function(el) {
			var cont = $(el).getNext();
			var fwd  = $(cont).getElements('.scrollforward');
			var back = $(cont).getElements('.scrollback');
			fwd.setProperty('id',  'scrollerfwd_'  + pos);
			back.setProperty('id', 'scrollerback_' + pos);
			fwd  = $('scrollerfwd_'  + pos);
			back = $('scrollerback_' + pos);
			var slider = new MooSlider($(el), back, fwd);
			pos++;
		});
	},


	requestPermanentLogin: function() {
		if ("https:" != document.location.protocol) {
			var username = Cookie.get('username');
			var password = Cookie.get('password');
			this.fillPermanentLogin(username, password);
		}
	},


	fillPermanentLogin: function(username, password) {
		if (username && password) {
			username = decodeURI(username);
			password = decodeURI(password);
			if ($('sidebar-login')) {
				$('sidebar-username').setProperty('value', username);
				$('sidebar-password').setProperty('value', password);
			}
			if ($('form-login')) {
				$('xlogin').setProperty('value', username);
				$('xpw').setProperty('value', password);
			}
			this.toggleSaveLogin();
		}
	},

	savePermanentLogin: function(type) {
		var username, password, toggle = false;
		if (type && type == 'sidebar') {
			username = $('sidebar-username').value;
			password = $('sidebar-password').value;
			toggle = $('sidebar-save-toggle');
		} else {
			username = $('xlogin').value;
			password = $('xpw').value;
			toggle = $('save-toggle');
		}
		if (username && password && toggle && toggle.getProperty('value') == 'true') {
			Cookie.remove('username');
			Cookie.remove('password');
			Cookie.set('username', encodeURI(username), {duration: 365, path: '/'} );
			Cookie.set('password', encodeURI(password), {duration: 365, path: '/'} );
			toggle.setProperty('value', 'false');	// this is needed so the login.do doesn't have problems with umlauts
		} else if (toggle) {
			Cookie.remove('username');
			Cookie.remove('password');
		}
	},

	toggleSaveLogin: function() {
		var i = $('sidebar-save-toggle-image');
		var t = $('sidebar-save-toggle');
		if (t) {
			if (t.value != 'true') {
				t.setProperty('value', 'true');
				i.setProperty('src', 'fileadmin/templates/img/login-save-toggle-on.png');
			} else {
				t.setProperty('value', 'false');
				i.setProperty('src', 'fileadmin/templates/img/login-save-toggle-off.png');
			}
		}
		var i = $('save-toggle-image');
		var t = $('save-toggle');
		if (!t) return;
		if (t.value != 'true') {
			t.setProperty('value', 'true');
			i.setProperty('src', 'fileadmin/templates/img/login-save-grey-toggle-on.png');
		} else {
			t.setProperty('value', 'false');
			i.setProperty('src', 'fileadmin/templates/img/login-save-grey-toggle-off.png');
		}
		return false;
	},

	addBookmark: function() {
		var title = 'automobil-message.de'; 
		var url = document.URL;
		if (window.ie) {	// IE Favorite
			window.external.AddFavorite(url, title);
		} else if (window.sidebar) {	// Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url, '');
		} else if (window.opera && window.print) {	// Opera Hotlist
			return true;
		}
	},

	switchSidebarBox: function(el, counterpart) {
		if ($(el).getStyle('display') != 'none') {
			$(el).hide();
			$(counterpart).show();
		} else {
			$(counterpart).hide();
			$(el).show();
		}
		return false;
	}

}

window.addEvent('load', function() {
	ServiceMessage.init();
});


var ServiceSMS = {
	demoURL: 'http://www.automobil-message.de/am/DemoSMS.do',
	loading: 'testsms-submitting',

	send: function() {
		if ($(this.loading).getStyle('display') == 'block') {
			return false;
		}
		$(this.loading).setStyle('display', 'block');
		var params = {
			tempNum: $('template-type').value,
			phoneNumber: $('demo-prefix').value + $('demo-phone').value
		}
		var rq = new Ajax(this.demoURL, {
			method: 'get',
			data: params,
			onSuccess: this.evaluate.bind(this)
		}).request(); 
	},

	evaluate: function(txt, xml) {
		$(this.loading).setStyle('display', 'none');
		var response   = xml.getElementsByTagName('response')[0];
		var sendStatus = xml.getElementsByTagName('status')[0].childNodes[0].nodeValue;
		var message    = xml.getElementsByTagName('message')[0].childNodes[0].nodeValue;

		if (sendStatus != -1) {
			$('form-livetest').setStyle('display', 'none');
		}
		switch (sendStatus) {
			// 0 = success
			case '0':
				this.showContent('23');	// success header
				this.showContent('24');
				// errors
				this.hideContent('26');
				this.hideContent('31');
				this.hideContent('25');
				this.hideContent('30');
				this.hideContent('398');
				break;

			// -1 = Error: invalid number (Bitte überprüfen Sie die Korrektheit...)
			case '-1':
				this.showContent('26');	// error header
				var c = this.showContent('31');
				$ES('strong', c)[0].setHTML(' ' + $('demo-phone').value + ' ');
				break;

			// -2 = Error: max 5 sms to one number
			case '-2':
				this.showContent('26');	// error header
				this.showContent('25');
				break;

			// -3 = Error: max 15 sms to one IP address 
			case '-3':
				this.showContent('26');	// error header
				this.showContent('30');
				break;

			// -4 = Error: max 150 sms on one day
			case '-4':
				this.showContent('26');	// error header
				this.showContent('398');
				break;
		}
	},

	showContent: function(contentID) {
		var c = $('content_' + contentID).firstChild;
		if (!c) return;
		$(c).setStyle('display', 'block');
		return $(c);
	},

	hideContent: function(contentID) {
		var c = $('content_' + contentID).firstChild;
		if (!c) return;
		$(c).setStyle('display', 'none');
		return $(c);
	}


}


function waitCursor(id) {
	setCursor('wait', id);
	setCursor('wait');
}  

function autoCursor(id, value) {
	setCursor(value, id);
	setCursor('auto');
}

function setCursor(value, id) {
	if (!value) {
		value = 'auto';
	}
	var element = (id ? document.getElementById(id) : document.getElementsByTagName('body')[0]);
	element.style.cursor = value;
}

