/* MODAL BEGIN */

function modal_create (caption, options) {

	this.options = $merge({
		height : '380px',
		width : '460px',
		content : '',
		ajax : false,
		buttons : [
		{
			icon : 'formaccept',
			text : 'Ok'
		},
		{
			icon : 'formcancel',
			text : 'Cancel'
		}
		]
	}, options);

	var modal_container = new Element('div').setProperty('class', 'modalContainer').setStyles({
		height : this.options.height,
		width : this.options.width
	});

	if (this.options.ajax == true) {

		var margin_top = ((this.options.height.toInt() - 106) / 2);
		var margin_left = ((this.options.width.toInt() - 16) / 2);
		
		modal_container.adopt(new Element('div').setProperty('class', 'caption').setHTML(caption)).adopt(new Element('div').setProperty('id', 'modal_content').adopt(new Element('img').setProperty('src', 'sImages/ajaxLoaderModal.gif').setStyles({
			'margin-top' : margin_top + 'px',
			'margin-left' : margin_left + 'px'
		})));
		
	} else {
		modal_container.adopt(new Element('div').setProperty('class', 'caption').setHTML(caption)).adopt(new Element('div').setProperty('id', 'modal_content').setHTML(this.options.content));
	}

	if (this.options.buttons.length > 0) {
		var tool_bar = new Element('ul').addClass('toolbar').setStyles({
			'float' : 'right',
			'margin' : '10px 12px'
		});

		this.options.buttons.each( function (button) {
			new Element('li').adopt(new Element('a').addClass('closeSticky').addClass(button.icon || 'formcancel').addEvent('click', button.onClick || Class.empty).adopt(new Element('span').appendText(button.text))).injectInside(tool_bar);
		});

		modal_container.adopt(new Element('div').setProperty('class', 'footer').setStyle('width', this.options.width).adopt(new Element('div')).adopt(tool_bar));
	} else {
		new Element('li').adopt(new Element('a').addClass('closeSticky').addClass('formcancel').adopt(new Element('span').appendText('Close'))).injectInside(tool_bar);
		modal_container.adopt(new Element('div').setProperty('class', 'footer').setStyle('width', this.options.width).adopt(new Element('div')).adopt(new Element('div').setHTML('')));
	}

	return modal_container;
}

function modal_alert (alert, options) {
	this.options = $merge({
		height : '140px',
		width : '400px'
	}, options);

	var content = '<div style="font-weight: bold; padding-top: 20px; text-align: center;">'+ alert +'</div>';
	var modal_container = new Element('div').setProperty('class', 'modalContainer').setStyles({
		height : this.options.height,
		width : this.options.width
	});

	modal_container.adopt(new Element('div').setProperty('class', 'caption').setHTML('Alert')).adopt(new Element('div').setProperty('id', 'modal_content').setStyles({
		'text-align' : 'center'
	}).setHTML(content));

	
	var tool_bar = new Element('ul').addClass('toolbar').setProperty('id', 'modal_alert_toolbar');
	new Element('li').adopt(new Element('a').addClass('closeSticky').addEvent('click', this.options.onClose || Class.empty).appendText('OK')).injectInside(tool_bar);

	var padding_left = ((this.options.width.toInt() - 32) / 2);
	modal_container.adopt(new Element('div').setProperty('class', 'footer').setStyles({
		'padding-bottom' : '4px',
		'padding-left' : padding_left + 'px',
		width : this.options.width.toInt() - padding_left + 'px'
	}).adopt(new Element('div').setStyle('margin-bottom', '4px')).adopt(tool_bar));

	modalWindow = new StickyWinModal({
		allowMultiple: false,
		content: modal_container,
		modalOptions: {
			hideOnClick: false
		}
	});
}

function modal_confirm (question, onAccept) {

	var content = '<div style="font-weight: bold; padding-top: 20px; text-align: center;">'+ question +'</div>';
	return modal_create('Confirm', {
		height : '140px',
		width : '400px',
		content : content,
		ajax : false,
		buttons : [
		{
			icon : 'formaccept',
			text : 'OK',
			onClick : onAccept
		},
		{
			icon : 'formcancel',
			text : 'Cancel'
		}
		]
	});
}

/* MODAL END */

/* --------------------------------------------------------------------- */

/* POPUP BEGIN */

function S4 () {
	return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}

function popUpAForm (page, id, width, height) {
	if (page == 'mailz') {
		popUpAWindow('mailz.core.php?id='+ id +'', '', width, height);		
	} else {
		popUpAWindow('core.php?page='+ page +'&id='+ id +'', '', width, height);
	}
}

