//JAVASCRIPT DOCUMENT
//connector.js
var returnValue;
var connector = 
{
	 fields: new Array()
	,labels: new Array()
	,messageForm: null
	,baseUrl: 'http://www.scouting-team.com/connector/web/index.php/'
	,newfavoriteflag: 0
	,unreadedInterval: 0
	

	
	,init: function()
	{
	////	connector.getUnreaded(); DOBA ,MOQTA STARA FUNKIZQ SHTE POLZVAME :)
		connector.clearContents();
		connector.checkSelected();
		connector.checkAttachments();
		connector.checkTaskAttachments();
		
	}//END ,init: function()
	
	,	
	checkTaskForm: function()
	{
		var index   =  document.getElementById('connector_task_task_recipient_id').selectedIndex;
		if (	!document.getElementById('connector_task_task_recipient_id').options[index].value ) 
		alert('Please choose Task recipient.');
		
		var message = tinyMCE.get('connector_task_content').getContent();
		if (message.length<1)
		{	
			alert('The message field is required');
			///document.getElementById('connector_task_content_wrong').innerHTML = 'The task field is required.';
			return false;
		}
		return true;	
	} 
	
	
	
		
	,checkTaskReply:function()
	{
		var message = tinyMCE.get('connector_task_replay_content').getContent();

		if (message.length<3)
		{	
			alert('The message field is required');
			return false;
		}
		
		
		return true;	
	}
	
	,checkMessageForm:function(id)
	{
		var message = tinyMCE.get('connector_message_content').getContent();
		if (message.length<1)
		{	
			document.getElementById('connector_message_content_wrong').innerHTML = 'The message field is required.';
			return false;
		}
		
		 $.ajax({
		   async: false,
		   type: "POST",
		   url: this.baseUrl+'ajax',
		   data:"ajaxaction=checkfordelete&conversation_id="+id,
		   success: function(msg){
			 	returnValue =    msg ;
		   		
		   }
		 });
		
		var confirm_str = 'Some of the  partners has been deleted this conversation : ';
		if(returnValue.length>1) 
		{ 
			confirm_str = confirm_str + returnValue + 'Are you sure that you want to submit this message ?';
			return confirm(confirm_str);
		} 
		
		return true;
	}
	
	,getUnreaded: function()
	{
		var ajax = new connector.ajaxRequest();
		ajax.open("post", this.baseUrl+'ajax', true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.send("ajaxaction=getunreadedmessages");
		
		ajax.onreadystatechange = function()
		{
			if(ajax.readyState == 4)
			{
				connector.setUnreadedFlag(ajax.responseText);
				return false;
			}
			else
			{
			}
			return false;
		}
		
		return false;
		
	}
	
	,setUnreadedFlag: function(count)
	{
			
		if(parent.document.getElementById('connector_unreaded')==undefined)return ;
			parent.document.getElementById('connector_unreaded').innerHTML = count;
			parent.document.getElementById('msgcount').innerHTML = count;
		
	}
	
	,addNewRecipients:function()
	{
		if (document.getElementById('newRecipientsTable').style.display == 'none')
		{
			document.getElementById('newRecipientsTable').style.display = 'block';
			document.getElementById('addnewrecipients_href').innerHTML = 'hide form'; 
		}
		else
		{
			document.getElementById('newRecipientsTable').style.display = 'none';
			document.getElementById('addnewrecipients_href').innerHTML = 'Add new recipients';
		}
		return false;
	}
	
	,hideNewRecipients:function()
	{
		document.getElementById('newRecipientsTable').style.display = 'none';
		return false;
	}
	
	,onAttachmentSelect:function(id)
	{
		var id = parseInt(id)
		id = id + 1;
		
		var o = connector.getElementsByAttribute(connector.messageForm, 'label', 'for', 'connector_conversation_file_'+(id));
//		var oo = connector.getElementsByAttribute(connector.messageForm, 'label', 'for', 'connector_message_file_'+(id));
		try//NEW CONVERSATION
		{
			o[0].style.display = 'block';
		}
		catch(e){}
//		else if ( oo.length )//MESSAGE REPLY
//		{
//			oo[0].style.display = 'block';
//		}
//		else
//		{
//			return false;	
//		}

		connector.fields[id].style.display = 'block';
		return false;
	}//END ,onAttachmentSelect:function(id)
	
	,checkAttachments: function()
	{
		var messageForm = document.getElementById('message_form');
		connector.messageForm = messageForm;
		
		if (messageForm)//WE ARE INTO SHOW MESSAGE PAGE
		{
			connector.labels = this.getElementsByAttribute(messageForm, 'label', 'for');
			
			for (i=1; i<11; i++)
			{
				if (document.getElementById('connector_message_file_'+i))
				{
					connector.fields[i] = document.getElementById('connector_message_file_'+i);
					connector.fields[i].value = "";
					connector.fields[i].id = i;
					connector.fields[i].onchange = function(){connector.onAttachmentSelect(this.id)};
					if (i>1)
					{
						connector.fields[i].style.display = 'none';
					}
				}
				
				if (document.getElementById('connector_conversation_file_'+i))
				{
					connector.fields[i] = document.getElementById('connector_conversation_file_'+i);
					connector.fields[i].value = "";
					connector.fields[i].id = i;
					connector.fields[i].onchange = function(){connector.onAttachmentSelect(this.id)};
					if (i>1)
					{
						connector.fields[i].style.display = 'none';
					}
				}
				
			}
			
			var j = 10;
			for ( i=(connector.labels.length-1); i>0; i-- )
			{
				if ( j>1 && ( (connector.labels[i].htmlFor=="connector_message_file_"+j) || (connector.labels[i].htmlFor=="connector_conversation_file_"+j) ) )
				{
					connector.labels[i].style.display = 'none';
				}
				j--;
			}
		}
		
	}//END ,checkAttachments: function()
	
	
	,checkTaskAttachments: function()
	{
		
		
	} 
	,checkSelected: function()
	{
		return false;
		var checkbox = null;
		var elements = connector.getElementsByClassName_IEfixed('conversationlist_row', 'conversationlist_row checked');
		
		for ( i=0; i<elements.length; i++)
		{
			checkbox = document.getElementById('checkbox_'+i);
			
			if (!checkbox) return false;
			if (checkbox.checked)
			{
	//			alert ('i = '+i+', checked = '+checkbox.checked);
				elements[i].className = "conversationlist_row checked";
//				document.getElementById('conversationlist_row_'+i).className = "conversationlist_row checked";
			}
			else
			{
				//alert ('i = '+i+', checked = '+checkbox.checked);
				elements[i].className = "conversationlist_row";
	//			document.getElementById('conversationlist_row_'+i).className = "conversationlist_row";
			}
			elements[i].onclick = connector.checkSelected;
		}	
	}//END ,checkSelected: function()
	
	,clearContents: function()
	{
		var fields = new Array(
			 'connector_message_content'
			,'connector_conversation_subject'
			,'connector_conversation_content'
		)
		
		for(i=0; i<fields.length; i++)
		{
			if (document.getElementById(fields[i]))
			{
				document.getElementById(fields[i]).value = "";
			}
		}
	}//END ,clearContents: function()
	
	,getElementsByAttribute:function(oElm, strTagName, strAttributeName, strAttributeValue)
	{
		var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
		var oCurrent;
		var oAttribute;
		for(var i=0; i<arrElements.length; i++)
		{
			oCurrent = arrElements[i];
			oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
			if(typeof oAttribute == "string" && oAttribute.length > 0)
			{
				if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute)))
				{
					arrReturnElements.push(oCurrent);
				}
			}
		}
		return arrReturnElements;
	}//END ,getElementsByAttribute:function(oElm, strTagName, strAttributeName, strAttributeValue)
	
	,favorites:function(id, favoriteflag)
	{
		this.newfavoriteflag = 0;
		
		if ( favoriteflag.className=='star')
		{
			this.newfavoriteflag = 1;
		}
		
		var ajax = new connector.ajaxRequest();
		ajax.open("post", this.baseUrl+'ajax', true);
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.send("ajaxaction=newfavoriteflag&conversation_id="+id+'&newfavoriteflag='+this.newfavoriteflag);
		//console.log("ajaxaction=newfavoriteflag&conversation_id="+id+'&newfavoriteflag='+this.newfavoriteflag);
		ajax.onreadystatechange = function()
		{
			if(ajax.readyState == 4)
			{
				//alert (ajax.responseText);
				connector.newfavoriteflagSuccess(ajax.responseText);
				return false;
			}
			else
			{
				//alert ('system error');
			}
			return false;
		}
		
		return false;
	}
	
	,ajaxRequest: function()
	{
		var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
		if (window.ActiveXObject)
		{ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
			for (var i=0; i<activexmodes.length; i++)
			{
				try
				{
					return new ActiveXObject(activexmodes[i]);
				} 
				catch(e) 
				{
					//suppress error
				}
			}
		}
		else if (window.XMLHttpRequest) // if Mozilla, Safari etc
			return new XMLHttpRequest();
		else return false;
		
		
	}//end ajaxRequest: function()
	
	,newfavoriteflagSuccess: function(response)
	{
		var class_name = 'star';
		if ( this.newfavoriteflag==1 )
		{
			class_name = 'star on';
		}
		var o = document.getElementById('conversationlist_row_message_favorite_'+response);
		try
		{ 
			o.className =class_name;
		}
		catch(e){}
		
		return false;
		
	}//,functionSuccess: function()
	
	,getElementsByClassName_IEfixed: function(className1, className2)
	{
		if (document.getElementsByClassName) 
		{
			var r = document.getElementsByClassName(className1);
		}
		else 
		{
			var r = new Array();
			var i = 0;
			var j = 0;
			var a = document.getElementsByTagName("*");
			while (element = a[i++]) 
			{
				if (element.className == className1 || element.className == className2) 
				{
					r[j] = a[i-1];
					j++;
				}
			}
		}
		return r;
	}
	
	,selectAllRecipients: function(checked)
	{
		var elements = this.getElementsByClassName_IEfixed('checkbox_list');
		var el = elements[0];
		var el2 = el.children;
		for ( i=0; i<el2.length; i++)
		{
			var el3 = el2[i];
			var el4 = el3.children;
			for ( j=0; j<el4.length; j++)
			{
				var el5 = el4[j];
				if ( checked==1 )
				{
					el5.checked = true;
				}
				else
				{
					el5.checked = false;	
				}
			}
		}
		
		return false;
	}

	
}//END var connector = 


window.onload = connector.init;
