/**************************************************************************************************
* Copyright(c) 2009 Fabian Gerold, http://www.alumniserver.net
*
* This script is part of the AlumniServer project. It is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any later version.
*
* The GNU General Public License can be found at http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt
*
* This script is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************************************************/

function createRequest(){
	var httpRequest
	if(window.XMLHttpRequest) httpRequest=new XMLHttpRequest()
 	else if(window.ActiveXObject) httpRequest=new ActiveXObject("Microsoft.XMLHTTP")
	if(httpRequest==null){ alert("Your browser does not support XMLHTTP."); return null; }
 	return httpRequest
}
function IsNumeric(sText){
	var ValidChars="0123456789.";
	var IsNumber=true;
	var Char;

	for(i=0; i<sText.length && IsNumber==true; ++i){ 
		Char=sText.charAt(i); 
      	if(ValidChars.indexOf(Char)==-1){
			IsNumber=false;
		}
	}
	return IsNumber;
}
   
function loadIntoDiv(div,server_file,data){
	var target_div=document.getElementById(div);
	target_div.innerHTML='loading...';
	
	var load_r=createRequest();
	load_r.onreadystatechange=function(){
		if(load_r.readyState==4){
			target_div.innerHTML=load_r.responseText;
		}
	};
	var data='';
	load_r.open('POST',server_file,true);
	load_r.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	load_r.send(data);
}
function setMaxLength(id){
	var x=document.getElementById(id)
	var counter=document.getElementById('max')
	if(x.getAttribute('maxlength')){
		counter.appendChild(document.createTextNode('0/'+x.getAttribute('maxlength')))
		x.onkeyup=x.onchange=checkMaxLength
		x.onkeyup()
	}
}

function checkMaxLength(){
	var maxLength=this.getAttribute('maxlength')
	var counter=document.getElementById('max')
	var currentLength=this.value.length
	while(counter.hasChildNodes())counter.removeChild(counter.lastChild)
	if(currentLength>maxLength){
		counter.appendChild(document.createTextNode('maximum length of '+maxLength+' characters reached.'))
		this.value=this.value.substr(0,2000);
		this.scrollTop=this.scrollHeight;
	}
}
function checkUncheck(checkbox,send) {
	if(checkbox.checked==true){
		send.disabled=false;
	}
	else {
		send.disabled=true;
	}
}

function searchFS(){
	var so=document.getElementById('searchOption2Div');
	so.innerHTML='';
	if(document.form1.searchOption1.value!='graduation')return;
	var sel='<select name="searchOption2" class="combo" style="width:80px;">';
	for(k=0;k<4;++k){
		
		if(k==1)
			sel+='<option value="' + k + '" selected>\u00b1 '+ +k+' '+strYear+'</option>';
		else
			sel+='<option value="' + k + '">\u00b1 '+ +k+' '+strYears+'</option>';
	}
	so.style.width='80px'
	so.innerHTML=sel;
}

function setPicture(path,owner,c){
	document['fullsize'].src=path
	var so=document.getElementById('owner');while(so.hasChildNodes())so.removeChild(so.lastChild);
	so.appendChild(document.createTextNode(owner));
	so.appendChild(document.createElement('br'));
	so.appendChild(document.createTextNode(c));
}


// messages
var messages=new Array()
var curMsgDiv
var curMsg
function detectEvent(e) {
	var evt=e || window.event;
	if(evt.keyCode==27){
		// cancel all messages
		if(messages.length==0)return
		for(i=0;i<messages.length;++i)if(curMsg==messages[i]){messages.splice(i,1);break}
		answerDiv=document.getElementById(curMsgDiv)
		while(answerDiv.hasChildNodes()) answerDiv.removeChild(answerDiv.lastChild)
		unselectAllMessages()
	}
	return document.defaultAction;
}