function popUpAWindow(url, name, width, height, properties)
{
	if (!properties) { properties = ',resizable, status=0, toolbar=0, scrollbars=1, modal=1' }
	var wLeft = (screen.width - width) / 2
	var wTop = (screen.height - height) / 2

	name = (S4()+S4()+S4()).toUpperCase();
	var evalThis = 'winObj = window.open("' + url + '", "' + name + '", "width=' + width + ', height=' + height + ', left=' + wLeft + ', top=' + wTop + ', screenX=' + wLeft + ', screenY=' + wTop +  properties + '")'
	eval(evalThis);

	if (!winObj) {
		modal_alert('Please allow popups from this website.');
	} else {
		winObj.focus();
	}
}

/* POPUP END */

/* --------------------------------------------------------------------- */

/* CONTACTZ BEGIN */

function contactz_add_projectz () {

	contactz = table_selected_return();
	if (contactz.length > 0) {

		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content: modal_create('Add to project', {
				width : '400px',
				ajax : true,
				buttons : [
				{
					icon : 'formaccept',
					text : 'Ok',
					onClick : function () {
						
						if ($('projectz_name').getValue()) {
			
							var values = new Array();
							values['contactz'] = contactz;
							values['projectz_id'] = 0;
							values['projectz_name'] = $('projectz_name').getValue();

							xajax_projectzInterface('add_action', values);
						
						} else if ($('projectz_id').getValue()) {

							var values = new Array();
							values['contactz'] = contactz;
							values['projectz_id'] = $('projectz_id').getValue();
							values['projectz_name'] = '';

							xajax_projectzInterface('add_action', values);							
						
						} else {
							// console.log('No adding at all!');
						}
					}
				},
				{
					icon : 'formcancel',
					text : 'Cancel'
				}
				]				
			}),
			modalOptions: {
				hideOnClick: false
			}
		});
	
		var values = new Array();
		values['contactz'] = contactz;

		xajax_projectzInterface('add_gui', values);
	} else {
		modal_alert('No contacts selected');
	}
}

function contactz_send_email () {
	contactz = table_selected_return();
	if (contactz.length > 0) {
		popUpAWindow('mailz.core.php?contactz='+ contactz +'', 0, 580, 640, ',resizable, status=0, toolbar=0, scrollbars=0, modal=1');
	} else {
		modal_alert('No contacts selected');
	}
}

function contactz_export_excel () {
	contactz = table_selected_return();
	if (contactz.length > 0) {

		var values = new Array();
		values['contactz'] = contactz;
	
		xajax_contactzInterface('excel', values);	

	} else {
		modal_alert('No contacts selected');
	}
}

function table_count (count) {
	$('table_count').setText($('table_count').getText().toInt()-1);
	--count;
	
	if (count > 0) {
		var time = ((count*50) > 100) ? (count*50) : 100;
		var time = (time > 400) ? 400 : time;

		setTimeout('table_count('+ count  +')', time);
		$('table_count').setStyles({
			'color' : '#a9ca69',
			'font-weight' : 'bold'
		});
	} else {
		$('table_count').setStyles({
			'color' : '#333333',
			'font-weight' : 'normal'
		});
	}
}

function contactz_delete () {

	contactz = table_selected_return();
	if (contactz.length > 0) {
		question = (contactz.length == 1) ? 'This will delete the contact' : 'This will delete '+ contactz.length +' contacts';
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				contactz.each( function (id) {
					$('table_row_' + id).remove();
				});

				fdTableSort.init('table');
				$('paginateList-table').remove();
				tablePaginater.init();

				table_count(contactz.length);
				xajax_contactzInterface('delete', contactz);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No contacts selected');
	}
}

/* CONTACTZ END */

/* --------------------------------------------------------------------- */

/* COMPANIEZ BEGIN */

function companiez_delete () {

	companiez = table_selected_return();
	if (companiez.length > 0) {
		question = (companiez.length == 1) ? 'This will delete the company' : 'This will delete '+ companiez.length +' companies';;
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				companiez.each( function (id) {
					$('table_row_' + id).remove();
				});

				fdTableSort.init('table');
				$('paginateList-table').remove();
				tablePaginater.init();

				table_count(companiez.length);
				xajax_companiezInterface('delete', companiez);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No companies selected');
	}
}

function companiez_export_excel () {
	companiez = table_selected_return();
	if (companiez.length > 0) {

		var values = new Array();
		values['companiez'] = companiez;
	
		xajax_companiezInterface('excel', values);	

	} else {
		modal_alert('No companies selected');
	}
}

/* COMPANIEZ END */

/* --------------------------------------------------------------------- */

/* PROJECTZ BEGIN */

