/**
 * project name:	miracle
 * task name:		module.login.php
 * author:		wiktor
 * - - - - 
 * started		@ 2005.02.03.
 * last modified	@ see the timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", init_login);

/**
 * FUNCTIONS
 */
function init_login() {
	init_zipcode();
}

function init_zipcode() {
	zipcodes=document.getElementsByTagNameAndAttribute("input", "accept", "zipcode");

	for (var i=0; i<zipcodes.length; i++) {
		zipcodes[i].onchange=getCityByZipCode;
	}
}

function getCityByZipCode() {
	// parseInt bug : http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604
	zipcode=parseInt(this.value, 10);
	if (zipcode>999 && zipcode<10000) {
		pattern=new RegExp ('zipcode','ig');
		cityid=this.id.replace(pattern, 'city');
		if (document.getElementById(cityid)) {
			params=new Array();
			params[0]=zipcode;
			cityNode=document.getElementById(cityid);
			handlerparams=new Array(cityNode)
			server_action_async("miracle","zipcode", params, 
				function (response) {
					cityHandler(handlerparams, response);
				}, 
				false);
		}
	} else {
		cityNode.readOnly=false;
	}
}

function cityHandler(handlerparams, response) {
	if (response!='') {
		handlerparams[0].value=response;
		handlerparams[0].readOnly=true;
	} else {
		handlerparams[0].readOnly=false;
	}
}

function getZipCodeBy() {
	// parseInt bug : http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604
	zipcode=parseInt(this.value, 10);
	if (zipcode>999 && zipcode<10000) {
		pattern=new RegExp ('zipcode','ig');
		cityid=this.id.replace(pattern, 'city');
		if (document.getElementById(cityid)) {
			params=new Array();
			params[0]=zipcode;
			cityNode=document.getElementById(cityid);
			city=server_action("miracle","zipcode", params, false);
			if (city!='') {
				cityNode.value=city;
			}
		}
	}
}

/*
	
*/

function onlyInteger() {
/*	if (!this.oldValue) {
		this.oldValue=0;
	}
	if (this.nodeValue.parseInt()!=this.nodeValue) {
		this.nodeValue=this.oldValue;
	} else {
		this.oldValue=this.value;
	}*/
} /**
 * project name:	???
 * task name:		???.???.php
 * author:		wiktor
 * - - - - 
 * started		@ 2005.??.??
 * last modified	@ see the timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", initInvoiceProblems);

/**
 * FUNCTIONS
 */

function initInvoiceProblems() {
	var module=document.getElementById("module_billproblems");
	if (!module) return;

	setInvoiceProblems();
	document.getElementById("billproblems_type").onchange=function () {
		setInvoiceProblems();
	}
}

function setInvoiceProblems() {
	var select=document.getElementById("billproblems_type");
	var value=select.options[select.selectedIndex].value;
	if (value==3) {
		document.getElementById("new_invoice").style.display="block";
		document.getElementById("old_invoice_legend").innerHTML="Régi számlázási cím";
	} else {
		document.getElementById("new_invoice").style.display="none";
		document.getElementById("old_invoice_legend").innerHTML="Számlázási cím";
	}
} /**
 * project name:	???
 * task name:		???.???.php
 * author:		wiktor
 * - - - - 
 * started		@ 2005.??.??
 * last modified	@ see the timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", initOrder);
var price=0;
var hcs=false;
var hkbt=false;
var h_price1=0;

/**
 * FUNCTIONS
 */
