document.u0 = '';
//document.u0 = '/cp';

function MsgBox(iname, y, rw) {
	this.iname = iname;
	this.panel_y = y;
	this.rw = rw;
	this.state = 0;
	this.progress = 0;
	this.step = 10;
	this.interval = 20;
	this.xy_offset = 5;

	this.panel = document.createElement('div');
	this.panel.style.display = 'none';
	this.panel.style.position = 'absolute';
	this.panel.style.left = '0px';
	this.panel.style.top = '0px';

	var tbl = document.createElement('table');
	this.panel.appendChild(tbl);
	tbl.setAttribute('cellSpacing', 0);
	tbl.setAttribute('cellPadding', 0);
	tbl.setAttribute('width', '100%');

	var tbody = document.createElement('tbody');
	tbl.appendChild(tbody);

	var tr = document.createElement('tr');
	tbody.appendChild(tr);
	this.make_td(tr, 'msgbox_tl', document.u0 + '/images/blank.gif');
	this.make_td(tr, 'msgbox_tc', '');
	this.make_td(tr, 'msgbox_tr', document.u0 + '/images/blank.gif');

	var tr = document.createElement('tr');
	tbody.appendChild(tr);

		var td = document.createElement('td');
		tr.appendChild(td);
		td.className = 'msgbox_ml';

		this.main_area = document.createElement('td');
		tr.appendChild(this.main_area);
		this.main_area.className = 'msgbox_mc';

		var td = document.createElement('td');
		tr.appendChild(td);
		td.className = 'msgbox_mr';

	var tr = document.createElement('tr');
	tbody.appendChild(tr);
	this.make_td(tr, 'msgbox_bl', document.u0 + '/images/blank.gif');
	this.make_td(tr, 'msgbox_bc', '');
	this.make_td(tr, 'msgbox_br', document.u0 + '/images/blank.gif');

	document.getElementsByTagName('body')[0].appendChild(this.panel);
}

MsgBox.prototype.make_td = function (tr, aclass, url) {
	var td = document.createElement('td');
	tr.appendChild(td);

	if (url != '') {
		td.setAttribute('width', 30);

		var img = document.createElement('img');
		td.appendChild(img);
		img.src = url;
		img.width = 30;
		img.height = 30;
	}
	else {
		td.innerHTML = '<div style="height:1px;overflow:hidden;">&nbsp;</div>';
	}

	td.className = aclass;
}


MsgBox.prototype.show = function () {
	this.state = 2;
	this.h_timer();
}

MsgBox.prototype.hide = function () {
	this.state = -1;
	this.h_timer();
}

MsgBox.prototype.h_timer = function () {
	if (this.state == -1) {
		this.progress -= this.step;
		if (this.progress <= 1) {
			this.progress = 0;
			this.state = 0;
		}
	}
	else if (this.state == 2) {
		this.progress += this.step;
		if (this.progress >= 100) {
			this.progress = 100;
			this.state = 1;
		}
	}

	if (this.state >= 1 && this.panel.style.display != 'block') {
		var obj = document.getElementById('wrapper');
		this.panel_x = 0;
		var w = obj.offsetWidth;
		while (obj) {
			this.panel_x += obj.offsetLeft;
			obj = obj.offsetParent;
		}
		this.panel_x += Math.round((w - this.rw) / 2);
	
		this.panel.style.top = this.panel_y + 'px';
		this.panel.style.left = this.panel_x + 'px';
		this.panel.style.width = this.rw + 'px';
		this.panel.style.filter = 'alpha(opacity=100)';
		this.panel.style.opacity = 1;
		this.panel.style.display = 'block';
	}
	else if (this.state == 0 && this.panel.style.display == 'block') {
		this.panel.style.display = 'none';
	}

	this.panel.style.filter = 'alpha(opacity=' + this.progress + ')';
	this.panel.style.opacity = this.progress / 100;
	this.panel.style.top = Math.round(this.panel_y + this.xy_offset - this.xy_offset * this.progress / 100) + 'px';
	this.panel.style.left = Math.round(this.panel_x + this.xy_offset - this.xy_offset * this.progress / 100) + 'px';

	if (this.state == -1 || this.state == 2)
		setTimeout('document["' + this.iname + '"].h_timer()', this.interval);
}

MsgBox.prototype.is_visible = function () {
	return this.panel.style.display == 'block';
}

function msg_init() {
	document.loaded = true;
}

if (window.addEventListener)
	window.addEventListener('load', msg_init, false);
else if (window.attachEvent)
	window.attachEvent('onload', msg_init);


document.strs = {
	email: 'Email',
	subject: 'Subject',
	message: 'Message',
	send: 'Send',
	close: 'Close',
	loading_page: 'Please wait until the page is loaded.',
	need_ajax: 'Critical Error: Your browser was unable to initialize the AJAX sub-system',
	http_error: 'HTTP error status',
	empty_msg: 'Error: Message is required',
	empty_email: 'The email address is not set. Without your email we will not be able to answer your message. Continue anyway?',
	invalid_email: 'The email address is invalid. Without your email we will not be able to answer your message. Continue anyway?',
	licenses_added: '%s licenses are added to your shopping cart',
	view_cart: 'View Cart',
	adding_to_cart: 'Adding product to cart...'
}


function ask_a_question_process() {
	if (document.ajax.readyState == 4) {
		if (document.ajax.status != 200)
			alert(document.strs.http_error + ' ' + document.ajax.status + ':\n' + document.ajax.responseText);

		ask_a_question_close();
	}
}

