var sendtomehtml;
var sendname;
var sendtomehtmlpreview;


function sendemail(to, subject, bodytext){
	
	var fromemail = 'admin@cplanet.com';
	var sendalias = 'Cplanet';
	var sendusername = '';
	var sendtype = '';
	var sendbodyhtml = false;
	var senddestid = '';
	
	WebService.sendemail(to, fromemail, sendalias, subject, bodytext, sendusername, sendtype, sendbodyhtml, senddestid);
}

function sendemailinvitation(){
	var inviteemails = document.getElementById("sharedestemails").value;
	var multipleemails = inviteemails.split(", ");
	var emailresultlist = '';
	
	var invitationnum = multipleemails.length;
	
	if(multipleemails.length < 6){
		sentcount = 0;
		for (var i = 0; i < invitationnum; i++) {
			var to = multipleemails[i];
			var subject = currentuser+' has sent you a Cthere Destination';
			var bodytext = buildsharedesttext();
			
			if(multipleemails[i].indexOf('@') != -1){
				sendemail(to, subject, bodytext);
				sentcount++;
				emailresultlist += to+'\n';
			}
			
		}
		
		if(sentcount > 0){
			var resultalert = 'Your email was sent to '+sentcount+' recipients:\n'+emailresultlist;
		}else{
			var resultalert = 'Please enter valid email addresses only';
		}
		
		document.getElementById("sharedestemails").style.color = '#990000';
		document.getElementById("sharedestemails").value = resultalert;
		
	}else{
		alert('You may only send 5 emails at a time to non Cplanet Members');	
	}
}

function buildsharedesttext(){
		
		try{
			var sharedestname = activedest[0].name;
			var sharedestid = savedestinationid;
		}catch(err){
			var sharedestname = cpdestname;
			var sharedestid = destinationid;
		}
		
		var sharetext = currentuser+' has invited you to see the Cthere Destination: '+sharedestname+'\n\n'
						+'Click the link below to view the destination:\n\n'
						+'http://www.cplanet.com/destwindow.aspx?destid='+sharedestid;
						
		return sharetext;
	
}


//THE FOLLOWING TWO FUNCTIONS WORK WITH A HIGHSLIDE DIV RESIDING ON THE PAGE THAT IS REFRENCING THE FUNCTION.
function buildsendbubble(){
	
	//alert(sendtomehtml);
	var getsendname = sendtomehtml.split("\n", 1);
	sendname = getsendname[0];
	//alert(sendname);
	var formatsendpreview = sendtomehtml.replace(/\n/g, '<br>');
	sendtomehtmlpreview = '<div>'+formatsendpreview+'</div>';
	//alert(sendtomehtmlpreview);
	
	document.getElementById("sendtextpreview").innerHTML = sendtomehtmlpreview;
	//document.getElementById("sendemailpreview").value = sendtomehtml+'\nVisit Our Cthere Storefront - http://www.cplanet.com/destwindow.aspx?destid='+destarray[0].destinationid;
	document.getElementById("sendemailpreview").value = sendtomehtml+'\nVisit Our Cthere Storefront - http://www.cplanet.com';

}


function sendtouser(type){
	
	if(type=='text'){
		var subject = sendname;
		var bodytext = sendtomehtml;
		if((document.getElementById("phonenumber").value != '')&&(document.getElementById("carrierlist").value != 'select your carrier')){
			var to = document.getElementById("phonenumber").value+'@'+document.getElementById("carrierlist").value;
			
			sendemail(to, subject, bodytext);
			
			var sendpanelid = document.getElementById('sendtomepanel');
			hs.close(sendpanelid);
			
			alert('Message Sent');
		}else{
			alert('Please verify you have entered a valid phone number and selected a carrier service.');	
		}
	}
	
	if(type=='email'){
		if((document.getElementById("useremailto").value != '')&&(document.getElementById("useremailfrom").value != '')){
			var subject = sendname;
			var bodytext = document.getElementById("sendemailpreview").value;
			var to = document.getElementById("useremailto").value;
			if(document.getElementById("copymyself").checked){
				to += ', '+document.getElementById("useremailfrom").value;
			}
			sendemail(to, subject, bodytext);
			
			var sendpanelid = document.getElementById('sendtomepanel');
			hs.close(sendpanelid);
			
			alert('Message Sent');
		}else{
			alert('To and From email addresses are required');	
		}
	}
	
}