function initOrder() {
	var module=document.getElementById("module_settings");
	if (!module) return;

	if (document.getElementById("settings_price1_1")) {
		var hcs_price1=document.getElementById("settings_price1_1").value;
	}
	if (document.getElementById("settings_h_price1")) {
		var h_price1=document.getElementById("settings_h_price1").value;
		price=document.getElementById("settings_price1").value;
		hkbt=true;
		document.getElementById("settings_hkbt_hat_").onchange=function() { 
			orderPrice(); 
		}
	}	
	if (hcs_price1) { // itt bizony hcs-s lesz
		hcs=true;
		document.getElementById("settings_shipping_mode").onchange=function() { 
			orderPrice(); 
		}
	}
	else if (document.getElementById("settings_price1")) {
		price=document.getElementById("settings_price1").value;
	}
	var amount=document.getElementById("settings_amount");
	var yearStart=document.getElementById("settings_start_year");
	var yearEnd=document.getElementById("settings_end_year");
	var monthStart=document.getElementById("settings_start_month");
	var monthEnd=document.getElementById("settings_end_month");
	var req=document.getElementById("settings_subscriber_req_");
	
	orderPrice();
	
	if (amount) {
		amount.onchange=function() { orderPrice(); }
	}

	if (yearStart) {
		yearStart.onchange=function() {	orderPrice();	}
	}

	if (yearEnd) {
		yearEnd.onchange=function() {	orderPrice();	}
	}

	if (monthStart) {
		monthStart.onchange=function() {	orderPrice();}
	}

	if (monthEnd) {
		monthEnd.onchange=function() {	orderPrice();}
	}

}

function orderPrice() {
	if (hcs==true) {
		var value=document.getElementById("settings_shipping_mode").options[document.getElementById("settings_shipping_mode").selectedIndex].value;
		if (value>0) {
			newPrice=document.getElementById("settings_price1_"+value).value;
			price=newPrice;
		} else {
			document.getElementById("price").value="";
			return;
		}
		//alert ('price='+price);
	}

	if (document.getElementById("settings_start_year") && (!document.getElementById("settings_hkbt_hat_") || document.getElementById("settings_hkbt_hat_").checked)) {
		var yearStart=document.getElementById("settings_start_year").options[document.getElementById("settings_start_year").selectedIndex].value;
		var yearEnd=document.getElementById("settings_end_year").options[document.getElementById("settings_end_year").selectedIndex].value;

		var monthStart=document.getElementById("settings_start_month").options[document.getElementById("settings_start_month").selectedIndex].value;
		var monthEnd=document.getElementById("settings_end_month").options[document.getElementById("settings_end_month").selectedIndex].value;

		if (yearStart==0 || yearEnd==0 || monthStart==0 || monthEnd==0) {
			document.getElementById("price").value="";
			return;
		}
		var months=((12*yearEnd)+monthEnd*1)-((12*yearStart)+monthStart*1)+1;
	
		if (months<1) {
			document.getElementById("price").value="";
			return;
		}
	} else {
		months=1;
	}

	var amount=document.getElementById("settings_amount").value;
	amount=parseInt(amount, 10) ? parseInt(amount, 10) : 0;
	if (amount<1) {
		document.getElementById("price").value="";
		return;
	}

	if (hkbt) {
		var totalPrice=amount*price;
		if (document.getElementById("settings_hkbt_hat_").checked) {
			totalPrice=totalPrice+months*document.getElementById("settings_h_price1").value;
		}
	}
	else {
		var totalPrice=amount*months*price;
	}

	if (totalPrice<price || isNaN(totalPrice)) {
		totalPrice="";
	}

	if (document.getElementById("settings_shipping_mode") && (document.getElementById("settings_shipping_mode").value==1) && _action_ && totalPrice && (document.getElementById("settings_start_year").value==a_start_year) && (document.getElementById("settings_start_month").value==a_start_month) && (document.getElementById("settings_end_year").value==a_end_year) && (document.getElementById("settings_end_month").value==a_end_month)) {
		totalPrice=a_price*amount;
	}
	else if (!document.getElementById("settings_shipping_mode") && _action_ && totalPrice && (document.getElementById("settings_start_year").value==a_start_year) && (document.getElementById("settings_start_month").value==a_start_month) && (document.getElementById("settings_end_year").value==a_end_year) && (document.getElementById("settings_end_month").value==a_end_month)) {
		totalPrice=a_price*amount;
	}

	if (totalPrice && document.getElementById("settings_start_year")) {
		if (totalPrice && (document.getElementById("settings_start_year").value=='2008') && (document.getElementById("settings_start_month").value=='11') && (document.getElementById("settings_end_year").value=='2009') && (document.getElementById("settings_end_month").value=='6')) {
//		if (projectname=='szl') totalPrice=17856*amount;
//		if (projectname=='kvl') totalPrice=18258*amount;
//		if (projectname=='tbl') totalPrice=17886*amount;
//		if (projectname=='mal') totalPrice=19002*amount;
		}
		if (totalPrice && (document.getElementById("settings_start_year").value=='2008') && (document.getElementById("settings_start_month").value=='12') && (document.getElementById("settings_end_year").value=='2009') && (document.getElementById("settings_end_month").value=='6')) {
//		if (projectname=='szl') totalPrice=17856*amount;
//		if (projectname=='kvl') totalPrice=18258*amount;
//		if (projectname=='tbl') totalPrice=17886*amount;
//		if (projectname=='mal') totalPrice=19002*amount;
		}
	}

	document.getElementById("price").readOnly=true;
	document.getElementById("price").value=totalPrice+" Ft + áfa";
} /**
 * project name:	miracle
 * task name:		module.text.js
 * author:		wiktor@eworld.hu
 *
 * started		@ 2005.04.09.
 * last modified	@ timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", init_text);

var tabIndex=1;

/**
 * FUNCTIONS
 */
