var SiteLanguage = document.getElementsByTagName("html")[0].getAttribute('lang');
var prototypeFileUrl = "/ajax.php?language="+SiteLanguage

function checkThisBox(obj, mode, changeSubscription) {
	if (changeSubscription == true) {
		checkBoxStateMod(obj, "checkbox", "checkbox_s");
		if (mode) refresh_creditNums(mode);	
	} else {
		checkBoxStateMod(obj, "checkbox", "checkbox_s");
		if (mode) refresh_price(mode);	
	}
}

function checkBoxStateMod(obj, state1, state2) {
	obj.childNodes[0].value = (obj.childNodes[0].value == "on") ? "off" : "on";
	obj.className = (obj.className == state1) ? state2 : state1;	
}


function bill() {
	obj = $('bill_need');
	obj.value = (obj.value == "on") ? "off" : "on";
	//obj.parentNode.className = (obj.parentNode.className == "checkbox1") ? "checkbox1_s" : "checkbox1";	
	if (obj.value == "off") {
		disable_bill_inputs(false);
	} else {
		disable_bill_inputs(true);
	}
}

function disable_bill_inputs(mode) {
	if (mode) {
		$('bill_mail_name').value = ""; 			$('bill_mail_name').disabled = false;
													$('bill_mail_country').disabled = false;
		$('bill_mail_city').value = ""; 			$('bill_mail_city').disabled = false;
		$('bill_mail_zip').value = ""; 				$('bill_mail_zip').disabled = false;
		//$('bill_mail_street').value = ""; 			$('bill_mail_street').disabled = false;
		$('bill_mail_state').value = ""; 		$('bill_mail_state').disabled = false;
	} else {
		$('bill_mail_name').value = "-"; 			$('bill_mail_name').disabled = true;
													$('bill_mail_country').disabled = true;
		$('bill_mail_city').value = "-"; 			$('bill_mail_city').disabled = true;
		$('bill_mail_zip').value = "-"; 			$('bill_mail_zip').disabled = true;
		//$('bill_mail_street').value = "-"; 			$('bill_mail_street').disabled = true;
		$('bill_mail_state').value = "-"; 		$('bill_mail_state').disabled = true;
	}
}
function refresh_price(mode) {
//	new Ajax.Request(prototypeFileUrl+"&page=register_ajax&subpage=refresh_price",
	new Ajax.Request('/user/refreshPrices',
		{
			method:'post',
			parameters: ( $(mode).serialize() ),
			onSuccess: function(transport){
				var price = transport.responseText.evalJSON();			

				$(mode+'_price').innerHTML = price.netto;
				$(mode+'_price_b').innerHTML = price.brutto;
			},
			onFailure: function(){ 
				showErrorMsg(mode);
			}
		});
}

function refresh_creditNums(mode) {	
	new Ajax.Request('/user/refreshcreditNums',
		{
			method:'post',
			parameters: ( $(mode).serialize() ),
			onSuccess: function(transport){
				var price = transport.responseText.evalJSON();			

				$(mode+'_paid_dayz').innerHTML = price.paid_dayz;
				$(mode+'_price_n').innerHTML = price.netto;
				$(mode+'_price_b').innerHTML = price.brutto;
			},
			onFailure: function(){ 
				showErrorMsg(mode);
			}
		});
}

function showErrorMsg(mode) {
	$(mode+'_price').innerHTML = "???";
	$(mode+'_price_b').innerHTML = "???";
}

function user_address_is_bill_address() {
	$("bill_mail_country").value = $("user_mail_country").value;
	$("bill_mail_state").value = $("user_mail_state").value;
	$("bill_mail_city").value = $("user_mail_city").value;
	$("bill_mail_zip").value = $("user_mail_zip").value;
	$("bill_mail_street").value = $("user_mail_street").value;
	$("bill_mail_number").value = $("user_mail_number").value;
	$("bill_mail_floor").value = $("user_mail_floor").value;
	$("bill_mail_door").value = $("user_mail_door").value;
}

function user_name_is_bill_name() {
	$('subscript_title').value = $('user_title').value;
	$('subscript_surname').value = $('user_surname').value;
	$('subscript_forename').value = $('user_forename').value;
}

function reg_mail_is_user_mail() {
	$('user_email').value = $('account_email').value;
}

function pleaseWait() {
	document.getElementById('icon_container').innerHTML = "<br /><h3>Türelmét kérjük!</h3><br /><br /><br /><br />";
}

function showError() {
	alert("Probléma adódott! Kérjük próbálja újra");
}

var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads() {
	if (!W3CDOM) return;

	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));

	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
		x[i].onselect = function () {
			this.relatedElement.select();
		}
	}
}