function projectz_delete () {

	projectz = table_selected_return();
	if (projectz.length > 0) {
		question = (projectz.length == 1) ? 'This will delete the project' : 'This will delete '+ projectz.length +' projects';;
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				projectz.each( function (id) {
					$('table_row_' + id).remove();
				});

				fdTableSort.init('table');
				$('paginateList-table').remove();
				tablePaginater.init();

				table_count(projectz.length);
				xajax_projectzInterface('delete', projectz);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No projects selected');
	}
}

function projectz_export () {

	projectz = table_selected_return();
	if (projectz.length > 0) {
		question = (projectz.length == 1) ? 'This will export the project' : 'This will export '+ projectz.length +' projects';;
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				
				
				
				// remove checkboxes
				xajax_projectzInterface('export', projectz);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No projects selected');
	}
}

function projectz_export_excel () {
	projectz = table_selected_return();
	if (projectz.length > 0) {

		var values = new Array();
		values['projectz'] = projectz;
	
		xajax_projectzInterface('excel', values);	

	} else {
		modal_alert('No projects selected');
	}
}

function projectz_export_pdf () {
	projectz = table_selected_return();
	if (projectz.length == 1) {

		document.location.href = 'projectz.pdf.php?projectz_id=' + projectz[0];

	} else {
		modal_alert('You need to select a project, and only one project');
	}
}

function projectz_contactz_remove (projectz_contactz_id) {

	$('table_row_projectz_contactz_'+ projectz_contactz_id +'').remove();
	fdTableSort.init('projectz_contactz_table');
	
	var values = new Array();
	values['projectz_contactz_id'] = projectz_contactz_id;

	xajax_projectzInterface('remove', values);
}

/* PROJECTZ END */

/* --------------------------------------------------------------------- */

/* USERZ BEGIN */

function userz_delete () {

	userz = table_selected_return();
	if (userz.length > 0) {
		question = (userz.length == 1) ? 'This will delete the user' : 'This will delete '+ userz.length +' users';;
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				userz.each( function (id) {
					$('table_row_' + id).remove();
				});

				fdTableSort.init('table');
				$('paginateList-table').remove();
				tablePaginater.init();

				table_count(userz.length);
				xajax_userzInterface('delete', userz);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No users selected');
	}
}

function userz_login (status) {

	var title = (status == 'Failure') ? '<span style="color: #ff0000;">Wrong e-mail or password</span>' : 'Login';
	var title = (status == 'Password') ? '<span style="color: #ff0000;">Please provide a valid e-mail</span>' : title;
	var title = (status == 'Password_Send') ? '<span style="color: #000;">Password send to your e-mail</span>' : title;
	var content = '<div style="padding: 20px;"><p>E-mail:<br /><input type="text" id="userz_email" style="width: 240px;" /></p><p style="margin-top: 20px;">Password:<br /><input type="password" id="userz_password" style="width: 240px;"/></p><div style="padding: 10px 0 10px 10px;"><input type="checkbox" id="userz_automatic" value="1" />&nbsp;&nbsp;Automatic login</div></div>';

	modalWindow = new StickyWinModal({
		allowMultiple: false,
		content: modal_create(title, {
			height : '240px',
			width : '300px',
			content : content,
			buttons : [
			{
				icon : 'sendemail',
				text : 'Send Password',
				onClick : function () {
					var values = new Array();
					values['userz_email'] = $('userz_email').getValue();

					xajax_userzInterface('password', values);
				}
			},
			{
				icon : 'formaccept',
				text : 'Login',
				onClick : function () {
					var values = new Array();
					values['userz_email']		= $('userz_email').getValue();
					values['userz_password']	= $('userz_password').getValue();
					values['userz_automatic']	= ($('userz_automatic').getProperty('checked') == true) ? 1 : 0;

					xajax_userzInterface('login', values);
				}
			}			
			]
		}),
		modalOptions: {
			hideOnClick: false
		}
	});

	$('userz_email').addEvent('keypress', function (event) {
		var event = new Event(event);
		if (event.code == 13) {
			var values = new Array();
			values['userz_email']		= $('userz_email').getValue();
			values['userz_password']	= $('userz_password').getValue();
			values['userz_automatic']	= ($('userz_automatic').getProperty('checked') == true) ? 1 : 0;

			xajax_userzInterface('login', values);			
		}
	});	
	
	$('userz_password').addEvent('keypress', function (event) {
		var event = new Event(event);
		if (event.code == 13) {
			var values = new Array();
			values['userz_email']		= $('userz_email').getValue();
			values['userz_password']	= $('userz_password').getValue();
			values['userz_automatic']	= ($('userz_automatic').getProperty('checked') == true) ? 1 : 0;

			xajax_userzInterface('login', values);			
		}
	});
}

/* USERZ END */