function init_text() {

	// ha nem a hirszerkesztesnel vagyunk, akkor nincs mit inicializalni
	if (!document.getElementById("module_text_edit")) return;
	var text=document.getElementById("module_text_edit");
	switchable_elements=text__get_switchable_elements(text);

	var langs=document.getElementById("text_edit_lang");

	if (langs) {
		var elements=langs.getElementsByTagName("input");
		for (i=0; i<elements.length; i++) {
			elements[i].onclick=text___onclick;
		}
	}
}

function text__get_switchable_elements(text) {
	var switchable_elements=new Array();
	elements=text.getElementsByTagName("fieldset");
	j=0;
	for (i=0; i<elements.length; i++) {
		element=elements[i];
		if (element.getAttribute("lang")) {
			switchable_elements[j]=element;
			j++         
		}
	}
	return switchable_elements;
}

function text___onclick() {
	var tab=document.getElementById("tab_"+this.value);
	for (var i=0; i<switchable_elements.length; i++) {
		switchable_elements[i].style.display="none";
	}
	tab.style.display="block";
}
 /**
 * project name:	newsletter
 * task name:		newsletter.js
 * author:		wiktor
 * - - - - 
 * started		@ 2005.07.26
 * last modified	@ see the timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", init_newsletter);

/**
 * FUNCTIONS
 */
function init_newsletter() {
	init_newsletter_edit();
	init_add_new_email();
}

function init_newsletter_edit() {
	if (!miracleConfig["miracle_newsletter_edit"]) return;
	miracleZones=new Array();
	var elements=document.getElementsByTagNameAndAttribute("*", "class", "zone");
	for (var i in elements) {
		var newZone=new newsletterZone(elements[i].id);
		miracleZones[newZone.zoneid]=newZone;
	}
}

function init_add_new_email() {
	if (!document.getElementById("add_new_email_button")) return;

	var button=document.getElementById("add_new_email_button");
	button.onclick=function() {
		var email=document.getElementById("add_new_email").value;
		var groupId=new String(document.location).split("/");
		groupId=groupId[groupId.length-2];
		var message=document.createElement("div");
		message.innerHTML=server_action("newsletter", "add_email_to_group", new Array(email, groupId), false);
		DOM_InsertAfter(button, message);
	}
}


