/************************************************/
/* IPB3 Javascript								*/
/* -------------------------------------------- */
/* contact.js - Contact System 					*/
/* (c) IPS, Inc 2008							*/
/* -------------------------------------------- */
/* Author: Rikki Tissier						*/
/* Modified By: Michael (DevFuse)				*/
/************************************************/

var _contact = window.IPBoard;

_contact.prototype.contact = {
	
	/*------------------------------*/
	/* Constructor 					*/
	init: function()
	{			
		Debug.write("Initializing contact.js");
		
		document.observe("dom:loaded", function(){
			ipb.delegate.register(".show_message", ipb.contact.showMessage);
			if( $('subjectDropdown') )
			{
				ipb.contact.initChangeSubject();
			}
		});
	},
	
	initChangeSubject: function()
	{
		$('subjectDropdown').observe('change', ipb.contact.changeSubject);
		
		if( $F('subjectDropdown') )
		{
			ipb.contact.changeSubject();	
		}
	},	
	
	showMessage: function( e, elem )
	{		
		mid = elem.id.match('show_message_([0-9a-z]+)')[1];
		if( Object.isUndefined(mid) ){ return; }
		
		if( parseInt(mid) == 0 ){
			return false;
		}
		
		Event.stop(e);
		
		var _url = ipb.vars['base_url'] + '&app=contact&module=ajax&section=message&do=view&id=' + mid + "&secure_key=" + ipb.vars['secure_hash'];
		message = new ipb.Popup( 'message', {type: 'pane', modal: false, w: '700px', h: '500px', ajaxURL: _url, hideAtStart: false, close: '.cancel' } );
	},

	changeSubject: function(e)
	{
		var real_subject_id = $F('subjectDropdown');
		var split_result = real_subject_id.split('/');		
		subject_id = split_result[0];
		
		if( Object.isUndefined( subject_id ) || subject_id.blank() || subject_id == 'none' ){ return; }
		
		var url = ipb.vars['base_url'] + "app=contact&module=ajax&section=subject&do=fields&subject=" + subject_id + "&secure_key=" + ipb.vars['secure_hash'];
		
		new Ajax.Request( url.replace(/&amp;/, '&'),
		 				{
							method: 'get',
							onSuccess: function(t)
							{
								if( t.responseText != 'error' )
								{
									$('subjectFieldsContainer').update( t.responseText ).show();
								}
								
							}
						});	
						
		var url2 = ipb.vars['base_url'] + "app=contact&module=ajax&section=subject&do=can_attach&subject=" + subject_id + "&secure_key=" + ipb.vars['secure_hash'];
		
		new Ajax.Request( url2.replace(/&amp;/, '&'),
		 				{
							method: 'get',
							onSuccess: function(s)
							{
								if( s.responseText == '1' )
								{
									$('show_attachments').show();
								}	
								else
								{
									$('show_attachments').hide();
								}							
							}
						});							

	}

}

function contact_admin()
{
	window.location = ipb.vars['base_url'] + 'app=contact';
}

function check_boxes()
{
	var ticked = $('maincheckbox').checked;
	
	var checkboxes = document.getElementsByTagName('input');

	for ( var i = 0 ; i <= checkboxes.length ; i++ )
	{
		var e = checkboxes[i];
		
		if ( typeof(e) != 'undefined' && e.type == 'checkbox')
		{
			var boxname		= e.id;
			var boxcheck	= boxname.replace( /^(.+?)_.+?$/, "$1" );
			
			if ( boxcheck == 'cid' )
			{
				e.checked = ticked;
			}			
		}
	}
}

ipb.contact.init();
