// Email a Friend functions

function validEmail(s){
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")){ i++ }
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
		else i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")){ i++ }
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else { return true; }
}


function emailFriendDialog(divid, title) {
	var dialog = Ext.get(divid);
	if( dialog) { dialog.show(); }
	else {
		Ext.Ajax.request({
			url: '/components/public.cfc',
			params: { method: 'emailFriendDialog', divid: divid, title: title },
			success: function(response, options) { 
					var responseObj = Ext.util.JSON.decode(response.responseText);				
					var content = responseObj.data[0].CONTENT;
					//Ext.get('footer').insertHtml('afterEnd',content);	
					Ext.get('footer').insertHtml('afterEnd',content);	
				},
			failure: function(){
				alert('Error connecting to server');
			},
			scope: this
		});	
	}	
}

function closeEmailFriendDialog(divid) {
	var sentdlg = Ext.get('email-sent');	
	sentdlg.update('');
	sentdlg.dom.style.display = 'none';
	Ext.get(divid).hide();
}

function validateEmailFriendDialog(form) {
	if( form.fromname.value.length == 0 ) {
		alert('Please enter your name');
		form.fromname.focus();
		return false;
	}
	if( !validEmail(form.fromemail.value) ) {
		alert('Please enter a valid email address for yourself');
		form.fromemail.focus();
		return false;
	}
	if( form.toname.value.length == 0 ) {
		alert('Please enter your friend\'s name');
		form.toname.focus();
		return false;
	}
	if( !validEmail(form.toemail.value) ) {
		alert('Please enter a valid email address for your friend');
		form.toemail.focus();
		return false;
	}
	return true;
}

function submitEmailFriendDialog() {
	var form = Ext.get('shareform').dom;
	var ccme = form.ccMe.checked ? 1 : 0;
	
	Ext.Ajax.request({
		url: '/components/public.cfc',
		params: { method: 'emailFriend', fromname: form.fromname.value,
				  fromemail: form.fromemail.value, toname: form.toname.value, toemail: form.toemail.value, 
				  message: form.message.value,
				  ccme: ccme, title: form.title.value },
		success: function(response, options) {
				var str = '<h3>email sent!</h3><p>Your email has been sent to:<br><strong>'+options.toemail+'</strong>.</p>'
				var sentdlg = Ext.get('email-sent');
				sentdlg.update(str);
				sentdlg.show();
				//window.scrollTo(0,0);
			},
		failure: function(){
			alert('Error connecting to server');
		},
		toemail: form.toemail.value,
		scope: this
	});
}