/**
 * newsletterBlock
 */

/** 
 * construct
 */
function newsletterBlock(id, zoneid) {
//	alert("id "+id+" zoneid "+zoneid);
	if (document.getElementById(id)) {
		this.block			= document.getElementById(id);
	} else {
		return false;
	}

	this.zoneid=zoneid;
	this.id=id;
	this.blockid=parseInt(this.id.split("_")[1], 10);
	this.setToolBar();
}

newsletterBlock.prototype.setToolBar = function() {
	this.toolbarid="toolbar_for_"+this.id;

	var toolbar=document.getElementById("block_toolbar");
	if (!toolbar) {
		alert("FATAL ERROR: DIV with ID=block_toolbar not found");
	}
	toolbar=toolbar.cloneNode(true);
	toolbar.setAttribute("id", this.toolbarid);

	this.block.appendChild(toolbar);
	this.toolbar=document.getElementById(this.toolbarid);
	var typeName=this.toolbar.getElementsByTagNameAndAttribute("*", "class", "type_name")[0];
	typeName.innerHTML=this.block.getAttribute("title");
        
        this.setToolBarActions();
        /*
	var block=this;
	this.block.onmouseover=function() {
		block.toolbar.style.display="block";
	}
	this.block.onmouseout=function() {
		block.toolbar.style.display="none";
	}
        */
        
}

newsletterBlock.prototype.setToolBarActions = function() {
	this.buttons=this.toolbar.getElementsByTagName("img");
	for (var i=0; i<this.buttons.length; i++) {
		if (!this.buttons[i].getAttribute("action")) continue;
//		eval("buttons[i].onclick=this."+buttons[i].getAttribute("action")+"Block");
		var block=this;
		eval("this.buttons[i].onclick=function() { block."+this.buttons[i].getAttribute("action")+"Block(); }");
	}
}

newsletterBlock.prototype.deleteBlock = function() {
	if (confirm("Biztos?")) {
		var tmp=server_action("newsletter", "delete_block", this.blockid, false);
		if (tmp) {
			this.block.parentNode.removeChild(this.block);
		} else {
			alert("Error... :(");
		}
	}
}

newsletterBlock.prototype.editBlock = function() {
//	var popup=new Array("/plug-in/textarea/"+this.zoneid+"/"+this.blockid, 500, 500);
	var popup=new Array("/newsletter/block/plug-in/"+this.blockid, 500, 500, "auto");
	var callback="miracleZones["+this.zoneid+"].blocks["+this.blockid+"].setContent";
	miraclePlugIn=new PopupHTML(popup);
	miraclePlugInCheck=window.setInterval("miracleIsPlugInReturnValue(\""+callback+"\")", 500);
        /* oncloset kene megnezni... :( */

}

newsletterBlock.prototype.setContent = function(content) {
	var content=server_action("newsletter", "get_content", this.blockid, false);
	this.block.innerHTML=content;
	this.setToolBar();
	window.focus();
}

function miracleIsPlugInReturnValue(callback) {
	callback=callback+"(miraclePlugInReturnValue)";
	if (miraclePlugIn.mywindow.closed) {
		clearInterval(miraclePlugInCheck);
		if (miraclePlugInReturnValue=="reload") {
			window.location.reload();
		} else {
			if (miraclePlugInReturnValue) {
				eval(callback);
			}	
		}
	}
}


newsletterBlock.prototype.moveupBlock = function() {
	var previousBlock=this.block.previousSibling;
	if (previousBlock) this.block.swapSiblings(previousBlock);
	server_action("newsletter", "move_block", new Array(this.blockid, "up"), false);
}

newsletterBlock.prototype.movedownBlock = function() {
	var nextBlock=this.block.nextSibling;
	if (nextBlock) this.block.swapSiblings(nextBlock);
	server_action("newsletter", "move_block", new Array(this.blockid, "down"), false);
}

