///////////////////
//javascript functions for executing send to friend functionality
///////////////////

var tmp_cid = '';
var send_url = '';
var friend_url = '';

//main function for loading the friend search and sending the "send to friend" message
function process_load_friend_search_div(url,url2,cid,link,act,uids,emails,emailtome,message)  {
	tmp_cid = cid;
	send_url = url;
	friend_url = url2;	
	
	if(act=='load')  {
		var params = 'a=load&cid=' + cid + '&link=' + link;		
		
		YAHOO.util.Connect.asyncRequest(
			'GET',
			send_url + '?' + params,
			{
				success : return_friend_search_div
			}
		);	
	}
	
	if(act=='send')  {	
		
		showChatLoading('friendSearchResults');		
		
		message = message.substring(0,255);
		
		//if the emailtome checkbox was not checked, clear the value
		if(!document.getElementById('chkEmailMe').checked)  {
			emailtome = '';
		}

		var params = 'a=send&cid=' + cid + '&link=' + link + '&uids=' + uids + '&emails=' + emails + '&emailtome=' + emailtome + '&message=' + message;				

		YAHOO.util.Connect.asyncRequest(
			'GET',
			send_url + '?' + params,
			{
				success : return_send_to_friend_results
			}
		);	
	}			
}

//return the result message from executing the send to friend email
function return_send_to_friend_results(return_data)  {
	
	//pass the return string to a local variable
	send_response = return_data.responseText;	

	proc_sendtofriend_close();		
	
	tooltipOn('Your message has been sent. Click to send to more friends.',tmp_cid,-20);
	
	setTimeout("tooltipOff(tmp_cid)",3000);
	
	return false;
}

//return the friend search results and populate the send to friend div
function return_friend_search_div(return_data)  {	
	if(return_data.responseText == '') {			
		document.getElementById('sendtofriend_div').innerHTML = '';
		return false;
	}
	
	//pass the return string to a local variable
	search_response = return_data.responseText;

	//write the response	
	document.getElementById('sendtofriend_div').innerHTML = search_response;		
	
	format_friend_search_div();	
	
	process_fetch_group(friend_url, 'all', '1');

	document.getElementById('txtemails').value = '';
	
	return false;
}
var timeout_flag = false;
//formatting function to hide/show dive - move the div around the page to the right spot
function format_friend_search_div()  {	

        if ( !timeout_flag ) {
                timeout_flag = true;
                setTimeout(format_friend_search_div, '500');
                return;
        }

	//after the div has been returned, set its position and display/hide it
	setPosition('sendtofriend_div',document.getElementById('btnSendToFriend'+tmp_cid),495,12);		

	if(document.getElementById('sendtofriend_div').style.display=='block')  {
		
		document.getElementById('sendtofriend_div').style.display='none';
	} else {		
		
		document.getElementById('sendtofriend_div').style.display='block';
	}	
	
	if(document.getElementById('friendSearchForm').style.display=='block')  {
		
		document.getElementById('friendSearchForm').style.display='none';
	} else {		
		
		document.getElementById('friendSearchForm').style.display='block';
	}
	
	if(document.getElementById('friendSearchHolder').style.display=='none')  {
		
		document.getElementById('friendSearchHolder').style.display='block';
	} else {		
		
		document.getElementById('friendSearchHolder').style.display='none';
	}
	
	//alert(1);
	//document.getElementById('sendtofriend_div').toggle();
	//alert(2);
	//toggle_friend('friendSearchForm','friendSearchHolder');			
//	alert(3);
}

//cleanup methods and steps to be performed when the sendtofriend div is closed.
function proc_sendtofriend_close()  {
	format_friend_search_div(tmp_cid);
	clearList('uidHiddenList');	
	process_fetch_group(friend_url, 'all', '1');
	document.getElementById('txtemails').value = '';
	document.getElementById('txtName').value = '';		
}

//encoding functionality to encode the message that is being sent in the send to friend email
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

//check to ensure that a valid email address has been entered
function checkemail(emailfld){		
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	var emailval = emailfld.value;

	if(emailval.length==0)  {
		return true;	
	}
	
	if(emailval.indexOf(";")== -1)  {
	
		if (filter.test(emailval))  {
			testresults=true;
		} else {
			alert("Please input a valid email address!");
			testresults=false;
		}
	} else {
		emailval = emailval.replace(/\s/g , '');
		var arremails = emailval.split(';');	
		
		for (var x = 0; x < arremails.length; x++)  {
			if (filter.test(arremails[x]))  {
				testresults=true;
			} else {
				alert("One or more of the emails you entered is invalid!");
				testresults=false;
				return (testresults);
			}
		}			
	}
	return(testresults);
}
