function showObject(sw,obj) {
	var elt = document.getElementById(obj);
	if(elt != null) {
		elt.className = sw ? 'showit' : 'hideit';
	}
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Mark O'Sullivan :: http://lussumo.com/
 Jeff Larson :: http://www.jeffothy.com/
 Mark Percival :: http://webchicanery.com/ */
function copyToClipboard(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="http://www.scrapbook.com/javascript/_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}

function copyAffiliateLinkToClipboard() {
	var obj = document.getElementById('embedAffiliateLinkId');
	copyToClipboard(obj.value.replace(/\|/g,"\n"));
	obj = document.getElementById('copyAffiliateText');
	showObject(true,"copyAffiliateText");
}

function copyEmbedToClipboard() {
	var id = document.getElementById('embedImgId').value;
	if(document.embedForm.embedType[0].checked) {
		if(document.getElementById('embedChgInfo').checked) {
			var obj = document.getElementById('embedTextInfoControl'+id);
			obj.select();
		}
		else {
			var obj = document.getElementById('embedTextControl'+id);
			obj.select();
		}
	}
	else {
		if(document.getElementById('embedChgInfo').checked) {
			var obj = document.getElementById('embedBBCodeInfoControlPipe'+id);
			document.getElementById('embedBBCodeInfoControl'+id).select();
		}
		else {
			var obj = document.getElementById('embedBBCodeControl'+id);
			obj.select();
		}
	}
	copyToClipboard(obj.value.replace(/\|/g,"\n"));
	obj = document.getElementById('copyText');
	showObject(true,"copyText");
}

function changeEmbedType() {
	var id = document.getElementById('embedImgId').value;
	changeEmbed(id);
	return;
}

function changeInfo() {
	var id = document.getElementById('embedImgId').value;
	changeEmbed(id);
	showObject(false,"copyText");
}

function changeEmbed(id) {
	document.getElementById('embedImgId').value = id;
	for(var i=0;i<=4;i++) {
		if(i==id) {
			if(document.embedForm.embedType[0].checked) {
				if(document.getElementById('embedChgInfo').checked) {
					showObject(false,"embedText"+i);
					showObject(true,"embedTextInfo"+i);
					showObject(false,"embedBBCode"+i);
					showObject(false,"embedBBCodeInfo"+i);
					showObject(true,"embedImgInfo");
				}
				else {
					showObject(true,"embedText"+i);
					showObject(false,"embedTextInfo"+i);
					showObject(false,"embedBBCode"+i);
					showObject(false,"embedBBCodeInfo"+i);
					showObject(false,"embedImgInfo");
				}
			}
			else {
				if(document.getElementById('embedChgInfo').checked) {
					showObject(false,"embedText"+i);
					showObject(false,"embedTextInfo"+i);
					showObject(false,"embedBBCode"+i);
					showObject(true,"embedBBCodeInfo"+i);
					showObject(true,"embedImgInfo");
				}
				else {
					showObject(false,"embedText"+i);
					showObject(false,"embedTextInfo"+i);
					showObject(true,"embedBBCode"+i);
					showObject(false,"embedBBCodeInfo"+i);
					showObject(false,"embedImgInfo");
				}
			}
			showObject(true,"embedImg"+i);
		}
		else {
			showObject(false,"embedText"+i);
			showObject(false,"embedTextInfo"+i);
			showObject(false,"embedBBCode"+i);
			showObject(false,"embedBBCodeInfo"+i);
			showObject(false,"embedImg"+i);
		}
	}
	showObject(false,"copyText");
}

function grow() {
	if (this.scrollHeight > this.clientHeight && !window.opera) // opera=bad!
		this.rows += 1;
}

function growInit(obj) {
	if(obj.onkeypress==null) obj.onkeypress = grow;
}

function printEmailerErrors(errors) {
	var text = '';
	var html = '<font color="#ff0000"><ul>';
	for(var i=0; i<errors.length; i++) {
		text += errors[i] + "\n";
		html += '<li>' + errors[i] + '</li>';
	}
	html += '</ul></font>';
	var obj = document.getElementById('emailerMsgSection');
	obj.innerHTML = html;
	alert(text);
}

function stringTrim(s) {
	var l=0;
	var r=s.length-1;
	while(l < s.length && s[l] == ' ') l++;
	while(r > l && s[r] == ' ') r-=1;
	return s.substring(l, r+1);
}

function sendEmail(sku) {
	var errors = [];
	var idx = 0;
	var recipients = stringTrim(document.emailer.emailRecipients.value);
	if(recipients.length == 0) errors[idx++] = "Please add your recipients names/addresses";
	var subject = stringTrim(document.emailer.emailSubject.value);
	if(subject.length == 0) errors[idx++] = "Please include a subject";
	var body = stringTrim(document.emailer.emailBody.value);
	if(body.length == 0) errors[idx++] = "Please include a message in your email";
	if(idx>0) {
		printEmailerErrors(errors);
	} else {
		if(confirm('Send Email?')) {
			var e=document.createElement('script');
			e.src = 'http://www.scrapbook.com/myplace/emailer.php?m=ajaxsend&t=STORE&i='+sku+'&r='+encodeURIComponent(recipients)+'&s='+encodeURIComponent(subject)+'&b='+encodeURIComponent(body);
			document.body.appendChild(e);
		}
	}
}

function ajax_mailer_errors(errs) {
	printEmailerErrors(errs);
}

function ajax_mailer_success() {
	showObject(false,'emailerEntry');
	document.getElementById('emailerMsgSection').innerHTML = '<font color="#ff0000"><br><b>Email Sent Successfully</b></font>';
}