/*
newsletterBlock.prototype.setToolBarHREFs = function() {
	var buttons=this.toolbar.getElementsByTagName("img");
	for (var i=0; i<buttons.length; i++) {
		var button=buttons[i];
		var re=/\|\$([a-z]+)\|/g;
		var href=new String(a.getAttribute("href"))
		var matches=href.matchAll(re);
		for (var j=0; j<matches[0].length; j++) {
			href=href.replace(matches[0][j], this.block.getAttribute(matches[1][j]));
		}
		buttons.setAttribute("href", href);
}
*/

/**
 * newsletterZone
 */

/** 
 * construct
 */
function newsletterZone(id) {
	if (document.getElementById(id)) {
		this.zone			= document.getElementById(id);
	} else {
		return false;
	}

	this.id=id;
	this.zoneid=parseInt(this.id.split("_")[1], 10);
	this.blocks=new Array();
	this.buttons=new Array();
	this.blockPrototype=this.createBlockPrototype();

	this.setBlocks();
	this.setToolBar();
	this.setToolBarActions();
}

newsletterZone.prototype.createBlockPrototype = function() {
	var blockPrototype=document.createElement("div");
	blockPrototype.setAttribute("class", "block");
	return blockPrototype;
}

newsletterZone.prototype.setBlocks = function() {
	var blocks=this.zone.getElementsByTagNameAndAttribute("div", "class", "/block.+/");
	for (var i in blocks) {
		var newBlock=new newsletterBlock(blocks[i].id, this.zoneid);
		this.blocks[newBlock.blockid]=newBlock;
	}
}

newsletterZone.prototype.setToolBar = function() {
	this.toolbarid="toolbar_for_zone_"+this.id;

	var toolbar=document.getElementById("zone_toolbar");
	if (!toolbar) {
		alert("FATAL ERROR: DIV with ID=zone_toolbar not found");
	}
	toolbar=toolbar.cloneNode(true);
	toolbar.setAttribute("id", this.toolbarid);

	this.zone.appendChild(toolbar);
	this.toolbar=document.getElementById(this.toolbarid);
}

newsletterZone.prototype.setToolBarActions = function() {
//	var buttons=this.toolbar.getElementsByTagName("img");
	this.buttons=this.toolbar.getElementsByTagNameAndAttribute("*", "action");
	for (var i=0; i<this.buttons.length; i++) {
//		if (!buttons[i].getAttribute("action")) continue;

		// csak igy latja this-kent a zonat mint objektum
		var zone=this;
		eval("this.buttons[i].onclick=function() { zone."+this.buttons[i].getAttribute("action")+"Block(); }");
		
	}
}

newsletterZone.prototype.createBlock = function() {
	// this = aktualis zona
	var select=this.zone.getElementsByTagName("select")[0];
	var block_type=select.options[select.selectedIndex].value;
	if (block_type!=0) {
		var newBlockId=server_action("newsletter", "create_block", new Array(this.zoneid, block_type), false);
		var newBlock=this.blockPrototype.cloneNode(true);
		newBlock.setAttribute("id", "block_"+newBlockId);
		newBlock.setAttribute("class", "block "+block_type);
		this.zone.appendChild(newBlock);
		this.blocks[newBlockId]=new newsletterBlock("block_"+newBlockId, this.zoneid);
	}
} /**                                            +
 * project name:	???
 * task name:		???.???.php
 * author:		wiktor
 * - - - - 
 * started		@ 2005.??.??
 * last modified	@ see the timestamp ;)
 */

/**
 * INITIALIZATION
 */
addEvent(window, "load", initCaptcha);

/**
 * FUNCTIONS
 */
function initCaptcha() {
	var button=document.getElementById("captcha_button");
	if (button) {
		button.onclick=function() {
			var img=document.getElementById("captcha_img");
			img.src="/captcha.jpg?"+Math.random();
			return false;
		}
	}
}