function buildshare(type){
	
	if(currentuser){
		if(type=='tour'){
			var defaulttext='Check out this Cplanet Tour!';
		}
		if(type=='dest'){
			var defaulttext='Check out this Cplanet Destination!';
		}
		
		var sharehtml = '<div>'+
						'<table width="100%" border="0" cellspacing="0" cellpadding="2">'+
						'<tr height="25"><td class="titlebannerleft"></td>'+
						'<td class="titlebannermiddle"><div id="desttitle" class="bannertext">Invite friends to view:</div></td>'+
						'<td class="titlebannerright"></td></tr>'+
						'<tr><td align="center" colspan="3"><div id="sharetitle" class="titlefont"></div></td></tr>'+
						'<tr><td align="center" colspan="3">Please enter the email addresses of the people you would like to send an invitation to:<br />'+
						'<textarea id="invitees" name="invitees" cols="35" rows="2"></textarea><br>'+
						'<span class="minifont">Use a comma(,) to seperate the addresses</span></td></tr>'+
						'<tr><td align="center" colspan="3">Comments:<br />'+
						'<textarea id="invitecomments" name="invitecomments" cols="35" rows="3">'+defaulttext+'</textarea></td></tr>'+
						'<tr><td align="center" colspan="3">'+
						'<input name="sendinvitation" type="button" value="Send Invitation" onclick="sharestuff(\''+type+'\')"/>'+
						'<input name="cancelinvitation" type="button" value="Cancel" onclick="highslidecontrol(\'dynhtmloff\')"/>'+
						'</td></tr></table></div>';
						
		document.getElementById('dynhtmlcontent').innerHTML=sharehtml;
		
		if(type=='tour'){
			document.getElementById('sharetitle').innerHTML=activetourname+'<br><span class="minifont">'+activetourid+'</span>';
		}
		if(type=='dest'){
			document.getElementById('sharetitle').innerHTML=currentdestinations[activeresult].name;
		}
		
		highslidecontrol('dynhtml');
	}else{
		alert('You must be logged on to use this feature.');
	}
	
}

function sharestuff(type){
	
	if(type=='tour'){
	
		var to = document.getElementById('invitees').value;
		var comments = document.getElementById('invitecomments').value;
		var subject = currentuser+' has invited you to view a Cplanet Tour';
			
		var bodytext = currentuser+' has invited you to view the Cplanet tour titled '+activetourname+'\n\n'+comments+
						'\n\nPlease click to link below to view the tour'+
						'\n\nhttp://www.cplanet.com/default.aspx?id='+activetourid;
	}
	
	if(type=='dest'){
	
		var to = document.getElementById('invitees').value;
		var subject = currentuser+' has invited you to view a Cplanet Destination';
			
		var bodytext = currentuser+' has invited you to view the Cplanet destination titled '+currentdestinations[activeresult].name+
						'\n\nPlease click to link below to view the tour'+
						'\n\nhttp://www.cplanet.com/default.aspx?id='+currentdestinations[activeresult].destinationid;
	}
						
		//alert(to+'\n'+subject+'\n'+bodytext);
						
		sendemail(to, subject, bodytext);
		
		document.getElementById('dynhtmlcontent').innerHTML='<div align="center">An invitaion email has been sent to:<br>'+document.getElementById('invitees').value+
															'<br><br><b>Thank you for using Cplanet.com</b></div>';

}

function initfacebook(){
	FB.init("4522d40b6bbe9cee563f87fc9c0adfa0", "xd_receiver.htm");
	
	try{
		//FB.Connect.ifUserConnected(updateuserbox);
	}catch(err){
		alert('not connected to facebook');
	}
}


function updateuserbox(){
	alert('updating facebook avatar');
	
	var box = document.getElementById("facebookbox");
	
	box.innerHTML="<fb:profile-pic uid='loggedinuser'></fb:profile-pic>"
	 				+"Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>";
					
//	var expander = hs.getExpander(box);
//    expander.reflow();
					
	FB.XFBML.Host.parseDomTree();
}



function sendtofacebook(sharedestid){
	
	var texttopost = {"post-title": "Cplanet destination.", "post-url": "http://www.cplanet.com/destwindow.aspx?destid=" + sharedestid};
	
	
	FB.Connect.showFeedDialog(71302365808,texttopost);
}