// General functions 
function relocate(chapter,aq){
	self.location.href = '/?chapter=' + chapter + '&aq=' + aq;
}

function changeClass(id,classname){
	document.getElementById(id).className = classname;
}

function toggleDisplay(id){
	if(document.getElementById(id).style.display == 'none'){
		document.getElementById(id).style.display='block';
	} else {
		document.getElementById(id).style.display='none';
	}
}

function openPopup(popup,title,h,w){
	temp = window.open(popup,title,'toolbar=no,status=no,scrollbars=no,location=no,menubar=no,directories=no,width=' + w + ',height=' + h);
}		

function openPopupScroll(popup,title,h,w){
	temp = window.open(popup,title,'toolbar=no,status=no,scrollbars=yes,location=no,menubar=no,directories=no,width=' + w + ',height=' + h);
}		

function displayAJAXResult(result){
	alert(result);
}

function displayAJAXRelocate(result){
	alert(result);
	top.location.href='/?chapter=people&aq=people_friends_my' ;
}

function moveBubble(){}

// Friends functions
function friendsAdd(owner,profile){
	if(confirm('Add to your friends list?')){
		var args = owner + '|' + profile;
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileAddToFriends', args , displayAJAXResult);	
	}
}

function friendsRemove(profile,owner){
	if(confirm('Are you sure?')){
		var args = new Array();
		args[0] = profile;
		args[1] = owner;
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileDeleteFromFriends', args , displayAJAXFriendsRelocate);	
	}
}

function friendsConfirm(friend){
	var args = friend;
  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileConfirmFriends', args , displayAJAXFriendsRelocate);	
}

// Team functions 
function teamAdd(owner,profile,msg){
	if(!msg){msg='Add to your team?'}
	if(confirm(msg)){
		var args = owner + '|' + profile;
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileAddToTeam', args , displayAJAXResult);	
	}
}

function teamRequest(owner,profile){
	if(confirm('Would you like to join this team?')){
		var args = owner + '|' + profile;
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileAddRequestToTeam', args , displayAJAXResult);	
	}
}

function teamConfirm(team,relation,msg){
	if(!msg){msg='Are you sure?'}
	if(confirm('Would you like to join this team?')){
	var args = team + '|' + relation;
  	DWREngine._execute('/cfc/sys_user.cfc', null, 'profileConfirmTeam', args , displayAJAXTeamRelocate);	
	 }
}

function teamRemove(team,owner,msg){
	if(!msg){msg='Are you sure?'}
	if(confirm(msg)){
		var args = team + '|' + owner;
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileDeleteFromTeam', args , displayAJAXTeamRelocate);	
	}
}

function displayAJAXTeamRelocate(result){
	alert(result);
	location.href='/?chapter=people&aq=people_team';
}

function displayAJAXFriendsRelocate(result){
	alert(result);
	location.href='/?chapter=network&aq=user_network';
}

//Project and quotes
function setWorkspaceOn(quote){
	var args = quote + '|' + '1';
	DWREngine._execute('/cfc/sys_extern.cfc', null, 'quoteWorkSpace', args , displayAJAXResult);	
}

function updateProfileSettings(profile_id,id){
	var val = document.getElementById(id).checked;
	if(val==true){val=1}else{val=0};
	var args = profile_id + '|' + id + '|' + val;
	DWREngine._execute('/cfc/sys_user.cfc', null, 'updateProfileSettings', args , displayAJAXResult);	
}

function updateAnswersSettings(profile_id,id){
	var val = document.getElementById(id).checked;
		if(val){val=1} else {val=0}
	var args = profile_id + '|' + id + '|' + val;
	DWREngine._execute('/cfc/sys_answers.cfc', null, 'updateProfileSettings', args , displayAJAXResult);	
}

//Project and quotes
function setWorkspaceOff(quote){
	var args = quote + '|' + '0';
	DWREngine._execute('/cfc/sys_extern.cfc', null, 'quoteWorkSpace', args , displayAJAXResult);	
}