function ask_a_question_send() {
	var params = {}
	params.cmd = 'contact_form';
	params.email = document.forms.frm_question.email.value.replace(/^\s+|\s+$/g, '');
	params.subject = document.forms.frm_question.subject.value.replace(/^\s+|\s+$/g, '');
	params.msg = document.forms.frm_question.msg.value.replace(/^\s+|\s+$/g, '');
	if (params.msg == '') {
		alert(document.strs.empty_msg);
		return;
	}

	if (params.email == '') {
		if (!window.confirm(document.strs.empty_email))
			return;
	}
	else if (!params.email.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i)) {
		if (!window.confirm(document.strs.invalid_email))
			return;
	}

	if (window.XMLHttpRequest)
		document.ajax = new window.XMLHttpRequest();
	else if (window.ActiveXObject)
		document.ajax = new window.ActiveXObject('Microsoft.XMLHTTP');

	if (!document.ajax) {
		alert(document.strs.need_ajax);
	}
	else {
		document.forms.frm_question.email.disabled = true;
		document.forms.frm_question.subject.disabled = true;
		document.forms.frm_question.msg.disabled = true;
		document.forms.frm_question.send.disabled = true;
		document.forms.frm_question.close.disabled = true;

		var str = '';
		for (k in params) if (params.hasOwnProperty(k)) {
			if (str != '') str += '&';
			str += k + '=' + encodeURIComponent(params[k]).replace(/%20/g, '+');
		}

		document.ajax.open('POST', document.u0 + '/ajax_cmd.php', true);
		document.ajax.onreadystatechange = ask_a_question_process;
		document.ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		document.ajax.setRequestHeader("Content-length", str.length);
		document.ajax.setRequestHeader("Connection", "close");
		document.ajax.send(str);
	}
}

function ask_a_question_close() {
	document.msg_question.hide();
}

function ask_a_question(subject) {
	if (document.loaded) {
		if (!document.msg_question) {
			document.msg_question = new MsgBox('msg_question', 60, 400);

			document.msg_question.main_area.innerHTML = '\
<form name="frm_question" method="post" action="">\
	' + document.strs.email + '\
	<br/><input type="text" name="email" value="" size="45" />\
	<br/>\
	<br/>' + document.strs.subject + '\
	<br/><input type="text" name="subject" value="' + subject + '" size="45" />\
	<br/>\
	<br/>' + document.strs.message + '\
	<br/><textarea name="msg" cols="47" rows="5"></textarea>\
	<br/>\
	<br/><input type="button" name="send" value="' + document.strs.send + '" onclick="ask_a_question_send()" /> &nbsp; &nbsp; <input type="button" name="close" value="' + document.strs.close + '" onclick="ask_a_question_close()" />\
</form>\
';
		}
		else {
			document.forms.frm_question.email.disabled = false;
			document.forms.frm_question.email.value = '';
			document.forms.frm_question.subject.disabled = false;
			document.forms.frm_question.subject.value = subject;
			document.forms.frm_question.msg.disabled = false;
			document.forms.frm_question.msg.value = '';
			document.forms.frm_question.send.disabled = false;
			document.forms.frm_question.close.disabled = false;
		}

		document.msg_question.show();

		document.forms.frm_question.email.focus();

	}
	else {
		alert(document.strs.loading_page);
	}
}


function add_to_cart_process() {
	if (document.ajax.readyState == 4) {
		if (document.ajax.status == 200) {
			var results = eval(document.ajax.responseText);

			document.msg_addtocart.main_area.innerHTML = '\
<p align="center">' + document.strs.licenses_added.replace(/%s/, results) + '</p>\
<p align="center"><button onclick="add_to_cart_close()" style="cursor:pointer">' + document.strs.close + '</button> &nbsp; <button onclick="window.location=\'/shop/cart.php\'" style="cursor:pointer">' + document.strs.view_cart + '</button></p>\
';

		}
		else {
			add_to_cart_close();
			alert(document.strs.http_error + ' ' + document.ajax.status + ':\n' + document.ajax.responseText);
		}
	}
}

function add_to_cart_close() {
	document.msg_addtocart.hide();
}

function add_to_cart_send(prd_name) {
	var params = {}
	params.cmd = 'add_to_cart';
	params.prd_name = prd_name;

	if (window.XMLHttpRequest)
		document.ajax = new window.XMLHttpRequest();
	else if (window.ActiveXObject)
		document.ajax = new window.ActiveXObject('Microsoft.XMLHTTP');

	if (!document.ajax) {
		alert(document.strs.need_ajax);
	}
	else {
		var str = '';
		for (k in params) if (params.hasOwnProperty(k)) {
			if (str != '') str += '&';
			str += k + '=' + encodeURIComponent(params[k]).replace(/%20/g, '+');
		}

		document.ajax.open('POST', document.u0 + '/ajax_cmd.php', true);
		document.ajax.onreadystatechange = add_to_cart_process;
		document.ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		document.ajax.setRequestHeader("Content-length", str.length);
		document.ajax.setRequestHeader("Connection", "close");
		document.ajax.send(str);
	}
}

function add_to_cart(prd_name) {
	if (document.loaded) {
		if (!document.msg_addtocart)
			document.msg_addtocart = new MsgBox('msg_addtocart', 60, 400);

		document.msg_addtocart.main_area.innerHTML = '\
<p>' + document.strs.adding_to_cart + '</p>\
';

		document.msg_addtocart.show();

		add_to_cart_send(prd_name);

	}
	else {
		alert(document.strs.loading_page);
	}
}
