// JavaScript Document

/**
 * Mail To A Friend 
 *    
 *  add this function to a mailto: <a href and it will pop a javascript box to help the user send to a friend, 
 *   E.G. <a href="mailto:example@email.co.uk?subject=abc&body=aaaaa" onClick="mailToFriend(this); return false;">
 * 	        Email To Friend 
 * 		  </a>
 */
var a_obj, emailAddy, subjectPos, subject, emailbodyPos, emailbody;
function mailToFriend(a_obj) {
	
	// if mailto: ...
	if(a_obj.href.match('mailto')) {
		emailAddy = unescape(a_obj.href.replace('mailto:', ''));
		emailAddy = emailAddy.replace('?', '&');
		
		// if mailto contains 'subject' before body (if body at all)
		if(emailAddy.match('&subject=')) {
			
			subjectPos = emailAddy.indexOf('&subject=');
			subject = emailAddy.substring(subjectPos).replace('&subject=', '');
			
			emailAddy = emailAddy.replace( emailAddy.substring(subjectPos), '');
			
			// if mailto conatins 'body' 
			if(subject.match('&body=')) {
				
				emailbodyPos = subject.indexOf('&body=');
				emailbody = subject.substring(emailbodyPos).replace('&body=', '');
				
				subject = subject.replace( subject.substring(emailbodyPos), '');
			}
		}
			
		// if mailto conatins 'body' before subject (if subject at all)	
		if(emailAddy.match('&body=')) {
			
			emailbodyPos = emailAddy.indexOf('&body=');
			emailbody = emailAddy.substring(emailbodyPos).replace('&body=', '');
			
			emailAddy = emailAddy.replace( emailAddy.substring(emailbodyPos), '');
			
			// if body conatins 'subject' 
			if(emailbody.match('&subject=')) {
				
				subjectPos = emailbody.indexOf('&subject=');
				subject = emailbody.substring(subjectPos).replace('&subject=', '');
				
				emailbody = emailbody.replace( emailbody.substring(emailbodyPos), '');
			}
		}		
		
		//alert( emailAddy );
		//alert( subject );
		//alert( emailbody );
		
		// pop box 
		popSendToFriend(emailAddy, subject, emailbody, a_obj)
			
	}
	else {
		alert('Failed to send email');
	}
}


/**
 * Pop javascript send to friend box! 
 */
var makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable;
function popSendToFriend(emailAddy, subject, emailbody, a_obj, makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable) {
	
	if(a_obj==undefined) a_obj = '';
	
	if(makeEmailInputEditable==undefined) makeEmailInputEditable = true;
	if(makeSubjectInputEditable==undefined) makeSubjectInputEditable = true;
	if(makeBodyInputEditable==undefined) makeBodyInputEditable = true;
	
	 var oDiv = document.createElement('div');
	 
	 var randomnumber = Math.floor(Math.random()*11);
	 
	 var oinner_part_content = '';
	 
	// Email To (show as text or editable)
	 if(!makeEmailInputEditable) {
		 oinner_part_content += ' <span> <strong>Send to:</strong> ' + emailAddy + ' </span> <br /> ';
	 }
	 else {
		 oinner_part_content += ' <span> <strong>Send to:</strong> \
		 	<input type="text" name="email_input_part" id="email_input_part" value="' + emailAddy + '" /> \
			</span> <br /> ';
	 }
	 
	// Email Subject (show as text or editable)
	 if(!makeSubjectInputEditable) {
		 oinner_part_content += ' <span> <strong>Subject:</strong> ' + subject + ' </span> <br /> ';
	 }
	 else {
		 oinner_part_content += ' <span> <strong>Subject:</strong> \
		 	<input type="text" name="subject_input_part" id="subject_input_part" value="' + subject + '" /> \
			</span> <br /> ';
	 }
	
	// Email Body (show as text or editable)
	 if(!makeBodyInputEditable) {
		 oinner_part_content += ' <span> <strong>Body:</strong> ' + emailbody + ' </span> <br /> ';
	 }
	 else {
		 oinner_part_content += ' <span> <strong>Body:</strong> \
		 	<textarea name="body_input_part" id="body_input_part">' + emailbody + '</textarea> \
			</span> <br /> ';
	 }
	 
	 oDiv.id = 'email_popup_box_' + randomnumber;
	 
	 oDiv.innerHTML = "<div id='email_popup_box_back'> &nbsp; </div>";
	 oDiv.innerHTML += '<div id=\'email_popup_box_main_pos\'> \
	 						 <div id=\'email_popup_box_main\'> \
							 <form onsubmit="void(0); return false;"> \
							 <span class="email_close_button" onclick="popSendToFriendClose(\'' + oDiv.id + '\');"> <img src=\"images/glossy_icons/cancel.png\" width=\"20\" height=\"20\" /> </span>\
							 \
							 <h3> Send To A Friend. </h3>\
							 \
							 <p> <em> You can send to multiple emails by seperating each one with a semi-colon followed by a space E.g."email@address.com; another@email.com" </em> </p> \
							 \
							 <span> <strong>From:</strong> \
		 	<input type="text" name="email_input_from" id="email_input_from" value="YOUR EMAIL" /> \
			</span> <br /> ' + oinner_part_content + ' <input type="submit" value="Send" onClick="sendTheToFriendemail(\'' + oDiv.id + '\'); return false;" /> \
	 		</form> </div> </div> ';
	 
	 //if(a_obj == '') document.body.appendChild(oDiv);
	 //else insertAfter( oDiv, a_obj );
	 
	  document.body.appendChild(oDiv);
	 
	 setTimeout(function() {
	 	
		new Effect.Opacity('email_popup_box_back', { from: 0, to: 0.7, duration: 0.5, queue: 'end' });
	
		 setTimeout(function() {
			new Effect.Opacity('email_popup_box_main_pos', { from: 0, to: 1.1, duration: 0.5 });
			new Effect.SlideDown('email_popup_box_main_pos', { duration: 0.5 });	
		 }, 1000);
	 }, 200);
}

/**
 * Close pop-up send to friend box 
 */
function sendTheToFriendemail(sendBoxId) {
	
	var sendEmailUrl = "pages/send_to_friend.php"; 
	var email_input_part = $('email_input_part').value; 
	var subject_input_part = $('subject_input_part').value; 
	var body_input_part = $('body_input_part').value; 
	var email_input_from = $('email_input_from').value; 
	
	new Ajax.Request(sendEmailUrl, {
		method:'POST',
		parameters: { 
			email_part: email_input_part, 
			subject_part: subject_input_part, 
			body_part: body_input_part ,
			email_from: email_input_from
		}, 
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  alert(response);
		},
		onFailure: function(){ alert('Sorry, but there was an error with your request? Please try again.') }
    });
	
	// close Box 
	popSendToFriendClose(sendBoxId); 
}

//** DOM SHORTCUT FUNCTION **
function insertAfter(newChild, refChild) { 
 	refChild.parentNode.insertBefore(newChild, refChild.nextSibling); 
} 

/**
 * Close pop-up send to friend box 
 */
var sendBoxId;
function popSendToFriendClose(sendBoxId) {
	 	
	$('email_popup_box_back').fade();
	$('email_popup_box_main_pos').fade();
	 
	 setTimeout(function() {
		document.body.removeChild( document.getElementById(sendBoxId) ); 
	 }, 600);
}