//Others
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function openPrivateRatingForm(profile_id, profile_name){
	document.getElementById('user_bubble_ratings').style.display = 'none';
	document.getElementById('user_bubble').style.display = 'block';
	document.getElementById('profile_id').value = profile_id;
	document.getElementById('this_name').innerHTML = profile_name;
	
}

function addPrivateRating(){
	if(confirm('Are you sure?')){

		var rating_terminology 		= (getCheckedValue(document.getElementsByName('rating_terminology')))? getCheckedValue(document.getElementsByName('rating_terminology')): false;
		var rating_grammar				= (getCheckedValue(document.getElementsByName('rating_grammar')))? getCheckedValue(document.getElementsByName('rating_grammar')): false;
		var rating_spelling				=	(getCheckedValue(document.getElementsByName('rating_spelling')))? getCheckedValue(document.getElementsByName('rating_spelling')): false;
		var rating_delivery				=	(getCheckedValue(document.getElementsByName('rating_delivery')))? getCheckedValue(document.getElementsByName('rating_delivery')): false;
		var rating_communication	= (getCheckedValue(document.getElementsByName('rating_communication')))? getCheckedValue(document.getElementsByName('rating_communication')): false;	
		var rating_overall				= (getCheckedValue(document.getElementsByName('rating_overall')))? getCheckedValue(document.getElementsByName('rating_overall')): false;
		var profile_id 						= document.getElementById('profile_id').value;
		var author_id 						= document.getElementById('author_id').value;
		var args = profile_id + '|' + rating_terminology + '|' + rating_grammar + '|' + rating_spelling + '|' + rating_delivery+ '|' + rating_communication+ '|' + rating_overall + '|' + author_id;
		
	  DWREngine._execute('/cfc/sys_user.cfc', null, 'profileAddPrivateRating', args , confirmAddition);	
	}
}

function confirmAddition(result){
	alert(result);
	document.getElementById('user_bubble').style.display = 'none';
}

function displayPrivateRating(profile_id,author_id){
	document.getElementById('user_bubble_ratings').style.display = '';
	args = profile_id + '|' + author_id;
	DWREngine._execute('/cfc/sys_user.cfc', null, 'profileDisplayPrivateRatings', args , displayRatingHTML);
}

function displayRatingHTML(result){
	document.getElementById('user_ratings').innerHTML = result;
}

function reviewAdd(owner,profile){
	if(confirm('Would you like to add a review?')){
		location.href='/?chapter=people&aq=user_reviews&profile_id=' + profile;	
	}
}

function rateAnswer(answer_id,score,author_id,person){
	if(score>0){
		if(confirm('You believe that this answer is correct and want to thank ' + person + '. Is this correct?')) {
			args = answer_id + '|' + score + '|' + author_id;
			DWREngine._execute('/cfc/sys_answers.cfc', null, 'setKarmaAjax', args , processRateAnswer);
		}
	} else {
		if(confirm('You believe that this answer is incorrect and would like to bury it. Proceed?')) {
			args = answer_id + '|' + score + '|' + author_id;
			DWREngine._execute('/cfc/sys_answers.cfc', null, 'setKarmaAjax', args , processRateAnswer);
		}
	}
}

function processRateAnswer(result){
	var id = 'rating_' + result;
	document.getElementById(id).style.display = 'none';
	alert('Thank you! Your opinion is appreciated.')
}

function closeQuestion(question_id){
	if(confirm('Are you sure?')){
		args = question_id;
		DWREngine._execute('/cfc/sys_answers.cfc', null, 'closeQuestion', args , closeQuestionProcess);	
	}
}

function closeQuestionProcess(result){
	alert(result);	
	location.href='/answers/'
}

function submitLanguagePair(){
	document.languagePairs.project_source_id.value = document.thisProject.project_source_id.options[document.thisProject.project_source_id.selectedIndex].value;
	document.languagePairs.project_target_id.value = document.thisProject.project_target_id.options[document.thisProject.project_target_id.selectedIndex].value;
	document.languagePairs.submit();
}