var sMsgs=new Array()
function selectMsg(mid,cell){
	unselect=false;for(i=0;i<sMsgs.length;++i)if(mid==sMsgs[i]){unselect=true;sMsgs.splice(i,1);break}
	var d=document.getElementById('msgDiv'+mid);
	if(unselect){
		d.style.border='#fff 1px solid';
	}else{
		d.style.border='#b55454 1px solid';
		sMsgs.push(mid);
		document['onkeypress']=detectEvent;
	}
	if(sMsgs.length>0){document.getElementById('del1').style.visibility='visible';}
	else{document.getElementById('del1').style.visibility='hidden';}
}
function unselectAllMessages(){
	for(i=0;i<sMsgs.length;++i){
		var d=document.getElementById('msgDiv'+sMsgs[i]);d.style.border='#fff 1px solid';
	}
	sMsgs.length=0;document.getElementById('del1').style.visibility='hidden'
}
function delMsgs(box){
	if(!confirm(strReallyDeleteSelectedMessages+'? ('+sMsgs.length+')')) return;
	if(sMsgs.length==0) return;
	var rDelMsg=createRequest();
	rDelMsg.onreadystatechange=function(){
		if(rDelMsg.readyState==4){
			if(rDelMsg.status==200){
				var numDeleted=rDelMsg.responseText;
				if(numDeleted>0){
					for(i=0; i<sMsgs.length; ++i){
						var mid=sMsgs[i];
						var d=document.getElementById('msgDiv'+mid);
						while(d.hasChildNodes())d.removeChild(d.lastChild)
						var p=document.createElement("div");
						p.appendChild(document.createTextNode(strMessageDeleted));d.appendChild(p);
						d.style.border='0px';
					}
					sMsgs.length=0;
					document.getElementById('del1').style.visibility='hidden';
				}
				else alert('Could not delete message.'+rDelMsg.responseText);
			}else alert('There was a problem with the request.');
		}
	}
	rDelMsg.open('POST','src/a/delMsg.php', true);
	rDelMsg.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var ids=sMsgs[0];
	for(i=1;i<sMsgs.length;++i)ids+='#'+sMsgs[i];
	if(box=='inbox'){rDelMsg.send('mids='+ids);}
	else if(box=='sent'){rDelMsg.send('midss='+ids);}
}

var answerDiv;//=document.createElement("div")
var hiddenLink;//=document.createElement("div")
function hideLink(mid){
	hiddenLink=document.getElementById('link'+mid)
	hiddenLink.style.visibility='hidden'
}

function writeMsg(uid_to,div,mid){
	answerDiv=document.getElementById(div)
	hide=false;for(i=0;i<messages.length;++i)if(mid==messages[i]){hide=true;messages.splice(i,1);break}
	if(hide){
		while(answerDiv.hasChildNodes()) answerDiv.removeChild(answerDiv.lastChild)
		return;
	}
	messages.push(mid)
	curMsg=mid
	curMsgDiv=div
		
	answerDiv.innerHTML='<form name="msgForm">' +
			'<textarea id="msg" class="writeMsg"></textarea><br/>' +
			'<input type="hidden" id="to" value="'+uid_to+'">' +
			'<input type="button" value="'+strCancel+'" onclick="cancelWriteMsg(\''+div+'\')" class="redbtn" style="margin-right:3px;">' +
			'<input type="button" value="'+strSend+'" onclick="sendMsg(\''+div+'\')" class="btn">';
	document['onkeypress']=detectEvent;
	document.getElementById('msg').focus();
}
function cancelWriteMsg(msgDivId){
	document.getElementById(msgDivId).innerHTML='';
}

var writeMsgToLinkContent='';
function cancelWriteMsgTo(){
	var d=document.getElementById('writeMsgToLink');
	d.innerHTML=writeMsgToLinkContent;
	document.getElementById('msgDiv').innerHTML='';
}

function writeMsgTo(uid_to){
	var dl=document.getElementById('writeMsgToLink');
	writeMsgToLinkContent=dl.innerHTML;
	dl.innerHTML='';
	var d=document.getElementById('msgDiv');
	
	d.innerHTML='<form>' +
			'<input type="hidden" id="to" name="to" value="'+uid_to+'">' +
			'<textarea id="msg" name="msg" class="writeMsg"></textarea><br/>' +
			'<input type="button" value="'+strCancel+'" class="redbtn" style="margin-right:3px;" onclick="cancelWriteMsgTo()">' +
			'<input type="button" value="'+strSend+'" class="btn" onclick="sendMsg(\'msgDiv\')">';
	document.getElementById('msg').focus();
}

function sendMsg(div){
	var msg=document.getElementById("msg").value;
	var to=document.getElementById("to").value
	var d=document.getElementById(div)
	d.innerHTML='<div>Sending message...</div>';
	var mr=createRequest();
	mr.onreadystatechange=function(){
		if(mr.readyState==4){
			if(mr.responseText=='true'){
				d.innerHTML='<p class="green">'+strMessageHasBeenSentSuccessfully+'</p>';
			}else{
				d.innerHTML='<p class="red">Sending the message failed. Please try again.</p>'+strMessage+':<br/>'+msg;
			}
		}
	}
	var msge=encodeURIComponent(msg)
	var data='msgTo='+to+'&msg='+msge;
	mr.open('POST','src/a/sendMsg.php',true);
	mr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	mr.send(data)
}
function loadMsg(m_id,box){
	var dlm=document.getElementById('msg'+m_id);
	dlm.innerHTML="Loading...";
	var rm=createRequest();
	rm.onreadystatechange=function(){
		if(rm.readyState==4){
			dlm.innerHTML=rm.responseText.replace(/&lt;br \/&gt;/g,'<br />');
		}
	};
	var data;
	if(box=='inbox')data='mid='+m_id;
	else if(box=='sent')data='mids='+m_id;
	rm.open('POST','src/a/getMsg.php',true);
	rm.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	rm.send(data)
}