/* --------------------------------------------------------------------- */

/* MAILZ BEGIN */

function mailz_upload (action) {
	tab_change($('tab_for_attachment'));
	$('mailz_action').value = action;

	modalWindow = new StickyWinModal({
		allowMultiple: false,
		content: '<div style="background: #fff; border: 1px solid #000; height: 200px; width: 400px;"><div id="statusContainer" style="padding-top: 60px;"></div></div>',
		modalOptions: {
			hideOnClick: false
		}
	});

	setTimeout("$('swfu_movie').upload();", 250);
}

function mailz_save () {

	var values = new Array();
	values['action']				= $('mailz_action').getValue();
	values['mailz_id']				= $('mailz_id').getValue();
	values['mailz_userz']			= $('mailz_userz').getValue();
	values['mailz_mailtemplatez']	= $('mailz_mailtemplatez').getValue();
	values['mailz_recipients']		= $('mailz_recipients').getValue();
	values['projectz_array'] 		= $('projectz_id').getValue();
	values['mailz_subject']			= $('mailz_subject').getValue();
	values['mailz_content']			= tinyMCE.getContent();
	
	xajax_mailzInterface('save', values);
}

function mailz_copy (mailz_id) {

	var values = new Array();
	values['mailz_id']				= mailz_id;
	
	xajax_mailzInterface('copy', values);	
}

function attachmentz_delete (attachmentz_id) {

	$(attachmentz_id +'AttachmentContainer').remove();
		
	var values = new Array();
	values['attachmentz_id'] = attachmentz_id;

	xajax_mailzInterface('attachment_delete', values);
}

function attachmentz_download (attachmentz_id) {
	document.location.href = 'download.action.php?attachmentz_id='+ attachmentz_id;
}

function mailz_delete () {

	mailz = table_selected_return();
	if (mailz.length > 0) {
		question = (mailz.length == 1) ? 'This will delete the mail' : 'This will delete '+ mailz.length +' mails';;
		modalWindow = new StickyWinModal({
			allowMultiple: false,
			content : modal_confirm(question, function () {
				mailz.each( function (id) {
					$('table_row_' + id).remove();
				});

				fdTableSort.init('table');
				$('paginateList-table').remove();
				tablePaginater.init();

				table_count(mailz.length);
				xajax_mailzInterface('delete', mailz);
			}),
			modalOptions : {
				hideOnClick: false
			}
		});
	} else {
		modal_alert('No mails selected');
	}
}

/* MAILZ END */

/* --------------------------------------------------------------------- */

/* SEARCH BEGIN */

function search () {

	if ($('searchterm').getValue())	{
		document.location.href = 'index.php?page=search&searchterm='+ $('searchterm').getValue() +'';
	}

}

/* SEARCH END */

/* --------------------------------------------------------------------- */

/* TABS BEGIN */

function tab_change_old (el) {

	tab_element = $(el);
	var tab_index = 1000;	

	$$('.tab').each(function (el) {
		if (el == tab_element) {
			el.removeClass('tabNormal').addClass('tabSelected').setStyle('z-index', 2000);
		} else {
			el.removeClass('tabSelected').addClass('tabNormal').setStyle('z-index', tab_index);
			tab_index--;
		}
	});

	$$('.tab_content').each(function (el) { el.setStyle('display', 'none') });
	var el_tmp = el.getProperty('id').replace('tab_for_', 'tab_content_');
	$(el_tmp).setStyle('display', 'block');
}

function tab_change (el) {

	tab_element = $(el);
	var tab_index = 1000;
	var childs = '';

	$$('.tab').each(function (el) {
		childs = el.getChildren().filterByTag('div');

		if (el == tab_element) {
			childs[0].setHTML('<img onload="fixPNG(this);" src="sImages/Tabs/TabLeftForm.png" alt="" />');
			childs[2].setHTML('<img onload="fixPNG(this);" src="sImages/Tabs/TabRightForm.png" alt="" />');
			el.removeClass('tab-normal').addClass('tab-selected').setStyle('z-index', 2000);
		} else {
			childs[0].setHTML('<img onload="fixPNG(this);" src="sImages/Tabs/TabLeft.png" alt="" />');
			childs[2].setHTML('<img onload="fixPNG(this);" src="sImages/Tabs/TabRight.png" alt="" />');
			el.removeClass('tab-selected').addClass('tab-normal').setStyle('z-index', tab_index);
			tab_index--;
		}
	});

	$$('.tab_content').each(function (el) { el.setStyle('display', 'none') });
	var el_tmp = el.getProperty('id').replace('tab_for_', 'tab_content_');
	$(el_tmp).setStyle('display', 'block');
}

/* TABS END */