function openMessage(id){
	if(document.getElementById('message_' + id).style.display == 'none'){
		document.getElementById('message_' + id).style.display='';
		document.getElementById('icon_' + id).src='/images/icon_mail_opened.gif';
		//document.getElementById('frame_' + id).style.border = '1px solid red';
	} else {
		document.getElementById('message_' + id).style.display='none';
		document.getElementById('icon_' + id).src='/images/icon_mail.gif';
		//document.getElementById('frame_' + id).style.border = 'none';

	}
}

function openAnswerMail(id){
	document.getElementById('message_' + id).style.display = 'none';
	document.getElementById('answer_' + id).style.display = '';
}

function closeAnswerMail(id){
	document.getElementById('message_' + id).style.display = '';
	document.getElementById('answer_' + id).style.display = 'none';
}	

function deleteMessage(id,aq){
	var args = new Array();
	args[0] = id;
	args[1] = aq;
	DWREngine._execute('/cfc/sys_extern.cfc', null, 'deleteMailMessage', args , processMsgOps);
}

function markReadMessage(id){
	var args = id;
	DWREngine._execute('/cfc/sys_extern.cfc', null, 'markReadMailMessage', args , processMsgOpsVoid);
}

function answerMessage(id,msg,sbj,aq){
	var args = new Array();
			args[0] = id;
			args[1] = document.getElementById(msg).value;
			args[2] = sbj;
			args[3] = aq;
	DWREngine._execute('/cfc/sys_extern.cfc', null, 'answerMailMessage', args , processMsgOps);
}

function processMsgOps(result){
	alert('OK');
	location.href='/index.cfm?chapter=dashboard&aq=' + result;
}

function processMsgOpsVoid(result){}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function updatePrivacySetting(field,val){
		var args = new Array();
		args[0] = field;
		args[1] = val;
	    DWREngine._execute('/cfc/sys_user.cfc', null, 'profileUpdatePrivacySetting', args , confirmSettingUpdate);	
}

function confirmSettingUpdate(el){
	new Effect.Highlight(document.getElementById('label_' + el), { startcolor: '#000000', endcolor: '#eeeeee' });
}

var editable = false;

function swapEditables(el,el2){
	if(editable==false){
		var buffer = document.getElementById(el).innerHTML;
		document.getElementById(el).innerHTML = document.getElementById(el2).innerHTML;
		document.getElementById(el2).innerHTML = buffer;
		document.getElementById('zeitgeist_input').focus();
		editable = true;				
	}
}

function updateZeitgeist(val,id){
		var args = new Array();
		args[0] = id;
		args[1] = val;
		
		if(val.length>0){
	    	DWREngine._execute('/cfc/sys_extern.cfc', null, 'insertZeitgeistEventVoid', args , confirmZeitgeist);	
		}
	
}

function confirmZeitgeist(result){
		//swapEditables('zeitgeist_buffer','zeitgeist');
		document.getElementById('zeitgeist').innerHTML = result[1];
		document.getElementById('zeitgeist_buffer').innerHTML = '<input id="zeitgeist_input" type="text" style="width:500px;" value="" onblur="updateZeitgeist(this.value,' + result[0] + ');"><input type="button" value="Save" onclick="updateZeitgeist(document.getElementById(&quot;zeitgeist_input&quot;).value,' + result[0] + ');">';
		document.getElementById('zeitgeist_input').value = result[1];
		
		editable = false;		
}

function deleteZeitgeist(id,uid){
		var args = new Array();
		args[0] = id;
		args[1] = uid;
		
		DWREngine._execute('/cfc/sys_extern.cfc', null, 'deleteZeitgeistEventVoid', args , confirmZeitgeistDeletion);
		
}

function confirmZeitgeistDeletion(result){
		$('zg_' + result).fade(0.1);
}


