// 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');
	}
}

// ** URL encode function ** 
function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

// ** URL decode function ** 
function urldecode(str) {
	return unescape(str.replace(/\+/g, ' ')); 
}

/**
 * Pop javascript send to friend box! 
 */
var makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable, mainEmailOverallTitle, addInExtraFieldName, sendOnToThanksPage, sendOnToThanksPageText;
function popSendToFriend(emailAddy, subject, emailbody, a_obj, makeEmailInputEditable, makeSubjectInputEditable, makeBodyInputEditable, mainEmailOverallTitle, addInExtraFieldName, sendOnToThanksPage) {
	
	subject = urldecode(subject);
	
	if(a_obj==undefined) a_obj = '';
	
		// If IE 6, and hide all drop down menus on-open (they showed over the top of the modal), re-show them on close
		if(isRunningIE6OrBelow) {
			$$('select').each(function(el) {
				  el.style.display = "none";
			});
		}
	
	if(mainEmailOverallTitle==undefined) mainEmailOverallTitle = 'Send To A Friend.';
	if(mainEmailOverallTitle=='') mainEmailOverallTitle = 'Send To A Friend.';
	
	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 = '';
	 
	 // Added Optional Extra Part
	 if(addInExtraFieldName!=undefined) {
		 oinner_part_content += ' <span> <strong>' + addInExtraFieldName + ':</strong> \
		 	<input type="text" name="email_extra_part" id="email_extra_part" value="" /> \
			<input type="hidden" name="email_extra_part" id="email_extra_part_name" value="' + addInExtraFieldName + '" /> \
			</span> <br /> ';
	 }
	 
	 if(sendOnToThanksPage) sendOnToThanksPageText = "true";
	 else sendOnToThanksPageText = "false";
	 
	// Email To (show as text or editable)
	 if(!makeEmailInputEditable) {
		 oinner_part_content += ' <span> <strong>Send to:</strong> ' + emailAddy + ' </span> <br /> \
		 	<input type="hidden" name="email_input_part" id="email_input_part" value="' + emailAddy + '" />';
	 }
	 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 /> \
		 	<input type="hidden" name="subject_input_part" id="subject_input_part" value="' + subject + '" />';
	 }
	 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 /> \
		 	<input type="hidden" name="body_input_part" id="body_input_part" value="' + subject + '" />';
	 }
	 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;
	 
	 var addedLineAboutMultiSend = '';
	 if(makeEmailInputEditable) {
		 addedLineAboutMultiSend = '<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>';
	 }
	 
	 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\" alt=\"Close\" /> </span>\
							 \
							 <h3> ' + mainEmailOverallTitle + ' </h3>\
							 \
							 '+ addedLineAboutMultiSend +' \
							 \
							 <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 + '\', ' + sendOnToThanksPageText + '); 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, sendOnToThanksPage) {
	
	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; 
	var email_extra_part = ($('email_extra_part')) ? $('email_extra_part_name').value : ''; 
	var email_extra_part_name = ($('email_extra_part_name')) ? $('email_extra_part_name').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,
			email_extra_part: email_extra_part,
			email_extra_part_name: email_extra_part_name
		}, 
		onSuccess: function(transport){
		  var response = transport.responseText || "no response text";
		  alert(response);
		  
		  if(sendOnToThanksPage) window.location = "thanks.php";
		},
		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) ); 
		// If IE 6, re-open the closed select 
		if(isRunningIE6OrBelow) {
			$$('select').each(function(el) {
				  el.style.display = "inline";
			});
		}
	 }, 600);
	
}